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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkGpu.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrBackendSemaphore.h" |
| 11 | #include "include/gpu/GrBackendSurface.h" |
| 12 | #include "include/gpu/GrContextOptions.h" |
| 13 | #include "include/private/SkTo.h" |
| 14 | #include "src/core/SkConvertPixels.h" |
| 15 | #include "src/core/SkMipMap.h" |
| 16 | #include "src/gpu/GrContextPriv.h" |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrGeometryProcessor.h" |
| 19 | #include "src/gpu/GrGpuResourceCacheAccess.h" |
| 20 | #include "src/gpu/GrMesh.h" |
| 21 | #include "src/gpu/GrPipeline.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrRenderTargetPriv.h" |
| 24 | #include "src/gpu/GrTexturePriv.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 25 | #include "src/gpu/SkGpuDevice.h" |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 26 | #include "src/gpu/SkGr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/vk/GrVkAMDMemoryAllocator.h" |
| 28 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 29 | #include "src/gpu/vk/GrVkCommandPool.h" |
| 30 | #include "src/gpu/vk/GrVkGpuCommandBuffer.h" |
| 31 | #include "src/gpu/vk/GrVkImage.h" |
| 32 | #include "src/gpu/vk/GrVkIndexBuffer.h" |
| 33 | #include "src/gpu/vk/GrVkInterface.h" |
| 34 | #include "src/gpu/vk/GrVkMemory.h" |
| 35 | #include "src/gpu/vk/GrVkPipeline.h" |
| 36 | #include "src/gpu/vk/GrVkPipelineState.h" |
| 37 | #include "src/gpu/vk/GrVkRenderPass.h" |
| 38 | #include "src/gpu/vk/GrVkResourceProvider.h" |
| 39 | #include "src/gpu/vk/GrVkSemaphore.h" |
| 40 | #include "src/gpu/vk/GrVkTexture.h" |
| 41 | #include "src/gpu/vk/GrVkTextureRenderTarget.h" |
| 42 | #include "src/gpu/vk/GrVkTransferBuffer.h" |
| 43 | #include "src/gpu/vk/GrVkVertexBuffer.h" |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 44 | #include "src/image/SkImage_Gpu.h" |
| 45 | #include "src/image/SkSurface_Gpu.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "src/sksl/SkSLCompiler.h" |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 47 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 48 | #include "include/gpu/vk/GrVkExtensions.h" |
| 49 | #include "include/gpu/vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 50 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 51 | #include <utility> |
| 52 | |
Forrest Reiling | 44f8571 | 2017-03-27 23:22:20 -0700 | [diff] [blame] | 53 | #if !defined(SK_BUILD_FOR_WIN) |
| 54 | #include <unistd.h> |
| 55 | #endif // !defined(SK_BUILD_FOR_WIN) |
| 56 | |
Greg Daniel | def5546 | 2018-08-01 13:40:14 -0400 | [diff] [blame] | 57 | #if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG) |
Ben Wagner | ab6eefe | 2019-05-20 11:02:49 -0400 | [diff] [blame] | 58 | #include "src/core/SkLeanWindows.h" |
Greg Daniel | def5546 | 2018-08-01 13:40:14 -0400 | [diff] [blame] | 59 | #endif |
| 60 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 61 | #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) |
| 62 | #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) |
| 63 | #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) |
| 64 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 65 | sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext, |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 66 | const GrContextOptions& options, GrContext* context) { |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 67 | if (backendContext.fInstance == VK_NULL_HANDLE || |
| 68 | backendContext.fPhysicalDevice == VK_NULL_HANDLE || |
| 69 | backendContext.fDevice == VK_NULL_HANDLE || |
| 70 | backendContext.fQueue == VK_NULL_HANDLE) { |
| 71 | return nullptr; |
| 72 | } |
Greg Daniel | d3e65aa | 2018-08-01 09:19:45 -0400 | [diff] [blame] | 73 | if (!backendContext.fGetProc) { |
| 74 | return nullptr; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 75 | } |
Greg Daniel | d3e65aa | 2018-08-01 09:19:45 -0400 | [diff] [blame] | 76 | |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 77 | PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion = |
| 78 | reinterpret_cast<PFN_vkEnumerateInstanceVersion>( |
| 79 | backendContext.fGetProc("vkEnumerateInstanceVersion", |
| 80 | VK_NULL_HANDLE, VK_NULL_HANDLE)); |
| 81 | uint32_t instanceVersion = 0; |
| 82 | if (!localEnumerateInstanceVersion) { |
| 83 | instanceVersion = VK_MAKE_VERSION(1, 0, 0); |
| 84 | } else { |
| 85 | VkResult err = localEnumerateInstanceVersion(&instanceVersion); |
| 86 | if (err) { |
| 87 | SkDebugf("Failed to enumerate instance version. Err: %d\n", err); |
| 88 | return nullptr; |
| 89 | } |
| 90 | } |
| 91 | |
Greg Daniel | c0b03d8 | 2018-08-03 14:41:15 -0400 | [diff] [blame] | 92 | PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties = |
| 93 | reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>( |
| 94 | backendContext.fGetProc("vkGetPhysicalDeviceProperties", |
| 95 | backendContext.fInstance, |
| 96 | VK_NULL_HANDLE)); |
| 97 | |
| 98 | if (!localGetPhysicalDeviceProperties) { |
| 99 | return nullptr; |
| 100 | } |
| 101 | VkPhysicalDeviceProperties physDeviceProperties; |
| 102 | localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties); |
| 103 | uint32_t physDevVersion = physDeviceProperties.apiVersion; |
| 104 | |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 105 | uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion |
| 106 | : instanceVersion; |
| 107 | |
| 108 | instanceVersion = SkTMin(instanceVersion, apiVersion); |
| 109 | physDevVersion = SkTMin(physDevVersion, apiVersion); |
| 110 | |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 111 | sk_sp<const GrVkInterface> interface; |
Greg Daniel | d3e65aa | 2018-08-01 09:19:45 -0400 | [diff] [blame] | 112 | |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 113 | if (backendContext.fVkExtensions) { |
| 114 | interface.reset(new GrVkInterface(backendContext.fGetProc, |
| 115 | backendContext.fInstance, |
| 116 | backendContext.fDevice, |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 117 | instanceVersion, |
Greg Daniel | c0b03d8 | 2018-08-03 14:41:15 -0400 | [diff] [blame] | 118 | physDevVersion, |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 119 | backendContext.fVkExtensions)); |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 120 | if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) { |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 121 | return nullptr; |
| 122 | } |
| 123 | } else { |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 124 | GrVkExtensions extensions; |
Greg Daniel | 88e8ddc | 2019-04-25 16:37:08 -0400 | [diff] [blame] | 125 | // The only extension flag that may effect the vulkan backend is the swapchain extension. We |
| 126 | // need to know if this is enabled to know if we can transition to a present layout when |
| 127 | // flushing a surface. |
| 128 | if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) { |
| 129 | const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME; |
| 130 | extensions.init(backendContext.fGetProc, backendContext.fInstance, |
| 131 | backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName); |
| 132 | } |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 133 | interface.reset(new GrVkInterface(backendContext.fGetProc, |
| 134 | backendContext.fInstance, |
| 135 | backendContext.fDevice, |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 136 | instanceVersion, |
Greg Daniel | c0b03d8 | 2018-08-03 14:41:15 -0400 | [diff] [blame] | 137 | physDevVersion, |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 138 | &extensions)); |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 139 | if (!interface->validate(instanceVersion, physDevVersion, &extensions)) { |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 140 | return nullptr; |
| 141 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 144 | return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion, |
| 145 | physDevVersion)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | //////////////////////////////////////////////////////////////////////////////// |
| 149 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 150 | GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options, |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 151 | const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface, |
| 152 | uint32_t instanceVersion, uint32_t physicalDeviceVersion) |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 153 | : INHERITED(context) |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 154 | , fInterface(std::move(interface)) |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 155 | , fMemoryAllocator(backendContext.fMemoryAllocator) |
| 156 | , fInstance(backendContext.fInstance) |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 157 | , fPhysicalDevice(backendContext.fPhysicalDevice) |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 158 | , fDevice(backendContext.fDevice) |
| 159 | , fQueue(backendContext.fQueue) |
Greg Daniel | ecddbc0 | 2018-08-30 16:39:34 -0400 | [diff] [blame] | 160 | , fQueueIndex(backendContext.fGraphicsQueueIndex) |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 161 | , fResourceProvider(this) |
| 162 | , fDisconnected(false) { |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 163 | SkASSERT(!backendContext.fOwnsInstanceAndDevice); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 164 | |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 165 | if (!fMemoryAllocator) { |
| 166 | // We were not given a memory allocator at creation |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 167 | fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice, |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 168 | fDevice, fInterface)); |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 169 | } |
| 170 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 171 | fCompiler = new SkSL::Compiler(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 172 | |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 173 | if (backendContext.fDeviceFeatures2) { |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 174 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice, |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 175 | *backendContext.fDeviceFeatures2, instanceVersion, |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 176 | physicalDeviceVersion, |
Greg Daniel | c0b03d8 | 2018-08-03 14:41:15 -0400 | [diff] [blame] | 177 | *backendContext.fVkExtensions)); |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 178 | } else if (backendContext.fDeviceFeatures) { |
| 179 | VkPhysicalDeviceFeatures2 features2; |
| 180 | features2.pNext = nullptr; |
| 181 | features2.features = *backendContext.fDeviceFeatures; |
| 182 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice, |
Greg Daniel | 41f0e28 | 2019-01-28 13:15:05 -0500 | [diff] [blame] | 183 | features2, instanceVersion, physicalDeviceVersion, |
| 184 | *backendContext.fVkExtensions)); |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 185 | } else { |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 186 | VkPhysicalDeviceFeatures2 features; |
| 187 | memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2)); |
| 188 | features.pNext = nullptr; |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 189 | if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) { |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 190 | features.features.geometryShader = true; |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 191 | } |
| 192 | if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) { |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 193 | features.features.dualSrcBlend = true; |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 194 | } |
| 195 | if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) { |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 196 | features.features.sampleRateShading = true; |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 197 | } |
Greg Daniel | f808c5e | 2019-04-30 14:48:27 -0400 | [diff] [blame] | 198 | GrVkExtensions extensions; |
| 199 | // The only extension flag that may effect the vulkan backend is the swapchain extension. We |
| 200 | // need to know if this is enabled to know if we can transition to a present layout when |
| 201 | // flushing a surface. |
| 202 | if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) { |
| 203 | const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME; |
| 204 | extensions.init(backendContext.fGetProc, backendContext.fInstance, |
| 205 | backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName); |
| 206 | } |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 207 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice, |
Greg Daniel | f808c5e | 2019-04-30 14:48:27 -0400 | [diff] [blame] | 208 | features, instanceVersion, physicalDeviceVersion, extensions)); |
Greg Daniel | 3644360 | 2018-08-02 12:51:52 -0400 | [diff] [blame] | 209 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 210 | fCaps.reset(SkRef(fVkCaps.get())); |
| 211 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 212 | VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps)); |
| 213 | VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps)); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 214 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 215 | fResourceProvider.init(); |
| 216 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 217 | fCmdPool = fResourceProvider.findOrCreateCommandPool(); |
| 218 | fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer(); |
Ethan Nicholas | bff4e07 | 2018-12-12 18:17:24 +0000 | [diff] [blame] | 219 | SkASSERT(fCurrentCmdBuffer); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 220 | fCurrentCmdBuffer->begin(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 223 | void GrVkGpu::destroyResources() { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 224 | if (fCmdPool) { |
| 225 | fCmdPool->getPrimaryCommandBuffer()->end(this); |
| 226 | fCmdPool->close(); |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 227 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 228 | |
| 229 | // wait for all commands to finish |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 230 | VkResult res = VK_CALL(QueueWaitIdle(fQueue)); |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 231 | |
| 232 | // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences |
| 233 | // on the command buffers even though they have completed. This causes an assert to fire when |
| 234 | // destroying the command buffers. Currently this ony seems to happen on windows, so we add a |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 235 | // sleep to make sure the fence signals. |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 236 | #ifdef SK_DEBUG |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 237 | if (this->vkCaps().mustSleepOnTearDown()) { |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 238 | #if defined(SK_BUILD_FOR_WIN) |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 239 | Sleep(10); // In milliseconds |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 240 | #else |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 241 | sleep(1); // In seconds |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 242 | #endif |
Greg Daniel | 80a08dd | 2017-01-20 10:45:49 -0500 | [diff] [blame] | 243 | } |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 244 | #endif |
| 245 | |
egdaniel | be9d821 | 2016-09-20 08:54:23 -0700 | [diff] [blame] | 246 | #ifdef SK_DEBUG |
Greg Daniel | 8a8668b | 2016-10-31 16:34:42 -0400 | [diff] [blame] | 247 | SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res); |
egdaniel | be9d821 | 2016-09-20 08:54:23 -0700 | [diff] [blame] | 248 | #endif |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 249 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 250 | if (fCmdPool) { |
| 251 | fCmdPool->unref(this); |
| 252 | fCmdPool = nullptr; |
| 253 | } |
| 254 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 255 | for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) { |
| 256 | fSemaphoresToWaitOn[i]->unref(this); |
| 257 | } |
| 258 | fSemaphoresToWaitOn.reset(); |
| 259 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 260 | for (int i = 0; i < fSemaphoresToSignal.count(); ++i) { |
| 261 | fSemaphoresToSignal[i]->unref(this); |
| 262 | } |
| 263 | fSemaphoresToSignal.reset(); |
| 264 | |
| 265 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 266 | fCopyManager.destroyResources(this); |
| 267 | |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 268 | // must call this just before we destroy the command pool and VkDevice |
| 269 | fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 270 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 271 | fMemoryAllocator.reset(); |
| 272 | |
| 273 | fQueue = VK_NULL_HANDLE; |
| 274 | fDevice = VK_NULL_HANDLE; |
| 275 | fInstance = VK_NULL_HANDLE; |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | GrVkGpu::~GrVkGpu() { |
| 279 | if (!fDisconnected) { |
| 280 | this->destroyResources(); |
| 281 | } |
| 282 | delete fCompiler; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | void GrVkGpu::disconnect(DisconnectType type) { |
| 287 | INHERITED::disconnect(type); |
| 288 | if (!fDisconnected) { |
| 289 | if (DisconnectType::kCleanup == type) { |
| 290 | this->destroyResources(); |
| 291 | } else { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 292 | if (fCmdPool) { |
| 293 | fCmdPool->unrefAndAbandon(); |
| 294 | fCmdPool = nullptr; |
Greg Daniel | adb4bfe | 2018-08-23 16:15:05 -0400 | [diff] [blame] | 295 | } |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 296 | for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) { |
| 297 | fSemaphoresToWaitOn[i]->unrefAndAbandon(); |
| 298 | } |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 299 | for (int i = 0; i < fSemaphoresToSignal.count(); ++i) { |
| 300 | fSemaphoresToSignal[i]->unrefAndAbandon(); |
| 301 | } |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 302 | fCopyManager.abandonResources(); |
| 303 | |
| 304 | // must call this just before we destroy the command pool and VkDevice |
| 305 | fResourceProvider.abandonResources(); |
Greg Daniel | adb4bfe | 2018-08-23 16:15:05 -0400 | [diff] [blame] | 306 | |
| 307 | fMemoryAllocator.reset(); |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 308 | } |
| 309 | fSemaphoresToWaitOn.reset(); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 310 | fSemaphoresToSignal.reset(); |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 311 | fCurrentCmdBuffer = nullptr; |
Greg Daniel | 8606cf8 | 2017-05-08 16:17:53 -0400 | [diff] [blame] | 312 | fDisconnected = true; |
| 313 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /////////////////////////////////////////////////////////////////////////////// |
| 317 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 318 | GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer( |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 319 | GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 320 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
| 321 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) { |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 322 | if (!fCachedRTCommandBuffer) { |
| 323 | fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this)); |
| 324 | } |
| 325 | |
Greg Daniel | a41a74a | 2018-10-09 12:59:23 +0000 | [diff] [blame] | 326 | fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo); |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 327 | return fCachedRTCommandBuffer.get(); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 330 | GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) { |
| 331 | if (!fCachedTexCommandBuffer) { |
| 332 | fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this)); |
| 333 | } |
| 334 | |
| 335 | fCachedTexCommandBuffer->set(texture, origin); |
| 336 | return fCachedTexCommandBuffer.get(); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 339 | void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc, |
| 340 | GrGpuFinishedContext finishedContext) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 341 | SkASSERT(fCurrentCmdBuffer); |
Robert Phillips | ce0a2bf | 2019-04-02 13:37:34 -0400 | [diff] [blame] | 342 | |
| 343 | if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync && |
| 344 | !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) { |
| 345 | SkASSERT(fDrawables.empty()); |
Robert Phillips | 84614c3 | 2019-04-05 09:36:00 -0400 | [diff] [blame] | 346 | fResourceProvider.checkCommandBuffers(); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 347 | if (finishedProc) { |
| 348 | fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext); |
| 349 | } |
Robert Phillips | ce0a2bf | 2019-04-02 13:37:34 -0400 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 353 | fCurrentCmdBuffer->end(this); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 354 | fCmdPool->close(); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 355 | fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 356 | |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 357 | if (finishedProc) { |
| 358 | // Make sure this is called after closing the current command pool |
| 359 | fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext); |
| 360 | } |
| 361 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 362 | // We must delete and drawables that have been waitint till submit for us to destroy. |
| 363 | fDrawables.reset(); |
| 364 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 365 | for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) { |
| 366 | fSemaphoresToWaitOn[i]->unref(this); |
| 367 | } |
| 368 | fSemaphoresToWaitOn.reset(); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 369 | for (int i = 0; i < fSemaphoresToSignal.count(); ++i) { |
| 370 | fSemaphoresToSignal[i]->unref(this); |
| 371 | } |
| 372 | fSemaphoresToSignal.reset(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 373 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 374 | // Release old command pool and create a new one |
| 375 | fCmdPool->unref(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 376 | fResourceProvider.checkCommandBuffers(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 377 | fCmdPool = fResourceProvider.findOrCreateCommandPool(); |
| 378 | fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 379 | fCurrentCmdBuffer->begin(this); |
| 380 | } |
| 381 | |
| 382 | /////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 383 | sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type, |
| 384 | GrAccessPattern accessPattern, const void* data) { |
| 385 | sk_sp<GrGpuBuffer> buff; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 386 | switch (type) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 387 | case GrGpuBufferType::kVertex: |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 388 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 389 | kStatic_GrAccessPattern == accessPattern); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 390 | buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 391 | break; |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 392 | case GrGpuBufferType::kIndex: |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 393 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 394 | kStatic_GrAccessPattern == accessPattern); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 395 | buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 396 | break; |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 397 | case GrGpuBufferType::kXferCpuToGpu: |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 398 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 399 | kStream_GrAccessPattern == accessPattern); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 400 | buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 401 | break; |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 402 | case GrGpuBufferType::kXferGpuToCpu: |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 403 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 404 | kStream_GrAccessPattern == accessPattern); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 405 | buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 406 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 407 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 408 | SK_ABORT("Unknown buffer type."); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 409 | return nullptr; |
| 410 | } |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 411 | if (data && buff) { |
| 412 | buff->updateData(data, size); |
| 413 | } |
| 414 | return buff; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 415 | } |
| 416 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 417 | bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height, |
| 418 | GrColorType srcColorType, const GrMipLevel texels[], |
| 419 | int mipLevelCount) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 420 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture()); |
| 421 | if (!vkTex) { |
| 422 | return false; |
| 423 | } |
| 424 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 425 | // Make sure we have at least the base level |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 426 | if (!mipLevelCount || !texels[0].fPixels) { |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 427 | return false; |
| 428 | } |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 429 | |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 430 | SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat())); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 431 | bool success = false; |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 432 | bool linearTiling = vkTex->isLinearTiled(); |
| 433 | if (linearTiling) { |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 434 | if (mipLevelCount > 1) { |
Robert Phillips | 92de631 | 2017-05-23 07:43:48 -0400 | [diff] [blame] | 435 | SkDebugf("Can't upload mipmap data to linear tiled texture"); |
| 436 | return false; |
| 437 | } |
| 438 | if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) { |
| 439 | // Need to change the layout to general in order to perform a host write |
| 440 | vkTex->setImageLayout(this, |
| 441 | VK_IMAGE_LAYOUT_GENERAL, |
| 442 | VK_ACCESS_HOST_WRITE_BIT, |
| 443 | VK_PIPELINE_STAGE_HOST_BIT, |
| 444 | false); |
| 445 | this->submitCommandBuffer(kForce_SyncQueue); |
| 446 | } |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 447 | success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 448 | texels[0].fPixels, texels[0].fRowBytes); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 449 | } else { |
Greg Daniel | da86e28 | 2018-06-13 09:41:19 -0400 | [diff] [blame] | 450 | SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 451 | success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels, |
| 452 | mipLevelCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 453 | } |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 454 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 455 | return success; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Brian Salomon | e05ba5a | 2019-04-08 11:59:07 -0400 | [diff] [blame] | 458 | bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height, |
| 459 | GrColorType bufferColorType, GrGpuBuffer* transferBuffer, |
| 460 | size_t bufferOffset, size_t rowBytes) { |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 461 | // Vulkan only supports 4-byte aligned offsets |
| 462 | if (SkToBool(bufferOffset & 0x2)) { |
| 463 | return false; |
| 464 | } |
| 465 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture); |
| 466 | if (!vkTex) { |
| 467 | return false; |
| 468 | } |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 469 | |
| 470 | // Can't transfer compressed data |
| 471 | SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat())); |
| 472 | |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 473 | GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer); |
| 474 | if (!vkBuffer) { |
| 475 | return false; |
| 476 | } |
| 477 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 478 | SkDEBUGCODE( |
| 479 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 480 | SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height()); |
| 481 | SkASSERT(bounds.contains(subRect)); |
| 482 | ) |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 483 | int bpp = GrColorTypeBytesPerPixel(bufferColorType); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 484 | if (rowBytes == 0) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 485 | rowBytes = bpp * width; |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Set up copy region |
| 489 | VkBufferImageCopy region; |
| 490 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 491 | region.bufferOffset = bufferOffset; |
| 492 | region.bufferRowLength = (uint32_t)(rowBytes/bpp); |
| 493 | region.bufferImageHeight = 0; |
| 494 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 495 | region.imageOffset = { left, top, 0 }; |
| 496 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 497 | |
| 498 | // Change layout of our target so it can be copied to |
| 499 | vkTex->setImageLayout(this, |
| 500 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 501 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 502 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 503 | false); |
| 504 | |
| 505 | // Copy the buffer to the image |
| 506 | fCurrentCmdBuffer->copyBufferToImage(this, |
| 507 | vkBuffer, |
| 508 | vkTex, |
| 509 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 510 | 1, |
| 511 | ®ion); |
| 512 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 513 | vkTex->texturePriv().markMipMapsDirty(); |
Jim Van Verth | 2e5eaf0 | 2017-06-21 15:55:46 -0400 | [diff] [blame] | 514 | return true; |
| 515 | } |
| 516 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 517 | bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height, |
| 518 | GrColorType bufferColorType, GrGpuBuffer* transferBuffer, |
| 519 | size_t offset) { |
Brian Salomon | a585fe9 | 2019-04-09 14:57:00 -0400 | [diff] [blame] | 520 | SkASSERT(surface); |
| 521 | SkASSERT(transferBuffer); |
| 522 | |
Brian Salomon | a585fe9 | 2019-04-09 14:57:00 -0400 | [diff] [blame] | 523 | GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer); |
| 524 | |
| 525 | GrVkImage* srcImage; |
| 526 | if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) { |
| 527 | // Reading from render targets that wrap a secondary command buffer is not allowed since |
| 528 | // it would require us to know the VkImage, which we don't have, as well as need us to |
| 529 | // stop and start the VkRenderPass which we don't have access to. |
| 530 | if (rt->wrapsSecondaryCommandBuffer()) { |
| 531 | return false; |
| 532 | } |
| 533 | // resolve the render target if necessary |
| 534 | switch (rt->getResolveType()) { |
| 535 | case GrVkRenderTarget::kCantResolve_ResolveType: |
| 536 | return false; |
| 537 | case GrVkRenderTarget::kAutoResolves_ResolveType: |
| 538 | break; |
| 539 | case GrVkRenderTarget::kCanResolve_ResolveType: |
| 540 | this->resolveRenderTargetNoFlush(rt); |
| 541 | break; |
| 542 | default: |
| 543 | SK_ABORT("Unknown resolve type"); |
| 544 | } |
| 545 | srcImage = rt; |
| 546 | } else { |
| 547 | srcImage = static_cast<GrVkTexture*>(surface->asTexture()); |
| 548 | } |
| 549 | |
| 550 | // Set up copy region |
| 551 | VkBufferImageCopy region; |
| 552 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 553 | region.bufferOffset = offset; |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 554 | region.bufferRowLength = width; |
Brian Salomon | a585fe9 | 2019-04-09 14:57:00 -0400 | [diff] [blame] | 555 | region.bufferImageHeight = 0; |
| 556 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 557 | region.imageOffset = { left, top, 0 }; |
| 558 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 559 | |
| 560 | srcImage->setImageLayout(this, |
| 561 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 562 | VK_ACCESS_TRANSFER_READ_BIT, |
| 563 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 564 | false); |
| 565 | |
| 566 | fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 567 | vkBuffer, 1, ®ion); |
| 568 | |
| 569 | // Make sure the copy to buffer has finished. |
| 570 | vkBuffer->addMemoryBarrier(this, |
| 571 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 572 | VK_ACCESS_HOST_READ_BIT, |
| 573 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 574 | VK_PIPELINE_STAGE_HOST_BIT, |
| 575 | false); |
| 576 | |
| 577 | // The caller is responsible for syncing. |
| 578 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 579 | |
Brian Salomon | 26de56e | 2019-04-10 12:14:26 -0400 | [diff] [blame] | 580 | return true; |
Brian Salomon | a585fe9 | 2019-04-09 14:57:00 -0400 | [diff] [blame] | 581 | } |
| 582 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 583 | void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect, |
| 584 | const SkIPoint& dstPoint) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 585 | SkASSERT(dst); |
| 586 | SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage()); |
| 587 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 588 | VkImageResolve resolveInfo; |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 589 | resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; |
| 590 | resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0}; |
| 591 | resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; |
| 592 | resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0}; |
| 593 | resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1}; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 594 | |
Greg Daniel | bc26c39 | 2017-04-18 13:32:10 -0400 | [diff] [blame] | 595 | GrVkImage* dstImage; |
| 596 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 597 | if (dstRT) { |
| 598 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT); |
Greg Daniel | bc26c39 | 2017-04-18 13:32:10 -0400 | [diff] [blame] | 599 | dstImage = vkRT; |
| 600 | } else { |
| 601 | SkASSERT(dst->asTexture()); |
| 602 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
| 603 | } |
| 604 | dstImage->setImageLayout(this, |
| 605 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 606 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 607 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 608 | false); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 609 | |
| 610 | src->msaaImage()->setImageLayout(this, |
| 611 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 612 | VK_ACCESS_TRANSFER_READ_BIT, |
| 613 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 614 | false); |
| 615 | |
Greg Daniel | bc26c39 | 2017-04-18 13:32:10 -0400 | [diff] [blame] | 616 | fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 619 | void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) { |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 620 | if (target->needsResolve()) { |
| 621 | SkASSERT(target->numColorSamples() > 1); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 622 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target); |
| 623 | SkASSERT(rt->msaaImage()); |
Greg Daniel | 69d4992 | 2017-02-23 09:44:02 -0500 | [diff] [blame] | 624 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 625 | const SkIRect& srcRect = rt->getResolveRect(); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 626 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 627 | this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop)); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 628 | |
| 629 | rt->flagAsResolved(); |
Greg Daniel | 69d4992 | 2017-02-23 09:44:02 -0500 | [diff] [blame] | 630 | |
| 631 | if (requiresSubmit) { |
| 632 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 633 | } |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 637 | bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height, |
| 638 | GrColorType dataColorType, const void* data, size_t rowBytes) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 639 | SkASSERT(data); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 640 | SkASSERT(tex->isLinearTiled()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 641 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 642 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 643 | SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType, |
| 644 | GrSRGBEncoded::kNo))); |
| 645 | |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 646 | SkDEBUGCODE( |
| 647 | SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height); |
| 648 | SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height()); |
| 649 | SkASSERT(bounds.contains(subRect)); |
| 650 | ) |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 651 | int bpp = GrColorTypeBytesPerPixel(dataColorType); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 652 | size_t trimRowBytes = width * bpp; |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 653 | if (!rowBytes) { |
| 654 | rowBytes = trimRowBytes; |
| 655 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 656 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 657 | SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() || |
| 658 | VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout()); |
| 659 | const VkImageSubresource subres = { |
| 660 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 661 | 0, // mipLevel |
| 662 | 0, // arraySlice |
| 663 | }; |
| 664 | VkSubresourceLayout layout; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 665 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 666 | const GrVkInterface* interface = this->vkInterface(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 667 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 668 | GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 669 | tex->image(), |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 670 | &subres, |
| 671 | &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 672 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 673 | const GrVkAlloc& alloc = tex->alloc(); |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 674 | VkDeviceSize offset = top * layout.rowPitch + left * bpp; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 675 | VkDeviceSize size = height*layout.rowPitch; |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 676 | SkASSERT(size + offset <= alloc.fSize); |
| 677 | void* mapPtr = GrVkMemory::MapAlloc(this, alloc); |
| 678 | if (!mapPtr) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 679 | return false; |
| 680 | } |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 681 | mapPtr = reinterpret_cast<char*>(mapPtr) + offset; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 682 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 683 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes, |
| 684 | height); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 685 | |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 686 | GrVkMemory::FlushMappedAlloc(this, alloc, offset, size); |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 687 | GrVkMemory::UnmapAlloc(this, alloc); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 692 | bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height, |
| 693 | GrColorType dataColorType, const GrMipLevel texels[], |
| 694 | int mipLevelCount) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 695 | SkASSERT(!tex->isLinearTiled()); |
| 696 | // The assumption is either that we have no mipmaps, or that our rect is the entire texture |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 697 | SkASSERT(1 == mipLevelCount || |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 698 | (0 == left && 0 == top && width == tex->width() && height == tex->height())); |
| 699 | |
Greg Daniel | dd20e91 | 2017-04-07 14:42:23 -0400 | [diff] [blame] | 700 | // We assume that if the texture has mip levels, we either upload to all the levels or just the |
| 701 | // first. |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 702 | SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1)); |
Greg Daniel | dd20e91 | 2017-04-07 14:42:23 -0400 | [diff] [blame] | 703 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 704 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 705 | SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType, |
| 706 | GrSRGBEncoded::kNo))); |
| 707 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 708 | if (width == 0 || height == 0) { |
| 709 | return false; |
| 710 | } |
| 711 | |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 712 | if (GrPixelConfigToColorType(tex->config()) != dataColorType) { |
| 713 | return false; |
| 714 | } |
| 715 | |
| 716 | // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the |
| 717 | // dst RGB texture. Thus we do not upload mip levels for that. |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 718 | if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) { |
| 719 | SkASSERT(tex->config() == kRGB_888_GrPixelConfig); |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 720 | // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a |
| 721 | // blit or draw. |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 722 | if (!this->vkCaps().formatCanBeDstofBlit(VK_FORMAT_R8G8B8_UNORM, tex->isLinearTiled()) && |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 723 | !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) { |
| 724 | return false; |
| 725 | } |
| 726 | mipLevelCount = 1; |
| 727 | } |
| 728 | |
Brian Salomon | d1eaf49 | 2017-05-18 10:02:08 -0400 | [diff] [blame] | 729 | SkASSERT(this->caps()->isConfigTexturable(tex->config())); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 730 | int bpp = GrColorTypeBytesPerPixel(dataColorType); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 731 | |
| 732 | // texels is const. |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 733 | // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes. |
| 734 | // Because of this we need to make a non-const shallow copy of texels. |
Robert Phillips | 0f99277 | 2017-07-12 08:24:56 -0400 | [diff] [blame] | 735 | SkAutoTMalloc<GrMipLevel> texelsShallowCopy; |
| 736 | |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 737 | texelsShallowCopy.reset(mipLevelCount); |
| 738 | memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 739 | |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 740 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 741 | individualMipOffsets.push_back(0); |
| 742 | size_t combinedBufferSize = width * bpp * height; |
| 743 | int currentWidth = width; |
| 744 | int currentHeight = height; |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 745 | if (!texelsShallowCopy[0].fPixels) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 746 | combinedBufferSize = 0; |
| 747 | } |
| 748 | |
Greg Daniel | 468fd63 | 2017-03-22 17:03:45 -0400 | [diff] [blame] | 749 | // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image |
| 750 | // config. This works with the assumption that the bytes in pixel config is always a power of 2. |
| 751 | SkASSERT((bpp & (bpp - 1)) == 0); |
| 752 | const size_t alignmentMask = 0x3 | (bpp - 1); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 753 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) { |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 754 | currentWidth = SkTMax(1, currentWidth/2); |
| 755 | currentHeight = SkTMax(1, currentHeight/2); |
Greg Daniel | 660cc99 | 2017-06-26 14:55:05 -0400 | [diff] [blame] | 756 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 757 | if (texelsShallowCopy[currentMipLevel].fPixels) { |
| 758 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 759 | const size_t alignmentDiff = combinedBufferSize & alignmentMask; |
| 760 | if (alignmentDiff != 0) { |
| 761 | combinedBufferSize += alignmentMask - alignmentDiff + 1; |
| 762 | } |
| 763 | individualMipOffsets.push_back(combinedBufferSize); |
| 764 | combinedBufferSize += trimmedSize; |
| 765 | } else { |
| 766 | individualMipOffsets.push_back(0); |
Greg Daniel | 468fd63 | 2017-03-22 17:03:45 -0400 | [diff] [blame] | 767 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 768 | } |
| 769 | if (0 == combinedBufferSize) { |
| 770 | // We don't actually have any data to upload so just return success |
| 771 | return true; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | // allocate buffer to hold our mip data |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 775 | sk_sp<GrVkTransferBuffer> transferBuffer = |
| 776 | GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type); |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 777 | if (!transferBuffer) { |
Forrest Reiling | c04f845 | 2017-04-26 19:26:12 -0700 | [diff] [blame] | 778 | return false; |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 779 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 780 | |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 781 | int uploadLeft = left; |
| 782 | int uploadTop = top; |
| 783 | GrVkTexture* uploadTexture = tex; |
| 784 | // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an |
| 785 | // R8G8B8A8_UNORM image and then copy it. |
| 786 | sk_sp<GrVkTexture> copyTexture; |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 787 | if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) { |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 788 | GrSurfaceDesc surfDesc; |
| 789 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 790 | surfDesc.fWidth = width; |
| 791 | surfDesc.fHeight = height; |
| 792 | surfDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 793 | surfDesc.fSampleCnt = 1; |
| 794 | |
| 795 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT | |
| 796 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 797 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 798 | |
| 799 | GrVkImage::ImageDesc imageDesc; |
| 800 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 801 | imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM; |
| 802 | imageDesc.fWidth = width; |
| 803 | imageDesc.fHeight = height; |
| 804 | imageDesc.fLevels = 1; |
| 805 | imageDesc.fSamples = 1; |
| 806 | imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 807 | imageDesc.fUsageFlags = usageFlags; |
| 808 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 809 | |
| 810 | copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc, |
| 811 | GrMipMapsStatus::kNotAllocated); |
| 812 | if (!copyTexture) { |
| 813 | return false; |
| 814 | } |
Greg Daniel | 5c7b541 | 2019-05-10 11:39:55 -0400 | [diff] [blame] | 815 | |
| 816 | bool dstHasYcbcr = tex->ycbcrConversionInfo().isValid(); |
| 817 | if (!this->vkCaps().canCopyAsBlit(tex->config(), 1, false, dstHasYcbcr, |
| 818 | copyTexture->config(), 1, false, |
| 819 | false) && |
| 820 | !this->vkCaps().canCopyAsDraw(tex->config(), SkToBool(tex->asRenderTarget()), |
| 821 | dstHasYcbcr, |
| 822 | copyTexture->config(), true, false)) { |
| 823 | return false; |
| 824 | } |
| 825 | |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 826 | uploadTexture = copyTexture.get(); |
| 827 | uploadLeft = 0; |
| 828 | uploadTop = 0; |
| 829 | } |
| 830 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 831 | char* buffer = (char*) transferBuffer->map(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 832 | SkTArray<VkBufferImageCopy> regions(mipLevelCount); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 833 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 834 | currentWidth = width; |
| 835 | currentHeight = height; |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 836 | int layerHeight = uploadTexture->height(); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 837 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 838 | if (texelsShallowCopy[currentMipLevel].fPixels) { |
| 839 | SkASSERT(1 == mipLevelCount || currentHeight == layerHeight); |
| 840 | const size_t trimRowBytes = currentWidth * bpp; |
| 841 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes |
| 842 | ? texelsShallowCopy[currentMipLevel].fRowBytes |
| 843 | : trimRowBytes; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 844 | |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 845 | // copy data into the buffer, skipping the trailing bytes |
| 846 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
| 847 | const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels; |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 848 | SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight); |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 849 | |
| 850 | VkBufferImageCopy& region = regions.push_back(); |
| 851 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 852 | region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel]; |
| 853 | region.bufferRowLength = currentWidth; |
| 854 | region.bufferImageHeight = currentHeight; |
| 855 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 }; |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 856 | region.imageOffset = {uploadLeft, uploadTop, 0}; |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 857 | region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 }; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 858 | } |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 859 | currentWidth = SkTMax(1, currentWidth/2); |
| 860 | currentHeight = SkTMax(1, currentHeight/2); |
Greg Daniel | a1b282b | 2017-03-28 14:56:46 -0400 | [diff] [blame] | 861 | layerHeight = currentHeight; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 862 | } |
| 863 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 864 | // no need to flush non-coherent memory, unmap will do that for us |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 865 | transferBuffer->unmap(); |
| 866 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 867 | // Change layout of our target so it can be copied to |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 868 | uploadTexture->setImageLayout(this, |
| 869 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 870 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 871 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 872 | false); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 873 | |
| 874 | // Copy the buffer to the image |
| 875 | fCurrentCmdBuffer->copyBufferToImage(this, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 876 | transferBuffer.get(), |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 877 | uploadTexture, |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 878 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 879 | regions.count(), |
| 880 | regions.begin()); |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 881 | |
| 882 | // If we copied the data into a temporary image first, copy that image into our main texture |
| 883 | // now. |
| 884 | if (copyTexture.get()) { |
| 885 | SkASSERT(dataColorType == GrColorType::kRGB_888x); |
| 886 | static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin; |
| 887 | SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin, |
| 888 | SkIRect::MakeWH(width, height), SkIPoint::Make(left, top), |
| 889 | false)); |
| 890 | } |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 891 | if (1 == mipLevelCount) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 892 | tex->texturePriv().markMipMapsDirty(); |
Greg Daniel | dd20e91 | 2017-04-07 14:42:23 -0400 | [diff] [blame] | 893 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 894 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 895 | return true; |
| 896 | } |
| 897 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 898 | // It's probably possible to roll this into uploadTexDataOptimal, |
| 899 | // but for now it's easier to maintain as a separate entity. |
| 900 | bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height, |
| 901 | GrColorType dataColorType, const GrMipLevel texels[], |
| 902 | int mipLevelCount) { |
| 903 | SkASSERT(!tex->isLinearTiled()); |
| 904 | // For now the assumption is that our rect is the entire texture. |
| 905 | // Compressed textures are read-only so this should be a reasonable assumption. |
| 906 | SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height()); |
| 907 | |
| 908 | // We assume that if the texture has mip levels, we either upload to all the levels or just the |
| 909 | // first. |
| 910 | SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1)); |
| 911 | |
| 912 | SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType, |
| 913 | GrSRGBEncoded::kNo))); |
| 914 | |
| 915 | if (width == 0 || height == 0) { |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | if (GrPixelConfigToColorType(tex->config()) != dataColorType) { |
| 920 | return false; |
| 921 | } |
| 922 | |
| 923 | SkASSERT(this->caps()->isConfigTexturable(tex->config())); |
| 924 | |
| 925 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
| 926 | individualMipOffsets.push_back(0); |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 927 | size_t combinedBufferSize = GrVkFormatCompressedDataSize(tex->imageFormat(), width, height); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 928 | int currentWidth = width; |
| 929 | int currentHeight = height; |
| 930 | if (!texels[0].fPixels) { |
| 931 | return false; |
| 932 | } |
| 933 | |
| 934 | // We assume that the alignment for any compressed format is at least 4 bytes and so we don't |
| 935 | // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes. |
| 936 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 937 | currentWidth = SkTMax(1, currentWidth / 2); |
| 938 | currentHeight = SkTMax(1, currentHeight / 2); |
| 939 | |
| 940 | if (texels[currentMipLevel].fPixels) { |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 941 | const size_t dataSize = GrVkFormatCompressedDataSize(tex->imageFormat(), currentWidth, |
| 942 | currentHeight); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 943 | individualMipOffsets.push_back(combinedBufferSize); |
| 944 | combinedBufferSize += dataSize; |
| 945 | } else { |
| 946 | return false; |
| 947 | } |
| 948 | } |
| 949 | if (0 == combinedBufferSize) { |
| 950 | // We don't have any data to upload so fail (compressed textures are read-only). |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | // allocate buffer to hold our mip data |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 955 | sk_sp<GrVkTransferBuffer> transferBuffer = |
| 956 | GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 957 | if (!transferBuffer) { |
| 958 | return false; |
| 959 | } |
| 960 | |
| 961 | int uploadLeft = left; |
| 962 | int uploadTop = top; |
| 963 | GrVkTexture* uploadTexture = tex; |
| 964 | |
| 965 | char* buffer = (char*)transferBuffer->map(); |
| 966 | SkTArray<VkBufferImageCopy> regions(mipLevelCount); |
| 967 | |
| 968 | currentWidth = width; |
| 969 | currentHeight = height; |
| 970 | int layerHeight = uploadTexture->height(); |
| 971 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 972 | if (texels[currentMipLevel].fPixels) { |
| 973 | // Again, we're assuming that our rect is the entire texture |
| 974 | SkASSERT(currentHeight == layerHeight); |
| 975 | SkASSERT(0 == uploadLeft && 0 == uploadTop); |
| 976 | |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 977 | const size_t dataSize = GrVkFormatCompressedDataSize(tex->imageFormat(), currentWidth, |
| 978 | currentHeight); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 979 | |
| 980 | // copy data into the buffer, skipping the trailing bytes |
| 981 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
| 982 | const char* src = (const char*)texels[currentMipLevel].fPixels; |
| 983 | memcpy(dst, src, dataSize); |
| 984 | |
| 985 | VkBufferImageCopy& region = regions.push_back(); |
| 986 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 987 | region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel]; |
| 988 | region.bufferRowLength = currentWidth; |
| 989 | region.bufferImageHeight = currentHeight; |
| 990 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 }; |
| 991 | region.imageOffset = { uploadLeft, uploadTop, 0 }; |
| 992 | region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 }; |
| 993 | } |
| 994 | currentWidth = SkTMax(1, currentWidth / 2); |
| 995 | currentHeight = SkTMax(1, currentHeight / 2); |
| 996 | layerHeight = currentHeight; |
| 997 | } |
| 998 | |
| 999 | // no need to flush non-coherent memory, unmap will do that for us |
| 1000 | transferBuffer->unmap(); |
| 1001 | |
| 1002 | // Change layout of our target so it can be copied to |
| 1003 | uploadTexture->setImageLayout(this, |
| 1004 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1005 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1006 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1007 | false); |
| 1008 | |
| 1009 | // Copy the buffer to the image |
| 1010 | fCurrentCmdBuffer->copyBufferToImage(this, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1011 | transferBuffer.get(), |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1012 | uploadTexture, |
| 1013 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1014 | regions.count(), |
| 1015 | regions.begin()); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1016 | |
| 1017 | if (1 == mipLevelCount) { |
| 1018 | tex->texturePriv().markMipMapsDirty(); |
| 1019 | } |
| 1020 | |
| 1021 | return true; |
| 1022 | } |
| 1023 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1024 | //////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1025 | sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 1026 | const GrMipLevel texels[], int mipLevelCount) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1027 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 1028 | |
| 1029 | VkFormat pixelFormat; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1030 | SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)); |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 1031 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1032 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 1033 | if (renderTarget) { |
| 1034 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 1035 | } |
| 1036 | |
| 1037 | // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and |
| 1038 | // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we |
| 1039 | // 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] | 1040 | // 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] | 1041 | // both bits, we must make sure to set the destination bit if we are uploading srcData to the |
| 1042 | // texture. |
| 1043 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 1044 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1045 | // 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] | 1046 | // 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] | 1047 | // to 1. |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1048 | int mipLevels = !mipLevelCount ? 1 : mipLevelCount; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1049 | GrVkImage::ImageDesc imageDesc; |
| 1050 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 1051 | imageDesc.fFormat = pixelFormat; |
| 1052 | imageDesc.fWidth = desc.fWidth; |
| 1053 | imageDesc.fHeight = desc.fHeight; |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 1054 | imageDesc.fLevels = mipLevels; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1055 | imageDesc.fSamples = 1; |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 1056 | imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1057 | imageDesc.fUsageFlags = usageFlags; |
Brian Salomon | 7128fdd | 2017-05-22 14:00:07 -0400 | [diff] [blame] | 1058 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1059 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 1060 | GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated; |
| 1061 | if (mipLevels > 1) { |
| 1062 | mipMapsStatus = GrMipMapsStatus::kValid; |
| 1063 | for (int i = 0; i < mipLevels; ++i) { |
| 1064 | if (!texels[i].fPixels) { |
| 1065 | mipMapsStatus = GrMipMapsStatus::kDirty; |
| 1066 | break; |
| 1067 | } |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1071 | sk_sp<GrVkTexture> tex; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1072 | if (renderTarget) { |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 1073 | tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc, |
| 1074 | imageDesc, |
| 1075 | mipMapsStatus); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1076 | } else { |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 1077 | tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | if (!tex) { |
| 1081 | return nullptr; |
| 1082 | } |
| 1083 | |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1084 | bool isCompressed = GrVkFormatIsCompressed(tex->imageFormat()); |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 1085 | auto colorType = GrPixelConfigToColorType(desc.fConfig); |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 1086 | if (mipLevelCount) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1087 | bool success; |
| 1088 | if (isCompressed) { |
| 1089 | success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight, |
| 1090 | colorType, texels, mipLevelCount); |
| 1091 | } else { |
| 1092 | success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, |
| 1093 | colorType, texels, mipLevelCount); |
| 1094 | } |
| 1095 | if (!success) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1096 | tex->unref(); |
| 1097 | return nullptr; |
| 1098 | } |
| 1099 | } |
| 1100 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1101 | if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) { |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1102 | VkClearColorValue zeroClearColor; |
| 1103 | memset(&zeroClearColor, 0, sizeof(zeroClearColor)); |
| 1104 | VkImageSubresourceRange range; |
| 1105 | range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1106 | range.baseArrayLayer = 0; |
| 1107 | range.baseMipLevel = 0; |
| 1108 | range.layerCount = 1; |
| 1109 | range.levelCount = 1; |
| 1110 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1111 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 1112 | this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range); |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 1113 | } |
Ben Wagner | ff134f2 | 2018-04-24 16:29:16 -0400 | [diff] [blame] | 1114 | return std::move(tex); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | //////////////////////////////////////////////////////////////////////////////// |
| 1118 | |
Greg Daniel | 6888c0d | 2017-08-25 11:55:50 -0400 | [diff] [blame] | 1119 | void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset, |
| 1120 | VkDeviceSize dstOffset, VkDeviceSize size) { |
| 1121 | VkBufferCopy copyRegion; |
| 1122 | copyRegion.srcOffset = srcOffset; |
| 1123 | copyRegion.dstOffset = dstOffset; |
| 1124 | copyRegion.size = size; |
| 1125 | fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, ©Region); |
| 1126 | } |
| 1127 | |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 1128 | bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src, |
| 1129 | VkDeviceSize offset, VkDeviceSize size) { |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 1130 | // Update the buffer |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 1131 | fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src); |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 1132 | |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
| 1136 | //////////////////////////////////////////////////////////////////////////////// |
| 1137 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 1138 | static bool check_image_info(const GrVkCaps& caps, |
| 1139 | const GrVkImageInfo& info, |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1140 | GrPixelConfig config, |
| 1141 | bool isWrappedRT) { |
| 1142 | if (VK_NULL_HANDLE == info.fImage) { |
| 1143 | return false; |
| 1144 | } |
| 1145 | |
| 1146 | if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1147 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 1150 | if (info.fYcbcrConversionInfo.isValid()) { |
| 1151 | if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) { |
| 1152 | return false; |
| 1153 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1154 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1155 | |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1156 | if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) { |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1160 | SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config)); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1161 | return true; |
| 1162 | } |
| 1163 | |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1164 | static bool check_tex_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) { |
| 1165 | if (info.fImageTiling == VK_IMAGE_TILING_OPTIMAL) { |
Robert Phillips | 39ef2ef | 2019-05-15 08:45:53 -0400 | [diff] [blame] | 1166 | if (!caps.isFormatTexturable(info.fFormat)) { |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | } else { |
| 1170 | SkASSERT(info.fImageTiling == VK_IMAGE_TILING_LINEAR); |
Robert Phillips | 39ef2ef | 2019-05-15 08:45:53 -0400 | [diff] [blame] | 1171 | if (!caps.isFormatTexturableLinearly(info.fFormat)) { |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | } |
| 1175 | return true; |
| 1176 | } |
| 1177 | |
| 1178 | static bool check_rt_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) { |
| 1179 | if (!caps.maxRenderTargetSampleCount(info.fFormat)) { |
| 1180 | return false; |
| 1181 | } |
| 1182 | return true; |
| 1183 | } |
| 1184 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1185 | sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 1186 | GrWrapOwnership ownership, GrWrapCacheable cacheable, |
| 1187 | GrIOType ioType) { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 1188 | GrVkImageInfo imageInfo; |
| 1189 | if (!backendTex.getVkImageInfo(&imageInfo)) { |
| 1190 | return nullptr; |
| 1191 | } |
| 1192 | |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1193 | if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1194 | return nullptr; |
| 1195 | } |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1196 | if (!check_tex_image_info(this->vkCaps(), imageInfo)) { |
| 1197 | return nullptr; |
| 1198 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1199 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1200 | GrSurfaceDesc surfDesc; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1201 | surfDesc.fFlags = kNone_GrSurfaceFlags; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1202 | surfDesc.fWidth = backendTex.width(); |
| 1203 | surfDesc.fHeight = backendTex.height(); |
| 1204 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1205 | surfDesc.fSampleCnt = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1206 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1207 | sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout(); |
| 1208 | SkASSERT(layout); |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 1209 | return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo, |
| 1210 | std::move(layout)); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1214 | int sampleCnt, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 1215 | GrWrapOwnership ownership, |
| 1216 | GrWrapCacheable cacheable) { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 1217 | GrVkImageInfo imageInfo; |
| 1218 | if (!backendTex.getVkImageInfo(&imageInfo)) { |
| 1219 | return nullptr; |
| 1220 | } |
| 1221 | |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1222 | if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) { |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1223 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1224 | } |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1225 | if (!check_tex_image_info(this->vkCaps(), imageInfo)) { |
| 1226 | return nullptr; |
| 1227 | } |
| 1228 | if (!check_rt_image_info(this->vkCaps(), imageInfo)) { |
| 1229 | return nullptr; |
| 1230 | } |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1231 | |
| 1232 | GrSurfaceDesc surfDesc; |
| 1233 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 1234 | surfDesc.fWidth = backendTex.width(); |
| 1235 | surfDesc.fHeight = backendTex.height(); |
| 1236 | surfDesc.fConfig = backendTex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1237 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config()); |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 1238 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1239 | sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout(); |
| 1240 | SkASSERT(layout); |
| 1241 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 1242 | return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget( |
| 1243 | this, surfDesc, ownership, cacheable, imageInfo, std::move(layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1244 | } |
| 1245 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 1246 | sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){ |
Greg Daniel | e79b473 | 2017-04-20 14:07:46 -0400 | [diff] [blame] | 1247 | // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In |
| 1248 | // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if |
| 1249 | // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle |
| 1250 | // creating and owning the MSAA images. |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1251 | if (backendRT.sampleCnt() > 1) { |
Greg Daniel | e79b473 | 2017-04-20 14:07:46 -0400 | [diff] [blame] | 1252 | return nullptr; |
| 1253 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1254 | |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 1255 | GrVkImageInfo info; |
| 1256 | if (!backendRT.getVkImageInfo(&info)) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 1257 | return nullptr; |
| 1258 | } |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 1259 | |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1260 | if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1261 | return nullptr; |
| 1262 | } |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1263 | if (!check_rt_image_info(this->vkCaps(), info)) { |
| 1264 | return nullptr; |
| 1265 | } |
| 1266 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1267 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1268 | GrSurfaceDesc desc; |
Brian Salomon | 0ec981b | 2017-05-15 13:48:50 -0400 | [diff] [blame] | 1269 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 1270 | desc.fWidth = backendRT.width(); |
| 1271 | desc.fHeight = backendRT.height(); |
| 1272 | desc.fConfig = backendRT.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1273 | desc.fSampleCnt = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1274 | |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 1275 | sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1276 | |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 1277 | sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1278 | std::move(layout)); |
Brian Salomon | afdc6b1 | 2018-03-09 12:02:32 -0500 | [diff] [blame] | 1279 | |
| 1280 | // We don't allow the client to supply a premade stencil buffer. We always create one if needed. |
| 1281 | SkASSERT(!backendRT.stencilBits()); |
| 1282 | if (tgt) { |
| 1283 | SkASSERT(tgt->canAttemptStencilAttachment()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1284 | } |
Brian Salomon | afdc6b1 | 2018-03-09 12:02:32 -0500 | [diff] [blame] | 1285 | |
Ben Wagner | ff134f2 | 2018-04-24 16:29:16 -0400 | [diff] [blame] | 1286 | return std::move(tgt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1287 | } |
| 1288 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1289 | sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1290 | int sampleCnt) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 1291 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1292 | GrVkImageInfo imageInfo; |
| 1293 | if (!tex.getVkImageInfo(&imageInfo)) { |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 1294 | return nullptr; |
| 1295 | } |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1296 | if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) { |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 1297 | return nullptr; |
| 1298 | } |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1299 | if (!check_rt_image_info(this->vkCaps(), imageInfo)) { |
| 1300 | return nullptr; |
| 1301 | } |
Greg Daniel | cb32415 | 2019-02-25 11:36:53 -0500 | [diff] [blame] | 1302 | |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 1303 | GrSurfaceDesc desc; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1304 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 1305 | desc.fWidth = tex.width(); |
| 1306 | desc.fHeight = tex.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 1307 | desc.fConfig = tex.config(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 1308 | desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config()); |
| 1309 | if (!desc.fSampleCnt) { |
| 1310 | return nullptr; |
| 1311 | } |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 1312 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1313 | sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout(); |
| 1314 | SkASSERT(layout); |
| 1315 | |
Ben Wagner | ff134f2 | 2018-04-24 16:29:16 -0400 | [diff] [blame] | 1316 | return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout)); |
Brian Osman | 3391029 | 2017-04-18 14:38:53 -0400 | [diff] [blame] | 1317 | } |
| 1318 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 1319 | sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget( |
| 1320 | const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) { |
| 1321 | int maxSize = this->caps()->maxTextureSize(); |
| 1322 | if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) { |
| 1323 | return nullptr; |
| 1324 | } |
| 1325 | |
| 1326 | GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat); |
| 1327 | if (!backendFormat.isValid()) { |
| 1328 | return nullptr; |
| 1329 | } |
| 1330 | GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat, |
| 1331 | imageInfo.colorType()); |
| 1332 | if (config == kUnknown_GrPixelConfig) { |
| 1333 | return nullptr; |
| 1334 | } |
| 1335 | |
| 1336 | GrSurfaceDesc desc; |
| 1337 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 1338 | desc.fWidth = imageInfo.width(); |
| 1339 | desc.fHeight = imageInfo.height(); |
| 1340 | desc.fConfig = config; |
| 1341 | desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config); |
| 1342 | if (!desc.fSampleCnt) { |
| 1343 | return nullptr; |
| 1344 | } |
| 1345 | |
| 1346 | return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo); |
| 1347 | } |
| 1348 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1349 | bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) { |
| 1350 | auto* vkTex = static_cast<GrVkTexture*>(tex); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1351 | // don't do anything for linearly tiled textures (can't have mipmaps) |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1352 | if (vkTex->isLinearTiled()) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1353 | SkDebugf("Trying to create mipmap for linear tiled texture"); |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1354 | return false; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1357 | // determine if we can blit to and from this format |
| 1358 | const GrVkCaps& caps = this->vkCaps(); |
Greg Daniel | caa795f | 2019-05-14 11:54:25 -0400 | [diff] [blame] | 1359 | if (!caps.formatCanBeDstofBlit(vkTex->imageFormat(), false) || |
| 1360 | !caps.formatCanBeSrcofBlit(vkTex->imageFormat(), false) || |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 1361 | !caps.mipMapSupport()) { |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1362 | return false; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 1365 | int width = tex->width(); |
| 1366 | int height = tex->height(); |
| 1367 | VkImageBlit blitRegion; |
| 1368 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1369 | |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1370 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 1371 | uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1; |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1372 | SkASSERT(levelCount == vkTex->mipLevels()); |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 1373 | |
Greg Daniel | da86e28 | 2018-06-13 09:41:19 -0400 | [diff] [blame] | 1374 | // change layout of the layers so we can write to them. |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1375 | vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1376 | VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1377 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1378 | // setup memory barrier |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1379 | SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat())); |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1380 | VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1381 | VkImageMemoryBarrier imageMemoryBarrier = { |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1382 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 1383 | nullptr, // pNext |
| 1384 | VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask |
| 1385 | VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask |
| 1386 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout |
| 1387 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout |
| 1388 | VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex |
| 1389 | VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex |
| 1390 | vkTex->image(), // image |
| 1391 | {aspectFlags, 0, 1, 0, 1} // subresourceRange |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1392 | }; |
| 1393 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1394 | // Blit the miplevels |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1395 | uint32_t mipLevel = 1; |
| 1396 | while (mipLevel < levelCount) { |
| 1397 | int prevWidth = width; |
| 1398 | int prevHeight = height; |
| 1399 | width = SkTMax(1, width / 2); |
| 1400 | height = SkTMax(1, height / 2); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1401 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1402 | imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1; |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 1403 | this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1404 | VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier); |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1405 | |
| 1406 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1407 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
brianosman | e9906e7 | 2016-06-08 12:44:27 -0700 | [diff] [blame] | 1408 | blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1409 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 }; |
| 1410 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
brianosman | e9906e7 | 2016-06-08 12:44:27 -0700 | [diff] [blame] | 1411 | blitRegion.dstOffsets[1] = { width, height, 1 }; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1412 | fCurrentCmdBuffer->blitImage(this, |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1413 | vkTex->resource(), |
| 1414 | vkTex->image(), |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 1415 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1416 | vkTex->resource(), |
| 1417 | vkTex->image(), |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 1418 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1419 | 1, |
| 1420 | &blitRegion, |
| 1421 | VK_FILTER_LINEAR); |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 1422 | ++mipLevel; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1423 | } |
Greg Daniel | ee54f23 | 2019-04-03 14:58:40 -0400 | [diff] [blame] | 1424 | if (levelCount > 1) { |
| 1425 | // This barrier logically is not needed, but it changes the final level to the same layout |
| 1426 | // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the |
| 1427 | // layouts and future layout changes easier. The alternative here would be to track layout |
| 1428 | // and memory accesses per layer which doesn't seem work it. |
| 1429 | imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1; |
| 1430 | this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1431 | VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier); |
| 1432 | vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 1433 | } |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 1434 | return true; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1437 | //////////////////////////////////////////////////////////////////////////////// |
| 1438 | |
| 1439 | GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 1440 | int width, |
| 1441 | int height) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1442 | SkASSERT(width >= rt->width()); |
| 1443 | SkASSERT(height >= rt->height()); |
| 1444 | |
| 1445 | int samples = rt->numStencilSamples(); |
| 1446 | |
Ethan Nicholas | f610bae | 2018-09-20 16:55:21 -0400 | [diff] [blame] | 1447 | const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1448 | |
| 1449 | GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1450 | width, |
| 1451 | height, |
| 1452 | samples, |
| 1453 | sFmt)); |
| 1454 | fStats.incStencilAttachmentCreates(); |
| 1455 | return stencil; |
| 1456 | } |
| 1457 | |
| 1458 | //////////////////////////////////////////////////////////////////////////////// |
| 1459 | |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1460 | bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1461 | size_t bufferOffset, size_t srcRowBytes, |
| 1462 | GrPixelConfig destConfig, size_t dstRowBytes, |
| 1463 | size_t trimRowBytes, int width, int height, const SkColor4f* color) { |
| 1464 | SkASSERT(srcData || color); |
| 1465 | |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1466 | VkDeviceSize size = dstRowBytes * height; |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 1467 | VkDeviceSize offset = bufferOffset; |
| 1468 | SkASSERT(size + offset <= alloc.fSize); |
| 1469 | void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc); |
| 1470 | if (!mapPtr) { |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1471 | return false; |
| 1472 | } |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 1473 | mapPtr = reinterpret_cast<char*>(mapPtr) + offset; |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1474 | |
Greg Daniel | 20ece3a | 2017-03-28 10:24:43 -0400 | [diff] [blame] | 1475 | if (srcData) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1476 | SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, height); |
Greg Daniel | 20ece3a | 2017-03-28 10:24:43 -0400 | [diff] [blame] | 1477 | } else { |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1478 | if (kRGB_ETC1_GrPixelConfig == destConfig) { |
| 1479 | GrFillInETC1WithColor(width, height, *color, mapPtr); |
| 1480 | } else { |
| 1481 | GrFillBufferWithColor(destConfig, width, height, *color, mapPtr); |
| 1482 | } |
Greg Daniel | 20ece3a | 2017-03-28 10:24:43 -0400 | [diff] [blame] | 1483 | } |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 1484 | GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size); |
| 1485 | GrVkMemory::UnmapAlloc(gpu, alloc); |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1486 | return true; |
| 1487 | } |
| 1488 | |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1489 | static size_t compute_combined_buffer_size(VkFormat format, size_t bpp, int w, int h, |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 1490 | SkTArray<size_t>* individualMipOffsets, |
| 1491 | uint32_t mipLevels) { |
| 1492 | |
| 1493 | size_t combinedBufferSize = w * bpp * h; |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1494 | if (GrVkFormatIsCompressed(format)) { |
| 1495 | combinedBufferSize = GrVkFormatCompressedDataSize(format, w, h); |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | int currentWidth = w; |
| 1499 | int currentHeight = h; |
| 1500 | |
| 1501 | // The Vulkan spec for copying a buffer to an image, requires that the alignment must be at |
| 1502 | // least 4 bytes and a multiple of the bytes per pixel of the image config. |
| 1503 | SkASSERT(bpp == 1 || bpp == 2 || bpp == 3 || bpp == 4 || bpp == 8 || bpp == 16); |
| 1504 | int desiredAlignment = (bpp == 3) ? 12 : (bpp > 4 ? bpp : 4); |
| 1505 | |
| 1506 | for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) { |
| 1507 | currentWidth = SkTMax(1, currentWidth / 2); |
| 1508 | currentHeight = SkTMax(1, currentHeight / 2); |
| 1509 | |
| 1510 | size_t trimmedSize; |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1511 | if (GrVkFormatIsCompressed(format)) { |
| 1512 | trimmedSize = GrVkFormatCompressedDataSize(format, currentWidth, currentHeight); |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 1513 | } else { |
| 1514 | trimmedSize = currentWidth * bpp * currentHeight; |
| 1515 | } |
| 1516 | const size_t alignmentDiff = combinedBufferSize % desiredAlignment; |
| 1517 | if (alignmentDiff != 0) { |
| 1518 | combinedBufferSize += desiredAlignment - alignmentDiff; |
| 1519 | } |
| 1520 | SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bpp)); |
| 1521 | |
| 1522 | individualMipOffsets->push_back(combinedBufferSize); |
| 1523 | combinedBufferSize += trimmedSize; |
| 1524 | } |
| 1525 | |
| 1526 | return combinedBufferSize; |
| 1527 | } |
| 1528 | |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1529 | bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable, |
| 1530 | bool renderable, GrMipMapped mipMapped, const void* srcData, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1531 | size_t srcRowBytes, const SkColor4f& color, |
| 1532 | GrVkImageInfo* info) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1533 | SkASSERT(texturable || renderable); |
| 1534 | if (!texturable) { |
| 1535 | SkASSERT(GrMipMapped::kNo == mipMapped); |
| 1536 | SkASSERT(!srcData); |
| 1537 | } |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 1538 | VkFormat vkFormat; |
| 1539 | if (!GrPixelConfigToVkFormat(config, &vkFormat)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1540 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1541 | } |
| 1542 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 1543 | if (texturable && !fVkCaps->isFormatTexturable(vkFormat)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1544 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1545 | } |
| 1546 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 1547 | if (renderable && !fVkCaps->isFormatRenderable(vkFormat)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1548 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | // Currently we don't support uploading pixel data when mipped. |
| 1552 | if (srcData && GrMipMapped::kYes == mipMapped) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1553 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1554 | } |
| 1555 | |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1556 | VkImageUsageFlags usageFlags = 0; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1557 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 1558 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1559 | if (texturable) { |
| 1560 | usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT; |
| 1561 | } |
| 1562 | if (renderable) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1563 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 1564 | } |
| 1565 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1566 | // Figure out the number of mip levels. |
| 1567 | uint32_t mipLevels = 1; |
| 1568 | if (GrMipMapped::kYes == mipMapped) { |
| 1569 | mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1; |
| 1570 | } |
| 1571 | |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1572 | GrVkImage::ImageDesc imageDesc; |
| 1573 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 1574 | imageDesc.fFormat = vkFormat; |
| 1575 | imageDesc.fWidth = w; |
| 1576 | imageDesc.fHeight = h; |
| 1577 | imageDesc.fLevels = mipLevels; |
| 1578 | imageDesc.fSamples = 1; |
| 1579 | imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 1580 | imageDesc.fUsageFlags = usageFlags; |
| 1581 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1582 | |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1583 | if (!GrVkImage::InitImageInfo(this, imageDesc, info)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1584 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | // We need to declare these early so that we can delete them at the end outside of the if block. |
Greg Daniel | 8385a8a | 2018-02-26 13:29:37 -0500 | [diff] [blame] | 1588 | GrVkAlloc bufferAlloc; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1589 | VkBuffer buffer = VK_NULL_HANDLE; |
| 1590 | |
| 1591 | VkResult err; |
| 1592 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 1593 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
| 1594 | nullptr, // pNext |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1595 | fCmdPool->vkCommandPool(), // commandPool |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1596 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level |
| 1597 | 1 // bufferCount |
| 1598 | }; |
| 1599 | |
| 1600 | VkCommandBuffer cmdBuffer; |
| 1601 | err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer)); |
| 1602 | if (err) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1603 | GrVkImage::DestroyImageInfo(this, info); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1604 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 1608 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 1609 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 1610 | cmdBufferBeginInfo.pNext = nullptr; |
| 1611 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 1612 | cmdBufferBeginInfo.pInheritanceInfo = nullptr; |
| 1613 | |
| 1614 | err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo)); |
| 1615 | SkASSERT(!err); |
| 1616 | |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1617 | size_t bpp = GrVkBytesPerFormat(vkFormat); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1618 | SkASSERT(w && h); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1619 | |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1620 | SkTArray<size_t> individualMipOffsets(mipLevels); |
| 1621 | individualMipOffsets.push_back(0); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1622 | |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1623 | size_t combinedBufferSize = compute_combined_buffer_size(vkFormat, bpp, w, h, |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 1624 | &individualMipOffsets, mipLevels); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1625 | |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1626 | VkBufferCreateInfo bufInfo; |
| 1627 | memset(&bufInfo, 0, sizeof(VkBufferCreateInfo)); |
| 1628 | bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 1629 | bufInfo.flags = 0; |
| 1630 | bufInfo.size = combinedBufferSize; |
| 1631 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
| 1632 | bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 1633 | bufInfo.queueFamilyIndexCount = 0; |
| 1634 | bufInfo.pQueueFamilyIndices = nullptr; |
| 1635 | err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1636 | |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1637 | if (err) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1638 | GrVkImage::DestroyImageInfo(this, info); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1639 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1640 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer)); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1641 | return false; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1642 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1643 | |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1644 | if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true, |
| 1645 | &bufferAlloc)) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1646 | GrVkImage::DestroyImageInfo(this, info); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1647 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1648 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1649 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer)); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1650 | return false; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1651 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1652 | |
Robert Phillips | 42dda08 | 2019-05-14 13:29:45 -0400 | [diff] [blame] | 1653 | int currentWidth = w; |
| 1654 | int currentHeight = h; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1655 | for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) { |
| 1656 | SkASSERT(0 == currentMipLevel || !srcData); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1657 | size_t bufferOffset = individualMipOffsets[currentMipLevel]; |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1658 | bool result; |
Robert Phillips | 1f09898 | 2019-05-15 10:27:36 -0400 | [diff] [blame] | 1659 | if (GrVkFormatIsCompressed(vkFormat)) { |
| 1660 | size_t levelSize = GrVkFormatCompressedDataSize(vkFormat, currentWidth, currentHeight); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1661 | size_t currentRowBytes = levelSize / currentHeight; |
| 1662 | result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1663 | config, currentRowBytes, currentRowBytes, |
| 1664 | currentWidth, currentHeight, &color); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1665 | } else { |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame^] | 1666 | const size_t trimRowBytes = bpp * currentWidth; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1667 | if (!srcRowBytes) { |
| 1668 | srcRowBytes = trimRowBytes; |
| 1669 | } |
| 1670 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1671 | size_t currentRowBytes = bpp * currentWidth; |
| 1672 | result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1673 | config, currentRowBytes, trimRowBytes, |
| 1674 | currentWidth, currentHeight, &color); |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 1675 | } |
| 1676 | if (!result) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1677 | GrVkImage::DestroyImageInfo(this, info); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1678 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1679 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1680 | VK_CALL(EndCommandBuffer(cmdBuffer)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1681 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer)); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1682 | return false; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1683 | } |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1684 | currentWidth = SkTMax(1, currentWidth / 2); |
| 1685 | currentHeight = SkTMax(1, currentHeight / 2); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1686 | } |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1687 | |
| 1688 | // Set image layout and add barrier |
| 1689 | VkImageMemoryBarrier barrier; |
| 1690 | memset(&barrier, 0, sizeof(VkImageMemoryBarrier)); |
| 1691 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1692 | barrier.pNext = nullptr; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1693 | barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(info->fImageLayout); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1694 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1695 | barrier.oldLayout = info->fImageLayout; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1696 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1697 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1698 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1699 | barrier.image = info->fImage; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1700 | barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1}; |
| 1701 | |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1702 | VK_CALL(CmdPipelineBarrier(cmdBuffer, |
| 1703 | GrVkImage::LayoutToPipelineSrcStageFlags(info->fImageLayout), |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1704 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, |
| 1705 | &barrier)); |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1706 | info->fImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1707 | |
| 1708 | SkTArray<VkBufferImageCopy> regions(mipLevels); |
| 1709 | |
| 1710 | currentWidth = w; |
| 1711 | currentHeight = h; |
| 1712 | for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) { |
| 1713 | // Submit copy command |
| 1714 | VkBufferImageCopy& region = regions.push_back(); |
| 1715 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 1716 | region.bufferOffset = individualMipOffsets[currentMipLevel]; |
| 1717 | region.bufferRowLength = currentWidth; |
| 1718 | region.bufferImageHeight = currentHeight; |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame^] | 1719 | region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, currentMipLevel, 0, 1}; |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1720 | region.imageOffset = {0, 0, 0}; |
| 1721 | region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1}; |
| 1722 | currentWidth = SkTMax(1, currentWidth / 2); |
| 1723 | currentHeight = SkTMax(1, currentHeight / 2); |
| 1724 | } |
| 1725 | |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1726 | VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, info->fImage, info->fImageLayout, |
| 1727 | regions.count(), regions.begin())); |
Brian Salomon | de9f546 | 2018-03-07 14:23:58 -0500 | [diff] [blame] | 1728 | |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1729 | if (texturable) { |
| 1730 | // Change Image layout to shader read since if we use this texture as a borrowed textures |
| 1731 | // within Ganesh we require that its layout be set to that |
| 1732 | memset(&barrier, 0, sizeof(VkImageMemoryBarrier)); |
| 1733 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1734 | barrier.pNext = nullptr; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1735 | barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(info->fImageLayout); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1736 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1737 | barrier.oldLayout = info->fImageLayout; |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1738 | barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 1739 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1740 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1741 | barrier.image = info->fImage; |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1742 | barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1}; |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1743 | VK_CALL(CmdPipelineBarrier(cmdBuffer, |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1744 | GrVkImage::LayoutToPipelineSrcStageFlags(info->fImageLayout), |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 1745 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1746 | 0, |
| 1747 | 0, nullptr, |
| 1748 | 0, nullptr, |
| 1749 | 1, &barrier)); |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1750 | info->fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1751 | } |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1752 | |
| 1753 | // End CommandBuffer |
| 1754 | err = VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1755 | SkASSERT(!err); |
| 1756 | |
| 1757 | // Create Fence for queue |
| 1758 | VkFence fence; |
| 1759 | VkFenceCreateInfo fenceInfo; |
| 1760 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 1761 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 1762 | |
| 1763 | err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence)); |
| 1764 | SkASSERT(!err); |
| 1765 | |
| 1766 | VkSubmitInfo submitInfo; |
| 1767 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 1768 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1769 | submitInfo.pNext = nullptr; |
| 1770 | submitInfo.waitSemaphoreCount = 0; |
| 1771 | submitInfo.pWaitSemaphores = nullptr; |
| 1772 | submitInfo.pWaitDstStageMask = 0; |
| 1773 | submitInfo.commandBufferCount = 1; |
| 1774 | submitInfo.pCommandBuffers = &cmdBuffer; |
| 1775 | submitInfo.signalSemaphoreCount = 0; |
| 1776 | submitInfo.pSignalSemaphores = nullptr; |
| 1777 | err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence)); |
| 1778 | SkASSERT(!err); |
| 1779 | |
| 1780 | err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX)); |
| 1781 | if (VK_TIMEOUT == err) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1782 | GrVkImage::DestroyImageInfo(this, info); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1783 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1784 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1785 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1786 | VK_CALL(DestroyFence(fDevice, fence, nullptr)); |
| 1787 | SkDebugf("Fence failed to signal: %d\n", err); |
| 1788 | SK_ABORT("failing"); |
| 1789 | } |
| 1790 | SkASSERT(!err); |
| 1791 | |
| 1792 | // Clean up transfer resources |
| 1793 | if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash |
| 1794 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1795 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1796 | } |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1797 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer)); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1798 | VK_CALL(DestroyFence(fDevice, fence, nullptr)); |
| 1799 | |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1800 | return true; |
| 1801 | } |
| 1802 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 1803 | static bool vk_format_to_pixel_config(VkFormat format, GrPixelConfig* config) { |
| 1804 | GrPixelConfig dontCare; |
| 1805 | if (!config) { |
| 1806 | config = &dontCare; |
| 1807 | } |
| 1808 | |
| 1809 | switch (format) { |
| 1810 | case VK_FORMAT_UNDEFINED: |
| 1811 | *config = kUnknown_GrPixelConfig; |
| 1812 | return false; |
| 1813 | case VK_FORMAT_R8G8B8A8_UNORM: |
| 1814 | *config = kRGBA_8888_GrPixelConfig; |
| 1815 | return true; |
| 1816 | case VK_FORMAT_R8G8B8_UNORM: |
| 1817 | *config = kRGB_888_GrPixelConfig; |
| 1818 | return true; |
| 1819 | case VK_FORMAT_R8G8_UNORM: |
| 1820 | *config = kRG_88_GrPixelConfig; |
| 1821 | return true; |
| 1822 | case VK_FORMAT_B8G8R8A8_UNORM: |
| 1823 | *config = kBGRA_8888_GrPixelConfig; |
| 1824 | return true; |
| 1825 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 1826 | *config = kSRGBA_8888_GrPixelConfig; |
| 1827 | return true; |
| 1828 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 1829 | *config = kSBGRA_8888_GrPixelConfig; |
| 1830 | return true; |
| 1831 | case VK_FORMAT_A2B10G10R10_UNORM_PACK32: |
| 1832 | *config = kRGBA_1010102_GrPixelConfig; |
| 1833 | return true; |
| 1834 | case VK_FORMAT_R5G6B5_UNORM_PACK16: |
| 1835 | *config = kRGB_565_GrPixelConfig; |
| 1836 | return true; |
| 1837 | case VK_FORMAT_B4G4R4A4_UNORM_PACK16: |
| 1838 | *config = kRGBA_4444_GrPixelConfig; // we're swizzling in this case |
| 1839 | return true; |
| 1840 | case VK_FORMAT_R4G4B4A4_UNORM_PACK16: |
| 1841 | *config = kRGBA_4444_GrPixelConfig; |
| 1842 | return true; |
| 1843 | case VK_FORMAT_R8_UNORM: |
| 1844 | *config = kAlpha_8_GrPixelConfig; |
| 1845 | return true; |
| 1846 | case VK_FORMAT_R32G32B32A32_SFLOAT: |
| 1847 | *config = kRGBA_float_GrPixelConfig; |
| 1848 | return true; |
| 1849 | case VK_FORMAT_R32G32_SFLOAT: |
| 1850 | *config = kRG_float_GrPixelConfig; |
| 1851 | return true; |
| 1852 | case VK_FORMAT_R16G16B16A16_SFLOAT: |
| 1853 | *config = kRGBA_half_GrPixelConfig; |
| 1854 | return true; |
| 1855 | case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: |
| 1856 | *config = kRGB_ETC1_GrPixelConfig; |
| 1857 | return true; |
| 1858 | case VK_FORMAT_R16_SFLOAT: |
| 1859 | *config = kAlpha_half_GrPixelConfig; |
| 1860 | return true; |
| 1861 | default: |
| 1862 | return false; |
| 1863 | } |
| 1864 | SK_ABORT("Unexpected config"); |
| 1865 | return false; |
| 1866 | } |
| 1867 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 1868 | GrBackendTexture GrVkGpu::createBackendTexture(int w, int h, |
| 1869 | const GrBackendFormat& format, |
| 1870 | GrMipMapped mipMapped, |
| 1871 | GrRenderable renderable, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 1872 | const void* srcData, size_t rowBytes, |
| 1873 | const SkColor4f& color) { |
Brian Salomon | 8a37583 | 2018-03-14 10:21:40 -0400 | [diff] [blame] | 1874 | this->handleDirtyContext(); |
Robert Phillips | a479f96 | 2018-04-10 11:45:40 -0400 | [diff] [blame] | 1875 | |
| 1876 | if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) { |
| 1877 | return GrBackendTexture(); |
| 1878 | } |
| 1879 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 1880 | const VkFormat* vkFormat = format.getVkFormat(); |
| 1881 | if (!vkFormat) { |
| 1882 | return GrBackendTexture(); |
| 1883 | } |
| 1884 | |
| 1885 | GrPixelConfig config; |
| 1886 | |
| 1887 | if (!vk_format_to_pixel_config(*vkFormat, &config)) { |
| 1888 | return GrBackendTexture(); |
| 1889 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 1890 | if (!this->caps()->isConfigTexturable(config)) { |
| 1891 | return GrBackendTexture(); |
| 1892 | } |
| 1893 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1894 | GrVkImageInfo info; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 1895 | if (!this->createTestingOnlyVkImage(config, w, h, true, GrRenderable::kYes == renderable, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1896 | mipMapped, srcData, rowBytes, color, &info)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1897 | return {}; |
| 1898 | } |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 1899 | GrBackendTexture beTex = GrBackendTexture(w, h, info); |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 1900 | #if GR_TEST_UTILS |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 1901 | // Lots of tests don't go through Skia's public interface which will set the config so for |
| 1902 | // testing we make sure we set a config here. |
| 1903 | beTex.setPixelConfig(config); |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 1904 | #endif |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 1905 | return beTex; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1906 | } |
| 1907 | |
Robert Phillips | f0313ee | 2019-05-21 13:51:11 -0400 | [diff] [blame] | 1908 | void GrVkGpu::deleteBackendTexture(const GrBackendTexture& tex) { |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame] | 1909 | SkASSERT(GrBackendApi::kVulkan == tex.fBackend); |
| 1910 | |
| 1911 | GrVkImageInfo info; |
| 1912 | if (tex.getVkImageInfo(&info)) { |
| 1913 | GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info)); |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | #if GR_TEST_UTILS |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1918 | bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 1919 | SkASSERT(GrBackendApi::kVulkan == tex.fBackend); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 1920 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1921 | GrVkImageInfo backend; |
| 1922 | if (!tex.getVkImageInfo(&backend)) { |
| 1923 | return false; |
| 1924 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1925 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1926 | if (backend.fImage && backend.fAlloc.fMemory) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1927 | VkMemoryRequirements req; |
| 1928 | memset(&req, 0, sizeof(req)); |
| 1929 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 1930 | backend.fImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1931 | &req)); |
| 1932 | // TODO: find a better check |
| 1933 | // This will probably fail with a different driver |
| 1934 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 1935 | } |
| 1936 | |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 1940 | GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) { |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1941 | this->handleDirtyContext(); |
| 1942 | |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 1943 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
| 1944 | return GrBackendRenderTarget(); |
| 1945 | } |
| 1946 | |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 1947 | auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1948 | if (kUnknown_GrPixelConfig == config) { |
| 1949 | return {}; |
| 1950 | } |
Robert Phillips | f62e575 | 2019-05-30 10:36:13 -0400 | [diff] [blame] | 1951 | |
| 1952 | GrVkImageInfo info; |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 1953 | if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 1954 | SkColors::kTransparent, &info)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1955 | return {}; |
| 1956 | } |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 1957 | GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info); |
| 1958 | // Lots of tests don't go through Skia's public interface which will set the config so for |
| 1959 | // testing we make sure we set a config here. |
| 1960 | beRT.setPixelConfig(config); |
| 1961 | return beRT; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1962 | } |
| 1963 | |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1964 | void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 1965 | SkASSERT(GrBackendApi::kVulkan == rt.fBackend); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1966 | |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 1967 | GrVkImageInfo info; |
| 1968 | if (rt.getVkImageInfo(&info)) { |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1969 | // something in the command buffer may still be using this, so force submit |
| 1970 | this->submitCommandBuffer(kForce_SyncQueue); |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 1971 | GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info)); |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 1972 | } |
| 1973 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1974 | |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 1975 | void GrVkGpu::testingOnly_flushGpuAndSync() { |
| 1976 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1977 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 1978 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 1979 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1980 | //////////////////////////////////////////////////////////////////////////////// |
| 1981 | |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 1982 | void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource, |
| 1983 | VkPipelineStageFlags srcStageMask, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1984 | VkPipelineStageFlags dstStageMask, |
| 1985 | bool byRegion, |
| 1986 | VkBufferMemoryBarrier* barrier) const { |
| 1987 | SkASSERT(fCurrentCmdBuffer); |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 1988 | SkASSERT(resource); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1989 | fCurrentCmdBuffer->pipelineBarrier(this, |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 1990 | resource, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1991 | srcStageMask, |
| 1992 | dstStageMask, |
| 1993 | byRegion, |
| 1994 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 1995 | barrier); |
| 1996 | } |
| 1997 | |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 1998 | void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource, |
| 1999 | VkPipelineStageFlags srcStageMask, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2000 | VkPipelineStageFlags dstStageMask, |
| 2001 | bool byRegion, |
| 2002 | VkImageMemoryBarrier* barrier) const { |
| 2003 | SkASSERT(fCurrentCmdBuffer); |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 2004 | SkASSERT(resource); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2005 | fCurrentCmdBuffer->pipelineBarrier(this, |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 2006 | resource, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2007 | srcStageMask, |
| 2008 | dstStageMask, |
| 2009 | byRegion, |
| 2010 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 2011 | barrier); |
| 2012 | } |
| 2013 | |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 2014 | void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n, |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 2015 | SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info, |
| 2016 | const GrPrepareForExternalIORequests& externalRequests) { |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 2017 | SkASSERT(n >= 0); |
| 2018 | SkASSERT(!n || proxies); |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 2019 | // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does |
| 2020 | // not effect what we do here. |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 2021 | if (n && access == SkSurface::BackendSurfaceAccess::kPresent) { |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 2022 | GrVkImage* image; |
Brian Salomon | f9a1fdf | 2019-05-09 10:30:12 -0400 | [diff] [blame] | 2023 | for (int i = 0; i < n; ++i) { |
| 2024 | SkASSERT(proxies[i]->isInstantiated()); |
| 2025 | if (GrTexture* tex = proxies[i]->peekTexture()) { |
| 2026 | image = static_cast<GrVkTexture*>(tex); |
| 2027 | } else { |
| 2028 | GrRenderTarget* rt = proxies[i]->peekRenderTarget(); |
| 2029 | SkASSERT(rt); |
| 2030 | image = static_cast<GrVkRenderTarget*>(rt); |
| 2031 | } |
| 2032 | image->prepareForPresent(this); |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 2033 | } |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 2034 | } |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 2035 | |
| 2036 | // Handle requests for preparing for external IO |
| 2037 | for (int i = 0; i < externalRequests.fNumImages; ++i) { |
| 2038 | SkImage* image = externalRequests.fImages[i]; |
| 2039 | if (!image->isTextureBacked()) { |
| 2040 | continue; |
| 2041 | } |
| 2042 | SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image)); |
| 2043 | sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext()); |
| 2044 | SkASSERT(proxy); |
| 2045 | |
| 2046 | if (!proxy->isInstantiated()) { |
| 2047 | auto resourceProvider = this->getContext()->priv().resourceProvider(); |
| 2048 | if (!proxy->instantiate(resourceProvider)) { |
| 2049 | continue; |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | GrTexture* tex = proxy->peekTexture(); |
| 2054 | if (!tex) { |
| 2055 | continue; |
| 2056 | } |
| 2057 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex); |
| 2058 | vkTex->prepareForExternal(this); |
| 2059 | } |
| 2060 | for (int i = 0; i < externalRequests.fNumSurfaces; ++i) { |
| 2061 | SkSurface* surface = externalRequests.fSurfaces[i]; |
| 2062 | if (!surface->getCanvas()->getGrContext()) { |
| 2063 | continue; |
| 2064 | } |
| 2065 | SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface); |
| 2066 | auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext(); |
| 2067 | sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef(); |
| 2068 | if (!proxy->isInstantiated()) { |
| 2069 | auto resourceProvider = this->getContext()->priv().resourceProvider(); |
| 2070 | if (!proxy->instantiate(resourceProvider)) { |
| 2071 | continue; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | GrRenderTarget* rt = proxy->peekRenderTarget(); |
| 2076 | SkASSERT(rt); |
| 2077 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt); |
| 2078 | if (externalRequests.fPrepareSurfaceForPresent && |
| 2079 | externalRequests.fPrepareSurfaceForPresent[i]) { |
| 2080 | vkRT->prepareForPresent(this); |
| 2081 | } else { |
| 2082 | vkRT->prepareForExternal(this); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 2086 | if (info.fFlags & kSyncCpu_GrFlushFlag) { |
| 2087 | this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext); |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 2088 | } else { |
Greg Daniel | e6bfb7d | 2019-04-17 15:26:11 -0400 | [diff] [blame] | 2089 | this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext); |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 2090 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2091 | } |
| 2092 | |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2093 | static int get_surface_sample_cnt(GrSurface* surf) { |
| 2094 | if (const GrRenderTarget* rt = surf->asRenderTarget()) { |
| 2095 | return rt->numColorSamples(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2096 | } |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2097 | return 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2098 | } |
| 2099 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2100 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 2101 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2102 | GrVkImage* dstImage, |
| 2103 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2104 | const SkIRect& srcRect, |
| 2105 | const SkIPoint& dstPoint) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2106 | #ifdef SK_DEBUG |
| 2107 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 2108 | int srcSampleCnt = get_surface_sample_cnt(src); |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2109 | bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid(); |
| 2110 | bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid(); |
| 2111 | SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr, |
| 2112 | src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr)); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2113 | |
| 2114 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2115 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2116 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 2117 | // the cache is flushed since it is only being written to. |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2118 | dstImage->setImageLayout(this, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 2119 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 2120 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 2121 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 2122 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2123 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2124 | srcImage->setImageLayout(this, |
| 2125 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 2126 | VK_ACCESS_TRANSFER_READ_BIT, |
| 2127 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2128 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2129 | |
| 2130 | // Flip rect if necessary |
| 2131 | SkIRect srcVkRect = srcRect; |
| 2132 | int32_t dstY = dstPoint.fY; |
| 2133 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2134 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
| 2135 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2136 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 2137 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 2138 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 2139 | } |
| 2140 | |
| 2141 | VkImageCopy copyRegion; |
| 2142 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 2143 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 2144 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 2145 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 2146 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
egdaniel | c355bc8 | 2016-04-27 11:31:59 -0700 | [diff] [blame] | 2147 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2148 | |
| 2149 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2150 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2151 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2152 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2153 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 2154 | 1, |
| 2155 | ©Region); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 2156 | |
| 2157 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 2158 | srcRect.width(), srcRect.height()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2159 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2160 | } |
| 2161 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2162 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 2163 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2164 | GrVkImage* dstImage, |
| 2165 | GrVkImage* srcImage, |
| 2166 | const SkIRect& srcRect, |
| 2167 | const SkIPoint& dstPoint) { |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2168 | #ifdef SK_DEBUG |
| 2169 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 2170 | int srcSampleCnt = get_surface_sample_cnt(src); |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2171 | bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid(); |
| 2172 | bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid(); |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2173 | SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(), |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2174 | dstHasYcbcr, src->config(), srcSampleCnt, |
| 2175 | srcImage->isLinearTiled(), srcHasYcbcr)); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2176 | |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2177 | #endif |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2178 | dstImage->setImageLayout(this, |
| 2179 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 2180 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 2181 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2182 | false); |
| 2183 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2184 | srcImage->setImageLayout(this, |
| 2185 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 2186 | VK_ACCESS_TRANSFER_READ_BIT, |
| 2187 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2188 | false); |
| 2189 | |
| 2190 | // Flip rect if necessary |
| 2191 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 2192 | srcVkRect.fLeft = srcRect.fLeft; |
| 2193 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2194 | SkIRect dstRect; |
| 2195 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 2196 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2197 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2198 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2199 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 2200 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 2201 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 2202 | srcVkRect.fTop = srcRect.fTop; |
| 2203 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2206 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2207 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 2208 | } else { |
| 2209 | dstRect.fTop = dstPoint.fY; |
| 2210 | } |
| 2211 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 2212 | |
| 2213 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 2214 | // get the correct origintation of the copied data. |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2215 | if (srcOrigin != dstOrigin) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 2216 | using std::swap; |
| 2217 | swap(dstRect.fTop, dstRect.fBottom); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | VkImageBlit blitRegion; |
| 2221 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 2222 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 2223 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
Greg Daniel | e76071c | 2016-11-02 11:57:06 -0400 | [diff] [blame] | 2224 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 }; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2225 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 2226 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
Greg Daniel | e76071c | 2016-11-02 11:57:06 -0400 | [diff] [blame] | 2227 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 }; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2228 | |
| 2229 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 2230 | *srcImage, |
| 2231 | *dstImage, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2232 | 1, |
| 2233 | &blitRegion, |
| 2234 | VK_FILTER_NEAREST); // We never scale so any filter works here |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 2235 | |
Greg Daniel | 1ba1bfc | 2018-06-21 13:55:19 -0400 | [diff] [blame] | 2236 | dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2237 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2240 | void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src, |
| 2241 | GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect, |
| 2242 | const SkIPoint& origDstPoint) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 2243 | GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2244 | SkIRect srcRect = origSrcRect; |
| 2245 | SkIPoint dstPoint = origDstPoint; |
| 2246 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
| 2247 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin); |
| 2248 | srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom, |
| 2249 | origSrcRect.fRight, src->height() - origSrcRect.fTop}; |
| 2250 | dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height(); |
| 2251 | } |
| 2252 | this->resolveImage(dst, srcRT, srcRect, dstPoint); |
Greg Daniel | 1ba1bfc | 2018-06-21 13:55:19 -0400 | [diff] [blame] | 2253 | SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY, |
| 2254 | srcRect.width(), srcRect.height()); |
| 2255 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2258 | bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 2259 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 2260 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 2261 | bool canDiscardOutsideDstRect) { |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2262 | #ifdef SK_DEBUG |
| 2263 | if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) { |
| 2264 | SkASSERT(!srcRT->wrapsSecondaryCommandBuffer()); |
| 2265 | } |
| 2266 | if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) { |
| 2267 | SkASSERT(!dstRT->wrapsSecondaryCommandBuffer()); |
| 2268 | } |
| 2269 | #endif |
| 2270 | |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2271 | GrPixelConfig dstConfig = dst->config(); |
| 2272 | GrPixelConfig srcConfig = src->config(); |
| 2273 | |
| 2274 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 2275 | int srcSampleCnt = get_surface_sample_cnt(src); |
| 2276 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2277 | GrVkImage* dstImage; |
| 2278 | GrVkImage* srcImage; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 2279 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 2280 | if (dstRT) { |
| 2281 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT); |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2282 | if (vkRT->wrapsSecondaryCommandBuffer()) { |
| 2283 | return false; |
| 2284 | } |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 2285 | dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT; |
| 2286 | } else { |
| 2287 | SkASSERT(dst->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2288 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2289 | } |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 2290 | GrRenderTarget* srcRT = src->asRenderTarget(); |
| 2291 | if (srcRT) { |
| 2292 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT); |
| 2293 | srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2294 | } else { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 2295 | SkASSERT(src->asTexture()); |
| 2296 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2299 | bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid(); |
| 2300 | bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid(); |
| 2301 | |
| 2302 | if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr, |
| 2303 | srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) { |
| 2304 | this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint); |
| 2305 | return true; |
| 2306 | } |
| 2307 | |
| 2308 | if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr, |
| 2309 | srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) { |
| 2310 | SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect, |
| 2311 | dstPoint, canDiscardOutsideDstRect)); |
| 2312 | auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY); |
| 2313 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
| 2314 | return true; |
| 2315 | } |
| 2316 | |
| 2317 | if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr, |
| 2318 | srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2319 | this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage, |
| 2320 | srcRect, dstPoint); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 2321 | return true; |
| 2322 | } |
| 2323 | |
Greg Daniel | 25af671 | 2018-04-25 10:44:38 -0400 | [diff] [blame] | 2324 | if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(), |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2325 | dstHasYcbcr, srcConfig, srcSampleCnt, |
| 2326 | srcImage->isLinearTiled(), srcHasYcbcr)) { |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2327 | this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage, |
| 2328 | srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2329 | return true; |
| 2330 | } |
| 2331 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2332 | return false; |
| 2333 | } |
| 2334 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 2335 | bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height, |
| 2336 | GrColorType dstColorType, void* buffer, size_t rowBytes) { |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2337 | if (GrPixelConfigToColorType(surface->config()) != dstColorType) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2338 | return false; |
| 2339 | } |
| 2340 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2341 | GrVkImage* image = nullptr; |
| 2342 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget()); |
| 2343 | if (rt) { |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2344 | // Reading from render targets that wrap a secondary command buffer is not allowed since |
| 2345 | // it would require us to know the VkImage, which we don't have, as well as need us to |
| 2346 | // stop and start the VkRenderPass which we don't have access to. |
| 2347 | if (rt->wrapsSecondaryCommandBuffer()) { |
| 2348 | return false; |
| 2349 | } |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2350 | // resolve the render target if necessary |
| 2351 | switch (rt->getResolveType()) { |
| 2352 | case GrVkRenderTarget::kCantResolve_ResolveType: |
| 2353 | return false; |
| 2354 | case GrVkRenderTarget::kAutoResolves_ResolveType: |
| 2355 | break; |
| 2356 | case GrVkRenderTarget::kCanResolve_ResolveType: |
Greg Daniel | 0a77f43 | 2018-12-06 11:23:32 -0500 | [diff] [blame] | 2357 | this->resolveRenderTargetNoFlush(rt); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2358 | break; |
| 2359 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 2360 | SK_ABORT("Unknown resolve type"); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2361 | } |
| 2362 | image = rt; |
| 2363 | } else { |
| 2364 | image = static_cast<GrVkTexture*>(surface->asTexture()); |
| 2365 | } |
| 2366 | |
| 2367 | if (!image) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2368 | return false; |
| 2369 | } |
| 2370 | |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 2371 | // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be |
| 2372 | // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8 |
| 2373 | // image and then do the read pixels from that. |
| 2374 | sk_sp<GrVkTextureRenderTarget> copySurface; |
Greg Daniel | f259b8b | 2019-02-14 09:03:43 -0500 | [diff] [blame] | 2375 | if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) { |
| 2376 | SkASSERT(surface->config() == kRGB_888_GrPixelConfig); |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 2377 | |
| 2378 | // Make a new surface that is RGBA to copy the RGB surface into. |
| 2379 | GrSurfaceDesc surfDesc; |
| 2380 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 2381 | surfDesc.fWidth = width; |
| 2382 | surfDesc.fHeight = height; |
| 2383 | surfDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 2384 | surfDesc.fSampleCnt = 1; |
| 2385 | |
| 2386 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 2387 | VK_IMAGE_USAGE_SAMPLED_BIT | |
| 2388 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 2389 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 2390 | |
| 2391 | GrVkImage::ImageDesc imageDesc; |
| 2392 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 2393 | imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM; |
| 2394 | imageDesc.fWidth = width; |
| 2395 | imageDesc.fHeight = height; |
| 2396 | imageDesc.fLevels = 1; |
| 2397 | imageDesc.fSamples = 1; |
| 2398 | imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 2399 | imageDesc.fUsageFlags = usageFlags; |
| 2400 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 2401 | |
| 2402 | copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget( |
| 2403 | this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated); |
| 2404 | if (!copySurface) { |
| 2405 | return false; |
| 2406 | } |
| 2407 | |
| 2408 | int srcSampleCount = 0; |
| 2409 | if (rt) { |
| 2410 | srcSampleCount = rt->numColorSamples(); |
| 2411 | } |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2412 | bool srcHasYcbcr = image->ycbcrConversionInfo().isValid(); |
Greg Daniel | 5c7b541 | 2019-05-10 11:39:55 -0400 | [diff] [blame] | 2413 | if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, false, false, |
| 2414 | surface->config(), srcSampleCount, image->isLinearTiled(), |
Greg Daniel | a51e93c | 2019-03-25 12:30:45 -0400 | [diff] [blame] | 2415 | srcHasYcbcr) && |
| 2416 | !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false, |
| 2417 | surface->config(), SkToBool(surface->asTexture()), |
| 2418 | srcHasYcbcr)) { |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 2419 | return false; |
| 2420 | } |
| 2421 | SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height); |
Greg Daniel | 5c7b541 | 2019-05-10 11:39:55 -0400 | [diff] [blame] | 2422 | static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin; |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 2423 | if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin, |
| 2424 | srcRect, SkIPoint::Make(0,0))) { |
| 2425 | return false; |
| 2426 | } |
| 2427 | top = 0; |
| 2428 | left = 0; |
| 2429 | dstColorType = GrColorType::kRGBA_8888; |
| 2430 | image = copySurface.get(); |
| 2431 | } |
| 2432 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2433 | // Change layout of our target so it can be used as copy |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2434 | image->setImageLayout(this, |
| 2435 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 2436 | VK_ACCESS_TRANSFER_READ_BIT, |
| 2437 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 2438 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2439 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 2440 | int bpp = GrColorTypeBytesPerPixel(dstColorType); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 2441 | size_t tightRowBytes = bpp * width; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2442 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2443 | VkBufferImageCopy region; |
| 2444 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 2445 | |
| 2446 | bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin(); |
| 2447 | if (copyFromOrigin) { |
| 2448 | region.imageOffset = { 0, 0, 0 }; |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 2449 | region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 }; |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 2450 | } else { |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 2451 | VkOffset3D offset = { left, top, 0 }; |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 2452 | region.imageOffset = offset; |
| 2453 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 2454 | } |
| 2455 | |
| 2456 | size_t transBufferRowBytes = bpp * region.imageExtent.width; |
Greg Daniel | 386a9b6 | 2018-07-03 10:52:30 -0400 | [diff] [blame] | 2457 | size_t imageRows = region.imageExtent.height; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2458 | auto transferBuffer = sk_sp<GrVkTransferBuffer>( |
Greg Daniel | 3cdfa09 | 2018-02-26 16:14:10 -0500 | [diff] [blame] | 2459 | static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows, |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 2460 | GrGpuBufferType::kXferGpuToCpu, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2461 | kStream_GrAccessPattern) |
| 2462 | .release())); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 2463 | |
| 2464 | // Copy the image to a buffer so we can map it to cpu memory |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 2465 | region.bufferOffset = transferBuffer->offset(); |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 2466 | 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] | 2467 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 2468 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2469 | |
| 2470 | fCurrentCmdBuffer->copyImageToBuffer(this, |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2471 | image, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2472 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 2473 | transferBuffer.get(), |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2474 | 1, |
| 2475 | ®ion); |
| 2476 | |
| 2477 | // make sure the copy to buffer has finished |
| 2478 | transferBuffer->addMemoryBarrier(this, |
| 2479 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 2480 | VK_ACCESS_HOST_READ_BIT, |
| 2481 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 2482 | VK_PIPELINE_STAGE_HOST_BIT, |
| 2483 | false); |
| 2484 | |
| 2485 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 2486 | // we can copy the data out of the buffer. |
| 2487 | this->submitCommandBuffer(kForce_SyncQueue); |
Greg Daniel | 88fdee9 | 2018-02-24 22:41:50 +0000 | [diff] [blame] | 2488 | void* mappedMemory = transferBuffer->map(); |
Greg Daniel | e35a99e | 2018-03-02 11:44:22 -0500 | [diff] [blame] | 2489 | const GrVkAlloc& transAlloc = transferBuffer->alloc(); |
Greg Daniel | 81df041 | 2018-05-31 13:13:33 -0400 | [diff] [blame] | 2490 | GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2491 | |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 2492 | if (copyFromOrigin) { |
| 2493 | uint32_t skipRows = region.imageExtent.height - height; |
| 2494 | mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left; |
| 2495 | } |
| 2496 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 2497 | SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2498 | |
| 2499 | transferBuffer->unmap(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2500 | return true; |
| 2501 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 2502 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2503 | // The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple |
| 2504 | // of the granularity. The width must also be a multiple of the granularity or eaqual to the width |
| 2505 | // the the entire attachment. Similar requirements for the y and height components. |
| 2506 | void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds, |
| 2507 | const VkExtent2D& granularity, int maxWidth, int maxHeight) { |
| 2508 | // Adjust Width |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2509 | if ((0 != granularity.width && 1 != granularity.width)) { |
| 2510 | // Start with the right side of rect so we know if we end up going pass the maxWidth. |
| 2511 | int rightAdj = srcBounds.fRight % granularity.width; |
| 2512 | if (rightAdj != 0) { |
| 2513 | rightAdj = granularity.width - rightAdj; |
| 2514 | } |
| 2515 | dstBounds->fRight = srcBounds.fRight + rightAdj; |
| 2516 | if (dstBounds->fRight > maxWidth) { |
| 2517 | dstBounds->fRight = maxWidth; |
| 2518 | dstBounds->fLeft = 0; |
| 2519 | } else { |
| 2520 | dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width; |
| 2521 | } |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2522 | } else { |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2523 | dstBounds->fLeft = srcBounds.fLeft; |
| 2524 | dstBounds->fRight = srcBounds.fRight; |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | // Adjust height |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2528 | if ((0 != granularity.height && 1 != granularity.height)) { |
| 2529 | // Start with the bottom side of rect so we know if we end up going pass the maxHeight. |
| 2530 | int bottomAdj = srcBounds.fBottom % granularity.height; |
| 2531 | if (bottomAdj != 0) { |
| 2532 | bottomAdj = granularity.height - bottomAdj; |
| 2533 | } |
| 2534 | dstBounds->fBottom = srcBounds.fBottom + bottomAdj; |
| 2535 | if (dstBounds->fBottom > maxHeight) { |
| 2536 | dstBounds->fBottom = maxHeight; |
| 2537 | dstBounds->fTop = 0; |
| 2538 | } else { |
| 2539 | dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height; |
| 2540 | } |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2541 | } else { |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 2542 | dstBounds->fTop = srcBounds.fTop; |
| 2543 | dstBounds->fBottom = srcBounds.fBottom; |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 2547 | void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2548 | const GrVkRenderPass* renderPass, |
| 2549 | const VkClearValue* colorClear, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2550 | GrVkRenderTarget* target, GrSurfaceOrigin origin, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2551 | const SkIRect& bounds) { |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 2552 | SkASSERT (!target->wrapsSecondaryCommandBuffer()); |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 2553 | const SkIRect* pBounds = &bounds; |
| 2554 | SkIRect flippedBounds; |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 2555 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 2556 | flippedBounds = bounds; |
| 2557 | flippedBounds.fTop = target->height() - bounds.fBottom; |
| 2558 | flippedBounds.fBottom = target->height() - bounds.fTop; |
| 2559 | pBounds = &flippedBounds; |
| 2560 | } |
| 2561 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 2562 | // The bounds we use for the render pass should be of the granularity supported |
| 2563 | // by the device. |
| 2564 | const VkExtent2D& granularity = renderPass->granularity(); |
| 2565 | SkIRect adjustedBounds; |
| 2566 | if ((0 != granularity.width && 1 != granularity.width) || |
| 2567 | (0 != granularity.height && 1 != granularity.height)) { |
| 2568 | adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity, |
| 2569 | target->width(), target->height()); |
| 2570 | pBounds = &adjustedBounds; |
| 2571 | } |
| 2572 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2573 | #ifdef SK_DEBUG |
| 2574 | uint32_t index; |
| 2575 | bool result = renderPass->colorAttachmentIndex(&index); |
| 2576 | SkASSERT(result && 0 == index); |
| 2577 | result = renderPass->stencilAttachmentIndex(&index); |
| 2578 | if (result) { |
| 2579 | SkASSERT(1 == index); |
| 2580 | } |
| 2581 | #endif |
| 2582 | VkClearValue clears[2]; |
| 2583 | clears[0].color = colorClear->color; |
Robert Phillips | 8c326e9 | 2017-08-10 13:50:17 -0400 | [diff] [blame] | 2584 | clears[1].depthStencil.depth = 0.0f; |
| 2585 | clears[1].depthStencil.stencil = 0; |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 2586 | |
| 2587 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true); |
Greg Daniel | 22bc865 | 2017-03-22 15:45:43 -0400 | [diff] [blame] | 2588 | for (int i = 0; i < buffers.count(); ++i) { |
| 2589 | fCurrentCmdBuffer->executeCommands(this, buffers[i]); |
| 2590 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2591 | fCurrentCmdBuffer->endRenderPass(this); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 2592 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 2593 | this->didWriteToSurface(target, origin, &bounds); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 2594 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 2595 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 2596 | void GrVkGpu::submit(GrGpuCommandBuffer* buffer) { |
| 2597 | if (buffer->asRTCommandBuffer()) { |
| 2598 | SkASSERT(fCachedRTCommandBuffer.get() == buffer); |
| 2599 | |
| 2600 | fCachedRTCommandBuffer->submit(); |
| 2601 | fCachedRTCommandBuffer->reset(); |
| 2602 | } else { |
| 2603 | SkASSERT(fCachedTexCommandBuffer.get() == buffer); |
| 2604 | |
| 2605 | fCachedTexCommandBuffer->submit(); |
| 2606 | fCachedTexCommandBuffer->reset(); |
| 2607 | } |
| 2608 | } |
| 2609 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2610 | GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() { |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2611 | VkFenceCreateInfo createInfo; |
| 2612 | memset(&createInfo, 0, sizeof(VkFenceCreateInfo)); |
| 2613 | createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 2614 | createInfo.pNext = nullptr; |
| 2615 | createInfo.flags = 0; |
| 2616 | VkFence fence = VK_NULL_HANDLE; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2617 | |
| 2618 | VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence)); |
| 2619 | VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence)); |
| 2620 | |
| 2621 | GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2622 | return (GrFence)fence; |
| 2623 | } |
| 2624 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2625 | bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) { |
| 2626 | SkASSERT(VK_NULL_HANDLE != (VkFence)fence); |
| 2627 | |
| 2628 | VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout)); |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2629 | return (VK_SUCCESS == result); |
| 2630 | } |
| 2631 | |
| 2632 | void GrVkGpu::deleteFence(GrFence fence) const { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2633 | VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr)); |
| 2634 | } |
| 2635 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2636 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) { |
| 2637 | return GrVkSemaphore::Make(this, isOwned); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2638 | } |
| 2639 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2640 | sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
| 2641 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 2642 | GrWrapOwnership ownership) { |
| 2643 | return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 2644 | } |
| 2645 | |
Greg Daniel | 858e12c | 2018-12-06 11:11:37 -0500 | [diff] [blame] | 2646 | void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2647 | GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get()); |
| 2648 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2649 | GrVkSemaphore::Resource* resource = vkSem->getResource(); |
| 2650 | if (resource->shouldSignal()) { |
Greg Daniel | 17b7c05 | 2018-01-09 13:55:33 -0500 | [diff] [blame] | 2651 | resource->ref(); |
| 2652 | fSemaphoresToSignal.push_back(resource); |
| 2653 | } |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2654 | } |
| 2655 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2656 | void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 2657 | GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get()); |
| 2658 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 2659 | GrVkSemaphore::Resource* resource = vkSem->getResource(); |
| 2660 | if (resource->shouldWait()) { |
| 2661 | resource->ref(); |
| 2662 | fSemaphoresToWaitOn.push_back(resource); |
| 2663 | } |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 2664 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 2665 | |
| 2666 | sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) { |
| 2667 | SkASSERT(texture); |
| 2668 | GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture); |
| 2669 | vkTexture->setImageLayout(this, |
| 2670 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 2671 | VK_ACCESS_SHADER_READ_BIT, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 2672 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 2673 | false); |
| 2674 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 2675 | |
Greg Daniel | b3f6654 | 2019-05-10 17:11:19 -0400 | [diff] [blame] | 2676 | // The image layout change serves as a barrier, so no semaphore is needed. |
| 2677 | // If we ever decide we need to return a semaphore here, we need to make sure GrVkSemaphore is |
| 2678 | // thread safe so that only the first thread that tries to use the semaphore actually submits |
| 2679 | // it. This additionally would also require thread safety in command buffer submissions to |
| 2680 | // queues in general. |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 2681 | return nullptr; |
| 2682 | } |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 2683 | |
Greg Daniel | 64cc9aa | 2018-10-19 13:54:56 -0400 | [diff] [blame] | 2684 | void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) { |
| 2685 | fDrawables.emplace_back(std::move(drawable)); |
| 2686 | } |
| 2687 | |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 2688 | uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState, |
| 2689 | const GrBackendFormat& format) { |
| 2690 | const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo(); |
| 2691 | SkASSERT(ycbcrInfo); |
| 2692 | if (!ycbcrInfo->isValid()) { |
| 2693 | return 0; |
| 2694 | } |
| 2695 | |
| 2696 | const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler( |
| 2697 | samplerState, *ycbcrInfo); |
| 2698 | |
| 2699 | return sampler->uniqueID(); |
| 2700 | } |
| 2701 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 2702 | void GrVkGpu::storeVkPipelineCacheData() { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 2703 | if (this->getContext()->priv().getPersistentCache()) { |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 2704 | this->resourceProvider().storePipelineCacheData(); |
| 2705 | } |
| 2706 | } |