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