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