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