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