OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrVkDescriptorSetManager.h" | 8 #include "GrVkDescriptorSetManager.h" |
9 | 9 |
10 #include "GrVkDescriptorPool.h" | 10 #include "GrVkDescriptorPool.h" |
11 #include "GrVkDescriptorSet.h" | 11 #include "GrVkDescriptorSet.h" |
12 #include "GrVkGpu.h" | 12 #include "GrVkGpu.h" |
| 13 #include "GrVkUniformHandler.h" |
| 14 #include "glsl/GrGLSLSampler.h" |
13 | 15 |
14 GrVkDescriptorSetManager::GrVkDescriptorSetManager(GrVkGpu* gpu, | 16 GrVkDescriptorSetManager::GrVkDescriptorSetManager(GrVkGpu* gpu, |
15 VkDescriptorSetLayout layout, | |
16 VkDescriptorType type, | 17 VkDescriptorType type, |
17 uint32_t samplerCount) | 18 const GrVkUniformHandler* uni
formHandler) |
18 : fPoolManager(layout, type, samplerCount, gpu) | 19 : fPoolManager(type, gpu, uniformHandler) { |
19 , fNumSamplerBindings(samplerCount) { | 20 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 21 SkASSERT(uniformHandler); |
| 22 for (int i = 0; i < uniformHandler->numSamplers(); ++i) { |
| 23 fBindingVisibilities.push_back(uniformHandler->getSampler(i).visibil
ity()); |
| 24 } |
| 25 } else { |
| 26 SkASSERT(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 27 // We set the visibility of the first binding to the vertex shader and t
he second to the |
| 28 // fragment shader. |
| 29 fBindingVisibilities.push_back(kVertex_GrShaderFlag); |
| 30 fBindingVisibilities.push_back(kFragment_GrShaderFlag); |
| 31 } |
20 } | 32 } |
21 | 33 |
22 const GrVkDescriptorSet* GrVkDescriptorSetManager::getDescriptorSet(GrVkGpu* gpu
, | 34 const GrVkDescriptorSet* GrVkDescriptorSetManager::getDescriptorSet(GrVkGpu* gpu
, |
23 const Handle
& handle) { | 35 const Handle
& handle) { |
24 const GrVkDescriptorSet* ds = nullptr; | 36 const GrVkDescriptorSet* ds = nullptr; |
25 int count = fFreeSets.count(); | 37 int count = fFreeSets.count(); |
26 if (count > 0) { | 38 if (count > 0) { |
27 ds = fFreeSets[count - 1]; | 39 ds = fFreeSets[count - 1]; |
28 fFreeSets.removeShuffle(count - 1); | 40 fFreeSets.removeShuffle(count - 1); |
29 } else { | 41 } else { |
(...skipping 22 matching lines...) Expand all Loading... |
52 | 64 |
53 void GrVkDescriptorSetManager::abandon() { | 65 void GrVkDescriptorSetManager::abandon() { |
54 fPoolManager.abandonGPUResources(); | 66 fPoolManager.abandonGPUResources(); |
55 | 67 |
56 for (int i = 0; i < fFreeSets.count(); ++i) { | 68 for (int i = 0; i < fFreeSets.count(); ++i) { |
57 fFreeSets[i]->unrefAndAbandon(); | 69 fFreeSets[i]->unrefAndAbandon(); |
58 } | 70 } |
59 fFreeSets.reset(); | 71 fFreeSets.reset(); |
60 } | 72 } |
61 | 73 |
| 74 bool GrVkDescriptorSetManager::isCompatible(VkDescriptorType type, |
| 75 const GrVkUniformHandler* uniHandler
) const { |
| 76 SkASSERT(uniHandler); |
| 77 if (type != fPoolManager.fDescType) { |
| 78 return false; |
| 79 } |
| 80 |
| 81 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 82 if (fBindingVisibilities.count() != uniHandler->numSamplers()) { |
| 83 return false; |
| 84 } |
| 85 for (int i = 0; i < uniHandler->numSamplers(); ++i) { |
| 86 if (uniHandler->getSampler(i).visibility() != fBindingVisibilities[i
]) { |
| 87 return false; |
| 88 } |
| 89 } |
| 90 } |
| 91 return true; |
| 92 } |
| 93 |
62 //////////////////////////////////////////////////////////////////////////////// | 94 //////////////////////////////////////////////////////////////////////////////// |
63 | 95 |
| 96 VkShaderStageFlags visibility_to_vk_stage_flags(uint32_t visibility) { |
| 97 VkShaderStageFlags flags = 0; |
| 98 |
| 99 if (visibility & kVertex_GrShaderFlag) { |
| 100 flags |= VK_SHADER_STAGE_VERTEX_BIT; |
| 101 } |
| 102 if (visibility & kGeometry_GrShaderFlag) { |
| 103 flags |= VK_SHADER_STAGE_GEOMETRY_BIT; |
| 104 } |
| 105 if (visibility & kFragment_GrShaderFlag) { |
| 106 flags |= VK_SHADER_STAGE_FRAGMENT_BIT; |
| 107 } |
| 108 return flags; |
| 109 } |
| 110 |
| 111 GrVkDescriptorSetManager::DescriptorPoolManager::DescriptorPoolManager( |
| 112 VkDescriptorType type, |
| 113 GrVkGpu* gpu, |
| 114 const GrVkUniformHandler* uniformHandler) |
| 115 : fDescType(type) |
| 116 , fCurrentDescriptorCount(0) |
| 117 , fPool(nullptr) { |
| 118 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 119 SkASSERT(uniformHandler); |
| 120 uint32_t numSamplers = (uint32_t)uniformHandler->numSamplers(); |
| 121 |
| 122 SkAutoTDeleteArray<VkDescriptorSetLayoutBinding> dsSamplerBindings( |
| 123 new VkDescriptorSetLayoutBinding[numSamplers]); |
| 124 for (uint32_t i = 0; i < numSamplers; ++i) { |
| 125 const GrVkGLSLSampler& sampler = |
| 126 static_cast<const GrVkGLSLSampler&>(uniformHandler->getSampl
er(i)); |
| 127 SkASSERT(sampler.binding() == i); |
| 128 dsSamplerBindings[i].binding = sampler.binding(); |
| 129 dsSamplerBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IM
AGE_SAMPLER; |
| 130 dsSamplerBindings[i].descriptorCount = 1; |
| 131 dsSamplerBindings[i].stageFlags = visibility_to_vk_stage_flags(sampl
er.visibility()); |
| 132 dsSamplerBindings[i].pImmutableSamplers = nullptr; |
| 133 } |
| 134 |
| 135 VkDescriptorSetLayoutCreateInfo dsSamplerLayoutCreateInfo; |
| 136 memset(&dsSamplerLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreate
Info)); |
| 137 dsSamplerLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOU
T_CREATE_INFO; |
| 138 dsSamplerLayoutCreateInfo.pNext = nullptr; |
| 139 dsSamplerLayoutCreateInfo.flags = 0; |
| 140 dsSamplerLayoutCreateInfo.bindingCount = numSamplers; |
| 141 // Setting to nullptr fixes an error in the param checker validation lay
er. Even though |
| 142 // bindingCount is 0 (which is valid), it still tries to validate pBindi
ngs unless it is |
| 143 // null. |
| 144 dsSamplerLayoutCreateInfo.pBindings = numSamplers ? dsSamplerBindings.ge
t() : nullptr; |
| 145 |
| 146 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), |
| 147 CreateDescriptorSetLayout(gpu->device(), |
| 148 &dsSamplerLayoutCreateInfo
, |
| 149 nullptr, |
| 150 &fDescLayout)); |
| 151 fDescCountPerSet = numSamplers; |
| 152 } else { |
| 153 SkASSERT(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 154 // Create Uniform Buffer Descriptor |
| 155 // The vertex uniform buffer will have binding 0 and the fragment bindin
g 1. |
| 156 VkDescriptorSetLayoutBinding dsUniBindings[kUniformDescPerSet]; |
| 157 memset(&dsUniBindings, 0, 2 * sizeof(VkDescriptorSetLayoutBinding)); |
| 158 dsUniBindings[0].binding = GrVkUniformHandler::kVertexBinding; |
| 159 dsUniBindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 160 dsUniBindings[0].descriptorCount = 1; |
| 161 dsUniBindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; |
| 162 dsUniBindings[0].pImmutableSamplers = nullptr; |
| 163 dsUniBindings[1].binding = GrVkUniformHandler::kFragBinding; |
| 164 dsUniBindings[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 165 dsUniBindings[1].descriptorCount = 1; |
| 166 dsUniBindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; |
| 167 dsUniBindings[1].pImmutableSamplers = nullptr; |
| 168 |
| 169 VkDescriptorSetLayoutCreateInfo uniformLayoutCreateInfo; |
| 170 memset(&uniformLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreateIn
fo)); |
| 171 uniformLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_
CREATE_INFO; |
| 172 uniformLayoutCreateInfo.pNext = nullptr; |
| 173 uniformLayoutCreateInfo.flags = 0; |
| 174 uniformLayoutCreateInfo.bindingCount = 2; |
| 175 uniformLayoutCreateInfo.pBindings = dsUniBindings; |
| 176 |
| 177 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), CreateDescriptorSetLayout(gpu->d
evice(), |
| 178 &unifo
rmLayoutCreateInfo, |
| 179 nullpt
r, |
| 180 &fDesc
Layout)); |
| 181 fDescCountPerSet = kUniformDescPerSet; |
| 182 } |
| 183 |
| 184 SkASSERT(fDescCountPerSet < kStartNumDescriptors); |
| 185 fMaxDescriptors = kStartNumDescriptors; |
| 186 SkASSERT(fMaxDescriptors > 0); |
| 187 this->getNewPool(gpu); |
| 188 } |
| 189 |
64 void GrVkDescriptorSetManager::DescriptorPoolManager::getNewPool(GrVkGpu* gpu) { | 190 void GrVkDescriptorSetManager::DescriptorPoolManager::getNewPool(GrVkGpu* gpu) { |
65 if (fPool) { | 191 if (fPool) { |
66 fPool->unref(gpu); | 192 fPool->unref(gpu); |
67 uint32_t newPoolSize = fMaxDescriptors + ((fMaxDescriptors + 1) >> 1); | 193 uint32_t newPoolSize = fMaxDescriptors + ((fMaxDescriptors + 1) >> 1); |
68 if (newPoolSize < kMaxDescriptors) { | 194 if (newPoolSize < kMaxDescriptors) { |
69 fMaxDescriptors = newPoolSize; | 195 fMaxDescriptors = newPoolSize; |
70 } else { | 196 } else { |
71 fMaxDescriptors = kMaxDescriptors; | 197 fMaxDescriptors = kMaxDescriptors; |
72 } | 198 } |
73 | 199 |
(...skipping 20 matching lines...) Expand all Loading... |
94 dsAllocateInfo.pNext = nullptr; | 220 dsAllocateInfo.pNext = nullptr; |
95 dsAllocateInfo.descriptorPool = fPool->descPool(); | 221 dsAllocateInfo.descriptorPool = fPool->descPool(); |
96 dsAllocateInfo.descriptorSetCount = 1; | 222 dsAllocateInfo.descriptorSetCount = 1; |
97 dsAllocateInfo.pSetLayouts = &fDescLayout; | 223 dsAllocateInfo.pSetLayouts = &fDescLayout; |
98 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), AllocateDescriptorSets(gpu->device()
, | 224 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), AllocateDescriptorSets(gpu->device()
, |
99 &dsAllocateIn
fo, | 225 &dsAllocateIn
fo, |
100 ds)); | 226 ds)); |
101 } | 227 } |
102 | 228 |
103 void GrVkDescriptorSetManager::DescriptorPoolManager::freeGPUResources(const GrV
kGpu* gpu) { | 229 void GrVkDescriptorSetManager::DescriptorPoolManager::freeGPUResources(const GrV
kGpu* gpu) { |
104 // The layout should be owned by the class which owns the DescriptorSetManag
er so it will | 230 if (fDescLayout) { |
105 // take care of destroying it. | 231 GR_VK_CALL(gpu->vkInterface(), DestroyDescriptorSetLayout(gpu->device(),
fDescLayout, |
106 fDescLayout = VK_NULL_HANDLE; | 232 nullptr)); |
| 233 fDescLayout = VK_NULL_HANDLE; |
| 234 } |
107 | 235 |
108 if (fPool) { | 236 if (fPool) { |
109 fPool->unref(gpu); | 237 fPool->unref(gpu); |
110 fPool = nullptr; | 238 fPool = nullptr; |
111 } | 239 } |
112 } | 240 } |
113 | 241 |
114 void GrVkDescriptorSetManager::DescriptorPoolManager::abandonGPUResources() { | 242 void GrVkDescriptorSetManager::DescriptorPoolManager::abandonGPUResources() { |
115 fDescLayout = VK_NULL_HANDLE; | 243 fDescLayout = VK_NULL_HANDLE; |
116 if (fPool) { | 244 if (fPool) { |
117 fPool->unrefAndAbandon(); | 245 fPool->unrefAndAbandon(); |
118 fPool = nullptr; | 246 fPool = nullptr; |
119 } | 247 } |
120 } | 248 } |
OLD | NEW |