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