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