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