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