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