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