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