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