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