egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkDescriptorSetManager.h" |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/vk/GrVkDescriptorPool.h" |
| 11 | #include "src/gpu/vk/GrVkDescriptorSet.h" |
| 12 | #include "src/gpu/vk/GrVkGpu.h" |
| 13 | #include "src/gpu/vk/GrVkUniformHandler.h" |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 14 | |
Ben Wagner | 6c30e74 | 2019-02-06 10:46:14 -0500 | [diff] [blame] | 15 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 16 | #include <sanitizer/lsan_interface.h> |
| 17 | #endif |
| 18 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 19 | GrVkDescriptorSetManager* GrVkDescriptorSetManager::CreateUniformManager(GrVkGpu* gpu) { |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 20 | SkSTArray<1, uint32_t> visibilities; |
| 21 | uint32_t stages = kVertex_GrShaderFlag | kFragment_GrShaderFlag; |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 22 | if (gpu->vkCaps().shaderCaps()->geometryShaderSupport()) { |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 23 | stages |= kGeometry_GrShaderFlag; |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 24 | } |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 25 | visibilities.push_back(stages); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 26 | SkTArray<const GrVkSampler*> samplers; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 27 | return Create(gpu, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, visibilities, samplers); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | GrVkDescriptorSetManager* GrVkDescriptorSetManager::CreateSamplerManager( |
| 31 | GrVkGpu* gpu, VkDescriptorType type, const GrVkUniformHandler& uniformHandler) { |
| 32 | SkSTArray<4, uint32_t> visibilities; |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 33 | SkSTArray<4, const GrVkSampler*> immutableSamplers; |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 34 | SkASSERT(type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 35 | for (int i = 0 ; i < uniformHandler.numSamplers(); ++i) { |
| 36 | visibilities.push_back(uniformHandler.samplerVisibility(i)); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 37 | immutableSamplers.push_back(uniformHandler.immutableSampler(i)); |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 38 | } |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 39 | return Create(gpu, type, visibilities, immutableSamplers); |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 42 | GrVkDescriptorSetManager* GrVkDescriptorSetManager::CreateZeroSamplerManager(GrVkGpu* gpu) { |
| 43 | SkTArray<uint32_t> visibilities; |
| 44 | SkTArray<const GrVkSampler*> immutableSamplers; |
| 45 | return Create(gpu, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, visibilities, immutableSamplers); |
| 46 | } |
| 47 | |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 48 | GrVkDescriptorSetManager* GrVkDescriptorSetManager::CreateInputManager(GrVkGpu* gpu) { |
| 49 | SkSTArray<1, uint32_t> visibilities; |
| 50 | visibilities.push_back(kFragment_GrShaderFlag); |
| 51 | SkTArray<const GrVkSampler*> samplers; |
| 52 | return Create(gpu, VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, visibilities, samplers); |
| 53 | } |
| 54 | |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 55 | VkShaderStageFlags visibility_to_vk_stage_flags(uint32_t visibility) { |
| 56 | VkShaderStageFlags flags = 0; |
| 57 | |
| 58 | if (visibility & kVertex_GrShaderFlag) { |
| 59 | flags |= VK_SHADER_STAGE_VERTEX_BIT; |
| 60 | } |
| 61 | if (visibility & kGeometry_GrShaderFlag) { |
| 62 | flags |= VK_SHADER_STAGE_GEOMETRY_BIT; |
| 63 | } |
| 64 | if (visibility & kFragment_GrShaderFlag) { |
| 65 | flags |= VK_SHADER_STAGE_FRAGMENT_BIT; |
| 66 | } |
| 67 | return flags; |
| 68 | } |
| 69 | |
| 70 | static bool get_layout_and_desc_count(GrVkGpu* gpu, |
| 71 | VkDescriptorType type, |
| 72 | const SkTArray<uint32_t>& visibilities, |
| 73 | const SkTArray<const GrVkSampler*>& immutableSamplers, |
| 74 | VkDescriptorSetLayout* descSetLayout, |
| 75 | uint32_t* descCountPerSet) { |
| 76 | if (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type || |
| 77 | VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER == type) { |
| 78 | uint32_t numBindings = visibilities.count(); |
| 79 | std::unique_ptr<VkDescriptorSetLayoutBinding[]> dsSamplerBindings( |
| 80 | new VkDescriptorSetLayoutBinding[numBindings]); |
Greg Daniel | bee1b55 | 2021-06-28 17:47:14 -0400 | [diff] [blame^] | 81 | *descCountPerSet = 0; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 82 | for (uint32_t i = 0; i < numBindings; ++i) { |
| 83 | uint32_t visibility = visibilities[i]; |
| 84 | dsSamplerBindings[i].binding = i; |
| 85 | dsSamplerBindings[i].descriptorType = type; |
| 86 | dsSamplerBindings[i].descriptorCount = 1; |
| 87 | dsSamplerBindings[i].stageFlags = visibility_to_vk_stage_flags(visibility); |
| 88 | if (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER == type) { |
| 89 | if (immutableSamplers[i]) { |
Greg Daniel | bee1b55 | 2021-06-28 17:47:14 -0400 | [diff] [blame^] | 90 | (*descCountPerSet) += gpu->vkCaps().ycbcrCombinedImageSamplerDescriptorCount(); |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 91 | dsSamplerBindings[i].pImmutableSamplers = immutableSamplers[i]->samplerPtr(); |
| 92 | } else { |
Greg Daniel | bee1b55 | 2021-06-28 17:47:14 -0400 | [diff] [blame^] | 93 | (*descCountPerSet)++; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 94 | dsSamplerBindings[i].pImmutableSamplers = nullptr; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | VkDescriptorSetLayoutCreateInfo dsSamplerLayoutCreateInfo; |
| 100 | memset(&dsSamplerLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreateInfo)); |
| 101 | dsSamplerLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 102 | dsSamplerLayoutCreateInfo.pNext = nullptr; |
| 103 | dsSamplerLayoutCreateInfo.flags = 0; |
| 104 | dsSamplerLayoutCreateInfo.bindingCount = numBindings; |
| 105 | // Setting to nullptr fixes an error in the param checker validation layer. Even though |
| 106 | // bindingCount is 0 (which is valid), it still tries to validate pBindings unless it is |
| 107 | // null. |
| 108 | dsSamplerLayoutCreateInfo.pBindings = numBindings ? dsSamplerBindings.get() : nullptr; |
| 109 | |
| 110 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 111 | // skia:8713 |
| 112 | __lsan::ScopedDisabler lsanDisabler; |
| 113 | #endif |
| 114 | VkResult result; |
| 115 | GR_VK_CALL_RESULT(gpu, result, |
| 116 | CreateDescriptorSetLayout(gpu->device(), |
| 117 | &dsSamplerLayoutCreateInfo, |
| 118 | nullptr, |
| 119 | descSetLayout)); |
| 120 | if (result != VK_SUCCESS) { |
| 121 | return false; |
| 122 | } |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 123 | } else if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) { |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 124 | static constexpr int kUniformDescPerSet = 1; |
| 125 | SkASSERT(kUniformDescPerSet == visibilities.count()); |
| 126 | // Create Uniform Buffer Descriptor |
| 127 | VkDescriptorSetLayoutBinding dsUniBinding; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 128 | dsUniBinding.binding = GrVkUniformHandler::kUniformBinding; |
| 129 | dsUniBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 130 | dsUniBinding.descriptorCount = 1; |
| 131 | dsUniBinding.stageFlags = visibility_to_vk_stage_flags(visibilities[0]); |
| 132 | dsUniBinding.pImmutableSamplers = nullptr; |
| 133 | |
| 134 | VkDescriptorSetLayoutCreateInfo uniformLayoutCreateInfo; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 135 | uniformLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 136 | uniformLayoutCreateInfo.pNext = nullptr; |
| 137 | uniformLayoutCreateInfo.flags = 0; |
| 138 | uniformLayoutCreateInfo.bindingCount = 1; |
| 139 | uniformLayoutCreateInfo.pBindings = &dsUniBinding; |
| 140 | |
| 141 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 142 | // skia:8713 |
| 143 | __lsan::ScopedDisabler lsanDisabler; |
| 144 | #endif |
| 145 | VkResult result; |
| 146 | GR_VK_CALL_RESULT(gpu, result, CreateDescriptorSetLayout(gpu->device(), |
| 147 | &uniformLayoutCreateInfo, |
| 148 | nullptr, |
| 149 | descSetLayout)); |
| 150 | if (result != VK_SUCCESS) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | *descCountPerSet = kUniformDescPerSet; |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 155 | } else { |
| 156 | SkASSERT(type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 157 | static constexpr int kInputDescPerSet = 1; |
| 158 | SkASSERT(kInputDescPerSet == visibilities.count()); |
| 159 | |
| 160 | // Create Input Buffer Descriptor |
| 161 | VkDescriptorSetLayoutBinding dsInpuBinding; |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 162 | dsInpuBinding.binding = 0; |
| 163 | dsInpuBinding.descriptorType = VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT; |
| 164 | dsInpuBinding.descriptorCount = 1; |
| 165 | SkASSERT(visibilities[0] == kFragment_GrShaderFlag); |
| 166 | dsInpuBinding.stageFlags = visibility_to_vk_stage_flags(visibilities[0]); |
| 167 | dsInpuBinding.pImmutableSamplers = nullptr; |
| 168 | |
Greg Daniel | 37fd658 | 2020-09-14 12:36:09 -0400 | [diff] [blame] | 169 | VkDescriptorSetLayoutCreateInfo inputLayoutCreateInfo; |
| 170 | inputLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 171 | inputLayoutCreateInfo.pNext = nullptr; |
| 172 | inputLayoutCreateInfo.flags = 0; |
| 173 | inputLayoutCreateInfo.bindingCount = 1; |
| 174 | inputLayoutCreateInfo.pBindings = &dsInpuBinding; |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 175 | |
| 176 | #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS) |
| 177 | // skia:8713 |
| 178 | __lsan::ScopedDisabler lsanDisabler; |
| 179 | #endif |
| 180 | VkResult result; |
Greg Daniel | 37fd658 | 2020-09-14 12:36:09 -0400 | [diff] [blame] | 181 | GR_VK_CALL_RESULT(gpu, result, CreateDescriptorSetLayout(gpu->device(), |
| 182 | &inputLayoutCreateInfo, |
| 183 | nullptr, descSetLayout)); |
Greg Daniel | f32fec1 | 2020-09-08 13:05:32 -0400 | [diff] [blame] | 184 | if (result != VK_SUCCESS) { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | *descCountPerSet = kInputDescPerSet; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | GrVkDescriptorSetManager* GrVkDescriptorSetManager::Create( |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 194 | GrVkGpu* gpu, VkDescriptorType type, |
| 195 | const SkTArray<uint32_t>& visibilities, |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 196 | const SkTArray<const GrVkSampler*>& immutableSamplers) { |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 197 | #ifdef SK_DEBUG |
| 198 | if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 199 | SkASSERT(visibilities.count() == immutableSamplers.count()); |
| 200 | } else { |
| 201 | SkASSERT(immutableSamplers.count() == 0); |
| 202 | } |
| 203 | #endif |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 204 | |
| 205 | VkDescriptorSetLayout descSetLayout; |
| 206 | uint32_t descCountPerSet; |
| 207 | if (!get_layout_and_desc_count(gpu, type, visibilities, immutableSamplers, &descSetLayout, |
| 208 | &descCountPerSet)) { |
| 209 | return nullptr; |
| 210 | } |
| 211 | return new GrVkDescriptorSetManager(gpu, type, descSetLayout, descCountPerSet, visibilities, |
| 212 | immutableSamplers); |
| 213 | } |
| 214 | |
| 215 | GrVkDescriptorSetManager::GrVkDescriptorSetManager( |
| 216 | GrVkGpu* gpu, VkDescriptorType type, VkDescriptorSetLayout descSetLayout, |
| 217 | uint32_t descCountPerSet, const SkTArray<uint32_t>& visibilities, |
| 218 | const SkTArray<const GrVkSampler*>& immutableSamplers) |
| 219 | : fPoolManager(descSetLayout, type, descCountPerSet) { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 220 | for (int i = 0; i < visibilities.count(); ++i) { |
| 221 | fBindingVisibilities.push_back(visibilities[i]); |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 222 | } |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 223 | for (int i = 0; i < immutableSamplers.count(); ++i) { |
| 224 | const GrVkSampler* sampler = immutableSamplers[i]; |
| 225 | if (sampler) { |
| 226 | sampler->ref(); |
| 227 | } |
| 228 | fImmutableSamplers.push_back(sampler); |
| 229 | } |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 230 | } |
| 231 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 232 | const GrVkDescriptorSet* GrVkDescriptorSetManager::getDescriptorSet(GrVkGpu* gpu, |
| 233 | const Handle& handle) { |
| 234 | const GrVkDescriptorSet* ds = nullptr; |
| 235 | int count = fFreeSets.count(); |
| 236 | if (count > 0) { |
| 237 | ds = fFreeSets[count - 1]; |
| 238 | fFreeSets.removeShuffle(count - 1); |
| 239 | } else { |
| 240 | VkDescriptorSet vkDS; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 241 | if (!fPoolManager.getNewDescriptorSet(gpu, &vkDS)) { |
| 242 | return nullptr; |
| 243 | } |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 244 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 245 | ds = new GrVkDescriptorSet(gpu, vkDS, fPoolManager.fPool, handle); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 246 | } |
| 247 | SkASSERT(ds); |
| 248 | return ds; |
| 249 | } |
| 250 | |
| 251 | void GrVkDescriptorSetManager::recycleDescriptorSet(const GrVkDescriptorSet* descSet) { |
| 252 | SkASSERT(descSet); |
| 253 | fFreeSets.push_back(descSet); |
| 254 | } |
| 255 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 256 | void GrVkDescriptorSetManager::release(GrVkGpu* gpu) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 257 | fPoolManager.freeGPUResources(gpu); |
| 258 | |
| 259 | for (int i = 0; i < fFreeSets.count(); ++i) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 260 | fFreeSets[i]->unref(); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 261 | } |
| 262 | fFreeSets.reset(); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 263 | |
| 264 | for (int i = 0; i < fImmutableSamplers.count(); ++i) { |
| 265 | if (fImmutableSamplers[i]) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 266 | fImmutableSamplers[i]->unref(); |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | fImmutableSamplers.reset(); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 270 | } |
| 271 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 272 | bool GrVkDescriptorSetManager::isCompatible(VkDescriptorType type, |
| 273 | const GrVkUniformHandler* uniHandler) const { |
| 274 | SkASSERT(uniHandler); |
| 275 | if (type != fPoolManager.fDescType) { |
| 276 | return false; |
| 277 | } |
| 278 | |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 279 | SkASSERT(type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 280 | if (fBindingVisibilities.count() != uniHandler->numSamplers()) { |
| 281 | return false; |
| 282 | } |
| 283 | for (int i = 0; i < uniHandler->numSamplers(); ++i) { |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 284 | if (uniHandler->samplerVisibility(i) != fBindingVisibilities[i] || |
| 285 | uniHandler->immutableSampler(i) != fImmutableSamplers[i]) { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 286 | return false; |
| 287 | } |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
Greg Daniel | a8c3210 | 2020-12-30 15:09:32 -0500 | [diff] [blame] | 292 | bool GrVkDescriptorSetManager::isZeroSampler() const { |
| 293 | if (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER != fPoolManager.fDescType) { |
| 294 | return false; |
| 295 | } |
| 296 | if (fBindingVisibilities.count()) { |
| 297 | return false; |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 302 | //////////////////////////////////////////////////////////////////////////////// |
| 303 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 304 | GrVkDescriptorSetManager::DescriptorPoolManager::DescriptorPoolManager( |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 305 | VkDescriptorSetLayout layout, |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 306 | VkDescriptorType type, |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 307 | uint32_t descCountPerSet) |
| 308 | : fDescLayout(layout) |
| 309 | , fDescType(type) |
| 310 | , fDescCountPerSet(descCountPerSet) |
| 311 | , fMaxDescriptors(kStartNumDescriptors) |
egdaniel | 4d866df | 2016-08-25 13:52:00 -0700 | [diff] [blame] | 312 | , fCurrentDescriptorCount(0) |
| 313 | , fPool(nullptr) { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 316 | bool GrVkDescriptorSetManager::DescriptorPoolManager::getNewPool(GrVkGpu* gpu) { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 317 | if (fPool) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 318 | fPool->unref(); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 319 | uint32_t newPoolSize = fMaxDescriptors + ((fMaxDescriptors + 1) >> 1); |
| 320 | if (newPoolSize < kMaxDescriptors) { |
| 321 | fMaxDescriptors = newPoolSize; |
| 322 | } else { |
| 323 | fMaxDescriptors = kMaxDescriptors; |
| 324 | } |
| 325 | |
| 326 | } |
| 327 | fPool = gpu->resourceProvider().findOrCreateCompatibleDescriptorPool(fDescType, |
| 328 | fMaxDescriptors); |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 329 | return SkToBool(fPool); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 332 | bool GrVkDescriptorSetManager::DescriptorPoolManager::getNewDescriptorSet(GrVkGpu* gpu, |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 333 | VkDescriptorSet* ds) { |
| 334 | if (!fMaxDescriptors) { |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 335 | return false; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 336 | } |
| 337 | fCurrentDescriptorCount += fDescCountPerSet; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 338 | if (!fPool || fCurrentDescriptorCount > fMaxDescriptors) { |
| 339 | if (!this->getNewPool(gpu) ) { |
| 340 | return false; |
| 341 | } |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 342 | fCurrentDescriptorCount = fDescCountPerSet; |
| 343 | } |
| 344 | |
| 345 | VkDescriptorSetAllocateInfo dsAllocateInfo; |
| 346 | memset(&dsAllocateInfo, 0, sizeof(VkDescriptorSetAllocateInfo)); |
| 347 | dsAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 348 | dsAllocateInfo.pNext = nullptr; |
| 349 | dsAllocateInfo.descriptorPool = fPool->descPool(); |
| 350 | dsAllocateInfo.descriptorSetCount = 1; |
| 351 | dsAllocateInfo.pSetLayouts = &fDescLayout; |
Greg Daniel | 9b63dc8 | 2019-11-06 09:21:55 -0500 | [diff] [blame] | 352 | VkResult result; |
| 353 | GR_VK_CALL_RESULT(gpu, result, AllocateDescriptorSets(gpu->device(), |
| 354 | &dsAllocateInfo, |
| 355 | ds)); |
| 356 | return result == VK_SUCCESS; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 359 | void GrVkDescriptorSetManager::DescriptorPoolManager::freeGPUResources(GrVkGpu* gpu) { |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 360 | if (fDescLayout) { |
| 361 | GR_VK_CALL(gpu->vkInterface(), DestroyDescriptorSetLayout(gpu->device(), fDescLayout, |
| 362 | nullptr)); |
| 363 | fDescLayout = VK_NULL_HANDLE; |
| 364 | } |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 365 | |
| 366 | if (fPool) { |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 367 | fPool->unref(); |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 368 | fPool = nullptr; |
| 369 | } |
| 370 | } |
| 371 | |