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