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