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" |
| 12 | #include "GrVkPipeline.h" |
| 13 | #include "GrVkRenderPass.h" |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 14 | #include "GrVkSampler.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 15 | #include "GrVkUtil.h" |
| 16 | |
| 17 | #ifdef SK_TRACE_VK_RESOURCES |
| 18 | SkTDynamicHash<GrVkResource, uint32_t> GrVkResource::fTrace; |
| 19 | SkRandom GrVkResource::fRandom; |
| 20 | #endif |
| 21 | |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 22 | GrVkResourceProvider::GrVkResourceProvider(GrVkGpu* gpu) |
| 23 | : fGpu(gpu) |
| 24 | , fPipelineCache(VK_NULL_HANDLE) |
| 25 | , fUniformDescPool(nullptr) |
| 26 | , fCurrentUniformDescCount(0) { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 27 | fPipelineStateCache = new PipelineStateCache(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | GrVkResourceProvider::~GrVkResourceProvider() { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 31 | SkASSERT(0 == fRenderPassArray.count()); |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 32 | SkASSERT(VK_NULL_HANDLE == fPipelineCache); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 33 | delete fPipelineStateCache; |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 34 | } |
| 35 | |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 36 | void GrVkResourceProvider::initUniformDescObjects() { |
| 37 | // Create Uniform Buffer Descriptor |
| 38 | // The vertex uniform buffer will have binding 0 and the fragment binding 1. |
| 39 | VkDescriptorSetLayoutBinding dsUniBindings[2]; |
| 40 | memset(&dsUniBindings, 0, 2 * sizeof(VkDescriptorSetLayoutBinding)); |
| 41 | dsUniBindings[0].binding = GrVkUniformHandler::kVertexBinding; |
| 42 | dsUniBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 43 | dsUniBindings[0].descriptorCount = 1; |
| 44 | dsUniBindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; |
| 45 | dsUniBindings[0].pImmutableSamplers = nullptr; |
| 46 | dsUniBindings[1].binding = GrVkUniformHandler::kFragBinding; |
| 47 | dsUniBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 48 | dsUniBindings[1].descriptorCount = 1; |
| 49 | dsUniBindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; |
| 50 | dsUniBindings[1].pImmutableSamplers = nullptr; |
| 51 | |
| 52 | VkDescriptorSetLayoutCreateInfo dsUniformLayoutCreateInfo; |
| 53 | memset(&dsUniformLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreateInfo)); |
| 54 | dsUniformLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 55 | dsUniformLayoutCreateInfo.pNext = nullptr; |
| 56 | dsUniformLayoutCreateInfo.flags = 0; |
| 57 | dsUniformLayoutCreateInfo.bindingCount = 2; |
| 58 | dsUniformLayoutCreateInfo.pBindings = dsUniBindings; |
| 59 | |
| 60 | GR_VK_CALL_ERRCHECK(fGpu->vkInterface(), CreateDescriptorSetLayout(fGpu->device(), |
| 61 | &dsUniformLayoutCreateInfo, |
| 62 | nullptr, |
| 63 | &fUniformDescLayout)); |
| 64 | fCurrMaxUniDescriptors = kStartNumUniformDescriptors; |
| 65 | fUniformDescPool = this->findOrCreateCompatibleDescriptorPool(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 66 | fCurrMaxUniDescriptors); |
| 67 | } |
| 68 | |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 69 | void GrVkResourceProvider::init() { |
| 70 | VkPipelineCacheCreateInfo createInfo; |
| 71 | memset(&createInfo, 0, sizeof(VkPipelineCacheCreateInfo)); |
| 72 | createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 73 | createInfo.pNext = nullptr; |
| 74 | createInfo.flags = 0; |
| 75 | createInfo.initialDataSize = 0; |
| 76 | createInfo.pInitialData = nullptr; |
| 77 | VkResult result = GR_VK_CALL(fGpu->vkInterface(), |
| 78 | CreatePipelineCache(fGpu->device(), &createInfo, nullptr, |
| 79 | &fPipelineCache)); |
| 80 | SkASSERT(VK_SUCCESS == result); |
| 81 | if (VK_SUCCESS != result) { |
| 82 | fPipelineCache = VK_NULL_HANDLE; |
| 83 | } |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 84 | |
| 85 | this->initUniformDescObjects(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | GrVkPipeline* GrVkResourceProvider::createPipeline(const GrPipeline& pipeline, |
| 89 | const GrPrimitiveProcessor& primProc, |
| 90 | VkPipelineShaderStageCreateInfo* shaderStageInfo, |
| 91 | int shaderStageCount, |
| 92 | GrPrimitiveType primitiveType, |
| 93 | const GrVkRenderPass& renderPass, |
| 94 | VkPipelineLayout layout) { |
| 95 | |
| 96 | return GrVkPipeline::Create(fGpu, pipeline, primProc, shaderStageInfo, shaderStageCount, |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 97 | primitiveType, renderPass, layout, fPipelineCache); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 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 | |
| 137 | GrVkDescriptorPool* GrVkResourceProvider::findOrCreateCompatibleDescriptorPool( |
egdaniel | c2dc1b2 | 2016-03-18 13:18:23 -0700 | [diff] [blame] | 138 | VkDescriptorType type, uint32_t count) { |
| 139 | return new GrVkDescriptorPool(fGpu, type, count); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 140 | } |
| 141 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 142 | GrVkSampler* GrVkResourceProvider::findOrCreateCompatibleSampler(const GrTextureParams& params, |
| 143 | uint32_t mipLevels) { |
| 144 | GrVkSampler* sampler = fSamplers.find(GrVkSampler::GenerateKey(params, mipLevels)); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 145 | if (!sampler) { |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 146 | sampler = GrVkSampler::Create(fGpu, params, mipLevels); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 147 | fSamplers.add(sampler); |
| 148 | } |
| 149 | SkASSERT(sampler); |
| 150 | sampler->ref(); |
| 151 | return sampler; |
| 152 | } |
| 153 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 154 | sk_sp<GrVkPipelineState> GrVkResourceProvider::findOrCreateCompatiblePipelineState( |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 155 | const GrPipeline& pipeline, |
| 156 | const GrPrimitiveProcessor& proc, |
| 157 | GrPrimitiveType primitiveType, |
| 158 | const GrVkRenderPass& renderPass) { |
| 159 | return fPipelineStateCache->refPipelineState(pipeline, proc, primitiveType, renderPass); |
| 160 | } |
| 161 | |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 162 | void GrVkResourceProvider::getUniformDescriptorSet(VkDescriptorSet* ds, |
| 163 | const GrVkDescriptorPool** outPool) { |
| 164 | fCurrentUniformDescCount += kNumUniformDescPerSet; |
| 165 | if (fCurrentUniformDescCount > fCurrMaxUniDescriptors) { |
| 166 | fUniformDescPool->unref(fGpu); |
| 167 | if (fCurrMaxUniDescriptors < kMaxUniformDescriptors >> 1) { |
| 168 | fCurrMaxUniDescriptors = fCurrMaxUniDescriptors << 1; |
| 169 | } else { |
| 170 | fCurrMaxUniDescriptors = kMaxUniformDescriptors; |
| 171 | } |
| 172 | fUniformDescPool = |
| 173 | this->findOrCreateCompatibleDescriptorPool(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 174 | fCurrMaxUniDescriptors); |
| 175 | fCurrentUniformDescCount = kNumUniformDescPerSet; |
| 176 | } |
| 177 | SkASSERT(fUniformDescPool); |
| 178 | |
| 179 | VkDescriptorSetAllocateInfo dsAllocateInfo; |
| 180 | memset(&dsAllocateInfo, 0, sizeof(VkDescriptorSetAllocateInfo)); |
| 181 | dsAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 182 | dsAllocateInfo.pNext = nullptr; |
| 183 | dsAllocateInfo.descriptorPool = fUniformDescPool->descPool(); |
| 184 | dsAllocateInfo.descriptorSetCount = 1; |
| 185 | dsAllocateInfo.pSetLayouts = &fUniformDescLayout; |
| 186 | GR_VK_CALL_ERRCHECK(fGpu->vkInterface(), AllocateDescriptorSets(fGpu->device(), |
| 187 | &dsAllocateInfo, |
| 188 | ds)); |
| 189 | *outPool = fUniformDescPool; |
| 190 | } |
| 191 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 192 | GrVkCommandBuffer* GrVkResourceProvider::createCommandBuffer() { |
| 193 | GrVkCommandBuffer* cmdBuffer = GrVkCommandBuffer::Create(fGpu, fGpu->cmdPool()); |
| 194 | fActiveCommandBuffers.push_back(cmdBuffer); |
| 195 | cmdBuffer->ref(); |
| 196 | return cmdBuffer; |
| 197 | } |
| 198 | |
| 199 | void GrVkResourceProvider::checkCommandBuffers() { |
| 200 | for (int i = fActiveCommandBuffers.count()-1; i >= 0; --i) { |
| 201 | if (fActiveCommandBuffers[i]->finished(fGpu)) { |
| 202 | fActiveCommandBuffers[i]->unref(fGpu); |
| 203 | fActiveCommandBuffers.removeShuffle(i); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void GrVkResourceProvider::destroyResources() { |
| 209 | // release our current command buffers |
| 210 | for (int i = 0; i < fActiveCommandBuffers.count(); ++i) { |
| 211 | SkASSERT(fActiveCommandBuffers[i]->finished(fGpu)); |
| 212 | SkASSERT(fActiveCommandBuffers[i]->unique()); |
| 213 | fActiveCommandBuffers[i]->unref(fGpu); |
| 214 | } |
| 215 | fActiveCommandBuffers.reset(); |
| 216 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 217 | // loop over all render pass sets to make sure we destroy all the internal VkRenderPasses |
| 218 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
| 219 | fRenderPassArray[i].releaseResources(fGpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 220 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 221 | fRenderPassArray.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 222 | |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 223 | // Iterate through all store GrVkSamplers and unref them before resetting the hash. |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 224 | SkTDynamicHash<GrVkSampler, uint16_t>::Iter iter(&fSamplers); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 225 | for (; !iter.done(); ++iter) { |
| 226 | (*iter).unref(fGpu); |
| 227 | } |
| 228 | fSamplers.reset(); |
| 229 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 230 | fPipelineStateCache->release(); |
| 231 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 232 | #ifdef SK_TRACE_VK_RESOURCES |
| 233 | SkASSERT(0 == GrVkResource::fTrace.count()); |
| 234 | #endif |
| 235 | |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 236 | GR_VK_CALL(fGpu->vkInterface(), DestroyPipelineCache(fGpu->device(), fPipelineCache, nullptr)); |
| 237 | fPipelineCache = VK_NULL_HANDLE; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 238 | |
| 239 | if (fUniformDescLayout) { |
| 240 | GR_VK_CALL(fGpu->vkInterface(), DestroyDescriptorSetLayout(fGpu->device(), |
| 241 | fUniformDescLayout, |
| 242 | nullptr)); |
| 243 | fUniformDescLayout = VK_NULL_HANDLE; |
| 244 | } |
| 245 | fUniformDescPool->unref(fGpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void GrVkResourceProvider::abandonResources() { |
| 249 | // release our current command buffers |
| 250 | for (int i = 0; i < fActiveCommandBuffers.count(); ++i) { |
| 251 | SkASSERT(fActiveCommandBuffers[i]->finished(fGpu)); |
| 252 | fActiveCommandBuffers[i]->unrefAndAbandon(); |
| 253 | } |
| 254 | fActiveCommandBuffers.reset(); |
| 255 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 256 | // loop over all render pass sets to make sure we destroy all the internal VkRenderPasses |
| 257 | for (int i = 0; i < fRenderPassArray.count(); ++i) { |
| 258 | fRenderPassArray[i].abandonResources(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 259 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 260 | fRenderPassArray.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 261 | |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 262 | // Iterate through all store GrVkSamplers and unrefAndAbandon them before resetting the hash. |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 263 | SkTDynamicHash<GrVkSampler, uint16_t>::Iter iter(&fSamplers); |
egdaniel | 8b6394c | 2016-03-04 07:35:10 -0800 | [diff] [blame] | 264 | for (; !iter.done(); ++iter) { |
| 265 | (*iter).unrefAndAbandon(); |
| 266 | } |
| 267 | fSamplers.reset(); |
| 268 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 269 | fPipelineStateCache->abandon(); |
| 270 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 271 | #ifdef SK_TRACE_VK_RESOURCES |
| 272 | SkASSERT(0 == GrVkResource::fTrace.count()); |
| 273 | #endif |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 274 | fPipelineCache = VK_NULL_HANDLE; |
egdaniel | 778555c | 2016-05-02 06:50:36 -0700 | [diff] [blame] | 275 | |
| 276 | fUniformDescLayout = VK_NULL_HANDLE; |
| 277 | fUniformDescPool->unrefAndAbandon(); |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 278 | } |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 279 | |
| 280 | //////////////////////////////////////////////////////////////////////////////// |
| 281 | |
| 282 | GrVkResourceProvider::CompatibleRenderPassSet::CompatibleRenderPassSet( |
| 283 | const GrVkGpu* gpu, |
| 284 | const GrVkRenderTarget& target) |
| 285 | : fLastReturnedIndex(0) { |
| 286 | fRenderPasses.emplace_back(new GrVkRenderPass()); |
| 287 | fRenderPasses[0]->initSimple(gpu, target); |
| 288 | } |
| 289 | |
| 290 | bool GrVkResourceProvider::CompatibleRenderPassSet::isCompatible( |
| 291 | const GrVkRenderTarget& target) const { |
| 292 | // The first GrVkRenderpass should always exists since we create the basic load store |
| 293 | // render pass on create |
| 294 | SkASSERT(fRenderPasses[0]); |
| 295 | return fRenderPasses[0]->isCompatible(target); |
| 296 | } |
| 297 | |
| 298 | void GrVkResourceProvider::CompatibleRenderPassSet::releaseResources(const GrVkGpu* gpu) { |
| 299 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 300 | if (fRenderPasses[i]) { |
| 301 | fRenderPasses[i]->unref(gpu); |
| 302 | fRenderPasses[i] = nullptr; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void GrVkResourceProvider::CompatibleRenderPassSet::abandonResources() { |
| 308 | for (int i = 0; i < fRenderPasses.count(); ++i) { |
| 309 | if (fRenderPasses[i]) { |
| 310 | fRenderPasses[i]->unrefAndAbandon(); |
| 311 | fRenderPasses[i] = nullptr; |
| 312 | } |
| 313 | } |
| 314 | } |