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