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