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