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 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 10 | #include "GrBackendSemaphore.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 11 | #include "GrBackendSurface.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 12 | #include "GrContextOptions.h" |
| 13 | #include "GrGeometryProcessor.h" |
| 14 | #include "GrGpuResourceCacheAccess.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 15 | #include "GrMesh.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 16 | #include "GrPipeline.h" |
| 17 | #include "GrRenderTargetPriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | #include "GrTexturePriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | |
| 20 | #include "GrVkCommandBuffer.h" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 21 | #include "GrVkGpuCommandBuffer.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | #include "GrVkImage.h" |
| 23 | #include "GrVkIndexBuffer.h" |
| 24 | #include "GrVkMemory.h" |
| 25 | #include "GrVkPipeline.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 26 | #include "GrVkPipelineState.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | #include "GrVkRenderPass.h" |
| 28 | #include "GrVkResourceProvider.h" |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 29 | #include "GrVkSemaphore.h" |
Greg Daniel | c2dd5ed | 2017-05-05 13:49:11 -0400 | [diff] [blame] | 30 | #include "GrVkTexelBuffer.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 31 | #include "GrVkTexture.h" |
| 32 | #include "GrVkTextureRenderTarget.h" |
| 33 | #include "GrVkTransferBuffer.h" |
| 34 | #include "GrVkVertexBuffer.h" |
| 35 | |
Matt Sarett | 485c499 | 2017-02-14 14:18:27 -0500 | [diff] [blame] | 36 | #include "SkConvertPixels.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 37 | #include "SkMipMap.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | |
| 39 | #include "vk/GrVkInterface.h" |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 40 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 42 | #include "SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | |
Forrest Reiling | 44f8571 | 2017-03-27 23:22:20 -0700 | [diff] [blame] | 44 | #if !defined(SK_BUILD_FOR_WIN) |
| 45 | #include <unistd.h> |
| 46 | #endif // !defined(SK_BUILD_FOR_WIN) |
| 47 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 48 | #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) |
| 49 | #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) |
| 50 | #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) |
| 51 | |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 52 | #ifdef SK_ENABLE_VK_LAYERS |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 53 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback( |
| 54 | VkDebugReportFlagsEXT flags, |
| 55 | VkDebugReportObjectTypeEXT objectType, |
| 56 | uint64_t object, |
| 57 | size_t location, |
| 58 | int32_t messageCode, |
| 59 | const char* pLayerPrefix, |
| 60 | const char* pMessage, |
| 61 | void* pUserData) { |
| 62 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
| 63 | SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
Jim Van Verth | ce3fe23 | 2016-11-11 09:41:04 -0800 | [diff] [blame] | 64 | return VK_TRUE; // skip further layers |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 65 | } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
| 66 | SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 67 | } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { |
| 68 | SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 69 | } else { |
| 70 | SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 71 | } |
| 72 | return VK_FALSE; |
| 73 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 76 | sk_sp<GrGpu> GrVkGpu::Make(GrBackendContext backendContext, const GrContextOptions& options, |
| 77 | GrContext* context) { |
| 78 | const auto* backend = reinterpret_cast<const GrVkBackendContext*>(backendContext); |
| 79 | return Make(sk_ref_sp(backend), options, context); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 82 | sk_sp<GrGpu> GrVkGpu::Make(sk_sp<const GrVkBackendContext> backendContext, |
| 83 | const GrContextOptions& options, GrContext* context) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 84 | if (!backendContext) { |
Brian Salomon | c188982 | 2017-04-24 16:49:05 -0400 | [diff] [blame] | 85 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 88 | if (!backendContext->fInterface->validate(backendContext->fExtensions)) { |
Greg Daniel | fe2965a | 2016-10-11 16:31:05 -0400 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| 91 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 92 | return sk_sp<GrGpu>(new GrVkGpu(context, options, std::move(backendContext))); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | //////////////////////////////////////////////////////////////////////////////// |
| 96 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 97 | GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options, |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 98 | sk_sp<const GrVkBackendContext> backendCtx) |
| 99 | : INHERITED(context) |
| 100 | , fBackendContext(std::move(backendCtx)) |
| 101 | , fDevice(fBackendContext->fDevice) |
| 102 | , fQueue(fBackendContext->fQueue) |
| 103 | , fResourceProvider(this) |
| 104 | , fDisconnected(false) { |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 105 | #ifdef SK_ENABLE_VK_LAYERS |
brianosman | 419ca64 | 2016-05-04 08:19:44 -0700 | [diff] [blame] | 106 | fCallback = VK_NULL_HANDLE; |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 107 | if (fBackendContext->fExtensions & kEXT_debug_report_GrVkExtensionFlag) { |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 108 | // Setup callback creation information |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 109 | VkDebugReportCallbackCreateInfoEXT callbackCreateInfo; |
| 110 | callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 111 | callbackCreateInfo.pNext = nullptr; |
| 112 | callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | |
egdaniel | ef0c10c | 2016-04-07 07:51:22 -0700 | [diff] [blame] | 113 | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 114 | //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | |
| 115 | //VK_DEBUG_REPORT_DEBUG_BIT_EXT | |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 116 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 117 | callbackCreateInfo.pfnCallback = &DebugReportCallback; |
| 118 | callbackCreateInfo.pUserData = nullptr; |
| 119 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 120 | // Register the callback |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 121 | GR_VK_CALL_ERRCHECK(this->vkInterface(), |
| 122 | CreateDebugReportCallbackEXT(fBackendContext->fInstance, |
| 123 | &callbackCreateInfo, nullptr, &fCallback)); |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 124 | } |
| 125 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 126 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 127 | fCompiler = new SkSL::Compiler(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 128 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 129 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), fBackendContext->fPhysicalDevice, |
| 130 | fBackendContext->fFeatures, fBackendContext->fExtensions)); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 131 | fCaps.reset(SkRef(fVkCaps.get())); |
| 132 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 133 | VK_CALL(GetPhysicalDeviceMemoryProperties(fBackendContext->fPhysicalDevice, &fPhysDevMemProps)); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 134 | |
| 135 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 136 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 137 | nullptr, // pNext |
| 138 | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | |
| 139 | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 140 | fBackendContext->fGraphicsQueueIndex, // queueFamilyIndex |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 141 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 142 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 143 | &fCmdPool)); |
| 144 | |
| 145 | // must call this after creating the CommandPool |
| 146 | fResourceProvider.init(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 147 | fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 148 | SkASSERT(fCurrentCmdBuffer); |
| 149 | fCurrentCmdBuffer->begin(this); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 150 | |
| 151 | // set up our heaps |
| 152 | fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024)); |
Greg Daniel | 4f7d97c | 2017-04-26 15:10:00 -0400 | [diff] [blame] | 153 | fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 64*1024*1024)); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 154 | fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024)); |
| 155 | fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
| 156 | fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 157 | fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024)); |
Greg Daniel | c2dd5ed | 2017-05-05 13:49:11 -0400 | [diff] [blame] | 158 | fHeaps[kTexelBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 159 | fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
| 160 | fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 163 | void GrVkGpu::destroyResources() { |
| 164 | if (fCurrentCmdBuffer) { |
| 165 | fCurrentCmdBuffer->end(this); |
| 166 | fCurrentCmdBuffer->unref(this); |
| 167 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 168 | |
| 169 | // wait for all commands to finish |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 170 | fResourceProvider.checkCommandBuffers(); |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 171 | VkResult res = VK_CALL(QueueWaitIdle(fQueue)); |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 172 | |
| 173 | // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences |
| 174 | // on the command buffers even though they have completed. This causes an assert to fire when |
| 175 | // 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] | 176 | // sleep to make sure the fence signals. |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 177 | #ifdef SK_DEBUG |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 178 | if (this->vkCaps().mustSleepOnTearDown()) { |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 179 | #if defined(SK_BUILD_FOR_WIN) |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 180 | Sleep(10); // In milliseconds |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 181 | #else |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 182 | sleep(1); // In seconds |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 183 | #endif |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 184 | } |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 185 | #endif |
| 186 | |
egdaniel | be9d821 | 2016-09-20 08:54:23 -0700 | [diff] [blame] | 187 | #ifdef SK_DEBUG |
Greg Daniel | 8a8668b | 2016-10-31 16:34:42 -0400 | [diff] [blame] | 188 | SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res); |
egdaniel | be9d821 | 2016-09-20 08:54:23 -0700 | [diff] [blame] | 189 | #endif |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 190 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 191 | for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) { |
| 192 | fSemaphoresToWaitOn[i]->unref(this); |
| 193 | } |
| 194 | fSemaphoresToWaitOn.reset(); |
| 195 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 196 | for (int i = 0; i < fSemaphoresToSignal.count(); ++i) { |
| 197 | fSemaphoresToSignal[i]->unref(this); |
| 198 | } |
| 199 | fSemaphoresToSignal.reset(); |
| 200 | |
| 201 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 202 | fCopyManager.destroyResources(this); |
| 203 | |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 204 | // must call this just before we destroy the command pool and VkDevice |
| 205 | fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 206 | |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 207 | if (fCmdPool != VK_NULL_HANDLE) { |
| 208 | VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); |
| 209 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 210 | |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 211 | #ifdef SK_ENABLE_VK_LAYERS |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 212 | if (fCallback) { |
| 213 | VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr)); |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 214 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 215 | #endif |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 216 | |
| 217 | } |
| 218 | |
| 219 | GrVkGpu::~GrVkGpu() { |
| 220 | if (!fDisconnected) { |
| 221 | this->destroyResources(); |
| 222 | } |
| 223 | delete fCompiler; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void GrVkGpu::disconnect(DisconnectType type) { |
| 228 | INHERITED::disconnect(type); |
| 229 | if (!fDisconnected) { |
| 230 | if (DisconnectType::kCleanup == type) { |
| 231 | this->destroyResources(); |
| 232 | } else { |
| 233 | fCurrentCmdBuffer->unrefAndAbandon(); |
| 234 | for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) { |
| 235 | fSemaphoresToWaitOn[i]->unrefAndAbandon(); |
| 236 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 237 | for (int i = 0; i < fSemaphoresToSignal.count(); ++i) { |
| 238 | fSemaphoresToSignal[i]->unrefAndAbandon(); |
| 239 | } |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 240 | fCopyManager.abandonResources(); |
| 241 | |
| 242 | // must call this just before we destroy the command pool and VkDevice |
| 243 | fResourceProvider.abandonResources(); |
| 244 | } |
| 245 | fSemaphoresToWaitOn.reset(); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 246 | fSemaphoresToSignal.reset(); |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 247 | #ifdef SK_ENABLE_VK_LAYERS |
| 248 | fCallback = VK_NULL_HANDLE; |
| 249 | #endif |
| 250 | fCurrentCmdBuffer = nullptr; |
| 251 | fCmdPool = VK_NULL_HANDLE; |
| 252 | fDisconnected = true; |
| 253 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /////////////////////////////////////////////////////////////////////////////// |
| 257 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 258 | GrGpuRTCommandBuffer* GrVkGpu::createCommandBuffer( |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 259 | GrRenderTarget* rt, GrSurfaceOrigin origin, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 260 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
| 261 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) { |
| 262 | return new GrVkGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo); |
| 263 | } |
| 264 | |
| 265 | GrGpuTextureCommandBuffer* GrVkGpu::createCommandBuffer(GrTexture* texture, |
| 266 | GrSurfaceOrigin origin) { |
| 267 | return new GrVkGpuTextureCommandBuffer(this, texture, origin); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 270 | void GrVkGpu::submitCommandBuffer(SyncQueue sync) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 271 | SkASSERT(fCurrentCmdBuffer); |
| 272 | fCurrentCmdBuffer->end(this); |
| 273 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 274 | fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 275 | |
| 276 | for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) { |
| 277 | fSemaphoresToWaitOn[i]->unref(this); |
| 278 | } |
| 279 | fSemaphoresToWaitOn.reset(); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 280 | for (int i = 0; i < fSemaphoresToSignal.count(); ++i) { |
| 281 | fSemaphoresToSignal[i]->unref(this); |
| 282 | } |
| 283 | fSemaphoresToSignal.reset(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 284 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 285 | fResourceProvider.checkCommandBuffers(); |
| 286 | |
| 287 | // Release old command buffer and create a new one |
| 288 | fCurrentCmdBuffer->unref(this); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 289 | fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 290 | SkASSERT(fCurrentCmdBuffer); |
| 291 | |
| 292 | fCurrentCmdBuffer->begin(this); |
| 293 | } |
| 294 | |
| 295 | /////////////////////////////////////////////////////////////////////////////// |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 296 | GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern, |
| 297 | const void* data) { |
| 298 | GrBuffer* buff; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 299 | switch (type) { |
| 300 | case kVertex_GrBufferType: |
| 301 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 302 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 303 | buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 304 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 305 | case kIndex_GrBufferType: |
| 306 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 307 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 308 | buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 309 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 310 | case kXferCpuToGpu_GrBufferType: |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 311 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 312 | kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 313 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 314 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 315 | case kXferGpuToCpu_GrBufferType: |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 316 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 317 | kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 318 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 319 | break; |
Greg Daniel | c2dd5ed | 2017-05-05 13:49:11 -0400 | [diff] [blame] | 320 | case kTexel_GrBufferType: |
Chris Dalton | 6b65b98 | 2017-07-06 11:04:00 -0600 | [diff] [blame] | 321 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 322 | kStatic_GrAccessPattern == accessPattern); |
| 323 | buff = GrVkTexelBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
Greg Daniel | c2dd5ed | 2017-05-05 13:49:11 -0400 | [diff] [blame] | 324 | break; |
| 325 | case kDrawIndirect_GrBufferType: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 326 | SK_ABORT("DrawIndirect Buffers not supported in vulkan backend."); |
Greg Daniel | c2dd5ed | 2017-05-05 13:49:11 -0400 | [diff] [blame] | 327 | return nullptr; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 328 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 329 | SK_ABORT("Unknown buffer type."); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 330 | return nullptr; |
| 331 | } |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 332 | if (data && buff) { |
| 333 | buff->updateData(data, size); |
| 334 | } |
| 335 | return buff; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | //////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 339 | bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin, int width, |
| 340 | int height, GrColorType srcColorType, |
| 341 | DrawPreference* drawPreference, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 342 | WritePixelTempDrawInfo* tempDrawInfo) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 343 | // We don't want to introduce a sRGB conversion if we trigger a draw. |
| 344 | auto srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(dstSurface->config()); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 345 | if (*drawPreference != kNoDraw_DrawPreference) { |
| 346 | // We assume the base class has only inserted a draw for sRGB reasons. So the temp surface |
| 347 | // has the config of the original src data. There is no swizzling nor src config spoofing. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 348 | SkASSERT(tempDrawInfo->fWriteColorType == srcColorType); |
| 349 | SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == srcColorType); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 350 | SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 351 | // Don't undo a sRGB conversion introduced by our caller via an intermediate draw. |
| 352 | srcConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 353 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 354 | if (GrColorTypeIsAlphaOnly(srcColorType)) { |
| 355 | srcConfigSRGBEncoded = GrSRGBEncoded::kNo; |
| 356 | } |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 357 | GrRenderTarget* renderTarget = dstSurface->asRenderTarget(); |
| 358 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 359 | if (GrPixelConfigToColorType(dstSurface->config()) == srcColorType) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 360 | // We only support writing pixels to textures. Forcing a draw lets us write to pure RTs. |
| 361 | if (!dstSurface->asTexture()) { |
| 362 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 363 | } |
| 364 | // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target. |
| 365 | if (renderTarget && renderTarget->numColorSamples() > 1) { |
| 366 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 367 | } |
| 368 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 371 | // Any color type change requires a draw |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 372 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 373 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 374 | auto srcAsConfig = GrColorTypeToPixelConfig(srcColorType, srcConfigSRGBEncoded); |
| 375 | SkASSERT(srcAsConfig != kUnknown_GrPixelConfig); |
| 376 | bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcAsConfig) == dstSurface->config(); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 377 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 378 | if (!this->vkCaps().isConfigTexturable(srcAsConfig) && configsAreRBSwaps) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 379 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
| 380 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 381 | tempDrawInfo->fWriteColorType = GrPixelConfigToColorType(dstSurface->config()); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 382 | } |
| 383 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 386 | bool GrVkGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, |
| 387 | int width, int height, GrColorType srcColorType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 388 | const GrMipLevel texels[], int mipLevelCount) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 389 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture()); |
| 390 | if (!vkTex) { |
| 391 | return false; |
| 392 | } |
| 393 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 394 | // Make sure we have at least the base level |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 395 | if (!mipLevelCount || !texels[0].fPixels) { |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 396 | return false; |
| 397 | } |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 398 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 399 | bool success = false; |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 400 | bool linearTiling = vkTex->isLinearTiled(); |
| 401 | if (linearTiling) { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 402 | if (mipLevelCount > 1) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 403 | SkDebugf("Can't upload mipmap data to linear tiled texture"); |
| 404 | return false; |
| 405 | } |
| 406 | if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) { |
| 407 | // Need to change the layout to general in order to perform a host write |
| 408 | vkTex->setImageLayout(this, |
| 409 | VK_IMAGE_LAYOUT_GENERAL, |
| 410 | VK_ACCESS_HOST_WRITE_BIT, |
| 411 | VK_PIPELINE_STAGE_HOST_BIT, |
| 412 | false); |
| 413 | this->submitCommandBuffer(kForce_SyncQueue); |
| 414 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 415 | success = this->uploadTexDataLinear(vkTex, origin, left, top, width, height, srcColorType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 416 | texels[0].fPixels, texels[0].fRowBytes); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 417 | } else { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 418 | int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1; |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 419 | if (mipLevelCount > currentMipLevels) { |
| 420 | if (!vkTex->reallocForMipmap(this, mipLevelCount)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 421 | return false; |
| 422 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 423 | } |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 424 | success = this->uploadTexDataOptimal(vkTex, origin, left, top, width, height, srcColorType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 425 | texels, mipLevelCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 426 | } |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 427 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 428 | return success; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 431 | bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height, |
| 432 | GrColorType bufferColorType, GrBuffer* transferBuffer, |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 433 | size_t bufferOffset, size_t rowBytes) { |
| 434 | // Vulkan only supports 4-byte aligned offsets |
| 435 | if (SkToBool(bufferOffset & 0x2)) { |
| 436 | return false; |
| 437 | } |
| 438 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture); |
| 439 | if (!vkTex) { |
| 440 | return false; |
| 441 | } |
| 442 | GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer); |
| 443 | if (!vkBuffer) { |
| 444 | return false; |
| 445 | } |
| 446 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 447 | SkDEBUGCODE( |
| 448 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 449 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 450 | SkASSERT(bounds.contains(subRect)); |
| 451 | ) |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 452 | int bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 453 | if (rowBytes == 0) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 454 | rowBytes = bpp * width; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Set up copy region |
| 458 | VkBufferImageCopy region; |
| 459 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 460 | region.bufferOffset = bufferOffset; |
| 461 | region.bufferRowLength = (uint32_t)(rowBytes/bpp); |
| 462 | region.bufferImageHeight = 0; |
| 463 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 464 | region.imageOffset = { left, top, 0 }; |
| 465 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 466 | |
| 467 | // Change layout of our target so it can be copied to |
| 468 | vkTex->setImageLayout(this, |
| 469 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 470 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 471 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 472 | false); |
| 473 | |
| 474 | // Copy the buffer to the image |
| 475 | fCurrentCmdBuffer->copyBufferToImage(this, |
| 476 | vkBuffer, |
| 477 | vkTex, |
| 478 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 479 | 1, |
| 480 | ®ion); |
| 481 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 482 | vkTex->texturePriv().markMipMapsDirty(); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 483 | return true; |
| 484 | } |
| 485 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 486 | void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect, |
| 487 | const SkIPoint& dstPoint) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 488 | SkASSERT(dst); |
| 489 | SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage()); |
| 490 | |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 491 | if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) { |
| 492 | this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue); |
| 493 | } |
| 494 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 495 | VkImageResolve resolveInfo; |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 496 | resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; |
| 497 | resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0}; |
| 498 | resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; |
| 499 | resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0}; |
| 500 | resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1}; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 501 | |
Greg Daniel | bc26c39 | 2017-04-18 13:32:10 -0400 | [diff] [blame] | 502 | GrVkImage* dstImage; |
| 503 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 504 | if (dstRT) { |
| 505 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT); |
Greg Daniel | bc26c39 | 2017-04-18 13:32:10 -0400 | [diff] [blame] | 506 | dstImage = vkRT; |
| 507 | } else { |
| 508 | SkASSERT(dst->asTexture()); |
| 509 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
| 510 | } |
| 511 | dstImage->setImageLayout(this, |
| 512 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 513 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 514 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 515 | false); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 516 | |
| 517 | src->msaaImage()->setImageLayout(this, |
| 518 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 519 | VK_ACCESS_TRANSFER_READ_BIT, |
| 520 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 521 | false); |
| 522 | |
Greg Daniel | bc26c39 | 2017-04-18 13:32:10 -0400 | [diff] [blame] | 523 | fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 526 | void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) { |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 527 | if (target->needsResolve()) { |
| 528 | SkASSERT(target->numColorSamples() > 1); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 529 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target); |
| 530 | SkASSERT(rt->msaaImage()); |
Greg Daniel | 69d4992 | 2017-02-23 09:44:02 -0500 | [diff] [blame] | 531 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 532 | const SkIRect& srcRect = rt->getResolveRect(); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 533 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 534 | this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop)); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 535 | |
| 536 | rt->flagAsResolved(); |
Greg Daniel | 69d4992 | 2017-02-23 09:44:02 -0500 | [diff] [blame] | 537 | |
| 538 | if (requiresSubmit) { |
| 539 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 540 | } |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 544 | bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, GrSurfaceOrigin texOrigin, int left, int top, |
| 545 | int width, int height, GrColorType dataColorType, |
| 546 | const void* data, size_t rowBytes) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 547 | SkASSERT(data); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 548 | SkASSERT(tex->isLinearTiled()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 549 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 550 | SkDEBUGCODE( |
| 551 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 552 | SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height()); |
| 553 | SkASSERT(bounds.contains(subRect)); |
| 554 | ) |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 555 | int bpp = GrColorTypeBytesPerPixel(dataColorType); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 556 | size_t trimRowBytes = width * bpp; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 557 | if (!rowBytes) { |
| 558 | rowBytes = trimRowBytes; |
| 559 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 560 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 561 | SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() || |
| 562 | VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout()); |
| 563 | const VkImageSubresource subres = { |
| 564 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 565 | 0, // mipLevel |
| 566 | 0, // arraySlice |
| 567 | }; |
| 568 | VkSubresourceLayout layout; |
| 569 | VkResult err; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 570 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 571 | const GrVkInterface* interface = this->vkInterface(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 572 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 573 | GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 574 | tex->image(), |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 575 | &subres, |
| 576 | &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 577 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 578 | int texTop = kBottomLeft_GrSurfaceOrigin == texOrigin ? tex->height() - top - height : top; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 579 | const GrVkAlloc& alloc = tex->alloc(); |
| 580 | VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 581 | VkDeviceSize size = height*layout.rowPitch; |
| 582 | void* mapPtr; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 583 | err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 584 | if (err) { |
| 585 | return false; |
| 586 | } |
| 587 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 588 | if (kBottomLeft_GrSurfaceOrigin == texOrigin) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 589 | // copy into buffer by rows |
| 590 | const char* srcRow = reinterpret_cast<const char*>(data); |
| 591 | char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch; |
| 592 | for (int y = 0; y < height; y++) { |
| 593 | memcpy(dstRow, srcRow, trimRowBytes); |
| 594 | srcRow += rowBytes; |
| 595 | dstRow -= layout.rowPitch; |
| 596 | } |
| 597 | } else { |
Matt Sarett | cf3525a | 2017-01-24 12:43:41 -0500 | [diff] [blame] | 598 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes, |
| 599 | height); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 600 | } |
| 601 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 602 | GrVkMemory::FlushMappedAlloc(this, alloc); |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 603 | GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 604 | |
| 605 | return true; |
| 606 | } |
| 607 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 608 | bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, GrSurfaceOrigin texOrigin, int left, int top, |
| 609 | int width, int height, GrColorType dataColorType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 610 | const GrMipLevel texels[], int mipLevelCount) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 611 | SkASSERT(!tex->isLinearTiled()); |
| 612 | // The assumption is either that we have no mipmaps, or that our rect is the entire texture |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 613 | SkASSERT(1 == mipLevelCount || |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 614 | (0 == left && 0 == top && width == tex->width() && height == tex->height())); |
| 615 | |
Greg Daniel | dd20e91 | 2017-04-07 14:42:23 -0400 | [diff] [blame] | 616 | // We assume that if the texture has mip levels, we either upload to all the levels or just the |
| 617 | // first. |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 618 | SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1)); |
Greg Daniel | dd20e91 | 2017-04-07 14:42:23 -0400 | [diff] [blame] | 619 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 620 | if (width == 0 || height == 0) { |
| 621 | return false; |
| 622 | } |
| 623 | |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 624 | SkASSERT(this->caps()->isConfigTexturable(tex->config())); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 625 | int bpp = GrColorTypeBytesPerPixel(dataColorType); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 626 | |
| 627 | // texels is const. |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 628 | // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes. |
| 629 | // Because of this we need to make a non-const shallow copy of texels. |
Robert Phillips | 0f99277 | 2017-07-12 08:24:56 -0400 | [diff] [blame] | 630 | SkAutoTMalloc<GrMipLevel> texelsShallowCopy; |
| 631 | |
| 632 | if (mipLevelCount) { |
| 633 | texelsShallowCopy.reset(mipLevelCount); |
| 634 | memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel)); |
| 635 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 636 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 637 | // Determine whether we need to flip when we copy into the buffer |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 638 | bool flipY = (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 639 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 640 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 641 | individualMipOffsets.push_back(0); |
| 642 | size_t combinedBufferSize = width * bpp * height; |
| 643 | int currentWidth = width; |
| 644 | int currentHeight = height; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 645 | if (mipLevelCount > 0 && !texelsShallowCopy[0].fPixels) { |
| 646 | combinedBufferSize = 0; |
| 647 | } |
| 648 | |
Greg Daniel | 468fd63 | 2017-03-22 17:03:45 -0400 | [diff] [blame] | 649 | // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image |
| 650 | // config. This works with the assumption that the bytes in pixel config is always a power of 2. |
| 651 | SkASSERT((bpp & (bpp - 1)) == 0); |
| 652 | const size_t alignmentMask = 0x3 | (bpp - 1); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 653 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) { |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 654 | currentWidth = SkTMax(1, currentWidth/2); |
| 655 | currentHeight = SkTMax(1, currentHeight/2); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 656 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 657 | if (texelsShallowCopy[currentMipLevel].fPixels) { |
| 658 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 659 | const size_t alignmentDiff = combinedBufferSize & alignmentMask; |
| 660 | if (alignmentDiff != 0) { |
| 661 | combinedBufferSize += alignmentMask - alignmentDiff + 1; |
| 662 | } |
| 663 | individualMipOffsets.push_back(combinedBufferSize); |
| 664 | combinedBufferSize += trimmedSize; |
| 665 | } else { |
| 666 | individualMipOffsets.push_back(0); |
Greg Daniel | 468fd63 | 2017-03-22 17:03:45 -0400 | [diff] [blame] | 667 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 668 | } |
| 669 | if (0 == combinedBufferSize) { |
| 670 | // We don't actually have any data to upload so just return success |
| 671 | return true; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // allocate buffer to hold our mip data |
| 675 | GrVkTransferBuffer* transferBuffer = |
| 676 | GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type); |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 677 | if(!transferBuffer) { |
Forrest Reiling | c04f845 | 2017-04-26 19:26:12 -0700 | [diff] [blame] | 678 | return false; |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 679 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 680 | |
| 681 | char* buffer = (char*) transferBuffer->map(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 682 | SkTArray<VkBufferImageCopy> regions(mipLevelCount); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 683 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 684 | currentWidth = width; |
| 685 | currentHeight = height; |
Greg Daniel | a1b282b | 2017-03-28 14:56:46 -0400 | [diff] [blame] | 686 | int layerHeight = tex->height(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 687 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 688 | if (texelsShallowCopy[currentMipLevel].fPixels) { |
| 689 | SkASSERT(1 == mipLevelCount || currentHeight == layerHeight); |
| 690 | const size_t trimRowBytes = currentWidth * bpp; |
| 691 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes |
| 692 | ? texelsShallowCopy[currentMipLevel].fRowBytes |
| 693 | : trimRowBytes; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 694 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 695 | // copy data into the buffer, skipping the trailing bytes |
| 696 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
| 697 | const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels; |
| 698 | if (flipY) { |
| 699 | src += (currentHeight - 1) * rowBytes; |
| 700 | for (int y = 0; y < currentHeight; y++) { |
| 701 | memcpy(dst, src, trimRowBytes); |
| 702 | src -= rowBytes; |
| 703 | dst += trimRowBytes; |
| 704 | } |
| 705 | } else { |
| 706 | SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 707 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 708 | |
| 709 | VkBufferImageCopy& region = regions.push_back(); |
| 710 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 711 | region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel]; |
| 712 | region.bufferRowLength = currentWidth; |
| 713 | region.bufferImageHeight = currentHeight; |
| 714 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 }; |
| 715 | region.imageOffset = { left, flipY ? layerHeight - top - currentHeight : top, 0 }; |
| 716 | region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 }; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 717 | } |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 718 | currentWidth = SkTMax(1, currentWidth/2); |
| 719 | currentHeight = SkTMax(1, currentHeight/2); |
Greg Daniel | a1b282b | 2017-03-28 14:56:46 -0400 | [diff] [blame] | 720 | layerHeight = currentHeight; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 721 | } |
| 722 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 723 | // no need to flush non-coherent memory, unmap will do that for us |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 724 | transferBuffer->unmap(); |
| 725 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 726 | // Change layout of our target so it can be copied to |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 727 | tex->setImageLayout(this, |
| 728 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 729 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 730 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 731 | false); |
| 732 | |
| 733 | // Copy the buffer to the image |
| 734 | fCurrentCmdBuffer->copyBufferToImage(this, |
| 735 | transferBuffer, |
| 736 | tex, |
| 737 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 738 | regions.count(), |
| 739 | regions.begin()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 740 | transferBuffer->unref(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 741 | if (1 == mipLevelCount) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 742 | tex->texturePriv().markMipMapsDirty(); |
Greg Daniel | dd20e91 | 2017-04-07 14:42:23 -0400 | [diff] [blame] | 743 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 744 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 745 | return true; |
| 746 | } |
| 747 | |
| 748 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 749 | sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 750 | const GrMipLevel texels[], int mipLevelCount) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 751 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 752 | |
| 753 | VkFormat pixelFormat; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 754 | SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)); |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 755 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 756 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 757 | if (renderTarget) { |
| 758 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 759 | } |
| 760 | |
| 761 | // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and |
| 762 | // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we |
| 763 | // 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] | 764 | // 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] | 765 | // both bits, we must make sure to set the destination bit if we are uploading srcData to the |
| 766 | // texture. |
| 767 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 768 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 769 | // 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] | 770 | // 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] | 771 | // to 1. |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 772 | int mipLevels = !mipLevelCount ? 1 : mipLevelCount; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 773 | GrVkImage::ImageDesc imageDesc; |
| 774 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 775 | imageDesc.fFormat = pixelFormat; |
| 776 | imageDesc.fWidth = desc.fWidth; |
| 777 | imageDesc.fHeight = desc.fHeight; |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 778 | imageDesc.fLevels = mipLevels; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 779 | imageDesc.fSamples = 1; |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 780 | imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 781 | imageDesc.fUsageFlags = usageFlags; |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 782 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 783 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 784 | GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated; |
| 785 | if (mipLevels > 1) { |
| 786 | mipMapsStatus = GrMipMapsStatus::kValid; |
| 787 | for (int i = 0; i < mipLevels; ++i) { |
| 788 | if (!texels[i].fPixels) { |
| 789 | mipMapsStatus = GrMipMapsStatus::kDirty; |
| 790 | break; |
| 791 | } |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 795 | sk_sp<GrVkTexture> tex; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 796 | if (renderTarget) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 797 | tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 798 | imageDesc, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 799 | mipMapsStatus); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 800 | } else { |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 801 | tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 802 | mipMapsStatus); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (!tex) { |
| 806 | return nullptr; |
| 807 | } |
| 808 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 809 | auto colorType = GrPixelConfigToColorType(desc.fConfig); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 810 | if (mipLevelCount) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 811 | if (!this->uploadTexDataOptimal(tex.get(), desc.fOrigin, 0, 0, desc.fWidth, desc.fHeight, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 812 | colorType, texels, mipLevelCount)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 813 | tex->unref(); |
| 814 | return nullptr; |
| 815 | } |
| 816 | } |
| 817 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 818 | if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) { |
| 819 | VkClearColorValue zeroClearColor; |
| 820 | memset(&zeroClearColor, 0, sizeof(zeroClearColor)); |
| 821 | VkImageSubresourceRange range; |
| 822 | range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 823 | range.baseArrayLayer = 0; |
| 824 | range.baseMipLevel = 0; |
| 825 | range.layerCount = 1; |
| 826 | range.levelCount = 1; |
| 827 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 828 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 829 | this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 830 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 831 | return tex; |
| 832 | } |
| 833 | |
| 834 | //////////////////////////////////////////////////////////////////////////////// |
| 835 | |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 836 | void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset, |
| 837 | VkDeviceSize dstOffset, VkDeviceSize size) { |
| 838 | VkBufferCopy copyRegion; |
| 839 | copyRegion.srcOffset = srcOffset; |
| 840 | copyRegion.dstOffset = dstOffset; |
| 841 | copyRegion.size = size; |
| 842 | fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, ©Region); |
| 843 | } |
| 844 | |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 845 | bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src, |
| 846 | VkDeviceSize offset, VkDeviceSize size) { |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 847 | // Update the buffer |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 848 | fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src); |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 849 | |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | //////////////////////////////////////////////////////////////////////////////// |
| 854 | |
Greg Daniel | 5254ccc | 2017-11-13 11:05:52 -0500 | [diff] [blame] | 855 | static bool check_backend_texture(const GrBackendTexture& backendTex, |
| 856 | GrPixelConfig config) { |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 857 | const GrVkImageInfo* info = backendTex.getVkImageInfo(); |
| 858 | if (!info) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 859 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 860 | } |
| 861 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 862 | if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 863 | return false; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 864 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 865 | |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 866 | SkASSERT(GrVkFormatPixelConfigPairIsValid(info->fFormat, config)); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 867 | return true; |
| 868 | } |
| 869 | |
| 870 | sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 871 | GrWrapOwnership ownership) { |
Greg Daniel | 5254ccc | 2017-11-13 11:05:52 -0500 | [diff] [blame] | 872 | if (!check_backend_texture(backendTex, backendTex.config())) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 873 | return nullptr; |
| 874 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 875 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 876 | GrSurfaceDesc surfDesc; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 877 | surfDesc.fFlags = kNone_GrSurfaceFlags; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 878 | surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // Not actually used in the following |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 879 | surfDesc.fWidth = backendTex.width(); |
| 880 | surfDesc.fHeight = backendTex.height(); |
| 881 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 882 | surfDesc.fSampleCnt = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 883 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 884 | return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, backendTex.getVkImageInfo()); |
| 885 | } |
| 886 | |
| 887 | sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 888 | int sampleCnt, |
| 889 | GrWrapOwnership ownership) { |
Greg Daniel | 5254ccc | 2017-11-13 11:05:52 -0500 | [diff] [blame] | 890 | if (!check_backend_texture(backendTex, backendTex.config())) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 891 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 892 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 893 | |
| 894 | GrSurfaceDesc surfDesc; |
| 895 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 896 | surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 897 | surfDesc.fWidth = backendTex.width(); |
| 898 | surfDesc.fHeight = backendTex.height(); |
| 899 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 900 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config()); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 901 | |
| 902 | return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership, |
| 903 | backendTex.getVkImageInfo()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 904 | } |
| 905 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 906 | sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){ |
Greg Daniel | e79b473 | 2017-04-20 14:07:46 -0400 | [diff] [blame] | 907 | // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In |
| 908 | // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if |
| 909 | // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle |
| 910 | // creating and owning the MSAA images. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 911 | if (backendRT.sampleCnt() > 1) { |
Greg Daniel | e79b473 | 2017-04-20 14:07:46 -0400 | [diff] [blame] | 912 | return nullptr; |
| 913 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 914 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 915 | const GrVkImageInfo* info = backendRT.getVkImageInfo(); |
| 916 | if (!info) { |
| 917 | return nullptr; |
| 918 | } |
Brian Osman | 0b791f5 | 2017-03-10 08:30:22 -0500 | [diff] [blame] | 919 | if (VK_NULL_HANDLE == info->fImage) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 920 | return nullptr; |
| 921 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 922 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 923 | GrSurfaceDesc desc; |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 924 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 925 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 926 | desc.fWidth = backendRT.width(); |
| 927 | desc.fHeight = backendRT.height(); |
| 928 | desc.fConfig = backendRT.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 929 | desc.fSampleCnt = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 930 | |
Brian Osman | 0b791f5 | 2017-03-10 08:30:22 -0500 | [diff] [blame] | 931 | sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info); |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 932 | if (tgt && backendRT.stencilBits()) { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 933 | if (!createStencilAttachmentForRenderTarget(tgt.get(), desc.fWidth, desc.fHeight)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 934 | return nullptr; |
| 935 | } |
| 936 | } |
| 937 | return tgt; |
| 938 | } |
| 939 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 940 | sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 941 | int sampleCnt) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 942 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 943 | const GrVkImageInfo* info = tex.getVkImageInfo(); |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 944 | if (!info) { |
| 945 | return nullptr; |
| 946 | } |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 947 | if (VK_NULL_HANDLE == info->fImage) { |
| 948 | return nullptr; |
| 949 | } |
| 950 | |
| 951 | GrSurfaceDesc desc; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 952 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 953 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 954 | desc.fWidth = tex.width(); |
| 955 | desc.fHeight = tex.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 956 | desc.fConfig = tex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 957 | desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config()); |
| 958 | if (!desc.fSampleCnt) { |
| 959 | return nullptr; |
| 960 | } |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 961 | |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 962 | sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info); |
| 963 | return tgt; |
| 964 | } |
| 965 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 966 | void GrVkGpu::generateMipmap(GrVkTexture* tex, GrSurfaceOrigin texOrigin) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 967 | // don't do anything for linearly tiled textures (can't have mipmaps) |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 968 | if (tex->isLinearTiled()) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 969 | SkDebugf("Trying to create mipmap for linear tiled texture"); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 970 | return; |
| 971 | } |
| 972 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 973 | // determine if we can blit to and from this format |
| 974 | const GrVkCaps& caps = this->vkCaps(); |
| 975 | if (!caps.configCanBeDstofBlit(tex->config(), false) || |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 976 | !caps.configCanBeSrcofBlit(tex->config(), false) || |
| 977 | !caps.mipMapSupport()) { |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 978 | return; |
| 979 | } |
| 980 | |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 981 | if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) { |
| 982 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 983 | } |
| 984 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 985 | // We may need to resolve the texture first if it is also a render target |
| 986 | GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(tex->asRenderTarget()); |
| 987 | if (texRT) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 988 | this->internalResolveRenderTarget(texRT, false); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 989 | } |
| 990 | |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 991 | int width = tex->width(); |
| 992 | int height = tex->height(); |
| 993 | VkImageBlit blitRegion; |
| 994 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 995 | |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 996 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 997 | uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1; |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 998 | if (levelCount != tex->mipLevels()) { |
| 999 | const GrVkResource* oldResource = tex->resource(); |
| 1000 | oldResource->ref(); |
| 1001 | // grab handle to the original image resource |
| 1002 | VkImage oldImage = tex->image(); |
| 1003 | |
| 1004 | // change the original image's layout so we can copy from it |
| 1005 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1006 | VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
| 1007 | |
| 1008 | if (!tex->reallocForMipmap(this, levelCount)) { |
| 1009 | oldResource->unref(this); |
| 1010 | return; |
| 1011 | } |
| 1012 | // change the new image's layout so we can blit to it |
| 1013 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL, |
| 1014 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
| 1015 | |
| 1016 | // Blit original image to top level of new image |
| 1017 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1018 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
| 1019 | blitRegion.srcOffsets[1] = { width, height, 1 }; |
| 1020 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1021 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
| 1022 | blitRegion.dstOffsets[1] = { width, height, 1 }; |
| 1023 | |
| 1024 | fCurrentCmdBuffer->blitImage(this, |
| 1025 | oldResource, |
| 1026 | oldImage, |
| 1027 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1028 | tex->resource(), |
| 1029 | tex->image(), |
| 1030 | VK_IMAGE_LAYOUT_GENERAL, |
| 1031 | 1, |
| 1032 | &blitRegion, |
| 1033 | VK_FILTER_LINEAR); |
| 1034 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1035 | oldResource->unref(this); |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 1036 | } else { |
| 1037 | // change layout of the layers so we can write to them. |
| 1038 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL, |
| 1039 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1042 | // setup memory barrier |
Greg Daniel | 81b8059 | 2017-12-13 10:20:04 -0500 | [diff] [blame] | 1043 | SkASSERT(GrVkFormatIsSupported(tex->imageFormat())); |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1044 | VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1045 | VkImageMemoryBarrier imageMemoryBarrier = { |
| 1046 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 1047 | nullptr, // pNext |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 1048 | VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask |
| 1049 | VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1050 | VK_IMAGE_LAYOUT_GENERAL, // oldLayout |
| 1051 | VK_IMAGE_LAYOUT_GENERAL, // newLayout |
| 1052 | VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex |
| 1053 | VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1054 | tex->image(), // image |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1055 | { aspectFlags, 0, 1, 0, 1 } // subresourceRange |
| 1056 | }; |
| 1057 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1058 | // Blit the miplevels |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1059 | uint32_t mipLevel = 1; |
| 1060 | while (mipLevel < levelCount) { |
| 1061 | int prevWidth = width; |
| 1062 | int prevHeight = height; |
| 1063 | width = SkTMax(1, width / 2); |
| 1064 | height = SkTMax(1, height / 2); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1065 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1066 | imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1; |
| 1067 | this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1068 | false, &imageMemoryBarrier); |
| 1069 | |
| 1070 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1071 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
brianosman | e9906e7 | 2016-06-08 12:44:27 -0700 | [diff] [blame] | 1072 | blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1073 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 }; |
| 1074 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
brianosman | e9906e7 | 2016-06-08 12:44:27 -0700 | [diff] [blame] | 1075 | blitRegion.dstOffsets[1] = { width, height, 1 }; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1076 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1077 | *tex, |
| 1078 | *tex, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1079 | 1, |
| 1080 | &blitRegion, |
| 1081 | VK_FILTER_LINEAR); |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1082 | ++mipLevel; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1083 | } |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1086 | //////////////////////////////////////////////////////////////////////////////// |
| 1087 | |
| 1088 | GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 1089 | int width, |
| 1090 | int height) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1091 | SkASSERT(width >= rt->width()); |
| 1092 | SkASSERT(height >= rt->height()); |
| 1093 | |
| 1094 | int samples = rt->numStencilSamples(); |
| 1095 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 1096 | const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1097 | |
| 1098 | GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1099 | width, |
| 1100 | height, |
| 1101 | samples, |
| 1102 | sFmt)); |
| 1103 | fStats.incStencilAttachmentCreates(); |
| 1104 | return stencil; |
| 1105 | } |
| 1106 | |
| 1107 | //////////////////////////////////////////////////////////////////////////////// |
| 1108 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 1109 | bool copy_testing_data(GrVkGpu* gpu, void* srcData, const GrVkAlloc& alloc, size_t bufferOffset, |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1110 | size_t srcRowBytes, size_t dstRowBytes, int h) { |
| 1111 | void* mapPtr; |
| 1112 | VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(), |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1113 | alloc.fMemory, |
Greg Daniel | 88fdee9 | 2018-02-24 22:41:50 +0000 | [diff] [blame^] | 1114 | alloc.fOffset + bufferOffset, |
| 1115 | dstRowBytes * h, |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1116 | 0, |
| 1117 | &mapPtr)); |
| 1118 | if (err) { |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
Greg Daniel | 20ece3a | 2017-03-28 10:24:43 -0400 | [diff] [blame] | 1122 | if (srcData) { |
| 1123 | // If there is no padding on dst we can do a single memcopy. |
| 1124 | // This assumes the srcData comes in with no padding. |
| 1125 | SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes), |
| 1126 | srcData, srcRowBytes, srcRowBytes, h); |
| 1127 | } else { |
| 1128 | // If there is no srcdata we always copy 0's into the textures so that it is initialized |
| 1129 | // with some data. |
| 1130 | if (srcRowBytes == static_cast<size_t>(dstRowBytes)) { |
| 1131 | memset(mapPtr, 0, srcRowBytes * h); |
| 1132 | } else { |
| 1133 | for (int i = 0; i < h; ++i) { |
| 1134 | memset(mapPtr, 0, srcRowBytes); |
| 1135 | mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes)); |
| 1136 | } |
| 1137 | } |
| 1138 | } |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1139 | GrVkMemory::FlushMappedAlloc(gpu, alloc); |
| 1140 | GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory)); |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
| 1143 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1144 | GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h, |
| 1145 | GrPixelConfig config, |
| 1146 | bool isRenderTarget, |
| 1147 | GrMipMapped mipMapped) { |
| 1148 | |
| 1149 | VkFormat pixelFormat; |
| 1150 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1151 | return GrBackendTexture(); // invalid |
| 1152 | } |
| 1153 | |
| 1154 | bool linearTiling = false; |
| 1155 | if (!fVkCaps->isConfigTexturable(config)) { |
| 1156 | return GrBackendTexture(); // invalid |
| 1157 | } |
| 1158 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1159 | if (isRenderTarget && !fVkCaps->isConfigRenderable(config)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1160 | return GrBackendTexture(); // invalid |
| 1161 | } |
| 1162 | |
| 1163 | // Currently we don't support uploading pixel data when mipped. |
| 1164 | if (srcData && GrMipMapped::kYes == mipMapped) { |
| 1165 | return GrBackendTexture(); // invalid |
| 1166 | } |
| 1167 | |
| 1168 | if (fVkCaps->isConfigTexturableLinearly(config) && |
| 1169 | (!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false)) && |
| 1170 | GrMipMapped::kNo == mipMapped) { |
| 1171 | linearTiling = true; |
| 1172 | } |
| 1173 | |
| 1174 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 1175 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 1176 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 1177 | if (isRenderTarget) { |
| 1178 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 1179 | } |
| 1180 | |
| 1181 | VkImage image = VK_NULL_HANDLE; |
Greg Daniel | 88fdee9 | 2018-02-24 22:41:50 +0000 | [diff] [blame^] | 1182 | GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0, 0 }; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1183 | |
| 1184 | VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 1185 | VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling) |
| 1186 | ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 1187 | : VK_IMAGE_LAYOUT_UNDEFINED; |
| 1188 | |
| 1189 | // Create Image |
| 1190 | VkSampleCountFlagBits vkSamples; |
| 1191 | if (!GrSampleCountToVkSampleCount(1, &vkSamples)) { |
| 1192 | return GrBackendTexture(); // invalid |
| 1193 | } |
| 1194 | |
| 1195 | // Figure out the number of mip levels. |
| 1196 | uint32_t mipLevels = 1; |
| 1197 | if (GrMipMapped::kYes == mipMapped) { |
| 1198 | mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1; |
| 1199 | } |
| 1200 | |
| 1201 | const VkImageCreateInfo imageCreateInfo = { |
| 1202 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 1203 | nullptr, // pNext |
| 1204 | 0, // VkImageCreateFlags |
| 1205 | VK_IMAGE_TYPE_2D, // VkImageType |
| 1206 | pixelFormat, // VkFormat |
| 1207 | { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D |
| 1208 | mipLevels, // mipLevels |
| 1209 | 1, // arrayLayers |
| 1210 | vkSamples, // samples |
| 1211 | imageTiling, // VkImageTiling |
| 1212 | usageFlags, // VkImageUsageFlags |
| 1213 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 1214 | 0, // queueFamilyCount |
| 1215 | 0, // pQueueFamilyIndices |
| 1216 | initialLayout // initialLayout |
| 1217 | }; |
| 1218 | |
| 1219 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image)); |
| 1220 | |
| 1221 | if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) { |
| 1222 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
| 1223 | return GrBackendTexture(); // invalid |
| 1224 | } |
| 1225 | |
| 1226 | // We need to declare these early so that we can delete them at the end outside of the if block. |
Greg Daniel | 88fdee9 | 2018-02-24 22:41:50 +0000 | [diff] [blame^] | 1227 | GrVkAlloc bufferAlloc = { VK_NULL_HANDLE, 0, 0, 0 }; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1228 | VkBuffer buffer = VK_NULL_HANDLE; |
| 1229 | |
| 1230 | VkResult err; |
| 1231 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 1232 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
| 1233 | nullptr, // pNext |
| 1234 | fCmdPool, // commandPool |
| 1235 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level |
| 1236 | 1 // bufferCount |
| 1237 | }; |
| 1238 | |
| 1239 | VkCommandBuffer cmdBuffer; |
| 1240 | err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer)); |
| 1241 | if (err) { |
| 1242 | GrVkMemory::FreeImageMemory(this, false, alloc); |
| 1243 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1244 | return GrBackendTexture(); // invalid |
| 1245 | } |
| 1246 | |
| 1247 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 1248 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 1249 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 1250 | cmdBufferBeginInfo.pNext = nullptr; |
| 1251 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 1252 | cmdBufferBeginInfo.pInheritanceInfo = nullptr; |
| 1253 | |
| 1254 | err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo)); |
| 1255 | SkASSERT(!err); |
| 1256 | |
| 1257 | size_t bpp = GrBytesPerPixel(config); |
| 1258 | size_t rowCopyBytes = bpp * w; |
| 1259 | if (linearTiling) { |
| 1260 | const VkImageSubresource subres = { |
| 1261 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 1262 | 0, // mipLevel |
| 1263 | 0, // arraySlice |
| 1264 | }; |
| 1265 | VkSubresourceLayout layout; |
| 1266 | |
| 1267 | VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout)); |
| 1268 | |
| 1269 | if (!copy_testing_data(this, srcData, alloc, 0, rowCopyBytes, |
| 1270 | static_cast<size_t>(layout.rowPitch), h)) { |
| 1271 | GrVkMemory::FreeImageMemory(this, true, alloc); |
| 1272 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1273 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1274 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1275 | return GrBackendTexture(); // invalid |
| 1276 | } |
| 1277 | } else { |
| 1278 | SkASSERT(w && h); |
| 1279 | |
| 1280 | SkTArray<size_t> individualMipOffsets(mipLevels); |
| 1281 | individualMipOffsets.push_back(0); |
| 1282 | size_t combinedBufferSize = w * bpp * h; |
| 1283 | int currentWidth = w; |
| 1284 | int currentHeight = h; |
| 1285 | // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image |
| 1286 | // config. This works with the assumption that the bytes in pixel config is always a power |
| 1287 | // of 2. |
| 1288 | SkASSERT((bpp & (bpp - 1)) == 0); |
| 1289 | const size_t alignmentMask = 0x3 | (bpp - 1); |
| 1290 | for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) { |
| 1291 | currentWidth = SkTMax(1, currentWidth/2); |
| 1292 | currentHeight = SkTMax(1, currentHeight/2); |
| 1293 | |
| 1294 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 1295 | const size_t alignmentDiff = combinedBufferSize & alignmentMask; |
| 1296 | if (alignmentDiff != 0) { |
| 1297 | combinedBufferSize += alignmentMask - alignmentDiff + 1; |
| 1298 | } |
| 1299 | individualMipOffsets.push_back(combinedBufferSize); |
| 1300 | combinedBufferSize += trimmedSize; |
| 1301 | } |
| 1302 | |
| 1303 | VkBufferCreateInfo bufInfo; |
| 1304 | memset(&bufInfo, 0, sizeof(VkBufferCreateInfo)); |
| 1305 | bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 1306 | bufInfo.flags = 0; |
| 1307 | bufInfo.size = combinedBufferSize; |
| 1308 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
| 1309 | bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 1310 | bufInfo.queueFamilyIndexCount = 0; |
| 1311 | bufInfo.pQueueFamilyIndices = nullptr; |
| 1312 | err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer)); |
| 1313 | |
| 1314 | if (err) { |
| 1315 | GrVkMemory::FreeImageMemory(this, false, alloc); |
| 1316 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1317 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1318 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1319 | return GrBackendTexture(); // invalid |
| 1320 | } |
| 1321 | |
| 1322 | if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, |
| 1323 | true, &bufferAlloc)) { |
| 1324 | GrVkMemory::FreeImageMemory(this, false, alloc); |
| 1325 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1326 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1327 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1328 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1329 | return GrBackendTexture(); // invalid |
| 1330 | } |
| 1331 | |
| 1332 | currentWidth = w; |
| 1333 | currentHeight = h; |
| 1334 | for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) { |
| 1335 | SkASSERT(0 == currentMipLevel || !srcData); |
| 1336 | size_t currentRowBytes = bpp * currentWidth; |
| 1337 | size_t bufferOffset = individualMipOffsets[currentMipLevel]; |
| 1338 | if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset, |
| 1339 | currentRowBytes, currentRowBytes, currentHeight)) { |
| 1340 | GrVkMemory::FreeImageMemory(this, false, alloc); |
| 1341 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1342 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1343 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1344 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1345 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1346 | return GrBackendTexture(); // invalid |
| 1347 | } |
| 1348 | currentWidth = SkTMax(1, currentWidth/2); |
| 1349 | currentHeight = SkTMax(1, currentHeight/2); |
| 1350 | } |
| 1351 | |
| 1352 | // Set image layout and add barrier |
| 1353 | VkImageMemoryBarrier barrier; |
| 1354 | memset(&barrier, 0, sizeof(VkImageMemoryBarrier)); |
| 1355 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1356 | barrier.pNext = nullptr; |
| 1357 | barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout); |
| 1358 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1359 | barrier.oldLayout = initialLayout; |
| 1360 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1361 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1362 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1363 | barrier.image = image; |
| 1364 | barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0 , 1}; |
| 1365 | |
| 1366 | VK_CALL(CmdPipelineBarrier(cmdBuffer, |
| 1367 | GrVkMemory::LayoutToPipelineStageFlags(initialLayout), |
| 1368 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1369 | 0, |
| 1370 | 0, nullptr, |
| 1371 | 0, nullptr, |
| 1372 | 1, &barrier)); |
| 1373 | initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1374 | |
| 1375 | SkTArray<VkBufferImageCopy> regions(mipLevels); |
| 1376 | |
| 1377 | currentWidth = w; |
| 1378 | currentHeight = h; |
| 1379 | for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) { |
| 1380 | // Submit copy command |
| 1381 | VkBufferImageCopy& region = regions.push_back(); |
| 1382 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 1383 | region.bufferOffset = individualMipOffsets[currentMipLevel]; |
| 1384 | region.bufferRowLength = currentWidth; |
| 1385 | region.bufferImageHeight = currentHeight; |
| 1386 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1387 | region.imageOffset = { 0, 0, 0 }; |
| 1388 | region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 }; |
| 1389 | currentWidth = SkTMax(1, currentWidth/2); |
| 1390 | currentHeight = SkTMax(1, currentHeight/2); |
| 1391 | } |
| 1392 | |
| 1393 | VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(), |
| 1394 | regions.begin())); |
| 1395 | } |
| 1396 | // Change Image layout to shader read since if we use this texture as a borrowed textures within |
| 1397 | // Ganesh we require that its layout be set to that |
| 1398 | VkImageMemoryBarrier barrier; |
| 1399 | memset(&barrier, 0, sizeof(VkImageMemoryBarrier)); |
| 1400 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1401 | barrier.pNext = nullptr; |
| 1402 | barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout); |
| 1403 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 1404 | barrier.oldLayout = initialLayout; |
| 1405 | barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 1406 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1407 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1408 | barrier.image = image; |
| 1409 | barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0 , 1}; |
| 1410 | |
| 1411 | VK_CALL(CmdPipelineBarrier(cmdBuffer, |
| 1412 | GrVkMemory::LayoutToPipelineStageFlags(initialLayout), |
| 1413 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, |
| 1414 | 0, |
| 1415 | 0, nullptr, |
| 1416 | 0, nullptr, |
| 1417 | 1, &barrier)); |
| 1418 | |
| 1419 | // End CommandBuffer |
| 1420 | err = VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1421 | SkASSERT(!err); |
| 1422 | |
| 1423 | // Create Fence for queue |
| 1424 | VkFence fence; |
| 1425 | VkFenceCreateInfo fenceInfo; |
| 1426 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 1427 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 1428 | |
| 1429 | err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence)); |
| 1430 | SkASSERT(!err); |
| 1431 | |
| 1432 | VkSubmitInfo submitInfo; |
| 1433 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 1434 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1435 | submitInfo.pNext = nullptr; |
| 1436 | submitInfo.waitSemaphoreCount = 0; |
| 1437 | submitInfo.pWaitSemaphores = nullptr; |
| 1438 | submitInfo.pWaitDstStageMask = 0; |
| 1439 | submitInfo.commandBufferCount = 1; |
| 1440 | submitInfo.pCommandBuffers = &cmdBuffer; |
| 1441 | submitInfo.signalSemaphoreCount = 0; |
| 1442 | submitInfo.pSignalSemaphores = nullptr; |
| 1443 | err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence)); |
| 1444 | SkASSERT(!err); |
| 1445 | |
| 1446 | err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX)); |
| 1447 | if (VK_TIMEOUT == err) { |
| 1448 | GrVkMemory::FreeImageMemory(this, false, alloc); |
| 1449 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1450 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1451 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1452 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1453 | VK_CALL(DestroyFence(fDevice, fence, nullptr)); |
| 1454 | SkDebugf("Fence failed to signal: %d\n", err); |
| 1455 | SK_ABORT("failing"); |
| 1456 | } |
| 1457 | SkASSERT(!err); |
| 1458 | |
| 1459 | // Clean up transfer resources |
| 1460 | if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash |
| 1461 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1462 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1463 | } |
| 1464 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1465 | VK_CALL(DestroyFence(fDevice, fence, nullptr)); |
| 1466 | |
| 1467 | |
| 1468 | GrVkImageInfo info; |
| 1469 | info.fImage = image; |
| 1470 | info.fAlloc = alloc; |
| 1471 | info.fImageTiling = imageTiling; |
| 1472 | info.fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 1473 | info.fFormat = pixelFormat; |
| 1474 | info.fLevelCount = mipLevels; |
| 1475 | |
| 1476 | return GrBackendTexture(w, h, info); |
| 1477 | } |
| 1478 | |
| 1479 | bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
| 1480 | SkASSERT(kVulkan_GrBackend == tex.fBackend); |
| 1481 | |
| 1482 | const GrVkImageInfo* backend = tex.getVkImageInfo(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1483 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 1484 | if (backend && backend->fImage && backend->fAlloc.fMemory) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1485 | VkMemoryRequirements req; |
| 1486 | memset(&req, 0, sizeof(req)); |
| 1487 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
| 1488 | backend->fImage, |
| 1489 | &req)); |
| 1490 | // TODO: find a better check |
| 1491 | // This will probably fail with a different driver |
| 1492 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 1493 | } |
| 1494 | |
| 1495 | return false; |
| 1496 | } |
| 1497 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1498 | void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendTexture* tex, bool abandon) { |
| 1499 | SkASSERT(kVulkan_GrBackend == tex->fBackend); |
| 1500 | |
| 1501 | const GrVkImageInfo* info = tex->getVkImageInfo(); |
| 1502 | |
| 1503 | if (info && !abandon) { |
| 1504 | // something in the command buffer may still be using this, so force submit |
| 1505 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1506 | GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(info)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | //////////////////////////////////////////////////////////////////////////////// |
| 1511 | |
| 1512 | void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1513 | VkPipelineStageFlags dstStageMask, |
| 1514 | bool byRegion, |
| 1515 | VkMemoryBarrier* barrier) const { |
| 1516 | SkASSERT(fCurrentCmdBuffer); |
| 1517 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1518 | srcStageMask, |
| 1519 | dstStageMask, |
| 1520 | byRegion, |
| 1521 | GrVkCommandBuffer::kMemory_BarrierType, |
| 1522 | barrier); |
| 1523 | } |
| 1524 | |
| 1525 | void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1526 | VkPipelineStageFlags dstStageMask, |
| 1527 | bool byRegion, |
| 1528 | VkBufferMemoryBarrier* barrier) const { |
| 1529 | SkASSERT(fCurrentCmdBuffer); |
| 1530 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1531 | srcStageMask, |
| 1532 | dstStageMask, |
| 1533 | byRegion, |
| 1534 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 1535 | barrier); |
| 1536 | } |
| 1537 | |
| 1538 | void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1539 | VkPipelineStageFlags dstStageMask, |
| 1540 | bool byRegion, |
| 1541 | VkImageMemoryBarrier* barrier) const { |
| 1542 | SkASSERT(fCurrentCmdBuffer); |
| 1543 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1544 | srcStageMask, |
| 1545 | dstStageMask, |
| 1546 | byRegion, |
| 1547 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 1548 | barrier); |
| 1549 | } |
| 1550 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 1551 | void GrVkGpu::onFinishFlush(bool insertedSemaphore) { |
| 1552 | // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does |
| 1553 | // not effect what we do here. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1554 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 1555 | } |
| 1556 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 1557 | void GrVkGpu::clearStencil(GrRenderTarget* target, int clearValue) { |
| 1558 | if (!target) { |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1559 | return; |
| 1560 | } |
| 1561 | GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment(); |
| 1562 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1563 | |
| 1564 | |
| 1565 | VkClearDepthStencilValue vkStencilColor; |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 1566 | vkStencilColor.depth = 0.0f; |
| 1567 | vkStencilColor.stencil = clearValue; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1568 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1569 | vkStencil->setImageLayout(this, |
| 1570 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1571 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1572 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1573 | false); |
| 1574 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1575 | VkImageSubresourceRange subRange; |
| 1576 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1577 | subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1578 | subRange.baseMipLevel = 0; |
| 1579 | subRange.levelCount = 1; |
| 1580 | subRange.baseArrayLayer = 0; |
| 1581 | subRange.layerCount = 1; |
| 1582 | |
| 1583 | // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a |
| 1584 | // draw. Thus we should look into using the load op functions on the render pass to clear out |
| 1585 | // the stencil there. |
| 1586 | fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange); |
| 1587 | } |
| 1588 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1589 | inline bool can_copy_image(const GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 1590 | const GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1591 | const GrVkGpu* gpu) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1592 | const GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 1593 | const GrRenderTarget* srcRT = src->asRenderTarget(); |
| 1594 | if (dstRT && srcRT) { |
| 1595 | if (srcRT->numColorSamples() != dstRT->numColorSamples()) { |
| 1596 | return false; |
| 1597 | } |
| 1598 | } else if (dstRT) { |
| 1599 | if (dstRT->numColorSamples() > 1) { |
| 1600 | return false; |
| 1601 | } |
| 1602 | } else if (srcRT) { |
| 1603 | if (srcRT->numColorSamples() > 1) { |
| 1604 | return false; |
| 1605 | } |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Michael Jurka | 3251ed8 | 2017-04-05 09:52:55 -0700 | [diff] [blame] | 1608 | // 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] | 1609 | // as image usage flags. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1610 | if (srcOrigin == dstOrigin && |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1611 | GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1612 | return true; |
| 1613 | } |
| 1614 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1615 | return false; |
| 1616 | } |
| 1617 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1618 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 1619 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1620 | GrVkImage* dstImage, |
| 1621 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1622 | const SkIRect& srcRect, |
| 1623 | const SkIPoint& dstPoint) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1624 | SkASSERT(can_copy_image(dst, dstOrigin, src, srcOrigin, this)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1625 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1626 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 1627 | // the cache is flushed since it is only being written to. |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1628 | dstImage->setImageLayout(this, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1629 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1630 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1631 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1632 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1633 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1634 | srcImage->setImageLayout(this, |
| 1635 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1636 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1637 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1638 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1639 | |
| 1640 | // Flip rect if necessary |
| 1641 | SkIRect srcVkRect = srcRect; |
| 1642 | int32_t dstY = dstPoint.fY; |
| 1643 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1644 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
| 1645 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1646 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1647 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1648 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1649 | } |
| 1650 | |
| 1651 | VkImageCopy copyRegion; |
| 1652 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 1653 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1654 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1655 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1656 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
egdaniel | c355bc8 | 2016-04-27 11:31:59 -0700 | [diff] [blame] | 1657 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1658 | |
| 1659 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1660 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1661 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1662 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1663 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1664 | 1, |
| 1665 | ©Region); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1666 | |
| 1667 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 1668 | srcRect.width(), srcRect.height()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1669 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1670 | } |
| 1671 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1672 | inline bool can_copy_as_blit(const GrSurface* dst, |
| 1673 | const GrSurface* src, |
| 1674 | const GrVkImage* dstImage, |
| 1675 | const GrVkImage* srcImage, |
| 1676 | const GrVkGpu* gpu) { |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1677 | // 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] | 1678 | // as image usage flags. |
| 1679 | const GrVkCaps& caps = gpu->vkCaps(); |
| 1680 | if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) || |
| 1681 | !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) { |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
| 1685 | // We cannot blit images that are multisampled. Will need to figure out if we can blit the |
| 1686 | // resolved msaa though. |
| 1687 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1688 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1689 | return false; |
| 1690 | } |
| 1691 | |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1695 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 1696 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1697 | GrVkImage* dstImage, |
| 1698 | GrVkImage* srcImage, |
| 1699 | const SkIRect& srcRect, |
| 1700 | const SkIPoint& dstPoint) { |
| 1701 | SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this)); |
| 1702 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1703 | dstImage->setImageLayout(this, |
| 1704 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1705 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1706 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1707 | false); |
| 1708 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1709 | srcImage->setImageLayout(this, |
| 1710 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1711 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1712 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1713 | false); |
| 1714 | |
| 1715 | // Flip rect if necessary |
| 1716 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1717 | srcVkRect.fLeft = srcRect.fLeft; |
| 1718 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1719 | SkIRect dstRect; |
| 1720 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1721 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1722 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1723 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1724 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1725 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1726 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1727 | srcVkRect.fTop = srcRect.fTop; |
| 1728 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1731 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1732 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1733 | } else { |
| 1734 | dstRect.fTop = dstPoint.fY; |
| 1735 | } |
| 1736 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 1737 | |
| 1738 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 1739 | // get the correct origintation of the copied data. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1740 | if (srcOrigin != dstOrigin) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1741 | SkTSwap(dstRect.fTop, dstRect.fBottom); |
| 1742 | } |
| 1743 | |
| 1744 | VkImageBlit blitRegion; |
| 1745 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 1746 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1747 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
Greg Daniel | e76071c | 2016-11-02 11:57:06 -0400 | [diff] [blame] | 1748 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 }; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1749 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1750 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
Greg Daniel | e76071c | 2016-11-02 11:57:06 -0400 | [diff] [blame] | 1751 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 }; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1752 | |
| 1753 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1754 | *srcImage, |
| 1755 | *dstImage, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1756 | 1, |
| 1757 | &blitRegion, |
| 1758 | VK_FILTER_NEAREST); // We never scale so any filter works here |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1759 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1760 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1763 | inline bool can_copy_as_resolve(const GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 1764 | const GrSurface* src, GrSurfaceOrigin srcOrigin, |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1765 | const GrVkGpu* gpu) { |
| 1766 | // Our src must be a multisampled render target |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1767 | if (!src->asRenderTarget() || 1 == src->asRenderTarget()->numColorSamples()) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1768 | return false; |
| 1769 | } |
| 1770 | |
Greg Daniel | 7d6f666 | 2017-04-18 16:40:35 -0400 | [diff] [blame] | 1771 | // The dst must not be a multisampled render target, expect in the case where the dst is the |
| 1772 | // resolve texture connected to the msaa src. We check for this in case we are copying a part of |
| 1773 | // a surface to a different region in the same surface. |
| 1774 | if (dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1 && dst != src) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1775 | return false; |
| 1776 | } |
| 1777 | |
| 1778 | // Surfaces must have the same origin. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1779 | if (srcOrigin != dstOrigin) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1780 | return false; |
| 1781 | } |
| 1782 | |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1786 | void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src, |
| 1787 | GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect, |
| 1788 | const SkIPoint& origDstPoint) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1789 | GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1790 | SkIRect srcRect = origSrcRect; |
| 1791 | SkIPoint dstPoint = origDstPoint; |
| 1792 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
| 1793 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin); |
| 1794 | srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom, |
| 1795 | origSrcRect.fRight, src->height() - origSrcRect.fTop}; |
| 1796 | dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height(); |
| 1797 | } |
| 1798 | this->resolveImage(dst, srcRT, srcRect, dstPoint); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1801 | bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 1802 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1803 | const SkIRect& srcRect, |
| 1804 | const SkIPoint& dstPoint) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1805 | if (can_copy_as_resolve(dst, dstOrigin, src, srcOrigin, this)) { |
| 1806 | this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint); |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 1807 | return true; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 1810 | if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) { |
| 1811 | this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue); |
| 1812 | } |
| 1813 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1814 | if (fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) { |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 1815 | return true; |
| 1816 | } |
| 1817 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1818 | GrVkImage* dstImage; |
| 1819 | GrVkImage* srcImage; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1820 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 1821 | if (dstRT) { |
| 1822 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT); |
| 1823 | dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT; |
| 1824 | } else { |
| 1825 | SkASSERT(dst->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1826 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1827 | } |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1828 | GrRenderTarget* srcRT = src->asRenderTarget(); |
| 1829 | if (srcRT) { |
| 1830 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT); |
| 1831 | srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1832 | } else { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1833 | SkASSERT(src->asTexture()); |
| 1834 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 1837 | // For borrowed textures, we *only* want to copy using draws (to avoid layout changes) |
| 1838 | if (srcImage->isBorrowed()) { |
| 1839 | return false; |
| 1840 | } |
| 1841 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1842 | if (can_copy_image(dst, dstOrigin, src, srcOrigin, this)) { |
| 1843 | this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage, |
| 1844 | srcRect, dstPoint); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1845 | return true; |
| 1846 | } |
| 1847 | |
| 1848 | if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1849 | this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage, |
| 1850 | srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1851 | return true; |
| 1852 | } |
| 1853 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1854 | return false; |
| 1855 | } |
| 1856 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 1857 | bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin, int width, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1858 | int height, size_t rowBytes, GrColorType dstColorType, |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 1859 | DrawPreference* drawPreference, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1860 | ReadPixelTempDrawInfo* tempDrawInfo) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1861 | // We don't want to introduce a sRGB conversion if we trigger a draw. |
| 1862 | auto dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(srcSurface->config()); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 1863 | if (*drawPreference != kNoDraw_DrawPreference) { |
| 1864 | // We assume the base class has only inserted a draw for sRGB reasons. So the |
| 1865 | // the temp surface has the config of the dst data. There is no swizzling nor dst config. |
| 1866 | // spoofing. |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1867 | SkASSERT(tempDrawInfo->fReadColorType == dstColorType); |
| 1868 | SkASSERT(GrPixelConfigToColorType(tempDrawInfo->fTempSurfaceDesc.fConfig) == dstColorType); |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 1869 | SkASSERT(tempDrawInfo->fSwizzle == GrSwizzle::RGBA()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1870 | // Don't undo a sRGB conversion introduced by our caller via an intermediate draw. |
| 1871 | dstConfigSRGBEncoded = GrPixelConfigIsSRGBEncoded(tempDrawInfo->fTempSurfaceDesc.fConfig); |
| 1872 | } |
| 1873 | if (GrColorTypeIsAlphaOnly(dstColorType)) { |
| 1874 | dstConfigSRGBEncoded = GrSRGBEncoded::kNo; |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 1875 | } |
| 1876 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1877 | if (GrPixelConfigToColorType(srcSurface->config()) == dstColorType) { |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1878 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1879 | } |
| 1880 | |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 1881 | // Any config change requires a draw |
| 1882 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1883 | tempDrawInfo->fTempSurfaceDesc.fConfig = |
| 1884 | GrColorTypeToPixelConfig(dstColorType, dstConfigSRGBEncoded); |
| 1885 | tempDrawInfo->fReadColorType = dstColorType; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1886 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1887 | return kUnknown_GrPixelConfig != tempDrawInfo->fTempSurfaceDesc.fConfig; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1888 | } |
| 1889 | |
Brian Salomon | 9b009bb | 2018-02-14 13:53:55 -0500 | [diff] [blame] | 1890 | bool GrVkGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1891 | int height, GrColorType dstColorType, void* buffer, size_t rowBytes) { |
| 1892 | if (GrPixelConfigToColorType(surface->config()) != dstColorType) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1893 | return false; |
| 1894 | } |
| 1895 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1896 | GrVkImage* image = nullptr; |
| 1897 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget()); |
| 1898 | if (rt) { |
| 1899 | // resolve the render target if necessary |
| 1900 | switch (rt->getResolveType()) { |
| 1901 | case GrVkRenderTarget::kCantResolve_ResolveType: |
| 1902 | return false; |
| 1903 | case GrVkRenderTarget::kAutoResolves_ResolveType: |
| 1904 | break; |
| 1905 | case GrVkRenderTarget::kCanResolve_ResolveType: |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 1906 | this->internalResolveRenderTarget(rt, false); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1907 | break; |
| 1908 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 1909 | SK_ABORT("Unknown resolve type"); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1910 | } |
| 1911 | image = rt; |
| 1912 | } else { |
| 1913 | image = static_cast<GrVkTexture*>(surface->asTexture()); |
| 1914 | } |
| 1915 | |
| 1916 | if (!image) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1917 | return false; |
| 1918 | } |
| 1919 | |
| 1920 | // Change layout of our target so it can be used as copy |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1921 | image->setImageLayout(this, |
| 1922 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1923 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1924 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1925 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1926 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1927 | int bpp = GrColorTypeBytesPerPixel(dstColorType); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1928 | size_t tightRowBytes = bpp * width; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1929 | bool flipY = kBottomLeft_GrSurfaceOrigin == origin; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1930 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1931 | VkBufferImageCopy region; |
| 1932 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1933 | |
| 1934 | bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin(); |
| 1935 | if (copyFromOrigin) { |
| 1936 | region.imageOffset = { 0, 0, 0 }; |
| 1937 | region.imageExtent = { (uint32_t)(left + width), |
| 1938 | (uint32_t)(flipY ? surface->height() - top : top + height), |
| 1939 | 1 |
| 1940 | }; |
| 1941 | } else { |
| 1942 | VkOffset3D offset = { |
| 1943 | left, |
| 1944 | flipY ? surface->height() - top - height : top, |
| 1945 | 0 |
| 1946 | }; |
| 1947 | region.imageOffset = offset; |
| 1948 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 1949 | } |
| 1950 | |
| 1951 | size_t transBufferRowBytes = bpp * region.imageExtent.width; |
| 1952 | GrVkTransferBuffer* transferBuffer = |
| 1953 | static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * height, |
| 1954 | kXferGpuToCpu_GrBufferType, |
| 1955 | kStream_GrAccessPattern)); |
| 1956 | |
| 1957 | // 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] | 1958 | region.bufferOffset = transferBuffer->offset(); |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1959 | 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] | 1960 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 1961 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1962 | |
| 1963 | fCurrentCmdBuffer->copyImageToBuffer(this, |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1964 | image, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1965 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1966 | transferBuffer, |
| 1967 | 1, |
| 1968 | ®ion); |
| 1969 | |
| 1970 | // make sure the copy to buffer has finished |
| 1971 | transferBuffer->addMemoryBarrier(this, |
| 1972 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1973 | VK_ACCESS_HOST_READ_BIT, |
| 1974 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1975 | VK_PIPELINE_STAGE_HOST_BIT, |
| 1976 | false); |
| 1977 | |
| 1978 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 1979 | // we can copy the data out of the buffer. |
| 1980 | this->submitCommandBuffer(kForce_SyncQueue); |
Greg Daniel | 9fb6cf4 | 2018-02-23 09:16:23 -0500 | [diff] [blame] | 1981 | GrVkMemory::InvalidateMappedAlloc(this, transferBuffer->alloc()); |
Greg Daniel | 88fdee9 | 2018-02-24 22:41:50 +0000 | [diff] [blame^] | 1982 | void* mappedMemory = transferBuffer->map(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1983 | |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1984 | if (copyFromOrigin) { |
| 1985 | uint32_t skipRows = region.imageExtent.height - height; |
| 1986 | mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left; |
| 1987 | } |
| 1988 | |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1989 | if (flipY) { |
| 1990 | const char* srcRow = reinterpret_cast<const char*>(mappedMemory); |
| 1991 | char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes; |
| 1992 | for (int y = 0; y < height; y++) { |
| 1993 | memcpy(dstRow, srcRow, tightRowBytes); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1994 | srcRow += transBufferRowBytes; |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1995 | dstRow -= rowBytes; |
| 1996 | } |
| 1997 | } else { |
Matt Sarett | cf3525a | 2017-01-24 12:43:41 -0500 | [diff] [blame] | 1998 | SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height); |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1999 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2000 | |
| 2001 | transferBuffer->unmap(); |
| 2002 | transferBuffer->unref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2003 | return true; |
| 2004 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 2005 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2006 | // The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple |
| 2007 | // of the granularity. The width must also be a multiple of the granularity or eaqual to the width |
| 2008 | // the the entire attachment. Similar requirements for the y and height components. |
| 2009 | void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds, |
| 2010 | const VkExtent2D& granularity, int maxWidth, int maxHeight) { |
| 2011 | // Adjust Width |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2012 | if ((0 != granularity.width && 1 != granularity.width)) { |
| 2013 | // Start with the right side of rect so we know if we end up going pass the maxWidth. |
| 2014 | int rightAdj = srcBounds.fRight % granularity.width; |
| 2015 | if (rightAdj != 0) { |
| 2016 | rightAdj = granularity.width - rightAdj; |
| 2017 | } |
| 2018 | dstBounds->fRight = srcBounds.fRight + rightAdj; |
| 2019 | if (dstBounds->fRight > maxWidth) { |
| 2020 | dstBounds->fRight = maxWidth; |
| 2021 | dstBounds->fLeft = 0; |
| 2022 | } else { |
| 2023 | dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width; |
| 2024 | } |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2025 | } else { |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2026 | dstBounds->fLeft = srcBounds.fLeft; |
| 2027 | dstBounds->fRight = srcBounds.fRight; |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | // Adjust height |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2031 | if ((0 != granularity.height && 1 != granularity.height)) { |
| 2032 | // Start with the bottom side of rect so we know if we end up going pass the maxHeight. |
| 2033 | int bottomAdj = srcBounds.fBottom % granularity.height; |
| 2034 | if (bottomAdj != 0) { |
| 2035 | bottomAdj = granularity.height - bottomAdj; |
| 2036 | } |
| 2037 | dstBounds->fBottom = srcBounds.fBottom + bottomAdj; |
| 2038 | if (dstBounds->fBottom > maxHeight) { |
| 2039 | dstBounds->fBottom = maxHeight; |
| 2040 | dstBounds->fTop = 0; |
| 2041 | } else { |
| 2042 | dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height; |
| 2043 | } |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2044 | } else { |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2045 | dstBounds->fTop = srcBounds.fTop; |
| 2046 | dstBounds->fBottom = srcBounds.fBottom; |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 2050 | void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2051 | const GrVkRenderPass* renderPass, |
| 2052 | const VkClearValue* colorClear, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2053 | GrVkRenderTarget* target, GrSurfaceOrigin origin, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2054 | const SkIRect& bounds) { |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 2055 | const SkIRect* pBounds = &bounds; |
| 2056 | SkIRect flippedBounds; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2057 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 2058 | flippedBounds = bounds; |
| 2059 | flippedBounds.fTop = target->height() - bounds.fBottom; |
| 2060 | flippedBounds.fBottom = target->height() - bounds.fTop; |
| 2061 | pBounds = &flippedBounds; |
| 2062 | } |
| 2063 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2064 | // The bounds we use for the render pass should be of the granularity supported |
| 2065 | // by the device. |
| 2066 | const VkExtent2D& granularity = renderPass->granularity(); |
| 2067 | SkIRect adjustedBounds; |
| 2068 | if ((0 != granularity.width && 1 != granularity.width) || |
| 2069 | (0 != granularity.height && 1 != granularity.height)) { |
| 2070 | adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity, |
| 2071 | target->width(), target->height()); |
| 2072 | pBounds = &adjustedBounds; |
| 2073 | } |
| 2074 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2075 | #ifdef SK_DEBUG |
| 2076 | uint32_t index; |
| 2077 | bool result = renderPass->colorAttachmentIndex(&index); |
| 2078 | SkASSERT(result && 0 == index); |
| 2079 | result = renderPass->stencilAttachmentIndex(&index); |
| 2080 | if (result) { |
| 2081 | SkASSERT(1 == index); |
| 2082 | } |
| 2083 | #endif |
| 2084 | VkClearValue clears[2]; |
| 2085 | clears[0].color = colorClear->color; |
Robert Phillips | 8c326e9 | 2017-08-10 13:50:17 -0400 | [diff] [blame] | 2086 | clears[1].depthStencil.depth = 0.0f; |
| 2087 | clears[1].depthStencil.stencil = 0; |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2088 | |
| 2089 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 2090 | for (int i = 0; i < buffers.count(); ++i) { |
| 2091 | fCurrentCmdBuffer->executeCommands(this, buffers[i]); |
| 2092 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2093 | fCurrentCmdBuffer->endRenderPass(this); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2094 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2095 | this->didWriteToSurface(target, origin, &bounds); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2096 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2097 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2098 | GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() { |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2099 | VkFenceCreateInfo createInfo; |
| 2100 | memset(&createInfo, 0, sizeof(VkFenceCreateInfo)); |
| 2101 | createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 2102 | createInfo.pNext = nullptr; |
| 2103 | createInfo.flags = 0; |
| 2104 | VkFence fence = VK_NULL_HANDLE; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2105 | |
| 2106 | VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence)); |
| 2107 | VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence)); |
| 2108 | |
| 2109 | GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2110 | return (GrFence)fence; |
| 2111 | } |
| 2112 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2113 | bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) { |
| 2114 | SkASSERT(VK_NULL_HANDLE != (VkFence)fence); |
| 2115 | |
| 2116 | VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2117 | return (VK_SUCCESS == result); |
| 2118 | } |
| 2119 | |
| 2120 | void GrVkGpu::deleteFence(GrFence fence) const { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2121 | VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr)); |
| 2122 | } |
| 2123 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2124 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) { |
| 2125 | return GrVkSemaphore::Make(this, isOwned); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2126 | } |
| 2127 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2128 | sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
| 2129 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 2130 | GrWrapOwnership ownership) { |
| 2131 | return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2132 | } |
| 2133 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2134 | void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2135 | GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get()); |
| 2136 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2137 | GrVkSemaphore::Resource* resource = vkSem->getResource(); |
| 2138 | if (resource->shouldSignal()) { |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 2139 | resource->ref(); |
| 2140 | fSemaphoresToSignal.push_back(resource); |
| 2141 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2142 | |
| 2143 | if (flush) { |
| 2144 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 2145 | } |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2146 | } |
| 2147 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2148 | void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2149 | GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get()); |
| 2150 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2151 | GrVkSemaphore::Resource* resource = vkSem->getResource(); |
| 2152 | if (resource->shouldWait()) { |
| 2153 | resource->ref(); |
| 2154 | fSemaphoresToWaitOn.push_back(resource); |
| 2155 | } |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2156 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 2157 | |
| 2158 | sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) { |
| 2159 | SkASSERT(texture); |
| 2160 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture); |
| 2161 | vkTexture->setImageLayout(this, |
| 2162 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 2163 | VK_ACCESS_SHADER_READ_BIT, |
| 2164 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, |
| 2165 | false); |
| 2166 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 2167 | |
| 2168 | // The image layout change serves as a barrier, so no semaphore is needed |
| 2169 | return nullptr; |
| 2170 | } |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 2171 | |