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