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