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