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