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