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