daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | // Program.cpp: Implements the gl::Program class. Implements GL program objects |
| 8 | // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. |
| 9 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/Program.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 11 | |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | |
Jamie Madill | 20e005b | 2017-04-07 14:19:22 -0400 | [diff] [blame] | 14 | #include "common/bitset_utils.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 15 | #include "common/debug.h" |
| 16 | #include "common/platform.h" |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 17 | #include "common/string_utils.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 18 | #include "common/utilities.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 19 | #include "compiler/translator/blocklayout.h" |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 20 | #include "libANGLE/Context.h" |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 21 | #include "libANGLE/MemoryProgramCache.h" |
Jamie Madill | 7af0de5 | 2017-11-06 17:09:33 -0500 | [diff] [blame] | 22 | #include "libANGLE/ProgramLinkedResources.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 23 | #include "libANGLE/ResourceManager.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 24 | #include "libANGLE/Uniform.h" |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 25 | #include "libANGLE/VaryingPacking.h" |
| 26 | #include "libANGLE/features.h" |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 27 | #include "libANGLE/histogram_macros.h" |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 28 | #include "libANGLE/queryconversions.h" |
| 29 | #include "libANGLE/renderer/GLImplFactory.h" |
| 30 | #include "libANGLE/renderer/ProgramImpl.h" |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 31 | #include "platform/Platform.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 32 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 33 | namespace gl |
| 34 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 35 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 36 | namespace |
| 37 | { |
| 38 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 39 | // This simplified cast function doesn't need to worry about advanced concepts like |
| 40 | // depth range values, or casting to bool. |
| 41 | template <typename DestT, typename SrcT> |
| 42 | DestT UniformStateQueryCast(SrcT value); |
| 43 | |
| 44 | // From-Float-To-Integer Casts |
| 45 | template <> |
| 46 | GLint UniformStateQueryCast(GLfloat value) |
| 47 | { |
| 48 | return clampCast<GLint>(roundf(value)); |
| 49 | } |
| 50 | |
| 51 | template <> |
| 52 | GLuint UniformStateQueryCast(GLfloat value) |
| 53 | { |
| 54 | return clampCast<GLuint>(roundf(value)); |
| 55 | } |
| 56 | |
| 57 | // From-Integer-to-Integer Casts |
| 58 | template <> |
| 59 | GLint UniformStateQueryCast(GLuint value) |
| 60 | { |
| 61 | return clampCast<GLint>(value); |
| 62 | } |
| 63 | |
| 64 | template <> |
| 65 | GLuint UniformStateQueryCast(GLint value) |
| 66 | { |
| 67 | return clampCast<GLuint>(value); |
| 68 | } |
| 69 | |
| 70 | // From-Boolean-to-Anything Casts |
| 71 | template <> |
| 72 | GLfloat UniformStateQueryCast(GLboolean value) |
| 73 | { |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 74 | return (ConvertToBool(value) ? 1.0f : 0.0f); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | template <> |
| 78 | GLint UniformStateQueryCast(GLboolean value) |
| 79 | { |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 80 | return (ConvertToBool(value) ? 1 : 0); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | template <> |
| 84 | GLuint UniformStateQueryCast(GLboolean value) |
| 85 | { |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 86 | return (ConvertToBool(value) ? 1u : 0u); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Default to static_cast |
| 90 | template <typename DestT, typename SrcT> |
| 91 | DestT UniformStateQueryCast(SrcT value) |
| 92 | { |
| 93 | return static_cast<DestT>(value); |
| 94 | } |
| 95 | |
| 96 | template <typename SrcT, typename DestT> |
| 97 | void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components) |
| 98 | { |
| 99 | for (int comp = 0; comp < components; ++comp) |
| 100 | { |
| 101 | // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint) |
| 102 | // Don't use SrcT stride directly since GLboolean has a stride of 1 byte. |
| 103 | size_t offset = comp * 4; |
| 104 | const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]); |
| 105 | dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer); |
| 106 | } |
| 107 | } |
| 108 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 109 | template <typename VarT> |
| 110 | GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name) |
| 111 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 112 | std::string nameAsArrayName = name + "[0]"; |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 113 | for (size_t index = 0; index < list.size(); index++) |
| 114 | { |
| 115 | const VarT &resource = list[index]; |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 116 | if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName)) |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 117 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 118 | return static_cast<GLuint>(index); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | return GL_INVALID_INDEX; |
| 123 | } |
| 124 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 125 | template <typename VarT> |
| 126 | GLint GetVariableLocation(const std::vector<VarT> &list, |
| 127 | const std::vector<VariableLocation> &locationList, |
| 128 | const std::string &name) |
| 129 | { |
| 130 | size_t nameLengthWithoutArrayIndex; |
| 131 | unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex); |
| 132 | |
| 133 | for (size_t location = 0u; location < locationList.size(); ++location) |
| 134 | { |
| 135 | const VariableLocation &variableLocation = locationList[location]; |
| 136 | if (!variableLocation.used()) |
| 137 | { |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | const VarT &variable = list[variableLocation.index]; |
| 142 | |
| 143 | if (angle::BeginsWith(variable.name, name)) |
| 144 | { |
| 145 | if (name.length() == variable.name.length()) |
| 146 | { |
| 147 | ASSERT(name == variable.name); |
| 148 | // GLES 3.1 November 2016 page 87. |
| 149 | // The string exactly matches the name of the active variable. |
| 150 | return static_cast<GLint>(location); |
| 151 | } |
| 152 | if (name.length() + 3u == variable.name.length() && variable.isArray()) |
| 153 | { |
| 154 | ASSERT(name + "[0]" == variable.name); |
| 155 | // The string identifies the base name of an active array, where the string would |
| 156 | // exactly match the name of the variable if the suffix "[0]" were appended to the |
| 157 | // string. |
| 158 | return static_cast<GLint>(location); |
| 159 | } |
| 160 | } |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 161 | if (variable.isArray() && variableLocation.arrayIndex == arrayIndex && |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 162 | nameLengthWithoutArrayIndex + 3u == variable.name.length() && |
| 163 | angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex)) |
| 164 | { |
| 165 | ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name); |
| 166 | // The string identifies an active element of the array, where the string ends with the |
| 167 | // concatenation of the "[" character, an integer (with no "+" sign, extra leading |
| 168 | // zeroes, or whitespace) identifying an array element, and the "]" character, the |
| 169 | // integer is less than the number of active elements of the array variable, and where |
| 170 | // the string would exactly match the enumerated name of the array if the decimal |
| 171 | // integer were replaced with zero. |
| 172 | return static_cast<GLint>(location); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return -1; |
| 177 | } |
| 178 | |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 179 | void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length) |
| 180 | { |
| 181 | ASSERT(bufSize > 0); |
| 182 | strncpy(buffer, string.c_str(), bufSize); |
| 183 | buffer[bufSize - 1] = '\0'; |
| 184 | |
| 185 | if (length) |
| 186 | { |
| 187 | *length = static_cast<GLsizei>(strlen(buffer)); |
| 188 | } |
| 189 | } |
| 190 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 191 | bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name) |
| 192 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 193 | std::vector<unsigned int> subscripts; |
| 194 | std::string baseName = ParseResourceName(name, &subscripts); |
| 195 | for (auto nameInSet : nameSet) |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 196 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 197 | std::vector<unsigned int> arrayIndices; |
| 198 | std::string arrayName = ParseResourceName(nameInSet, &arrayIndices); |
| 199 | if (baseName == arrayName && |
| 200 | (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices)) |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 201 | { |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 208 | bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks, |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 209 | const std::vector<sh::InterfaceBlock> &interfaceBlocks, |
| 210 | const std::string &errorMessage, |
| 211 | InfoLog &infoLog) |
| 212 | { |
| 213 | GLuint blockCount = 0; |
| 214 | for (const sh::InterfaceBlock &block : interfaceBlocks) |
| 215 | { |
Olli Etuaho | 107c724 | 2018-03-20 15:45:35 +0200 | [diff] [blame] | 216 | if (block.active || block.layout != sh::BLOCKLAYOUT_PACKED) |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 217 | { |
| 218 | blockCount += (block.arraySize ? block.arraySize : 1); |
| 219 | if (blockCount > maxInterfaceBlocks) |
| 220 | { |
| 221 | infoLog << errorMessage << maxInterfaceBlocks << ")"; |
| 222 | return false; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 229 | GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name) |
| 230 | { |
| 231 | std::vector<unsigned int> subscripts; |
| 232 | std::string baseName = ParseResourceName(name, &subscripts); |
| 233 | |
| 234 | unsigned int numBlocks = static_cast<unsigned int>(list.size()); |
| 235 | for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++) |
| 236 | { |
| 237 | const auto &block = list[blockIndex]; |
| 238 | if (block.name == baseName) |
| 239 | { |
| 240 | const bool arrayElementZero = |
| 241 | (subscripts.empty() && (!block.isArray || block.arrayElement == 0)); |
| 242 | const bool arrayElementMatches = |
| 243 | (subscripts.size() == 1 && subscripts[0] == block.arrayElement); |
| 244 | if (arrayElementMatches || arrayElementZero) |
| 245 | { |
| 246 | return blockIndex; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return GL_INVALID_INDEX; |
| 252 | } |
| 253 | |
| 254 | void GetInterfaceBlockName(const GLuint index, |
| 255 | const std::vector<InterfaceBlock> &list, |
| 256 | GLsizei bufSize, |
| 257 | GLsizei *length, |
| 258 | GLchar *name) |
| 259 | { |
| 260 | ASSERT(index < list.size()); |
| 261 | |
| 262 | const auto &block = list[index]; |
| 263 | |
| 264 | if (bufSize > 0) |
| 265 | { |
| 266 | std::string blockName = block.name; |
| 267 | |
| 268 | if (block.isArray) |
| 269 | { |
| 270 | blockName += ArrayString(block.arrayElement); |
| 271 | } |
| 272 | CopyStringToBuffer(name, blockName, bufSize, length); |
| 273 | } |
| 274 | } |
| 275 | |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 276 | void InitUniformBlockLinker(const gl::Context *context, |
| 277 | const ProgramState &state, |
| 278 | UniformBlockLinker *blockLinker) |
| 279 | { |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 280 | for (ShaderType shaderType : AllShaderTypes()) |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 281 | { |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 282 | Shader *shader = state.getAttachedShader(shaderType); |
| 283 | if (shader) |
| 284 | { |
| 285 | blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks(context)); |
| 286 | } |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | void InitShaderStorageBlockLinker(const gl::Context *context, |
| 291 | const ProgramState &state, |
| 292 | ShaderStorageBlockLinker *blockLinker) |
| 293 | { |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 294 | for (ShaderType shaderType : AllShaderTypes()) |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 295 | { |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 296 | Shader *shader = state.getAttachedShader(shaderType); |
| 297 | if (shader != nullptr) |
| 298 | { |
| 299 | blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks(context)); |
| 300 | } |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
jchen10 | 8225e73 | 2017-11-14 16:29:03 +0800 | [diff] [blame] | 304 | // Find the matching varying or field by name. |
| 305 | const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings, |
| 306 | const std::string &name) |
| 307 | { |
| 308 | const sh::ShaderVariable *var = nullptr; |
| 309 | for (const auto &ref : varyings) |
| 310 | { |
| 311 | const sh::Varying *varying = ref.second.get(); |
| 312 | if (varying->name == name) |
| 313 | { |
| 314 | var = varying; |
| 315 | break; |
| 316 | } |
| 317 | var = FindShaderVarField(*varying, name); |
| 318 | if (var != nullptr) |
| 319 | { |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | return var; |
| 324 | } |
| 325 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 326 | void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName) |
| 327 | { |
| 328 | ASSERT(mismatchedFieldName); |
| 329 | if (mismatchedFieldName->empty()) |
| 330 | { |
| 331 | *mismatchedFieldName = parentName; |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | std::ostringstream stream; |
| 336 | stream << parentName << "." << *mismatchedFieldName; |
| 337 | *mismatchedFieldName = stream.str(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | const char *GetLinkMismatchErrorString(LinkMismatchError linkError) |
| 342 | { |
| 343 | switch (linkError) |
| 344 | { |
| 345 | case LinkMismatchError::TYPE_MISMATCH: |
| 346 | return "Type"; |
| 347 | case LinkMismatchError::ARRAY_SIZE_MISMATCH: |
| 348 | return "Array size"; |
| 349 | case LinkMismatchError::PRECISION_MISMATCH: |
| 350 | return "Precision"; |
| 351 | case LinkMismatchError::STRUCT_NAME_MISMATCH: |
| 352 | return "Structure name"; |
| 353 | case LinkMismatchError::FIELD_NUMBER_MISMATCH: |
| 354 | return "Field number"; |
| 355 | case LinkMismatchError::FIELD_NAME_MISMATCH: |
| 356 | return "Field name"; |
| 357 | |
| 358 | case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH: |
| 359 | return "Interpolation type"; |
| 360 | case LinkMismatchError::INVARIANCE_MISMATCH: |
| 361 | return "Invariance"; |
| 362 | |
| 363 | case LinkMismatchError::BINDING_MISMATCH: |
| 364 | return "Binding layout qualifier"; |
| 365 | case LinkMismatchError::LOCATION_MISMATCH: |
| 366 | return "Location layout qualifier"; |
| 367 | case LinkMismatchError::OFFSET_MISMATCH: |
| 368 | return "Offset layout qualilfier"; |
| 369 | |
| 370 | case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH: |
| 371 | return "Layout qualifier"; |
| 372 | case LinkMismatchError::MATRIX_PACKING_MISMATCH: |
| 373 | return "Matrix Packing"; |
| 374 | default: |
| 375 | UNREACHABLE(); |
| 376 | return ""; |
| 377 | } |
| 378 | } |
| 379 | |
Jiawei Shao | 1c08cbb | 2018-03-15 15:11:56 +0800 | [diff] [blame] | 380 | LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1, |
| 381 | const sh::InterfaceBlockField &blockField2, |
| 382 | bool webglCompatibility, |
| 383 | std::string *mismatchedBlockFieldName) |
| 384 | { |
| 385 | if (blockField1.name != blockField2.name) |
| 386 | { |
| 387 | return LinkMismatchError::FIELD_NAME_MISMATCH; |
| 388 | } |
| 389 | |
| 390 | // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287. |
| 391 | LinkMismatchError linkError = Program::LinkValidateVariablesBase( |
| 392 | blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName); |
| 393 | if (linkError != LinkMismatchError::NO_MISMATCH) |
| 394 | { |
| 395 | AddParentPrefix(blockField1.name, mismatchedBlockFieldName); |
| 396 | return linkError; |
| 397 | } |
| 398 | |
| 399 | if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout) |
| 400 | { |
| 401 | AddParentPrefix(blockField1.name, mismatchedBlockFieldName); |
| 402 | return LinkMismatchError::MATRIX_PACKING_MISMATCH; |
| 403 | } |
| 404 | |
| 405 | return LinkMismatchError::NO_MISMATCH; |
| 406 | } |
| 407 | |
| 408 | LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1, |
| 409 | const sh::InterfaceBlock &interfaceBlock2, |
| 410 | bool webglCompatibility, |
| 411 | std::string *mismatchedBlockFieldName) |
| 412 | { |
| 413 | // validate blocks for the same member types |
| 414 | if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size()) |
| 415 | { |
| 416 | return LinkMismatchError::FIELD_NUMBER_MISMATCH; |
| 417 | } |
| 418 | if (interfaceBlock1.arraySize != interfaceBlock2.arraySize) |
| 419 | { |
| 420 | return LinkMismatchError::ARRAY_SIZE_MISMATCH; |
| 421 | } |
| 422 | if (interfaceBlock1.layout != interfaceBlock2.layout || |
| 423 | interfaceBlock1.binding != interfaceBlock2.binding) |
| 424 | { |
| 425 | return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH; |
| 426 | } |
| 427 | const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size()); |
| 428 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++) |
| 429 | { |
| 430 | const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex]; |
| 431 | const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex]; |
| 432 | |
| 433 | LinkMismatchError linkError = LinkValidateInterfaceBlockFields( |
| 434 | member1, member2, webglCompatibility, mismatchedBlockFieldName); |
| 435 | if (linkError != LinkMismatchError::NO_MISMATCH) |
| 436 | { |
| 437 | return linkError; |
| 438 | } |
| 439 | } |
| 440 | return LinkMismatchError::NO_MISMATCH; |
| 441 | } |
| 442 | |
| 443 | bool ValidateGraphicsInterfaceBlocks(const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks, |
| 444 | const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks, |
| 445 | InfoLog &infoLog, |
| 446 | bool webglCompatibility, |
| 447 | sh::BlockType blockType, |
| 448 | GLuint maxCombinedInterfaceBlocks) |
| 449 | { |
| 450 | // Check that interface blocks defined in the vertex and fragment shaders are identical |
| 451 | typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap; |
| 452 | InterfaceBlockMap linkedInterfaceBlocks; |
| 453 | GLuint blockCount = 0; |
| 454 | |
| 455 | for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks) |
| 456 | { |
| 457 | linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock; |
| 458 | if (IsActiveInterfaceBlock(vertexInterfaceBlock)) |
| 459 | { |
| 460 | blockCount += std::max(vertexInterfaceBlock.arraySize, 1u); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks) |
| 465 | { |
| 466 | auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name); |
| 467 | if (entry != linkedInterfaceBlocks.end()) |
| 468 | { |
| 469 | const sh::InterfaceBlock &vertexInterfaceBlock = *(entry->second); |
| 470 | std::string mismatchedBlockFieldName; |
| 471 | LinkMismatchError linkError = |
| 472 | AreMatchingInterfaceBlocks(vertexInterfaceBlock, fragmentInterfaceBlock, |
| 473 | webglCompatibility, &mismatchedBlockFieldName); |
| 474 | if (linkError != LinkMismatchError::NO_MISMATCH) |
| 475 | { |
| 476 | LogLinkMismatch(infoLog, fragmentInterfaceBlock.name, "interface block", linkError, |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 477 | mismatchedBlockFieldName, ShaderType::Vertex, ShaderType::Fragment); |
Jiawei Shao | 1c08cbb | 2018-03-15 15:11:56 +0800 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // [OpenGL ES 3.1] Chapter 7.6.2 Page 105: |
| 483 | // If a uniform block is used by multiple shader stages, each such use counts separately |
| 484 | // against this combined limit. |
| 485 | // [OpenGL ES 3.1] Chapter 7.8 Page 111: |
| 486 | // If a shader storage block in a program is referenced by multiple shaders, each such |
| 487 | // reference counts separately against this combined limit. |
| 488 | if (IsActiveInterfaceBlock(fragmentInterfaceBlock)) |
| 489 | { |
| 490 | blockCount += std::max(fragmentInterfaceBlock.arraySize, 1u); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (blockCount > maxCombinedInterfaceBlocks) |
| 495 | { |
| 496 | switch (blockType) |
| 497 | { |
| 498 | case sh::BlockType::BLOCK_UNIFORM: |
| 499 | infoLog << "The sum of the number of active uniform blocks exceeds " |
| 500 | "MAX_COMBINED_UNIFORM_BLOCKS (" |
| 501 | << maxCombinedInterfaceBlocks << ")."; |
| 502 | break; |
| 503 | case sh::BlockType::BLOCK_BUFFER: |
| 504 | infoLog << "The sum of the number of active shader storage blocks exceeds " |
| 505 | "MAX_COMBINED_SHADER_STORAGE_BLOCKS (" |
| 506 | << maxCombinedInterfaceBlocks << ")."; |
| 507 | break; |
| 508 | default: |
| 509 | UNREACHABLE(); |
| 510 | } |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 516 | } // anonymous namespace |
| 517 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 518 | const char *const g_fakepath = "C:\\fakepath"; |
| 519 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 520 | // InfoLog implementation. |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 521 | InfoLog::InfoLog() |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 522 | { |
| 523 | } |
| 524 | |
| 525 | InfoLog::~InfoLog() |
| 526 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 529 | size_t InfoLog::getLength() const |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 530 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 531 | if (!mLazyStream) |
| 532 | { |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | const std::string &logString = mLazyStream->str(); |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 537 | return logString.empty() ? 0 : logString.length() + 1; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 540 | void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 541 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 542 | size_t index = 0; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 543 | |
| 544 | if (bufSize > 0) |
| 545 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 546 | const std::string logString(str()); |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 547 | |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 548 | if (!logString.empty()) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 549 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 550 | index = std::min(static_cast<size_t>(bufSize) - 1, logString.length()); |
| 551 | memcpy(infoLog, logString.c_str(), index); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | infoLog[index] = '\0'; |
| 555 | } |
| 556 | |
| 557 | if (length) |
| 558 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 559 | *length = static_cast<GLsizei>(index); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | // append a santized message to the program info log. |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 564 | // The D3D compiler includes a fake file path in some of the warning or error |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 565 | // messages, so lets remove all occurrences of this fake file path from the log. |
| 566 | void InfoLog::appendSanitized(const char *message) |
| 567 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 568 | ensureInitialized(); |
| 569 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 570 | std::string msg(message); |
| 571 | |
| 572 | size_t found; |
| 573 | do |
| 574 | { |
| 575 | found = msg.find(g_fakepath); |
| 576 | if (found != std::string::npos) |
| 577 | { |
| 578 | msg.erase(found, strlen(g_fakepath)); |
| 579 | } |
| 580 | } |
| 581 | while (found != std::string::npos); |
| 582 | |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 583 | *mLazyStream << message << std::endl; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 584 | } |
| 585 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 586 | void InfoLog::reset() |
| 587 | { |
Jiawei Shao | 02f1523 | 2017-12-27 10:10:28 +0800 | [diff] [blame] | 588 | if (mLazyStream) |
| 589 | { |
| 590 | mLazyStream.reset(nullptr); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | bool InfoLog::empty() const |
| 595 | { |
| 596 | if (!mLazyStream) |
| 597 | { |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | return mLazyStream->rdbuf()->in_avail() == 0; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 604 | void LogLinkMismatch(InfoLog &infoLog, |
| 605 | const std::string &variableName, |
| 606 | const char *variableType, |
| 607 | LinkMismatchError linkError, |
| 608 | const std::string &mismatchedStructOrBlockFieldName, |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 609 | ShaderType shaderType1, |
| 610 | ShaderType shaderType2) |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 611 | { |
| 612 | std::ostringstream stream; |
| 613 | stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '" |
| 614 | << variableName; |
| 615 | |
| 616 | if (!mismatchedStructOrBlockFieldName.empty()) |
| 617 | { |
| 618 | stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName; |
| 619 | } |
| 620 | |
| 621 | stream << "' differ between " << GetShaderTypeString(shaderType1) << " and " |
| 622 | << GetShaderTypeString(shaderType2) << " shaders."; |
| 623 | |
| 624 | infoLog << stream.str(); |
| 625 | } |
| 626 | |
Jiawei Shao | 1c08cbb | 2018-03-15 15:11:56 +0800 | [diff] [blame] | 627 | bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock) |
| 628 | { |
| 629 | // Only 'packed' blocks are allowed to be considered inactive. |
Olli Etuaho | 107c724 | 2018-03-20 15:45:35 +0200 | [diff] [blame] | 630 | return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED; |
Jiawei Shao | 1c08cbb | 2018-03-15 15:11:56 +0800 | [diff] [blame] | 631 | } |
| 632 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 633 | // VariableLocation implementation. |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 634 | VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 635 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 636 | } |
| 637 | |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 638 | VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index) |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 639 | : arrayIndex(arrayIndex), index(index), ignored(false) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 640 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 641 | ASSERT(arrayIndex != GL_INVALID_INDEX); |
| 642 | } |
| 643 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 644 | // SamplerBindings implementation. |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 645 | SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced) |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 646 | : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced) |
| 647 | { |
| 648 | } |
| 649 | |
| 650 | SamplerBinding::SamplerBinding(const SamplerBinding &other) = default; |
| 651 | |
| 652 | SamplerBinding::~SamplerBinding() = default; |
| 653 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 654 | // ProgramBindings implementation. |
| 655 | ProgramBindings::ProgramBindings() |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 656 | { |
| 657 | } |
| 658 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 659 | ProgramBindings::~ProgramBindings() |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 660 | { |
| 661 | } |
| 662 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 663 | void ProgramBindings::bindLocation(GLuint index, const std::string &name) |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 664 | { |
| 665 | mBindings[name] = index; |
| 666 | } |
| 667 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 668 | int ProgramBindings::getBinding(const std::string &name) const |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 669 | { |
| 670 | auto iter = mBindings.find(name); |
| 671 | return (iter != mBindings.end()) ? iter->second : -1; |
| 672 | } |
| 673 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 674 | ProgramBindings::const_iterator ProgramBindings::begin() const |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 675 | { |
| 676 | return mBindings.begin(); |
| 677 | } |
| 678 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 679 | ProgramBindings::const_iterator ProgramBindings::end() const |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 680 | { |
| 681 | return mBindings.end(); |
| 682 | } |
| 683 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 684 | // ImageBinding implementation. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 685 | ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0) |
| 686 | { |
| 687 | } |
| 688 | ImageBinding::ImageBinding(GLuint imageUnit, size_t count) |
| 689 | { |
| 690 | for (size_t index = 0; index < count; ++index) |
| 691 | { |
| 692 | boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index)); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | ImageBinding::ImageBinding(const ImageBinding &other) = default; |
| 697 | |
| 698 | ImageBinding::~ImageBinding() = default; |
| 699 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 700 | // ProgramState implementation. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 701 | ProgramState::ProgramState() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 702 | : mLabel(), |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 703 | mAttachedShaders({}), |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 704 | mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS), |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 705 | mMaxActiveAttribLocation(0), |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 706 | mSamplerUniformRange(0, 0), |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 707 | mImageUniformRange(0, 0), |
| 708 | mAtomicCounterUniformRange(0, 0), |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 709 | mBinaryRetrieveableHint(false), |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 710 | mNumViews(-1), |
| 711 | // [GL_EXT_geometry_shader] Table 20.22 |
| 712 | mGeometryShaderInputPrimitiveType(GL_TRIANGLES), |
| 713 | mGeometryShaderOutputPrimitiveType(GL_TRIANGLE_STRIP), |
| 714 | mGeometryShaderInvocations(1), |
| 715 | mGeometryShaderMaxVertices(0) |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 716 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 717 | mComputeShaderLocalSize.fill(1); |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 720 | ProgramState::~ProgramState() |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 721 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 722 | ASSERT(!hasAttachedShader()); |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 725 | const std::string &ProgramState::getLabel() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 726 | { |
| 727 | return mLabel; |
| 728 | } |
| 729 | |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 730 | Shader *ProgramState::getAttachedShader(ShaderType shaderType) const |
| 731 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 732 | ASSERT(shaderType != ShaderType::InvalidEnum); |
| 733 | return mAttachedShaders[shaderType]; |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 734 | } |
| 735 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 736 | GLuint ProgramState::getUniformIndexFromName(const std::string &name) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 737 | { |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 738 | return GetResourceIndexFromName(mUniforms, name); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 739 | } |
| 740 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 741 | GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const |
| 742 | { |
| 743 | return GetResourceIndexFromName(mBufferVariables, name); |
| 744 | } |
| 745 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 746 | GLuint ProgramState::getUniformIndexFromLocation(GLint location) const |
| 747 | { |
| 748 | ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size()); |
| 749 | return mUniformLocations[location].index; |
| 750 | } |
| 751 | |
| 752 | Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const |
| 753 | { |
| 754 | GLuint index = getUniformIndexFromLocation(location); |
| 755 | if (!isSamplerUniformIndex(index)) |
| 756 | { |
| 757 | return Optional<GLuint>::Invalid(); |
| 758 | } |
| 759 | |
| 760 | return getSamplerIndexFromUniformIndex(index); |
| 761 | } |
| 762 | |
| 763 | bool ProgramState::isSamplerUniformIndex(GLuint index) const |
| 764 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 765 | return mSamplerUniformRange.contains(index); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const |
| 769 | { |
| 770 | ASSERT(isSamplerUniformIndex(uniformIndex)); |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 771 | return uniformIndex - mSamplerUniformRange.low(); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 772 | } |
| 773 | |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 774 | GLuint ProgramState::getAttributeLocation(const std::string &name) const |
| 775 | { |
| 776 | for (const sh::Attribute &attribute : mAttributes) |
| 777 | { |
| 778 | if (attribute.name == name) |
| 779 | { |
| 780 | return attribute.location; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return static_cast<GLuint>(-1); |
| 785 | } |
| 786 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 787 | bool ProgramState::hasAttachedShader() const |
| 788 | { |
| 789 | for (const Shader *shader : mAttachedShaders) |
| 790 | { |
| 791 | if (shader) |
| 792 | { |
| 793 | return true; |
| 794 | } |
| 795 | } |
| 796 | return false; |
| 797 | } |
| 798 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 799 | Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle) |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 800 | : mProgram(factory->createProgram(mState)), |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 801 | mValidated(false), |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 802 | mLinked(false), |
| 803 | mDeleteStatus(false), |
| 804 | mRefCount(0), |
| 805 | mResourceManager(manager), |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 806 | mHandle(handle) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 807 | { |
| 808 | ASSERT(mProgram); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 809 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 810 | unlink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | Program::~Program() |
| 814 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 815 | ASSERT(!mProgram); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 818 | void Program::onDestroy(const Context *context) |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 819 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 820 | for (ShaderType shaderType : AllShaderTypes()) |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 821 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 822 | if (mState.mAttachedShaders[shaderType]) |
| 823 | { |
| 824 | mState.mAttachedShaders[shaderType]->release(context); |
| 825 | mState.mAttachedShaders[shaderType] = nullptr; |
| 826 | } |
Jiawei Shao | 89be29a | 2017-11-06 14:36:45 +0800 | [diff] [blame] | 827 | } |
| 828 | |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 829 | // TODO(jmadill): Handle error in the Context. |
| 830 | ANGLE_SWALLOW_ERR(mProgram->destroy(context)); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 831 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 832 | ASSERT(!mState.hasAttachedShader()); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 833 | SafeDelete(mProgram); |
| 834 | |
| 835 | delete this; |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 836 | } |
| 837 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 838 | void Program::setLabel(const std::string &label) |
| 839 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 840 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | const std::string &Program::getLabel() const |
| 844 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 845 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Jamie Madill | ef300b1 | 2016-10-07 15:12:09 -0400 | [diff] [blame] | 848 | void Program::attachShader(Shader *shader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 849 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 850 | ShaderType shaderType = shader->getType(); |
| 851 | ASSERT(shaderType != ShaderType::InvalidEnum); |
| 852 | |
| 853 | mState.mAttachedShaders[shaderType] = shader; |
| 854 | mState.mAttachedShaders[shaderType]->addRef(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Jamie Madill | c1d770e | 2017-04-13 17:31:24 -0400 | [diff] [blame] | 857 | void Program::detachShader(const Context *context, Shader *shader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 858 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 859 | ShaderType shaderType = shader->getType(); |
| 860 | ASSERT(shaderType != ShaderType::InvalidEnum); |
| 861 | |
| 862 | ASSERT(mState.mAttachedShaders[shaderType] == shader); |
| 863 | shader->release(context); |
| 864 | mState.mAttachedShaders[shaderType] = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 865 | } |
| 866 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 867 | int Program::getAttachedShadersCount() const |
| 868 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 869 | int numAttachedShaders = 0; |
| 870 | for (const Shader *shader : mState.mAttachedShaders) |
| 871 | { |
| 872 | if (shader) |
| 873 | { |
| 874 | ++numAttachedShaders; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | return numAttachedShaders; |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 881 | const Shader *Program::getAttachedShader(ShaderType shaderType) const |
| 882 | { |
| 883 | return mState.getAttachedShader(shaderType); |
| 884 | } |
| 885 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 886 | void Program::bindAttributeLocation(GLuint index, const char *name) |
| 887 | { |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 888 | mAttributeBindings.bindLocation(index, name); |
| 889 | } |
| 890 | |
| 891 | void Program::bindUniformLocation(GLuint index, const char *name) |
| 892 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 893 | mUniformLocationBindings.bindLocation(index, name); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 896 | void Program::bindFragmentInputLocation(GLint index, const char *name) |
| 897 | { |
| 898 | mFragmentInputBindings.bindLocation(index, name); |
| 899 | } |
| 900 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 901 | BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 902 | { |
| 903 | BindingInfo ret; |
| 904 | ret.type = GL_NONE; |
| 905 | ret.valid = false; |
| 906 | |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 907 | Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 908 | ASSERT(fragmentShader); |
| 909 | |
| 910 | // Find the actual fragment shader varying we're interested in |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 911 | const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 912 | |
| 913 | for (const auto &binding : mFragmentInputBindings) |
| 914 | { |
| 915 | if (binding.second != static_cast<GLuint>(index)) |
| 916 | continue; |
| 917 | |
| 918 | ret.valid = true; |
| 919 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 920 | size_t nameLengthWithoutArrayIndex; |
| 921 | unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 922 | |
| 923 | for (const auto &in : inputs) |
| 924 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 925 | if (in.name.length() == nameLengthWithoutArrayIndex && |
| 926 | angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex)) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 927 | { |
| 928 | if (in.isArray()) |
| 929 | { |
| 930 | // The client wants to bind either "name" or "name[0]". |
| 931 | // GL ES 3.1 spec refers to active array names with language such as: |
| 932 | // "if the string identifies the base name of an active array, where the |
| 933 | // string would exactly match the name of the variable if the suffix "[0]" |
| 934 | // were appended to the string". |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 935 | if (arrayIndex == GL_INVALID_INDEX) |
| 936 | arrayIndex = 0; |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 937 | |
Corentin Wallez | 054f7ed | 2016-09-20 17:15:59 -0400 | [diff] [blame] | 938 | ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]"; |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 939 | } |
| 940 | else |
| 941 | { |
| 942 | ret.name = in.mappedName; |
| 943 | } |
| 944 | ret.type = in.type; |
| 945 | return ret; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | return ret; |
| 951 | } |
| 952 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 953 | void Program::pathFragmentInputGen(const Context *context, |
| 954 | GLint index, |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 955 | GLenum genMode, |
| 956 | GLint components, |
| 957 | const GLfloat *coeffs) |
| 958 | { |
| 959 | // If the location is -1 then the command is silently ignored |
| 960 | if (index == -1) |
| 961 | return; |
| 962 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 963 | const auto &binding = getFragmentInputBindingInfo(context, index); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 964 | |
| 965 | // If the input doesn't exist then then the command is silently ignored |
| 966 | // This could happen through optimization for example, the shader translator |
| 967 | // decides that a variable is not actually being used and optimizes it away. |
| 968 | if (binding.name.empty()) |
| 969 | return; |
| 970 | |
| 971 | mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs); |
| 972 | } |
| 973 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 974 | // The attached shaders are checked for linking errors by matching up their variables. |
| 975 | // Uniform, input and output variables get collected. |
| 976 | // The code gets compiled into binaries. |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 977 | Error Program::link(const gl::Context *context) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 978 | { |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 979 | const auto &data = context->getContextState(); |
| 980 | |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 981 | auto *platform = ANGLEPlatformCurrent(); |
| 982 | double startTime = platform->currentTime(platform); |
| 983 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 984 | unlink(); |
Jamie Madill | 6bc264a | 2018-03-31 15:36:05 -0400 | [diff] [blame] | 985 | mInfoLog.reset(); |
| 986 | |
| 987 | // Validate we have properly attached shaders before checking the cache. |
| 988 | if (!linkValidateShaders(context, mInfoLog)) |
| 989 | { |
| 990 | return NoError(); |
| 991 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 992 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 993 | ProgramHash programHash; |
Jamie Madill | 6bc264a | 2018-03-31 15:36:05 -0400 | [diff] [blame] | 994 | MemoryProgramCache *cache = context->getMemoryProgramCache(); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 995 | if (cache) |
| 996 | { |
| 997 | ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked); |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 998 | ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | if (mLinked) |
| 1002 | { |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 1003 | double delta = platform->currentTime(platform) - startTime; |
| 1004 | int us = static_cast<int>(delta * 1000000.0); |
| 1005 | ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 1006 | return NoError(); |
| 1007 | } |
| 1008 | |
| 1009 | // Cache load failed, fall through to normal linking. |
| 1010 | unlink(); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1011 | |
Jamie Madill | 6bc264a | 2018-03-31 15:36:05 -0400 | [diff] [blame] | 1012 | // Re-link shaders after the unlink call. |
| 1013 | ASSERT(linkValidateShaders(context, mInfoLog)); |
Yuly Novikov | cfa48d3 | 2016-06-15 22:14:36 -0400 | [diff] [blame] | 1014 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1015 | if (mState.mAttachedShaders[ShaderType::Compute]) |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 1016 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1017 | if (!linkUniforms(context, mInfoLog, mUniformLocationBindings)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1018 | { |
| 1019 | return NoError(); |
| 1020 | } |
| 1021 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1022 | if (!linkInterfaceBlocks(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1023 | { |
| 1024 | return NoError(); |
| 1025 | } |
| 1026 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1027 | ProgramLinkedResources resources = { |
| 1028 | {0, PackMode::ANGLE_RELAXED}, |
| 1029 | {&mState.mUniformBlocks, &mState.mUniforms}, |
Jiajia Qin | 94f1e89 | 2017-11-20 12:14:32 +0800 | [diff] [blame] | 1030 | {&mState.mShaderStorageBlocks, &mState.mBufferVariables}, |
| 1031 | {&mState.mAtomicCounterBuffers}}; |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 1032 | |
| 1033 | InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker); |
| 1034 | InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker); |
| 1035 | |
| 1036 | ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked); |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 1037 | if (!mLinked) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1038 | { |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 1039 | return NoError(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | else |
| 1043 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1044 | if (!linkAttributes(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1045 | { |
| 1046 | return NoError(); |
| 1047 | } |
| 1048 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1049 | if (!linkVaryings(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1050 | { |
| 1051 | return NoError(); |
| 1052 | } |
| 1053 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1054 | if (!linkUniforms(context, mInfoLog, mUniformLocationBindings)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1055 | { |
| 1056 | return NoError(); |
| 1057 | } |
| 1058 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1059 | if (!linkInterfaceBlocks(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1060 | { |
| 1061 | return NoError(); |
| 1062 | } |
| 1063 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 1064 | if (!linkValidateGlobalNames(context, mInfoLog)) |
| 1065 | { |
| 1066 | return NoError(); |
| 1067 | } |
| 1068 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1069 | const auto &mergedVaryings = getMergedVaryings(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1070 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1071 | ASSERT(mState.mAttachedShaders[ShaderType::Vertex]); |
| 1072 | mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews(context); |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 1073 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1074 | linkOutputVariables(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1075 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1076 | // Map the varyings to the register file |
| 1077 | // In WebGL, we use a slightly different handling for packing variables. |
Jamie Madill | 61d5325 | 2018-01-31 14:49:24 -0500 | [diff] [blame] | 1078 | gl::PackMode packMode = PackMode::ANGLE_RELAXED; |
| 1079 | if (data.getLimitations().noFlexibleVaryingPacking) |
| 1080 | { |
| 1081 | // D3D9 pack mode is strictly more strict than WebGL, so takes priority. |
| 1082 | packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9; |
| 1083 | } |
| 1084 | else if (data.getExtensions().webglCompatibility) |
| 1085 | { |
| 1086 | packMode = PackMode::WEBGL_STRICT; |
| 1087 | } |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 1088 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1089 | ProgramLinkedResources resources = { |
| 1090 | {data.getCaps().maxVaryingVectors, packMode}, |
| 1091 | {&mState.mUniformBlocks, &mState.mUniforms}, |
Jiajia Qin | 94f1e89 | 2017-11-20 12:14:32 +0800 | [diff] [blame] | 1092 | {&mState.mShaderStorageBlocks, &mState.mBufferVariables}, |
| 1093 | {&mState.mAtomicCounterBuffers}}; |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 1094 | |
| 1095 | InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker); |
| 1096 | InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker); |
| 1097 | |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 1098 | if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps())) |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1099 | { |
| 1100 | return NoError(); |
| 1101 | } |
| 1102 | |
jchen10 | 85c93c4 | 2017-11-12 15:36:47 +0800 | [diff] [blame] | 1103 | if (!resources.varyingPacking.collectAndPackUserVaryings( |
| 1104 | mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames())) |
Olli Etuaho | 39e7812 | 2017-08-29 14:34:22 +0300 | [diff] [blame] | 1105 | { |
| 1106 | return NoError(); |
| 1107 | } |
| 1108 | |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 1109 | ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked); |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 1110 | if (!mLinked) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1111 | { |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 1112 | return NoError(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | gatherTransformFeedbackVaryings(mergedVaryings); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 1116 | } |
| 1117 | |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 1118 | initInterfaceBlockBindings(); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 1119 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1120 | setUniformValuesFromBindingQualifiers(); |
| 1121 | |
Yunchao He | ece1253 | 2017-11-21 15:50:21 +0800 | [diff] [blame] | 1122 | // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram, |
| 1123 | // Only successfully linked program can replace the executables. |
Yunchao He | 85072e8 | 2017-11-14 15:43:28 +0800 | [diff] [blame] | 1124 | ASSERT(mLinked); |
| 1125 | updateLinkedShaderStages(); |
| 1126 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1127 | // Mark implementation-specific unreferenced uniforms as ignored. |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 1128 | mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1129 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 1130 | // Save to the program cache. |
| 1131 | if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() || |
| 1132 | !context->getWorkarounds().disableProgramCachingForTransformFeedback)) |
| 1133 | { |
| 1134 | cache->putProgram(programHash, context, this); |
| 1135 | } |
| 1136 | |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 1137 | double delta = platform->currentTime(platform) - startTime; |
| 1138 | int us = static_cast<int>(delta * 1000000.0); |
| 1139 | ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us); |
| 1140 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1141 | return NoError(); |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Yunchao He | 85072e8 | 2017-11-14 15:43:28 +0800 | [diff] [blame] | 1144 | void Program::updateLinkedShaderStages() |
| 1145 | { |
Yunchao He | ece1253 | 2017-11-21 15:50:21 +0800 | [diff] [blame] | 1146 | mState.mLinkedShaderStages.reset(); |
| 1147 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1148 | for (const Shader *shader : mState.mAttachedShaders) |
Yunchao He | 85072e8 | 2017-11-14 15:43:28 +0800 | [diff] [blame] | 1149 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1150 | if (shader) |
| 1151 | { |
| 1152 | mState.mLinkedShaderStages.set(shader->getType()); |
| 1153 | } |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 1154 | } |
Yunchao He | 85072e8 | 2017-11-14 15:43:28 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
James Darpinian | 30b604d | 2018-03-12 17:26:57 -0700 | [diff] [blame] | 1157 | void ProgramState::updateTransformFeedbackStrides() |
| 1158 | { |
| 1159 | if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS) |
| 1160 | { |
| 1161 | mTransformFeedbackStrides.resize(1); |
| 1162 | size_t totalSize = 0; |
| 1163 | for (auto &varying : mLinkedTransformFeedbackVaryings) |
| 1164 | { |
| 1165 | totalSize += varying.size() * VariableExternalSize(varying.type); |
| 1166 | } |
| 1167 | mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize); |
| 1168 | } |
| 1169 | else |
| 1170 | { |
| 1171 | mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size()); |
| 1172 | for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++) |
| 1173 | { |
| 1174 | auto &varying = mLinkedTransformFeedbackVaryings[i]; |
| 1175 | mTransformFeedbackStrides[i] = |
| 1176 | static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type)); |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
daniel@transgaming.com | aa5e59b | 2011-10-04 18:43:12 +0000 | [diff] [blame] | 1181 | // Returns the program object to an unlinked state, before re-linking, or at destruction |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 1182 | void Program::unlink() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1183 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1184 | mState.mAttributes.clear(); |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 1185 | mState.mAttributesTypeMask.reset(); |
| 1186 | mState.mAttributesMask.reset(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1187 | mState.mActiveAttribLocationsMask.reset(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 1188 | mState.mMaxActiveAttribLocation = 0; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1189 | mState.mLinkedTransformFeedbackVaryings.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1190 | mState.mUniforms.clear(); |
| 1191 | mState.mUniformLocations.clear(); |
| 1192 | mState.mUniformBlocks.clear(); |
jchen10 | 7a20b97 | 2017-06-13 14:25:26 +0800 | [diff] [blame] | 1193 | mState.mActiveUniformBlockBindings.reset(); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1194 | mState.mAtomicCounterBuffers.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1195 | mState.mOutputVariables.clear(); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1196 | mState.mOutputLocations.clear(); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 1197 | mState.mOutputVariableTypes.clear(); |
Brandon Jones | 76746f9 | 2017-11-22 11:44:41 -0800 | [diff] [blame] | 1198 | mState.mDrawBufferTypeMask.reset(); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 1199 | mState.mActiveOutputVariables.reset(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1200 | mState.mComputeShaderLocalSize.fill(1); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1201 | mState.mSamplerBindings.clear(); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1202 | mState.mImageBindings.clear(); |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 1203 | mState.mNumViews = -1; |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 1204 | mState.mGeometryShaderInputPrimitiveType = GL_TRIANGLES; |
| 1205 | mState.mGeometryShaderOutputPrimitiveType = GL_TRIANGLE_STRIP; |
| 1206 | mState.mGeometryShaderInvocations = 1; |
| 1207 | mState.mGeometryShaderMaxVertices = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1208 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1209 | mValidated = false; |
| 1210 | |
daniel@transgaming.com | 716056c | 2012-07-24 18:38:59 +0000 | [diff] [blame] | 1211 | mLinked = false; |
Jamie Madill | 6bc264a | 2018-03-31 15:36:05 -0400 | [diff] [blame] | 1212 | mInfoLog.reset(); |
daniel@transgaming.com | 716056c | 2012-07-24 18:38:59 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1215 | bool Program::isLinked() const |
daniel@transgaming.com | 716056c | 2012-07-24 18:38:59 +0000 | [diff] [blame] | 1216 | { |
| 1217 | return mLinked; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 1220 | bool Program::hasLinkedShaderStage(ShaderType shaderType) const |
| 1221 | { |
| 1222 | ASSERT(shaderType != ShaderType::InvalidEnum); |
| 1223 | return mState.mLinkedShaderStages[shaderType]; |
| 1224 | } |
| 1225 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1226 | Error Program::loadBinary(const Context *context, |
| 1227 | GLenum binaryFormat, |
| 1228 | const void *binary, |
| 1229 | GLsizei length) |
apatrick@chromium.org | 3ce8dbc | 2012-06-08 17:52:30 +0000 | [diff] [blame] | 1230 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 1231 | unlink(); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1232 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1233 | #if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 1234 | return NoError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1235 | #else |
Geoff Lang | c46cc2f | 2015-10-01 17:16:20 -0400 | [diff] [blame] | 1236 | ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE); |
| 1237 | if (binaryFormat != GL_PROGRAM_BINARY_ANGLE) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 1238 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1239 | mInfoLog << "Invalid program binary format."; |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 1240 | return NoError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 1243 | const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary); |
| 1244 | ANGLE_TRY_RESULT( |
| 1245 | MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 1246 | |
| 1247 | // Currently we require the full shader text to compute the program hash. |
| 1248 | // TODO(jmadill): Store the binary in the internal program cache. |
| 1249 | |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 1250 | return NoError(); |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1251 | #endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1252 | } |
| 1253 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1254 | Error Program::saveBinary(const Context *context, |
| 1255 | GLenum *binaryFormat, |
| 1256 | void *binary, |
| 1257 | GLsizei bufSize, |
| 1258 | GLsizei *length) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1259 | { |
| 1260 | if (binaryFormat) |
| 1261 | { |
Geoff Lang | c46cc2f | 2015-10-01 17:16:20 -0400 | [diff] [blame] | 1262 | *binaryFormat = GL_PROGRAM_BINARY_ANGLE; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1263 | } |
| 1264 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 1265 | angle::MemoryBuffer memoryBuf; |
| 1266 | MemoryProgramCache::Serialize(context, this, &memoryBuf); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1267 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 1268 | GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size()); |
| 1269 | const uint8_t *streamState = memoryBuf.data(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1270 | |
| 1271 | if (streamLength > bufSize) |
| 1272 | { |
| 1273 | if (length) |
| 1274 | { |
| 1275 | *length = 0; |
| 1276 | } |
| 1277 | |
| 1278 | // TODO: This should be moved to the validation layer but computing the size of the binary before saving |
| 1279 | // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate |
| 1280 | // sizes and then copy it. |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1281 | return InternalError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | if (binary) |
| 1285 | { |
| 1286 | char *ptr = reinterpret_cast<char*>(binary); |
| 1287 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1288 | memcpy(ptr, streamState, streamLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1289 | ptr += streamLength; |
| 1290 | |
| 1291 | ASSERT(ptr - streamLength == binary); |
| 1292 | } |
| 1293 | |
| 1294 | if (length) |
| 1295 | { |
| 1296 | *length = streamLength; |
| 1297 | } |
| 1298 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 1299 | return NoError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1300 | } |
| 1301 | |
Jamie Madill | ffe00c0 | 2017-06-27 16:26:55 -0400 | [diff] [blame] | 1302 | GLint Program::getBinaryLength(const Context *context) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1303 | { |
| 1304 | GLint length; |
Jamie Madill | ffe00c0 | 2017-06-27 16:26:55 -0400 | [diff] [blame] | 1305 | Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1306 | if (error.isError()) |
| 1307 | { |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | return length; |
apatrick@chromium.org | 3ce8dbc | 2012-06-08 17:52:30 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 1314 | void Program::setBinaryRetrievableHint(bool retrievable) |
| 1315 | { |
| 1316 | // TODO(jmadill) : replace with dirty bits |
| 1317 | mProgram->setBinaryRetrievableHint(retrievable); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1318 | mState.mBinaryRetrieveableHint = retrievable; |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | bool Program::getBinaryRetrievableHint() const |
| 1322 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1323 | return mState.mBinaryRetrieveableHint; |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 1324 | } |
| 1325 | |
Yunchao He | 61afff1 | 2017-03-14 15:34:03 +0800 | [diff] [blame] | 1326 | void Program::setSeparable(bool separable) |
| 1327 | { |
| 1328 | // TODO(yunchao) : replace with dirty bits |
| 1329 | if (mState.mSeparable != separable) |
| 1330 | { |
| 1331 | mProgram->setSeparable(separable); |
| 1332 | mState.mSeparable = separable; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | bool Program::isSeparable() const |
| 1337 | { |
| 1338 | return mState.mSeparable; |
| 1339 | } |
| 1340 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 1341 | void Program::release(const Context *context) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1342 | { |
| 1343 | mRefCount--; |
| 1344 | |
| 1345 | if (mRefCount == 0 && mDeleteStatus) |
| 1346 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 1347 | mResourceManager->deleteProgram(context, mHandle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | void Program::addRef() |
| 1352 | { |
| 1353 | mRefCount++; |
| 1354 | } |
| 1355 | |
| 1356 | unsigned int Program::getRefCount() const |
| 1357 | { |
| 1358 | return mRefCount; |
| 1359 | } |
| 1360 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1361 | int Program::getInfoLogLength() const |
| 1362 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 1363 | return static_cast<int>(mInfoLog.getLength()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1366 | void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1367 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1368 | return mInfoLog.getLog(bufSize, length, infoLog); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1371 | void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1372 | { |
| 1373 | int total = 0; |
| 1374 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1375 | for (const Shader *shader : mState.mAttachedShaders) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1376 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1377 | if (shader && (total < maxCount)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1378 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 1379 | shaders[total] = shader->getHandle(); |
| 1380 | ++total; |
Jiawei Shao | 89be29a | 2017-11-06 14:36:45 +0800 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1384 | if (count) |
| 1385 | { |
| 1386 | *count = total; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1390 | GLuint Program::getAttributeLocation(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1391 | { |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 1392 | return mState.getAttributeLocation(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1393 | } |
| 1394 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 1395 | bool Program::isAttribLocationActive(size_t attribLocation) const |
Jamie Madill | 56c6e3c | 2015-04-15 10:18:05 -0400 | [diff] [blame] | 1396 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1397 | ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size()); |
| 1398 | return mState.mActiveAttribLocationsMask[attribLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1399 | } |
| 1400 | |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1401 | void Program::getActiveAttribute(GLuint index, |
| 1402 | GLsizei bufsize, |
| 1403 | GLsizei *length, |
| 1404 | GLint *size, |
| 1405 | GLenum *type, |
| 1406 | GLchar *name) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1407 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1408 | if (!mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1409 | { |
| 1410 | if (bufsize > 0) |
| 1411 | { |
| 1412 | name[0] = '\0'; |
| 1413 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1414 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1415 | if (length) |
| 1416 | { |
| 1417 | *length = 0; |
| 1418 | } |
| 1419 | |
| 1420 | *type = GL_NONE; |
| 1421 | *size = 1; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1422 | return; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1423 | } |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1424 | |
jchen10 | 36e120e | 2017-03-14 14:53:58 +0800 | [diff] [blame] | 1425 | ASSERT(index < mState.mAttributes.size()); |
| 1426 | const sh::Attribute &attrib = mState.mAttributes[index]; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1427 | |
| 1428 | if (bufsize > 0) |
| 1429 | { |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1430 | CopyStringToBuffer(name, attrib.name, bufsize, length); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // Always a single 'type' instance |
| 1434 | *size = 1; |
| 1435 | *type = attrib.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1438 | GLint Program::getActiveAttributeCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1439 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1440 | if (!mLinked) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1441 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1442 | return 0; |
| 1443 | } |
| 1444 | |
jchen10 | 36e120e | 2017-03-14 14:53:58 +0800 | [diff] [blame] | 1445 | return static_cast<GLint>(mState.mAttributes.size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1448 | GLint Program::getActiveAttributeMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1449 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1450 | if (!mLinked) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1451 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | size_t maxLength = 0; |
| 1456 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1457 | for (const sh::Attribute &attrib : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1458 | { |
jchen10 | 36e120e | 2017-03-14 14:53:58 +0800 | [diff] [blame] | 1459 | maxLength = std::max(attrib.name.length() + 1, maxLength); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1460 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1461 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1462 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1463 | } |
| 1464 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1465 | GLuint Program::getInputResourceIndex(const GLchar *name) const |
| 1466 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1467 | return GetResourceIndexFromName(mState.mAttributes, std::string(name)); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | GLuint Program::getOutputResourceIndex(const GLchar *name) const |
| 1471 | { |
| 1472 | return GetResourceIndexFromName(mState.mOutputVariables, std::string(name)); |
| 1473 | } |
| 1474 | |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1475 | size_t Program::getOutputResourceCount() const |
| 1476 | { |
| 1477 | return (mLinked ? mState.mOutputVariables.size() : 0); |
| 1478 | } |
| 1479 | |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 1480 | template <typename T> |
| 1481 | void Program::getResourceName(GLuint index, |
| 1482 | const std::vector<T> &resources, |
| 1483 | GLsizei bufSize, |
| 1484 | GLsizei *length, |
| 1485 | GLchar *name) const |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1486 | { |
| 1487 | if (length) |
| 1488 | { |
| 1489 | *length = 0; |
| 1490 | } |
| 1491 | |
| 1492 | if (!mLinked) |
| 1493 | { |
| 1494 | if (bufSize > 0) |
| 1495 | { |
| 1496 | name[0] = '\0'; |
| 1497 | } |
| 1498 | return; |
| 1499 | } |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 1500 | ASSERT(index < resources.size()); |
| 1501 | const auto &resource = resources[index]; |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1502 | |
| 1503 | if (bufSize > 0) |
| 1504 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1505 | CopyStringToBuffer(name, resource.name, bufSize, length); |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 1509 | void Program::getInputResourceName(GLuint index, |
| 1510 | GLsizei bufSize, |
| 1511 | GLsizei *length, |
| 1512 | GLchar *name) const |
| 1513 | { |
| 1514 | getResourceName(index, mState.mAttributes, bufSize, length, name); |
| 1515 | } |
| 1516 | |
| 1517 | void Program::getOutputResourceName(GLuint index, |
| 1518 | GLsizei bufSize, |
| 1519 | GLsizei *length, |
| 1520 | GLchar *name) const |
| 1521 | { |
| 1522 | getResourceName(index, mState.mOutputVariables, bufSize, length, name); |
| 1523 | } |
| 1524 | |
| 1525 | void Program::getUniformResourceName(GLuint index, |
| 1526 | GLsizei bufSize, |
| 1527 | GLsizei *length, |
| 1528 | GLchar *name) const |
| 1529 | { |
| 1530 | getResourceName(index, mState.mUniforms, bufSize, length, name); |
| 1531 | } |
| 1532 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1533 | void Program::getBufferVariableResourceName(GLuint index, |
| 1534 | GLsizei bufSize, |
| 1535 | GLsizei *length, |
| 1536 | GLchar *name) const |
| 1537 | { |
| 1538 | getResourceName(index, mState.mBufferVariables, bufSize, length, name); |
| 1539 | } |
| 1540 | |
jchen10 | 880683b | 2017-04-12 16:21:55 +0800 | [diff] [blame] | 1541 | const sh::Attribute &Program::getInputResource(GLuint index) const |
| 1542 | { |
| 1543 | ASSERT(index < mState.mAttributes.size()); |
| 1544 | return mState.mAttributes[index]; |
| 1545 | } |
| 1546 | |
| 1547 | const sh::OutputVariable &Program::getOutputResource(GLuint index) const |
| 1548 | { |
| 1549 | ASSERT(index < mState.mOutputVariables.size()); |
| 1550 | return mState.mOutputVariables[index]; |
| 1551 | } |
| 1552 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1553 | GLint Program::getFragDataLocation(const std::string &name) const |
| 1554 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1555 | return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1558 | void Program::getActiveUniform(GLuint index, |
| 1559 | GLsizei bufsize, |
| 1560 | GLsizei *length, |
| 1561 | GLint *size, |
| 1562 | GLenum *type, |
| 1563 | GLchar *name) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1564 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1565 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1566 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1567 | // index must be smaller than getActiveUniformCount() |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1568 | ASSERT(index < mState.mUniforms.size()); |
| 1569 | const LinkedUniform &uniform = mState.mUniforms[index]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1570 | |
| 1571 | if (bufsize > 0) |
| 1572 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1573 | std::string string = uniform.name; |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1574 | CopyStringToBuffer(name, string, bufsize, length); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 1577 | *size = clampCast<GLint>(uniform.getBasicTypeElementCount()); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1578 | *type = uniform.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | if (bufsize > 0) |
| 1583 | { |
| 1584 | name[0] = '\0'; |
| 1585 | } |
| 1586 | |
| 1587 | if (length) |
| 1588 | { |
| 1589 | *length = 0; |
| 1590 | } |
| 1591 | |
| 1592 | *size = 0; |
| 1593 | *type = GL_NONE; |
| 1594 | } |
| 1595 | } |
| 1596 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1597 | GLint Program::getActiveUniformCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1598 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1599 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1600 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1601 | return static_cast<GLint>(mState.mUniforms.size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1602 | } |
| 1603 | else |
| 1604 | { |
| 1605 | return 0; |
| 1606 | } |
| 1607 | } |
| 1608 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1609 | size_t Program::getActiveBufferVariableCount() const |
| 1610 | { |
| 1611 | return mLinked ? mState.mBufferVariables.size() : 0; |
| 1612 | } |
| 1613 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1614 | GLint Program::getActiveUniformMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1615 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1616 | size_t maxLength = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1617 | |
| 1618 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1619 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1620 | for (const LinkedUniform &uniform : mState.mUniforms) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1621 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1622 | if (!uniform.name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1623 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1624 | size_t length = uniform.name.length() + 1u; |
| 1625 | if (uniform.isArray()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1626 | { |
| 1627 | length += 3; // Counting in "[0]". |
| 1628 | } |
| 1629 | maxLength = std::max(length, maxLength); |
| 1630 | } |
| 1631 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1632 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1633 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1634 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1635 | } |
| 1636 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1637 | bool Program::isValidUniformLocation(GLint location) const |
| 1638 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1639 | ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size())); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1640 | return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() && |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 1641 | mState.mUniformLocations[static_cast<size_t>(location)].used()); |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 1642 | } |
| 1643 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1644 | const LinkedUniform &Program::getUniformByLocation(GLint location) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1645 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1646 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1647 | return mState.mUniforms[mState.getUniformIndexFromLocation(location)]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1648 | } |
| 1649 | |
Jamie Madill | ac4e9c3 | 2017-01-13 14:07:12 -0500 | [diff] [blame] | 1650 | const VariableLocation &Program::getUniformLocation(GLint location) const |
| 1651 | { |
| 1652 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
| 1653 | return mState.mUniformLocations[location]; |
| 1654 | } |
| 1655 | |
| 1656 | const std::vector<VariableLocation> &Program::getUniformLocations() const |
| 1657 | { |
| 1658 | return mState.mUniformLocations; |
| 1659 | } |
| 1660 | |
| 1661 | const LinkedUniform &Program::getUniformByIndex(GLuint index) const |
| 1662 | { |
| 1663 | ASSERT(index < static_cast<size_t>(mState.mUniforms.size())); |
| 1664 | return mState.mUniforms[index]; |
| 1665 | } |
| 1666 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1667 | const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const |
| 1668 | { |
| 1669 | ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size())); |
| 1670 | return mState.mBufferVariables[index]; |
| 1671 | } |
| 1672 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1673 | GLint Program::getUniformLocation(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1674 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1675 | return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1676 | } |
| 1677 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1678 | GLuint Program::getUniformIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1679 | { |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1680 | return mState.getUniformIndexFromName(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 1684 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1685 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1686 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1687 | mProgram->setUniform1fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 1691 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1692 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1693 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1694 | mProgram->setUniform2fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 1698 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1699 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1700 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1701 | mProgram->setUniform3fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 1705 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1706 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1707 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1708 | mProgram->setUniform4fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1711 | Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1712 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1713 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1714 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
| 1715 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1716 | mProgram->setUniform1iv(location, clampedCount, v); |
| 1717 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1718 | if (mState.isSamplerUniformIndex(locationInfo.index)) |
| 1719 | { |
| 1720 | updateSamplerUniform(locationInfo, clampedCount, v); |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1721 | return SetUniformResult::SamplerChanged; |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1722 | } |
| 1723 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1724 | return SetUniformResult::NoSamplerChange; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 1728 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1729 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1730 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1731 | mProgram->setUniform2iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 1735 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1736 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1737 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1738 | mProgram->setUniform3iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 1742 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1743 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1744 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1745 | mProgram->setUniform4iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 1749 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1750 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1751 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1752 | mProgram->setUniform1uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 1756 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1757 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1758 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1759 | mProgram->setUniform2uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 1763 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1764 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1765 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1766 | mProgram->setUniform3uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 1770 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1771 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1772 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1773 | mProgram->setUniform4uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1777 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1778 | GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1779 | mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1783 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1784 | GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1785 | mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1789 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1790 | GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1791 | mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1795 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1796 | GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1797 | mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1801 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1802 | GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1803 | mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1807 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1808 | GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1809 | mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1813 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1814 | GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1815 | mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1819 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1820 | GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1821 | mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1825 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1826 | GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1827 | mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1828 | } |
| 1829 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1830 | void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1831 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1832 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1833 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1834 | |
| 1835 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1836 | if (nativeType == GL_FLOAT) |
| 1837 | { |
| 1838 | mProgram->getUniformfv(context, location, v); |
| 1839 | } |
| 1840 | else |
| 1841 | { |
| 1842 | getUniformInternal(context, v, location, nativeType, |
| 1843 | gl::VariableComponentCount(uniform.type)); |
| 1844 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1845 | } |
| 1846 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1847 | void Program::getUniformiv(const Context *context, GLint location, GLint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1848 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1849 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1850 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1851 | |
| 1852 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1853 | if (nativeType == GL_INT || nativeType == GL_BOOL) |
| 1854 | { |
| 1855 | mProgram->getUniformiv(context, location, v); |
| 1856 | } |
| 1857 | else |
| 1858 | { |
| 1859 | getUniformInternal(context, v, location, nativeType, |
| 1860 | gl::VariableComponentCount(uniform.type)); |
| 1861 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1862 | } |
| 1863 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1864 | void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1865 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1866 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1867 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1868 | |
| 1869 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1870 | if (nativeType == GL_UNSIGNED_INT) |
| 1871 | { |
| 1872 | mProgram->getUniformuiv(context, location, v); |
| 1873 | } |
| 1874 | else |
| 1875 | { |
| 1876 | getUniformInternal(context, v, location, nativeType, |
| 1877 | gl::VariableComponentCount(uniform.type)); |
| 1878 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1879 | } |
| 1880 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1881 | void Program::flagForDeletion() |
| 1882 | { |
| 1883 | mDeleteStatus = true; |
| 1884 | } |
| 1885 | |
| 1886 | bool Program::isFlaggedForDeletion() const |
| 1887 | { |
| 1888 | return mDeleteStatus; |
| 1889 | } |
daniel@transgaming.com | 86a7a13 | 2010-04-29 03:32:32 +0000 | [diff] [blame] | 1890 | |
Brandon Jones | 43a53e2 | 2014-08-28 16:23:22 -0700 | [diff] [blame] | 1891 | void Program::validate(const Caps &caps) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1892 | { |
| 1893 | mInfoLog.reset(); |
| 1894 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1895 | if (mLinked) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1896 | { |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 1897 | mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog)); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1898 | } |
| 1899 | else |
| 1900 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1901 | mInfoLog << "Program has not been successfully linked."; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1905 | bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps) |
| 1906 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1907 | // Skip cache if we're using an infolog, so we get the full error. |
| 1908 | // Also skip the cache if the sample mapping has changed, or if we haven't ever validated. |
| 1909 | if (infoLog == nullptr && mCachedValidateSamplersResult.valid()) |
| 1910 | { |
| 1911 | return mCachedValidateSamplersResult.value(); |
| 1912 | } |
| 1913 | |
| 1914 | if (mTextureUnitTypesCache.empty()) |
| 1915 | { |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 1916 | mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, TextureType::InvalidEnum); |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1917 | } |
| 1918 | else |
| 1919 | { |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 1920 | std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), |
| 1921 | TextureType::InvalidEnum); |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | // if any two active samplers in a program are of different types, but refer to the same |
| 1925 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 1926 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1927 | for (const auto &samplerBinding : mState.mSamplerBindings) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1928 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1929 | if (samplerBinding.unreferenced) |
| 1930 | continue; |
| 1931 | |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 1932 | TextureType textureType = samplerBinding.textureType; |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1933 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1934 | for (GLuint textureUnit : samplerBinding.boundTextureUnits) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1935 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1936 | if (textureUnit >= caps.maxCombinedTextureImageUnits) |
| 1937 | { |
| 1938 | if (infoLog) |
| 1939 | { |
| 1940 | (*infoLog) << "Sampler uniform (" << textureUnit |
| 1941 | << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (" |
| 1942 | << caps.maxCombinedTextureImageUnits << ")"; |
| 1943 | } |
| 1944 | |
| 1945 | mCachedValidateSamplersResult = false; |
| 1946 | return false; |
| 1947 | } |
| 1948 | |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 1949 | if (mTextureUnitTypesCache[textureUnit] != TextureType::InvalidEnum) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1950 | { |
| 1951 | if (textureType != mTextureUnitTypesCache[textureUnit]) |
| 1952 | { |
| 1953 | if (infoLog) |
| 1954 | { |
| 1955 | (*infoLog) << "Samplers of conflicting types refer to the same texture " |
| 1956 | "image unit (" |
| 1957 | << textureUnit << ")."; |
| 1958 | } |
| 1959 | |
| 1960 | mCachedValidateSamplersResult = false; |
| 1961 | return false; |
| 1962 | } |
| 1963 | } |
| 1964 | else |
| 1965 | { |
| 1966 | mTextureUnitTypesCache[textureUnit] = textureType; |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | mCachedValidateSamplersResult = true; |
| 1972 | return true; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1973 | } |
| 1974 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1975 | bool Program::isValidated() const |
| 1976 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1977 | return mValidated; |
| 1978 | } |
| 1979 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1980 | GLuint Program::getActiveUniformBlockCount() const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1981 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1982 | return static_cast<GLuint>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1983 | } |
| 1984 | |
jchen10 | 58f67be | 2017-10-27 08:59:27 +0800 | [diff] [blame] | 1985 | GLuint Program::getActiveAtomicCounterBufferCount() const |
| 1986 | { |
| 1987 | return static_cast<GLuint>(mState.mAtomicCounterBuffers.size()); |
| 1988 | } |
| 1989 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1990 | GLuint Program::getActiveShaderStorageBlockCount() const |
| 1991 | { |
| 1992 | return static_cast<GLuint>(mState.mShaderStorageBlocks.size()); |
| 1993 | } |
| 1994 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1995 | void Program::getActiveUniformBlockName(const GLuint blockIndex, |
| 1996 | GLsizei bufSize, |
| 1997 | GLsizei *length, |
| 1998 | GLchar *blockName) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1999 | { |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 2000 | GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName); |
| 2001 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2002 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 2003 | void Program::getActiveShaderStorageBlockName(const GLuint blockIndex, |
| 2004 | GLsizei bufSize, |
| 2005 | GLsizei *length, |
| 2006 | GLchar *blockName) const |
| 2007 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2008 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 2009 | GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Qin Jiajia | 9bf5552 | 2018-01-29 13:56:23 +0800 | [diff] [blame] | 2012 | template <typename T> |
| 2013 | GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 2014 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2015 | int maxLength = 0; |
| 2016 | |
| 2017 | if (mLinked) |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 2018 | { |
Qin Jiajia | 9bf5552 | 2018-01-29 13:56:23 +0800 | [diff] [blame] | 2019 | for (const T &resource : resources) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2020 | { |
Qin Jiajia | 9bf5552 | 2018-01-29 13:56:23 +0800 | [diff] [blame] | 2021 | if (!resource.name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2022 | { |
Qin Jiajia | 9bf5552 | 2018-01-29 13:56:23 +0800 | [diff] [blame] | 2023 | int length = static_cast<int>(resource.nameWithArrayIndex().length()); |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2024 | maxLength = std::max(length + 1, maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2025 | } |
| 2026 | } |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 2027 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2028 | |
| 2029 | return maxLength; |
| 2030 | } |
| 2031 | |
Qin Jiajia | 9bf5552 | 2018-01-29 13:56:23 +0800 | [diff] [blame] | 2032 | GLint Program::getActiveUniformBlockMaxNameLength() const |
| 2033 | { |
| 2034 | return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks); |
| 2035 | } |
| 2036 | |
| 2037 | GLint Program::getActiveShaderStorageBlockMaxNameLength() const |
| 2038 | { |
| 2039 | return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks); |
| 2040 | } |
| 2041 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 2042 | GLuint Program::getUniformBlockIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2043 | { |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 2044 | return GetInterfaceBlockIndex(mState.mUniformBlocks, name); |
| 2045 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2046 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 2047 | GLuint Program::getShaderStorageBlockIndex(const std::string &name) const |
| 2048 | { |
| 2049 | return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name); |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2052 | const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 2053 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2054 | ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size())); |
| 2055 | return mState.mUniformBlocks[index]; |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 2058 | const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const |
| 2059 | { |
| 2060 | ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size())); |
| 2061 | return mState.mShaderStorageBlocks[index]; |
| 2062 | } |
| 2063 | |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 2064 | void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 2065 | { |
jchen10 | 7a20b97 | 2017-06-13 14:25:26 +0800 | [diff] [blame] | 2066 | mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding; |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 2067 | mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 2068 | mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const |
| 2072 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2073 | return mState.getUniformBlockBinding(uniformBlockIndex); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2076 | GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const |
| 2077 | { |
| 2078 | return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex); |
| 2079 | } |
| 2080 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2081 | void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode) |
| 2082 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2083 | mState.mTransformFeedbackVaryingNames.resize(count); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2084 | for (GLsizei i = 0; i < count; i++) |
| 2085 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2086 | mState.mTransformFeedbackVaryingNames[i] = varyings[i]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2087 | } |
| 2088 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2089 | mState.mTransformFeedbackBufferMode = bufferMode; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const |
| 2093 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2094 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2095 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2096 | ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size()); |
| 2097 | const auto &var = mState.mLinkedTransformFeedbackVaryings[index]; |
| 2098 | std::string varName = var.nameWithArrayIndex(); |
| 2099 | GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length())); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2100 | if (length) |
| 2101 | { |
| 2102 | *length = lastNameIdx; |
| 2103 | } |
| 2104 | if (size) |
| 2105 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2106 | *size = var.size(); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2107 | } |
| 2108 | if (type) |
| 2109 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2110 | *type = var.type; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2111 | } |
| 2112 | if (name) |
| 2113 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2114 | memcpy(name, varName.c_str(), lastNameIdx); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2115 | name[lastNameIdx] = '\0'; |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 2120 | GLsizei Program::getTransformFeedbackVaryingCount() const |
| 2121 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2122 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2123 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2124 | return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size()); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2125 | } |
| 2126 | else |
| 2127 | { |
| 2128 | return 0; |
| 2129 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | GLsizei Program::getTransformFeedbackVaryingMaxLength() const |
| 2133 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2134 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2135 | { |
| 2136 | GLsizei maxSize = 0; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2137 | for (const auto &var : mState.mLinkedTransformFeedbackVaryings) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2138 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2139 | maxSize = |
| 2140 | std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1)); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | return maxSize; |
| 2144 | } |
| 2145 | else |
| 2146 | { |
| 2147 | return 0; |
| 2148 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | GLenum Program::getTransformFeedbackBufferMode() const |
| 2152 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2153 | return mState.mTransformFeedbackBufferMode; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2154 | } |
| 2155 | |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2156 | bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog) |
| 2157 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2158 | Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex]; |
| 2159 | Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment]; |
| 2160 | Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute]; |
| 2161 | Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry]; |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2162 | |
| 2163 | bool isComputeShaderAttached = (computeShader != nullptr); |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 2164 | bool isGraphicsShaderAttached = |
| 2165 | (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr); |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2166 | // Check whether we both have a compute and non-compute shaders attached. |
| 2167 | // If there are of both types attached, then linking should fail. |
| 2168 | // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram |
| 2169 | if (isComputeShaderAttached == true && isGraphicsShaderAttached == true) |
| 2170 | { |
| 2171 | infoLog << "Both compute and graphics shaders are attached to the same program."; |
| 2172 | return false; |
| 2173 | } |
| 2174 | |
| 2175 | if (computeShader) |
| 2176 | { |
| 2177 | if (!computeShader->isCompiled(context)) |
| 2178 | { |
| 2179 | infoLog << "Attached compute shader is not compiled."; |
| 2180 | return false; |
| 2181 | } |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 2182 | ASSERT(computeShader->getType() == ShaderType::Compute); |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2183 | |
| 2184 | mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context); |
| 2185 | |
| 2186 | // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs |
| 2187 | // If the work group size is not specified, a link time error should occur. |
| 2188 | if (!mState.mComputeShaderLocalSize.isDeclared()) |
| 2189 | { |
| 2190 | infoLog << "Work group size is not specified."; |
| 2191 | return false; |
| 2192 | } |
| 2193 | } |
| 2194 | else |
| 2195 | { |
| 2196 | if (!fragmentShader || !fragmentShader->isCompiled(context)) |
| 2197 | { |
| 2198 | infoLog << "No compiled fragment shader when at least one graphics shader is attached."; |
| 2199 | return false; |
| 2200 | } |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 2201 | ASSERT(fragmentShader->getType() == ShaderType::Fragment); |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2202 | |
| 2203 | if (!vertexShader || !vertexShader->isCompiled(context)) |
| 2204 | { |
| 2205 | infoLog << "No compiled vertex shader when at least one graphics shader is attached."; |
| 2206 | return false; |
| 2207 | } |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 2208 | ASSERT(vertexShader->getType() == ShaderType::Vertex); |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2209 | |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 2210 | int vertexShaderVersion = vertexShader->getShaderVersion(context); |
| 2211 | if (fragmentShader->getShaderVersion(context) != vertexShaderVersion) |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2212 | { |
| 2213 | infoLog << "Fragment shader version does not match vertex shader version."; |
| 2214 | return false; |
| 2215 | } |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 2216 | |
| 2217 | if (geometryShader) |
| 2218 | { |
| 2219 | // [GL_EXT_geometry_shader] Chapter 7 |
| 2220 | // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading |
| 2221 | // Language Specification, as well as any of the following reasons: |
| 2222 | // * One or more of the shader objects attached to <program> are not compiled |
| 2223 | // successfully. |
| 2224 | // * The shaders do not use the same shader language version. |
| 2225 | // * <program> contains objects to form a geometry shader, and |
| 2226 | // - <program> is not separable and contains no objects to form a vertex shader; or |
| 2227 | // - the input primitive type, output primitive type, or maximum output vertex count |
| 2228 | // is not specified in the compiled geometry shader object. |
| 2229 | if (!geometryShader->isCompiled(context)) |
| 2230 | { |
| 2231 | infoLog << "The attached geometry shader isn't compiled."; |
| 2232 | return false; |
| 2233 | } |
| 2234 | |
| 2235 | if (geometryShader->getShaderVersion(context) != vertexShaderVersion) |
| 2236 | { |
| 2237 | mInfoLog << "Geometry shader version does not match vertex shader version."; |
| 2238 | return false; |
| 2239 | } |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 2240 | ASSERT(geometryShader->getType() == ShaderType::Geometry); |
Jiawei Shao | 4ed05da | 2018-02-02 14:26:15 +0800 | [diff] [blame] | 2241 | |
| 2242 | Optional<GLenum> inputPrimitive = |
| 2243 | geometryShader->getGeometryShaderInputPrimitiveType(context); |
| 2244 | if (!inputPrimitive.valid()) |
| 2245 | { |
| 2246 | mInfoLog << "Input primitive type is not specified in the geometry shader."; |
| 2247 | return false; |
| 2248 | } |
| 2249 | |
| 2250 | Optional<GLenum> outputPrimitive = |
| 2251 | geometryShader->getGeometryShaderOutputPrimitiveType(context); |
| 2252 | if (!outputPrimitive.valid()) |
| 2253 | { |
| 2254 | mInfoLog << "Output primitive type is not specified in the geometry shader."; |
| 2255 | return false; |
| 2256 | } |
| 2257 | |
| 2258 | Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices(context); |
| 2259 | if (!maxVertices.valid()) |
| 2260 | { |
| 2261 | mInfoLog << "'max_vertices' is not specified in the geometry shader."; |
| 2262 | return false; |
| 2263 | } |
| 2264 | |
| 2265 | mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value(); |
| 2266 | mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value(); |
| 2267 | mState.mGeometryShaderMaxVertices = maxVertices.value(); |
| 2268 | mState.mGeometryShaderInvocations = |
| 2269 | geometryShader->getGeometryShaderInvocations(context); |
| 2270 | } |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | return true; |
| 2274 | } |
| 2275 | |
jchen10 | 910a3da | 2017-11-15 09:40:11 +0800 | [diff] [blame] | 2276 | GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const |
| 2277 | { |
| 2278 | for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex) |
| 2279 | { |
| 2280 | const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex]; |
| 2281 | if (tf.nameWithArrayIndex() == name) |
| 2282 | { |
| 2283 | return tfIndex; |
| 2284 | } |
| 2285 | } |
| 2286 | return GL_INVALID_INDEX; |
| 2287 | } |
| 2288 | |
| 2289 | const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const |
| 2290 | { |
| 2291 | ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size()); |
| 2292 | return mState.mLinkedTransformFeedbackVaryings[index]; |
| 2293 | } |
| 2294 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2295 | bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2296 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2297 | Shader *previousShader = nullptr; |
| 2298 | for (ShaderType shaderType : kAllGraphicsShaderTypes) |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2299 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2300 | Shader *currentShader = mState.mAttachedShaders[shaderType]; |
| 2301 | if (!currentShader) |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2302 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2303 | continue; |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2304 | } |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2305 | |
| 2306 | if (previousShader) |
| 2307 | { |
| 2308 | if (!linkValidateShaderInterfaceMatching(context, previousShader, currentShader, |
| 2309 | infoLog)) |
| 2310 | { |
| 2311 | return false; |
| 2312 | } |
| 2313 | } |
| 2314 | previousShader = currentShader; |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | if (!linkValidateBuiltInVaryings(context, infoLog)) |
| 2318 | { |
| 2319 | return false; |
| 2320 | } |
| 2321 | |
| 2322 | if (!linkValidateFragmentInputBindings(context, infoLog)) |
| 2323 | { |
| 2324 | return false; |
| 2325 | } |
| 2326 | |
| 2327 | return true; |
| 2328 | } |
| 2329 | |
| 2330 | // [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91 |
| 2331 | // TODO(jiawei.shao@intel.com): add validation on input/output blocks matching |
| 2332 | bool Program::linkValidateShaderInterfaceMatching(const Context *context, |
| 2333 | gl::Shader *generatingShader, |
| 2334 | gl::Shader *consumingShader, |
| 2335 | gl::InfoLog &infoLog) const |
| 2336 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2337 | ASSERT(generatingShader->getShaderVersion(context) == |
| 2338 | consumingShader->getShaderVersion(context)); |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2339 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2340 | const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context); |
| 2341 | const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2342 | |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 2343 | bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry; |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2344 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2345 | for (const sh::Varying &input : inputVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2346 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2347 | bool matched = false; |
| 2348 | |
| 2349 | // Built-in varyings obey special rules |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2350 | if (input.isBuiltIn()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2351 | { |
| 2352 | continue; |
| 2353 | } |
| 2354 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2355 | for (const sh::Varying &output : outputVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2356 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2357 | if (input.name == output.name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2358 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2359 | ASSERT(!output.isBuiltIn()); |
| 2360 | |
| 2361 | std::string mismatchedStructFieldName; |
| 2362 | LinkMismatchError linkError = |
| 2363 | LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context), |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2364 | validateGeometryShaderInputs, &mismatchedStructFieldName); |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2365 | if (linkError != LinkMismatchError::NO_MISMATCH) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2366 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2367 | LogLinkMismatch(infoLog, input.name, "varying", linkError, |
| 2368 | mismatchedStructFieldName, generatingShader->getType(), |
| 2369 | consumingShader->getType()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2370 | return false; |
| 2371 | } |
| 2372 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2373 | matched = true; |
| 2374 | break; |
| 2375 | } |
| 2376 | } |
| 2377 | |
Olli Etuaho | 107c724 | 2018-03-20 15:45:35 +0200 | [diff] [blame] | 2378 | // We permit unmatched, unreferenced varyings. Note that this specifically depends on |
| 2379 | // whether the input is statically used - a statically used input should fail this test even |
| 2380 | // if it is not active. GLSL ES 3.00.6 section 4.3.10. |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2381 | if (!matched && input.staticUse) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2382 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2383 | infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name |
| 2384 | << " does not match any " << GetShaderTypeString(generatingShader->getType()) |
| 2385 | << " varying"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2386 | return false; |
| 2387 | } |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2388 | } |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2389 | |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2390 | // TODO(jmadill): verify no unmatched output varyings? |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2391 | |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2392 | return true; |
| 2393 | } |
| 2394 | |
| 2395 | bool Program::linkValidateFragmentInputBindings(const Context *context, gl::InfoLog &infoLog) const |
| 2396 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2397 | ASSERT(mState.mAttachedShaders[ShaderType::Fragment]); |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2398 | |
| 2399 | std::map<GLuint, std::string> staticFragmentInputLocations; |
| 2400 | |
| 2401 | const std::vector<sh::Varying> &fragmentInputVaryings = |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2402 | mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings(context); |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2403 | for (const sh::Varying &input : fragmentInputVaryings) |
| 2404 | { |
| 2405 | if (input.isBuiltIn() || !input.staticUse) |
| 2406 | { |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2407 | continue; |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2408 | } |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2409 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2410 | const auto inputBinding = mFragmentInputBindings.getBinding(input.name); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2411 | if (inputBinding == -1) |
| 2412 | continue; |
| 2413 | |
| 2414 | const auto it = staticFragmentInputLocations.find(inputBinding); |
| 2415 | if (it == std::end(staticFragmentInputLocations)) |
| 2416 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2417 | staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name)); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2418 | } |
| 2419 | else |
| 2420 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2421 | infoLog << "Binding for fragment input " << input.name << " conflicts with " |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 2422 | << it->second; |
| 2423 | return false; |
| 2424 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | return true; |
| 2428 | } |
| 2429 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2430 | bool Program::linkUniforms(const Context *context, |
| 2431 | InfoLog &infoLog, |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 2432 | const ProgramBindings &uniformLocationBindings) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2433 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 2434 | UniformLinker linker(mState); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2435 | if (!linker.link(context, infoLog, uniformLocationBindings)) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2436 | { |
| 2437 | return false; |
| 2438 | } |
| 2439 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 2440 | linker.getResults(&mState.mUniforms, &mState.mUniformLocations); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2441 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2442 | linkSamplerAndImageBindings(); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2443 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2444 | if (!linkAtomicCounterBuffers()) |
| 2445 | { |
| 2446 | return false; |
| 2447 | } |
| 2448 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2449 | return true; |
| 2450 | } |
| 2451 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2452 | void Program::linkSamplerAndImageBindings() |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2453 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2454 | unsigned int high = static_cast<unsigned int>(mState.mUniforms.size()); |
| 2455 | unsigned int low = high; |
| 2456 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2457 | for (auto counterIter = mState.mUniforms.rbegin(); |
| 2458 | counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter) |
| 2459 | { |
| 2460 | --low; |
| 2461 | } |
| 2462 | |
| 2463 | mState.mAtomicCounterUniformRange = RangeUI(low, high); |
| 2464 | |
| 2465 | high = low; |
| 2466 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2467 | for (auto imageIter = mState.mUniforms.rbegin(); |
| 2468 | imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter) |
| 2469 | { |
| 2470 | --low; |
| 2471 | } |
| 2472 | |
| 2473 | mState.mImageUniformRange = RangeUI(low, high); |
| 2474 | |
| 2475 | // If uniform is a image type, insert it into the mImageBindings array. |
| 2476 | for (unsigned int imageIndex : mState.mImageUniformRange) |
| 2477 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 2478 | // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands |
| 2479 | // cannot load values into a uniform defined as an image. if declare without a |
| 2480 | // binding qualifier, any uniform image variable (include all elements of |
| 2481 | // unbound image array) shoud be bound to unit zero. |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2482 | auto &imageUniform = mState.mUniforms[imageIndex]; |
| 2483 | if (imageUniform.binding == -1) |
| 2484 | { |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 2485 | mState.mImageBindings.emplace_back( |
| 2486 | ImageBinding(imageUniform.getBasicTypeElementCount())); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2487 | } |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 2488 | else |
| 2489 | { |
| 2490 | mState.mImageBindings.emplace_back( |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 2491 | ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount())); |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 2492 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | high = low; |
| 2496 | |
| 2497 | for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length(); |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2498 | samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter) |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2499 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2500 | --low; |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2501 | } |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2502 | |
| 2503 | mState.mSamplerUniformRange = RangeUI(low, high); |
| 2504 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2505 | // If uniform is a sampler type, insert it into the mSamplerBindings array. |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2506 | for (unsigned int samplerIndex : mState.mSamplerUniformRange) |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2507 | { |
| 2508 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 2509 | TextureType textureType = SamplerTypeToTextureType(samplerUniform.type); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2510 | mState.mSamplerBindings.emplace_back( |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 2511 | SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false)); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
| 2514 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2515 | bool Program::linkAtomicCounterBuffers() |
| 2516 | { |
| 2517 | for (unsigned int index : mState.mAtomicCounterUniformRange) |
| 2518 | { |
| 2519 | auto &uniform = mState.mUniforms[index]; |
Jiajia Qin | 94f1e89 | 2017-11-20 12:14:32 +0800 | [diff] [blame] | 2520 | uniform.blockInfo.offset = uniform.offset; |
| 2521 | uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0); |
| 2522 | uniform.blockInfo.matrixStride = 0; |
| 2523 | uniform.blockInfo.isRowMajorMatrix = false; |
| 2524 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2525 | bool found = false; |
| 2526 | for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size(); |
| 2527 | ++bufferIndex) |
| 2528 | { |
| 2529 | auto &buffer = mState.mAtomicCounterBuffers[bufferIndex]; |
| 2530 | if (buffer.binding == uniform.binding) |
| 2531 | { |
| 2532 | buffer.memberIndexes.push_back(index); |
| 2533 | uniform.bufferIndex = bufferIndex; |
| 2534 | found = true; |
jchen10 | 58f67be | 2017-10-27 08:59:27 +0800 | [diff] [blame] | 2535 | buffer.unionReferencesWith(uniform); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2536 | break; |
| 2537 | } |
| 2538 | } |
| 2539 | if (!found) |
| 2540 | { |
| 2541 | AtomicCounterBuffer atomicCounterBuffer; |
| 2542 | atomicCounterBuffer.binding = uniform.binding; |
| 2543 | atomicCounterBuffer.memberIndexes.push_back(index); |
jchen10 | 58f67be | 2017-10-27 08:59:27 +0800 | [diff] [blame] | 2544 | atomicCounterBuffer.unionReferencesWith(uniform); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2545 | mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer); |
| 2546 | uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1); |
| 2547 | } |
| 2548 | } |
| 2549 | // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against |
Jiawei Shao | 0d88ec9 | 2018-02-27 16:25:31 +0800 | [diff] [blame] | 2550 | // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers. |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2551 | |
| 2552 | return true; |
| 2553 | } |
| 2554 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2555 | // Assigns locations to all attributes from the bindings and program locations. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2556 | bool Program::linkAttributes(const Context *context, InfoLog &infoLog) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2557 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2558 | const ContextState &data = context->getContextState(); |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 2559 | Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex); |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2560 | |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2561 | int shaderVersion = vertexShader->getShaderVersion(context); |
| 2562 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2563 | unsigned int usedLocations = 0; |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2564 | if (shaderVersion >= 300) |
| 2565 | { |
| 2566 | // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL |
| 2567 | // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks. |
| 2568 | mState.mAttributes = vertexShader->getAllAttributes(context); |
| 2569 | } |
| 2570 | else |
| 2571 | { |
| 2572 | // In GLSL ES 1.00.17 we only do aliasing checks for active attributes. |
| 2573 | mState.mAttributes = vertexShader->getActiveAttributes(context); |
| 2574 | } |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 2575 | GLuint maxAttribs = data.getCaps().maxVertexAttributes; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2576 | |
| 2577 | // TODO(jmadill): handle aliasing robustly |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2578 | if (mState.mAttributes.size() > maxAttribs) |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2579 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2580 | infoLog << "Too many vertex attributes."; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2581 | return false; |
| 2582 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2583 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 2584 | std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr); |
Jamie Madill | 4e10722 | 2015-08-24 14:12:17 +0000 | [diff] [blame] | 2585 | |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2586 | // Assign locations to attributes that have a binding location and check for attribute aliasing. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2587 | for (sh::Attribute &attribute : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2588 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 2589 | // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or |
| 2590 | // structures, so we don't need to worry about adjusting their names or generating entries |
| 2591 | // for each member/element (unlike uniforms for example). |
| 2592 | ASSERT(!attribute.isArray() && !attribute.isStruct()); |
| 2593 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2594 | int bindingLocation = mAttributeBindings.getBinding(attribute.name); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2595 | if (attribute.location == -1 && bindingLocation != -1) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 2596 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2597 | attribute.location = bindingLocation; |
| 2598 | } |
| 2599 | |
| 2600 | if (attribute.location != -1) |
| 2601 | { |
| 2602 | // Location is set by glBindAttribLocation or by location layout qualifier |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2603 | const int regs = VariableRegisterCount(attribute.type); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2604 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2605 | if (static_cast<GLuint>(regs + attribute.location) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2606 | { |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2607 | infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location |
| 2608 | << " is too big to fit"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2609 | |
| 2610 | return false; |
| 2611 | } |
| 2612 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2613 | for (int reg = 0; reg < regs; reg++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2614 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2615 | const int regLocation = attribute.location + reg; |
| 2616 | sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2617 | |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2618 | // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error. |
| 2619 | // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some |
| 2620 | // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug. |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2621 | if (linkedAttribute) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2622 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2623 | // TODO(jmadill): fix aliasing on ES2 |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2624 | // if (shaderVersion >= 300 && !webgl) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2625 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 2626 | infoLog << "Attribute '" << attribute.name << "' aliases attribute '" |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2627 | << linkedAttribute->name << "' at location " << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2628 | return false; |
| 2629 | } |
| 2630 | } |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2631 | else |
| 2632 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2633 | usedAttribMap[regLocation] = &attribute; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2634 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2635 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2636 | usedLocations |= 1 << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | } |
| 2640 | |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2641 | // Assign locations to attributes that don't have a binding location. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2642 | for (sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2643 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2644 | // Not set by glBindAttribLocation or by location layout qualifier |
| 2645 | if (attribute.location == -1) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2646 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2647 | int regs = VariableRegisterCount(attribute.type); |
| 2648 | int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2649 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2650 | if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2651 | { |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2652 | infoLog << "Too many attributes (" << attribute.name << ")"; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2653 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2654 | } |
| 2655 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2656 | attribute.location = availableIndex; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 2660 | ASSERT(mState.mAttributesTypeMask.none()); |
| 2661 | ASSERT(mState.mAttributesMask.none()); |
| 2662 | |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2663 | // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier |
| 2664 | // shader versions we're only processing active attributes to begin with. |
| 2665 | if (shaderVersion >= 300) |
| 2666 | { |
| 2667 | for (auto attributeIter = mState.mAttributes.begin(); |
| 2668 | attributeIter != mState.mAttributes.end();) |
| 2669 | { |
| 2670 | if (attributeIter->active) |
| 2671 | { |
| 2672 | ++attributeIter; |
| 2673 | } |
| 2674 | else |
| 2675 | { |
| 2676 | attributeIter = mState.mAttributes.erase(attributeIter); |
| 2677 | } |
| 2678 | } |
| 2679 | } |
| 2680 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2681 | for (const sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2682 | { |
Olli Etuaho | ebd6e2d | 2018-03-23 17:07:55 +0200 | [diff] [blame] | 2683 | ASSERT(attribute.active); |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2684 | ASSERT(attribute.location != -1); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 2685 | unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type)); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2686 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 2687 | for (unsigned int r = 0; r < regs; r++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2688 | { |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 2689 | unsigned int location = static_cast<unsigned int>(attribute.location) + r; |
| 2690 | mState.mActiveAttribLocationsMask.set(location); |
| 2691 | mState.mMaxActiveAttribLocation = |
| 2692 | std::max(mState.mMaxActiveAttribLocation, location + 1); |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 2693 | |
| 2694 | // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute. |
| 2695 | if (!attribute.isBuiltIn()) |
| 2696 | { |
| 2697 | mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type), |
| 2698 | location); |
| 2699 | mState.mAttributesMask.set(location); |
| 2700 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2704 | return true; |
| 2705 | } |
| 2706 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2707 | bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2708 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2709 | const auto &caps = context->getCaps(); |
| 2710 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2711 | if (mState.mAttachedShaders[ShaderType::Compute]) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2712 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2713 | Shader &computeShader = *mState.mAttachedShaders[ShaderType::Compute]; |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2714 | const auto &computeUniformBlocks = computeShader.getUniformBlocks(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2715 | |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2716 | if (!ValidateInterfaceBlocksCount( |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2717 | caps.maxComputeUniformBlocks, computeUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2718 | "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (", |
| 2719 | infoLog)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2720 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2721 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2722 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2723 | |
| 2724 | const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context); |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2725 | if (!ValidateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks, |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2726 | computeShaderStorageBlocks, |
| 2727 | "Compute shader shader storage block count exceeds " |
| 2728 | "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (", |
| 2729 | infoLog)) |
| 2730 | { |
| 2731 | return false; |
| 2732 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2733 | return true; |
| 2734 | } |
| 2735 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2736 | Shader &vertexShader = *mState.mAttachedShaders[ShaderType::Vertex]; |
| 2737 | Shader &fragmentShader = *mState.mAttachedShaders[ShaderType::Fragment]; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2738 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2739 | const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context); |
| 2740 | const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2741 | |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2742 | if (!ValidateInterfaceBlocksCount( |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2743 | caps.maxVertexUniformBlocks, vertexUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2744 | "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog)) |
| 2745 | { |
| 2746 | return false; |
| 2747 | } |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2748 | if (!ValidateInterfaceBlocksCount( |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2749 | caps.maxFragmentUniformBlocks, fragmentUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2750 | "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (", |
| 2751 | infoLog)) |
| 2752 | { |
| 2753 | |
| 2754 | return false; |
| 2755 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2756 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2757 | Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry]; |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2758 | if (geometryShader) |
| 2759 | { |
| 2760 | const auto &geometryUniformBlocks = geometryShader->getUniformBlocks(context); |
| 2761 | if (!ValidateInterfaceBlocksCount( |
| 2762 | caps.maxGeometryUniformBlocks, geometryUniformBlocks, |
| 2763 | "Geometry shader uniform block count exceeds GL_MAX_GEOMETRY_UNIFORM_BLOCKS_EXT (", |
| 2764 | infoLog)) |
| 2765 | { |
| 2766 | return false; |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | // TODO(jiawei.shao@intel.com): validate geometry shader uniform blocks. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2771 | bool webglCompatibility = context->getExtensions().webglCompatibility; |
Jiawei Shao | 7361860 | 2017-12-20 15:47:15 +0800 | [diff] [blame] | 2772 | if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog, |
Jiajia Qin | 8efd126 | 2017-12-19 09:32:55 +0800 | [diff] [blame] | 2773 | webglCompatibility, sh::BlockType::BLOCK_UNIFORM, |
| 2774 | caps.maxCombinedUniformBlocks)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2775 | { |
| 2776 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2777 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2778 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2779 | if (context->getClientVersion() >= Version(3, 1)) |
| 2780 | { |
| 2781 | const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context); |
| 2782 | const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context); |
| 2783 | |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2784 | if (!ValidateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks, |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2785 | vertexShaderStorageBlocks, |
| 2786 | "Vertex shader shader storage block count exceeds " |
| 2787 | "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (", |
| 2788 | infoLog)) |
| 2789 | { |
| 2790 | return false; |
| 2791 | } |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2792 | if (!ValidateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks, |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2793 | fragmentShaderStorageBlocks, |
| 2794 | "Fragment shader shader storage block count exceeds " |
| 2795 | "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (", |
| 2796 | infoLog)) |
| 2797 | { |
| 2798 | |
| 2799 | return false; |
| 2800 | } |
| 2801 | |
Jiawei Shao | 427071e | 2018-03-19 09:21:37 +0800 | [diff] [blame] | 2802 | if (geometryShader) |
| 2803 | { |
| 2804 | const auto &geometryShaderStorageBlocks = |
| 2805 | geometryShader->getShaderStorageBlocks(context); |
| 2806 | if (!ValidateInterfaceBlocksCount(caps.maxGeometryShaderStorageBlocks, |
| 2807 | geometryShaderStorageBlocks, |
| 2808 | "Geometry shader shader storage block count exceeds " |
| 2809 | "GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS_EXT (", |
| 2810 | infoLog)) |
| 2811 | { |
| 2812 | return false; |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | // TODO(jiawei.shao@intel.com): validate geometry shader shader storage blocks. |
Jiajia Qin | 8efd126 | 2017-12-19 09:32:55 +0800 | [diff] [blame] | 2817 | if (!ValidateGraphicsInterfaceBlocks( |
| 2818 | vertexShaderStorageBlocks, fragmentShaderStorageBlocks, infoLog, webglCompatibility, |
| 2819 | sh::BlockType::BLOCK_BUFFER, caps.maxCombinedShaderStorageBlocks)) |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2820 | { |
| 2821 | return false; |
| 2822 | } |
| 2823 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2824 | return true; |
| 2825 | } |
| 2826 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2827 | LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1, |
| 2828 | const sh::ShaderVariable &variable2, |
| 2829 | bool validatePrecision, |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2830 | bool validateArraySize, |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2831 | std::string *mismatchedStructOrBlockMemberName) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2832 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2833 | if (variable1.type != variable2.type) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2834 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2835 | return LinkMismatchError::TYPE_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2836 | } |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2837 | if (validateArraySize && variable1.arraySizes != variable2.arraySizes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2838 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2839 | return LinkMismatchError::ARRAY_SIZE_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2840 | } |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2841 | if (validatePrecision && variable1.precision != variable2.precision) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2842 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2843 | return LinkMismatchError::PRECISION_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2844 | } |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2845 | if (variable1.structName != variable2.structName) |
Geoff Lang | bb1e750 | 2017-06-05 16:40:09 -0400 | [diff] [blame] | 2846 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2847 | return LinkMismatchError::STRUCT_NAME_MISMATCH; |
Geoff Lang | bb1e750 | 2017-06-05 16:40:09 -0400 | [diff] [blame] | 2848 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2849 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2850 | if (variable1.fields.size() != variable2.fields.size()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2851 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2852 | return LinkMismatchError::FIELD_NUMBER_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2853 | } |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2854 | const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2855 | for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++) |
| 2856 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2857 | const sh::ShaderVariable &member1 = variable1.fields[memberIndex]; |
| 2858 | const sh::ShaderVariable &member2 = variable2.fields[memberIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2859 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2860 | if (member1.name != member2.name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2861 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2862 | return LinkMismatchError::FIELD_NAME_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2863 | } |
| 2864 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2865 | LinkMismatchError linkErrorOnField = LinkValidateVariablesBase( |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2866 | member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName); |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2867 | if (linkErrorOnField != LinkMismatchError::NO_MISMATCH) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2868 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2869 | AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName); |
| 2870 | return linkErrorOnField; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2874 | return LinkMismatchError::NO_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2875 | } |
| 2876 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2877 | LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying, |
| 2878 | const sh::Varying &inputVarying, |
| 2879 | int shaderVersion, |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2880 | bool validateGeometryShaderInputVarying, |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2881 | std::string *mismatchedStructFieldName) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2882 | { |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2883 | if (validateGeometryShaderInputVarying) |
| 2884 | { |
| 2885 | // [GL_EXT_geometry_shader] Section 11.1gs.4.3: |
| 2886 | // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs |
| 2887 | // or outputs. |
| 2888 | ASSERT(inputVarying.arraySizes.size() == 1u); |
| 2889 | |
| 2890 | // Geometry shader input varyings are not treated as arrays, so a vertex array output |
| 2891 | // varying cannot match a geometry shader input varying. |
| 2892 | // [GL_EXT_geometry_shader] Section 7.4.1: |
| 2893 | // Geometry shader per-vertex input variables and blocks are required to be declared as |
| 2894 | // arrays, with each element representing input or output values for a single vertex of a |
| 2895 | // multi-vertex primitive. For the purposes of interface matching, such variables and blocks |
| 2896 | // are treated as though they were not declared as arrays. |
| 2897 | if (outputVarying.isArray()) |
| 2898 | { |
| 2899 | return LinkMismatchError::ARRAY_SIZE_MISMATCH; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | // Skip the validation on the array sizes between a vertex output varying and a geometry input |
| 2904 | // varying as it has been done before. |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2905 | LinkMismatchError linkError = |
Jiawei Shao | d063aff | 2018-02-22 10:19:09 +0800 | [diff] [blame] | 2906 | LinkValidateVariablesBase(outputVarying, inputVarying, false, |
| 2907 | !validateGeometryShaderInputVarying, mismatchedStructFieldName); |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2908 | if (linkError != LinkMismatchError::NO_MISMATCH) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2909 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2910 | return linkError; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2911 | } |
| 2912 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2913 | if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2914 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2915 | return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH; |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2916 | } |
| 2917 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2918 | if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant) |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2919 | { |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2920 | return LinkMismatchError::INVARIANCE_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2921 | } |
| 2922 | |
Jiawei Shao | 881b7bf | 2017-12-25 11:18:37 +0800 | [diff] [blame] | 2923 | return LinkMismatchError::NO_MISMATCH; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2924 | } |
| 2925 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2926 | bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2927 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 2928 | Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex]; |
| 2929 | Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment]; |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 2930 | const auto &vertexVaryings = vertexShader->getOutputVaryings(context); |
| 2931 | const auto &fragmentVaryings = fragmentShader->getInputVaryings(context); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2932 | int shaderVersion = vertexShader->getShaderVersion(context); |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2933 | |
| 2934 | if (shaderVersion != 100) |
| 2935 | { |
| 2936 | // Only ESSL 1.0 has restrictions on matching input and output invariance |
| 2937 | return true; |
| 2938 | } |
| 2939 | |
| 2940 | bool glPositionIsInvariant = false; |
| 2941 | bool glPointSizeIsInvariant = false; |
| 2942 | bool glFragCoordIsInvariant = false; |
| 2943 | bool glPointCoordIsInvariant = false; |
| 2944 | |
| 2945 | for (const sh::Varying &varying : vertexVaryings) |
| 2946 | { |
| 2947 | if (!varying.isBuiltIn()) |
| 2948 | { |
| 2949 | continue; |
| 2950 | } |
| 2951 | if (varying.name.compare("gl_Position") == 0) |
| 2952 | { |
| 2953 | glPositionIsInvariant = varying.isInvariant; |
| 2954 | } |
| 2955 | else if (varying.name.compare("gl_PointSize") == 0) |
| 2956 | { |
| 2957 | glPointSizeIsInvariant = varying.isInvariant; |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | for (const sh::Varying &varying : fragmentVaryings) |
| 2962 | { |
| 2963 | if (!varying.isBuiltIn()) |
| 2964 | { |
| 2965 | continue; |
| 2966 | } |
| 2967 | if (varying.name.compare("gl_FragCoord") == 0) |
| 2968 | { |
| 2969 | glFragCoordIsInvariant = varying.isInvariant; |
| 2970 | } |
| 2971 | else if (varying.name.compare("gl_PointCoord") == 0) |
| 2972 | { |
| 2973 | glPointCoordIsInvariant = varying.isInvariant; |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation, |
| 2978 | // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842. |
| 2979 | // Not requiring invariance to match is supported by: |
| 2980 | // dEQP, WebGL CTS, Nexus 5X GLES |
| 2981 | if (glFragCoordIsInvariant && !glPositionIsInvariant) |
| 2982 | { |
| 2983 | infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is " |
| 2984 | "declared invariant."; |
| 2985 | return false; |
| 2986 | } |
| 2987 | if (glPointCoordIsInvariant && !glPointSizeIsInvariant) |
| 2988 | { |
| 2989 | infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is " |
| 2990 | "declared invariant."; |
| 2991 | return false; |
| 2992 | } |
| 2993 | |
| 2994 | return true; |
| 2995 | } |
| 2996 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2997 | bool Program::linkValidateTransformFeedback(const gl::Context *context, |
| 2998 | InfoLog &infoLog, |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 2999 | const ProgramMergedVaryings &varyings, |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3000 | const Caps &caps) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 3001 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 3002 | |
jchen10 | 8225e73 | 2017-11-14 16:29:03 +0800 | [diff] [blame] | 3003 | // Validate the tf names regardless of the actual program varyings. |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3004 | std::set<std::string> uniqueNames; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 3005 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 3006 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 3007 | if (context->getClientVersion() < Version(3, 1) && |
| 3008 | tfVaryingName.find('[') != std::string::npos) |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 3009 | { |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 3010 | infoLog << "Capture of array elements is undefined and not supported."; |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 3011 | return false; |
| 3012 | } |
jchen10 | 8225e73 | 2017-11-14 16:29:03 +0800 | [diff] [blame] | 3013 | if (context->getClientVersion() >= Version(3, 1)) |
| 3014 | { |
| 3015 | if (IncludeSameArrayElement(uniqueNames, tfVaryingName)) |
| 3016 | { |
| 3017 | infoLog << "Two transform feedback varyings include the same array element (" |
| 3018 | << tfVaryingName << ")."; |
| 3019 | return false; |
| 3020 | } |
| 3021 | } |
| 3022 | else |
| 3023 | { |
| 3024 | if (uniqueNames.count(tfVaryingName) > 0) |
| 3025 | { |
| 3026 | infoLog << "Two transform feedback varyings specify the same output variable (" |
| 3027 | << tfVaryingName << ")."; |
| 3028 | return false; |
| 3029 | } |
| 3030 | } |
| 3031 | uniqueNames.insert(tfVaryingName); |
| 3032 | } |
| 3033 | |
| 3034 | // Validate against program varyings. |
| 3035 | size_t totalComponents = 0; |
| 3036 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
| 3037 | { |
| 3038 | std::vector<unsigned int> subscripts; |
| 3039 | std::string baseName = ParseResourceName(tfVaryingName, &subscripts); |
| 3040 | |
| 3041 | const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName); |
| 3042 | if (var == nullptr) |
jchen10 | 85c93c4 | 2017-11-12 15:36:47 +0800 | [diff] [blame] | 3043 | { |
| 3044 | infoLog << "Transform feedback varying " << tfVaryingName |
| 3045 | << " does not exist in the vertex shader."; |
| 3046 | return false; |
| 3047 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 3048 | |
jchen10 | 8225e73 | 2017-11-14 16:29:03 +0800 | [diff] [blame] | 3049 | // Validate the matching variable. |
| 3050 | if (var->isStruct()) |
| 3051 | { |
| 3052 | infoLog << "Struct cannot be captured directly (" << baseName << ")."; |
| 3053 | return false; |
| 3054 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 3055 | |
jchen10 | 8225e73 | 2017-11-14 16:29:03 +0800 | [diff] [blame] | 3056 | size_t elementCount = 0; |
| 3057 | size_t componentCount = 0; |
| 3058 | |
| 3059 | if (var->isArray()) |
| 3060 | { |
| 3061 | if (context->getClientVersion() < Version(3, 1)) |
| 3062 | { |
| 3063 | infoLog << "Capture of arrays is undefined and not supported."; |
| 3064 | return false; |
| 3065 | } |
| 3066 | |
| 3067 | // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays. |
| 3068 | ASSERT(!var->isArrayOfArrays()); |
| 3069 | |
| 3070 | if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize()) |
| 3071 | { |
| 3072 | infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'."; |
| 3073 | return false; |
| 3074 | } |
| 3075 | elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1); |
| 3076 | } |
| 3077 | else |
| 3078 | { |
| 3079 | if (!subscripts.empty()) |
| 3080 | { |
| 3081 | infoLog << "Varying '" << baseName |
| 3082 | << "' is not an array to be captured by element."; |
| 3083 | return false; |
| 3084 | } |
| 3085 | elementCount = 1; |
| 3086 | } |
| 3087 | |
| 3088 | // TODO(jmadill): Investigate implementation limits on D3D11 |
| 3089 | componentCount = VariableComponentCount(var->type) * elementCount; |
| 3090 | if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS && |
| 3091 | componentCount > caps.maxTransformFeedbackSeparateComponents) |
| 3092 | { |
| 3093 | infoLog << "Transform feedback varying " << tfVaryingName << " components (" |
| 3094 | << componentCount << ") exceed the maximum separate components (" |
| 3095 | << caps.maxTransformFeedbackSeparateComponents << ")."; |
| 3096 | return false; |
| 3097 | } |
| 3098 | |
| 3099 | totalComponents += componentCount; |
| 3100 | if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && |
| 3101 | totalComponents > caps.maxTransformFeedbackInterleavedComponents) |
| 3102 | { |
| 3103 | infoLog << "Transform feedback varying total components (" << totalComponents |
| 3104 | << ") exceed the maximum interleaved components (" |
| 3105 | << caps.maxTransformFeedbackInterleavedComponents << ")."; |
| 3106 | return false; |
| 3107 | } |
| 3108 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 3109 | return true; |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 3110 | } |
| 3111 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3112 | bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const |
| 3113 | { |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3114 | const std::vector<sh::Attribute> &attributes = |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3115 | mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes(context); |
| 3116 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3117 | for (const auto &attrib : attributes) |
| 3118 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3119 | for (ShaderType shaderType : kAllGraphicsShaderTypes) |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3120 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3121 | Shader *shader = mState.mAttachedShaders[shaderType]; |
| 3122 | if (!shader) |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3123 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3124 | continue; |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3125 | } |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3126 | |
| 3127 | const std::vector<sh::Uniform> &uniforms = shader->getUniforms(context); |
| 3128 | for (const auto &uniform : uniforms) |
Jiawei Shao | 0d88ec9 | 2018-02-27 16:25:31 +0800 | [diff] [blame] | 3129 | { |
| 3130 | if (uniform.name == attrib.name) |
| 3131 | { |
| 3132 | infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name; |
| 3133 | return false; |
| 3134 | } |
| 3135 | } |
| 3136 | } |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3137 | } |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3138 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 3139 | return true; |
| 3140 | } |
| 3141 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 3142 | void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3143 | { |
| 3144 | // Gather the linked varyings that are used for transform feedback, they should all exist. |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 3145 | mState.mLinkedTransformFeedbackVaryings.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 3146 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3147 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 3148 | std::vector<unsigned int> subscripts; |
| 3149 | std::string baseName = ParseResourceName(tfVaryingName, &subscripts); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 3150 | size_t subscript = GL_INVALID_INDEX; |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 3151 | if (!subscripts.empty()) |
| 3152 | { |
| 3153 | subscript = subscripts.back(); |
| 3154 | } |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 3155 | for (const auto &ref : varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3156 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 3157 | const sh::Varying *varying = ref.second.get(); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 3158 | if (baseName == varying->name) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3159 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 3160 | mState.mLinkedTransformFeedbackVaryings.emplace_back( |
| 3161 | *varying, static_cast<GLuint>(subscript)); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3162 | break; |
| 3163 | } |
jchen10 | 8225e73 | 2017-11-14 16:29:03 +0800 | [diff] [blame] | 3164 | else if (varying->isStruct()) |
| 3165 | { |
| 3166 | const auto *field = FindShaderVarField(*varying, tfVaryingName); |
| 3167 | if (field != nullptr) |
| 3168 | { |
| 3169 | mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying); |
| 3170 | break; |
| 3171 | } |
| 3172 | } |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3173 | } |
| 3174 | } |
James Darpinian | 30b604d | 2018-03-12 17:26:57 -0700 | [diff] [blame] | 3175 | mState.updateTransformFeedbackStrides(); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3176 | } |
| 3177 | |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 3178 | ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3179 | { |
Jamie Madill | 3c1da04 | 2017-11-27 18:33:40 -0500 | [diff] [blame] | 3180 | ProgramMergedVaryings merged; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3181 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3182 | for (const sh::Varying &varying : |
| 3183 | mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings(context)) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3184 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 3185 | merged[varying.name].vertex = &varying; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3186 | } |
| 3187 | |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3188 | for (const sh::Varying &varying : |
| 3189 | mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings(context)) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 3190 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 3191 | merged[varying.name].fragment = &varying; |
| 3192 | } |
| 3193 | |
| 3194 | return merged; |
| 3195 | } |
| 3196 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 3197 | void Program::linkOutputVariables(const Context *context) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3198 | { |
Jiawei Shao | 016105b | 2018-04-12 16:38:31 +0800 | [diff] [blame] | 3199 | Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment]; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3200 | ASSERT(fragmentShader != nullptr); |
| 3201 | |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 3202 | ASSERT(mState.mOutputVariableTypes.empty()); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 3203 | ASSERT(mState.mActiveOutputVariables.none()); |
Brandon Jones | 76746f9 | 2017-11-22 11:44:41 -0800 | [diff] [blame] | 3204 | ASSERT(mState.mDrawBufferTypeMask.none()); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 3205 | |
| 3206 | // Gather output variable types |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 3207 | for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context)) |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 3208 | { |
| 3209 | if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" && |
| 3210 | outputVariable.name != "gl_FragData") |
| 3211 | { |
| 3212 | continue; |
| 3213 | } |
| 3214 | |
| 3215 | unsigned int baseLocation = |
| 3216 | (outputVariable.location == -1 ? 0u |
| 3217 | : static_cast<unsigned int>(outputVariable.location)); |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 3218 | |
| 3219 | // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of |
| 3220 | // structures, so we may use getBasicTypeElementCount(). |
| 3221 | unsigned int elementCount = outputVariable.getBasicTypeElementCount(); |
| 3222 | for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++) |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 3223 | { |
| 3224 | const unsigned int location = baseLocation + elementIndex; |
| 3225 | if (location >= mState.mOutputVariableTypes.size()) |
| 3226 | { |
| 3227 | mState.mOutputVariableTypes.resize(location + 1, GL_NONE); |
| 3228 | } |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 3229 | ASSERT(location < mState.mActiveOutputVariables.size()); |
| 3230 | mState.mActiveOutputVariables.set(location); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 3231 | mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type); |
Brandon Jones | 76746f9 | 2017-11-22 11:44:41 -0800 | [diff] [blame] | 3232 | mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3236 | // Skip this step for GLES2 shaders. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 3237 | if (fragmentShader->getShaderVersion(context) == 100) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3238 | return; |
| 3239 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 3240 | mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3241 | // TODO(jmadill): any caps validation here? |
| 3242 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 3243 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size(); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3244 | outputVariableIndex++) |
| 3245 | { |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 3246 | const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex]; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3247 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 3248 | if (outputVariable.isArray()) |
| 3249 | { |
| 3250 | // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active |
| 3251 | // Resources and including [0] at the end of array variable names. |
| 3252 | mState.mOutputVariables[outputVariableIndex].name += "[0]"; |
| 3253 | mState.mOutputVariables[outputVariableIndex].mappedName += "[0]"; |
| 3254 | } |
| 3255 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3256 | // Don't store outputs for gl_FragDepth, gl_FragColor, etc. |
| 3257 | if (outputVariable.isBuiltIn()) |
| 3258 | continue; |
| 3259 | |
| 3260 | // Since multiple output locations must be specified, use 0 for non-specified locations. |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 3261 | unsigned int baseLocation = |
| 3262 | (outputVariable.location == -1 ? 0u |
| 3263 | : static_cast<unsigned int>(outputVariable.location)); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3264 | |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 3265 | // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of |
| 3266 | // structures, so we may use getBasicTypeElementCount(). |
| 3267 | unsigned int elementCount = outputVariable.getBasicTypeElementCount(); |
| 3268 | for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3269 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 3270 | const unsigned int location = baseLocation + elementIndex; |
| 3271 | if (location >= mState.mOutputLocations.size()) |
| 3272 | { |
| 3273 | mState.mOutputLocations.resize(location + 1); |
| 3274 | } |
| 3275 | ASSERT(!mState.mOutputLocations.at(location).used()); |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 3276 | if (outputVariable.isArray()) |
| 3277 | { |
| 3278 | mState.mOutputLocations[location] = |
| 3279 | VariableLocation(elementIndex, outputVariableIndex); |
| 3280 | } |
| 3281 | else |
| 3282 | { |
| 3283 | VariableLocation locationInfo; |
| 3284 | locationInfo.index = outputVariableIndex; |
| 3285 | mState.mOutputLocations[location] = locationInfo; |
| 3286 | } |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 3287 | } |
| 3288 | } |
| 3289 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3290 | |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 3291 | void Program::setUniformValuesFromBindingQualifiers() |
| 3292 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 3293 | for (unsigned int samplerIndex : mState.mSamplerUniformRange) |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 3294 | { |
| 3295 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
| 3296 | if (samplerUniform.binding != -1) |
| 3297 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 3298 | GLint location = getUniformLocation(samplerUniform.name); |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 3299 | ASSERT(location != -1); |
| 3300 | std::vector<GLint> boundTextureUnits; |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 3301 | for (unsigned int elementIndex = 0; |
| 3302 | elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex) |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 3303 | { |
| 3304 | boundTextureUnits.push_back(samplerUniform.binding + elementIndex); |
| 3305 | } |
| 3306 | setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()), |
| 3307 | boundTextureUnits.data()); |
| 3308 | } |
| 3309 | } |
| 3310 | } |
| 3311 | |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 3312 | void Program::initInterfaceBlockBindings() |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3313 | { |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 3314 | // Set initial bindings from shader. |
| 3315 | for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++) |
| 3316 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3317 | InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex]; |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 3318 | bindUniformBlock(blockIndex, uniformBlock.binding); |
| 3319 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3320 | } |
| 3321 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3322 | void Program::updateSamplerUniform(const VariableLocation &locationInfo, |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3323 | GLsizei clampedCount, |
| 3324 | const GLint *v) |
| 3325 | { |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 3326 | ASSERT(mState.isSamplerUniformIndex(locationInfo.index)); |
| 3327 | GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index); |
| 3328 | std::vector<GLuint> *boundTextureUnits = |
| 3329 | &mState.mSamplerBindings[samplerIndex].boundTextureUnits; |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3330 | |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 3331 | std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex); |
Jamie Madill | d68248b | 2017-09-11 14:34:14 -0400 | [diff] [blame] | 3332 | |
| 3333 | // Invalidate the validation cache. |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 3334 | mCachedValidateSamplersResult.reset(); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | template <typename T> |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3338 | GLsizei Program::clampUniformCount(const VariableLocation &locationInfo, |
| 3339 | GLsizei count, |
| 3340 | int vectorSize, |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3341 | const T *v) |
| 3342 | { |
Jamie Madill | 134f93d | 2017-08-31 17:11:00 -0400 | [diff] [blame] | 3343 | if (count == 1) |
| 3344 | return 1; |
| 3345 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3346 | const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3347 | |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 3348 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 3349 | // element index used, as reported by GetActiveUniform, will be ignored by the GL." |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 3350 | unsigned int remainingElements = |
| 3351 | linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex; |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3352 | GLsizei maxElementCount = |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3353 | static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents()); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3354 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3355 | if (count * vectorSize > maxElementCount) |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3356 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3357 | return maxElementCount / vectorSize; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3358 | } |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3359 | |
| 3360 | return count; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | template <size_t cols, size_t rows, typename T> |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3364 | GLsizei Program::clampMatrixUniformCount(GLint location, |
| 3365 | GLsizei count, |
| 3366 | GLboolean transpose, |
| 3367 | const T *v) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3368 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3369 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 3370 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3371 | if (!transpose) |
| 3372 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3373 | return clampUniformCount(locationInfo, count, cols * rows, v); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3374 | } |
| 3375 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3376 | const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index]; |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 3377 | |
| 3378 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 3379 | // element index used, as reported by GetActiveUniform, will be ignored by the GL." |
Olli Etuaho | 465835d | 2017-09-26 13:34:10 +0300 | [diff] [blame] | 3380 | unsigned int remainingElements = |
| 3381 | linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex; |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3382 | return std::min(count, static_cast<GLsizei>(remainingElements)); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3383 | } |
| 3384 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3385 | // Driver differences mean that doing the uniform value cast ourselves gives consistent results. |
| 3386 | // EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT. |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3387 | template <typename DestT> |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3388 | void Program::getUniformInternal(const Context *context, |
| 3389 | DestT *dataOut, |
| 3390 | GLint location, |
| 3391 | GLenum nativeType, |
| 3392 | int components) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3393 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3394 | switch (nativeType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3395 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3396 | case GL_BOOL: |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3397 | { |
| 3398 | GLint tempValue[16] = {0}; |
| 3399 | mProgram->getUniformiv(context, location, tempValue); |
| 3400 | UniformStateQueryCastLoop<GLboolean>( |
| 3401 | dataOut, reinterpret_cast<const uint8_t *>(tempValue), components); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3402 | break; |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3403 | } |
| 3404 | case GL_INT: |
| 3405 | { |
| 3406 | GLint tempValue[16] = {0}; |
| 3407 | mProgram->getUniformiv(context, location, tempValue); |
| 3408 | UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue), |
| 3409 | components); |
| 3410 | break; |
| 3411 | } |
| 3412 | case GL_UNSIGNED_INT: |
| 3413 | { |
| 3414 | GLuint tempValue[16] = {0}; |
| 3415 | mProgram->getUniformuiv(context, location, tempValue); |
| 3416 | UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue), |
| 3417 | components); |
| 3418 | break; |
| 3419 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3420 | case GL_FLOAT: |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3421 | { |
| 3422 | GLfloat tempValue[16] = {0}; |
| 3423 | mProgram->getUniformfv(context, location, tempValue); |
| 3424 | UniformStateQueryCastLoop<GLfloat>( |
| 3425 | dataOut, reinterpret_cast<const uint8_t *>(tempValue), components); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3426 | break; |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3427 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3428 | default: |
| 3429 | UNREACHABLE(); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3430 | break; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3431 | } |
| 3432 | } |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 3433 | |
| 3434 | bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const |
| 3435 | { |
| 3436 | // Must be called after samplers are validated. |
| 3437 | ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value()); |
| 3438 | |
| 3439 | for (const auto &binding : mState.mSamplerBindings) |
| 3440 | { |
Corentin Wallez | f0e89be | 2017-11-08 14:00:32 -0800 | [diff] [blame] | 3441 | TextureType textureType = binding.textureType; |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 3442 | for (const auto &unit : binding.boundTextureUnits) |
| 3443 | { |
| 3444 | GLenum programTextureID = state.getSamplerTextureId(unit, textureType); |
| 3445 | if (programTextureID == textureID) |
| 3446 | { |
| 3447 | // TODO(jmadill): Check for appropriate overlap. |
| 3448 | return true; |
| 3449 | } |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | return false; |
| 3454 | } |
| 3455 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 3456 | } // namespace gl |