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