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 | |
| 13 | #include "common/utilities.h" |
| 14 | #include "libANGLE/Caps.h" |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 15 | #include "libANGLE/Context.h" |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 16 | #include "libANGLE/Shader.h" |
Jiawei-Shao | ddb5eb5 | 2017-03-14 13:36:18 +0800 | [diff] [blame] | 17 | #include "libANGLE/features.h" |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 18 | |
| 19 | namespace gl |
| 20 | { |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | LinkedUniform *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 | |
| 38 | UniformLinker::UniformLinker(const ProgramState &state) : mState(state) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void UniformLinker::getResults(std::vector<LinkedUniform> *uniforms, |
| 43 | std::vector<VariableLocation> *uniformLocations) |
| 44 | { |
| 45 | uniforms->swap(mUniforms); |
| 46 | uniformLocations->swap(mUniformLocations); |
| 47 | } |
| 48 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 49 | bool UniformLinker::link(const Context *context, |
| 50 | InfoLog &infoLog, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 51 | const Program::Bindings &uniformLocationBindings) |
| 52 | { |
| 53 | if (mState.getAttachedVertexShader() && mState.getAttachedFragmentShader()) |
| 54 | { |
| 55 | ASSERT(mState.getAttachedComputeShader() == nullptr); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 56 | if (!validateVertexAndFragmentUniforms(context, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 57 | { |
| 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 Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 64 | if (!flattenUniformsAndCheckCaps(context, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 69 | if (!checkMaxCombinedAtomicCounters(context->getCaps(), infoLog)) |
| 70 | { |
| 71 | return false; |
| 72 | } |
| 73 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 74 | if (!indexUniforms(infoLog, uniformLocationBindings)) |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 82 | bool UniformLinker::validateVertexAndFragmentUniforms(const Context *context, |
| 83 | InfoLog &infoLog) const |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 84 | { |
| 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 Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 88 | mState.getAttachedVertexShader()->getUniforms(context); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 89 | const std::vector<sh::Uniform> &fragmentUniforms = |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 90 | mState.getAttachedFragmentShader()->getUniforms(context); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 91 | |
| 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. |
| 114 | bool 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 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 148 | 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 Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool 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 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame^] | 204 | VariableLocation location(arrayIndex, static_cast<unsigned int>(uniformIndex)); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 205 | |
| 206 | if ((arrayIndex == 0 && preSetLocation != -1) || shaderLocation != -1) |
| 207 | { |
| 208 | int elementLocation = preSetLocation + arrayIndex; |
| 209 | preLocatedUniforms[elementLocation] = location; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | unlocatedUniforms.push_back(location); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Make enough space for all uniforms, with pre-set locations or not. |
| 219 | mUniformLocations.resize( |
| 220 | std::max(unlocatedUniforms.size() + preLocatedUniforms.size() + ignoredLocations.size(), |
| 221 | static_cast<size_t>(maxUniformLocation + 1))); |
| 222 | |
| 223 | // Assign uniforms with pre-set locations |
| 224 | for (const auto &uniform : preLocatedUniforms) |
| 225 | { |
| 226 | mUniformLocations[uniform.first] = uniform.second; |
| 227 | } |
| 228 | |
| 229 | // Assign ignored uniforms |
| 230 | for (const auto &ignoredLocation : ignoredLocations) |
| 231 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame^] | 232 | mUniformLocations[ignoredLocation].markIgnored(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Automatically assign locations for the rest of the uniforms |
| 236 | size_t nextUniformLocation = 0; |
| 237 | for (const auto &unlocatedUniform : unlocatedUniforms) |
| 238 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame^] | 239 | while (mUniformLocations[nextUniformLocation].used() || |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 240 | mUniformLocations[nextUniformLocation].ignored) |
| 241 | { |
| 242 | nextUniformLocation++; |
| 243 | } |
| 244 | |
| 245 | ASSERT(nextUniformLocation < mUniformLocations.size()); |
| 246 | mUniformLocations[nextUniformLocation] = unlocatedUniform; |
| 247 | nextUniformLocation++; |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool UniformLinker::gatherUniformLocationsAndCheckConflicts( |
| 254 | InfoLog &infoLog, |
| 255 | const Program::Bindings &uniformLocationBindings, |
| 256 | std::set<GLuint> *reservedLocations, |
| 257 | std::set<GLuint> *ignoredLocations, |
| 258 | int *maxUniformLocation) |
| 259 | { |
| 260 | for (const LinkedUniform &uniform : mUniforms) |
| 261 | { |
| 262 | if (uniform.isBuiltIn()) |
| 263 | { |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | int apiBoundLocation = uniformLocationBindings.getBinding(uniform.name); |
| 268 | int shaderLocation = uniform.location; |
| 269 | |
| 270 | if (shaderLocation != -1) |
| 271 | { |
| 272 | for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++) |
| 273 | { |
| 274 | // GLSL ES 3.10 section 4.4.3 |
| 275 | int elementLocation = shaderLocation + arrayIndex; |
| 276 | *maxUniformLocation = std::max(*maxUniformLocation, elementLocation); |
| 277 | if (reservedLocations->find(elementLocation) != reservedLocations->end()) |
| 278 | { |
| 279 | infoLog << "Multiple uniforms bound to location " << elementLocation << "."; |
| 280 | return false; |
| 281 | } |
| 282 | reservedLocations->insert(elementLocation); |
| 283 | if (!uniform.staticUse) |
| 284 | { |
| 285 | ignoredLocations->insert(elementLocation); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | else if (apiBoundLocation != -1 && uniform.staticUse) |
| 290 | { |
| 291 | // Only the first location is reserved even if the uniform is an array. |
| 292 | *maxUniformLocation = std::max(*maxUniformLocation, apiBoundLocation); |
| 293 | if (reservedLocations->find(apiBoundLocation) != reservedLocations->end()) |
| 294 | { |
| 295 | infoLog << "Multiple uniforms bound to location " << apiBoundLocation << "."; |
| 296 | return false; |
| 297 | } |
| 298 | reservedLocations->insert(apiBoundLocation); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // Record the uniform locations that were bound using the API for uniforms that were not found |
| 303 | // from the shader. Other uniforms should not be assigned to those locations. |
| 304 | for (const auto &locationBinding : uniformLocationBindings) |
| 305 | { |
| 306 | GLuint location = locationBinding.second; |
| 307 | if (reservedLocations->find(location) == reservedLocations->end()) |
| 308 | { |
| 309 | ignoredLocations->insert(location); |
| 310 | *maxUniformLocation = std::max(*maxUniformLocation, static_cast<int>(location)); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | void UniformLinker::pruneUnusedUniforms() |
| 318 | { |
| 319 | auto uniformIter = mUniforms.begin(); |
| 320 | while (uniformIter != mUniforms.end()) |
| 321 | { |
| 322 | if (uniformIter->staticUse) |
| 323 | { |
| 324 | ++uniformIter; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | uniformIter = mUniforms.erase(uniformIter); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | bool UniformLinker::flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 334 | const Context *context, |
| 335 | Shader *shader, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 336 | GLuint maxUniformComponents, |
| 337 | GLuint maxTextureImageUnits, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 338 | GLuint maxImageUnits, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 339 | GLuint maxAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 340 | const std::string &componentsErrorMessage, |
| 341 | const std::string &samplerErrorMessage, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 342 | const std::string &imageErrorMessage, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 343 | const std::string &atomicCounterErrorMessage, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 344 | std::vector<LinkedUniform> &samplerUniforms, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 345 | std::vector<LinkedUniform> &imageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 346 | std::vector<LinkedUniform> &atomicCounterUniforms, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 347 | InfoLog &infoLog) |
| 348 | { |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 349 | ShaderUniformCount shaderUniformCount; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 350 | for (const sh::Uniform &uniform : shader->getUniforms(context)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 351 | { |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 352 | shaderUniformCount += |
| 353 | flattenUniform(uniform, &samplerUniforms, &imageUniforms, &atomicCounterUniforms); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 356 | if (shaderUniformCount.vectorCount > maxUniformComponents) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 357 | { |
| 358 | infoLog << componentsErrorMessage << maxUniformComponents << ")."; |
| 359 | return false; |
| 360 | } |
| 361 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 362 | if (shaderUniformCount.samplerCount > maxTextureImageUnits) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 363 | { |
| 364 | infoLog << samplerErrorMessage << maxTextureImageUnits << ")."; |
| 365 | return false; |
| 366 | } |
| 367 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 368 | if (shaderUniformCount.imageCount > maxImageUnits) |
| 369 | { |
| 370 | infoLog << imageErrorMessage << maxImageUnits << ")."; |
| 371 | return false; |
| 372 | } |
| 373 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 374 | if (shaderUniformCount.atomicCounterCount > maxAtomicCounters) |
| 375 | { |
| 376 | infoLog << atomicCounterErrorMessage << maxAtomicCounters << ")."; |
| 377 | return false; |
| 378 | } |
| 379 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 383 | bool UniformLinker::flattenUniformsAndCheckCaps(const Context *context, InfoLog &infoLog) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 384 | { |
| 385 | std::vector<LinkedUniform> samplerUniforms; |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 386 | std::vector<LinkedUniform> imageUniforms; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 387 | std::vector<LinkedUniform> atomicCounterUniforms; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 388 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 389 | const Caps &caps = context->getCaps(); |
| 390 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 391 | if (mState.getAttachedComputeShader()) |
| 392 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 393 | Shader *computeShader = mState.getAttachedComputeShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 394 | |
| 395 | // TODO (mradev): check whether we need finer-grained component counting |
| 396 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 397 | context, computeShader, caps.maxComputeUniformComponents / 4, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 398 | caps.maxComputeTextureImageUnits, caps.maxComputeImageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 399 | caps.maxComputeAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 400 | "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (", |
| 401 | "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 402 | "Compute shader image count exceeds MAX_COMPUTE_IMAGE_UNIFORMS (", |
| 403 | "Compute shader atomic counter count exceeds MAX_COMPUTE_ATOMIC_COUNTERS (", |
| 404 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 405 | { |
| 406 | return false; |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 411 | Shader *vertexShader = mState.getAttachedVertexShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 412 | |
| 413 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 414 | context, vertexShader, caps.maxVertexUniformVectors, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 415 | caps.maxVertexTextureImageUnits, caps.maxVertexImageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 416 | caps.maxVertexAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 417 | "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (", |
| 418 | "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 419 | "Vertex shader image count exceeds MAX_VERTEX_IMAGE_UNIFORMS (", |
| 420 | "Vertex shader atomic counter count exceeds MAX_VERTEX_ATOMIC_COUNTERS (", |
| 421 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 422 | { |
| 423 | return false; |
| 424 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 425 | |
| 426 | Shader *fragmentShader = mState.getAttachedFragmentShader(); |
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, fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 430 | caps.maxFragmentImageUniforms, caps.maxFragmentAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 431 | "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (", |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 432 | "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", |
| 433 | "Fragment shader image count exceeds MAX_FRAGMENT_IMAGE_UNIFORMS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 434 | "Fragment shader atomic counter count exceeds MAX_FRAGMENT_ATOMIC_COUNTERS (", |
| 435 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 436 | { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | mUniforms.insert(mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end()); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 442 | mUniforms.insert(mUniforms.end(), imageUniforms.begin(), imageUniforms.end()); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 443 | mUniforms.insert(mUniforms.end(), atomicCounterUniforms.begin(), atomicCounterUniforms.end()); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 444 | return true; |
| 445 | } |
| 446 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 447 | UniformLinker::ShaderUniformCount UniformLinker::flattenUniform( |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 448 | const sh::Uniform &uniform, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 449 | std::vector<LinkedUniform> *samplerUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 450 | std::vector<LinkedUniform> *imageUniforms, |
| 451 | std::vector<LinkedUniform> *atomicCounterUniforms) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 452 | { |
| 453 | int location = uniform.location; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 454 | ShaderUniformCount shaderUniformCount = flattenUniformImpl( |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 455 | uniform, uniform.name, uniform.mappedName, samplerUniforms, imageUniforms, |
| 456 | atomicCounterUniforms, uniform.staticUse, uniform.binding, uniform.offset, &location); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 457 | if (uniform.staticUse) |
| 458 | { |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 459 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 460 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 461 | return ShaderUniformCount(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 464 | UniformLinker::ShaderUniformCount UniformLinker::flattenUniformImpl( |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 465 | const sh::ShaderVariable &uniform, |
| 466 | const std::string &fullName, |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 467 | const std::string &fullMappedName, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 468 | std::vector<LinkedUniform> *samplerUniforms, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 469 | std::vector<LinkedUniform> *imageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 470 | std::vector<LinkedUniform> *atomicCounterUniforms, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 471 | bool markStaticUse, |
| 472 | int binding, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 473 | int offset, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 474 | int *location) |
| 475 | { |
| 476 | ASSERT(location); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 477 | ShaderUniformCount shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 478 | |
| 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); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 489 | const std::string &fieldFullMappedName = |
| 490 | (fullMappedName + elementString + "." + field.mappedName); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 491 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 492 | shaderUniformCount += flattenUniformImpl( |
| 493 | field, fieldFullName, fieldFullMappedName, samplerUniforms, imageUniforms, |
| 494 | atomicCounterUniforms, markStaticUse, -1, -1, location); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 498 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Not a struct |
| 502 | bool isSampler = IsSamplerType(uniform.type); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 503 | bool isImage = IsImageType(uniform.type); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 504 | bool isAtomicCounter = IsAtomicCounterType(uniform.type); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 505 | std::vector<gl::LinkedUniform> *uniformList = &mUniforms; |
| 506 | if (isSampler) |
| 507 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 508 | uniformList = samplerUniforms; |
| 509 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 510 | else if (isImage) |
| 511 | { |
| 512 | uniformList = imageUniforms; |
| 513 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 514 | else if (isAtomicCounter) |
| 515 | { |
| 516 | uniformList = atomicCounterUniforms; |
| 517 | } |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 518 | LinkedUniform *existingUniform = FindUniform(*uniformList, fullName); |
| 519 | if (existingUniform) |
| 520 | { |
| 521 | if (binding != -1) |
| 522 | { |
| 523 | existingUniform->binding = binding; |
| 524 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 525 | if (offset != -1) |
| 526 | { |
| 527 | existingUniform->offset = offset; |
| 528 | } |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 529 | if (*location != -1) |
| 530 | { |
| 531 | existingUniform->location = *location; |
| 532 | } |
| 533 | if (markStaticUse) |
| 534 | { |
| 535 | existingUniform->staticUse = true; |
| 536 | } |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 541 | binding, -1, *location, -1, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 542 | sh::BlockMemberInfo::getDefaultBlockInfo()); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 543 | linkedUniform.mappedName = fullMappedName; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 544 | linkedUniform.staticUse = markStaticUse; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 545 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 546 | uniformList->push_back(linkedUniform); |
| 547 | } |
| 548 | |
| 549 | unsigned int elementCount = uniform.elementCount(); |
| 550 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 551 | // 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] | 552 | // Likewise, don't count "real" uniforms towards opaque count. |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 553 | shaderUniformCount.vectorCount = |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 554 | (IsOpaqueType(uniform.type) ? 0 : (VariableRegisterCount(uniform.type) * elementCount)); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 555 | shaderUniformCount.samplerCount = (isSampler ? elementCount : 0); |
| 556 | shaderUniformCount.imageCount = (isImage ? elementCount : 0); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 557 | shaderUniformCount.atomicCounterCount = (isAtomicCounter ? elementCount : 0); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 558 | |
| 559 | if (*location != -1) |
| 560 | { |
| 561 | *location += elementCount; |
| 562 | } |
| 563 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 564 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 565 | } |
| 566 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 567 | bool UniformLinker::checkMaxCombinedAtomicCounters(const Caps &caps, InfoLog &infoLog) |
| 568 | { |
| 569 | unsigned int atomicCounterCount = 0; |
| 570 | for (const auto &uniform : mUniforms) |
| 571 | { |
| 572 | if (IsAtomicCounterType(uniform.type) && uniform.staticUse) |
| 573 | { |
| 574 | atomicCounterCount += uniform.elementCount(); |
| 575 | if (atomicCounterCount > caps.maxCombinedAtomicCounters) |
| 576 | { |
| 577 | infoLog << "atomic counter count exceeds MAX_COMBINED_ATOMIC_COUNTERS" |
| 578 | << caps.maxCombinedAtomicCounters << ")."; |
| 579 | return false; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | return true; |
| 584 | } |
| 585 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 586 | } // namespace gl |