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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 } | 24 } |
25 } else { | 25 } else { |
26 SkASSERT(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); | 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 | 27 // We set the visibility of the first binding to the vertex shader and t
he second to the |
28 // fragment shader. | 28 // fragment shader. |
29 fBindingVisibilities.push_back(kVertex_GrShaderFlag); | 29 fBindingVisibilities.push_back(kVertex_GrShaderFlag); |
30 fBindingVisibilities.push_back(kFragment_GrShaderFlag); | 30 fBindingVisibilities.push_back(kFragment_GrShaderFlag); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
| 34 GrVkDescriptorSetManager::GrVkDescriptorSetManager(GrVkGpu* gpu, |
| 35 VkDescriptorType type, |
| 36 const SkTArray<uint32_t>& vis
ibilities) |
| 37 : fPoolManager(type, gpu, visibilities) { |
| 38 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 39 for (int i = 0; i < visibilities.count(); ++i) { |
| 40 fBindingVisibilities.push_back(visibilities[i]); |
| 41 } |
| 42 } else { |
| 43 SkASSERT(type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 44 SkASSERT(2 == visibilities.count() && |
| 45 kVertex_GrShaderFlag == visibilities[0] && |
| 46 kFragment_GrShaderFlag == visibilities[1]); |
| 47 // We set the visibility of the first binding to the vertex shader and t
he second to the |
| 48 // fragment shader. |
| 49 fBindingVisibilities.push_back(kVertex_GrShaderFlag); |
| 50 fBindingVisibilities.push_back(kFragment_GrShaderFlag); |
| 51 } |
| 52 } |
| 53 |
34 const GrVkDescriptorSet* GrVkDescriptorSetManager::getDescriptorSet(GrVkGpu* gpu
, | 54 const GrVkDescriptorSet* GrVkDescriptorSetManager::getDescriptorSet(GrVkGpu* gpu
, |
35 const Handle
& handle) { | 55 const Handle
& handle) { |
36 const GrVkDescriptorSet* ds = nullptr; | 56 const GrVkDescriptorSet* ds = nullptr; |
37 int count = fFreeSets.count(); | 57 int count = fFreeSets.count(); |
38 if (count > 0) { | 58 if (count > 0) { |
39 ds = fFreeSets[count - 1]; | 59 ds = fFreeSets[count - 1]; |
40 fFreeSets.removeShuffle(count - 1); | 60 fFreeSets.removeShuffle(count - 1); |
41 } else { | 61 } else { |
42 VkDescriptorSet vkDS; | 62 VkDescriptorSet vkDS; |
43 fPoolManager.getNewDescriptorSet(gpu, &vkDS); | 63 fPoolManager.getNewDescriptorSet(gpu, &vkDS); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 104 } |
85 for (int i = 0; i < uniHandler->numSamplers(); ++i) { | 105 for (int i = 0; i < uniHandler->numSamplers(); ++i) { |
86 if (uniHandler->getSampler(i).visibility() != fBindingVisibilities[i
]) { | 106 if (uniHandler->getSampler(i).visibility() != fBindingVisibilities[i
]) { |
87 return false; | 107 return false; |
88 } | 108 } |
89 } | 109 } |
90 } | 110 } |
91 return true; | 111 return true; |
92 } | 112 } |
93 | 113 |
| 114 bool GrVkDescriptorSetManager::isCompatible(VkDescriptorType type, |
| 115 const SkTArray<uint32_t>& visibiliti
es) const { |
| 116 if (type != fPoolManager.fDescType) { |
| 117 return false; |
| 118 } |
| 119 |
| 120 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
| 121 if (fBindingVisibilities.count() != visibilities.count()) { |
| 122 return false; |
| 123 } |
| 124 for (int i = 0; i < visibilities.count(); ++i) { |
| 125 if (visibilities[i] != fBindingVisibilities[i]) { |
| 126 return false; |
| 127 } |
| 128 } |
| 129 } |
| 130 return true; |
| 131 } |
| 132 |
94 //////////////////////////////////////////////////////////////////////////////// | 133 //////////////////////////////////////////////////////////////////////////////// |
95 | 134 |
96 VkShaderStageFlags visibility_to_vk_stage_flags(uint32_t visibility) { | 135 VkShaderStageFlags visibility_to_vk_stage_flags(uint32_t visibility) { |
97 VkShaderStageFlags flags = 0; | 136 VkShaderStageFlags flags = 0; |
98 | 137 |
99 if (visibility & kVertex_GrShaderFlag) { | 138 if (visibility & kVertex_GrShaderFlag) { |
100 flags |= VK_SHADER_STAGE_VERTEX_BIT; | 139 flags |= VK_SHADER_STAGE_VERTEX_BIT; |
101 } | 140 } |
102 if (visibility & kGeometry_GrShaderFlag) { | 141 if (visibility & kGeometry_GrShaderFlag) { |
103 flags |= VK_SHADER_STAGE_GEOMETRY_BIT; | 142 flags |= VK_SHADER_STAGE_GEOMETRY_BIT; |
104 } | 143 } |
105 if (visibility & kFragment_GrShaderFlag) { | 144 if (visibility & kFragment_GrShaderFlag) { |
106 flags |= VK_SHADER_STAGE_FRAGMENT_BIT; | 145 flags |= VK_SHADER_STAGE_FRAGMENT_BIT; |
107 } | 146 } |
108 return flags; | 147 return flags; |
109 } | 148 } |
110 | 149 |
111 GrVkDescriptorSetManager::DescriptorPoolManager::DescriptorPoolManager( | 150 GrVkDescriptorSetManager::DescriptorPoolManager::DescriptorPoolManager( |
112 VkDescriptorType type, | 151 VkDescriptorType type, |
113 GrVkGpu* gpu, | 152 GrVkGpu* gpu, |
114 const GrVkUniformHandler* uniformHandler) | 153 const GrVkUniformHandler* uniformHandler) |
115 : fDescType(type) | 154 : fDescType(type) |
116 , fCurrentDescriptorCount(0) | 155 , fCurrentDescriptorCount(0) |
117 , fPool(nullptr) { | 156 , fPool(nullptr) { |
| 157 this->init(gpu, type, uniformHandler, nullptr); |
| 158 } |
| 159 |
| 160 GrVkDescriptorSetManager::DescriptorPoolManager::DescriptorPoolManager( |
| 161 VkDescriptorType type, |
| 162 GrVkGpu* gpu, |
| 163 const SkTArray<uint32_t>& visibilities) |
| 164 : fDescType(type) |
| 165 , fCurrentDescriptorCount(0) |
| 166 , fPool(nullptr) { |
| 167 this->init(gpu, type, nullptr, &visibilities); |
| 168 } |
| 169 |
| 170 void GrVkDescriptorSetManager::DescriptorPoolManager::init(GrVkGpu* gpu, |
| 171 VkDescriptorType type
, |
| 172 const GrVkUniformHand
ler* uniformHandler, |
| 173 const SkTArray<uint32
_t>* visibilities) { |
118 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { | 174 if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) { |
119 SkASSERT(uniformHandler); | 175 SkASSERT(SkToBool(uniformHandler) != SkToBool(visibilities)); |
120 uint32_t numSamplers = (uint32_t)uniformHandler->numSamplers(); | 176 uint32_t numSamplers; |
| 177 if (uniformHandler) { |
| 178 numSamplers = (uint32_t)uniformHandler->numSamplers(); |
| 179 } else { |
| 180 numSamplers = (uint32_t)visibilities->count(); |
| 181 } |
121 | 182 |
122 SkAutoTDeleteArray<VkDescriptorSetLayoutBinding> dsSamplerBindings( | 183 SkAutoTDeleteArray<VkDescriptorSetLayoutBinding> dsSamplerBindings( |
123 new VkDescriptorSetLayoutBinding[numSamplers]); | 184 new VkDescriptorSetLayoutBinding[numSamplers]); |
124 for (uint32_t i = 0; i < numSamplers; ++i) { | 185 for (uint32_t i = 0; i < numSamplers; ++i) { |
125 const GrVkGLSLSampler& sampler = | 186 uint32_t visibility; |
| 187 if (uniformHandler) { |
| 188 const GrVkGLSLSampler& sampler = |
126 static_cast<const GrVkGLSLSampler&>(uniformHandler->getSampl
er(i)); | 189 static_cast<const GrVkGLSLSampler&>(uniformHandler->getSampl
er(i)); |
127 SkASSERT(sampler.binding() == i); | 190 SkASSERT(sampler.binding() == i); |
128 dsSamplerBindings[i].binding = sampler.binding(); | 191 visibility = sampler.visibility(); |
| 192 } else { |
| 193 visibility = (*visibilities)[i]; |
| 194 } |
| 195 dsSamplerBindings[i].binding = i; |
129 dsSamplerBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IM
AGE_SAMPLER; | 196 dsSamplerBindings[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IM
AGE_SAMPLER; |
130 dsSamplerBindings[i].descriptorCount = 1; | 197 dsSamplerBindings[i].descriptorCount = 1; |
131 dsSamplerBindings[i].stageFlags = visibility_to_vk_stage_flags(sampl
er.visibility()); | 198 dsSamplerBindings[i].stageFlags = visibility_to_vk_stage_flags(visib
ility); |
132 dsSamplerBindings[i].pImmutableSamplers = nullptr; | 199 dsSamplerBindings[i].pImmutableSamplers = nullptr; |
133 } | 200 } |
134 | 201 |
135 VkDescriptorSetLayoutCreateInfo dsSamplerLayoutCreateInfo; | 202 VkDescriptorSetLayoutCreateInfo dsSamplerLayoutCreateInfo; |
136 memset(&dsSamplerLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreate
Info)); | 203 memset(&dsSamplerLayoutCreateInfo, 0, sizeof(VkDescriptorSetLayoutCreate
Info)); |
137 dsSamplerLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOU
T_CREATE_INFO; | 204 dsSamplerLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOU
T_CREATE_INFO; |
138 dsSamplerLayoutCreateInfo.pNext = nullptr; | 205 dsSamplerLayoutCreateInfo.pNext = nullptr; |
139 dsSamplerLayoutCreateInfo.flags = 0; | 206 dsSamplerLayoutCreateInfo.flags = 0; |
140 dsSamplerLayoutCreateInfo.bindingCount = numSamplers; | 207 dsSamplerLayoutCreateInfo.bindingCount = numSamplers; |
141 // Setting to nullptr fixes an error in the param checker validation lay
er. Even though | 208 // Setting to nullptr fixes an error in the param checker validation lay
er. Even though |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 } | 306 } |
240 } | 307 } |
241 | 308 |
242 void GrVkDescriptorSetManager::DescriptorPoolManager::abandonGPUResources() { | 309 void GrVkDescriptorSetManager::DescriptorPoolManager::abandonGPUResources() { |
243 fDescLayout = VK_NULL_HANDLE; | 310 fDescLayout = VK_NULL_HANDLE; |
244 if (fPool) { | 311 if (fPool) { |
245 fPool->unrefAndAbandon(); | 312 fPool->unrefAndAbandon(); |
246 fPool = nullptr; | 313 fPool = nullptr; |
247 } | 314 } |
248 } | 315 } |
OLD | NEW |