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