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