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