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