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