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