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