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