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