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