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