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 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 641 | if (fVkCaps->isConfigTexurableLinearly(desc.fConfig) && |
| 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 | |
| 952 | GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h, |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 953 | GrPixelConfig config, |
| 954 | bool isRenderTarget) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 955 | |
| 956 | VkFormat pixelFormat; |
| 957 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | bool linearTiling = false; |
| 962 | if (!fVkCaps->isConfigTexturable(config)) { |
| 963 | return 0; |
| 964 | } |
| 965 | |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 966 | if (isRenderTarget && !fVkCaps->isConfigRenderable(config, false)) { |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | if (fVkCaps->isConfigTexurableLinearly(config) && |
| 971 | (!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false))) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 972 | linearTiling = true; |
| 973 | } |
| 974 | |
| 975 | // Currently this is not supported since it requires a copy which has not yet been implemented. |
| 976 | if (srcData && !linearTiling) { |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 981 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 982 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 983 | if (isRenderTarget) { |
| 984 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 985 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 986 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 987 | VkImage image = VK_NULL_HANDLE; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 988 | GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 989 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 990 | VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 991 | VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling) |
| 992 | ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 993 | : VK_IMAGE_LAYOUT_UNDEFINED; |
| 994 | |
| 995 | // Create Image |
| 996 | VkSampleCountFlagBits vkSamples; |
| 997 | if (!GrSampleCountToVkSampleCount(1, &vkSamples)) { |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | const VkImageCreateInfo imageCreateInfo = { |
| 1002 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 1003 | NULL, // pNext |
| 1004 | 0, // VkImageCreateFlags |
| 1005 | VK_IMAGE_TYPE_2D, // VkImageType |
| 1006 | pixelFormat, // VkFormat |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 1007 | { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1008 | 1, // mipLevels |
| 1009 | 1, // arrayLayers |
| 1010 | vkSamples, // samples |
| 1011 | imageTiling, // VkImageTiling |
| 1012 | usageFlags, // VkImageUsageFlags |
| 1013 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 1014 | 0, // queueFamilyCount |
| 1015 | 0, // pQueueFamilyIndices |
| 1016 | initialLayout // initialLayout |
| 1017 | }; |
| 1018 | |
| 1019 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image)); |
| 1020 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1021 | if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1022 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | if (srcData) { |
| 1027 | if (linearTiling) { |
| 1028 | const VkImageSubresource subres = { |
| 1029 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 1030 | 0, // mipLevel |
| 1031 | 0, // arraySlice |
| 1032 | }; |
| 1033 | VkSubresourceLayout layout; |
| 1034 | VkResult err; |
| 1035 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1036 | VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1037 | |
| 1038 | void* mapPtr; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 1039 | err = VK_CALL(MapMemory(fDevice, alloc.fMemory, alloc.fOffset, layout.rowPitch * h, |
| 1040 | 0, &mapPtr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1041 | if (err) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1042 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1043 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | size_t bpp = GrBytesPerPixel(config); |
| 1048 | size_t rowCopyBytes = bpp * w; |
| 1049 | // If there is no padding on dst (layout.rowPitch) we can do a single memcopy. |
| 1050 | // This assumes the srcData comes in with no padding. |
| 1051 | if (rowCopyBytes == layout.rowPitch) { |
| 1052 | memcpy(mapPtr, srcData, rowCopyBytes * h); |
| 1053 | } else { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1054 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), srcData, rowCopyBytes, |
| 1055 | rowCopyBytes, h); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1056 | } |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 1057 | VK_CALL(UnmapMemory(fDevice, alloc.fMemory)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1058 | } else { |
| 1059 | // TODO: Add support for copying to optimal tiling |
| 1060 | SkASSERT(false); |
| 1061 | } |
| 1062 | } |
| 1063 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1064 | GrVkImageInfo* info = new GrVkImageInfo; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1065 | info->fImage = image; |
| 1066 | info->fAlloc = alloc; |
| 1067 | info->fImageTiling = imageTiling; |
| 1068 | info->fImageLayout = initialLayout; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1069 | info->fFormat = pixelFormat; |
jvanverth | 2af0f1b | 2016-05-03 10:36:49 -0700 | [diff] [blame] | 1070 | info->fLevelCount = 1; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1071 | |
| 1072 | return (GrBackendObject)info; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1076 | const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1077 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 1078 | if (backend && backend->fImage && backend->fAlloc.fMemory) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1079 | VkMemoryRequirements req; |
| 1080 | memset(&req, 0, sizeof(req)); |
| 1081 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
| 1082 | backend->fImage, |
| 1083 | &req)); |
| 1084 | // TODO: find a better check |
| 1085 | // This will probably fail with a different driver |
| 1086 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 1087 | } |
| 1088 | |
| 1089 | return false; |
| 1090 | } |
| 1091 | |
| 1092 | void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1093 | GrVkImageInfo* backend = reinterpret_cast<GrVkImageInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1094 | if (backend) { |
| 1095 | if (!abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1096 | // something in the command buffer may still be using this, so force submit |
| 1097 | this->submitCommandBuffer(kForce_SyncQueue); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1098 | GrVkImage::DestroyImageInfo(this, backend); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1099 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1100 | delete backend; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | //////////////////////////////////////////////////////////////////////////////// |
| 1105 | |
| 1106 | void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1107 | VkPipelineStageFlags dstStageMask, |
| 1108 | bool byRegion, |
| 1109 | VkMemoryBarrier* barrier) const { |
| 1110 | SkASSERT(fCurrentCmdBuffer); |
| 1111 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1112 | srcStageMask, |
| 1113 | dstStageMask, |
| 1114 | byRegion, |
| 1115 | GrVkCommandBuffer::kMemory_BarrierType, |
| 1116 | barrier); |
| 1117 | } |
| 1118 | |
| 1119 | void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1120 | VkPipelineStageFlags dstStageMask, |
| 1121 | bool byRegion, |
| 1122 | VkBufferMemoryBarrier* barrier) const { |
| 1123 | SkASSERT(fCurrentCmdBuffer); |
| 1124 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1125 | srcStageMask, |
| 1126 | dstStageMask, |
| 1127 | byRegion, |
| 1128 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 1129 | barrier); |
| 1130 | } |
| 1131 | |
| 1132 | void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1133 | VkPipelineStageFlags dstStageMask, |
| 1134 | bool byRegion, |
| 1135 | VkImageMemoryBarrier* barrier) const { |
| 1136 | SkASSERT(fCurrentCmdBuffer); |
| 1137 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1138 | srcStageMask, |
| 1139 | dstStageMask, |
| 1140 | byRegion, |
| 1141 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 1142 | barrier); |
| 1143 | } |
| 1144 | |
| 1145 | void GrVkGpu::finishDrawTarget() { |
| 1146 | // Submit the current command buffer to the Queue |
| 1147 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 1148 | } |
| 1149 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1150 | void GrVkGpu::clearStencil(GrRenderTarget* target) { |
| 1151 | if (nullptr == target) { |
| 1152 | return; |
| 1153 | } |
| 1154 | GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment(); |
| 1155 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1156 | |
| 1157 | |
| 1158 | VkClearDepthStencilValue vkStencilColor; |
| 1159 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 1160 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1161 | vkStencil->setImageLayout(this, |
| 1162 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1163 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1164 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1165 | false); |
| 1166 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1167 | VkImageSubresourceRange subRange; |
| 1168 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1169 | subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1170 | subRange.baseMipLevel = 0; |
| 1171 | subRange.levelCount = 1; |
| 1172 | subRange.baseArrayLayer = 0; |
| 1173 | subRange.layerCount = 1; |
| 1174 | |
| 1175 | // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a |
| 1176 | // draw. Thus we should look into using the load op functions on the render pass to clear out |
| 1177 | // the stencil there. |
| 1178 | fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange); |
| 1179 | } |
| 1180 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1181 | inline bool can_copy_image(const GrSurface* dst, |
| 1182 | const GrSurface* src, |
| 1183 | const GrVkGpu* gpu) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1184 | // Currently we don't support msaa |
| 1185 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1186 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1191 | // as image usage flags. |
| 1192 | if (src->origin() == dst->origin() && |
| 1193 | GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | // 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] | 1198 | // 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] | 1199 | |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, |
| 1204 | GrSurface* src, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1205 | GrVkImage* dstImage, |
| 1206 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1207 | const SkIRect& srcRect, |
| 1208 | const SkIPoint& dstPoint) { |
| 1209 | SkASSERT(can_copy_image(dst, src, this)); |
| 1210 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1211 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 1212 | // the cache is flushed since it is only being written to. |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1213 | dstImage->setImageLayout(this, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1214 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1215 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1216 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1217 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1218 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1219 | srcImage->setImageLayout(this, |
| 1220 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1221 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1222 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1223 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1224 | |
| 1225 | // Flip rect if necessary |
| 1226 | SkIRect srcVkRect = srcRect; |
| 1227 | int32_t dstY = dstPoint.fY; |
| 1228 | |
| 1229 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1230 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin()); |
| 1231 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1232 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1233 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1234 | } |
| 1235 | |
| 1236 | VkImageCopy copyRegion; |
| 1237 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 1238 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1239 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1240 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1241 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
egdaniel | c355bc8 | 2016-04-27 11:31:59 -0700 | [diff] [blame] | 1242 | // The depth value of the extent is ignored according the vulkan spec for 2D images. However, on |
| 1243 | // at least the nexus 5X it seems to be checking it. Thus as a working around we must have the |
| 1244 | // depth value be 1. |
| 1245 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1246 | |
| 1247 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1248 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1249 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1250 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1251 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1252 | 1, |
| 1253 | ©Region); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1254 | |
| 1255 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 1256 | srcRect.width(), srcRect.height()); |
| 1257 | this->didWriteToSurface(dst, &dstRect); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1260 | inline bool can_copy_as_blit(const GrSurface* dst, |
| 1261 | const GrSurface* src, |
| 1262 | const GrVkImage* dstImage, |
| 1263 | const GrVkImage* srcImage, |
| 1264 | const GrVkGpu* gpu) { |
| 1265 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1266 | // as image usage flags. |
| 1267 | const GrVkCaps& caps = gpu->vkCaps(); |
| 1268 | if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) || |
| 1269 | !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) { |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
| 1273 | // We cannot blit images that are multisampled. Will need to figure out if we can blit the |
| 1274 | // resolved msaa though. |
| 1275 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1276 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
| 1280 | return true; |
| 1281 | } |
| 1282 | |
| 1283 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, |
| 1284 | GrSurface* src, |
| 1285 | GrVkImage* dstImage, |
| 1286 | GrVkImage* srcImage, |
| 1287 | const SkIRect& srcRect, |
| 1288 | const SkIPoint& dstPoint) { |
| 1289 | SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this)); |
| 1290 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1291 | dstImage->setImageLayout(this, |
| 1292 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1293 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1294 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1295 | false); |
| 1296 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1297 | srcImage->setImageLayout(this, |
| 1298 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1299 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1300 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1301 | false); |
| 1302 | |
| 1303 | // Flip rect if necessary |
| 1304 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1305 | srcVkRect.fLeft = srcRect.fLeft; |
| 1306 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1307 | SkIRect dstRect; |
| 1308 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1309 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1310 | |
| 1311 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1312 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1313 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1314 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1315 | srcVkRect.fTop = srcRect.fTop; |
| 1316 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 1320 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1321 | } else { |
| 1322 | dstRect.fTop = dstPoint.fY; |
| 1323 | } |
| 1324 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 1325 | |
| 1326 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 1327 | // get the correct origintation of the copied data. |
| 1328 | if (src->origin() != dst->origin()) { |
| 1329 | SkTSwap(dstRect.fTop, dstRect.fBottom); |
| 1330 | } |
| 1331 | |
| 1332 | VkImageBlit blitRegion; |
| 1333 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 1334 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1335 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1336 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 0 }; |
| 1337 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1338 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
| 1339 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 0 }; |
| 1340 | |
| 1341 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1342 | *srcImage, |
| 1343 | *dstImage, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1344 | 1, |
| 1345 | &blitRegion, |
| 1346 | VK_FILTER_NEAREST); // We never scale so any filter works here |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1347 | |
| 1348 | this->didWriteToSurface(dst, &dstRect); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1351 | inline bool can_copy_as_draw(const GrSurface* dst, |
| 1352 | const GrSurface* src, |
| 1353 | const GrVkGpu* gpu) { |
| 1354 | return false; |
| 1355 | } |
| 1356 | |
| 1357 | void GrVkGpu::copySurfaceAsDraw(GrSurface* dst, |
| 1358 | GrSurface* src, |
| 1359 | const SkIRect& srcRect, |
| 1360 | const SkIPoint& dstPoint) { |
| 1361 | SkASSERT(false); |
| 1362 | } |
| 1363 | |
| 1364 | bool GrVkGpu::onCopySurface(GrSurface* dst, |
| 1365 | GrSurface* src, |
| 1366 | const SkIRect& srcRect, |
| 1367 | const SkIPoint& dstPoint) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1368 | GrVkImage* dstImage; |
| 1369 | GrVkImage* srcImage; |
| 1370 | if (dst->asTexture()) { |
| 1371 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
| 1372 | } else { |
| 1373 | SkASSERT(dst->asRenderTarget()); |
| 1374 | dstImage = static_cast<GrVkRenderTarget*>(dst->asRenderTarget()); |
| 1375 | } |
| 1376 | if (src->asTexture()) { |
| 1377 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
| 1378 | } else { |
| 1379 | SkASSERT(src->asRenderTarget()); |
| 1380 | srcImage = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
| 1381 | } |
| 1382 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1383 | if (can_copy_image(dst, src, this)) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1384 | this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint); |
| 1385 | return true; |
| 1386 | } |
| 1387 | |
| 1388 | if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) { |
| 1389 | this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1390 | return true; |
| 1391 | } |
| 1392 | |
| 1393 | if (can_copy_as_draw(dst, src, this)) { |
| 1394 | this->copySurfaceAsDraw(dst, src, srcRect, dstPoint); |
| 1395 | return true; |
| 1396 | } |
| 1397 | |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
egdaniel | 37798fb | 2016-04-12 07:31:49 -0700 | [diff] [blame] | 1401 | bool GrVkGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const { |
| 1402 | // Currently we don't support msaa |
| 1403 | if (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1) { |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | // This will support copying the dst as CopyImage since all of our surfaces require transferSrc |
| 1408 | // and transferDst usage flags in Vulkan. |
| 1409 | desc->fOrigin = src->origin(); |
| 1410 | desc->fConfig = src->config(); |
| 1411 | desc->fFlags = kNone_GrSurfaceFlags; |
| 1412 | return true; |
| 1413 | } |
| 1414 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1415 | void GrVkGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&, |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 1416 | int* effectiveSampleCnt, SamplePattern*) { |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1417 | // TODO: stub. |
| 1418 | SkASSERT(!this->caps()->sampleLocationsSupport()); |
| 1419 | *effectiveSampleCnt = rt->desc().fSampleCnt; |
| 1420 | } |
| 1421 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1422 | bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 1423 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 1424 | ReadPixelTempDrawInfo* tempDrawInfo) { |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1425 | // These settings we will always want if a temp draw is performed. |
| 1426 | tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 1427 | tempDrawInfo->fTempSurfaceDesc.fWidth = width; |
| 1428 | tempDrawInfo->fTempSurfaceDesc.fHeight = height; |
| 1429 | tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0; |
| 1430 | tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL. |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 1431 | tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox; |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1432 | |
| 1433 | // For now assume no swizzling, we may change that below. |
| 1434 | tempDrawInfo->fSwizzle = GrSwizzle::RGBA(); |
| 1435 | |
| 1436 | // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read |
| 1437 | // from will be srcConfig and we will read readConfig pixels from it. |
| 1438 | // Not that if we require a draw and return a non-renderable format for the temp surface the |
| 1439 | // base class will fail for us. |
| 1440 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcSurface->config(); |
| 1441 | tempDrawInfo->fReadConfig = readConfig; |
| 1442 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1443 | if (srcSurface->config() == readConfig) { |
| 1444 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1445 | } |
| 1446 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1447 | if (this->vkCaps().isConfigRenderable(readConfig, false)) { |
| 1448 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 1449 | tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig; |
| 1450 | tempDrawInfo->fReadConfig = readConfig; |
| 1451 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1452 | } |
| 1453 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1454 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | bool GrVkGpu::onReadPixels(GrSurface* surface, |
| 1458 | int left, int top, int width, int height, |
| 1459 | GrPixelConfig config, |
| 1460 | void* buffer, |
| 1461 | size_t rowBytes) { |
| 1462 | VkFormat pixelFormat; |
| 1463 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
| 1467 | GrVkTexture* tgt = static_cast<GrVkTexture*>(surface->asTexture()); |
| 1468 | if (!tgt) { |
| 1469 | return false; |
| 1470 | } |
| 1471 | |
| 1472 | // Change layout of our target so it can be used as copy |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1473 | tgt->setImageLayout(this, |
| 1474 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1475 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1476 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1477 | false); |
| 1478 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1479 | GrVkTransferBuffer* transferBuffer = |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1480 | static_cast<GrVkTransferBuffer*>(this->createBuffer(rowBytes * height, |
| 1481 | kXferGpuToCpu_GrBufferType, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 1482 | kStream_GrAccessPattern)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1483 | |
| 1484 | bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin(); |
| 1485 | VkOffset3D offset = { |
| 1486 | left, |
| 1487 | flipY ? surface->height() - top - height : top, |
| 1488 | 0 |
| 1489 | }; |
| 1490 | |
| 1491 | // Copy the image to a buffer so we can map it to cpu memory |
| 1492 | VkBufferImageCopy region; |
| 1493 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 1494 | region.bufferOffset = transferBuffer->offset(); |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1495 | 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] | 1496 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 1497 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1498 | region.imageOffset = offset; |
| 1499 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 1500 | |
| 1501 | fCurrentCmdBuffer->copyImageToBuffer(this, |
| 1502 | tgt, |
| 1503 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1504 | transferBuffer, |
| 1505 | 1, |
| 1506 | ®ion); |
| 1507 | |
| 1508 | // make sure the copy to buffer has finished |
| 1509 | transferBuffer->addMemoryBarrier(this, |
| 1510 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1511 | VK_ACCESS_HOST_READ_BIT, |
| 1512 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1513 | VK_PIPELINE_STAGE_HOST_BIT, |
| 1514 | false); |
| 1515 | |
| 1516 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 1517 | // we can copy the data out of the buffer. |
| 1518 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1519 | |
| 1520 | void* mappedMemory = transferBuffer->map(); |
| 1521 | |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1522 | size_t tightRowBytes = GrBytesPerPixel(config) * width; |
| 1523 | if (flipY) { |
| 1524 | const char* srcRow = reinterpret_cast<const char*>(mappedMemory); |
| 1525 | char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes; |
| 1526 | for (int y = 0; y < height; y++) { |
| 1527 | memcpy(dstRow, srcRow, tightRowBytes); |
| 1528 | srcRow += tightRowBytes; |
| 1529 | dstRow -= rowBytes; |
| 1530 | } |
| 1531 | } else { |
| 1532 | if (tightRowBytes == rowBytes) { |
| 1533 | memcpy(buffer, mappedMemory, rowBytes*height); |
| 1534 | } else { |
| 1535 | SkRectMemcpy(buffer, rowBytes, mappedMemory, tightRowBytes, tightRowBytes, height); |
| 1536 | } |
| 1537 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1538 | |
| 1539 | transferBuffer->unmap(); |
| 1540 | transferBuffer->unref(); |
| 1541 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1542 | return true; |
| 1543 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 1544 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1545 | // The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple |
| 1546 | // of the granularity. The width must also be a multiple of the granularity or eaqual to the width |
| 1547 | // the the entire attachment. Similar requirements for the y and height components. |
| 1548 | void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds, |
| 1549 | const VkExtent2D& granularity, int maxWidth, int maxHeight) { |
| 1550 | // Adjust Width |
| 1551 | // Start with the right side of rect so we know if we end up going pass the maxWidth. |
| 1552 | int rightAdj = srcBounds.fRight % granularity.width; |
| 1553 | if (rightAdj != 0) { |
| 1554 | rightAdj = granularity.width - rightAdj; |
| 1555 | } |
| 1556 | dstBounds->fRight = srcBounds.fRight + rightAdj; |
| 1557 | if (dstBounds->fRight > maxWidth) { |
| 1558 | dstBounds->fRight = maxWidth; |
| 1559 | dstBounds->fLeft = 0; |
| 1560 | } else { |
| 1561 | dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width; |
| 1562 | } |
| 1563 | |
| 1564 | // Adjust height |
| 1565 | // Start with the bottom side of rect so we know if we end up going pass the maxHeight. |
| 1566 | int bottomAdj = srcBounds.fBottom % granularity.height; |
| 1567 | if (bottomAdj != 0) { |
| 1568 | bottomAdj = granularity.height - bottomAdj; |
| 1569 | } |
| 1570 | dstBounds->fBottom = srcBounds.fBottom + bottomAdj; |
| 1571 | if (dstBounds->fBottom > maxHeight) { |
| 1572 | dstBounds->fBottom = maxHeight; |
| 1573 | dstBounds->fTop = 0; |
| 1574 | } else { |
| 1575 | dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height; |
| 1576 | } |
| 1577 | } |
| 1578 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 1579 | void GrVkGpu::submitSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1580 | const GrVkRenderPass* renderPass, |
| 1581 | const VkClearValue* colorClear, |
| 1582 | GrVkRenderTarget* target, |
| 1583 | const SkIRect& bounds) { |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 1584 | const SkIRect* pBounds = &bounds; |
| 1585 | SkIRect flippedBounds; |
| 1586 | if (kBottomLeft_GrSurfaceOrigin == target->origin()) { |
| 1587 | flippedBounds = bounds; |
| 1588 | flippedBounds.fTop = target->height() - bounds.fBottom; |
| 1589 | flippedBounds.fBottom = target->height() - bounds.fTop; |
| 1590 | pBounds = &flippedBounds; |
| 1591 | } |
| 1592 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1593 | // The bounds we use for the render pass should be of the granularity supported |
| 1594 | // by the device. |
| 1595 | const VkExtent2D& granularity = renderPass->granularity(); |
| 1596 | SkIRect adjustedBounds; |
| 1597 | if ((0 != granularity.width && 1 != granularity.width) || |
| 1598 | (0 != granularity.height && 1 != granularity.height)) { |
| 1599 | adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity, |
| 1600 | target->width(), target->height()); |
| 1601 | pBounds = &adjustedBounds; |
| 1602 | } |
| 1603 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1604 | // Currently it is fine for us to always pass in 1 for the clear count even if no attachment |
| 1605 | // uses it. In the current state, we also only use the LOAD_OP_CLEAR for the color attachment |
| 1606 | // which is always at the first attachment. |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 1607 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, 1, colorClear, *target, *pBounds, true); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 1608 | fCurrentCmdBuffer->executeCommands(this, buffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1609 | fCurrentCmdBuffer->endRenderPass(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1610 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1611 | |