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