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 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 109 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 110 | template <typename VarT> |
| 111 | GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name) |
| 112 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 113 | std::string nameAsArrayName = name + "[0]"; |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 114 | for (size_t index = 0; index < list.size(); index++) |
| 115 | { |
| 116 | const VarT &resource = list[index]; |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 117 | if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName)) |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 118 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 119 | return static_cast<GLuint>(index); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | return GL_INVALID_INDEX; |
| 124 | } |
| 125 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 126 | template <typename VarT> |
| 127 | GLint GetVariableLocation(const std::vector<VarT> &list, |
| 128 | const std::vector<VariableLocation> &locationList, |
| 129 | const std::string &name) |
| 130 | { |
| 131 | size_t nameLengthWithoutArrayIndex; |
| 132 | unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex); |
| 133 | |
| 134 | for (size_t location = 0u; location < locationList.size(); ++location) |
| 135 | { |
| 136 | const VariableLocation &variableLocation = locationList[location]; |
| 137 | if (!variableLocation.used()) |
| 138 | { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | const VarT &variable = list[variableLocation.index]; |
| 143 | |
| 144 | if (angle::BeginsWith(variable.name, name)) |
| 145 | { |
| 146 | if (name.length() == variable.name.length()) |
| 147 | { |
| 148 | ASSERT(name == variable.name); |
| 149 | // GLES 3.1 November 2016 page 87. |
| 150 | // The string exactly matches the name of the active variable. |
| 151 | return static_cast<GLint>(location); |
| 152 | } |
| 153 | if (name.length() + 3u == variable.name.length() && variable.isArray()) |
| 154 | { |
| 155 | ASSERT(name + "[0]" == variable.name); |
| 156 | // The string identifies the base name of an active array, where the string would |
| 157 | // exactly match the name of the variable if the suffix "[0]" were appended to the |
| 158 | // string. |
| 159 | return static_cast<GLint>(location); |
| 160 | } |
| 161 | } |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 162 | if (variable.isArray() && variableLocation.arrayIndex == arrayIndex && |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 163 | nameLengthWithoutArrayIndex + 3u == variable.name.length() && |
| 164 | angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex)) |
| 165 | { |
| 166 | ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name); |
| 167 | // The string identifies an active element of the array, where the string ends with the |
| 168 | // concatenation of the "[" character, an integer (with no "+" sign, extra leading |
| 169 | // zeroes, or whitespace) identifying an array element, and the "]" character, the |
| 170 | // integer is less than the number of active elements of the array variable, and where |
| 171 | // the string would exactly match the enumerated name of the array if the decimal |
| 172 | // integer were replaced with zero. |
| 173 | return static_cast<GLint>(location); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return -1; |
| 178 | } |
| 179 | |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 180 | void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length) |
| 181 | { |
| 182 | ASSERT(bufSize > 0); |
| 183 | strncpy(buffer, string.c_str(), bufSize); |
| 184 | buffer[bufSize - 1] = '\0'; |
| 185 | |
| 186 | if (length) |
| 187 | { |
| 188 | *length = static_cast<GLsizei>(strlen(buffer)); |
| 189 | } |
| 190 | } |
| 191 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 192 | bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name) |
| 193 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 194 | std::vector<unsigned int> subscripts; |
| 195 | std::string baseName = ParseResourceName(name, &subscripts); |
| 196 | for (auto nameInSet : nameSet) |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 197 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 198 | std::vector<unsigned int> arrayIndices; |
| 199 | std::string arrayName = ParseResourceName(nameInSet, &arrayIndices); |
| 200 | if (baseName == arrayName && |
| 201 | (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices)) |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 202 | { |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 209 | bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks, |
| 210 | const std::vector<sh::InterfaceBlock> &interfaceBlocks, |
| 211 | const std::string &errorMessage, |
| 212 | InfoLog &infoLog) |
| 213 | { |
| 214 | GLuint blockCount = 0; |
| 215 | for (const sh::InterfaceBlock &block : interfaceBlocks) |
| 216 | { |
| 217 | if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED) |
| 218 | { |
| 219 | blockCount += (block.arraySize ? block.arraySize : 1); |
| 220 | if (blockCount > maxInterfaceBlocks) |
| 221 | { |
| 222 | infoLog << errorMessage << maxInterfaceBlocks << ")"; |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 230 | GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name) |
| 231 | { |
| 232 | std::vector<unsigned int> subscripts; |
| 233 | std::string baseName = ParseResourceName(name, &subscripts); |
| 234 | |
| 235 | unsigned int numBlocks = static_cast<unsigned int>(list.size()); |
| 236 | for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++) |
| 237 | { |
| 238 | const auto &block = list[blockIndex]; |
| 239 | if (block.name == baseName) |
| 240 | { |
| 241 | const bool arrayElementZero = |
| 242 | (subscripts.empty() && (!block.isArray || block.arrayElement == 0)); |
| 243 | const bool arrayElementMatches = |
| 244 | (subscripts.size() == 1 && subscripts[0] == block.arrayElement); |
| 245 | if (arrayElementMatches || arrayElementZero) |
| 246 | { |
| 247 | return blockIndex; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return GL_INVALID_INDEX; |
| 253 | } |
| 254 | |
| 255 | void GetInterfaceBlockName(const GLuint index, |
| 256 | const std::vector<InterfaceBlock> &list, |
| 257 | GLsizei bufSize, |
| 258 | GLsizei *length, |
| 259 | GLchar *name) |
| 260 | { |
| 261 | ASSERT(index < list.size()); |
| 262 | |
| 263 | const auto &block = list[index]; |
| 264 | |
| 265 | if (bufSize > 0) |
| 266 | { |
| 267 | std::string blockName = block.name; |
| 268 | |
| 269 | if (block.isArray) |
| 270 | { |
| 271 | blockName += ArrayString(block.arrayElement); |
| 272 | } |
| 273 | CopyStringToBuffer(name, blockName, bufSize, length); |
| 274 | } |
| 275 | } |
| 276 | |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 277 | void InitUniformBlockLinker(const gl::Context *context, |
| 278 | const ProgramState &state, |
| 279 | UniformBlockLinker *blockLinker) |
| 280 | { |
| 281 | if (state.getAttachedVertexShader()) |
| 282 | { |
| 283 | blockLinker->addShaderBlocks(GL_VERTEX_SHADER, |
| 284 | &state.getAttachedVertexShader()->getUniformBlocks(context)); |
| 285 | } |
| 286 | |
| 287 | if (state.getAttachedFragmentShader()) |
| 288 | { |
| 289 | blockLinker->addShaderBlocks(GL_FRAGMENT_SHADER, |
| 290 | &state.getAttachedFragmentShader()->getUniformBlocks(context)); |
| 291 | } |
| 292 | |
| 293 | if (state.getAttachedComputeShader()) |
| 294 | { |
| 295 | blockLinker->addShaderBlocks(GL_COMPUTE_SHADER, |
| 296 | &state.getAttachedComputeShader()->getUniformBlocks(context)); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void InitShaderStorageBlockLinker(const gl::Context *context, |
| 301 | const ProgramState &state, |
| 302 | ShaderStorageBlockLinker *blockLinker) |
| 303 | { |
| 304 | if (state.getAttachedVertexShader()) |
| 305 | { |
| 306 | blockLinker->addShaderBlocks( |
| 307 | GL_VERTEX_SHADER, &state.getAttachedVertexShader()->getShaderStorageBlocks(context)); |
| 308 | } |
| 309 | |
| 310 | if (state.getAttachedFragmentShader()) |
| 311 | { |
| 312 | blockLinker->addShaderBlocks( |
| 313 | GL_FRAGMENT_SHADER, |
| 314 | &state.getAttachedFragmentShader()->getShaderStorageBlocks(context)); |
| 315 | } |
| 316 | |
| 317 | if (state.getAttachedComputeShader()) |
| 318 | { |
| 319 | blockLinker->addShaderBlocks( |
| 320 | GL_COMPUTE_SHADER, &state.getAttachedComputeShader()->getShaderStorageBlocks(context)); |
| 321 | } |
| 322 | } |
| 323 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 324 | } // anonymous namespace |
| 325 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 326 | const char *const g_fakepath = "C:\\fakepath"; |
| 327 | |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 328 | InfoLog::InfoLog() |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 329 | { |
| 330 | } |
| 331 | |
| 332 | InfoLog::~InfoLog() |
| 333 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 336 | size_t InfoLog::getLength() const |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 337 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 338 | if (!mLazyStream) |
| 339 | { |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | const std::string &logString = mLazyStream->str(); |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 344 | return logString.empty() ? 0 : logString.length() + 1; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 347 | void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 348 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 349 | size_t index = 0; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 350 | |
| 351 | if (bufSize > 0) |
| 352 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 353 | const std::string logString(str()); |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 354 | |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 355 | if (!logString.empty()) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 356 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 357 | index = std::min(static_cast<size_t>(bufSize) - 1, logString.length()); |
| 358 | memcpy(infoLog, logString.c_str(), index); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | infoLog[index] = '\0'; |
| 362 | } |
| 363 | |
| 364 | if (length) |
| 365 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 366 | *length = static_cast<GLsizei>(index); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // append a santized message to the program info log. |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 371 | // 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] | 372 | // messages, so lets remove all occurrences of this fake file path from the log. |
| 373 | void InfoLog::appendSanitized(const char *message) |
| 374 | { |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 375 | ensureInitialized(); |
| 376 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 377 | std::string msg(message); |
| 378 | |
| 379 | size_t found; |
| 380 | do |
| 381 | { |
| 382 | found = msg.find(g_fakepath); |
| 383 | if (found != std::string::npos) |
| 384 | { |
| 385 | msg.erase(found, strlen(g_fakepath)); |
| 386 | } |
| 387 | } |
| 388 | while (found != std::string::npos); |
| 389 | |
Jamie Madill | 23176ce | 2017-07-31 14:14:33 -0400 | [diff] [blame] | 390 | *mLazyStream << message << std::endl; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 391 | } |
| 392 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 393 | void InfoLog::reset() |
| 394 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 397 | VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 398 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 399 | } |
| 400 | |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 401 | VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index) |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 402 | : arrayIndex(arrayIndex), index(index), ignored(false) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 403 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 404 | ASSERT(arrayIndex != GL_INVALID_INDEX); |
| 405 | } |
| 406 | |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 407 | void Program::Bindings::bindLocation(GLuint index, const std::string &name) |
| 408 | { |
| 409 | mBindings[name] = index; |
| 410 | } |
| 411 | |
| 412 | int Program::Bindings::getBinding(const std::string &name) const |
| 413 | { |
| 414 | auto iter = mBindings.find(name); |
| 415 | return (iter != mBindings.end()) ? iter->second : -1; |
| 416 | } |
| 417 | |
| 418 | Program::Bindings::const_iterator Program::Bindings::begin() const |
| 419 | { |
| 420 | return mBindings.begin(); |
| 421 | } |
| 422 | |
| 423 | Program::Bindings::const_iterator Program::Bindings::end() const |
| 424 | { |
| 425 | return mBindings.end(); |
| 426 | } |
| 427 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 428 | ProgramState::ProgramState() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 429 | : mLabel(), |
| 430 | mAttachedFragmentShader(nullptr), |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 431 | mAttachedVertexShader(nullptr), |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 432 | mAttachedComputeShader(nullptr), |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 433 | mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS), |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 434 | mMaxActiveAttribLocation(0), |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 435 | mSamplerUniformRange(0, 0), |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 436 | mImageUniformRange(0, 0), |
| 437 | mAtomicCounterUniformRange(0, 0), |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 438 | mBinaryRetrieveableHint(false), |
| 439 | mNumViews(-1) |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 440 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 441 | mComputeShaderLocalSize.fill(1); |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 444 | ProgramState::~ProgramState() |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 445 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 446 | ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader); |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 449 | const std::string &ProgramState::getLabel() |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 450 | { |
| 451 | return mLabel; |
| 452 | } |
| 453 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 454 | GLuint ProgramState::getUniformIndexFromName(const std::string &name) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 455 | { |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 456 | return GetResourceIndexFromName(mUniforms, name); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 459 | GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const |
| 460 | { |
| 461 | return GetResourceIndexFromName(mBufferVariables, name); |
| 462 | } |
| 463 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 464 | GLuint ProgramState::getUniformIndexFromLocation(GLint location) const |
| 465 | { |
| 466 | ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size()); |
| 467 | return mUniformLocations[location].index; |
| 468 | } |
| 469 | |
| 470 | Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const |
| 471 | { |
| 472 | GLuint index = getUniformIndexFromLocation(location); |
| 473 | if (!isSamplerUniformIndex(index)) |
| 474 | { |
| 475 | return Optional<GLuint>::Invalid(); |
| 476 | } |
| 477 | |
| 478 | return getSamplerIndexFromUniformIndex(index); |
| 479 | } |
| 480 | |
| 481 | bool ProgramState::isSamplerUniformIndex(GLuint index) const |
| 482 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 483 | return mSamplerUniformRange.contains(index); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const |
| 487 | { |
| 488 | ASSERT(isSamplerUniformIndex(uniformIndex)); |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 489 | return uniformIndex - mSamplerUniformRange.low(); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 492 | GLuint ProgramState::getAttributeLocation(const std::string &name) const |
| 493 | { |
| 494 | for (const sh::Attribute &attribute : mAttributes) |
| 495 | { |
| 496 | if (attribute.name == name) |
| 497 | { |
| 498 | return attribute.location; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return static_cast<GLuint>(-1); |
| 503 | } |
| 504 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 505 | Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle) |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 506 | : mProgram(factory->createProgram(mState)), |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 507 | mValidated(false), |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 508 | mLinked(false), |
| 509 | mDeleteStatus(false), |
| 510 | mRefCount(0), |
| 511 | mResourceManager(manager), |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 512 | mHandle(handle) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 513 | { |
| 514 | ASSERT(mProgram); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 515 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 516 | unlink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | Program::~Program() |
| 520 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 521 | ASSERT(!mProgram); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 524 | void Program::onDestroy(const Context *context) |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 525 | { |
| 526 | if (mState.mAttachedVertexShader != nullptr) |
| 527 | { |
| 528 | mState.mAttachedVertexShader->release(context); |
| 529 | mState.mAttachedVertexShader = nullptr; |
| 530 | } |
| 531 | |
| 532 | if (mState.mAttachedFragmentShader != nullptr) |
| 533 | { |
| 534 | mState.mAttachedFragmentShader->release(context); |
| 535 | mState.mAttachedFragmentShader = nullptr; |
| 536 | } |
| 537 | |
| 538 | if (mState.mAttachedComputeShader != nullptr) |
| 539 | { |
| 540 | mState.mAttachedComputeShader->release(context); |
| 541 | mState.mAttachedComputeShader = nullptr; |
| 542 | } |
| 543 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 544 | mProgram->destroy(context); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 545 | |
| 546 | ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader && |
| 547 | !mState.mAttachedComputeShader); |
| 548 | SafeDelete(mProgram); |
| 549 | |
| 550 | delete this; |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 553 | void Program::setLabel(const std::string &label) |
| 554 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 555 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | const std::string &Program::getLabel() const |
| 559 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 560 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 561 | } |
| 562 | |
Jamie Madill | ef300b1 | 2016-10-07 15:12:09 -0400 | [diff] [blame] | 563 | void Program::attachShader(Shader *shader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 564 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 565 | switch (shader->getType()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 566 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 567 | case GL_VERTEX_SHADER: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 568 | { |
Jamie Madill | ef300b1 | 2016-10-07 15:12:09 -0400 | [diff] [blame] | 569 | ASSERT(!mState.mAttachedVertexShader); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 570 | mState.mAttachedVertexShader = shader; |
| 571 | mState.mAttachedVertexShader->addRef(); |
| 572 | break; |
| 573 | } |
| 574 | case GL_FRAGMENT_SHADER: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 575 | { |
Jamie Madill | ef300b1 | 2016-10-07 15:12:09 -0400 | [diff] [blame] | 576 | ASSERT(!mState.mAttachedFragmentShader); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 577 | mState.mAttachedFragmentShader = shader; |
| 578 | mState.mAttachedFragmentShader->addRef(); |
| 579 | break; |
| 580 | } |
| 581 | case GL_COMPUTE_SHADER: |
| 582 | { |
Jamie Madill | ef300b1 | 2016-10-07 15:12:09 -0400 | [diff] [blame] | 583 | ASSERT(!mState.mAttachedComputeShader); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 584 | mState.mAttachedComputeShader = shader; |
| 585 | mState.mAttachedComputeShader->addRef(); |
| 586 | break; |
| 587 | } |
| 588 | default: |
| 589 | UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 590 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Jamie Madill | c1d770e | 2017-04-13 17:31:24 -0400 | [diff] [blame] | 593 | void Program::detachShader(const Context *context, Shader *shader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 594 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 595 | switch (shader->getType()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 596 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 597 | case GL_VERTEX_SHADER: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 598 | { |
Jamie Madill | c1d770e | 2017-04-13 17:31:24 -0400 | [diff] [blame] | 599 | ASSERT(mState.mAttachedVertexShader == shader); |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 600 | shader->release(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 601 | mState.mAttachedVertexShader = nullptr; |
| 602 | break; |
| 603 | } |
| 604 | case GL_FRAGMENT_SHADER: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 605 | { |
Jamie Madill | c1d770e | 2017-04-13 17:31:24 -0400 | [diff] [blame] | 606 | ASSERT(mState.mAttachedFragmentShader == shader); |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 607 | shader->release(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 608 | mState.mAttachedFragmentShader = nullptr; |
| 609 | break; |
| 610 | } |
| 611 | case GL_COMPUTE_SHADER: |
| 612 | { |
Jamie Madill | c1d770e | 2017-04-13 17:31:24 -0400 | [diff] [blame] | 613 | ASSERT(mState.mAttachedComputeShader == shader); |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 614 | shader->release(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 615 | mState.mAttachedComputeShader = nullptr; |
| 616 | break; |
| 617 | } |
| 618 | default: |
| 619 | UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 620 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 621 | } |
| 622 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 623 | int Program::getAttachedShadersCount() const |
| 624 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 625 | return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) + |
| 626 | (mState.mAttachedComputeShader ? 1 : 0); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 627 | } |
| 628 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 629 | void Program::bindAttributeLocation(GLuint index, const char *name) |
| 630 | { |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 631 | mAttributeBindings.bindLocation(index, name); |
| 632 | } |
| 633 | |
| 634 | void Program::bindUniformLocation(GLuint index, const char *name) |
| 635 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 636 | mUniformLocationBindings.bindLocation(index, name); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 639 | void Program::bindFragmentInputLocation(GLint index, const char *name) |
| 640 | { |
| 641 | mFragmentInputBindings.bindLocation(index, name); |
| 642 | } |
| 643 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 644 | BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 645 | { |
| 646 | BindingInfo ret; |
| 647 | ret.type = GL_NONE; |
| 648 | ret.valid = false; |
| 649 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 650 | Shader *fragmentShader = mState.getAttachedFragmentShader(); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 651 | ASSERT(fragmentShader); |
| 652 | |
| 653 | // Find the actual fragment shader varying we're interested in |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 654 | const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 655 | |
| 656 | for (const auto &binding : mFragmentInputBindings) |
| 657 | { |
| 658 | if (binding.second != static_cast<GLuint>(index)) |
| 659 | continue; |
| 660 | |
| 661 | ret.valid = true; |
| 662 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 663 | size_t nameLengthWithoutArrayIndex; |
| 664 | unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 665 | |
| 666 | for (const auto &in : inputs) |
| 667 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 668 | if (in.name.length() == nameLengthWithoutArrayIndex && |
| 669 | angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex)) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 670 | { |
| 671 | if (in.isArray()) |
| 672 | { |
| 673 | // The client wants to bind either "name" or "name[0]". |
| 674 | // GL ES 3.1 spec refers to active array names with language such as: |
| 675 | // "if the string identifies the base name of an active array, where the |
| 676 | // string would exactly match the name of the variable if the suffix "[0]" |
| 677 | // were appended to the string". |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 678 | if (arrayIndex == GL_INVALID_INDEX) |
| 679 | arrayIndex = 0; |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 680 | |
Corentin Wallez | 054f7ed | 2016-09-20 17:15:59 -0400 | [diff] [blame] | 681 | ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]"; |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 682 | } |
| 683 | else |
| 684 | { |
| 685 | ret.name = in.mappedName; |
| 686 | } |
| 687 | ret.type = in.type; |
| 688 | return ret; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return ret; |
| 694 | } |
| 695 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 696 | void Program::pathFragmentInputGen(const Context *context, |
| 697 | GLint index, |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 698 | GLenum genMode, |
| 699 | GLint components, |
| 700 | const GLfloat *coeffs) |
| 701 | { |
| 702 | // If the location is -1 then the command is silently ignored |
| 703 | if (index == -1) |
| 704 | return; |
| 705 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 706 | const auto &binding = getFragmentInputBindingInfo(context, index); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 707 | |
| 708 | // If the input doesn't exist then then the command is silently ignored |
| 709 | // This could happen through optimization for example, the shader translator |
| 710 | // decides that a variable is not actually being used and optimizes it away. |
| 711 | if (binding.name.empty()) |
| 712 | return; |
| 713 | |
| 714 | mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs); |
| 715 | } |
| 716 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 717 | // The attached shaders are checked for linking errors by matching up their variables. |
| 718 | // Uniform, input and output variables get collected. |
| 719 | // The code gets compiled into binaries. |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 720 | Error Program::link(const gl::Context *context) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 721 | { |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 722 | const auto &data = context->getContextState(); |
| 723 | |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 724 | auto *platform = ANGLEPlatformCurrent(); |
| 725 | double startTime = platform->currentTime(platform); |
| 726 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 727 | unlink(); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 728 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 729 | ProgramHash programHash; |
| 730 | auto *cache = context->getMemoryProgramCache(); |
| 731 | if (cache) |
| 732 | { |
| 733 | ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked); |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 734 | ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (mLinked) |
| 738 | { |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 739 | double delta = platform->currentTime(platform) - startTime; |
| 740 | int us = static_cast<int>(delta * 1000000.0); |
| 741 | ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 742 | return NoError(); |
| 743 | } |
| 744 | |
| 745 | // Cache load failed, fall through to normal linking. |
| 746 | unlink(); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 747 | mInfoLog.reset(); |
| 748 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 749 | const Caps &caps = data.getCaps(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 750 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 751 | auto vertexShader = mState.mAttachedVertexShader; |
| 752 | auto fragmentShader = mState.mAttachedFragmentShader; |
| 753 | auto computeShader = mState.mAttachedComputeShader; |
| 754 | |
| 755 | bool isComputeShaderAttached = (computeShader != nullptr); |
| 756 | bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 757 | // Check whether we both have a compute and non-compute shaders attached. |
| 758 | // If there are of both types attached, then linking should fail. |
| 759 | // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram |
| 760 | if (isComputeShaderAttached == true && nonComputeShadersAttached == true) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 761 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 762 | mInfoLog << "Both a compute and non-compute shaders are attached to the same program."; |
| 763 | return NoError(); |
Yuly Novikov | cfa48d3 | 2016-06-15 22:14:36 -0400 | [diff] [blame] | 764 | } |
| 765 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 766 | if (computeShader) |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 767 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 768 | if (!computeShader->isCompiled(context)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 769 | { |
| 770 | mInfoLog << "Attached compute shader is not compiled."; |
| 771 | return NoError(); |
| 772 | } |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 773 | ASSERT(computeShader->getType() == GL_COMPUTE_SHADER); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 774 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 775 | mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 776 | |
| 777 | // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs |
| 778 | // If the work group size is not specified, a link time error should occur. |
| 779 | if (!mState.mComputeShaderLocalSize.isDeclared()) |
| 780 | { |
| 781 | mInfoLog << "Work group size is not specified."; |
| 782 | return NoError(); |
| 783 | } |
| 784 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 785 | if (!linkUniforms(context, mInfoLog, mUniformLocationBindings)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 786 | { |
| 787 | return NoError(); |
| 788 | } |
| 789 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 790 | if (!linkInterfaceBlocks(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 791 | { |
| 792 | return NoError(); |
| 793 | } |
| 794 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 795 | ProgramLinkedResources resources = { |
| 796 | {0, PackMode::ANGLE_RELAXED}, |
| 797 | {&mState.mUniformBlocks, &mState.mUniforms}, |
| 798 | {&mState.mShaderStorageBlocks, &mState.mBufferVariables}}; |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 799 | |
| 800 | InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker); |
| 801 | InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker); |
| 802 | |
| 803 | ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked); |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 804 | if (!mLinked) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 805 | { |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 806 | return NoError(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | else |
| 810 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 811 | if (!fragmentShader || !fragmentShader->isCompiled(context)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 812 | { |
| 813 | return NoError(); |
| 814 | } |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 815 | ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 816 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 817 | if (!vertexShader || !vertexShader->isCompiled(context)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 818 | { |
| 819 | return NoError(); |
| 820 | } |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 821 | ASSERT(vertexShader->getType() == GL_VERTEX_SHADER); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 822 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 823 | if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 824 | { |
| 825 | mInfoLog << "Fragment shader version does not match vertex shader version."; |
| 826 | return NoError(); |
| 827 | } |
| 828 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 829 | if (!linkAttributes(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 830 | { |
| 831 | return NoError(); |
| 832 | } |
| 833 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 834 | if (!linkVaryings(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 835 | { |
| 836 | return NoError(); |
| 837 | } |
| 838 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 839 | if (!linkUniforms(context, mInfoLog, mUniformLocationBindings)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 840 | { |
| 841 | return NoError(); |
| 842 | } |
| 843 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 844 | if (!linkInterfaceBlocks(context, mInfoLog)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 845 | { |
| 846 | return NoError(); |
| 847 | } |
| 848 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 849 | if (!linkValidateGlobalNames(context, mInfoLog)) |
| 850 | { |
| 851 | return NoError(); |
| 852 | } |
| 853 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 854 | const auto &mergedVaryings = getMergedVaryings(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 855 | |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 856 | mState.mNumViews = vertexShader->getNumViews(context); |
| 857 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 858 | linkOutputVariables(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 859 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 860 | // Map the varyings to the register file |
| 861 | // In WebGL, we use a slightly different handling for packing variables. |
| 862 | auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT |
| 863 | : PackMode::ANGLE_RELAXED; |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 864 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 865 | ProgramLinkedResources resources = { |
| 866 | {data.getCaps().maxVaryingVectors, packMode}, |
| 867 | {&mState.mUniformBlocks, &mState.mUniforms}, |
| 868 | {&mState.mShaderStorageBlocks, &mState.mBufferVariables}}; |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 869 | |
| 870 | InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker); |
| 871 | InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker); |
| 872 | |
jchen10 | 85c93c4 | 2017-11-12 15:36:47 +0800 | [diff] [blame] | 873 | if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps)) |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 874 | { |
| 875 | return NoError(); |
| 876 | } |
| 877 | |
jchen10 | 85c93c4 | 2017-11-12 15:36:47 +0800 | [diff] [blame] | 878 | if (!resources.varyingPacking.collectAndPackUserVaryings( |
| 879 | mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames())) |
Olli Etuaho | 39e7812 | 2017-08-29 14:34:22 +0300 | [diff] [blame] | 880 | { |
| 881 | return NoError(); |
| 882 | } |
| 883 | |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 884 | ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked); |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 885 | if (!mLinked) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 886 | { |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 887 | return NoError(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | gatherTransformFeedbackVaryings(mergedVaryings); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 891 | } |
| 892 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 893 | gatherAtomicCounterBuffers(); |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 894 | initInterfaceBlockBindings(); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 895 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 896 | setUniformValuesFromBindingQualifiers(); |
| 897 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 898 | // Mark implementation-specific unreferenced uniforms as ignored. |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 899 | mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 900 | |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 901 | // Save to the program cache. |
| 902 | if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() || |
| 903 | !context->getWorkarounds().disableProgramCachingForTransformFeedback)) |
| 904 | { |
| 905 | cache->putProgram(programHash, context, this); |
| 906 | } |
| 907 | |
Jamie Madill | 6c58b06 | 2017-08-01 13:44:25 -0400 | [diff] [blame] | 908 | double delta = platform->currentTime(platform) - startTime; |
| 909 | int us = static_cast<int>(delta * 1000000.0); |
| 910 | ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us); |
| 911 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 912 | return NoError(); |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 913 | } |
| 914 | |
daniel@transgaming.com | aa5e59b | 2011-10-04 18:43:12 +0000 | [diff] [blame] | 915 | // 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] | 916 | void Program::unlink() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 917 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 918 | mState.mAttributes.clear(); |
| 919 | mState.mActiveAttribLocationsMask.reset(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 920 | mState.mMaxActiveAttribLocation = 0; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 921 | mState.mLinkedTransformFeedbackVaryings.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 922 | mState.mUniforms.clear(); |
| 923 | mState.mUniformLocations.clear(); |
| 924 | mState.mUniformBlocks.clear(); |
jchen10 | 7a20b97 | 2017-06-13 14:25:26 +0800 | [diff] [blame] | 925 | mState.mActiveUniformBlockBindings.reset(); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 926 | mState.mAtomicCounterBuffers.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 927 | mState.mOutputVariables.clear(); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 928 | mState.mOutputLocations.clear(); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 929 | mState.mOutputVariableTypes.clear(); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 930 | mState.mActiveOutputVariables.reset(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 931 | mState.mComputeShaderLocalSize.fill(1); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 932 | mState.mSamplerBindings.clear(); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 933 | mState.mImageBindings.clear(); |
Martin Radev | 7cf6166 | 2017-07-26 17:10:53 +0300 | [diff] [blame] | 934 | mState.mNumViews = -1; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 935 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 936 | mValidated = false; |
| 937 | |
daniel@transgaming.com | 716056c | 2012-07-24 18:38:59 +0000 | [diff] [blame] | 938 | mLinked = false; |
| 939 | } |
| 940 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 941 | bool Program::isLinked() const |
daniel@transgaming.com | 716056c | 2012-07-24 18:38:59 +0000 | [diff] [blame] | 942 | { |
| 943 | return mLinked; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 946 | Error Program::loadBinary(const Context *context, |
| 947 | GLenum binaryFormat, |
| 948 | const void *binary, |
| 949 | GLsizei length) |
apatrick@chromium.org | 3ce8dbc | 2012-06-08 17:52:30 +0000 | [diff] [blame] | 950 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 951 | unlink(); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 952 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 953 | #if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 954 | return NoError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 955 | #else |
Geoff Lang | c46cc2f | 2015-10-01 17:16:20 -0400 | [diff] [blame] | 956 | ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE); |
| 957 | if (binaryFormat != GL_PROGRAM_BINARY_ANGLE) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 958 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 959 | mInfoLog << "Invalid program binary format."; |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 960 | return NoError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 961 | } |
| 962 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 963 | const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary); |
| 964 | ANGLE_TRY_RESULT( |
| 965 | MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked); |
Jamie Madill | 3244736 | 2017-06-28 14:53:52 -0400 | [diff] [blame] | 966 | |
| 967 | // Currently we require the full shader text to compute the program hash. |
| 968 | // TODO(jmadill): Store the binary in the internal program cache. |
| 969 | |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 970 | return NoError(); |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 971 | #endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 972 | } |
| 973 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 974 | Error Program::saveBinary(const Context *context, |
| 975 | GLenum *binaryFormat, |
| 976 | void *binary, |
| 977 | GLsizei bufSize, |
| 978 | GLsizei *length) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 979 | { |
| 980 | if (binaryFormat) |
| 981 | { |
Geoff Lang | c46cc2f | 2015-10-01 17:16:20 -0400 | [diff] [blame] | 982 | *binaryFormat = GL_PROGRAM_BINARY_ANGLE; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 983 | } |
| 984 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 985 | angle::MemoryBuffer memoryBuf; |
| 986 | MemoryProgramCache::Serialize(context, this, &memoryBuf); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 987 | |
Jamie Madill | 4f86d05 | 2017-06-05 12:59:26 -0400 | [diff] [blame] | 988 | GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size()); |
| 989 | const uint8_t *streamState = memoryBuf.data(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 990 | |
| 991 | if (streamLength > bufSize) |
| 992 | { |
| 993 | if (length) |
| 994 | { |
| 995 | *length = 0; |
| 996 | } |
| 997 | |
| 998 | // TODO: This should be moved to the validation layer but computing the size of the binary before saving |
| 999 | // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate |
| 1000 | // sizes and then copy it. |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 1001 | return InternalError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (binary) |
| 1005 | { |
| 1006 | char *ptr = reinterpret_cast<char*>(binary); |
| 1007 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1008 | memcpy(ptr, streamState, streamLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1009 | ptr += streamLength; |
| 1010 | |
| 1011 | ASSERT(ptr - streamLength == binary); |
| 1012 | } |
| 1013 | |
| 1014 | if (length) |
| 1015 | { |
| 1016 | *length = streamLength; |
| 1017 | } |
| 1018 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 1019 | return NoError(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1020 | } |
| 1021 | |
Jamie Madill | ffe00c0 | 2017-06-27 16:26:55 -0400 | [diff] [blame] | 1022 | GLint Program::getBinaryLength(const Context *context) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1023 | { |
| 1024 | GLint length; |
Jamie Madill | ffe00c0 | 2017-06-27 16:26:55 -0400 | [diff] [blame] | 1025 | Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1026 | if (error.isError()) |
| 1027 | { |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | return length; |
apatrick@chromium.org | 3ce8dbc | 2012-06-08 17:52:30 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 1034 | void Program::setBinaryRetrievableHint(bool retrievable) |
| 1035 | { |
| 1036 | // TODO(jmadill) : replace with dirty bits |
| 1037 | mProgram->setBinaryRetrievableHint(retrievable); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1038 | mState.mBinaryRetrieveableHint = retrievable; |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | bool Program::getBinaryRetrievableHint() const |
| 1042 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1043 | return mState.mBinaryRetrieveableHint; |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 1044 | } |
| 1045 | |
Yunchao He | 61afff1 | 2017-03-14 15:34:03 +0800 | [diff] [blame] | 1046 | void Program::setSeparable(bool separable) |
| 1047 | { |
| 1048 | // TODO(yunchao) : replace with dirty bits |
| 1049 | if (mState.mSeparable != separable) |
| 1050 | { |
| 1051 | mProgram->setSeparable(separable); |
| 1052 | mState.mSeparable = separable; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | bool Program::isSeparable() const |
| 1057 | { |
| 1058 | return mState.mSeparable; |
| 1059 | } |
| 1060 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 1061 | void Program::release(const Context *context) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1062 | { |
| 1063 | mRefCount--; |
| 1064 | |
| 1065 | if (mRefCount == 0 && mDeleteStatus) |
| 1066 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 1067 | mResourceManager->deleteProgram(context, mHandle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | void Program::addRef() |
| 1072 | { |
| 1073 | mRefCount++; |
| 1074 | } |
| 1075 | |
| 1076 | unsigned int Program::getRefCount() const |
| 1077 | { |
| 1078 | return mRefCount; |
| 1079 | } |
| 1080 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1081 | int Program::getInfoLogLength() const |
| 1082 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 1083 | return static_cast<int>(mInfoLog.getLength()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1086 | void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1087 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1088 | return mInfoLog.getLog(bufSize, length, infoLog); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1091 | void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1092 | { |
| 1093 | int total = 0; |
| 1094 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1095 | if (mState.mAttachedComputeShader) |
| 1096 | { |
| 1097 | if (total < maxCount) |
| 1098 | { |
| 1099 | shaders[total] = mState.mAttachedComputeShader->getHandle(); |
| 1100 | total++; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1104 | if (mState.mAttachedVertexShader) |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1105 | { |
| 1106 | if (total < maxCount) |
| 1107 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1108 | shaders[total] = mState.mAttachedVertexShader->getHandle(); |
Olli Etuaho | 586bc55 | 2016-03-04 11:46:03 +0200 | [diff] [blame] | 1109 | total++; |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1110 | } |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1113 | if (mState.mAttachedFragmentShader) |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1114 | { |
| 1115 | if (total < maxCount) |
| 1116 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1117 | shaders[total] = mState.mAttachedFragmentShader->getHandle(); |
Olli Etuaho | 586bc55 | 2016-03-04 11:46:03 +0200 | [diff] [blame] | 1118 | total++; |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1119 | } |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | if (count) |
| 1123 | { |
| 1124 | *count = total; |
| 1125 | } |
| 1126 | } |
| 1127 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1128 | GLuint Program::getAttributeLocation(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1129 | { |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 1130 | return mState.getAttributeLocation(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1131 | } |
| 1132 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 1133 | bool Program::isAttribLocationActive(size_t attribLocation) const |
Jamie Madill | 56c6e3c | 2015-04-15 10:18:05 -0400 | [diff] [blame] | 1134 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1135 | ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size()); |
| 1136 | return mState.mActiveAttribLocationsMask[attribLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1137 | } |
| 1138 | |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1139 | void Program::getActiveAttribute(GLuint index, |
| 1140 | GLsizei bufsize, |
| 1141 | GLsizei *length, |
| 1142 | GLint *size, |
| 1143 | GLenum *type, |
| 1144 | GLchar *name) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1145 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1146 | if (!mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1147 | { |
| 1148 | if (bufsize > 0) |
| 1149 | { |
| 1150 | name[0] = '\0'; |
| 1151 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1152 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1153 | if (length) |
| 1154 | { |
| 1155 | *length = 0; |
| 1156 | } |
| 1157 | |
| 1158 | *type = GL_NONE; |
| 1159 | *size = 1; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1160 | return; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1161 | } |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1162 | |
jchen10 | 36e120e | 2017-03-14 14:53:58 +0800 | [diff] [blame] | 1163 | ASSERT(index < mState.mAttributes.size()); |
| 1164 | const sh::Attribute &attrib = mState.mAttributes[index]; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1165 | |
| 1166 | if (bufsize > 0) |
| 1167 | { |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1168 | CopyStringToBuffer(name, attrib.name, bufsize, length); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | // Always a single 'type' instance |
| 1172 | *size = 1; |
| 1173 | *type = attrib.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1176 | GLint Program::getActiveAttributeCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1177 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1178 | if (!mLinked) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1179 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | |
jchen10 | 36e120e | 2017-03-14 14:53:58 +0800 | [diff] [blame] | 1183 | return static_cast<GLint>(mState.mAttributes.size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1186 | GLint Program::getActiveAttributeMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1187 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1188 | if (!mLinked) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1189 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | size_t maxLength = 0; |
| 1194 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1195 | for (const sh::Attribute &attrib : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1196 | { |
jchen10 | 36e120e | 2017-03-14 14:53:58 +0800 | [diff] [blame] | 1197 | maxLength = std::max(attrib.name.length() + 1, maxLength); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1198 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1199 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1200 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1201 | } |
| 1202 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1203 | GLuint Program::getInputResourceIndex(const GLchar *name) const |
| 1204 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1205 | return GetResourceIndexFromName(mState.mAttributes, std::string(name)); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | GLuint Program::getOutputResourceIndex(const GLchar *name) const |
| 1209 | { |
| 1210 | return GetResourceIndexFromName(mState.mOutputVariables, std::string(name)); |
| 1211 | } |
| 1212 | |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1213 | size_t Program::getOutputResourceCount() const |
| 1214 | { |
| 1215 | return (mLinked ? mState.mOutputVariables.size() : 0); |
| 1216 | } |
| 1217 | |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 1218 | template <typename T> |
| 1219 | void Program::getResourceName(GLuint index, |
| 1220 | const std::vector<T> &resources, |
| 1221 | GLsizei bufSize, |
| 1222 | GLsizei *length, |
| 1223 | GLchar *name) const |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1224 | { |
| 1225 | if (length) |
| 1226 | { |
| 1227 | *length = 0; |
| 1228 | } |
| 1229 | |
| 1230 | if (!mLinked) |
| 1231 | { |
| 1232 | if (bufSize > 0) |
| 1233 | { |
| 1234 | name[0] = '\0'; |
| 1235 | } |
| 1236 | return; |
| 1237 | } |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 1238 | ASSERT(index < resources.size()); |
| 1239 | const auto &resource = resources[index]; |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1240 | |
| 1241 | if (bufSize > 0) |
| 1242 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1243 | CopyStringToBuffer(name, resource.name, bufSize, length); |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 1247 | void Program::getInputResourceName(GLuint index, |
| 1248 | GLsizei bufSize, |
| 1249 | GLsizei *length, |
| 1250 | GLchar *name) const |
| 1251 | { |
| 1252 | getResourceName(index, mState.mAttributes, bufSize, length, name); |
| 1253 | } |
| 1254 | |
| 1255 | void Program::getOutputResourceName(GLuint index, |
| 1256 | GLsizei bufSize, |
| 1257 | GLsizei *length, |
| 1258 | GLchar *name) const |
| 1259 | { |
| 1260 | getResourceName(index, mState.mOutputVariables, bufSize, length, name); |
| 1261 | } |
| 1262 | |
| 1263 | void Program::getUniformResourceName(GLuint index, |
| 1264 | GLsizei bufSize, |
| 1265 | GLsizei *length, |
| 1266 | GLchar *name) const |
| 1267 | { |
| 1268 | getResourceName(index, mState.mUniforms, bufSize, length, name); |
| 1269 | } |
| 1270 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1271 | void Program::getBufferVariableResourceName(GLuint index, |
| 1272 | GLsizei bufSize, |
| 1273 | GLsizei *length, |
| 1274 | GLchar *name) const |
| 1275 | { |
| 1276 | getResourceName(index, mState.mBufferVariables, bufSize, length, name); |
| 1277 | } |
| 1278 | |
jchen10 | 880683b | 2017-04-12 16:21:55 +0800 | [diff] [blame] | 1279 | const sh::Attribute &Program::getInputResource(GLuint index) const |
| 1280 | { |
| 1281 | ASSERT(index < mState.mAttributes.size()); |
| 1282 | return mState.mAttributes[index]; |
| 1283 | } |
| 1284 | |
| 1285 | const sh::OutputVariable &Program::getOutputResource(GLuint index) const |
| 1286 | { |
| 1287 | ASSERT(index < mState.mOutputVariables.size()); |
| 1288 | return mState.mOutputVariables[index]; |
| 1289 | } |
| 1290 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1291 | GLint Program::getFragDataLocation(const std::string &name) const |
| 1292 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1293 | return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1296 | void Program::getActiveUniform(GLuint index, |
| 1297 | GLsizei bufsize, |
| 1298 | GLsizei *length, |
| 1299 | GLint *size, |
| 1300 | GLenum *type, |
| 1301 | GLchar *name) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1302 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1303 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1304 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1305 | // index must be smaller than getActiveUniformCount() |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1306 | ASSERT(index < mState.mUniforms.size()); |
| 1307 | const LinkedUniform &uniform = mState.mUniforms[index]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1308 | |
| 1309 | if (bufsize > 0) |
| 1310 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1311 | std::string string = uniform.name; |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1312 | CopyStringToBuffer(name, string, bufsize, length); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1313 | } |
| 1314 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1315 | *size = uniform.elementCount(); |
| 1316 | *type = uniform.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1317 | } |
| 1318 | else |
| 1319 | { |
| 1320 | if (bufsize > 0) |
| 1321 | { |
| 1322 | name[0] = '\0'; |
| 1323 | } |
| 1324 | |
| 1325 | if (length) |
| 1326 | { |
| 1327 | *length = 0; |
| 1328 | } |
| 1329 | |
| 1330 | *size = 0; |
| 1331 | *type = GL_NONE; |
| 1332 | } |
| 1333 | } |
| 1334 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1335 | GLint Program::getActiveUniformCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1336 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1337 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1338 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1339 | return static_cast<GLint>(mState.mUniforms.size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1340 | } |
| 1341 | else |
| 1342 | { |
| 1343 | return 0; |
| 1344 | } |
| 1345 | } |
| 1346 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1347 | size_t Program::getActiveBufferVariableCount() const |
| 1348 | { |
| 1349 | return mLinked ? mState.mBufferVariables.size() : 0; |
| 1350 | } |
| 1351 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1352 | GLint Program::getActiveUniformMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1353 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1354 | size_t maxLength = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1355 | |
| 1356 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1357 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1358 | for (const LinkedUniform &uniform : mState.mUniforms) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1359 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1360 | if (!uniform.name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1361 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1362 | size_t length = uniform.name.length() + 1u; |
| 1363 | if (uniform.isArray()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1364 | { |
| 1365 | length += 3; // Counting in "[0]". |
| 1366 | } |
| 1367 | maxLength = std::max(length, maxLength); |
| 1368 | } |
| 1369 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1370 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1371 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1372 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1373 | } |
| 1374 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1375 | bool Program::isValidUniformLocation(GLint location) const |
| 1376 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1377 | ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size())); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1378 | return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() && |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 1379 | mState.mUniformLocations[static_cast<size_t>(location)].used()); |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 1380 | } |
| 1381 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1382 | const LinkedUniform &Program::getUniformByLocation(GLint location) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1383 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1384 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1385 | return mState.mUniforms[mState.getUniformIndexFromLocation(location)]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1386 | } |
| 1387 | |
Jamie Madill | ac4e9c3 | 2017-01-13 14:07:12 -0500 | [diff] [blame] | 1388 | const VariableLocation &Program::getUniformLocation(GLint location) const |
| 1389 | { |
| 1390 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
| 1391 | return mState.mUniformLocations[location]; |
| 1392 | } |
| 1393 | |
| 1394 | const std::vector<VariableLocation> &Program::getUniformLocations() const |
| 1395 | { |
| 1396 | return mState.mUniformLocations; |
| 1397 | } |
| 1398 | |
| 1399 | const LinkedUniform &Program::getUniformByIndex(GLuint index) const |
| 1400 | { |
| 1401 | ASSERT(index < static_cast<size_t>(mState.mUniforms.size())); |
| 1402 | return mState.mUniforms[index]; |
| 1403 | } |
| 1404 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1405 | const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const |
| 1406 | { |
| 1407 | ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size())); |
| 1408 | return mState.mBufferVariables[index]; |
| 1409 | } |
| 1410 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1411 | GLint Program::getUniformLocation(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1412 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 1413 | return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1414 | } |
| 1415 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1416 | GLuint Program::getUniformIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1417 | { |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1418 | return mState.getUniformIndexFromName(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 1422 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1423 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1424 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1425 | mProgram->setUniform1fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 1429 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1430 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1431 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1432 | mProgram->setUniform2fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 1436 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1437 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1438 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1439 | mProgram->setUniform3fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 1443 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1444 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1445 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1446 | mProgram->setUniform4fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1447 | } |
| 1448 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1449 | Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1450 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1451 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1452 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
| 1453 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1454 | mProgram->setUniform1iv(location, clampedCount, v); |
| 1455 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1456 | if (mState.isSamplerUniformIndex(locationInfo.index)) |
| 1457 | { |
| 1458 | updateSamplerUniform(locationInfo, clampedCount, v); |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1459 | return SetUniformResult::SamplerChanged; |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1460 | } |
| 1461 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1462 | return SetUniformResult::NoSamplerChange; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 1466 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1467 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1468 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1469 | mProgram->setUniform2iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 1473 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1474 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1475 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1476 | mProgram->setUniform3iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 1480 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1481 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1482 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1483 | mProgram->setUniform4iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 1487 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1488 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1489 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1490 | mProgram->setUniform1uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 1494 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1495 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1496 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1497 | mProgram->setUniform2uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 1501 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1502 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1503 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1504 | mProgram->setUniform3uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 1508 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1509 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1510 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1511 | mProgram->setUniform4uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1515 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1516 | GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1517 | mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1521 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1522 | GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1523 | mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1527 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1528 | GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1529 | mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1533 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1534 | GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1535 | mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1539 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1540 | GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1541 | mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1545 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1546 | GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1547 | mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1551 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1552 | GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1553 | mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1557 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1558 | GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1559 | mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1563 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1564 | GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1565 | mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1566 | } |
| 1567 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1568 | void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1569 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1570 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1571 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1572 | |
| 1573 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1574 | if (nativeType == GL_FLOAT) |
| 1575 | { |
| 1576 | mProgram->getUniformfv(context, location, v); |
| 1577 | } |
| 1578 | else |
| 1579 | { |
| 1580 | getUniformInternal(context, v, location, nativeType, |
| 1581 | gl::VariableComponentCount(uniform.type)); |
| 1582 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1583 | } |
| 1584 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1585 | void Program::getUniformiv(const Context *context, GLint location, GLint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1586 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1587 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1588 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1589 | |
| 1590 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1591 | if (nativeType == GL_INT || nativeType == GL_BOOL) |
| 1592 | { |
| 1593 | mProgram->getUniformiv(context, location, v); |
| 1594 | } |
| 1595 | else |
| 1596 | { |
| 1597 | getUniformInternal(context, v, location, nativeType, |
| 1598 | gl::VariableComponentCount(uniform.type)); |
| 1599 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1600 | } |
| 1601 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1602 | void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1603 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1604 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1605 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1606 | |
| 1607 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1608 | if (nativeType == GL_UNSIGNED_INT) |
| 1609 | { |
| 1610 | mProgram->getUniformuiv(context, location, v); |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | getUniformInternal(context, v, location, nativeType, |
| 1615 | gl::VariableComponentCount(uniform.type)); |
| 1616 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1617 | } |
| 1618 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1619 | void Program::flagForDeletion() |
| 1620 | { |
| 1621 | mDeleteStatus = true; |
| 1622 | } |
| 1623 | |
| 1624 | bool Program::isFlaggedForDeletion() const |
| 1625 | { |
| 1626 | return mDeleteStatus; |
| 1627 | } |
daniel@transgaming.com | 86a7a13 | 2010-04-29 03:32:32 +0000 | [diff] [blame] | 1628 | |
Brandon Jones | 43a53e2 | 2014-08-28 16:23:22 -0700 | [diff] [blame] | 1629 | void Program::validate(const Caps &caps) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1630 | { |
| 1631 | mInfoLog.reset(); |
| 1632 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1633 | if (mLinked) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1634 | { |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 1635 | mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog)); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1636 | } |
| 1637 | else |
| 1638 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1639 | mInfoLog << "Program has not been successfully linked."; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1643 | bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps) |
| 1644 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1645 | // Skip cache if we're using an infolog, so we get the full error. |
| 1646 | // Also skip the cache if the sample mapping has changed, or if we haven't ever validated. |
| 1647 | if (infoLog == nullptr && mCachedValidateSamplersResult.valid()) |
| 1648 | { |
| 1649 | return mCachedValidateSamplersResult.value(); |
| 1650 | } |
| 1651 | |
| 1652 | if (mTextureUnitTypesCache.empty()) |
| 1653 | { |
| 1654 | mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE); |
| 1659 | } |
| 1660 | |
| 1661 | // if any two active samplers in a program are of different types, but refer to the same |
| 1662 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 1663 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1664 | for (const auto &samplerBinding : mState.mSamplerBindings) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1665 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1666 | if (samplerBinding.unreferenced) |
| 1667 | continue; |
| 1668 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1669 | GLenum textureType = samplerBinding.textureType; |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1670 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1671 | for (GLuint textureUnit : samplerBinding.boundTextureUnits) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1672 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1673 | if (textureUnit >= caps.maxCombinedTextureImageUnits) |
| 1674 | { |
| 1675 | if (infoLog) |
| 1676 | { |
| 1677 | (*infoLog) << "Sampler uniform (" << textureUnit |
| 1678 | << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (" |
| 1679 | << caps.maxCombinedTextureImageUnits << ")"; |
| 1680 | } |
| 1681 | |
| 1682 | mCachedValidateSamplersResult = false; |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
| 1686 | if (mTextureUnitTypesCache[textureUnit] != GL_NONE) |
| 1687 | { |
| 1688 | if (textureType != mTextureUnitTypesCache[textureUnit]) |
| 1689 | { |
| 1690 | if (infoLog) |
| 1691 | { |
| 1692 | (*infoLog) << "Samplers of conflicting types refer to the same texture " |
| 1693 | "image unit (" |
| 1694 | << textureUnit << ")."; |
| 1695 | } |
| 1696 | |
| 1697 | mCachedValidateSamplersResult = false; |
| 1698 | return false; |
| 1699 | } |
| 1700 | } |
| 1701 | else |
| 1702 | { |
| 1703 | mTextureUnitTypesCache[textureUnit] = textureType; |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | mCachedValidateSamplersResult = true; |
| 1709 | return true; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1710 | } |
| 1711 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1712 | bool Program::isValidated() const |
| 1713 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1714 | return mValidated; |
| 1715 | } |
| 1716 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1717 | GLuint Program::getActiveUniformBlockCount() const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1718 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1719 | return static_cast<GLuint>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1720 | } |
| 1721 | |
jchen10 | 58f67be | 2017-10-27 08:59:27 +0800 | [diff] [blame] | 1722 | GLuint Program::getActiveAtomicCounterBufferCount() const |
| 1723 | { |
| 1724 | return static_cast<GLuint>(mState.mAtomicCounterBuffers.size()); |
| 1725 | } |
| 1726 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1727 | GLuint Program::getActiveShaderStorageBlockCount() const |
| 1728 | { |
| 1729 | return static_cast<GLuint>(mState.mShaderStorageBlocks.size()); |
| 1730 | } |
| 1731 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1732 | void Program::getActiveUniformBlockName(const GLuint blockIndex, |
| 1733 | GLsizei bufSize, |
| 1734 | GLsizei *length, |
| 1735 | GLchar *blockName) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1736 | { |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1737 | GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName); |
| 1738 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1739 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1740 | void Program::getActiveShaderStorageBlockName(const GLuint blockIndex, |
| 1741 | GLsizei bufSize, |
| 1742 | GLsizei *length, |
| 1743 | GLchar *blockName) const |
| 1744 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1745 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1746 | GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1749 | GLint Program::getActiveUniformBlockMaxLength() const |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1750 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1751 | int maxLength = 0; |
| 1752 | |
| 1753 | if (mLinked) |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1754 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1755 | unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1756 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++) |
| 1757 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1758 | const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1759 | if (!uniformBlock.name.empty()) |
| 1760 | { |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 1761 | int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length()); |
| 1762 | maxLength = std::max(length + 1, maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1763 | } |
| 1764 | } |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1765 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1766 | |
| 1767 | return maxLength; |
| 1768 | } |
| 1769 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1770 | GLuint Program::getUniformBlockIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1771 | { |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1772 | return GetInterfaceBlockIndex(mState.mUniformBlocks, name); |
| 1773 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1774 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1775 | GLuint Program::getShaderStorageBlockIndex(const std::string &name) const |
| 1776 | { |
| 1777 | return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name); |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1780 | const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1781 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1782 | ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size())); |
| 1783 | return mState.mUniformBlocks[index]; |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 1786 | const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const |
| 1787 | { |
| 1788 | ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size())); |
| 1789 | return mState.mShaderStorageBlocks[index]; |
| 1790 | } |
| 1791 | |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1792 | void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 1793 | { |
jchen10 | 7a20b97 | 2017-06-13 14:25:26 +0800 | [diff] [blame] | 1794 | mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding; |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 1795 | mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 1796 | mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const |
| 1800 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1801 | return mState.getUniformBlockBinding(uniformBlockIndex); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1804 | GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const |
| 1805 | { |
| 1806 | return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex); |
| 1807 | } |
| 1808 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1809 | void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode) |
| 1810 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1811 | mState.mTransformFeedbackVaryingNames.resize(count); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1812 | for (GLsizei i = 0; i < count; i++) |
| 1813 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1814 | mState.mTransformFeedbackVaryingNames[i] = varyings[i]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1815 | } |
| 1816 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1817 | mState.mTransformFeedbackBufferMode = bufferMode; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const |
| 1821 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1822 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1823 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1824 | ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size()); |
| 1825 | const auto &var = mState.mLinkedTransformFeedbackVaryings[index]; |
| 1826 | std::string varName = var.nameWithArrayIndex(); |
| 1827 | GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length())); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1828 | if (length) |
| 1829 | { |
| 1830 | *length = lastNameIdx; |
| 1831 | } |
| 1832 | if (size) |
| 1833 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1834 | *size = var.size(); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1835 | } |
| 1836 | if (type) |
| 1837 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1838 | *type = var.type; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1839 | } |
| 1840 | if (name) |
| 1841 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1842 | memcpy(name, varName.c_str(), lastNameIdx); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1843 | name[lastNameIdx] = '\0'; |
| 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1848 | GLsizei Program::getTransformFeedbackVaryingCount() const |
| 1849 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1850 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1851 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1852 | return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size()); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1853 | } |
| 1854 | else |
| 1855 | { |
| 1856 | return 0; |
| 1857 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | GLsizei Program::getTransformFeedbackVaryingMaxLength() const |
| 1861 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1862 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1863 | { |
| 1864 | GLsizei maxSize = 0; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1865 | for (const auto &var : mState.mLinkedTransformFeedbackVaryings) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1866 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1867 | maxSize = |
| 1868 | std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1)); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | return maxSize; |
| 1872 | } |
| 1873 | else |
| 1874 | { |
| 1875 | return 0; |
| 1876 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | GLenum Program::getTransformFeedbackBufferMode() const |
| 1880 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1881 | return mState.mTransformFeedbackBufferMode; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1882 | } |
| 1883 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1884 | bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1885 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1886 | Shader *vertexShader = mState.mAttachedVertexShader; |
| 1887 | Shader *fragmentShader = mState.mAttachedFragmentShader; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1888 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1889 | ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context)); |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 1890 | |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 1891 | const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context); |
| 1892 | const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1893 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 1894 | std::map<GLuint, std::string> staticFragmentInputLocations; |
| 1895 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1896 | for (const sh::Varying &output : fragmentVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1897 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1898 | bool matched = false; |
| 1899 | |
| 1900 | // Built-in varyings obey special rules |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1901 | if (output.isBuiltIn()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1902 | { |
| 1903 | continue; |
| 1904 | } |
| 1905 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1906 | for (const sh::Varying &input : vertexVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1907 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1908 | if (output.name == input.name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1909 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1910 | ASSERT(!input.isBuiltIn()); |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 1911 | if (!linkValidateVaryings(infoLog, output.name, input, output, |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1912 | vertexShader->getShaderVersion(context))) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1913 | { |
| 1914 | return false; |
| 1915 | } |
| 1916 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1917 | matched = true; |
| 1918 | break; |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | // We permit unmatched, unreferenced varyings |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1923 | if (!matched && output.staticUse) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1924 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1925 | infoLog << "Fragment varying " << output.name << " does not match any vertex varying"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1926 | return false; |
| 1927 | } |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 1928 | |
| 1929 | // Check for aliased path rendering input bindings (if any). |
| 1930 | // If more than one binding refer statically to the same |
| 1931 | // location the link must fail. |
| 1932 | |
| 1933 | if (!output.staticUse) |
| 1934 | continue; |
| 1935 | |
| 1936 | const auto inputBinding = mFragmentInputBindings.getBinding(output.name); |
| 1937 | if (inputBinding == -1) |
| 1938 | continue; |
| 1939 | |
| 1940 | const auto it = staticFragmentInputLocations.find(inputBinding); |
| 1941 | if (it == std::end(staticFragmentInputLocations)) |
| 1942 | { |
| 1943 | staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name)); |
| 1944 | } |
| 1945 | else |
| 1946 | { |
| 1947 | infoLog << "Binding for fragment input " << output.name << " conflicts with " |
| 1948 | << it->second; |
| 1949 | return false; |
| 1950 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1951 | } |
| 1952 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1953 | if (!linkValidateBuiltInVaryings(context, infoLog)) |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 1954 | { |
| 1955 | return false; |
| 1956 | } |
| 1957 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1958 | // TODO(jmadill): verify no unmatched vertex varyings? |
| 1959 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1960 | return true; |
| 1961 | } |
| 1962 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1963 | bool Program::linkUniforms(const Context *context, |
| 1964 | InfoLog &infoLog, |
Olli Etuaho | 4a92ceb | 2017-02-19 17:51:24 +0000 | [diff] [blame] | 1965 | const Bindings &uniformLocationBindings) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1966 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1967 | UniformLinker linker(mState); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1968 | if (!linker.link(context, infoLog, uniformLocationBindings)) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1969 | { |
| 1970 | return false; |
| 1971 | } |
| 1972 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1973 | linker.getResults(&mState.mUniforms, &mState.mUniformLocations); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1974 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1975 | linkSamplerAndImageBindings(); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1976 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1977 | if (!linkAtomicCounterBuffers()) |
| 1978 | { |
| 1979 | return false; |
| 1980 | } |
| 1981 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1982 | return true; |
| 1983 | } |
| 1984 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1985 | void Program::linkSamplerAndImageBindings() |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1986 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 1987 | unsigned int high = static_cast<unsigned int>(mState.mUniforms.size()); |
| 1988 | unsigned int low = high; |
| 1989 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1990 | for (auto counterIter = mState.mUniforms.rbegin(); |
| 1991 | counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter) |
| 1992 | { |
| 1993 | --low; |
| 1994 | } |
| 1995 | |
| 1996 | mState.mAtomicCounterUniformRange = RangeUI(low, high); |
| 1997 | |
| 1998 | high = low; |
| 1999 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2000 | for (auto imageIter = mState.mUniforms.rbegin(); |
| 2001 | imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter) |
| 2002 | { |
| 2003 | --low; |
| 2004 | } |
| 2005 | |
| 2006 | mState.mImageUniformRange = RangeUI(low, high); |
| 2007 | |
| 2008 | // If uniform is a image type, insert it into the mImageBindings array. |
| 2009 | for (unsigned int imageIndex : mState.mImageUniformRange) |
| 2010 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 2011 | // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands |
| 2012 | // cannot load values into a uniform defined as an image. if declare without a |
| 2013 | // binding qualifier, any uniform image variable (include all elements of |
| 2014 | // unbound image array) shoud be bound to unit zero. |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2015 | auto &imageUniform = mState.mUniforms[imageIndex]; |
| 2016 | if (imageUniform.binding == -1) |
| 2017 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 2018 | mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount())); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2019 | } |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 2020 | else |
| 2021 | { |
| 2022 | mState.mImageBindings.emplace_back( |
| 2023 | ImageBinding(imageUniform.binding, imageUniform.elementCount())); |
| 2024 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | high = low; |
| 2028 | |
| 2029 | for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length(); |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2030 | samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter) |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2031 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2032 | --low; |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2033 | } |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2034 | |
| 2035 | mState.mSamplerUniformRange = RangeUI(low, high); |
| 2036 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2037 | // If uniform is a sampler type, insert it into the mSamplerBindings array. |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2038 | for (unsigned int samplerIndex : mState.mSamplerUniformRange) |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2039 | { |
| 2040 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
| 2041 | GLenum textureType = SamplerTypeToTextureType(samplerUniform.type); |
| 2042 | mState.mSamplerBindings.emplace_back( |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2043 | SamplerBinding(textureType, samplerUniform.elementCount(), false)); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2047 | bool Program::linkAtomicCounterBuffers() |
| 2048 | { |
| 2049 | for (unsigned int index : mState.mAtomicCounterUniformRange) |
| 2050 | { |
| 2051 | auto &uniform = mState.mUniforms[index]; |
| 2052 | bool found = false; |
| 2053 | for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size(); |
| 2054 | ++bufferIndex) |
| 2055 | { |
| 2056 | auto &buffer = mState.mAtomicCounterBuffers[bufferIndex]; |
| 2057 | if (buffer.binding == uniform.binding) |
| 2058 | { |
| 2059 | buffer.memberIndexes.push_back(index); |
| 2060 | uniform.bufferIndex = bufferIndex; |
| 2061 | found = true; |
jchen10 | 58f67be | 2017-10-27 08:59:27 +0800 | [diff] [blame] | 2062 | buffer.unionReferencesWith(uniform); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2063 | break; |
| 2064 | } |
| 2065 | } |
| 2066 | if (!found) |
| 2067 | { |
| 2068 | AtomicCounterBuffer atomicCounterBuffer; |
| 2069 | atomicCounterBuffer.binding = uniform.binding; |
| 2070 | atomicCounterBuffer.memberIndexes.push_back(index); |
jchen10 | 58f67be | 2017-10-27 08:59:27 +0800 | [diff] [blame] | 2071 | atomicCounterBuffer.unionReferencesWith(uniform); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2072 | mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer); |
| 2073 | uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1); |
| 2074 | } |
| 2075 | } |
| 2076 | // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against |
| 2077 | // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers. |
| 2078 | |
| 2079 | return true; |
| 2080 | } |
| 2081 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2082 | bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, |
| 2083 | const std::string &uniformName, |
| 2084 | const sh::InterfaceBlockField &vertexUniform, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2085 | const sh::InterfaceBlockField &fragmentUniform, |
| 2086 | bool webglCompatibility) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2087 | { |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2088 | // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287. |
| 2089 | if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, |
| 2090 | webglCompatibility)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2091 | { |
| 2092 | return false; |
| 2093 | } |
| 2094 | |
| 2095 | if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout) |
| 2096 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2097 | infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2098 | return false; |
| 2099 | } |
| 2100 | |
| 2101 | return true; |
| 2102 | } |
| 2103 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2104 | // Assigns locations to all attributes from the bindings and program locations. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2105 | bool Program::linkAttributes(const Context *context, InfoLog &infoLog) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2106 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2107 | const ContextState &data = context->getContextState(); |
| 2108 | auto *vertexShader = mState.getAttachedVertexShader(); |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2109 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2110 | unsigned int usedLocations = 0; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2111 | mState.mAttributes = vertexShader->getActiveAttributes(context); |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 2112 | GLuint maxAttribs = data.getCaps().maxVertexAttributes; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2113 | |
| 2114 | // TODO(jmadill): handle aliasing robustly |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2115 | if (mState.mAttributes.size() > maxAttribs) |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2116 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2117 | infoLog << "Too many vertex attributes."; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2118 | return false; |
| 2119 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2120 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 2121 | std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr); |
Jamie Madill | 4e10722 | 2015-08-24 14:12:17 +0000 | [diff] [blame] | 2122 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2123 | // Link attributes that have a binding location |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2124 | for (sh::Attribute &attribute : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2125 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 2126 | // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or |
| 2127 | // structures, so we don't need to worry about adjusting their names or generating entries |
| 2128 | // for each member/element (unlike uniforms for example). |
| 2129 | ASSERT(!attribute.isArray() && !attribute.isStruct()); |
| 2130 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2131 | int bindingLocation = mAttributeBindings.getBinding(attribute.name); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2132 | if (attribute.location == -1 && bindingLocation != -1) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 2133 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2134 | attribute.location = bindingLocation; |
| 2135 | } |
| 2136 | |
| 2137 | if (attribute.location != -1) |
| 2138 | { |
| 2139 | // Location is set by glBindAttribLocation or by location layout qualifier |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2140 | const int regs = VariableRegisterCount(attribute.type); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2141 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2142 | if (static_cast<GLuint>(regs + attribute.location) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2143 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2144 | infoLog << "Active attribute (" << attribute.name << ") at location " |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2145 | << attribute.location << " is too big to fit"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2146 | |
| 2147 | return false; |
| 2148 | } |
| 2149 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2150 | for (int reg = 0; reg < regs; reg++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2151 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2152 | const int regLocation = attribute.location + reg; |
| 2153 | sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2154 | |
| 2155 | // In GLSL 3.00, attribute aliasing produces a link error |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2156 | // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2157 | if (linkedAttribute) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2158 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2159 | // TODO(jmadill): fix aliasing on ES2 |
| 2160 | // if (mProgram->getShaderVersion() >= 300) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2161 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 2162 | infoLog << "Attribute '" << attribute.name << "' aliases attribute '" |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2163 | << linkedAttribute->name << "' at location " << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2164 | return false; |
| 2165 | } |
| 2166 | } |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2167 | else |
| 2168 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2169 | usedAttribMap[regLocation] = &attribute; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2170 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2171 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2172 | usedLocations |= 1 << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | // Link attributes that don't have a binding location |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2178 | for (sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2179 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2180 | // Not set by glBindAttribLocation or by location layout qualifier |
| 2181 | if (attribute.location == -1) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2182 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2183 | int regs = VariableRegisterCount(attribute.type); |
| 2184 | int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2185 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2186 | if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2187 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2188 | infoLog << "Too many active attributes (" << attribute.name << ")"; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2189 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2190 | } |
| 2191 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2192 | attribute.location = availableIndex; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2196 | for (const sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2197 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2198 | ASSERT(attribute.location != -1); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 2199 | unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type)); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2200 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 2201 | for (unsigned int r = 0; r < regs; r++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2202 | { |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 2203 | unsigned int location = static_cast<unsigned int>(attribute.location) + r; |
| 2204 | mState.mActiveAttribLocationsMask.set(location); |
| 2205 | mState.mMaxActiveAttribLocation = |
| 2206 | std::max(mState.mMaxActiveAttribLocation, location + 1); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2210 | return true; |
| 2211 | } |
| 2212 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2213 | bool Program::validateVertexAndFragmentInterfaceBlocks( |
| 2214 | const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks, |
| 2215 | const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2216 | InfoLog &infoLog, |
| 2217 | bool webglCompatibility) const |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2218 | { |
| 2219 | // Check that interface blocks defined in the vertex and fragment shaders are identical |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2220 | typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap; |
| 2221 | InterfaceBlockMap linkedInterfaceBlocks; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2222 | |
| 2223 | for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks) |
| 2224 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2225 | linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2226 | } |
| 2227 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2228 | for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2229 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2230 | auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name); |
| 2231 | if (entry != linkedInterfaceBlocks.end()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2232 | { |
| 2233 | const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second; |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2234 | if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock, |
| 2235 | webglCompatibility)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2236 | { |
| 2237 | return false; |
| 2238 | } |
| 2239 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2240 | // TODO(jiajia.qin@intel.com): Add |
| 2241 | // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation. |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2242 | } |
| 2243 | return true; |
| 2244 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2245 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2246 | bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2247 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2248 | const auto &caps = context->getCaps(); |
| 2249 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2250 | if (mState.mAttachedComputeShader) |
| 2251 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2252 | Shader &computeShader = *mState.mAttachedComputeShader; |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2253 | const auto &computeUniformBlocks = computeShader.getUniformBlocks(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2254 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2255 | if (!validateInterfaceBlocksCount( |
| 2256 | caps.maxComputeUniformBlocks, computeUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2257 | "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (", |
| 2258 | infoLog)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2259 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2260 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2261 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2262 | |
| 2263 | const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context); |
| 2264 | if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks, |
| 2265 | computeShaderStorageBlocks, |
| 2266 | "Compute shader shader storage block count exceeds " |
| 2267 | "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (", |
| 2268 | infoLog)) |
| 2269 | { |
| 2270 | return false; |
| 2271 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2272 | return true; |
| 2273 | } |
| 2274 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2275 | Shader &vertexShader = *mState.mAttachedVertexShader; |
| 2276 | Shader &fragmentShader = *mState.mAttachedFragmentShader; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2277 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2278 | const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context); |
| 2279 | const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2280 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2281 | if (!validateInterfaceBlocksCount( |
| 2282 | caps.maxVertexUniformBlocks, vertexUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2283 | "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog)) |
| 2284 | { |
| 2285 | return false; |
| 2286 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2287 | if (!validateInterfaceBlocksCount( |
| 2288 | caps.maxFragmentUniformBlocks, fragmentUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2289 | "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (", |
| 2290 | infoLog)) |
| 2291 | { |
| 2292 | |
| 2293 | return false; |
| 2294 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2295 | |
| 2296 | bool webglCompatibility = context->getExtensions().webglCompatibility; |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2297 | if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2298 | infoLog, webglCompatibility)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2299 | { |
| 2300 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2301 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2302 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2303 | if (context->getClientVersion() >= Version(3, 1)) |
| 2304 | { |
| 2305 | const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context); |
| 2306 | const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context); |
| 2307 | |
| 2308 | if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks, |
| 2309 | vertexShaderStorageBlocks, |
| 2310 | "Vertex shader shader storage block count exceeds " |
| 2311 | "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (", |
| 2312 | infoLog)) |
| 2313 | { |
| 2314 | return false; |
| 2315 | } |
| 2316 | if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks, |
| 2317 | fragmentShaderStorageBlocks, |
| 2318 | "Fragment shader shader storage block count exceeds " |
| 2319 | "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (", |
| 2320 | infoLog)) |
| 2321 | { |
| 2322 | |
| 2323 | return false; |
| 2324 | } |
| 2325 | |
| 2326 | if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks, |
| 2327 | fragmentShaderStorageBlocks, infoLog, |
| 2328 | webglCompatibility)) |
| 2329 | { |
| 2330 | return false; |
| 2331 | } |
| 2332 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2333 | return true; |
| 2334 | } |
| 2335 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2336 | bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2337 | const sh::InterfaceBlock &vertexInterfaceBlock, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2338 | const sh::InterfaceBlock &fragmentInterfaceBlock, |
| 2339 | bool webglCompatibility) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2340 | { |
| 2341 | const char* blockName = vertexInterfaceBlock.name.c_str(); |
| 2342 | // validate blocks for the same member types |
| 2343 | if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size()) |
| 2344 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2345 | infoLog << "Types for interface block '" << blockName |
| 2346 | << "' differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2347 | return false; |
| 2348 | } |
| 2349 | if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize) |
| 2350 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2351 | infoLog << "Array sizes differ for interface block '" << blockName |
| 2352 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2353 | return false; |
| 2354 | } |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2355 | if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || |
| 2356 | vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout || |
| 2357 | vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2358 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2359 | infoLog << "Layout qualifiers differ for interface block '" << blockName |
| 2360 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2361 | return false; |
| 2362 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2363 | const unsigned int numBlockMembers = |
| 2364 | static_cast<unsigned int>(vertexInterfaceBlock.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2365 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++) |
| 2366 | { |
| 2367 | const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex]; |
| 2368 | const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex]; |
| 2369 | if (vertexMember.name != fragmentMember.name) |
| 2370 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2371 | infoLog << "Name mismatch for field " << blockMemberIndex |
| 2372 | << " of interface block '" << blockName |
| 2373 | << "': (in vertex: '" << vertexMember.name |
| 2374 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2375 | return false; |
| 2376 | } |
| 2377 | std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'"; |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2378 | if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember, |
| 2379 | webglCompatibility)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2380 | { |
| 2381 | return false; |
| 2382 | } |
| 2383 | } |
| 2384 | return true; |
| 2385 | } |
| 2386 | |
| 2387 | bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable, |
| 2388 | const sh::ShaderVariable &fragmentVariable, bool validatePrecision) |
| 2389 | { |
| 2390 | if (vertexVariable.type != fragmentVariable.type) |
| 2391 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2392 | infoLog << "Types for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2393 | return false; |
| 2394 | } |
| 2395 | if (vertexVariable.arraySize != fragmentVariable.arraySize) |
| 2396 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2397 | infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2398 | return false; |
| 2399 | } |
| 2400 | if (validatePrecision && vertexVariable.precision != fragmentVariable.precision) |
| 2401 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2402 | infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2403 | return false; |
| 2404 | } |
Geoff Lang | bb1e750 | 2017-06-05 16:40:09 -0400 | [diff] [blame] | 2405 | if (vertexVariable.structName != fragmentVariable.structName) |
| 2406 | { |
| 2407 | infoLog << "Structure names for " << variableName |
| 2408 | << " differ between vertex and fragment shaders"; |
| 2409 | return false; |
| 2410 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2411 | |
| 2412 | if (vertexVariable.fields.size() != fragmentVariable.fields.size()) |
| 2413 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2414 | infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2415 | return false; |
| 2416 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2417 | const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2418 | for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++) |
| 2419 | { |
| 2420 | const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex]; |
| 2421 | const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex]; |
| 2422 | |
| 2423 | if (vertexMember.name != fragmentMember.name) |
| 2424 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2425 | infoLog << "Name mismatch for field '" << memberIndex |
| 2426 | << "' of " << variableName |
| 2427 | << ": (in vertex: '" << vertexMember.name |
| 2428 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2429 | return false; |
| 2430 | } |
| 2431 | |
| 2432 | const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." + |
| 2433 | vertexMember.name + "'"; |
| 2434 | |
| 2435 | if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision)) |
| 2436 | { |
| 2437 | return false; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | return true; |
| 2442 | } |
| 2443 | |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2444 | bool Program::linkValidateVaryings(InfoLog &infoLog, |
| 2445 | const std::string &varyingName, |
| 2446 | const sh::Varying &vertexVarying, |
| 2447 | const sh::Varying &fragmentVarying, |
| 2448 | int shaderVersion) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2449 | { |
| 2450 | if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false)) |
| 2451 | { |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
Jamie Madill | e9cc469 | 2015-02-19 16:00:13 -0500 | [diff] [blame] | 2455 | if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2456 | { |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2457 | infoLog << "Interpolation types for " << varyingName |
| 2458 | << " differ between vertex and fragment shaders."; |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
| 2462 | if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant) |
| 2463 | { |
| 2464 | infoLog << "Invariance for " << varyingName |
| 2465 | << " differs between vertex and fragment shaders."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2466 | return false; |
| 2467 | } |
| 2468 | |
| 2469 | return true; |
| 2470 | } |
| 2471 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2472 | bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2473 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2474 | Shader *vertexShader = mState.mAttachedVertexShader; |
| 2475 | Shader *fragmentShader = mState.mAttachedFragmentShader; |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 2476 | const auto &vertexVaryings = vertexShader->getOutputVaryings(context); |
| 2477 | const auto &fragmentVaryings = fragmentShader->getInputVaryings(context); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2478 | int shaderVersion = vertexShader->getShaderVersion(context); |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2479 | |
| 2480 | if (shaderVersion != 100) |
| 2481 | { |
| 2482 | // Only ESSL 1.0 has restrictions on matching input and output invariance |
| 2483 | return true; |
| 2484 | } |
| 2485 | |
| 2486 | bool glPositionIsInvariant = false; |
| 2487 | bool glPointSizeIsInvariant = false; |
| 2488 | bool glFragCoordIsInvariant = false; |
| 2489 | bool glPointCoordIsInvariant = false; |
| 2490 | |
| 2491 | for (const sh::Varying &varying : vertexVaryings) |
| 2492 | { |
| 2493 | if (!varying.isBuiltIn()) |
| 2494 | { |
| 2495 | continue; |
| 2496 | } |
| 2497 | if (varying.name.compare("gl_Position") == 0) |
| 2498 | { |
| 2499 | glPositionIsInvariant = varying.isInvariant; |
| 2500 | } |
| 2501 | else if (varying.name.compare("gl_PointSize") == 0) |
| 2502 | { |
| 2503 | glPointSizeIsInvariant = varying.isInvariant; |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | for (const sh::Varying &varying : fragmentVaryings) |
| 2508 | { |
| 2509 | if (!varying.isBuiltIn()) |
| 2510 | { |
| 2511 | continue; |
| 2512 | } |
| 2513 | if (varying.name.compare("gl_FragCoord") == 0) |
| 2514 | { |
| 2515 | glFragCoordIsInvariant = varying.isInvariant; |
| 2516 | } |
| 2517 | else if (varying.name.compare("gl_PointCoord") == 0) |
| 2518 | { |
| 2519 | glPointCoordIsInvariant = varying.isInvariant; |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation, |
| 2524 | // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842. |
| 2525 | // Not requiring invariance to match is supported by: |
| 2526 | // dEQP, WebGL CTS, Nexus 5X GLES |
| 2527 | if (glFragCoordIsInvariant && !glPositionIsInvariant) |
| 2528 | { |
| 2529 | infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is " |
| 2530 | "declared invariant."; |
| 2531 | return false; |
| 2532 | } |
| 2533 | if (glPointCoordIsInvariant && !glPointSizeIsInvariant) |
| 2534 | { |
| 2535 | infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is " |
| 2536 | "declared invariant."; |
| 2537 | return false; |
| 2538 | } |
| 2539 | |
| 2540 | return true; |
| 2541 | } |
| 2542 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2543 | bool Program::linkValidateTransformFeedback(const gl::Context *context, |
| 2544 | InfoLog &infoLog, |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2545 | const Program::MergedVaryings &varyings, |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2546 | const Caps &caps) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2547 | { |
| 2548 | size_t totalComponents = 0; |
| 2549 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2550 | std::set<std::string> uniqueNames; |
| 2551 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2552 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2553 | { |
| 2554 | bool found = false; |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 2555 | std::vector<unsigned int> subscripts; |
| 2556 | std::string baseName = ParseResourceName(tfVaryingName, &subscripts); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2557 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2558 | for (const auto &ref : varyings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2559 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2560 | const sh::Varying *varying = ref.second.get(); |
| 2561 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2562 | if (baseName == varying->name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2563 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2564 | if (uniqueNames.count(tfVaryingName) > 0) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2565 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2566 | infoLog << "Two transform feedback varyings specify the same output variable (" |
| 2567 | << tfVaryingName << ")."; |
| 2568 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2569 | } |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2570 | if (context->getClientVersion() >= Version(3, 1)) |
| 2571 | { |
| 2572 | if (IncludeSameArrayElement(uniqueNames, tfVaryingName)) |
| 2573 | { |
| 2574 | infoLog |
| 2575 | << "Two transform feedback varyings include the same array element (" |
| 2576 | << tfVaryingName << ")."; |
| 2577 | return false; |
| 2578 | } |
| 2579 | } |
| 2580 | else if (varying->isArray()) |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 2581 | { |
| 2582 | infoLog << "Capture of arrays is undefined and not supported."; |
| 2583 | return false; |
| 2584 | } |
| 2585 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2586 | uniqueNames.insert(tfVaryingName); |
| 2587 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2588 | // TODO(jmadill): Investigate implementation limits on D3D11 |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2589 | size_t elementCount = |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 2590 | ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2591 | size_t componentCount = VariableComponentCount(varying->type) * elementCount; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2592 | if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS && |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2593 | componentCount > caps.maxTransformFeedbackSeparateComponents) |
| 2594 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2595 | infoLog << "Transform feedback varying's " << varying->name << " components (" |
| 2596 | << componentCount << ") exceed the maximum separate components (" |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2597 | << caps.maxTransformFeedbackSeparateComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2598 | return false; |
| 2599 | } |
| 2600 | |
| 2601 | totalComponents += componentCount; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2602 | found = true; |
| 2603 | break; |
| 2604 | } |
| 2605 | } |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2606 | if (context->getClientVersion() < Version(3, 1) && |
| 2607 | tfVaryingName.find('[') != std::string::npos) |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 2608 | { |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 2609 | infoLog << "Capture of array elements is undefined and not supported."; |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 2610 | return false; |
| 2611 | } |
jchen10 | 85c93c4 | 2017-11-12 15:36:47 +0800 | [diff] [blame] | 2612 | if (!found) |
| 2613 | { |
| 2614 | infoLog << "Transform feedback varying " << tfVaryingName |
| 2615 | << " does not exist in the vertex shader."; |
| 2616 | return false; |
| 2617 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2618 | } |
| 2619 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2620 | if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2621 | totalComponents > caps.maxTransformFeedbackInterleavedComponents) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2622 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2623 | infoLog << "Transform feedback varying total components (" << totalComponents |
| 2624 | << ") exceed the maximum interleaved components (" |
| 2625 | << caps.maxTransformFeedbackInterleavedComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2626 | return false; |
| 2627 | } |
| 2628 | |
| 2629 | return true; |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 2630 | } |
| 2631 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 2632 | bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const |
| 2633 | { |
| 2634 | const std::vector<sh::Uniform> &vertexUniforms = |
| 2635 | mState.mAttachedVertexShader->getUniforms(context); |
| 2636 | const std::vector<sh::Uniform> &fragmentUniforms = |
| 2637 | mState.mAttachedFragmentShader->getUniforms(context); |
| 2638 | const std::vector<sh::Attribute> &attributes = |
| 2639 | mState.mAttachedVertexShader->getActiveAttributes(context); |
| 2640 | for (const auto &attrib : attributes) |
| 2641 | { |
| 2642 | for (const auto &uniform : vertexUniforms) |
| 2643 | { |
| 2644 | if (uniform.name == attrib.name) |
| 2645 | { |
| 2646 | infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name; |
| 2647 | return false; |
| 2648 | } |
| 2649 | } |
| 2650 | for (const auto &uniform : fragmentUniforms) |
| 2651 | { |
| 2652 | if (uniform.name == attrib.name) |
| 2653 | { |
| 2654 | infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name; |
| 2655 | return false; |
| 2656 | } |
| 2657 | } |
| 2658 | } |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2662 | void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2663 | { |
| 2664 | // 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] | 2665 | mState.mLinkedTransformFeedbackVaryings.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2666 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2667 | { |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 2668 | std::vector<unsigned int> subscripts; |
| 2669 | std::string baseName = ParseResourceName(tfVaryingName, &subscripts); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2670 | size_t subscript = GL_INVALID_INDEX; |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 2671 | if (!subscripts.empty()) |
| 2672 | { |
| 2673 | subscript = subscripts.back(); |
| 2674 | } |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2675 | for (const auto &ref : varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2676 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2677 | const sh::Varying *varying = ref.second.get(); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2678 | if (baseName == varying->name) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2679 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2680 | mState.mLinkedTransformFeedbackVaryings.emplace_back( |
| 2681 | *varying, static_cast<GLuint>(subscript)); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2682 | break; |
| 2683 | } |
| 2684 | } |
| 2685 | } |
| 2686 | } |
| 2687 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2688 | Program::MergedVaryings Program::getMergedVaryings(const Context *context) const |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2689 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2690 | MergedVaryings merged; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2691 | |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 2692 | for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context)) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2693 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2694 | merged[varying.name].vertex = &varying; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2695 | } |
| 2696 | |
Jiawei Shao | 3d40488 | 2017-10-16 13:30:48 +0800 | [diff] [blame] | 2697 | for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context)) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2698 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2699 | merged[varying.name].fragment = &varying; |
| 2700 | } |
| 2701 | |
| 2702 | return merged; |
| 2703 | } |
| 2704 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2705 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2706 | void Program::linkOutputVariables(const Context *context) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2707 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2708 | Shader *fragmentShader = mState.mAttachedFragmentShader; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2709 | ASSERT(fragmentShader != nullptr); |
| 2710 | |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2711 | ASSERT(mState.mOutputVariableTypes.empty()); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 2712 | ASSERT(mState.mActiveOutputVariables.none()); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2713 | |
| 2714 | // Gather output variable types |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2715 | for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context)) |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2716 | { |
| 2717 | if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" && |
| 2718 | outputVariable.name != "gl_FragData") |
| 2719 | { |
| 2720 | continue; |
| 2721 | } |
| 2722 | |
| 2723 | unsigned int baseLocation = |
| 2724 | (outputVariable.location == -1 ? 0u |
| 2725 | : static_cast<unsigned int>(outputVariable.location)); |
| 2726 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount(); |
| 2727 | elementIndex++) |
| 2728 | { |
| 2729 | const unsigned int location = baseLocation + elementIndex; |
| 2730 | if (location >= mState.mOutputVariableTypes.size()) |
| 2731 | { |
| 2732 | mState.mOutputVariableTypes.resize(location + 1, GL_NONE); |
| 2733 | } |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 2734 | ASSERT(location < mState.mActiveOutputVariables.size()); |
| 2735 | mState.mActiveOutputVariables.set(location); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2736 | mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type); |
| 2737 | } |
| 2738 | } |
| 2739 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2740 | // Skip this step for GLES2 shaders. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2741 | if (fragmentShader->getShaderVersion(context) == 100) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2742 | return; |
| 2743 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2744 | mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2745 | // TODO(jmadill): any caps validation here? |
| 2746 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 2747 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size(); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2748 | outputVariableIndex++) |
| 2749 | { |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 2750 | const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex]; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2751 | |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 2752 | if (outputVariable.isArray()) |
| 2753 | { |
| 2754 | // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active |
| 2755 | // Resources and including [0] at the end of array variable names. |
| 2756 | mState.mOutputVariables[outputVariableIndex].name += "[0]"; |
| 2757 | mState.mOutputVariables[outputVariableIndex].mappedName += "[0]"; |
| 2758 | } |
| 2759 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2760 | // Don't store outputs for gl_FragDepth, gl_FragColor, etc. |
| 2761 | if (outputVariable.isBuiltIn()) |
| 2762 | continue; |
| 2763 | |
| 2764 | // 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] | 2765 | unsigned int baseLocation = |
| 2766 | (outputVariable.location == -1 ? 0u |
| 2767 | : static_cast<unsigned int>(outputVariable.location)); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2768 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2769 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount(); |
| 2770 | elementIndex++) |
| 2771 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 2772 | const unsigned int location = baseLocation + elementIndex; |
| 2773 | if (location >= mState.mOutputLocations.size()) |
| 2774 | { |
| 2775 | mState.mOutputLocations.resize(location + 1); |
| 2776 | } |
| 2777 | ASSERT(!mState.mOutputLocations.at(location).used()); |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 2778 | if (outputVariable.isArray()) |
| 2779 | { |
| 2780 | mState.mOutputLocations[location] = |
| 2781 | VariableLocation(elementIndex, outputVariableIndex); |
| 2782 | } |
| 2783 | else |
| 2784 | { |
| 2785 | VariableLocation locationInfo; |
| 2786 | locationInfo.index = outputVariableIndex; |
| 2787 | mState.mOutputLocations[location] = locationInfo; |
| 2788 | } |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2792 | |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 2793 | void Program::setUniformValuesFromBindingQualifiers() |
| 2794 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2795 | for (unsigned int samplerIndex : mState.mSamplerUniformRange) |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 2796 | { |
| 2797 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
| 2798 | if (samplerUniform.binding != -1) |
| 2799 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 2800 | GLint location = getUniformLocation(samplerUniform.name); |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 2801 | ASSERT(location != -1); |
| 2802 | std::vector<GLint> boundTextureUnits; |
| 2803 | for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount(); |
| 2804 | ++elementIndex) |
| 2805 | { |
| 2806 | boundTextureUnits.push_back(samplerUniform.binding + elementIndex); |
| 2807 | } |
| 2808 | setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()), |
| 2809 | boundTextureUnits.data()); |
| 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2814 | void Program::gatherAtomicCounterBuffers() |
| 2815 | { |
jchen10 | baf5d94 | 2017-08-28 20:45:48 +0800 | [diff] [blame] | 2816 | for (unsigned int index : mState.mAtomicCounterUniformRange) |
| 2817 | { |
| 2818 | auto &uniform = mState.mUniforms[index]; |
| 2819 | uniform.blockInfo.offset = uniform.offset; |
| 2820 | uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0); |
| 2821 | uniform.blockInfo.matrixStride = 0; |
| 2822 | uniform.blockInfo.isRowMajorMatrix = false; |
| 2823 | } |
| 2824 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2825 | // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer. |
| 2826 | } |
| 2827 | |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 2828 | void Program::initInterfaceBlockBindings() |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2829 | { |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2830 | // Set initial bindings from shader. |
| 2831 | for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++) |
| 2832 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2833 | InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex]; |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2834 | bindUniformBlock(blockIndex, uniformBlock.binding); |
| 2835 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2836 | } |
| 2837 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2838 | void Program::updateSamplerUniform(const VariableLocation &locationInfo, |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2839 | GLsizei clampedCount, |
| 2840 | const GLint *v) |
| 2841 | { |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 2842 | ASSERT(mState.isSamplerUniformIndex(locationInfo.index)); |
| 2843 | GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index); |
| 2844 | std::vector<GLuint> *boundTextureUnits = |
| 2845 | &mState.mSamplerBindings[samplerIndex].boundTextureUnits; |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2846 | |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 2847 | std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex); |
Jamie Madill | d68248b | 2017-09-11 14:34:14 -0400 | [diff] [blame] | 2848 | |
| 2849 | // Invalidate the validation cache. |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 2850 | mCachedValidateSamplersResult.reset(); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | template <typename T> |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2854 | GLsizei Program::clampUniformCount(const VariableLocation &locationInfo, |
| 2855 | GLsizei count, |
| 2856 | int vectorSize, |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2857 | const T *v) |
| 2858 | { |
Jamie Madill | 134f93d | 2017-08-31 17:11:00 -0400 | [diff] [blame] | 2859 | if (count == 1) |
| 2860 | return 1; |
| 2861 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2862 | const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2863 | |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2864 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 2865 | // element index used, as reported by GetActiveUniform, will be ignored by the GL." |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 2866 | unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.arrayIndex; |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2867 | GLsizei maxElementCount = |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2868 | static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents()); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2869 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2870 | if (count * vectorSize > maxElementCount) |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2871 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2872 | return maxElementCount / vectorSize; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2873 | } |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2874 | |
| 2875 | return count; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | template <size_t cols, size_t rows, typename T> |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2879 | GLsizei Program::clampMatrixUniformCount(GLint location, |
| 2880 | GLsizei count, |
| 2881 | GLboolean transpose, |
| 2882 | const T *v) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2883 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2884 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 2885 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2886 | if (!transpose) |
| 2887 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2888 | return clampUniformCount(locationInfo, count, cols * rows, v); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2889 | } |
| 2890 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2891 | const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index]; |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2892 | |
| 2893 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 2894 | // element index used, as reported by GetActiveUniform, will be ignored by the GL." |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 2895 | unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.arrayIndex; |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 2896 | return std::min(count, static_cast<GLsizei>(remainingElements)); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2897 | } |
| 2898 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2899 | // Driver differences mean that doing the uniform value cast ourselves gives consistent results. |
| 2900 | // 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] | 2901 | template <typename DestT> |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2902 | void Program::getUniformInternal(const Context *context, |
| 2903 | DestT *dataOut, |
| 2904 | GLint location, |
| 2905 | GLenum nativeType, |
| 2906 | int components) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2907 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2908 | switch (nativeType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2909 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2910 | case GL_BOOL: |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2911 | { |
| 2912 | GLint tempValue[16] = {0}; |
| 2913 | mProgram->getUniformiv(context, location, tempValue); |
| 2914 | UniformStateQueryCastLoop<GLboolean>( |
| 2915 | dataOut, reinterpret_cast<const uint8_t *>(tempValue), components); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2916 | break; |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2917 | } |
| 2918 | case GL_INT: |
| 2919 | { |
| 2920 | GLint tempValue[16] = {0}; |
| 2921 | mProgram->getUniformiv(context, location, tempValue); |
| 2922 | UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue), |
| 2923 | components); |
| 2924 | break; |
| 2925 | } |
| 2926 | case GL_UNSIGNED_INT: |
| 2927 | { |
| 2928 | GLuint tempValue[16] = {0}; |
| 2929 | mProgram->getUniformuiv(context, location, tempValue); |
| 2930 | UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue), |
| 2931 | components); |
| 2932 | break; |
| 2933 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2934 | case GL_FLOAT: |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2935 | { |
| 2936 | GLfloat tempValue[16] = {0}; |
| 2937 | mProgram->getUniformfv(context, location, tempValue); |
| 2938 | UniformStateQueryCastLoop<GLfloat>( |
| 2939 | dataOut, reinterpret_cast<const uint8_t *>(tempValue), components); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2940 | break; |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2941 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2942 | default: |
| 2943 | UNREACHABLE(); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 2944 | break; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2945 | } |
| 2946 | } |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 2947 | |
| 2948 | bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const |
| 2949 | { |
| 2950 | // Must be called after samplers are validated. |
| 2951 | ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value()); |
| 2952 | |
| 2953 | for (const auto &binding : mState.mSamplerBindings) |
| 2954 | { |
| 2955 | GLenum textureType = binding.textureType; |
| 2956 | for (const auto &unit : binding.boundTextureUnits) |
| 2957 | { |
| 2958 | GLenum programTextureID = state.getSamplerTextureId(unit, textureType); |
| 2959 | if (programTextureID == textureID) |
| 2960 | { |
| 2961 | // TODO(jmadill): Check for appropriate overlap. |
| 2962 | return true; |
| 2963 | } |
| 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | return false; |
| 2968 | } |
| 2969 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2970 | } // namespace gl |