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