Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1 | // |
| 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 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 13 | #include "common/string_utils.h" |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 14 | #include "common/utilities.h" |
| 15 | #include "libANGLE/Caps.h" |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 17 | #include "libANGLE/Shader.h" |
Jiawei-Shao | ddb5eb5 | 2017-03-14 13:36:18 +0800 | [diff] [blame] | 18 | #include "libANGLE/features.h" |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 19 | |
| 20 | namespace gl |
| 21 | { |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | |
| 26 | LinkedUniform *FindUniform(std::vector<LinkedUniform> &list, const std::string &name) |
| 27 | { |
| 28 | for (LinkedUniform &uniform : list) |
| 29 | { |
| 30 | if (uniform.name == name) |
| 31 | return &uniform; |
| 32 | } |
| 33 | |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 37 | int GetUniformLocationBinding(const Program::Bindings &uniformLocationBindings, |
| 38 | const sh::Uniform &uniform) |
| 39 | { |
| 40 | int binding = uniformLocationBindings.getBinding(uniform.name); |
| 41 | if (uniform.isArray() && binding == -1) |
| 42 | { |
| 43 | // Bindings for array uniforms can be set either with or without [0] in the end. |
| 44 | ASSERT(angle::EndsWith(uniform.name, "[0]")); |
| 45 | std::string nameWithoutIndex = uniform.name.substr(0u, uniform.name.length() - 3u); |
| 46 | return uniformLocationBindings.getBinding(nameWithoutIndex); |
| 47 | } |
| 48 | return binding; |
| 49 | } |
| 50 | |
| 51 | } // anonymous namespace |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 52 | |
| 53 | UniformLinker::UniformLinker(const ProgramState &state) : mState(state) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void UniformLinker::getResults(std::vector<LinkedUniform> *uniforms, |
| 58 | std::vector<VariableLocation> *uniformLocations) |
| 59 | { |
| 60 | uniforms->swap(mUniforms); |
| 61 | uniformLocations->swap(mUniformLocations); |
| 62 | } |
| 63 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 64 | bool UniformLinker::link(const Context *context, |
| 65 | InfoLog &infoLog, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 66 | const Program::Bindings &uniformLocationBindings) |
| 67 | { |
| 68 | if (mState.getAttachedVertexShader() && mState.getAttachedFragmentShader()) |
| 69 | { |
| 70 | ASSERT(mState.getAttachedComputeShader() == nullptr); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 71 | if (!validateVertexAndFragmentUniforms(context, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Flatten the uniforms list (nested fields) into a simple list (no nesting). |
| 78 | // Also check the maximum uniform vector and sampler counts. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 79 | if (!flattenUniformsAndCheckCaps(context, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 80 | { |
| 81 | return false; |
| 82 | } |
| 83 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 84 | if (!checkMaxCombinedAtomicCounters(context->getCaps(), infoLog)) |
| 85 | { |
| 86 | return false; |
| 87 | } |
| 88 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 89 | if (!indexUniforms(infoLog, uniformLocationBindings)) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 97 | bool UniformLinker::validateVertexAndFragmentUniforms(const Context *context, |
| 98 | InfoLog &infoLog) const |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 99 | { |
| 100 | // Check that uniforms defined in the vertex and fragment shaders are identical |
| 101 | std::map<std::string, LinkedUniform> linkedUniforms; |
| 102 | const std::vector<sh::Uniform> &vertexUniforms = |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 103 | mState.getAttachedVertexShader()->getUniforms(context); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 104 | const std::vector<sh::Uniform> &fragmentUniforms = |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 105 | mState.getAttachedFragmentShader()->getUniforms(context); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 106 | |
| 107 | for (const sh::Uniform &vertexUniform : vertexUniforms) |
| 108 | { |
| 109 | linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform); |
| 110 | } |
| 111 | |
| 112 | for (const sh::Uniform &fragmentUniform : fragmentUniforms) |
| 113 | { |
| 114 | auto entry = linkedUniforms.find(fragmentUniform.name); |
| 115 | if (entry != linkedUniforms.end()) |
| 116 | { |
| 117 | LinkedUniform *linkedUniform = &entry->second; |
| 118 | const std::string &uniformName = "uniform '" + linkedUniform->name + "'"; |
| 119 | if (!linkValidateUniforms(infoLog, uniformName, *linkedUniform, fragmentUniform)) |
| 120 | { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | // GLSL ES Spec 3.00.3, section 4.3.5. |
| 129 | bool UniformLinker::linkValidateUniforms(InfoLog &infoLog, |
| 130 | const std::string &uniformName, |
| 131 | const sh::Uniform &vertexUniform, |
| 132 | const sh::Uniform &fragmentUniform) |
| 133 | { |
| 134 | #if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED |
| 135 | const bool validatePrecision = true; |
| 136 | #else |
| 137 | const bool validatePrecision = false; |
| 138 | #endif |
| 139 | |
| 140 | if (!Program::linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, |
| 141 | validatePrecision)) |
| 142 | { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // GLSL ES Spec 3.10.4, section 4.4.5. |
| 147 | if (vertexUniform.binding != -1 && fragmentUniform.binding != -1 && |
| 148 | vertexUniform.binding != fragmentUniform.binding) |
| 149 | { |
| 150 | infoLog << "Binding layout qualifiers for " << uniformName |
| 151 | << " differ between vertex and fragment shaders."; |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // GLSL ES Spec 3.10.4, section 9.2.1. |
| 156 | if (vertexUniform.location != -1 && fragmentUniform.location != -1 && |
| 157 | vertexUniform.location != fragmentUniform.location) |
| 158 | { |
| 159 | infoLog << "Location layout qualifiers for " << uniformName |
| 160 | << " differ between vertex and fragment shaders."; |
| 161 | return false; |
| 162 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 163 | if (vertexUniform.offset != fragmentUniform.offset) |
| 164 | { |
| 165 | infoLog << "Offset layout qualifiers for " << uniformName |
| 166 | << " differ between vertex and fragment shaders."; |
| 167 | return false; |
| 168 | } |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool UniformLinker::indexUniforms(InfoLog &infoLog, |
| 174 | const Program::Bindings &uniformLocationBindings) |
| 175 | { |
| 176 | // All the locations where another uniform can't be located. |
| 177 | std::set<GLuint> reservedLocations; |
| 178 | // Locations which have been allocated for an unused uniform. |
| 179 | std::set<GLuint> ignoredLocations; |
| 180 | |
| 181 | int maxUniformLocation = -1; |
| 182 | |
| 183 | // Gather uniform locations that have been set either using the bindUniformLocation API or by |
| 184 | // using a location layout qualifier and check conflicts between them. |
| 185 | if (!gatherUniformLocationsAndCheckConflicts(infoLog, uniformLocationBindings, |
| 186 | &reservedLocations, &ignoredLocations, |
| 187 | &maxUniformLocation)) |
| 188 | { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // Conflicts have been checked, now we can prune non-statically used uniforms. Code further down |
| 193 | // the line relies on only having statically used uniforms in mUniforms. |
| 194 | pruneUnusedUniforms(); |
| 195 | |
| 196 | // Gather uniforms that have their location pre-set and uniforms that don't yet have a location. |
| 197 | std::vector<VariableLocation> unlocatedUniforms; |
| 198 | std::map<GLuint, VariableLocation> preLocatedUniforms; |
| 199 | |
| 200 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 201 | { |
| 202 | const LinkedUniform &uniform = mUniforms[uniformIndex]; |
| 203 | |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 204 | if (uniform.isBuiltIn() || IsAtomicCounterType(uniform.type)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 205 | { |
| 206 | continue; |
| 207 | } |
| 208 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 209 | int preSetLocation = GetUniformLocationBinding(uniformLocationBindings, uniform); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 210 | int shaderLocation = uniform.location; |
| 211 | |
| 212 | if (shaderLocation != -1) |
| 213 | { |
| 214 | preSetLocation = shaderLocation; |
| 215 | } |
| 216 | |
| 217 | for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++) |
| 218 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 219 | VariableLocation location(arrayIndex, static_cast<unsigned int>(uniformIndex)); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 220 | |
| 221 | if ((arrayIndex == 0 && preSetLocation != -1) || shaderLocation != -1) |
| 222 | { |
| 223 | int elementLocation = preSetLocation + arrayIndex; |
| 224 | preLocatedUniforms[elementLocation] = location; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | unlocatedUniforms.push_back(location); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Make enough space for all uniforms, with pre-set locations or not. |
| 234 | mUniformLocations.resize( |
| 235 | std::max(unlocatedUniforms.size() + preLocatedUniforms.size() + ignoredLocations.size(), |
| 236 | static_cast<size_t>(maxUniformLocation + 1))); |
| 237 | |
| 238 | // Assign uniforms with pre-set locations |
| 239 | for (const auto &uniform : preLocatedUniforms) |
| 240 | { |
| 241 | mUniformLocations[uniform.first] = uniform.second; |
| 242 | } |
| 243 | |
| 244 | // Assign ignored uniforms |
| 245 | for (const auto &ignoredLocation : ignoredLocations) |
| 246 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 247 | mUniformLocations[ignoredLocation].markIgnored(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Automatically assign locations for the rest of the uniforms |
| 251 | size_t nextUniformLocation = 0; |
| 252 | for (const auto &unlocatedUniform : unlocatedUniforms) |
| 253 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 254 | while (mUniformLocations[nextUniformLocation].used() || |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 255 | mUniformLocations[nextUniformLocation].ignored) |
| 256 | { |
| 257 | nextUniformLocation++; |
| 258 | } |
| 259 | |
| 260 | ASSERT(nextUniformLocation < mUniformLocations.size()); |
| 261 | mUniformLocations[nextUniformLocation] = unlocatedUniform; |
| 262 | nextUniformLocation++; |
| 263 | } |
| 264 | |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | bool UniformLinker::gatherUniformLocationsAndCheckConflicts( |
| 269 | InfoLog &infoLog, |
| 270 | const Program::Bindings &uniformLocationBindings, |
| 271 | std::set<GLuint> *reservedLocations, |
| 272 | std::set<GLuint> *ignoredLocations, |
| 273 | int *maxUniformLocation) |
| 274 | { |
| 275 | for (const LinkedUniform &uniform : mUniforms) |
| 276 | { |
| 277 | if (uniform.isBuiltIn()) |
| 278 | { |
| 279 | continue; |
| 280 | } |
| 281 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 282 | int apiBoundLocation = GetUniformLocationBinding(uniformLocationBindings, uniform); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 283 | int shaderLocation = uniform.location; |
| 284 | |
| 285 | if (shaderLocation != -1) |
| 286 | { |
| 287 | for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++) |
| 288 | { |
| 289 | // GLSL ES 3.10 section 4.4.3 |
| 290 | int elementLocation = shaderLocation + arrayIndex; |
| 291 | *maxUniformLocation = std::max(*maxUniformLocation, elementLocation); |
| 292 | if (reservedLocations->find(elementLocation) != reservedLocations->end()) |
| 293 | { |
| 294 | infoLog << "Multiple uniforms bound to location " << elementLocation << "."; |
| 295 | return false; |
| 296 | } |
| 297 | reservedLocations->insert(elementLocation); |
| 298 | if (!uniform.staticUse) |
| 299 | { |
| 300 | ignoredLocations->insert(elementLocation); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | else if (apiBoundLocation != -1 && uniform.staticUse) |
| 305 | { |
| 306 | // Only the first location is reserved even if the uniform is an array. |
| 307 | *maxUniformLocation = std::max(*maxUniformLocation, apiBoundLocation); |
| 308 | if (reservedLocations->find(apiBoundLocation) != reservedLocations->end()) |
| 309 | { |
| 310 | infoLog << "Multiple uniforms bound to location " << apiBoundLocation << "."; |
| 311 | return false; |
| 312 | } |
| 313 | reservedLocations->insert(apiBoundLocation); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Record the uniform locations that were bound using the API for uniforms that were not found |
| 318 | // from the shader. Other uniforms should not be assigned to those locations. |
| 319 | for (const auto &locationBinding : uniformLocationBindings) |
| 320 | { |
| 321 | GLuint location = locationBinding.second; |
| 322 | if (reservedLocations->find(location) == reservedLocations->end()) |
| 323 | { |
| 324 | ignoredLocations->insert(location); |
| 325 | *maxUniformLocation = std::max(*maxUniformLocation, static_cast<int>(location)); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | void UniformLinker::pruneUnusedUniforms() |
| 333 | { |
| 334 | auto uniformIter = mUniforms.begin(); |
| 335 | while (uniformIter != mUniforms.end()) |
| 336 | { |
| 337 | if (uniformIter->staticUse) |
| 338 | { |
| 339 | ++uniformIter; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | uniformIter = mUniforms.erase(uniformIter); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | bool UniformLinker::flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 349 | const Context *context, |
| 350 | Shader *shader, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 351 | GLuint maxUniformComponents, |
| 352 | GLuint maxTextureImageUnits, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 353 | GLuint maxImageUnits, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 354 | GLuint maxAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 355 | const std::string &componentsErrorMessage, |
| 356 | const std::string &samplerErrorMessage, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 357 | const std::string &imageErrorMessage, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 358 | const std::string &atomicCounterErrorMessage, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 359 | std::vector<LinkedUniform> &samplerUniforms, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 360 | std::vector<LinkedUniform> &imageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 361 | std::vector<LinkedUniform> &atomicCounterUniforms, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 362 | InfoLog &infoLog) |
| 363 | { |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 364 | ShaderUniformCount shaderUniformCount; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 365 | for (const sh::Uniform &uniform : shader->getUniforms(context)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 366 | { |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 367 | shaderUniformCount += flattenUniform(uniform, &samplerUniforms, &imageUniforms, |
| 368 | &atomicCounterUniforms, shader->getType()); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 371 | if (shaderUniformCount.vectorCount > maxUniformComponents) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 372 | { |
| 373 | infoLog << componentsErrorMessage << maxUniformComponents << ")."; |
| 374 | return false; |
| 375 | } |
| 376 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 377 | if (shaderUniformCount.samplerCount > maxTextureImageUnits) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 378 | { |
| 379 | infoLog << samplerErrorMessage << maxTextureImageUnits << ")."; |
| 380 | return false; |
| 381 | } |
| 382 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 383 | if (shaderUniformCount.imageCount > maxImageUnits) |
| 384 | { |
| 385 | infoLog << imageErrorMessage << maxImageUnits << ")."; |
| 386 | return false; |
| 387 | } |
| 388 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 389 | if (shaderUniformCount.atomicCounterCount > maxAtomicCounters) |
| 390 | { |
| 391 | infoLog << atomicCounterErrorMessage << maxAtomicCounters << ")."; |
| 392 | return false; |
| 393 | } |
| 394 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 398 | bool UniformLinker::flattenUniformsAndCheckCaps(const Context *context, InfoLog &infoLog) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 399 | { |
| 400 | std::vector<LinkedUniform> samplerUniforms; |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 401 | std::vector<LinkedUniform> imageUniforms; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 402 | std::vector<LinkedUniform> atomicCounterUniforms; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 403 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 404 | const Caps &caps = context->getCaps(); |
| 405 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 406 | if (mState.getAttachedComputeShader()) |
| 407 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 408 | Shader *computeShader = mState.getAttachedComputeShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 409 | |
| 410 | // TODO (mradev): check whether we need finer-grained component counting |
| 411 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 412 | context, computeShader, caps.maxComputeUniformComponents / 4, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 413 | caps.maxComputeTextureImageUnits, caps.maxComputeImageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 414 | caps.maxComputeAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 415 | "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (", |
| 416 | "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 417 | "Compute shader image count exceeds MAX_COMPUTE_IMAGE_UNIFORMS (", |
| 418 | "Compute shader atomic counter count exceeds MAX_COMPUTE_ATOMIC_COUNTERS (", |
| 419 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 420 | { |
| 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | else |
| 425 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 426 | Shader *vertexShader = mState.getAttachedVertexShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 427 | |
| 428 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 429 | context, vertexShader, caps.maxVertexUniformVectors, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 430 | caps.maxVertexTextureImageUnits, caps.maxVertexImageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 431 | caps.maxVertexAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 432 | "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (", |
| 433 | "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 434 | "Vertex shader image count exceeds MAX_VERTEX_IMAGE_UNIFORMS (", |
| 435 | "Vertex shader atomic counter count exceeds MAX_VERTEX_ATOMIC_COUNTERS (", |
| 436 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 437 | { |
| 438 | return false; |
| 439 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 440 | |
| 441 | Shader *fragmentShader = mState.getAttachedFragmentShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 442 | |
| 443 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 444 | context, fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 445 | caps.maxFragmentImageUniforms, caps.maxFragmentAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 446 | "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (", |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 447 | "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", |
| 448 | "Fragment shader image count exceeds MAX_FRAGMENT_IMAGE_UNIFORMS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 449 | "Fragment shader atomic counter count exceeds MAX_FRAGMENT_ATOMIC_COUNTERS (", |
| 450 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 451 | { |
| 452 | return false; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | mUniforms.insert(mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end()); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 457 | mUniforms.insert(mUniforms.end(), imageUniforms.begin(), imageUniforms.end()); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 458 | mUniforms.insert(mUniforms.end(), atomicCounterUniforms.begin(), atomicCounterUniforms.end()); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 459 | return true; |
| 460 | } |
| 461 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 462 | UniformLinker::ShaderUniformCount UniformLinker::flattenUniform( |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 463 | const sh::Uniform &uniform, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 464 | std::vector<LinkedUniform> *samplerUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 465 | std::vector<LinkedUniform> *imageUniforms, |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 466 | std::vector<LinkedUniform> *atomicCounterUniforms, |
| 467 | GLenum shaderType) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 468 | { |
| 469 | int location = uniform.location; |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 470 | ShaderUniformCount shaderUniformCount = |
| 471 | flattenUniformImpl(uniform, uniform.name, uniform.mappedName, samplerUniforms, |
| 472 | imageUniforms, atomicCounterUniforms, shaderType, uniform.staticUse, |
| 473 | uniform.binding, uniform.offset, &location); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 474 | if (uniform.staticUse) |
| 475 | { |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 476 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 477 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 478 | return ShaderUniformCount(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 481 | UniformLinker::ShaderUniformCount UniformLinker::flattenUniformImpl( |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 482 | const sh::ShaderVariable &uniform, |
| 483 | const std::string &fullName, |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 484 | const std::string &fullMappedName, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 485 | std::vector<LinkedUniform> *samplerUniforms, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 486 | std::vector<LinkedUniform> *imageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 487 | std::vector<LinkedUniform> *atomicCounterUniforms, |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 488 | GLenum shaderType, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 489 | bool markStaticUse, |
| 490 | int binding, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 491 | int offset, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 492 | int *location) |
| 493 | { |
| 494 | ASSERT(location); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 495 | ShaderUniformCount shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 496 | |
| 497 | if (uniform.isStruct()) |
| 498 | { |
| 499 | for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++) |
| 500 | { |
| 501 | const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : ""); |
| 502 | |
| 503 | for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++) |
| 504 | { |
| 505 | const sh::ShaderVariable &field = uniform.fields[fieldIndex]; |
| 506 | const std::string &fieldFullName = (fullName + elementString + "." + field.name); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 507 | const std::string &fieldFullMappedName = |
| 508 | (fullMappedName + elementString + "." + field.mappedName); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 509 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 510 | shaderUniformCount += flattenUniformImpl( |
| 511 | field, fieldFullName, fieldFullMappedName, samplerUniforms, imageUniforms, |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 512 | atomicCounterUniforms, shaderType, markStaticUse, -1, -1, location); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 516 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Not a struct |
| 520 | bool isSampler = IsSamplerType(uniform.type); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 521 | bool isImage = IsImageType(uniform.type); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 522 | bool isAtomicCounter = IsAtomicCounterType(uniform.type); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 523 | std::vector<gl::LinkedUniform> *uniformList = &mUniforms; |
| 524 | if (isSampler) |
| 525 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 526 | uniformList = samplerUniforms; |
| 527 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 528 | else if (isImage) |
| 529 | { |
| 530 | uniformList = imageUniforms; |
| 531 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 532 | else if (isAtomicCounter) |
| 533 | { |
| 534 | uniformList = atomicCounterUniforms; |
| 535 | } |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 536 | |
| 537 | std::string fullNameWithArrayIndex(fullName); |
| 538 | std::string fullMappedNameWithArrayIndex(fullMappedName); |
| 539 | |
| 540 | if (uniform.isArray()) |
| 541 | { |
| 542 | // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active Resources |
| 543 | // and including [0] at the end of array variable names. |
| 544 | fullNameWithArrayIndex += "[0]"; |
| 545 | fullMappedNameWithArrayIndex += "[0]"; |
| 546 | } |
| 547 | |
| 548 | LinkedUniform *existingUniform = FindUniform(*uniformList, fullNameWithArrayIndex); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 549 | if (existingUniform) |
| 550 | { |
| 551 | if (binding != -1) |
| 552 | { |
| 553 | existingUniform->binding = binding; |
| 554 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 555 | if (offset != -1) |
| 556 | { |
| 557 | existingUniform->offset = offset; |
| 558 | } |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 559 | if (*location != -1) |
| 560 | { |
| 561 | existingUniform->location = *location; |
| 562 | } |
| 563 | if (markStaticUse) |
| 564 | { |
| 565 | existingUniform->staticUse = true; |
jchen10 | 4ef2503 | 2017-11-01 09:36:51 +0800 | [diff] [blame] | 566 | existingUniform->setStaticUse(shaderType, true); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | else |
| 570 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 571 | LinkedUniform linkedUniform(uniform.type, uniform.precision, fullNameWithArrayIndex, |
| 572 | uniform.arraySize, binding, offset, *location, -1, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 573 | sh::BlockMemberInfo::getDefaultBlockInfo()); |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 574 | linkedUniform.mappedName = fullMappedNameWithArrayIndex; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 575 | linkedUniform.staticUse = markStaticUse; |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 576 | if (markStaticUse) |
| 577 | { |
jchen10 | 4ef2503 | 2017-11-01 09:36:51 +0800 | [diff] [blame] | 578 | linkedUniform.setStaticUse(shaderType, true); |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 579 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 580 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 581 | uniformList->push_back(linkedUniform); |
| 582 | } |
| 583 | |
| 584 | unsigned int elementCount = uniform.elementCount(); |
| 585 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 586 | // Samplers and images aren't "real" uniforms, so they don't count towards register usage. |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 587 | // Likewise, don't count "real" uniforms towards opaque count. |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 588 | shaderUniformCount.vectorCount = |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 589 | (IsOpaqueType(uniform.type) ? 0 : (VariableRegisterCount(uniform.type) * elementCount)); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 590 | shaderUniformCount.samplerCount = (isSampler ? elementCount : 0); |
| 591 | shaderUniformCount.imageCount = (isImage ? elementCount : 0); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 592 | shaderUniformCount.atomicCounterCount = (isAtomicCounter ? elementCount : 0); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 593 | |
| 594 | if (*location != -1) |
| 595 | { |
| 596 | *location += elementCount; |
| 597 | } |
| 598 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 599 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 600 | } |
| 601 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 602 | bool UniformLinker::checkMaxCombinedAtomicCounters(const Caps &caps, InfoLog &infoLog) |
| 603 | { |
| 604 | unsigned int atomicCounterCount = 0; |
| 605 | for (const auto &uniform : mUniforms) |
| 606 | { |
| 607 | if (IsAtomicCounterType(uniform.type) && uniform.staticUse) |
| 608 | { |
| 609 | atomicCounterCount += uniform.elementCount(); |
| 610 | if (atomicCounterCount > caps.maxCombinedAtomicCounters) |
| 611 | { |
| 612 | infoLog << "atomic counter count exceeds MAX_COMBINED_ATOMIC_COUNTERS" |
| 613 | << caps.maxCombinedAtomicCounters << ")."; |
| 614 | return false; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | return true; |
| 619 | } |
| 620 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 621 | } // namespace gl |