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