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