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