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 | { |
| 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 | |
| 254 | bool 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 | |
| 318 | void 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 | |
| 334 | bool UniformLinker::flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 335 | const Context *context, |
| 336 | Shader *shader, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 337 | GLuint maxUniformComponents, |
| 338 | GLuint maxTextureImageUnits, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 339 | GLuint maxImageUnits, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 340 | GLuint maxAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 341 | const std::string &componentsErrorMessage, |
| 342 | const std::string &samplerErrorMessage, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 343 | const std::string &imageErrorMessage, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 344 | const std::string &atomicCounterErrorMessage, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 345 | std::vector<LinkedUniform> &samplerUniforms, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 346 | std::vector<LinkedUniform> &imageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 347 | std::vector<LinkedUniform> &atomicCounterUniforms, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 348 | InfoLog &infoLog) |
| 349 | { |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 350 | ShaderUniformCount shaderUniformCount; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 351 | for (const sh::Uniform &uniform : shader->getUniforms(context)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 352 | { |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 353 | shaderUniformCount += |
| 354 | flattenUniform(uniform, &samplerUniforms, &imageUniforms, &atomicCounterUniforms); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 357 | if (shaderUniformCount.vectorCount > maxUniformComponents) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 358 | { |
| 359 | infoLog << componentsErrorMessage << maxUniformComponents << ")."; |
| 360 | return false; |
| 361 | } |
| 362 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 363 | if (shaderUniformCount.samplerCount > maxTextureImageUnits) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 364 | { |
| 365 | infoLog << samplerErrorMessage << maxTextureImageUnits << ")."; |
| 366 | return false; |
| 367 | } |
| 368 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 369 | if (shaderUniformCount.imageCount > maxImageUnits) |
| 370 | { |
| 371 | infoLog << imageErrorMessage << maxImageUnits << ")."; |
| 372 | return false; |
| 373 | } |
| 374 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 375 | if (shaderUniformCount.atomicCounterCount > maxAtomicCounters) |
| 376 | { |
| 377 | infoLog << atomicCounterErrorMessage << maxAtomicCounters << ")."; |
| 378 | return false; |
| 379 | } |
| 380 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 381 | return true; |
| 382 | } |
| 383 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 384 | bool UniformLinker::flattenUniformsAndCheckCaps(const Context *context, InfoLog &infoLog) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 385 | { |
| 386 | std::vector<LinkedUniform> samplerUniforms; |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 387 | std::vector<LinkedUniform> imageUniforms; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 388 | std::vector<LinkedUniform> atomicCounterUniforms; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 389 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 390 | const Caps &caps = context->getCaps(); |
| 391 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 392 | if (mState.getAttachedComputeShader()) |
| 393 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 394 | Shader *computeShader = mState.getAttachedComputeShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 395 | |
| 396 | // TODO (mradev): check whether we need finer-grained component counting |
| 397 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 398 | context, computeShader, caps.maxComputeUniformComponents / 4, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 399 | caps.maxComputeTextureImageUnits, caps.maxComputeImageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 400 | caps.maxComputeAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 401 | "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (", |
| 402 | "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 403 | "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 Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 406 | { |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | else |
| 411 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 412 | Shader *vertexShader = mState.getAttachedVertexShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 413 | |
| 414 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 415 | context, vertexShader, caps.maxVertexUniformVectors, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 416 | caps.maxVertexTextureImageUnits, caps.maxVertexImageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 417 | caps.maxVertexAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 418 | "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (", |
| 419 | "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 420 | "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 Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 423 | { |
| 424 | return false; |
| 425 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 426 | |
| 427 | Shader *fragmentShader = mState.getAttachedFragmentShader(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 428 | |
| 429 | if (!flattenUniformsAndCheckCapsForShader( |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 430 | context, fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 431 | caps.maxFragmentImageUniforms, caps.maxFragmentAtomicCounters, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 432 | "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (", |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 433 | "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", |
| 434 | "Fragment shader image count exceeds MAX_FRAGMENT_IMAGE_UNIFORMS (", |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 435 | "Fragment shader atomic counter count exceeds MAX_FRAGMENT_ATOMIC_COUNTERS (", |
| 436 | samplerUniforms, imageUniforms, atomicCounterUniforms, infoLog)) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 437 | { |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | mUniforms.insert(mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end()); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 443 | mUniforms.insert(mUniforms.end(), imageUniforms.begin(), imageUniforms.end()); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 444 | mUniforms.insert(mUniforms.end(), atomicCounterUniforms.begin(), atomicCounterUniforms.end()); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 448 | UniformLinker::ShaderUniformCount UniformLinker::flattenUniform( |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 449 | const sh::Uniform &uniform, |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 450 | std::vector<LinkedUniform> *samplerUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 451 | std::vector<LinkedUniform> *imageUniforms, |
| 452 | std::vector<LinkedUniform> *atomicCounterUniforms) |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 453 | { |
| 454 | int location = uniform.location; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 455 | ShaderUniformCount shaderUniformCount = flattenUniformImpl( |
| 456 | uniform, uniform.name, samplerUniforms, imageUniforms, atomicCounterUniforms, |
| 457 | uniform.staticUse, uniform.binding, uniform.offset, &location); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 458 | if (uniform.staticUse) |
| 459 | { |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 460 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 461 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 462 | return ShaderUniformCount(); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 465 | UniformLinker::ShaderUniformCount UniformLinker::flattenUniformImpl( |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 466 | const sh::ShaderVariable &uniform, |
| 467 | const std::string &fullName, |
| 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); |
| 489 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 490 | shaderUniformCount += |
| 491 | flattenUniformImpl(field, fieldFullName, samplerUniforms, imageUniforms, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 492 | atomicCounterUniforms, markStaticUse, -1, -1, location); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 496 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Not a struct |
| 500 | bool isSampler = IsSamplerType(uniform.type); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 501 | bool isImage = IsImageType(uniform.type); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 502 | bool isAtomicCounter = IsAtomicCounterType(uniform.type); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 503 | std::vector<gl::LinkedUniform> *uniformList = &mUniforms; |
| 504 | if (isSampler) |
| 505 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 506 | uniformList = samplerUniforms; |
| 507 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 508 | else if (isImage) |
| 509 | { |
| 510 | uniformList = imageUniforms; |
| 511 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 512 | else if (isAtomicCounter) |
| 513 | { |
| 514 | uniformList = atomicCounterUniforms; |
| 515 | } |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 516 | LinkedUniform *existingUniform = FindUniform(*uniformList, fullName); |
| 517 | if (existingUniform) |
| 518 | { |
| 519 | if (binding != -1) |
| 520 | { |
| 521 | existingUniform->binding = binding; |
| 522 | } |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 523 | if (offset != -1) |
| 524 | { |
| 525 | existingUniform->offset = offset; |
| 526 | } |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 527 | 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, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 539 | binding, -1, *location, -1, |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 540 | sh::BlockMemberInfo::getDefaultBlockInfo()); |
| 541 | linkedUniform.staticUse = markStaticUse; |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 542 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 543 | uniformList->push_back(linkedUniform); |
| 544 | } |
| 545 | |
| 546 | unsigned int elementCount = uniform.elementCount(); |
| 547 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 548 | // 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^] | 549 | // Likewise, don't count "real" uniforms towards opaque count. |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 550 | shaderUniformCount.vectorCount = |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 551 | (IsOpaqueType(uniform.type) ? 0 : (VariableRegisterCount(uniform.type) * elementCount)); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 552 | shaderUniformCount.samplerCount = (isSampler ? elementCount : 0); |
| 553 | shaderUniformCount.imageCount = (isImage ? elementCount : 0); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 554 | shaderUniformCount.atomicCounterCount = (isAtomicCounter ? elementCount : 0); |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 555 | |
| 556 | if (*location != -1) |
| 557 | { |
| 558 | *location += elementCount; |
| 559 | } |
| 560 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 561 | return shaderUniformCount; |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 562 | } |
| 563 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame^] | 564 | bool 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 Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 583 | } // namespace gl |