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