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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkTaskGroup.h" |
| 11 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrSamplerState.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 14 | #include "src/gpu/vk/GrVkCommandPool.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/vk/GrVkGpu.h" |
| 16 | #include "src/gpu/vk/GrVkPipeline.h" |
| 17 | #include "src/gpu/vk/GrVkRenderTarget.h" |
| 18 | #include "src/gpu/vk/GrVkUniformBuffer.h" |
| 19 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 20 | |
| 21 | #ifdef SK_TRACE_VK_RESOURCES |
Mike Klein | 820e79b | 2018-12-04 09:31:31 -0500 | [diff] [blame] | 22 | std::atomic<uint32_t> GrVkResource::fKeyCounter{0}; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 23 | #endif |
| 24 | |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 25 | GrVkResourceProvider::GrVkResourceProvider(GrVkGpu* gpu) |
| 26 | : fGpu(gpu) |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 27 | , fPipelineCache(VK_NULL_HANDLE) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 28 | fPipelineStateCache = new PipelineStateCache(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | GrVkResourceProvider::~GrVkResourceProvider() { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 32 | SkASSERT(0 == fRenderPassArray.count()); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 33 | SkASSERT(0 == fExternalRenderPasses.count()); |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 34 | SkASSERT(VK_NULL_HANDLE == fPipelineCache); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 35 | delete fPipelineStateCache; |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 38 | VkPipelineCache GrVkResourceProvider::pipelineCache() { |
| 39 | if (fPipelineCache == VK_NULL_HANDLE) { |
| 40 | VkPipelineCacheCreateInfo createInfo; |
| 41 | memset(&createInfo, 0, sizeof(VkPipelineCacheCreateInfo)); |
| 42 | createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 43 | createInfo.pNext = nullptr; |
| 44 | createInfo.flags = 0; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 45 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 46 | auto persistentCache = fGpu->getContext()->priv().getPersistentCache(); |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 47 | sk_sp<SkData> cached; |
| 48 | if (persistentCache) { |
| 49 | uint32_t key = GrVkGpu::kPipelineCache_PersistentCacheKeyType; |
| 50 | sk_sp<SkData> keyData = SkData::MakeWithoutCopy(&key, sizeof(uint32_t)); |
| 51 | cached = persistentCache->load(*keyData); |
| 52 | } |
| 53 | bool usedCached = false; |
| 54 | if (cached) { |
| 55 | uint32_t* cacheHeader = (uint32_t*)cached->data(); |
| 56 | if (cacheHeader[1] == VK_PIPELINE_CACHE_HEADER_VERSION_ONE) { |
| 57 | // For version one of the header, the total header size is 16 bytes plus |
| 58 | // VK_UUID_SIZE bytes. See Section 9.6 (Pipeline Cache) in the vulkan spec to see |
| 59 | // the breakdown of these bytes. |
| 60 | SkASSERT(cacheHeader[0] == 16 + VK_UUID_SIZE); |
| 61 | const VkPhysicalDeviceProperties& devProps = fGpu->physicalDeviceProperties(); |
| 62 | const uint8_t* supportedPipelineCacheUUID = devProps.pipelineCacheUUID; |
| 63 | if (cacheHeader[2] == devProps.vendorID && cacheHeader[3] == devProps.deviceID && |
| 64 | !memcmp(&cacheHeader[4], supportedPipelineCacheUUID, VK_UUID_SIZE)) { |
| 65 | createInfo.initialDataSize = cached->size(); |
| 66 | createInfo.pInitialData = cached->data(); |
| 67 | usedCached = true; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | if (!usedCached) { |
| 72 | createInfo.initialDataSize = 0; |
| 73 | createInfo.pInitialData = nullptr; |
| 74 | } |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 75 | |
| 76 | VkResult result; |
| 77 | GR_VK_CALL_RESULT(fGpu, result, CreatePipelineCache(fGpu->device(), &createInfo, nullptr, |
| 78 | &fPipelineCache)); |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 79 | if (VK_SUCCESS != result) { |
| 80 | fPipelineCache = VK_NULL_HANDLE; |
| 81 | } |
| 82 | } |
| 83 | return fPipelineCache; |
| 84 | } |
| 85 | |
| 86 | void GrVkResourceProvider::init() { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 87 | // Init uniform descriptor objects |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 88 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateUniformManager(fGpu); |
| 89 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 90 | SkASSERT(1 == fDescriptorSetManagers.count()); |
| 91 | fUniformDSHandle = GrVkDescriptorSetManager::Handle(0); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 94 | GrVkPipeline* GrVkResourceProvider::createPipeline(const GrProgramInfo& programInfo, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 95 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
| 96 | int shaderStageCount, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 97 | VkRenderPass compatibleRenderPass, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | VkPipelineLayout layout) { |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 99 | return GrVkPipeline::Create(fGpu, programInfo, shaderStageInfo, |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 100 | shaderStageCount, compatibleRenderPass, layout, |
| 101 | this->pipelineCache()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 104 | // To create framebuffers, we first need to create a simple RenderPass that is |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 105 | // only used for framebuffer creation. When we actually render we will create |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 106 | // RenderPasses as needed that are compatible with the framebuffer. |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 107 | const GrVkRenderPass* |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 108 | GrVkResourceProvider::findCompatibleRenderPass(const GrVkRenderTarget& target, |
| 109 | CompatibleRPHandle* compatibleHandle) { |
| 110 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
| 111 | if (fRenderPassArray[i].isCompatible(target)) { |
| 112 | const GrVkRenderPass* renderPass = fRenderPassArray[i].getCompatibleRenderPass(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 113 | renderPass->ref(); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 114 | if (compatibleHandle) { |
| 115 | *compatibleHandle = CompatibleRPHandle(i); |
| 116 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 117 | return renderPass; |
| 118 | } |
| 119 | } |
| 120 | |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 121 | GrVkRenderPass* renderPass = GrVkRenderPass::CreateSimple(fGpu, target); |
| 122 | if (!renderPass) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | fRenderPassArray.emplace_back(renderPass); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 126 | |
| 127 | if (compatibleHandle) { |
| 128 | *compatibleHandle = CompatibleRPHandle(fRenderPassArray.count() - 1); |
| 129 | } |
| 130 | return renderPass; |
| 131 | } |
| 132 | |
| 133 | const GrVkRenderPass* |
| 134 | GrVkResourceProvider::findCompatibleRenderPass(const CompatibleRPHandle& compatibleHandle) { |
| 135 | SkASSERT(compatibleHandle.isValid() && compatibleHandle.toIndex() < fRenderPassArray.count()); |
| 136 | int index = compatibleHandle.toIndex(); |
| 137 | const GrVkRenderPass* renderPass = fRenderPassArray[index].getCompatibleRenderPass(); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 138 | SkASSERT(renderPass); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 139 | renderPass->ref(); |
| 140 | return renderPass; |
| 141 | } |
| 142 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 143 | const GrVkRenderPass* GrVkResourceProvider::findCompatibleExternalRenderPass( |
| 144 | VkRenderPass renderPass, uint32_t colorAttachmentIndex) { |
| 145 | for (int i = 0; i < fExternalRenderPasses.count(); ++i) { |
| 146 | if (fExternalRenderPasses[i]->isCompatibleExternalRP(renderPass)) { |
| 147 | fExternalRenderPasses[i]->ref(); |
| 148 | #ifdef SK_DEBUG |
| 149 | uint32_t cachedColorIndex; |
| 150 | SkASSERT(fExternalRenderPasses[i]->colorAttachmentIndex(&cachedColorIndex)); |
| 151 | SkASSERT(cachedColorIndex == colorAttachmentIndex); |
| 152 | #endif |
| 153 | return fExternalRenderPasses[i]; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | const GrVkRenderPass* newRenderPass = new GrVkRenderPass(renderPass, colorAttachmentIndex); |
| 158 | fExternalRenderPasses.push_back(newRenderPass); |
| 159 | newRenderPass->ref(); |
| 160 | return newRenderPass; |
| 161 | } |
| 162 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 163 | const GrVkRenderPass* GrVkResourceProvider::findRenderPass( |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 164 | GrVkRenderTarget* target, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 165 | const GrVkRenderPass::LoadStoreOps& colorOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 166 | const GrVkRenderPass::LoadStoreOps& stencilOps, |
| 167 | CompatibleRPHandle* compatibleHandle) { |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 168 | GrVkResourceProvider::CompatibleRPHandle tempRPHandle; |
| 169 | GrVkResourceProvider::CompatibleRPHandle* pRPHandle = compatibleHandle ? compatibleHandle |
| 170 | : &tempRPHandle; |
Greg Daniel | fa3adf7 | 2019-11-07 09:53:41 -0500 | [diff] [blame] | 171 | *pRPHandle = target->compatibleRenderPassHandle(); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 172 | if (!pRPHandle->isValid()) { |
| 173 | return nullptr; |
| 174 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 175 | |
Greg Daniel | d368211 | 2016-10-03 15:06:07 -0400 | [diff] [blame] | 176 | return this->findRenderPass(*pRPHandle, colorOps, stencilOps); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | const GrVkRenderPass* |
| 180 | GrVkResourceProvider::findRenderPass(const CompatibleRPHandle& compatibleHandle, |
| 181 | const GrVkRenderPass::LoadStoreOps& colorOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 182 | const GrVkRenderPass::LoadStoreOps& stencilOps) { |
| 183 | SkASSERT(compatibleHandle.isValid() && compatibleHandle.toIndex() < fRenderPassArray.count()); |
| 184 | CompatibleRenderPassSet& compatibleSet = fRenderPassArray[compatibleHandle.toIndex()]; |
| 185 | const GrVkRenderPass* renderPass = compatibleSet.getRenderPass(fGpu, |
| 186 | colorOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 187 | stencilOps); |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 188 | if (!renderPass) { |
| 189 | return nullptr; |
| 190 | } |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 191 | renderPass->ref(); |
| 192 | return renderPass; |
| 193 | } |
| 194 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 195 | GrVkDescriptorPool* GrVkResourceProvider::findOrCreateCompatibleDescriptorPool( |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 196 | VkDescriptorType type, uint32_t count) { |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 197 | return GrVkDescriptorPool::Create(fGpu, type, count); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 200 | GrVkSampler* GrVkResourceProvider::findOrCreateCompatibleSampler( |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 201 | GrSamplerState params, const GrVkYcbcrConversionInfo& ycbcrInfo) { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 202 | GrVkSampler* sampler = fSamplers.find(GrVkSampler::GenerateKey(params, ycbcrInfo)); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 203 | if (!sampler) { |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 204 | sampler = GrVkSampler::Create(fGpu, params, ycbcrInfo); |
| 205 | if (!sampler) { |
| 206 | return nullptr; |
| 207 | } |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 208 | fSamplers.add(sampler); |
| 209 | } |
| 210 | SkASSERT(sampler); |
| 211 | sampler->ref(); |
| 212 | return sampler; |
| 213 | } |
| 214 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 215 | GrVkSamplerYcbcrConversion* GrVkResourceProvider::findOrCreateCompatibleSamplerYcbcrConversion( |
| 216 | const GrVkYcbcrConversionInfo& ycbcrInfo) { |
| 217 | GrVkSamplerYcbcrConversion* ycbcrConversion = |
| 218 | fYcbcrConversions.find(GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)); |
| 219 | if (!ycbcrConversion) { |
| 220 | ycbcrConversion = GrVkSamplerYcbcrConversion::Create(fGpu, ycbcrInfo); |
| 221 | if (!ycbcrConversion) { |
| 222 | return nullptr; |
| 223 | } |
| 224 | fYcbcrConversions.add(ycbcrConversion); |
| 225 | } |
| 226 | SkASSERT(ycbcrConversion); |
| 227 | ycbcrConversion->ref(); |
| 228 | return ycbcrConversion; |
| 229 | } |
| 230 | |
Greg Daniel | 09eeefb | 2017-10-16 15:15:02 -0400 | [diff] [blame] | 231 | GrVkPipelineState* GrVkResourceProvider::findOrCreateCompatiblePipelineState( |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 232 | GrRenderTarget* renderTarget, |
| 233 | const GrProgramInfo& programInfo, |
Greg Daniel | 99b88e0 | 2018-10-03 15:31:20 -0400 | [diff] [blame] | 234 | VkRenderPass compatibleRenderPass) { |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame^] | 235 | return fPipelineStateCache->findOrCreatePipelineState(renderTarget, programInfo, |
| 236 | compatibleRenderPass); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 239 | void GrVkResourceProvider::getSamplerDescriptorSetHandle(VkDescriptorType type, |
| 240 | const GrVkUniformHandler& uniformHandler, |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 241 | GrVkDescriptorSetManager::Handle* handle) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 242 | SkASSERT(handle); |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 243 | SkASSERT(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type || |
| 244 | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER == type); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 245 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 246 | if (fDescriptorSetManagers[i]->isCompatible(type, &uniformHandler)) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 247 | *handle = GrVkDescriptorSetManager::Handle(i); |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 252 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateSamplerManager(fGpu, type, |
| 253 | uniformHandler); |
| 254 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 255 | *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() - 1); |
| 256 | } |
| 257 | |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 258 | void GrVkResourceProvider::getSamplerDescriptorSetHandle(VkDescriptorType type, |
| 259 | const SkTArray<uint32_t>& visibilities, |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 260 | GrVkDescriptorSetManager::Handle* handle) { |
| 261 | SkASSERT(handle); |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 262 | SkASSERT(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type || |
| 263 | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER == type); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 264 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 265 | if (fDescriptorSetManagers[i]->isCompatible(type, visibilities)) { |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 266 | *handle = GrVkDescriptorSetManager::Handle(i); |
| 267 | return; |
| 268 | } |
| 269 | } |
| 270 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 271 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateSamplerManager(fGpu, type, |
| 272 | visibilities); |
| 273 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 274 | *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() - 1); |
| 275 | } |
| 276 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 277 | VkDescriptorSetLayout GrVkResourceProvider::getUniformDSLayout() const { |
| 278 | SkASSERT(fUniformDSHandle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 279 | return fDescriptorSetManagers[fUniformDSHandle.toIndex()]->layout(); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | VkDescriptorSetLayout GrVkResourceProvider::getSamplerDSLayout( |
| 283 | const GrVkDescriptorSetManager::Handle& handle) const { |
| 284 | SkASSERT(handle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 285 | return fDescriptorSetManagers[handle.toIndex()]->layout(); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 286 | } |
| 287 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 288 | const GrVkDescriptorSet* GrVkResourceProvider::getUniformDescriptorSet() { |
| 289 | SkASSERT(fUniformDSHandle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 290 | return fDescriptorSetManagers[fUniformDSHandle.toIndex()]->getDescriptorSet(fGpu, |
| 291 | fUniformDSHandle); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 292 | } |
| 293 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 294 | const GrVkDescriptorSet* GrVkResourceProvider::getSamplerDescriptorSet( |
| 295 | const GrVkDescriptorSetManager::Handle& handle) { |
| 296 | SkASSERT(handle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 297 | return fDescriptorSetManagers[handle.toIndex()]->getDescriptorSet(fGpu, handle); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 298 | } |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 299 | |
| 300 | void GrVkResourceProvider::recycleDescriptorSet(const GrVkDescriptorSet* descSet, |
| 301 | const GrVkDescriptorSetManager::Handle& handle) { |
| 302 | SkASSERT(descSet); |
| 303 | SkASSERT(handle.isValid()); |
| 304 | int managerIdx = handle.toIndex(); |
| 305 | SkASSERT(managerIdx < fDescriptorSetManagers.count()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 306 | fDescriptorSetManagers[managerIdx]->recycleDescriptorSet(descSet); |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 309 | GrVkCommandPool* GrVkResourceProvider::findOrCreateCommandPool() { |
| 310 | std::unique_lock<std::recursive_mutex> lock(fBackgroundMutex); |
| 311 | GrVkCommandPool* result; |
| 312 | if (fAvailableCommandPools.count()) { |
| 313 | result = fAvailableCommandPools.back(); |
| 314 | fAvailableCommandPools.pop_back(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 315 | } else { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 316 | result = GrVkCommandPool::Create(fGpu); |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 317 | if (!result) { |
| 318 | return nullptr; |
| 319 | } |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 320 | } |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 321 | SkASSERT(result->unique()); |
| 322 | SkDEBUGCODE( |
| 323 | for (const GrVkCommandPool* pool : fActiveCommandPools) { |
| 324 | SkASSERT(pool != result); |
| 325 | } |
| 326 | for (const GrVkCommandPool* pool : fAvailableCommandPools) { |
| 327 | SkASSERT(pool != result); |
| 328 | } |
Ben Wagner | 1c0cacf | 2019-01-14 12:57:36 -0500 | [diff] [blame] | 329 | ) |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 330 | fActiveCommandPools.push_back(result); |
| 331 | result->ref(); |
| 332 | return result; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void GrVkResourceProvider::checkCommandBuffers() { |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 336 | for (int i = fActiveCommandPools.count() - 1; i >= 0; --i) { |
| 337 | GrVkCommandPool* pool = fActiveCommandPools[i]; |
| 338 | if (!pool->isOpen()) { |
| 339 | GrVkPrimaryCommandBuffer* buffer = pool->getPrimaryCommandBuffer(); |
| 340 | if (buffer->finished(fGpu)) { |
| 341 | fActiveCommandPools.removeShuffle(i); |
| 342 | this->backgroundReset(pool); |
| 343 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Greg Daniel | a3aa75a | 2019-04-12 14:24:55 -0400 | [diff] [blame] | 348 | void GrVkResourceProvider::addFinishedProcToActiveCommandBuffers( |
| 349 | GrGpuFinishedProc finishedProc, GrGpuFinishedContext finishedContext) { |
| 350 | sk_sp<GrRefCntedCallback> procRef(new GrRefCntedCallback(finishedProc, finishedContext)); |
| 351 | for (int i = 0; i < fActiveCommandPools.count(); ++i) { |
| 352 | GrVkCommandPool* pool = fActiveCommandPools[i]; |
| 353 | if (!pool->isOpen()) { |
| 354 | GrVkPrimaryCommandBuffer* buffer = pool->getPrimaryCommandBuffer(); |
| 355 | buffer->addFinishedProc(procRef); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 360 | const GrVkResource* GrVkResourceProvider::findOrCreateStandardUniformBufferResource() { |
| 361 | const GrVkResource* resource = nullptr; |
| 362 | int count = fAvailableUniformBufferResources.count(); |
| 363 | if (count > 0) { |
| 364 | resource = fAvailableUniformBufferResources[count - 1]; |
| 365 | fAvailableUniformBufferResources.removeShuffle(count - 1); |
| 366 | } else { |
| 367 | resource = GrVkUniformBuffer::CreateResource(fGpu, GrVkUniformBuffer::kStandardSize); |
| 368 | } |
| 369 | return resource; |
| 370 | } |
| 371 | |
| 372 | void GrVkResourceProvider::recycleStandardUniformBufferResource(const GrVkResource* resource) { |
| 373 | fAvailableUniformBufferResources.push_back(resource); |
| 374 | } |
| 375 | |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 376 | void GrVkResourceProvider::destroyResources(bool deviceLost) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 377 | SkTaskGroup* taskGroup = fGpu->getContext()->priv().getTaskGroup(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 378 | if (taskGroup) { |
| 379 | taskGroup->wait(); |
Ethan Nicholas | bff4e07 | 2018-12-12 18:17:24 +0000 | [diff] [blame] | 380 | } |
Ethan Nicholas | bff4e07 | 2018-12-12 18:17:24 +0000 | [diff] [blame] | 381 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 382 | // loop over all render pass sets to make sure we destroy all the internal VkRenderPasses |
| 383 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
| 384 | fRenderPassArray[i].releaseResources(fGpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 385 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 386 | fRenderPassArray.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 387 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 388 | for (int i = 0; i < fExternalRenderPasses.count(); ++i) { |
| 389 | fExternalRenderPasses[i]->unref(fGpu); |
| 390 | } |
| 391 | fExternalRenderPasses.reset(); |
| 392 | |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 393 | // Iterate through all store GrVkSamplers and unref them before resetting the hash. |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 394 | for (decltype(fSamplers)::Iter iter(&fSamplers); !iter.done(); ++iter) { |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 395 | (*iter).unref(fGpu); |
| 396 | } |
| 397 | fSamplers.reset(); |
| 398 | |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 399 | for (decltype(fYcbcrConversions)::Iter iter(&fYcbcrConversions); !iter.done(); ++iter) { |
| 400 | (*iter).unref(fGpu); |
| 401 | } |
| 402 | fYcbcrConversions.reset(); |
| 403 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 404 | fPipelineStateCache->release(); |
| 405 | |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 406 | GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineCache(fGpu->device(), fPipelineCache, nullptr)); |
| 407 | fPipelineCache = VK_NULL_HANDLE; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 408 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 409 | for (GrVkCommandPool* pool : fActiveCommandPools) { |
| 410 | SkASSERT(pool->unique()); |
| 411 | pool->unref(fGpu); |
| 412 | } |
| 413 | fActiveCommandPools.reset(); |
| 414 | |
| 415 | for (GrVkCommandPool* pool : fAvailableCommandPools) { |
| 416 | SkASSERT(pool->unique()); |
| 417 | pool->unref(fGpu); |
| 418 | } |
| 419 | fAvailableCommandPools.reset(); |
| 420 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 421 | // We must release/destroy all command buffers and pipeline states before releasing the |
| 422 | // GrVkDescriptorSetManagers |
| 423 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 424 | fDescriptorSetManagers[i]->release(fGpu); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 425 | } |
| 426 | fDescriptorSetManagers.reset(); |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 427 | |
| 428 | // release our uniform buffers |
| 429 | for (int i = 0; i < fAvailableUniformBufferResources.count(); ++i) { |
| 430 | SkASSERT(fAvailableUniformBufferResources[i]->unique()); |
| 431 | fAvailableUniformBufferResources[i]->unref(fGpu); |
| 432 | } |
| 433 | fAvailableUniformBufferResources.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 434 | } |
| 435 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 436 | void GrVkResourceProvider::backgroundReset(GrVkCommandPool* pool) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 437 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 438 | SkASSERT(pool->unique()); |
| 439 | pool->releaseResources(fGpu); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 440 | SkTaskGroup* taskGroup = fGpu->getContext()->priv().getTaskGroup(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 441 | if (taskGroup) { |
| 442 | taskGroup->add([this, pool]() { |
| 443 | this->reset(pool); |
| 444 | }); |
| 445 | } else { |
| 446 | this->reset(pool); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void GrVkResourceProvider::reset(GrVkCommandPool* pool) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 451 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 452 | SkASSERT(pool->unique()); |
| 453 | pool->reset(fGpu); |
| 454 | std::unique_lock<std::recursive_mutex> providerLock(fBackgroundMutex); |
| 455 | fAvailableCommandPools.push_back(pool); |
| 456 | } |
| 457 | |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 458 | void GrVkResourceProvider::storePipelineCacheData() { |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 459 | if (this->pipelineCache() == VK_NULL_HANDLE) { |
| 460 | return; |
| 461 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 462 | size_t dataSize = 0; |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 463 | VkResult result; |
| 464 | GR_VK_CALL_RESULT(fGpu, result, GetPipelineCacheData(fGpu->device(), this->pipelineCache(), |
| 465 | &dataSize, nullptr)); |
| 466 | if (result != VK_SUCCESS) { |
| 467 | return; |
| 468 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 469 | |
| 470 | std::unique_ptr<uint8_t[]> data(new uint8_t[dataSize]); |
| 471 | |
Greg Daniel | d034c62 | 2019-11-20 09:33:29 -0500 | [diff] [blame] | 472 | GR_VK_CALL_RESULT(fGpu, result, GetPipelineCacheData(fGpu->device(), this->pipelineCache(), |
| 473 | &dataSize, (void*)data.get())); |
| 474 | if (result != VK_SUCCESS) { |
| 475 | return; |
| 476 | } |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 477 | |
| 478 | uint32_t key = GrVkGpu::kPipelineCache_PersistentCacheKeyType; |
| 479 | sk_sp<SkData> keyData = SkData::MakeWithoutCopy(&key, sizeof(uint32_t)); |
| 480 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 481 | fGpu->getContext()->priv().getPersistentCache()->store( |
Greg Daniel | a870b46 | 2019-01-08 15:49:46 -0500 | [diff] [blame] | 482 | *keyData, *SkData::MakeWithoutCopy(data.get(), dataSize)); |
| 483 | } |
| 484 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 485 | //////////////////////////////////////////////////////////////////////////////// |
| 486 | |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 487 | GrVkResourceProvider::CompatibleRenderPassSet::CompatibleRenderPassSet(GrVkRenderPass* renderPass) |
| 488 | : fLastReturnedIndex(0) { |
| 489 | renderPass->ref(); |
| 490 | fRenderPasses.push_back(renderPass); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | bool GrVkResourceProvider::CompatibleRenderPassSet::isCompatible( |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 494 | const GrVkRenderTarget& target) const { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 495 | // The first GrVkRenderpass should always exists since we create the basic load store |
| 496 | // render pass on create |
| 497 | SkASSERT(fRenderPasses[0]); |
| 498 | return fRenderPasses[0]->isCompatible(target); |
| 499 | } |
| 500 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 501 | GrVkRenderPass* GrVkResourceProvider::CompatibleRenderPassSet::getRenderPass( |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 502 | GrVkGpu* gpu, |
| 503 | const GrVkRenderPass::LoadStoreOps& colorOps, |
| 504 | const GrVkRenderPass::LoadStoreOps& stencilOps) { |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 505 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 506 | int idx = (i + fLastReturnedIndex) % fRenderPasses.count(); |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 507 | if (fRenderPasses[idx]->equalLoadStoreOps(colorOps, stencilOps)) { |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 508 | fLastReturnedIndex = idx; |
| 509 | return fRenderPasses[idx]; |
| 510 | } |
| 511 | } |
Greg Daniel | ed98476 | 2019-11-07 17:15:45 -0500 | [diff] [blame] | 512 | GrVkRenderPass* renderPass = GrVkRenderPass::Create(gpu, *this->getCompatibleRenderPass(), |
| 513 | colorOps, stencilOps); |
| 514 | if (!renderPass) { |
| 515 | return nullptr; |
| 516 | } |
| 517 | fRenderPasses.push_back(renderPass); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 518 | fLastReturnedIndex = fRenderPasses.count() - 1; |
| 519 | return renderPass; |
| 520 | } |
| 521 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 522 | void GrVkResourceProvider::CompatibleRenderPassSet::releaseResources(GrVkGpu* gpu) { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 523 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 524 | if (fRenderPasses[i]) { |
| 525 | fRenderPasses[i]->unref(gpu); |
| 526 | fRenderPasses[i] = nullptr; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |