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