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