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