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