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