Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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/GrVkResourceProvider.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 9 | |
Adlai Holler | 3d0359a | 2020-07-09 15:35:55 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkTaskGroup.h" |
Robert Phillips | 06273bc | 2021-08-11 15:43:50 -0400 | [diff] [blame] | 12 | #include "src/core/SkTraceEvent.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrSamplerState.h" |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrStencilSettings.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 17 | #include "src/gpu/vk/GrVkCommandPool.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/vk/GrVkGpu.h" |
| 19 | #include "src/gpu/vk/GrVkPipeline.h" |
| 20 | #include "src/gpu/vk/GrVkRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 23 | GrVkResourceProvider::GrVkResourceProvider(GrVkGpu* gpu) |
| 24 | : fGpu(gpu) |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 25 | , fPipelineCache(VK_NULL_HANDLE) { |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 26 | fPipelineStateCache = sk_make_sp<PipelineStateCache>(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | GrVkResourceProvider::~GrVkResourceProvider() { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 30 | SkASSERT(0 == fRenderPassArray.count()); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 31 | SkASSERT(0 == fExternalRenderPasses.count()); |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 32 | SkASSERT(0 == fMSAALoadPipelines.count()); |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 33 | SkASSERT(VK_NULL_HANDLE == fPipelineCache); |
| 34 | } |
| 35 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 36 | VkPipelineCache GrVkResourceProvider::pipelineCache() { |
| 37 | if (fPipelineCache == VK_NULL_HANDLE) { |
| 38 | VkPipelineCacheCreateInfo createInfo; |
| 39 | memset(&createInfo, 0, sizeof(VkPipelineCacheCreateInfo)); |
| 40 | createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 41 | createInfo.pNext = nullptr; |
| 42 | createInfo.flags = 0; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 43 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 44 | auto persistentCache = fGpu->getContext()->priv().getPersistentCache(); |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 45 | sk_sp<SkData> cached; |
| 46 | if (persistentCache) { |
| 47 | uint32_t key = GrVkGpu::kPipelineCache_PersistentCacheKeyType; |
| 48 | sk_sp<SkData> keyData = SkData::MakeWithoutCopy(&key, sizeof(uint32_t)); |
| 49 | cached = persistentCache->load(*keyData); |
| 50 | } |
| 51 | bool usedCached = false; |
| 52 | if (cached) { |
| 53 | uint32_t* cacheHeader = (uint32_t*)cached->data(); |
| 54 | if (cacheHeader[1] == VK_PIPELINE_CACHE_HEADER_VERSION_ONE) { |
| 55 | // For version one of the header, the total header size is 16 bytes plus |
| 56 | // VK_UUID_SIZE bytes. See Section 9.6 (Pipeline Cache) in the vulkan spec to see |
| 57 | // the breakdown of these bytes. |
| 58 | SkASSERT(cacheHeader[0] == 16 + VK_UUID_SIZE); |
| 59 | const VkPhysicalDeviceProperties& devProps = fGpu->physicalDeviceProperties(); |
| 60 | const uint8_t* supportedPipelineCacheUUID = devProps.pipelineCacheUUID; |
| 61 | if (cacheHeader[2] == devProps.vendorID && cacheHeader[3] == devProps.deviceID && |
| 62 | !memcmp(&cacheHeader[4], supportedPipelineCacheUUID, VK_UUID_SIZE)) { |
| 63 | createInfo.initialDataSize = cached->size(); |
| 64 | createInfo.pInitialData = cached->data(); |
| 65 | usedCached = true; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | if (!usedCached) { |
| 70 | createInfo.initialDataSize = 0; |
| 71 | createInfo.pInitialData = nullptr; |
| 72 | } |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 73 | |
| 74 | VkResult result; |
| 75 | GR_VK_CALL_RESULT(fGpu, result, CreatePipelineCache(fGpu->device(), &createInfo, nullptr, |
| 76 | &fPipelineCache)); |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 77 | if (VK_SUCCESS != result) { |
| 78 | fPipelineCache = VK_NULL_HANDLE; |
| 79 | } |
| 80 | } |
| 81 | return fPipelineCache; |
| 82 | } |
| 83 | |
| 84 | void GrVkResourceProvider::init() { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 85 | // Init uniform descriptor objects |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 86 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateUniformManager(fGpu); |
| 87 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 88 | SkASSERT(1 == fDescriptorSetManagers.count()); |
| 89 | fUniformDSHandle = GrVkDescriptorSetManager::Handle(0); |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 90 | dsm = GrVkDescriptorSetManager::CreateInputManager(fGpu); |
| 91 | fDescriptorSetManagers.emplace_back(dsm); |
| 92 | SkASSERT(2 == fDescriptorSetManagers.count()); |
| 93 | fInputDSHandle = GrVkDescriptorSetManager::Handle(1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 96 | sk_sp<const GrVkPipeline> GrVkResourceProvider::makePipeline( |
| 97 | const GrProgramInfo& programInfo, |
| 98 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
| 99 | int shaderStageCount, |
| 100 | VkRenderPass compatibleRenderPass, |
Greg Daniel | 91b37b1 | 2021-01-05 15:40:54 -0500 | [diff] [blame] | 101 | VkPipelineLayout layout, |
| 102 | uint32_t subpass) { |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 103 | return GrVkPipeline::Make(fGpu, programInfo, shaderStageInfo, shaderStageCount, |
Greg Daniel | 91b37b1 | 2021-01-05 15:40:54 -0500 | [diff] [blame] | 104 | compatibleRenderPass, layout, this->pipelineCache(), subpass); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 107 | // To create framebuffers, we first need to create a simple RenderPass that is |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 108 | // only used for framebuffer creation. When we actually render we will create |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 109 | // RenderPasses as needed that are compatible with the framebuffer. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 110 | const GrVkRenderPass* |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 111 | GrVkResourceProvider::findCompatibleRenderPass(GrVkRenderTarget* target, |
Robert Phillips | 96f2237 | 2020-05-20 12:31:18 -0400 | [diff] [blame] | 112 | CompatibleRPHandle* compatibleHandle, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 113 | bool withResolve, |
Greg Daniel | ed629c1 | 2020-08-14 13:11:18 -0400 | [diff] [blame] | 114 | bool withStencil, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 115 | SelfDependencyFlags selfDepFlags, |
| 116 | LoadFromResolve loadFromResolve) { |
Robert Phillips | 8bd0719 | 2020-05-12 13:26:56 -0400 | [diff] [blame] | 117 | // Get attachment information from render target. This includes which attachments the render |
| 118 | // target has (color, stencil) and the attachments format and sample count. |
| 119 | GrVkRenderPass::AttachmentFlags attachmentFlags; |
| 120 | GrVkRenderPass::AttachmentsDescriptor attachmentsDesc; |
Greg Daniel | 60ec617 | 2021-04-16 11:31:58 -0400 | [diff] [blame] | 121 | target->getAttachmentsDescriptor(&attachmentsDesc, &attachmentFlags, withResolve, withStencil); |
Robert Phillips | 8bd0719 | 2020-05-12 13:26:56 -0400 | [diff] [blame] | 122 | |
Greg Daniel | 2177436 | 2020-09-14 10:36:43 -0400 | [diff] [blame] | 123 | return this->findCompatibleRenderPass(&attachmentsDesc, attachmentFlags, selfDepFlags, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 124 | loadFromResolve, compatibleHandle); |
Robert Phillips | 8bd0719 | 2020-05-12 13:26:56 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | const GrVkRenderPass* |
| 128 | GrVkResourceProvider::findCompatibleRenderPass(GrVkRenderPass::AttachmentsDescriptor* desc, |
| 129 | GrVkRenderPass::AttachmentFlags attachmentFlags, |
Greg Daniel | 2177436 | 2020-09-14 10:36:43 -0400 | [diff] [blame] | 130 | SelfDependencyFlags selfDepFlags, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 131 | LoadFromResolve loadFromResolve, |
Robert Phillips | 8bd0719 | 2020-05-12 13:26:56 -0400 | [diff] [blame] | 132 | CompatibleRPHandle* compatibleHandle) { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 133 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 134 | if (fRenderPassArray[i].isCompatible(*desc, attachmentFlags, selfDepFlags, |
| 135 | loadFromResolve)) { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 136 | const GrVkRenderPass* renderPass = fRenderPassArray[i].getCompatibleRenderPass(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 137 | renderPass->ref(); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 138 | if (compatibleHandle) { |
| 139 | *compatibleHandle = CompatibleRPHandle(i); |
| 140 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 141 | return renderPass; |
| 142 | } |
| 143 | } |
| 144 | |
Greg Daniel | b418921 | 2020-08-10 11:46:30 -0400 | [diff] [blame] | 145 | GrVkRenderPass* renderPass = GrVkRenderPass::CreateSimple(fGpu, desc, attachmentFlags, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 146 | selfDepFlags, loadFromResolve); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 147 | if (!renderPass) { |
| 148 | return nullptr; |
| 149 | } |
| 150 | fRenderPassArray.emplace_back(renderPass); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 151 | |
| 152 | if (compatibleHandle) { |
| 153 | *compatibleHandle = CompatibleRPHandle(fRenderPassArray.count() - 1); |
| 154 | } |
| 155 | return renderPass; |
| 156 | } |
| 157 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 158 | const GrVkRenderPass* GrVkResourceProvider::findCompatibleExternalRenderPass( |
| 159 | VkRenderPass renderPass, uint32_t colorAttachmentIndex) { |
| 160 | for (int i = 0; i < fExternalRenderPasses.count(); ++i) { |
| 161 | if (fExternalRenderPasses[i]->isCompatibleExternalRP(renderPass)) { |
| 162 | fExternalRenderPasses[i]->ref(); |
| 163 | #ifdef SK_DEBUG |
| 164 | uint32_t cachedColorIndex; |
| 165 | SkASSERT(fExternalRenderPasses[i]->colorAttachmentIndex(&cachedColorIndex)); |
| 166 | SkASSERT(cachedColorIndex == colorAttachmentIndex); |
| 167 | #endif |
| 168 | return fExternalRenderPasses[i]; |
| 169 | } |
| 170 | } |
| 171 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 172 | const GrVkRenderPass* newRenderPass = new GrVkRenderPass(fGpu, renderPass, |
| 173 | colorAttachmentIndex); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 174 | fExternalRenderPasses.push_back(newRenderPass); |
| 175 | newRenderPass->ref(); |
| 176 | return newRenderPass; |
| 177 | } |
| 178 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 179 | const GrVkRenderPass* GrVkResourceProvider::findRenderPass( |
Greg Daniel | ed629c1 | 2020-08-14 13:11:18 -0400 | [diff] [blame] | 180 | GrVkRenderTarget* target, |
| 181 | const GrVkRenderPass::LoadStoreOps& colorOps, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 182 | const GrVkRenderPass::LoadStoreOps& resolveOps, |
Greg Daniel | ed629c1 | 2020-08-14 13:11:18 -0400 | [diff] [blame] | 183 | const GrVkRenderPass::LoadStoreOps& stencilOps, |
| 184 | CompatibleRPHandle* compatibleHandle, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 185 | bool withResolve, |
Greg Daniel | ed629c1 | 2020-08-14 13:11:18 -0400 | [diff] [blame] | 186 | bool withStencil, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 187 | SelfDependencyFlags selfDepFlags, |
| 188 | LoadFromResolve loadFromResolve) { |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 189 | GrVkResourceProvider::CompatibleRPHandle tempRPHandle; |
| 190 | GrVkResourceProvider::CompatibleRPHandle* pRPHandle = compatibleHandle ? compatibleHandle |
| 191 | : &tempRPHandle; |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 192 | *pRPHandle = target->compatibleRenderPassHandle(withResolve, withStencil, selfDepFlags, |
| 193 | loadFromResolve); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 194 | if (!pRPHandle->isValid()) { |
| 195 | return nullptr; |
| 196 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 197 | |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 198 | return this->findRenderPass(*pRPHandle, colorOps, resolveOps, stencilOps); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | const GrVkRenderPass* |
| 202 | GrVkResourceProvider::findRenderPass(const CompatibleRPHandle& compatibleHandle, |
| 203 | const GrVkRenderPass::LoadStoreOps& colorOps, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 204 | const GrVkRenderPass::LoadStoreOps& resolveOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 205 | const GrVkRenderPass::LoadStoreOps& stencilOps) { |
| 206 | SkASSERT(compatibleHandle.isValid() && compatibleHandle.toIndex() < fRenderPassArray.count()); |
| 207 | CompatibleRenderPassSet& compatibleSet = fRenderPassArray[compatibleHandle.toIndex()]; |
| 208 | const GrVkRenderPass* renderPass = compatibleSet.getRenderPass(fGpu, |
| 209 | colorOps, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 210 | resolveOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 211 | stencilOps); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 212 | if (!renderPass) { |
| 213 | return nullptr; |
| 214 | } |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 215 | renderPass->ref(); |
| 216 | return renderPass; |
| 217 | } |
| 218 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 219 | GrVkDescriptorPool* GrVkResourceProvider::findOrCreateCompatibleDescriptorPool( |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 220 | VkDescriptorType type, uint32_t count) { |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 221 | return GrVkDescriptorPool::Create(fGpu, type, count); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 224 | GrVkSampler* GrVkResourceProvider::findOrCreateCompatibleSampler( |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 225 | GrSamplerState params, const GrVkYcbcrConversionInfo& ycbcrInfo) { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 226 | GrVkSampler* sampler = fSamplers.find(GrVkSampler::GenerateKey(params, ycbcrInfo)); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 227 | if (!sampler) { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 228 | sampler = GrVkSampler::Create(fGpu, params, ycbcrInfo); |
| 229 | if (!sampler) { |
| 230 | return nullptr; |
| 231 | } |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 232 | fSamplers.add(sampler); |
| 233 | } |
| 234 | SkASSERT(sampler); |
| 235 | sampler->ref(); |
| 236 | return sampler; |
| 237 | } |
| 238 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 239 | GrVkSamplerYcbcrConversion* GrVkResourceProvider::findOrCreateCompatibleSamplerYcbcrConversion( |
| 240 | const GrVkYcbcrConversionInfo& ycbcrInfo) { |
| 241 | GrVkSamplerYcbcrConversion* ycbcrConversion = |
| 242 | fYcbcrConversions.find(GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)); |
| 243 | if (!ycbcrConversion) { |
| 244 | ycbcrConversion = GrVkSamplerYcbcrConversion::Create(fGpu, ycbcrInfo); |
| 245 | if (!ycbcrConversion) { |
| 246 | return nullptr; |
| 247 | } |
| 248 | fYcbcrConversions.add(ycbcrConversion); |
| 249 | } |
| 250 | SkASSERT(ycbcrConversion); |
| 251 | ycbcrConversion->ref(); |
| 252 | return ycbcrConversion; |
| 253 | } |
| 254 | |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 255 | GrVkPipelineState* GrVkResourceProvider::findOrCreateCompatiblePipelineState( |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 256 | GrRenderTarget* renderTarget, |
| 257 | const GrProgramInfo& programInfo, |
Greg Daniel | 91b37b1 | 2021-01-05 15:40:54 -0500 | [diff] [blame] | 258 | VkRenderPass compatibleRenderPass, |
| 259 | bool overrideSubpassForResolveLoad) { |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame] | 260 | return fPipelineStateCache->findOrCreatePipelineState(renderTarget, programInfo, |
Greg Daniel | 91b37b1 | 2021-01-05 15:40:54 -0500 | [diff] [blame] | 261 | compatibleRenderPass, |
| 262 | overrideSubpassForResolveLoad); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame] | 265 | GrVkPipelineState* GrVkResourceProvider::findOrCreateCompatiblePipelineState( |
| 266 | const GrProgramDesc& desc, |
| 267 | const GrProgramInfo& programInfo, |
| 268 | VkRenderPass compatibleRenderPass, |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 269 | GrThreadSafePipelineBuilder::Stats::ProgramCacheResult* stat) { |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame] | 270 | |
| 271 | auto tmp = fPipelineStateCache->findOrCreatePipelineState(desc, programInfo, |
| 272 | compatibleRenderPass, stat); |
| 273 | if (!tmp) { |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 274 | fPipelineStateCache->stats()->incNumPreCompilationFailures(); |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame] | 275 | } else { |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame] | 276 | fPipelineStateCache->stats()->incNumPreProgramCacheResult(*stat); |
Robert Phillips | f497c36 | 2020-05-12 10:35:18 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return tmp; |
| 280 | } |
| 281 | |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 282 | sk_sp<const GrVkPipeline> GrVkResourceProvider::findOrCreateMSAALoadPipeline( |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 283 | const GrVkRenderPass& renderPass, |
Greg Daniel | 1034425 | 2021-04-22 09:52:25 -0400 | [diff] [blame] | 284 | int numSamples, |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 285 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
| 286 | VkPipelineLayout pipelineLayout) { |
| 287 | // Find or Create a compatible pipeline |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 288 | sk_sp<const GrVkPipeline> pipeline; |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 289 | for (int i = 0; i < fMSAALoadPipelines.count() && !pipeline; ++i) { |
| 290 | if (fMSAALoadPipelines[i].fRenderPass->isCompatible(renderPass)) { |
| 291 | pipeline = fMSAALoadPipelines[i].fPipeline; |
| 292 | } |
| 293 | } |
| 294 | if (!pipeline) { |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 295 | pipeline = GrVkPipeline::Make( |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 296 | fGpu, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 297 | /*vertexAttribs=*/GrGeometryProcessor::AttributeSet(), |
| 298 | /*instanceAttribs=*/GrGeometryProcessor::AttributeSet(), |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 299 | GrPrimitiveType::kTriangleStrip, |
| 300 | kTopLeft_GrSurfaceOrigin, |
| 301 | GrStencilSettings(), |
Greg Daniel | 1034425 | 2021-04-22 09:52:25 -0400 | [diff] [blame] | 302 | numSamples, |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 303 | /*isHWantialiasState=*/false, |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 304 | GrXferProcessor::BlendInfo(), |
| 305 | /*isWireframe=*/false, |
| 306 | /*useConservativeRaster=*/false, |
| 307 | /*subpass=*/0, |
| 308 | shaderStageInfo, |
| 309 | /*shaderStageCount=*/2, |
| 310 | renderPass.vkRenderPass(), |
| 311 | pipelineLayout, |
| 312 | /*ownsLayout=*/false, |
| 313 | this->pipelineCache()); |
| 314 | if (!pipeline) { |
| 315 | return nullptr; |
| 316 | } |
| 317 | fMSAALoadPipelines.push_back({pipeline, &renderPass}); |
| 318 | } |
| 319 | SkASSERT(pipeline); |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 320 | return pipeline; |
| 321 | } |
| 322 | |
| 323 | void GrVkResourceProvider::getZeroSamplerDescriptorSetHandle( |
| 324 | GrVkDescriptorSetManager::Handle* handle) { |
| 325 | SkASSERT(handle); |
| 326 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
| 327 | if (fDescriptorSetManagers[i]->isZeroSampler()) { |
| 328 | *handle = GrVkDescriptorSetManager::Handle(i); |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | GrVkDescriptorSetManager* dsm = |
| 334 | GrVkDescriptorSetManager::CreateZeroSamplerManager(fGpu); |
| 335 | fDescriptorSetManagers.emplace_back(dsm); |
| 336 | *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() - 1); |
| 337 | } |
| 338 | |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 339 | void GrVkResourceProvider::getSamplerDescriptorSetHandle(VkDescriptorType type, |
| 340 | const GrVkUniformHandler& uniformHandler, |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 341 | GrVkDescriptorSetManager::Handle* handle) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 342 | SkASSERT(handle); |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 343 | SkASSERT(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type || |
| 344 | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER == type); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 345 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 346 | if (fDescriptorSetManagers[i]->isCompatible(type, &uniformHandler)) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 347 | *handle = GrVkDescriptorSetManager::Handle(i); |
| 348 | return; |
| 349 | } |
| 350 | } |
| 351 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 352 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateSamplerManager(fGpu, type, |
| 353 | uniformHandler); |
| 354 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 355 | *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() - 1); |
| 356 | } |
| 357 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 358 | VkDescriptorSetLayout GrVkResourceProvider::getUniformDSLayout() const { |
| 359 | SkASSERT(fUniformDSHandle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 360 | return fDescriptorSetManagers[fUniformDSHandle.toIndex()]->layout(); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 363 | VkDescriptorSetLayout GrVkResourceProvider::getInputDSLayout() const { |
| 364 | SkASSERT(fInputDSHandle.isValid()); |
| 365 | return fDescriptorSetManagers[fInputDSHandle.toIndex()]->layout(); |
| 366 | } |
| 367 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 368 | VkDescriptorSetLayout GrVkResourceProvider::getSamplerDSLayout( |
| 369 | const GrVkDescriptorSetManager::Handle& handle) const { |
| 370 | SkASSERT(handle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 371 | return fDescriptorSetManagers[handle.toIndex()]->layout(); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 372 | } |
| 373 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 374 | const GrVkDescriptorSet* GrVkResourceProvider::getUniformDescriptorSet() { |
| 375 | SkASSERT(fUniformDSHandle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 376 | return fDescriptorSetManagers[fUniformDSHandle.toIndex()]->getDescriptorSet(fGpu, |
| 377 | fUniformDSHandle); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 380 | const GrVkDescriptorSet* GrVkResourceProvider::getInputDescriptorSet() { |
| 381 | SkASSERT(fInputDSHandle.isValid()); |
| 382 | return fDescriptorSetManagers[fInputDSHandle.toIndex()]->getDescriptorSet(fGpu, fInputDSHandle); |
| 383 | } |
| 384 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 385 | const GrVkDescriptorSet* GrVkResourceProvider::getSamplerDescriptorSet( |
| 386 | const GrVkDescriptorSetManager::Handle& handle) { |
| 387 | SkASSERT(handle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 388 | return fDescriptorSetManagers[handle.toIndex()]->getDescriptorSet(fGpu, handle); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 389 | } |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 390 | |
| 391 | void GrVkResourceProvider::recycleDescriptorSet(const GrVkDescriptorSet* descSet, |
| 392 | const GrVkDescriptorSetManager::Handle& handle) { |
| 393 | SkASSERT(descSet); |
| 394 | SkASSERT(handle.isValid()); |
| 395 | int managerIdx = handle.toIndex(); |
| 396 | SkASSERT(managerIdx < fDescriptorSetManagers.count()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 397 | fDescriptorSetManagers[managerIdx]->recycleDescriptorSet(descSet); |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 400 | GrVkCommandPool* GrVkResourceProvider::findOrCreateCommandPool() { |
Greg Daniel | 0a6cd5a | 2021-03-31 13:04:47 -0400 | [diff] [blame] | 401 | SkAutoMutexExclusive lock(fBackgroundMutex); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 402 | GrVkCommandPool* result; |
| 403 | if (fAvailableCommandPools.count()) { |
| 404 | result = fAvailableCommandPools.back(); |
| 405 | fAvailableCommandPools.pop_back(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 406 | } else { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 407 | result = GrVkCommandPool::Create(fGpu); |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 408 | if (!result) { |
| 409 | return nullptr; |
| 410 | } |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 411 | } |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 412 | SkASSERT(result->unique()); |
| 413 | SkDEBUGCODE( |
| 414 | for (const GrVkCommandPool* pool : fActiveCommandPools) { |
| 415 | SkASSERT(pool != result); |
| 416 | } |
| 417 | for (const GrVkCommandPool* pool : fAvailableCommandPools) { |
| 418 | SkASSERT(pool != result); |
| 419 | } |
Ben Wagner | 1c0cacf | 2019-01-14 12:57:36 -0500 | [diff] [blame] | 420 | ) |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 421 | fActiveCommandPools.push_back(result); |
| 422 | result->ref(); |
| 423 | return result; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void GrVkResourceProvider::checkCommandBuffers() { |
Greg Daniel | ee792d6 | 2020-11-24 14:02:04 -0500 | [diff] [blame] | 427 | // When resetting a command buffer it can trigger client provided procs (e.g. release or |
| 428 | // finished) to be called. During these calls the client could trigger us to abandon the vk |
| 429 | // context, e.g. if we are in a DEVICE_LOST state. When we abandon the vk context we will |
| 430 | // unref all the fActiveCommandPools and reset the array. Since this can happen in the middle |
| 431 | // of the loop here, we need to additionally check that fActiveCommandPools still has pools on |
| 432 | // each iteration. |
| 433 | // |
| 434 | // TODO: We really need to have a more robust way to protect us from client proc calls that |
| 435 | // happen in the middle of us doing work. This may be just one of many potential pitfalls that |
| 436 | // could happen from the client triggering GrDirectContext changes during a proc call. |
| 437 | for (int i = fActiveCommandPools.count() - 1; fActiveCommandPools.count() && i >= 0; --i) { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 438 | GrVkCommandPool* pool = fActiveCommandPools[i]; |
| 439 | if (!pool->isOpen()) { |
| 440 | GrVkPrimaryCommandBuffer* buffer = pool->getPrimaryCommandBuffer(); |
| 441 | if (buffer->finished(fGpu)) { |
| 442 | fActiveCommandPools.removeShuffle(i); |
Greg Daniel | ee792d6 | 2020-11-24 14:02:04 -0500 | [diff] [blame] | 443 | // This passes ownership of the pool to the backgroundReset call. The pool should |
| 444 | // not be used again from this function. |
| 445 | // TODO: We should see if we can use sk_sps here to make this more explicit. |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 446 | this->backgroundReset(pool); |
| 447 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
Greg Daniel | fda4586 | 2021-02-09 17:55:07 -0500 | [diff] [blame] | 452 | void GrVkResourceProvider::forceSyncAllCommandBuffers() { |
| 453 | for (int i = fActiveCommandPools.count() - 1; fActiveCommandPools.count() && i >= 0; --i) { |
| 454 | GrVkCommandPool* pool = fActiveCommandPools[i]; |
| 455 | if (!pool->isOpen()) { |
| 456 | GrVkPrimaryCommandBuffer* buffer = pool->getPrimaryCommandBuffer(); |
| 457 | buffer->forceSync(fGpu); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 462 | void GrVkResourceProvider::addFinishedProcToActiveCommandBuffers( |
Greg Daniel | 288ecf6 | 2020-06-05 10:22:26 -0400 | [diff] [blame] | 463 | sk_sp<GrRefCntedCallback> finishedCallback) { |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 464 | for (int i = 0; i < fActiveCommandPools.count(); ++i) { |
| 465 | GrVkCommandPool* pool = fActiveCommandPools[i]; |
Greg Daniel | fe15962 | 2020-04-10 17:43:51 +0000 | [diff] [blame] | 466 | GrVkPrimaryCommandBuffer* buffer = pool->getPrimaryCommandBuffer(); |
Greg Daniel | 288ecf6 | 2020-06-05 10:22:26 -0400 | [diff] [blame] | 467 | buffer->addFinishedProc(finishedCallback); |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Greg Daniel | a89b430 | 2021-01-29 10:48:40 -0500 | [diff] [blame] | 471 | void GrVkResourceProvider::destroyResources() { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 472 | SkTaskGroup* taskGroup = fGpu->getContext()->priv().getTaskGroup(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 473 | if (taskGroup) { |
| 474 | taskGroup->wait(); |
Ethan Nicholas | bff4e07 | 2018-12-12 18:17:24 +0000 | [diff] [blame] | 475 | } |
Ethan Nicholas | bff4e07 | 2018-12-12 18:17:24 +0000 | [diff] [blame] | 476 | |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 477 | // Release all msaa load pipelines |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 478 | fMSAALoadPipelines.reset(); |
| 479 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 480 | // loop over all render pass sets to make sure we destroy all the internal VkRenderPasses |
| 481 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 482 | fRenderPassArray[i].releaseResources(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 483 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 484 | fRenderPassArray.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 485 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 486 | for (int i = 0; i < fExternalRenderPasses.count(); ++i) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 487 | fExternalRenderPasses[i]->unref(); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 488 | } |
| 489 | fExternalRenderPasses.reset(); |
| 490 | |
Greg Daniel | 428523f | 2021-03-30 14:22:54 -0400 | [diff] [blame] | 491 | // Iterate through all store GrVkSamplers and unref them before resetting the hash table. |
Mike Klein | cff6396 | 2020-03-14 16:22:45 -0500 | [diff] [blame] | 492 | fSamplers.foreach([&](auto* elt) { elt->unref(); }); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 493 | fSamplers.reset(); |
| 494 | |
Mike Klein | cff6396 | 2020-03-14 16:22:45 -0500 | [diff] [blame] | 495 | fYcbcrConversions.foreach([&](auto* elt) { elt->unref(); }); |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 496 | fYcbcrConversions.reset(); |
| 497 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 498 | fPipelineStateCache->release(); |
| 499 | |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 500 | GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineCache(fGpu->device(), fPipelineCache, nullptr)); |
| 501 | fPipelineCache = VK_NULL_HANDLE; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 502 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 503 | for (GrVkCommandPool* pool : fActiveCommandPools) { |
| 504 | SkASSERT(pool->unique()); |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 505 | pool->unref(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 506 | } |
| 507 | fActiveCommandPools.reset(); |
| 508 | |
Greg Daniel | 0a6cd5a | 2021-03-31 13:04:47 -0400 | [diff] [blame] | 509 | { |
| 510 | SkAutoMutexExclusive lock(fBackgroundMutex); |
| 511 | for (GrVkCommandPool* pool : fAvailableCommandPools) { |
| 512 | SkASSERT(pool->unique()); |
| 513 | pool->unref(); |
| 514 | } |
| 515 | fAvailableCommandPools.reset(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 516 | } |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 517 | |
Greg Daniel | a31ab4b | 2020-03-11 13:59:38 -0400 | [diff] [blame] | 518 | // We must release/destroy all command buffers and pipeline states before releasing the |
| 519 | // GrVkDescriptorSetManagers. Additionally, we must release all uniform buffers since they hold |
| 520 | // refs to GrVkDescriptorSets. |
| 521 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
| 522 | fDescriptorSetManagers[i]->release(fGpu); |
| 523 | } |
| 524 | fDescriptorSetManagers.reset(); |
| 525 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Greg Daniel | 428523f | 2021-03-30 14:22:54 -0400 | [diff] [blame] | 528 | void GrVkResourceProvider::releaseUnlockedBackendObjects() { |
Greg Daniel | 0a6cd5a | 2021-03-31 13:04:47 -0400 | [diff] [blame] | 529 | SkAutoMutexExclusive lock(fBackgroundMutex); |
Greg Daniel | 428523f | 2021-03-30 14:22:54 -0400 | [diff] [blame] | 530 | for (GrVkCommandPool* pool : fAvailableCommandPools) { |
| 531 | SkASSERT(pool->unique()); |
| 532 | pool->unref(); |
| 533 | } |
| 534 | fAvailableCommandPools.reset(); |
| 535 | } |
| 536 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 537 | void GrVkResourceProvider::backgroundReset(GrVkCommandPool* pool) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 538 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 539 | SkASSERT(pool->unique()); |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 540 | pool->releaseResources(); |
Greg Daniel | ee792d6 | 2020-11-24 14:02:04 -0500 | [diff] [blame] | 541 | // After releasing resources we may have called a client callback proc which may have |
| 542 | // disconnected the GrVkGpu. In that case we do not want to push the pool back onto the cache, |
| 543 | // but instead just drop the pool. |
| 544 | if (fGpu->disconnected()) { |
| 545 | pool->unref(); |
| 546 | return; |
| 547 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 548 | SkTaskGroup* taskGroup = fGpu->getContext()->priv().getTaskGroup(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 549 | if (taskGroup) { |
| 550 | taskGroup->add([this, pool]() { |
| 551 | this->reset(pool); |
| 552 | }); |
| 553 | } else { |
| 554 | this->reset(pool); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | void GrVkResourceProvider::reset(GrVkCommandPool* pool) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 559 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 560 | SkASSERT(pool->unique()); |
| 561 | pool->reset(fGpu); |
Greg Daniel | 0a6cd5a | 2021-03-31 13:04:47 -0400 | [diff] [blame] | 562 | SkAutoMutexExclusive lock(fBackgroundMutex); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 563 | fAvailableCommandPools.push_back(pool); |
| 564 | } |
| 565 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 566 | void GrVkResourceProvider::storePipelineCacheData() { |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 567 | if (this->pipelineCache() == VK_NULL_HANDLE) { |
| 568 | return; |
| 569 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 570 | size_t dataSize = 0; |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 571 | VkResult result; |
| 572 | GR_VK_CALL_RESULT(fGpu, result, GetPipelineCacheData(fGpu->device(), this->pipelineCache(), |
| 573 | &dataSize, nullptr)); |
| 574 | if (result != VK_SUCCESS) { |
| 575 | return; |
| 576 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 577 | |
| 578 | std::unique_ptr<uint8_t[]> data(new uint8_t[dataSize]); |
| 579 | |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 580 | GR_VK_CALL_RESULT(fGpu, result, GetPipelineCacheData(fGpu->device(), this->pipelineCache(), |
| 581 | &dataSize, (void*)data.get())); |
| 582 | if (result != VK_SUCCESS) { |
| 583 | return; |
| 584 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 585 | |
| 586 | uint32_t key = GrVkGpu::kPipelineCache_PersistentCacheKeyType; |
| 587 | sk_sp<SkData> keyData = SkData::MakeWithoutCopy(&key, sizeof(uint32_t)); |
| 588 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 589 | fGpu->getContext()->priv().getPersistentCache()->store( |
Brian Osman | f0de96f | 2021-02-26 13:54:11 -0500 | [diff] [blame] | 590 | *keyData, *SkData::MakeWithoutCopy(data.get(), dataSize), SkString("VkPipelineCache")); |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 591 | } |
| 592 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 593 | //////////////////////////////////////////////////////////////////////////////// |
| 594 | |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 595 | GrVkResourceProvider::CompatibleRenderPassSet::CompatibleRenderPassSet(GrVkRenderPass* renderPass) |
| 596 | : fLastReturnedIndex(0) { |
| 597 | renderPass->ref(); |
| 598 | fRenderPasses.push_back(renderPass); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | bool GrVkResourceProvider::CompatibleRenderPassSet::isCompatible( |
Robert Phillips | 8bd0719 | 2020-05-12 13:26:56 -0400 | [diff] [blame] | 602 | const GrVkRenderPass::AttachmentsDescriptor& attachmentsDescriptor, |
Greg Daniel | b418921 | 2020-08-10 11:46:30 -0400 | [diff] [blame] | 603 | GrVkRenderPass::AttachmentFlags attachmentFlags, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 604 | SelfDependencyFlags selfDepFlags, |
| 605 | LoadFromResolve loadFromResolve) const { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 606 | // The first GrVkRenderpass should always exists since we create the basic load store |
| 607 | // render pass on create |
| 608 | SkASSERT(fRenderPasses[0]); |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 609 | return fRenderPasses[0]->isCompatible(attachmentsDescriptor, attachmentFlags, selfDepFlags, |
| 610 | loadFromResolve); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 611 | } |
| 612 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 613 | GrVkRenderPass* GrVkResourceProvider::CompatibleRenderPassSet::getRenderPass( |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 614 | GrVkGpu* gpu, |
| 615 | const GrVkRenderPass::LoadStoreOps& colorOps, |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 616 | const GrVkRenderPass::LoadStoreOps& resolveOps, |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 617 | const GrVkRenderPass::LoadStoreOps& stencilOps) { |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 618 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 619 | int idx = (i + fLastReturnedIndex) % fRenderPasses.count(); |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 620 | if (fRenderPasses[idx]->equalLoadStoreOps(colorOps, resolveOps, stencilOps)) { |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 621 | fLastReturnedIndex = idx; |
| 622 | return fRenderPasses[idx]; |
| 623 | } |
| 624 | } |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 625 | GrVkRenderPass* renderPass = GrVkRenderPass::Create(gpu, *this->getCompatibleRenderPass(), |
Greg Daniel | 7acddf5 | 2020-12-16 15:15:51 -0500 | [diff] [blame] | 626 | colorOps, resolveOps, stencilOps); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 627 | if (!renderPass) { |
| 628 | return nullptr; |
| 629 | } |
| 630 | fRenderPasses.push_back(renderPass); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 631 | fLastReturnedIndex = fRenderPasses.count() - 1; |
| 632 | return renderPass; |
| 633 | } |
| 634 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 635 | void GrVkResourceProvider::CompatibleRenderPassSet::releaseResources() { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 636 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 637 | if (fRenderPasses[i]) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 638 | fRenderPasses[i]->unref(); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 639 | fRenderPasses[i] = nullptr; |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |