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