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