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