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