blob: 065d7808df11803647d602b7c97aacb20260d2f1 [file] [log] [blame]
Olli Etuahob78707c2017-03-09 15:03:11 +00001//
2// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// UniformLinker.cpp: implements link-time checks for default block uniforms, and generates uniform
8// locations. Populates data structures related to uniforms so that they can be stored in program
9// state.
10
11#include "libANGLE/UniformLinker.h"
12
13#include "common/utilities.h"
14#include "libANGLE/Caps.h"
Jamie Madillbd044ed2017-06-05 12:59:21 -040015#include "libANGLE/Context.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000016#include "libANGLE/Shader.h"
Jiawei-Shaoddb5eb52017-03-14 13:36:18 +080017#include "libANGLE/features.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000018
19namespace gl
20{
21
22namespace
23{
24
25LinkedUniform *FindUniform(std::vector<LinkedUniform> &list, const std::string &name)
26{
27 for (LinkedUniform &uniform : list)
28 {
29 if (uniform.name == name)
30 return &uniform;
31 }
32
33 return nullptr;
34}
35
36} // anonymouse namespace
37
38UniformLinker::UniformLinker(const ProgramState &state) : mState(state)
39{
40}
41
42void UniformLinker::getResults(std::vector<LinkedUniform> *uniforms,
43 std::vector<VariableLocation> *uniformLocations)
44{
45 uniforms->swap(mUniforms);
46 uniformLocations->swap(mUniformLocations);
47}
48
Jamie Madillbd044ed2017-06-05 12:59:21 -040049bool UniformLinker::link(const Context *context,
50 InfoLog &infoLog,
Olli Etuahob78707c2017-03-09 15:03:11 +000051 const Program::Bindings &uniformLocationBindings)
52{
53 if (mState.getAttachedVertexShader() && mState.getAttachedFragmentShader())
54 {
55 ASSERT(mState.getAttachedComputeShader() == nullptr);
Jamie Madillbd044ed2017-06-05 12:59:21 -040056 if (!validateVertexAndFragmentUniforms(context, infoLog))
Olli Etuahob78707c2017-03-09 15:03:11 +000057 {
58 return false;
59 }
60 }
61
62 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
63 // Also check the maximum uniform vector and sampler counts.
Jamie Madillbd044ed2017-06-05 12:59:21 -040064 if (!flattenUniformsAndCheckCaps(context, infoLog))
Olli Etuahob78707c2017-03-09 15:03:11 +000065 {
66 return false;
67 }
68
jchen10eaef1e52017-06-13 10:44:11 +080069 if (!checkMaxCombinedAtomicCounters(context->getCaps(), infoLog))
70 {
71 return false;
72 }
73
Olli Etuahob78707c2017-03-09 15:03:11 +000074 if (!indexUniforms(infoLog, uniformLocationBindings))
75 {
76 return false;
77 }
78
79 return true;
80}
81
Jamie Madillbd044ed2017-06-05 12:59:21 -040082bool UniformLinker::validateVertexAndFragmentUniforms(const Context *context,
83 InfoLog &infoLog) const
Olli Etuahob78707c2017-03-09 15:03:11 +000084{
85 // Check that uniforms defined in the vertex and fragment shaders are identical
86 std::map<std::string, LinkedUniform> linkedUniforms;
87 const std::vector<sh::Uniform> &vertexUniforms =
Jamie Madillbd044ed2017-06-05 12:59:21 -040088 mState.getAttachedVertexShader()->getUniforms(context);
Olli Etuahob78707c2017-03-09 15:03:11 +000089 const std::vector<sh::Uniform> &fragmentUniforms =
Jamie Madillbd044ed2017-06-05 12:59:21 -040090 mState.getAttachedFragmentShader()->getUniforms(context);
Olli Etuahob78707c2017-03-09 15:03:11 +000091
92 for (const sh::Uniform &vertexUniform : vertexUniforms)
93 {
94 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
95 }
96
97 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
98 {
99 auto entry = linkedUniforms.find(fragmentUniform.name);
100 if (entry != linkedUniforms.end())
101 {
102 LinkedUniform *linkedUniform = &entry->second;
103 const std::string &uniformName = "uniform '" + linkedUniform->name + "'";
104 if (!linkValidateUniforms(infoLog, uniformName, *linkedUniform, fragmentUniform))
105 {
106 return false;
107 }
108 }
109 }
110 return true;
111}
112
113// GLSL ES Spec 3.00.3, section 4.3.5.
114bool UniformLinker::linkValidateUniforms(InfoLog &infoLog,
115 const std::string &uniformName,
116 const sh::Uniform &vertexUniform,
117 const sh::Uniform &fragmentUniform)
118{
119#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
120 const bool validatePrecision = true;
121#else
122 const bool validatePrecision = false;
123#endif
124
125 if (!Program::linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
126 validatePrecision))
127 {
128 return false;
129 }
130
131 // GLSL ES Spec 3.10.4, section 4.4.5.
132 if (vertexUniform.binding != -1 && fragmentUniform.binding != -1 &&
133 vertexUniform.binding != fragmentUniform.binding)
134 {
135 infoLog << "Binding layout qualifiers for " << uniformName
136 << " differ between vertex and fragment shaders.";
137 return false;
138 }
139
140 // GLSL ES Spec 3.10.4, section 9.2.1.
141 if (vertexUniform.location != -1 && fragmentUniform.location != -1 &&
142 vertexUniform.location != fragmentUniform.location)
143 {
144 infoLog << "Location layout qualifiers for " << uniformName
145 << " differ between vertex and fragment shaders.";
146 return false;
147 }
jchen10eaef1e52017-06-13 10:44:11 +0800148 if (vertexUniform.offset != fragmentUniform.offset)
149 {
150 infoLog << "Offset layout qualifiers for " << uniformName
151 << " differ between vertex and fragment shaders.";
152 return false;
153 }
Olli Etuahob78707c2017-03-09 15:03:11 +0000154
155 return true;
156}
157
158bool UniformLinker::indexUniforms(InfoLog &infoLog,
159 const Program::Bindings &uniformLocationBindings)
160{
161 // All the locations where another uniform can't be located.
162 std::set<GLuint> reservedLocations;
163 // Locations which have been allocated for an unused uniform.
164 std::set<GLuint> ignoredLocations;
165
166 int maxUniformLocation = -1;
167
168 // Gather uniform locations that have been set either using the bindUniformLocation API or by
169 // using a location layout qualifier and check conflicts between them.
170 if (!gatherUniformLocationsAndCheckConflicts(infoLog, uniformLocationBindings,
171 &reservedLocations, &ignoredLocations,
172 &maxUniformLocation))
173 {
174 return false;
175 }
176
177 // Conflicts have been checked, now we can prune non-statically used uniforms. Code further down
178 // the line relies on only having statically used uniforms in mUniforms.
179 pruneUnusedUniforms();
180
181 // Gather uniforms that have their location pre-set and uniforms that don't yet have a location.
182 std::vector<VariableLocation> unlocatedUniforms;
183 std::map<GLuint, VariableLocation> preLocatedUniforms;
184
185 for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++)
186 {
187 const LinkedUniform &uniform = mUniforms[uniformIndex];
188
189 if (uniform.isBuiltIn())
190 {
191 continue;
192 }
193
194 int preSetLocation = uniformLocationBindings.getBinding(uniform.name);
195 int shaderLocation = uniform.location;
196
197 if (shaderLocation != -1)
198 {
199 preSetLocation = shaderLocation;
200 }
201
202 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
203 {
204 VariableLocation location(uniform.name, arrayIndex,
205 static_cast<unsigned int>(uniformIndex));
206
207 if ((arrayIndex == 0 && preSetLocation != -1) || shaderLocation != -1)
208 {
209 int elementLocation = preSetLocation + arrayIndex;
210 preLocatedUniforms[elementLocation] = location;
211 }
212 else
213 {
214 unlocatedUniforms.push_back(location);
215 }
216 }
217 }
218
219 // Make enough space for all uniforms, with pre-set locations or not.
220 mUniformLocations.resize(
221 std::max(unlocatedUniforms.size() + preLocatedUniforms.size() + ignoredLocations.size(),
222 static_cast<size_t>(maxUniformLocation + 1)));
223
224 // Assign uniforms with pre-set locations
225 for (const auto &uniform : preLocatedUniforms)
226 {
227 mUniformLocations[uniform.first] = uniform.second;
228 }
229
230 // Assign ignored uniforms
231 for (const auto &ignoredLocation : ignoredLocations)
232 {
233 mUniformLocations[ignoredLocation].ignored = true;
234 }
235
236 // Automatically assign locations for the rest of the uniforms
237 size_t nextUniformLocation = 0;
238 for (const auto &unlocatedUniform : unlocatedUniforms)
239 {
240 while (mUniformLocations[nextUniformLocation].used ||
241 mUniformLocations[nextUniformLocation].ignored)
242 {
243 nextUniformLocation++;
244 }
245
246 ASSERT(nextUniformLocation < mUniformLocations.size());
247 mUniformLocations[nextUniformLocation] = unlocatedUniform;
248 nextUniformLocation++;
249 }
250
251 return true;
252}
253
254bool UniformLinker::gatherUniformLocationsAndCheckConflicts(
255 InfoLog &infoLog,
256 const Program::Bindings &uniformLocationBindings,
257 std::set<GLuint> *reservedLocations,
258 std::set<GLuint> *ignoredLocations,
259 int *maxUniformLocation)
260{
261 for (const LinkedUniform &uniform : mUniforms)
262 {
263 if (uniform.isBuiltIn())
264 {
265 continue;
266 }
267
268 int apiBoundLocation = uniformLocationBindings.getBinding(uniform.name);
269 int shaderLocation = uniform.location;
270
271 if (shaderLocation != -1)
272 {
273 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
274 {
275 // GLSL ES 3.10 section 4.4.3
276 int elementLocation = shaderLocation + arrayIndex;
277 *maxUniformLocation = std::max(*maxUniformLocation, elementLocation);
278 if (reservedLocations->find(elementLocation) != reservedLocations->end())
279 {
280 infoLog << "Multiple uniforms bound to location " << elementLocation << ".";
281 return false;
282 }
283 reservedLocations->insert(elementLocation);
284 if (!uniform.staticUse)
285 {
286 ignoredLocations->insert(elementLocation);
287 }
288 }
289 }
290 else if (apiBoundLocation != -1 && uniform.staticUse)
291 {
292 // Only the first location is reserved even if the uniform is an array.
293 *maxUniformLocation = std::max(*maxUniformLocation, apiBoundLocation);
294 if (reservedLocations->find(apiBoundLocation) != reservedLocations->end())
295 {
296 infoLog << "Multiple uniforms bound to location " << apiBoundLocation << ".";
297 return false;
298 }
299 reservedLocations->insert(apiBoundLocation);
300 }
301 }
302
303 // Record the uniform locations that were bound using the API for uniforms that were not found
304 // from the shader. Other uniforms should not be assigned to those locations.
305 for (const auto &locationBinding : uniformLocationBindings)
306 {
307 GLuint location = locationBinding.second;
308 if (reservedLocations->find(location) == reservedLocations->end())
309 {
310 ignoredLocations->insert(location);
311 *maxUniformLocation = std::max(*maxUniformLocation, static_cast<int>(location));
312 }
313 }
314
315 return true;
316}
317
318void UniformLinker::pruneUnusedUniforms()
319{
320 auto uniformIter = mUniforms.begin();
321 while (uniformIter != mUniforms.end())
322 {
323 if (uniformIter->staticUse)
324 {
325 ++uniformIter;
326 }
327 else
328 {
329 uniformIter = mUniforms.erase(uniformIter);
330 }
331 }
332}
333
334bool UniformLinker::flattenUniformsAndCheckCapsForShader(
Jamie Madillbd044ed2017-06-05 12:59:21 -0400335 const Context *context,
336 Shader *shader,
Olli Etuahob78707c2017-03-09 15:03:11 +0000337 GLuint maxUniformComponents,
338 GLuint maxTextureImageUnits,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800339 GLuint maxImageUnits,
jchen10eaef1e52017-06-13 10:44:11 +0800340 GLuint maxAtomicCounters,
Olli Etuahob78707c2017-03-09 15:03:11 +0000341 const std::string &componentsErrorMessage,
342 const std::string &samplerErrorMessage,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800343 const std::string &imageErrorMessage,
jchen10eaef1e52017-06-13 10:44:11 +0800344 const std::string &atomicCounterErrorMessage,
Olli Etuahob78707c2017-03-09 15:03:11 +0000345 std::vector<LinkedUniform> &samplerUniforms,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800346 std::vector<LinkedUniform> &imageUniforms,
jchen10eaef1e52017-06-13 10:44:11 +0800347 std::vector<LinkedUniform> &atomicCounterUniforms,
Olli Etuahob78707c2017-03-09 15:03:11 +0000348 InfoLog &infoLog)
349{
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800350 ShaderUniformCount shaderUniformCount;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400351 for (const sh::Uniform &uniform : shader->getUniforms(context))
Olli Etuahob78707c2017-03-09 15:03:11 +0000352 {
jchen10eaef1e52017-06-13 10:44:11 +0800353 shaderUniformCount +=
354 flattenUniform(uniform, &samplerUniforms, &imageUniforms, &atomicCounterUniforms);
Olli Etuahob78707c2017-03-09 15:03:11 +0000355 }
356
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800357 if (shaderUniformCount.vectorCount > maxUniformComponents)
Olli Etuahob78707c2017-03-09 15:03:11 +0000358 {
359 infoLog << componentsErrorMessage << maxUniformComponents << ").";
360 return false;
361 }
362
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800363 if (shaderUniformCount.samplerCount > maxTextureImageUnits)
Olli Etuahob78707c2017-03-09 15:03:11 +0000364 {
365 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
366 return false;
367 }
368
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800369 if (shaderUniformCount.imageCount > maxImageUnits)
370 {
371 infoLog << imageErrorMessage << maxImageUnits << ").";
372 return false;
373 }
374
jchen10eaef1e52017-06-13 10:44:11 +0800375 if (shaderUniformCount.atomicCounterCount > maxAtomicCounters)
376 {
377 infoLog << atomicCounterErrorMessage << maxAtomicCounters << ").";
378 return false;
379 }
380
Olli Etuahob78707c2017-03-09 15:03:11 +0000381 return true;
382}
383
Jamie Madillbd044ed2017-06-05 12:59:21 -0400384bool UniformLinker::flattenUniformsAndCheckCaps(const Context *context, InfoLog &infoLog)
Olli Etuahob78707c2017-03-09 15:03:11 +0000385{
386 std::vector<LinkedUniform> samplerUniforms;
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800387 std::vector<LinkedUniform> imageUniforms;
jchen10eaef1e52017-06-13 10:44:11 +0800388 std::vector<LinkedUniform> atomicCounterUniforms;
Olli Etuahob78707c2017-03-09 15:03:11 +0000389
Jamie Madillbd044ed2017-06-05 12:59:21 -0400390 const Caps &caps = context->getCaps();
391
Olli Etuahob78707c2017-03-09 15:03:11 +0000392 if (mState.getAttachedComputeShader())
393 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400394 Shader *computeShader = mState.getAttachedComputeShader();
Olli Etuahob78707c2017-03-09 15:03:11 +0000395
396 // TODO (mradev): check whether we need finer-grained component counting
397 if (!flattenUniformsAndCheckCapsForShader(
Jamie Madillbd044ed2017-06-05 12:59:21 -0400398 context, computeShader, caps.maxComputeUniformComponents / 4,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800399 caps.maxComputeTextureImageUnits, caps.maxComputeImageUniforms,
jchen10eaef1e52017-06-13 10:44:11 +0800400 caps.maxComputeAtomicCounters,
Olli Etuahob78707c2017-03-09 15:03:11 +0000401 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
402 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
jchen10eaef1e52017-06-13 10:44:11 +0800403 "Compute shader image count exceeds MAX_COMPUTE_IMAGE_UNIFORMS (",
404 "Compute shader atomic counter count exceeds MAX_COMPUTE_ATOMIC_COUNTERS (",
405 samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog))
Olli Etuahob78707c2017-03-09 15:03:11 +0000406 {
407 return false;
408 }
409 }
410 else
411 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400412 Shader *vertexShader = mState.getAttachedVertexShader();
Olli Etuahob78707c2017-03-09 15:03:11 +0000413
414 if (!flattenUniformsAndCheckCapsForShader(
Jamie Madillbd044ed2017-06-05 12:59:21 -0400415 context, vertexShader, caps.maxVertexUniformVectors,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800416 caps.maxVertexTextureImageUnits, caps.maxVertexImageUniforms,
jchen10eaef1e52017-06-13 10:44:11 +0800417 caps.maxVertexAtomicCounters,
Olli Etuahob78707c2017-03-09 15:03:11 +0000418 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
419 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
jchen10eaef1e52017-06-13 10:44:11 +0800420 "Vertex shader image count exceeds MAX_VERTEX_IMAGE_UNIFORMS (",
421 "Vertex shader atomic counter count exceeds MAX_VERTEX_ATOMIC_COUNTERS (",
422 samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog))
Olli Etuahob78707c2017-03-09 15:03:11 +0000423 {
424 return false;
425 }
Jamie Madillbd044ed2017-06-05 12:59:21 -0400426
427 Shader *fragmentShader = mState.getAttachedFragmentShader();
Olli Etuahob78707c2017-03-09 15:03:11 +0000428
429 if (!flattenUniformsAndCheckCapsForShader(
Jamie Madillbd044ed2017-06-05 12:59:21 -0400430 context, fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
jchen10eaef1e52017-06-13 10:44:11 +0800431 caps.maxFragmentImageUniforms, caps.maxFragmentAtomicCounters,
Olli Etuahob78707c2017-03-09 15:03:11 +0000432 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800433 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (",
434 "Fragment shader image count exceeds MAX_FRAGMENT_IMAGE_UNIFORMS (",
jchen10eaef1e52017-06-13 10:44:11 +0800435 "Fragment shader atomic counter count exceeds MAX_FRAGMENT_ATOMIC_COUNTERS (",
436 samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog))
Olli Etuahob78707c2017-03-09 15:03:11 +0000437 {
438 return false;
439 }
440 }
441
442 mUniforms.insert(mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800443 mUniforms.insert(mUniforms.end(), imageUniforms.begin(), imageUniforms.end());
jchen10eaef1e52017-06-13 10:44:11 +0800444 mUniforms.insert(mUniforms.end(), atomicCounterUniforms.begin(), atomicCounterUniforms.end());
Olli Etuahob78707c2017-03-09 15:03:11 +0000445 return true;
446}
447
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800448UniformLinker::ShaderUniformCount UniformLinker::flattenUniform(
Olli Etuahob78707c2017-03-09 15:03:11 +0000449 const sh::Uniform &uniform,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800450 std::vector<LinkedUniform> *samplerUniforms,
jchen10eaef1e52017-06-13 10:44:11 +0800451 std::vector<LinkedUniform> *imageUniforms,
452 std::vector<LinkedUniform> *atomicCounterUniforms)
Olli Etuahob78707c2017-03-09 15:03:11 +0000453{
454 int location = uniform.location;
jchen10eaef1e52017-06-13 10:44:11 +0800455 ShaderUniformCount shaderUniformCount = flattenUniformImpl(
456 uniform, uniform.name, samplerUniforms, imageUniforms, atomicCounterUniforms,
457 uniform.staticUse, uniform.binding, uniform.offset, &location);
Olli Etuahob78707c2017-03-09 15:03:11 +0000458 if (uniform.staticUse)
459 {
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800460 return shaderUniformCount;
Olli Etuahob78707c2017-03-09 15:03:11 +0000461 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800462 return ShaderUniformCount();
Olli Etuahob78707c2017-03-09 15:03:11 +0000463}
464
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800465UniformLinker::ShaderUniformCount UniformLinker::flattenUniformImpl(
Olli Etuahob78707c2017-03-09 15:03:11 +0000466 const sh::ShaderVariable &uniform,
467 const std::string &fullName,
468 std::vector<LinkedUniform> *samplerUniforms,
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800469 std::vector<LinkedUniform> *imageUniforms,
jchen10eaef1e52017-06-13 10:44:11 +0800470 std::vector<LinkedUniform> *atomicCounterUniforms,
Olli Etuahob78707c2017-03-09 15:03:11 +0000471 bool markStaticUse,
472 int binding,
jchen10eaef1e52017-06-13 10:44:11 +0800473 int offset,
Olli Etuahob78707c2017-03-09 15:03:11 +0000474 int *location)
475{
476 ASSERT(location);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800477 ShaderUniformCount shaderUniformCount;
Olli Etuahob78707c2017-03-09 15:03:11 +0000478
479 if (uniform.isStruct())
480 {
481 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
482 {
483 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
484
485 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
486 {
487 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
488 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
489
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800490 shaderUniformCount +=
491 flattenUniformImpl(field, fieldFullName, samplerUniforms, imageUniforms,
jchen10eaef1e52017-06-13 10:44:11 +0800492 atomicCounterUniforms, markStaticUse, -1, -1, location);
Olli Etuahob78707c2017-03-09 15:03:11 +0000493 }
494 }
495
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800496 return shaderUniformCount;
Olli Etuahob78707c2017-03-09 15:03:11 +0000497 }
498
499 // Not a struct
500 bool isSampler = IsSamplerType(uniform.type);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800501 bool isImage = IsImageType(uniform.type);
jchen10eaef1e52017-06-13 10:44:11 +0800502 bool isAtomicCounter = IsAtomicCounterType(uniform.type);
Olli Etuahob78707c2017-03-09 15:03:11 +0000503 std::vector<gl::LinkedUniform> *uniformList = &mUniforms;
504 if (isSampler)
505 {
Olli Etuahob78707c2017-03-09 15:03:11 +0000506 uniformList = samplerUniforms;
507 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800508 else if (isImage)
509 {
510 uniformList = imageUniforms;
511 }
jchen10eaef1e52017-06-13 10:44:11 +0800512 else if (isAtomicCounter)
513 {
514 uniformList = atomicCounterUniforms;
515 }
Olli Etuahob78707c2017-03-09 15:03:11 +0000516 LinkedUniform *existingUniform = FindUniform(*uniformList, fullName);
517 if (existingUniform)
518 {
519 if (binding != -1)
520 {
521 existingUniform->binding = binding;
522 }
jchen10eaef1e52017-06-13 10:44:11 +0800523 if (offset != -1)
524 {
525 existingUniform->offset = offset;
526 }
Olli Etuahob78707c2017-03-09 15:03:11 +0000527 if (*location != -1)
528 {
529 existingUniform->location = *location;
530 }
531 if (markStaticUse)
532 {
533 existingUniform->staticUse = true;
534 }
535 }
536 else
537 {
538 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
jchen10eaef1e52017-06-13 10:44:11 +0800539 binding, -1, *location, -1,
Olli Etuahob78707c2017-03-09 15:03:11 +0000540 sh::BlockMemberInfo::getDefaultBlockInfo());
541 linkedUniform.staticUse = markStaticUse;
jchen10eaef1e52017-06-13 10:44:11 +0800542
Olli Etuahob78707c2017-03-09 15:03:11 +0000543 uniformList->push_back(linkedUniform);
544 }
545
546 unsigned int elementCount = uniform.elementCount();
547
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800548 // Samplers and images aren't "real" uniforms, so they don't count towards register usage.
jchen10eaef1e52017-06-13 10:44:11 +0800549 // Likewise, don't count "real" uniforms towards opaque count.
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800550 shaderUniformCount.vectorCount =
jchen10eaef1e52017-06-13 10:44:11 +0800551 (IsOpaqueType(uniform.type) ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800552 shaderUniformCount.samplerCount = (isSampler ? elementCount : 0);
553 shaderUniformCount.imageCount = (isImage ? elementCount : 0);
jchen10eaef1e52017-06-13 10:44:11 +0800554 shaderUniformCount.atomicCounterCount = (isAtomicCounter ? elementCount : 0);
Olli Etuahob78707c2017-03-09 15:03:11 +0000555
556 if (*location != -1)
557 {
558 *location += elementCount;
559 }
560
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800561 return shaderUniformCount;
Olli Etuahob78707c2017-03-09 15:03:11 +0000562}
563
jchen10eaef1e52017-06-13 10:44:11 +0800564bool UniformLinker::checkMaxCombinedAtomicCounters(const Caps &caps, InfoLog &infoLog)
565{
566 unsigned int atomicCounterCount = 0;
567 for (const auto &uniform : mUniforms)
568 {
569 if (IsAtomicCounterType(uniform.type) && uniform.staticUse)
570 {
571 atomicCounterCount += uniform.elementCount();
572 if (atomicCounterCount > caps.maxCombinedAtomicCounters)
573 {
574 infoLog << "atomic counter count exceeds MAX_COMBINED_ATOMIC_COUNTERS"
575 << caps.maxCombinedAtomicCounters << ").";
576 return false;
577 }
578 }
579 }
580 return true;
581}
582
Olli Etuahob78707c2017-03-09 15:03:11 +0000583} // namespace gl