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, |
| 65 | const GrVkRenderPass& renderPass, |
| 66 | VkPipelineLayout layout) { |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 67 | return GrVkPipeline::Create(fGpu, primProc, pipeline, stencil, shaderStageInfo, |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 68 | shaderStageCount, primitiveType, renderPass, layout, |
| 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( |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 182 | const GrPipeline& pipeline, |
| 183 | const GrPrimitiveProcessor& proc, |
| 184 | GrPrimitiveType primitiveType, |
| 185 | const GrVkRenderPass& renderPass) { |
Brian Salomon | ff168d9 | 2018-06-23 15:17:27 -0400 | [diff] [blame] | 186 | return fPipelineStateCache->refPipelineState(proc, pipeline, primitiveType, renderPass); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 189 | void GrVkResourceProvider::getSamplerDescriptorSetHandle(VkDescriptorType type, |
| 190 | const GrVkUniformHandler& uniformHandler, |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 191 | GrVkDescriptorSetManager::Handle* handle) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 192 | SkASSERT(handle); |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 193 | SkASSERT(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type || |
| 194 | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER == type); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 195 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 196 | if (fDescriptorSetManagers[i]->isCompatible(type, &uniformHandler)) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 197 | *handle = GrVkDescriptorSetManager::Handle(i); |
| 198 | return; |
| 199 | } |
| 200 | } |
| 201 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 202 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateSamplerManager(fGpu, type, |
| 203 | uniformHandler); |
| 204 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 205 | *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() - 1); |
| 206 | } |
| 207 | |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 208 | void GrVkResourceProvider::getSamplerDescriptorSetHandle(VkDescriptorType type, |
| 209 | const SkTArray<uint32_t>& visibilities, |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 210 | GrVkDescriptorSetManager::Handle* handle) { |
| 211 | SkASSERT(handle); |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 212 | SkASSERT(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type || |
| 213 | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER == type); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 214 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 215 | if (fDescriptorSetManagers[i]->isCompatible(type, visibilities)) { |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 216 | *handle = GrVkDescriptorSetManager::Handle(i); |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 221 | GrVkDescriptorSetManager* dsm = GrVkDescriptorSetManager::CreateSamplerManager(fGpu, type, |
| 222 | visibilities); |
| 223 | fDescriptorSetManagers.emplace_back(dsm); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 224 | *handle = GrVkDescriptorSetManager::Handle(fDescriptorSetManagers.count() - 1); |
| 225 | } |
| 226 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 227 | VkDescriptorSetLayout GrVkResourceProvider::getUniformDSLayout() const { |
| 228 | SkASSERT(fUniformDSHandle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 229 | return fDescriptorSetManagers[fUniformDSHandle.toIndex()]->layout(); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | VkDescriptorSetLayout GrVkResourceProvider::getSamplerDSLayout( |
| 233 | const GrVkDescriptorSetManager::Handle& handle) const { |
| 234 | SkASSERT(handle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 235 | return fDescriptorSetManagers[handle.toIndex()]->layout(); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 236 | } |
| 237 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 238 | const GrVkDescriptorSet* GrVkResourceProvider::getUniformDescriptorSet() { |
| 239 | SkASSERT(fUniformDSHandle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 240 | return fDescriptorSetManagers[fUniformDSHandle.toIndex()]->getDescriptorSet(fGpu, |
| 241 | fUniformDSHandle); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 242 | } |
| 243 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 244 | const GrVkDescriptorSet* GrVkResourceProvider::getSamplerDescriptorSet( |
| 245 | const GrVkDescriptorSetManager::Handle& handle) { |
| 246 | SkASSERT(handle.isValid()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 247 | return fDescriptorSetManagers[handle.toIndex()]->getDescriptorSet(fGpu, handle); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 248 | } |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 249 | |
| 250 | void GrVkResourceProvider::recycleDescriptorSet(const GrVkDescriptorSet* descSet, |
| 251 | const GrVkDescriptorSetManager::Handle& handle) { |
| 252 | SkASSERT(descSet); |
| 253 | SkASSERT(handle.isValid()); |
| 254 | int managerIdx = handle.toIndex(); |
| 255 | SkASSERT(managerIdx < fDescriptorSetManagers.count()); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 256 | fDescriptorSetManagers[managerIdx]->recycleDescriptorSet(descSet); |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 259 | GrVkPrimaryCommandBuffer* GrVkResourceProvider::findOrCreatePrimaryCommandBuffer() { |
| 260 | GrVkPrimaryCommandBuffer* cmdBuffer = nullptr; |
| 261 | int count = fAvailableCommandBuffers.count(); |
| 262 | if (count > 0) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 263 | cmdBuffer = fAvailableCommandBuffers[count - 1]; |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 264 | SkASSERT(cmdBuffer->finished(fGpu)); |
| 265 | fAvailableCommandBuffers.removeShuffle(count - 1); |
| 266 | } else { |
| 267 | cmdBuffer = GrVkPrimaryCommandBuffer::Create(fGpu, fGpu->cmdPool()); |
| 268 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 269 | fActiveCommandBuffers.push_back(cmdBuffer); |
| 270 | cmdBuffer->ref(); |
| 271 | return cmdBuffer; |
| 272 | } |
| 273 | |
| 274 | void GrVkResourceProvider::checkCommandBuffers() { |
| 275 | for (int i = fActiveCommandBuffers.count()-1; i >= 0; --i) { |
| 276 | if (fActiveCommandBuffers[i]->finished(fGpu)) { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 277 | GrVkPrimaryCommandBuffer* cmdBuffer = fActiveCommandBuffers[i]; |
| 278 | cmdBuffer->reset(fGpu); |
| 279 | fAvailableCommandBuffers.push_back(cmdBuffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 280 | fActiveCommandBuffers.removeShuffle(i); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 285 | GrVkSecondaryCommandBuffer* GrVkResourceProvider::findOrCreateSecondaryCommandBuffer() { |
| 286 | GrVkSecondaryCommandBuffer* cmdBuffer = nullptr; |
| 287 | int count = fAvailableSecondaryCommandBuffers.count(); |
| 288 | if (count > 0) { |
| 289 | cmdBuffer = fAvailableSecondaryCommandBuffers[count-1]; |
| 290 | fAvailableSecondaryCommandBuffers.removeShuffle(count - 1); |
| 291 | } else { |
| 292 | cmdBuffer = GrVkSecondaryCommandBuffer::Create(fGpu, fGpu->cmdPool()); |
| 293 | } |
| 294 | return cmdBuffer; |
| 295 | } |
| 296 | |
| 297 | void GrVkResourceProvider::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* cb) { |
| 298 | cb->reset(fGpu); |
| 299 | fAvailableSecondaryCommandBuffers.push_back(cb); |
| 300 | } |
| 301 | |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 302 | const GrVkResource* GrVkResourceProvider::findOrCreateStandardUniformBufferResource() { |
| 303 | const GrVkResource* resource = nullptr; |
| 304 | int count = fAvailableUniformBufferResources.count(); |
| 305 | if (count > 0) { |
| 306 | resource = fAvailableUniformBufferResources[count - 1]; |
| 307 | fAvailableUniformBufferResources.removeShuffle(count - 1); |
| 308 | } else { |
| 309 | resource = GrVkUniformBuffer::CreateResource(fGpu, GrVkUniformBuffer::kStandardSize); |
| 310 | } |
| 311 | return resource; |
| 312 | } |
| 313 | |
| 314 | void GrVkResourceProvider::recycleStandardUniformBufferResource(const GrVkResource* resource) { |
| 315 | fAvailableUniformBufferResources.push_back(resource); |
| 316 | } |
| 317 | |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 318 | void GrVkResourceProvider::destroyResources(bool deviceLost) { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 319 | // release our active command buffers |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 320 | for (int i = 0; i < fActiveCommandBuffers.count(); ++i) { |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 321 | SkASSERT(deviceLost || fActiveCommandBuffers[i]->finished(fGpu)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 322 | SkASSERT(fActiveCommandBuffers[i]->unique()); |
jvanverth | 069c464 | 2016-07-06 12:56:11 -0700 | [diff] [blame] | 323 | fActiveCommandBuffers[i]->reset(fGpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 324 | fActiveCommandBuffers[i]->unref(fGpu); |
| 325 | } |
| 326 | fActiveCommandBuffers.reset(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 327 | // release our available command buffers |
| 328 | for (int i = 0; i < fAvailableCommandBuffers.count(); ++i) { |
Jim Van Verth | 09557d7 | 2016-11-07 11:10:21 -0500 | [diff] [blame] | 329 | SkASSERT(deviceLost || fAvailableCommandBuffers[i]->finished(fGpu)); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 330 | SkASSERT(fAvailableCommandBuffers[i]->unique()); |
| 331 | fAvailableCommandBuffers[i]->unref(fGpu); |
| 332 | } |
| 333 | fAvailableCommandBuffers.reset(); |
| 334 | |
| 335 | // release our available secondary command buffers |
| 336 | for (int i = 0; i < fAvailableSecondaryCommandBuffers.count(); ++i) { |
| 337 | SkASSERT(fAvailableSecondaryCommandBuffers[i]->unique()); |
| 338 | fAvailableSecondaryCommandBuffers[i]->unref(fGpu); |
| 339 | } |
| 340 | fAvailableSecondaryCommandBuffers.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 341 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 342 | // Release all copy pipelines |
| 343 | for (int i = 0; i < fCopyPipelines.count(); ++i) { |
| 344 | fCopyPipelines[i]->unref(fGpu); |
| 345 | } |
| 346 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 347 | // loop over all render pass sets to make sure we destroy all the internal VkRenderPasses |
| 348 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
| 349 | fRenderPassArray[i].releaseResources(fGpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 350 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 351 | fRenderPassArray.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 352 | |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 353 | // Iterate through all store GrVkSamplers and unref them before resetting the hash. |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 354 | SkTDynamicHash<GrVkSampler, uint16_t>::Iter iter(&fSamplers); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 355 | for (; !iter.done(); ++iter) { |
| 356 | (*iter).unref(fGpu); |
| 357 | } |
| 358 | fSamplers.reset(); |
| 359 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 360 | fPipelineStateCache->release(); |
| 361 | |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 362 | GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineCache(fGpu->device(), fPipelineCache, nullptr)); |
| 363 | fPipelineCache = VK_NULL_HANDLE; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 364 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 365 | // We must release/destroy all command buffers and pipeline states before releasing the |
| 366 | // GrVkDescriptorSetManagers |
| 367 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 368 | fDescriptorSetManagers[i]->release(fGpu); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 369 | } |
| 370 | fDescriptorSetManagers.reset(); |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 371 | |
| 372 | // release our uniform buffers |
| 373 | for (int i = 0; i < fAvailableUniformBufferResources.count(); ++i) { |
| 374 | SkASSERT(fAvailableUniformBufferResources[i]->unique()); |
| 375 | fAvailableUniformBufferResources[i]->unref(fGpu); |
| 376 | } |
| 377 | fAvailableUniformBufferResources.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void GrVkResourceProvider::abandonResources() { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 381 | // release our active command buffers |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 382 | for (int i = 0; i < fActiveCommandBuffers.count(); ++i) { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 383 | SkASSERT(fActiveCommandBuffers[i]->unique()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 384 | fActiveCommandBuffers[i]->unrefAndAbandon(); |
| 385 | } |
| 386 | fActiveCommandBuffers.reset(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 387 | // release our available command buffers |
| 388 | for (int i = 0; i < fAvailableCommandBuffers.count(); ++i) { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 389 | SkASSERT(fAvailableCommandBuffers[i]->unique()); |
| 390 | fAvailableCommandBuffers[i]->unrefAndAbandon(); |
| 391 | } |
| 392 | fAvailableCommandBuffers.reset(); |
| 393 | |
| 394 | // release our available secondary command buffers |
| 395 | for (int i = 0; i < fAvailableSecondaryCommandBuffers.count(); ++i) { |
| 396 | SkASSERT(fAvailableSecondaryCommandBuffers[i]->unique()); |
| 397 | fAvailableSecondaryCommandBuffers[i]->unrefAndAbandon(); |
| 398 | } |
| 399 | fAvailableSecondaryCommandBuffers.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 400 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 401 | // Abandon all copy pipelines |
| 402 | for (int i = 0; i < fCopyPipelines.count(); ++i) { |
| 403 | fCopyPipelines[i]->unrefAndAbandon(); |
| 404 | } |
| 405 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 406 | // loop over all render pass sets to make sure we destroy all the internal VkRenderPasses |
| 407 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
| 408 | fRenderPassArray[i].abandonResources(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 409 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 410 | fRenderPassArray.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 411 | |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 412 | // Iterate through all store GrVkSamplers and unrefAndAbandon them before resetting the hash. |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 413 | SkTDynamicHash<GrVkSampler, uint16_t>::Iter iter(&fSamplers); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 414 | for (; !iter.done(); ++iter) { |
| 415 | (*iter).unrefAndAbandon(); |
| 416 | } |
| 417 | fSamplers.reset(); |
| 418 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 419 | fPipelineStateCache->abandon(); |
| 420 | |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 421 | fPipelineCache = VK_NULL_HANDLE; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 422 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 423 | // We must abandon all command buffers and pipeline states before abandoning the |
| 424 | // GrVkDescriptorSetManagers |
| 425 | for (int i = 0; i < fDescriptorSetManagers.count(); ++i) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 426 | fDescriptorSetManagers[i]->abandon(); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 427 | } |
| 428 | fDescriptorSetManagers.reset(); |
| 429 | |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 430 | // release our uniform buffers |
| 431 | for (int i = 0; i < fAvailableUniformBufferResources.count(); ++i) { |
| 432 | SkASSERT(fAvailableUniformBufferResources[i]->unique()); |
| 433 | fAvailableUniformBufferResources[i]->unrefAndAbandon(); |
| 434 | } |
| 435 | fAvailableUniformBufferResources.reset(); |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 436 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 437 | |
| 438 | //////////////////////////////////////////////////////////////////////////////// |
| 439 | |
| 440 | GrVkResourceProvider::CompatibleRenderPassSet::CompatibleRenderPassSet( |
| 441 | const GrVkGpu* gpu, |
| 442 | const GrVkRenderTarget& target) |
| 443 | : fLastReturnedIndex(0) { |
| 444 | fRenderPasses.emplace_back(new GrVkRenderPass()); |
| 445 | fRenderPasses[0]->initSimple(gpu, target); |
| 446 | } |
| 447 | |
| 448 | bool GrVkResourceProvider::CompatibleRenderPassSet::isCompatible( |
| 449 | const GrVkRenderTarget& target) const { |
| 450 | // The first GrVkRenderpass should always exists since we create the basic load store |
| 451 | // render pass on create |
| 452 | SkASSERT(fRenderPasses[0]); |
| 453 | return fRenderPasses[0]->isCompatible(target); |
| 454 | } |
| 455 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 456 | GrVkRenderPass* GrVkResourceProvider::CompatibleRenderPassSet::getRenderPass( |
| 457 | const GrVkGpu* gpu, |
| 458 | const GrVkRenderPass::LoadStoreOps& colorOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 459 | const GrVkRenderPass::LoadStoreOps& stencilOps) { |
| 460 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 461 | int idx = (i + fLastReturnedIndex) % fRenderPasses.count(); |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 462 | if (fRenderPasses[idx]->equalLoadStoreOps(colorOps, stencilOps)) { |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 463 | fLastReturnedIndex = idx; |
| 464 | return fRenderPasses[idx]; |
| 465 | } |
| 466 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 467 | GrVkRenderPass* renderPass = fRenderPasses.emplace_back(new GrVkRenderPass()); |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 468 | renderPass->init(gpu, *this->getCompatibleRenderPass(), colorOps, stencilOps); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 469 | fLastReturnedIndex = fRenderPasses.count() - 1; |
| 470 | return renderPass; |
| 471 | } |
| 472 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 473 | void GrVkResourceProvider::CompatibleRenderPassSet::releaseResources(const GrVkGpu* gpu) { |
| 474 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 475 | if (fRenderPasses[i]) { |
| 476 | fRenderPasses[i]->unref(gpu); |
| 477 | fRenderPasses[i] = nullptr; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | void GrVkResourceProvider::CompatibleRenderPassSet::abandonResources() { |
| 483 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 484 | if (fRenderPasses[i]) { |
| 485 | fRenderPasses[i]->unrefAndAbandon(); |
| 486 | fRenderPasses[i] = nullptr; |
| 487 | } |
| 488 | } |
| 489 | } |