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