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