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 | |
| 1137 | void Program::getInputResourceName(GLuint index, |
| 1138 | GLsizei bufSize, |
| 1139 | GLsizei *length, |
| 1140 | GLchar *name) const |
| 1141 | { |
| 1142 | GLint size; |
| 1143 | GLenum type; |
| 1144 | getActiveAttribute(index, bufSize, length, &size, &type, name); |
| 1145 | } |
| 1146 | |
| 1147 | void Program::getOutputResourceName(GLuint index, |
| 1148 | GLsizei bufSize, |
| 1149 | GLsizei *length, |
| 1150 | GLchar *name) const |
| 1151 | { |
| 1152 | if (length) |
| 1153 | { |
| 1154 | *length = 0; |
| 1155 | } |
| 1156 | |
| 1157 | if (!mLinked) |
| 1158 | { |
| 1159 | if (bufSize > 0) |
| 1160 | { |
| 1161 | name[0] = '\0'; |
| 1162 | } |
| 1163 | return; |
| 1164 | } |
| 1165 | ASSERT(index < mState.mOutputVariables.size()); |
| 1166 | const auto &output = mState.mOutputVariables[index]; |
| 1167 | |
| 1168 | if (bufSize > 0) |
| 1169 | { |
| 1170 | std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name); |
| 1171 | |
| 1172 | CopyStringToBuffer(name, nameWithArray, bufSize, length); |
| 1173 | } |
| 1174 | } |
| 1175 | |
jchen10 | 880683b | 2017-04-12 16:21:55 +0800 | [diff] [blame] | 1176 | const sh::Attribute &Program::getInputResource(GLuint index) const |
| 1177 | { |
| 1178 | ASSERT(index < mState.mAttributes.size()); |
| 1179 | return mState.mAttributes[index]; |
| 1180 | } |
| 1181 | |
| 1182 | const sh::OutputVariable &Program::getOutputResource(GLuint index) const |
| 1183 | { |
| 1184 | ASSERT(index < mState.mOutputVariables.size()); |
| 1185 | return mState.mOutputVariables[index]; |
| 1186 | } |
| 1187 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1188 | GLint Program::getFragDataLocation(const std::string &name) const |
| 1189 | { |
| 1190 | std::string baseName(name); |
| 1191 | unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName); |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1192 | for (auto outputPair : mState.mOutputLocations) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1193 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 1194 | const VariableLocation &locationInfo = outputPair.second; |
| 1195 | const sh::OutputVariable &outputVariable = mState.mOutputVariables[locationInfo.index]; |
| 1196 | if (outputVariable.name == baseName && |
| 1197 | (arrayIndex == GL_INVALID_INDEX || arrayIndex == locationInfo.element)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1198 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 1199 | return static_cast<GLint>(outputPair.first); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1200 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1201 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1202 | return -1; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1205 | void Program::getActiveUniform(GLuint index, |
| 1206 | GLsizei bufsize, |
| 1207 | GLsizei *length, |
| 1208 | GLint *size, |
| 1209 | GLenum *type, |
| 1210 | GLchar *name) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1211 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1212 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1213 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1214 | // index must be smaller than getActiveUniformCount() |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1215 | ASSERT(index < mState.mUniforms.size()); |
| 1216 | const LinkedUniform &uniform = mState.mUniforms[index]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1217 | |
| 1218 | if (bufsize > 0) |
| 1219 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1220 | std::string string = uniform.name; |
| 1221 | if (uniform.isArray()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1222 | { |
| 1223 | string += "[0]"; |
| 1224 | } |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1225 | CopyStringToBuffer(name, string, bufsize, length); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1226 | } |
| 1227 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1228 | *size = uniform.elementCount(); |
| 1229 | *type = uniform.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | if (bufsize > 0) |
| 1234 | { |
| 1235 | name[0] = '\0'; |
| 1236 | } |
| 1237 | |
| 1238 | if (length) |
| 1239 | { |
| 1240 | *length = 0; |
| 1241 | } |
| 1242 | |
| 1243 | *size = 0; |
| 1244 | *type = GL_NONE; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1248 | GLint Program::getActiveUniformCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1249 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1250 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1251 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1252 | return static_cast<GLint>(mState.mUniforms.size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | return 0; |
| 1257 | } |
| 1258 | } |
| 1259 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1260 | GLint Program::getActiveUniformMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1261 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1262 | size_t maxLength = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1263 | |
| 1264 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1265 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1266 | for (const LinkedUniform &uniform : mState.mUniforms) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1267 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1268 | if (!uniform.name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1269 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1270 | size_t length = uniform.name.length() + 1u; |
| 1271 | if (uniform.isArray()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1272 | { |
| 1273 | length += 3; // Counting in "[0]". |
| 1274 | } |
| 1275 | maxLength = std::max(length, maxLength); |
| 1276 | } |
| 1277 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1278 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1279 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1280 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | GLint Program::getActiveUniformi(GLuint index, GLenum pname) const |
| 1284 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1285 | ASSERT(static_cast<size_t>(index) < mState.mUniforms.size()); |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1286 | const LinkedUniform &uniform = mState.mUniforms[index]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1287 | switch (pname) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1288 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1289 | case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type); |
| 1290 | case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount()); |
| 1291 | case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0)); |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1292 | case GL_UNIFORM_BLOCK_INDEX: |
| 1293 | return uniform.bufferIndex; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1294 | case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset; |
| 1295 | case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride; |
| 1296 | case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride; |
| 1297 | case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix); |
| 1298 | default: |
| 1299 | UNREACHABLE(); |
| 1300 | break; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1301 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | bool Program::isValidUniformLocation(GLint location) const |
| 1306 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1307 | ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size())); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1308 | return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() && |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 1309 | mState.mUniformLocations[static_cast<size_t>(location)].used()); |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 1310 | } |
| 1311 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1312 | const LinkedUniform &Program::getUniformByLocation(GLint location) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1313 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1314 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1315 | return mState.mUniforms[mState.getUniformIndexFromLocation(location)]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1316 | } |
| 1317 | |
Jamie Madill | ac4e9c3 | 2017-01-13 14:07:12 -0500 | [diff] [blame] | 1318 | const VariableLocation &Program::getUniformLocation(GLint location) const |
| 1319 | { |
| 1320 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
| 1321 | return mState.mUniformLocations[location]; |
| 1322 | } |
| 1323 | |
| 1324 | const std::vector<VariableLocation> &Program::getUniformLocations() const |
| 1325 | { |
| 1326 | return mState.mUniformLocations; |
| 1327 | } |
| 1328 | |
| 1329 | const LinkedUniform &Program::getUniformByIndex(GLuint index) const |
| 1330 | { |
| 1331 | ASSERT(index < static_cast<size_t>(mState.mUniforms.size())); |
| 1332 | return mState.mUniforms[index]; |
| 1333 | } |
| 1334 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1335 | GLint Program::getUniformLocation(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1336 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1337 | return mState.getUniformLocation(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1338 | } |
| 1339 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1340 | GLuint Program::getUniformIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1341 | { |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1342 | return mState.getUniformIndexFromName(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 1346 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1347 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1348 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1349 | mProgram->setUniform1fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 1353 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1354 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1355 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1356 | mProgram->setUniform2fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 1360 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1361 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1362 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1363 | mProgram->setUniform3fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 1367 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1368 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1369 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1370 | mProgram->setUniform4fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1371 | } |
| 1372 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1373 | Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1374 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1375 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1376 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
| 1377 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1378 | mProgram->setUniform1iv(location, clampedCount, v); |
| 1379 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1380 | if (mState.isSamplerUniformIndex(locationInfo.index)) |
| 1381 | { |
| 1382 | updateSamplerUniform(locationInfo, clampedCount, v); |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1383 | return SetUniformResult::SamplerChanged; |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1384 | } |
| 1385 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 1386 | return SetUniformResult::NoSamplerChange; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 1390 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1391 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1392 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1393 | mProgram->setUniform2iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 1397 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1398 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1399 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1400 | mProgram->setUniform3iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 1404 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1405 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1406 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1407 | mProgram->setUniform4iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 1411 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1412 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1413 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1414 | mProgram->setUniform1uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 1418 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1419 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1420 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1421 | mProgram->setUniform2uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 1425 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1426 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1427 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1428 | mProgram->setUniform3uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 1432 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1433 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 1434 | GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1435 | mProgram->setUniform4uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | void Program::setUniformMatrix2fv(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<2, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1441 | mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | void Program::setUniformMatrix3fv(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<3, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1447 | mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | void Program::setUniformMatrix4fv(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<4, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1453 | mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | void Program::setUniformMatrix2x3fv(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, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1459 | mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | void Program::setUniformMatrix2x4fv(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<2, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1465 | mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | void Program::setUniformMatrix3x2fv(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, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1471 | mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | void Program::setUniformMatrix3x4fv(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<3, 4>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1477 | mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | void Program::setUniformMatrix4x2fv(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, 2>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1483 | mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1487 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 1488 | GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1489 | mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1490 | } |
| 1491 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1492 | void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1493 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1494 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1495 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1496 | |
| 1497 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1498 | if (nativeType == GL_FLOAT) |
| 1499 | { |
| 1500 | mProgram->getUniformfv(context, location, v); |
| 1501 | } |
| 1502 | else |
| 1503 | { |
| 1504 | getUniformInternal(context, v, location, nativeType, |
| 1505 | gl::VariableComponentCount(uniform.type)); |
| 1506 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1507 | } |
| 1508 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1509 | void Program::getUniformiv(const Context *context, GLint location, GLint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1510 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1511 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1512 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1513 | |
| 1514 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1515 | if (nativeType == GL_INT || nativeType == GL_BOOL) |
| 1516 | { |
| 1517 | mProgram->getUniformiv(context, location, v); |
| 1518 | } |
| 1519 | else |
| 1520 | { |
| 1521 | getUniformInternal(context, v, location, nativeType, |
| 1522 | gl::VariableComponentCount(uniform.type)); |
| 1523 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1524 | } |
| 1525 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1526 | void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1527 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1528 | const auto &uniformLocation = mState.getUniformLocations()[location]; |
| 1529 | const auto &uniform = mState.getUniforms()[uniformLocation.index]; |
| 1530 | |
| 1531 | GLenum nativeType = gl::VariableComponentType(uniform.type); |
| 1532 | if (nativeType == GL_UNSIGNED_INT) |
| 1533 | { |
| 1534 | mProgram->getUniformuiv(context, location, v); |
| 1535 | } |
| 1536 | else |
| 1537 | { |
| 1538 | getUniformInternal(context, v, location, nativeType, |
| 1539 | gl::VariableComponentCount(uniform.type)); |
| 1540 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1541 | } |
| 1542 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1543 | void Program::flagForDeletion() |
| 1544 | { |
| 1545 | mDeleteStatus = true; |
| 1546 | } |
| 1547 | |
| 1548 | bool Program::isFlaggedForDeletion() const |
| 1549 | { |
| 1550 | return mDeleteStatus; |
| 1551 | } |
daniel@transgaming.com | 86a7a13 | 2010-04-29 03:32:32 +0000 | [diff] [blame] | 1552 | |
Brandon Jones | 43a53e2 | 2014-08-28 16:23:22 -0700 | [diff] [blame] | 1553 | void Program::validate(const Caps &caps) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1554 | { |
| 1555 | mInfoLog.reset(); |
| 1556 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1557 | if (mLinked) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1558 | { |
Jamie Madill | 36cfd6a | 2015-08-18 10:46:20 -0400 | [diff] [blame] | 1559 | mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1560 | } |
| 1561 | else |
| 1562 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1563 | mInfoLog << "Program has not been successfully linked."; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1567 | bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps) |
| 1568 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1569 | // Skip cache if we're using an infolog, so we get the full error. |
| 1570 | // Also skip the cache if the sample mapping has changed, or if we haven't ever validated. |
| 1571 | if (infoLog == nullptr && mCachedValidateSamplersResult.valid()) |
| 1572 | { |
| 1573 | return mCachedValidateSamplersResult.value(); |
| 1574 | } |
| 1575 | |
| 1576 | if (mTextureUnitTypesCache.empty()) |
| 1577 | { |
| 1578 | mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE); |
| 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE); |
| 1583 | } |
| 1584 | |
| 1585 | // if any two active samplers in a program are of different types, but refer to the same |
| 1586 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 1587 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1588 | for (const auto &samplerBinding : mState.mSamplerBindings) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1589 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1590 | if (samplerBinding.unreferenced) |
| 1591 | continue; |
| 1592 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1593 | GLenum textureType = samplerBinding.textureType; |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1594 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1595 | for (GLuint textureUnit : samplerBinding.boundTextureUnits) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1596 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1597 | if (textureUnit >= caps.maxCombinedTextureImageUnits) |
| 1598 | { |
| 1599 | if (infoLog) |
| 1600 | { |
| 1601 | (*infoLog) << "Sampler uniform (" << textureUnit |
| 1602 | << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (" |
| 1603 | << caps.maxCombinedTextureImageUnits << ")"; |
| 1604 | } |
| 1605 | |
| 1606 | mCachedValidateSamplersResult = false; |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
| 1610 | if (mTextureUnitTypesCache[textureUnit] != GL_NONE) |
| 1611 | { |
| 1612 | if (textureType != mTextureUnitTypesCache[textureUnit]) |
| 1613 | { |
| 1614 | if (infoLog) |
| 1615 | { |
| 1616 | (*infoLog) << "Samplers of conflicting types refer to the same texture " |
| 1617 | "image unit (" |
| 1618 | << textureUnit << ")."; |
| 1619 | } |
| 1620 | |
| 1621 | mCachedValidateSamplersResult = false; |
| 1622 | return false; |
| 1623 | } |
| 1624 | } |
| 1625 | else |
| 1626 | { |
| 1627 | mTextureUnitTypesCache[textureUnit] = textureType; |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | mCachedValidateSamplersResult = true; |
| 1633 | return true; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1634 | } |
| 1635 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1636 | bool Program::isValidated() const |
| 1637 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1638 | return mValidated; |
| 1639 | } |
| 1640 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1641 | GLuint Program::getActiveUniformBlockCount() const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1642 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1643 | return static_cast<GLuint>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1644 | } |
| 1645 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1646 | GLuint Program::getActiveShaderStorageBlockCount() const |
| 1647 | { |
| 1648 | return static_cast<GLuint>(mState.mShaderStorageBlocks.size()); |
| 1649 | } |
| 1650 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1651 | void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const |
| 1652 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1653 | ASSERT( |
| 1654 | uniformBlockIndex < |
| 1655 | mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount() |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1656 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1657 | const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1658 | |
| 1659 | if (bufSize > 0) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1660 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1661 | std::string string = uniformBlock.name; |
| 1662 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1663 | if (uniformBlock.isArray) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1664 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1665 | string += ArrayString(uniformBlock.arrayElement); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1666 | } |
jchen10 | fd7c3b5 | 2017-03-21 15:36:03 +0800 | [diff] [blame] | 1667 | CopyStringToBuffer(uniformBlockName, string, bufSize, length); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1671 | GLint Program::getActiveUniformBlockMaxLength() const |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1672 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1673 | int maxLength = 0; |
| 1674 | |
| 1675 | if (mLinked) |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1676 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1677 | unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1678 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++) |
| 1679 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1680 | const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1681 | if (!uniformBlock.name.empty()) |
| 1682 | { |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 1683 | int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length()); |
| 1684 | maxLength = std::max(length + 1, maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1685 | } |
| 1686 | } |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1687 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1688 | |
| 1689 | return maxLength; |
| 1690 | } |
| 1691 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1692 | GLuint Program::getUniformBlockIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1693 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1694 | size_t subscript = GL_INVALID_INDEX; |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1695 | std::string baseName = ParseResourceName(name, &subscript); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1696 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1697 | unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size()); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1698 | for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++) |
| 1699 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1700 | const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1701 | if (uniformBlock.name == baseName) |
| 1702 | { |
| 1703 | const bool arrayElementZero = |
| 1704 | (subscript == GL_INVALID_INDEX && |
| 1705 | (!uniformBlock.isArray || uniformBlock.arrayElement == 0)); |
| 1706 | if (subscript == uniformBlock.arrayElement || arrayElementZero) |
| 1707 | { |
| 1708 | return blockIndex; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | return GL_INVALID_INDEX; |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1716 | const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1717 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1718 | ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size())); |
| 1719 | return mState.mUniformBlocks[index]; |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1722 | void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 1723 | { |
jchen10 | 7a20b97 | 2017-06-13 14:25:26 +0800 | [diff] [blame] | 1724 | mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding; |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 1725 | mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 1726 | mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const |
| 1730 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1731 | return mState.getUniformBlockBinding(uniformBlockIndex); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 1734 | GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const |
| 1735 | { |
| 1736 | return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex); |
| 1737 | } |
| 1738 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1739 | void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode) |
| 1740 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1741 | mState.mTransformFeedbackVaryingNames.resize(count); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1742 | for (GLsizei i = 0; i < count; i++) |
| 1743 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1744 | mState.mTransformFeedbackVaryingNames[i] = varyings[i]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1745 | } |
| 1746 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1747 | mState.mTransformFeedbackBufferMode = bufferMode; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const |
| 1751 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1752 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1753 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1754 | ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size()); |
| 1755 | const auto &var = mState.mLinkedTransformFeedbackVaryings[index]; |
| 1756 | std::string varName = var.nameWithArrayIndex(); |
| 1757 | GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length())); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1758 | if (length) |
| 1759 | { |
| 1760 | *length = lastNameIdx; |
| 1761 | } |
| 1762 | if (size) |
| 1763 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1764 | *size = var.size(); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1765 | } |
| 1766 | if (type) |
| 1767 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1768 | *type = var.type; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1769 | } |
| 1770 | if (name) |
| 1771 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1772 | memcpy(name, varName.c_str(), lastNameIdx); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1773 | name[lastNameIdx] = '\0'; |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1778 | GLsizei Program::getTransformFeedbackVaryingCount() const |
| 1779 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1780 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1781 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1782 | return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size()); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1783 | } |
| 1784 | else |
| 1785 | { |
| 1786 | return 0; |
| 1787 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | GLsizei Program::getTransformFeedbackVaryingMaxLength() const |
| 1791 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1792 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1793 | { |
| 1794 | GLsizei maxSize = 0; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1795 | for (const auto &var : mState.mLinkedTransformFeedbackVaryings) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1796 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 1797 | maxSize = |
| 1798 | std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1)); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | return maxSize; |
| 1802 | } |
| 1803 | else |
| 1804 | { |
| 1805 | return 0; |
| 1806 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | GLenum Program::getTransformFeedbackBufferMode() const |
| 1810 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1811 | return mState.mTransformFeedbackBufferMode; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1812 | } |
| 1813 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1814 | bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1815 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1816 | Shader *vertexShader = mState.mAttachedVertexShader; |
| 1817 | Shader *fragmentShader = mState.mAttachedFragmentShader; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1818 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1819 | ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context)); |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 1820 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1821 | const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context); |
| 1822 | const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1823 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 1824 | std::map<GLuint, std::string> staticFragmentInputLocations; |
| 1825 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1826 | for (const sh::Varying &output : fragmentVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1827 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1828 | bool matched = false; |
| 1829 | |
| 1830 | // Built-in varyings obey special rules |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1831 | if (output.isBuiltIn()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1832 | { |
| 1833 | continue; |
| 1834 | } |
| 1835 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1836 | for (const sh::Varying &input : vertexVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1837 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1838 | if (output.name == input.name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1839 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1840 | ASSERT(!input.isBuiltIn()); |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 1841 | if (!linkValidateVaryings(infoLog, output.name, input, output, |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1842 | vertexShader->getShaderVersion(context))) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1843 | { |
| 1844 | return false; |
| 1845 | } |
| 1846 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1847 | matched = true; |
| 1848 | break; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | // We permit unmatched, unreferenced varyings |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1853 | if (!matched && output.staticUse) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1854 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1855 | infoLog << "Fragment varying " << output.name << " does not match any vertex varying"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1856 | return false; |
| 1857 | } |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 1858 | |
| 1859 | // Check for aliased path rendering input bindings (if any). |
| 1860 | // If more than one binding refer statically to the same |
| 1861 | // location the link must fail. |
| 1862 | |
| 1863 | if (!output.staticUse) |
| 1864 | continue; |
| 1865 | |
| 1866 | const auto inputBinding = mFragmentInputBindings.getBinding(output.name); |
| 1867 | if (inputBinding == -1) |
| 1868 | continue; |
| 1869 | |
| 1870 | const auto it = staticFragmentInputLocations.find(inputBinding); |
| 1871 | if (it == std::end(staticFragmentInputLocations)) |
| 1872 | { |
| 1873 | staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name)); |
| 1874 | } |
| 1875 | else |
| 1876 | { |
| 1877 | infoLog << "Binding for fragment input " << output.name << " conflicts with " |
| 1878 | << it->second; |
| 1879 | return false; |
| 1880 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1881 | } |
| 1882 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1883 | if (!linkValidateBuiltInVaryings(context, infoLog)) |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 1884 | { |
| 1885 | return false; |
| 1886 | } |
| 1887 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1888 | // TODO(jmadill): verify no unmatched vertex varyings? |
| 1889 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1890 | return true; |
| 1891 | } |
| 1892 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1893 | bool Program::linkUniforms(const Context *context, |
| 1894 | InfoLog &infoLog, |
Olli Etuaho | 4a92ceb | 2017-02-19 17:51:24 +0000 | [diff] [blame] | 1895 | const Bindings &uniformLocationBindings) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1896 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1897 | UniformLinker linker(mState); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 1898 | if (!linker.link(context, infoLog, uniformLocationBindings)) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1899 | { |
| 1900 | return false; |
| 1901 | } |
| 1902 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1903 | linker.getResults(&mState.mUniforms, &mState.mUniformLocations); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1904 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1905 | linkSamplerAndImageBindings(); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1906 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1907 | if (!linkAtomicCounterBuffers()) |
| 1908 | { |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1912 | return true; |
| 1913 | } |
| 1914 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1915 | void Program::linkSamplerAndImageBindings() |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1916 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 1917 | unsigned int high = static_cast<unsigned int>(mState.mUniforms.size()); |
| 1918 | unsigned int low = high; |
| 1919 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1920 | for (auto counterIter = mState.mUniforms.rbegin(); |
| 1921 | counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter) |
| 1922 | { |
| 1923 | --low; |
| 1924 | } |
| 1925 | |
| 1926 | mState.mAtomicCounterUniformRange = RangeUI(low, high); |
| 1927 | |
| 1928 | high = low; |
| 1929 | |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1930 | for (auto imageIter = mState.mUniforms.rbegin(); |
| 1931 | imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter) |
| 1932 | { |
| 1933 | --low; |
| 1934 | } |
| 1935 | |
| 1936 | mState.mImageUniformRange = RangeUI(low, high); |
| 1937 | |
| 1938 | // If uniform is a image type, insert it into the mImageBindings array. |
| 1939 | for (unsigned int imageIndex : mState.mImageUniformRange) |
| 1940 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 1941 | // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands |
| 1942 | // cannot load values into a uniform defined as an image. if declare without a |
| 1943 | // binding qualifier, any uniform image variable (include all elements of |
| 1944 | // unbound image array) shoud be bound to unit zero. |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1945 | auto &imageUniform = mState.mUniforms[imageIndex]; |
| 1946 | if (imageUniform.binding == -1) |
| 1947 | { |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 1948 | mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount())); |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1949 | } |
Xinghua Cao | 0328b57 | 2017-06-26 15:51:36 +0800 | [diff] [blame] | 1950 | else |
| 1951 | { |
| 1952 | mState.mImageBindings.emplace_back( |
| 1953 | ImageBinding(imageUniform.binding, imageUniform.elementCount())); |
| 1954 | } |
Xinghua Cao | 65ec0b2 | 2017-03-28 16:10:52 +0800 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | high = low; |
| 1958 | |
| 1959 | for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length(); |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 1960 | samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter) |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1961 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 1962 | --low; |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1963 | } |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 1964 | |
| 1965 | mState.mSamplerUniformRange = RangeUI(low, high); |
| 1966 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1967 | // If uniform is a sampler type, insert it into the mSamplerBindings array. |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 1968 | for (unsigned int samplerIndex : mState.mSamplerUniformRange) |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1969 | { |
| 1970 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
| 1971 | GLenum textureType = SamplerTypeToTextureType(samplerUniform.type); |
| 1972 | mState.mSamplerBindings.emplace_back( |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 1973 | SamplerBinding(textureType, samplerUniform.elementCount(), false)); |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 1977 | bool Program::linkAtomicCounterBuffers() |
| 1978 | { |
| 1979 | for (unsigned int index : mState.mAtomicCounterUniformRange) |
| 1980 | { |
| 1981 | auto &uniform = mState.mUniforms[index]; |
| 1982 | bool found = false; |
| 1983 | for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size(); |
| 1984 | ++bufferIndex) |
| 1985 | { |
| 1986 | auto &buffer = mState.mAtomicCounterBuffers[bufferIndex]; |
| 1987 | if (buffer.binding == uniform.binding) |
| 1988 | { |
| 1989 | buffer.memberIndexes.push_back(index); |
| 1990 | uniform.bufferIndex = bufferIndex; |
| 1991 | found = true; |
| 1992 | break; |
| 1993 | } |
| 1994 | } |
| 1995 | if (!found) |
| 1996 | { |
| 1997 | AtomicCounterBuffer atomicCounterBuffer; |
| 1998 | atomicCounterBuffer.binding = uniform.binding; |
| 1999 | atomicCounterBuffer.memberIndexes.push_back(index); |
| 2000 | mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer); |
| 2001 | uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1); |
| 2002 | } |
| 2003 | } |
| 2004 | // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against |
| 2005 | // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers. |
| 2006 | |
| 2007 | return true; |
| 2008 | } |
| 2009 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2010 | bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, |
| 2011 | const std::string &uniformName, |
| 2012 | const sh::InterfaceBlockField &vertexUniform, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2013 | const sh::InterfaceBlockField &fragmentUniform, |
| 2014 | bool webglCompatibility) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2015 | { |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2016 | // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287. |
| 2017 | if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, |
| 2018 | webglCompatibility)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2019 | { |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
| 2023 | if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout) |
| 2024 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2025 | infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | return true; |
| 2030 | } |
| 2031 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2032 | // Assigns locations to all attributes from the bindings and program locations. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2033 | bool Program::linkAttributes(const Context *context, InfoLog &infoLog) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2034 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2035 | const ContextState &data = context->getContextState(); |
| 2036 | auto *vertexShader = mState.getAttachedVertexShader(); |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2037 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2038 | unsigned int usedLocations = 0; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2039 | mState.mAttributes = vertexShader->getActiveAttributes(context); |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 2040 | GLuint maxAttribs = data.getCaps().maxVertexAttributes; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2041 | |
| 2042 | // TODO(jmadill): handle aliasing robustly |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2043 | if (mState.mAttributes.size() > maxAttribs) |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2044 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2045 | infoLog << "Too many vertex attributes."; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2046 | return false; |
| 2047 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2048 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 2049 | std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr); |
Jamie Madill | 4e10722 | 2015-08-24 14:12:17 +0000 | [diff] [blame] | 2050 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2051 | // Link attributes that have a binding location |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2052 | for (sh::Attribute &attribute : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2053 | { |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 2054 | int bindingLocation = mAttributeBindings.getBinding(attribute.name); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2055 | if (attribute.location == -1 && bindingLocation != -1) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 2056 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2057 | attribute.location = bindingLocation; |
| 2058 | } |
| 2059 | |
| 2060 | if (attribute.location != -1) |
| 2061 | { |
| 2062 | // Location is set by glBindAttribLocation or by location layout qualifier |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2063 | const int regs = VariableRegisterCount(attribute.type); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2064 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2065 | if (static_cast<GLuint>(regs + attribute.location) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2066 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2067 | infoLog << "Active attribute (" << attribute.name << ") at location " |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2068 | << attribute.location << " is too big to fit"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2069 | |
| 2070 | return false; |
| 2071 | } |
| 2072 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2073 | for (int reg = 0; reg < regs; reg++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2074 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2075 | const int regLocation = attribute.location + reg; |
| 2076 | sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2077 | |
| 2078 | // In GLSL 3.00, attribute aliasing produces a link error |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2079 | // 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] | 2080 | if (linkedAttribute) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2081 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2082 | // TODO(jmadill): fix aliasing on ES2 |
| 2083 | // if (mProgram->getShaderVersion() >= 300) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2084 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 2085 | infoLog << "Attribute '" << attribute.name << "' aliases attribute '" |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2086 | << linkedAttribute->name << "' at location " << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2087 | return false; |
| 2088 | } |
| 2089 | } |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2090 | else |
| 2091 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2092 | usedAttribMap[regLocation] = &attribute; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2093 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2094 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2095 | usedLocations |= 1 << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | // Link attributes that don't have a binding location |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2101 | for (sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2102 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2103 | // Not set by glBindAttribLocation or by location layout qualifier |
| 2104 | if (attribute.location == -1) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2105 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2106 | int regs = VariableRegisterCount(attribute.type); |
| 2107 | int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2108 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2109 | if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2110 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2111 | infoLog << "Too many active attributes (" << attribute.name << ")"; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2112 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2113 | } |
| 2114 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2115 | attribute.location = availableIndex; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2116 | } |
| 2117 | } |
| 2118 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2119 | for (const sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2120 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2121 | ASSERT(attribute.location != -1); |
| 2122 | int regs = VariableRegisterCount(attribute.type); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2123 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2124 | for (int r = 0; r < regs; r++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2125 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2126 | mState.mActiveAttribLocationsMask.set(attribute.location + r); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2130 | return true; |
| 2131 | } |
| 2132 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2133 | bool Program::validateVertexAndFragmentInterfaceBlocks( |
| 2134 | const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks, |
| 2135 | const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2136 | InfoLog &infoLog, |
| 2137 | bool webglCompatibility) const |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2138 | { |
| 2139 | // 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] | 2140 | typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap; |
| 2141 | InterfaceBlockMap linkedInterfaceBlocks; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2142 | |
| 2143 | for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks) |
| 2144 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2145 | linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2146 | } |
| 2147 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2148 | for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2149 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2150 | auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name); |
| 2151 | if (entry != linkedInterfaceBlocks.end()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2152 | { |
| 2153 | const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second; |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2154 | if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock, |
| 2155 | webglCompatibility)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2156 | { |
| 2157 | return false; |
| 2158 | } |
| 2159 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2160 | // TODO(jiajia.qin@intel.com): Add |
| 2161 | // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation. |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2162 | } |
| 2163 | return true; |
| 2164 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2165 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2166 | bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2167 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2168 | const auto &caps = context->getCaps(); |
| 2169 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2170 | if (mState.mAttachedComputeShader) |
| 2171 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2172 | Shader &computeShader = *mState.mAttachedComputeShader; |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2173 | const auto &computeUniformBlocks = computeShader.getUniformBlocks(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2174 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2175 | if (!validateInterfaceBlocksCount( |
| 2176 | caps.maxComputeUniformBlocks, computeUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2177 | "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (", |
| 2178 | infoLog)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2179 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2180 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2181 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2182 | |
| 2183 | const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context); |
| 2184 | if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks, |
| 2185 | computeShaderStorageBlocks, |
| 2186 | "Compute shader shader storage block count exceeds " |
| 2187 | "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (", |
| 2188 | infoLog)) |
| 2189 | { |
| 2190 | return false; |
| 2191 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2192 | return true; |
| 2193 | } |
| 2194 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2195 | Shader &vertexShader = *mState.mAttachedVertexShader; |
| 2196 | Shader &fragmentShader = *mState.mAttachedFragmentShader; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2197 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2198 | const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context); |
| 2199 | const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2200 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2201 | if (!validateInterfaceBlocksCount( |
| 2202 | caps.maxVertexUniformBlocks, vertexUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2203 | "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog)) |
| 2204 | { |
| 2205 | return false; |
| 2206 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2207 | if (!validateInterfaceBlocksCount( |
| 2208 | caps.maxFragmentUniformBlocks, fragmentUniformBlocks, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2209 | "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (", |
| 2210 | infoLog)) |
| 2211 | { |
| 2212 | |
| 2213 | return false; |
| 2214 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2215 | |
| 2216 | bool webglCompatibility = context->getExtensions().webglCompatibility; |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2217 | if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2218 | infoLog, webglCompatibility)) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2219 | { |
| 2220 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2221 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2222 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2223 | if (context->getClientVersion() >= Version(3, 1)) |
| 2224 | { |
| 2225 | const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context); |
| 2226 | const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context); |
| 2227 | |
| 2228 | if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks, |
| 2229 | vertexShaderStorageBlocks, |
| 2230 | "Vertex shader shader storage block count exceeds " |
| 2231 | "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (", |
| 2232 | infoLog)) |
| 2233 | { |
| 2234 | return false; |
| 2235 | } |
| 2236 | if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks, |
| 2237 | fragmentShaderStorageBlocks, |
| 2238 | "Fragment shader shader storage block count exceeds " |
| 2239 | "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (", |
| 2240 | infoLog)) |
| 2241 | { |
| 2242 | |
| 2243 | return false; |
| 2244 | } |
| 2245 | |
| 2246 | if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks, |
| 2247 | fragmentShaderStorageBlocks, infoLog, |
| 2248 | webglCompatibility)) |
| 2249 | { |
| 2250 | return false; |
| 2251 | } |
| 2252 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2253 | return true; |
| 2254 | } |
| 2255 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2256 | bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2257 | const sh::InterfaceBlock &vertexInterfaceBlock, |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2258 | const sh::InterfaceBlock &fragmentInterfaceBlock, |
| 2259 | bool webglCompatibility) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2260 | { |
| 2261 | const char* blockName = vertexInterfaceBlock.name.c_str(); |
| 2262 | // validate blocks for the same member types |
| 2263 | if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size()) |
| 2264 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2265 | infoLog << "Types for interface block '" << blockName |
| 2266 | << "' differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2267 | return false; |
| 2268 | } |
| 2269 | if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize) |
| 2270 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2271 | infoLog << "Array sizes differ for interface block '" << blockName |
| 2272 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2273 | return false; |
| 2274 | } |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2275 | if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || |
| 2276 | vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout || |
| 2277 | vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2278 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2279 | infoLog << "Layout qualifiers differ for interface block '" << blockName |
| 2280 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2281 | return false; |
| 2282 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2283 | const unsigned int numBlockMembers = |
| 2284 | static_cast<unsigned int>(vertexInterfaceBlock.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2285 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++) |
| 2286 | { |
| 2287 | const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex]; |
| 2288 | const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex]; |
| 2289 | if (vertexMember.name != fragmentMember.name) |
| 2290 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2291 | infoLog << "Name mismatch for field " << blockMemberIndex |
| 2292 | << " of interface block '" << blockName |
| 2293 | << "': (in vertex: '" << vertexMember.name |
| 2294 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2295 | return false; |
| 2296 | } |
| 2297 | std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'"; |
Frank Henigman | fccbac2 | 2017-05-28 17:29:26 -0400 | [diff] [blame] | 2298 | if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember, |
| 2299 | webglCompatibility)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2300 | { |
| 2301 | return false; |
| 2302 | } |
| 2303 | } |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
| 2307 | bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable, |
| 2308 | const sh::ShaderVariable &fragmentVariable, bool validatePrecision) |
| 2309 | { |
| 2310 | if (vertexVariable.type != fragmentVariable.type) |
| 2311 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2312 | infoLog << "Types for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2313 | return false; |
| 2314 | } |
| 2315 | if (vertexVariable.arraySize != fragmentVariable.arraySize) |
| 2316 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2317 | infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2318 | return false; |
| 2319 | } |
| 2320 | if (validatePrecision && vertexVariable.precision != fragmentVariable.precision) |
| 2321 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2322 | infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2323 | return false; |
| 2324 | } |
Geoff Lang | bb1e750 | 2017-06-05 16:40:09 -0400 | [diff] [blame] | 2325 | if (vertexVariable.structName != fragmentVariable.structName) |
| 2326 | { |
| 2327 | infoLog << "Structure names for " << variableName |
| 2328 | << " differ between vertex and fragment shaders"; |
| 2329 | return false; |
| 2330 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2331 | |
| 2332 | if (vertexVariable.fields.size() != fragmentVariable.fields.size()) |
| 2333 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2334 | infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2335 | return false; |
| 2336 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2337 | const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2338 | for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++) |
| 2339 | { |
| 2340 | const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex]; |
| 2341 | const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex]; |
| 2342 | |
| 2343 | if (vertexMember.name != fragmentMember.name) |
| 2344 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2345 | infoLog << "Name mismatch for field '" << memberIndex |
| 2346 | << "' of " << variableName |
| 2347 | << ": (in vertex: '" << vertexMember.name |
| 2348 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2349 | return false; |
| 2350 | } |
| 2351 | |
| 2352 | const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." + |
| 2353 | vertexMember.name + "'"; |
| 2354 | |
| 2355 | if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision)) |
| 2356 | { |
| 2357 | return false; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | return true; |
| 2362 | } |
| 2363 | |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2364 | bool Program::linkValidateVaryings(InfoLog &infoLog, |
| 2365 | const std::string &varyingName, |
| 2366 | const sh::Varying &vertexVarying, |
| 2367 | const sh::Varying &fragmentVarying, |
| 2368 | int shaderVersion) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2369 | { |
| 2370 | if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false)) |
| 2371 | { |
| 2372 | return false; |
| 2373 | } |
| 2374 | |
Jamie Madill | e9cc469 | 2015-02-19 16:00:13 -0500 | [diff] [blame] | 2375 | if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2376 | { |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2377 | infoLog << "Interpolation types for " << varyingName |
| 2378 | << " differ between vertex and fragment shaders."; |
| 2379 | return false; |
| 2380 | } |
| 2381 | |
| 2382 | if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant) |
| 2383 | { |
| 2384 | infoLog << "Invariance for " << varyingName |
| 2385 | << " differs between vertex and fragment shaders."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2386 | return false; |
| 2387 | } |
| 2388 | |
| 2389 | return true; |
| 2390 | } |
| 2391 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2392 | bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2393 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2394 | Shader *vertexShader = mState.mAttachedVertexShader; |
| 2395 | Shader *fragmentShader = mState.mAttachedFragmentShader; |
| 2396 | const auto &vertexVaryings = vertexShader->getVaryings(context); |
| 2397 | const auto &fragmentVaryings = fragmentShader->getVaryings(context); |
| 2398 | int shaderVersion = vertexShader->getShaderVersion(context); |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2399 | |
| 2400 | if (shaderVersion != 100) |
| 2401 | { |
| 2402 | // Only ESSL 1.0 has restrictions on matching input and output invariance |
| 2403 | return true; |
| 2404 | } |
| 2405 | |
| 2406 | bool glPositionIsInvariant = false; |
| 2407 | bool glPointSizeIsInvariant = false; |
| 2408 | bool glFragCoordIsInvariant = false; |
| 2409 | bool glPointCoordIsInvariant = false; |
| 2410 | |
| 2411 | for (const sh::Varying &varying : vertexVaryings) |
| 2412 | { |
| 2413 | if (!varying.isBuiltIn()) |
| 2414 | { |
| 2415 | continue; |
| 2416 | } |
| 2417 | if (varying.name.compare("gl_Position") == 0) |
| 2418 | { |
| 2419 | glPositionIsInvariant = varying.isInvariant; |
| 2420 | } |
| 2421 | else if (varying.name.compare("gl_PointSize") == 0) |
| 2422 | { |
| 2423 | glPointSizeIsInvariant = varying.isInvariant; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | for (const sh::Varying &varying : fragmentVaryings) |
| 2428 | { |
| 2429 | if (!varying.isBuiltIn()) |
| 2430 | { |
| 2431 | continue; |
| 2432 | } |
| 2433 | if (varying.name.compare("gl_FragCoord") == 0) |
| 2434 | { |
| 2435 | glFragCoordIsInvariant = varying.isInvariant; |
| 2436 | } |
| 2437 | else if (varying.name.compare("gl_PointCoord") == 0) |
| 2438 | { |
| 2439 | glPointCoordIsInvariant = varying.isInvariant; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation, |
| 2444 | // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842. |
| 2445 | // Not requiring invariance to match is supported by: |
| 2446 | // dEQP, WebGL CTS, Nexus 5X GLES |
| 2447 | if (glFragCoordIsInvariant && !glPositionIsInvariant) |
| 2448 | { |
| 2449 | infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is " |
| 2450 | "declared invariant."; |
| 2451 | return false; |
| 2452 | } |
| 2453 | if (glPointCoordIsInvariant && !glPointSizeIsInvariant) |
| 2454 | { |
| 2455 | infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is " |
| 2456 | "declared invariant."; |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | return true; |
| 2461 | } |
| 2462 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2463 | bool Program::linkValidateTransformFeedback(const gl::Context *context, |
| 2464 | InfoLog &infoLog, |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2465 | const Program::MergedVaryings &varyings, |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2466 | const Caps &caps) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2467 | { |
| 2468 | size_t totalComponents = 0; |
| 2469 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2470 | std::set<std::string> uniqueNames; |
| 2471 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2472 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2473 | { |
| 2474 | bool found = false; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2475 | size_t subscript = GL_INVALID_INDEX; |
| 2476 | std::string baseName = ParseResourceName(tfVaryingName, &subscript); |
| 2477 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2478 | for (const auto &ref : varyings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2479 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2480 | const sh::Varying *varying = ref.second.get(); |
| 2481 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2482 | if (baseName == varying->name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2483 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2484 | if (uniqueNames.count(tfVaryingName) > 0) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2485 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2486 | infoLog << "Two transform feedback varyings specify the same output variable (" |
| 2487 | << tfVaryingName << ")."; |
| 2488 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2489 | } |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2490 | if (context->getClientVersion() >= Version(3, 1)) |
| 2491 | { |
| 2492 | if (IncludeSameArrayElement(uniqueNames, tfVaryingName)) |
| 2493 | { |
| 2494 | infoLog |
| 2495 | << "Two transform feedback varyings include the same array element (" |
| 2496 | << tfVaryingName << ")."; |
| 2497 | return false; |
| 2498 | } |
| 2499 | } |
| 2500 | else if (varying->isArray()) |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 2501 | { |
| 2502 | infoLog << "Capture of arrays is undefined and not supported."; |
| 2503 | return false; |
| 2504 | } |
| 2505 | |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2506 | uniqueNames.insert(tfVaryingName); |
| 2507 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2508 | // TODO(jmadill): Investigate implementation limits on D3D11 |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2509 | size_t elementCount = |
| 2510 | ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount() |
| 2511 | : 1); |
| 2512 | size_t componentCount = VariableComponentCount(varying->type) * elementCount; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2513 | if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS && |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2514 | componentCount > caps.maxTransformFeedbackSeparateComponents) |
| 2515 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2516 | infoLog << "Transform feedback varying's " << varying->name << " components (" |
| 2517 | << componentCount << ") exceed the maximum separate components (" |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2518 | << caps.maxTransformFeedbackSeparateComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2519 | return false; |
| 2520 | } |
| 2521 | |
| 2522 | totalComponents += componentCount; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2523 | found = true; |
| 2524 | break; |
| 2525 | } |
| 2526 | } |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2527 | if (context->getClientVersion() < Version(3, 1) && |
| 2528 | tfVaryingName.find('[') != std::string::npos) |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 2529 | { |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 2530 | infoLog << "Capture of array elements is undefined and not supported."; |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 2531 | return false; |
| 2532 | } |
Olli Etuaho | 39e7812 | 2017-08-29 14:34:22 +0300 | [diff] [blame] | 2533 | // All transform feedback varyings are expected to exist since packUserVaryings checks for |
| 2534 | // them. |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2535 | ASSERT(found); |
| 2536 | } |
| 2537 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2538 | if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2539 | totalComponents > caps.maxTransformFeedbackInterleavedComponents) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2540 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2541 | infoLog << "Transform feedback varying total components (" << totalComponents |
| 2542 | << ") exceed the maximum interleaved components (" |
| 2543 | << caps.maxTransformFeedbackInterleavedComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2544 | return false; |
| 2545 | } |
| 2546 | |
| 2547 | return true; |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 2548 | } |
| 2549 | |
Yuly Novikov | caa5cda | 2017-06-15 21:14:03 -0400 | [diff] [blame] | 2550 | bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const |
| 2551 | { |
| 2552 | const std::vector<sh::Uniform> &vertexUniforms = |
| 2553 | mState.mAttachedVertexShader->getUniforms(context); |
| 2554 | const std::vector<sh::Uniform> &fragmentUniforms = |
| 2555 | mState.mAttachedFragmentShader->getUniforms(context); |
| 2556 | const std::vector<sh::Attribute> &attributes = |
| 2557 | mState.mAttachedVertexShader->getActiveAttributes(context); |
| 2558 | for (const auto &attrib : attributes) |
| 2559 | { |
| 2560 | for (const auto &uniform : vertexUniforms) |
| 2561 | { |
| 2562 | if (uniform.name == attrib.name) |
| 2563 | { |
| 2564 | infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name; |
| 2565 | return false; |
| 2566 | } |
| 2567 | } |
| 2568 | for (const auto &uniform : fragmentUniforms) |
| 2569 | { |
| 2570 | if (uniform.name == attrib.name) |
| 2571 | { |
| 2572 | infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name; |
| 2573 | return false; |
| 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | return true; |
| 2578 | } |
| 2579 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2580 | void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2581 | { |
| 2582 | // 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] | 2583 | mState.mLinkedTransformFeedbackVaryings.clear(); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2584 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2585 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2586 | size_t subscript = GL_INVALID_INDEX; |
| 2587 | std::string baseName = ParseResourceName(tfVaryingName, &subscript); |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2588 | for (const auto &ref : varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2589 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2590 | const sh::Varying *varying = ref.second.get(); |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2591 | if (baseName == varying->name) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2592 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2593 | mState.mLinkedTransformFeedbackVaryings.emplace_back( |
| 2594 | *varying, static_cast<GLuint>(subscript)); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2595 | break; |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2601 | Program::MergedVaryings Program::getMergedVaryings(const Context *context) const |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2602 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2603 | MergedVaryings merged; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2604 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2605 | for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context)) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2606 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2607 | merged[varying.name].vertex = &varying; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2608 | } |
| 2609 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2610 | for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context)) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2611 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2612 | merged[varying.name].fragment = &varying; |
| 2613 | } |
| 2614 | |
| 2615 | return merged; |
| 2616 | } |
| 2617 | |
| 2618 | std::vector<PackedVarying> Program::getPackedVaryings( |
| 2619 | const Program::MergedVaryings &mergedVaryings) const |
| 2620 | { |
| 2621 | const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames(); |
| 2622 | std::vector<PackedVarying> packedVaryings; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2623 | std::set<std::string> uniqueFullNames; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2624 | |
| 2625 | for (const auto &ref : mergedVaryings) |
| 2626 | { |
| 2627 | const sh::Varying *input = ref.second.vertex; |
| 2628 | const sh::Varying *output = ref.second.fragment; |
| 2629 | |
| 2630 | // Only pack varyings that have a matched input or output, plus special builtins. |
| 2631 | if ((input && output) || (output && output->isBuiltIn())) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2632 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2633 | // Will get the vertex shader interpolation by default. |
| 2634 | auto interpolation = ref.second.get()->interpolation; |
| 2635 | |
Olli Etuaho | 06a06f5 | 2017-07-12 12:22:15 +0300 | [diff] [blame] | 2636 | // Note that we lose the vertex shader static use information here. The data for the |
| 2637 | // variable is taken from the fragment shader. |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2638 | if (output->isStruct()) |
| 2639 | { |
| 2640 | ASSERT(!output->isArray()); |
| 2641 | for (const auto &field : output->fields) |
| 2642 | { |
| 2643 | ASSERT(!field.isStruct() && !field.isArray()); |
| 2644 | packedVaryings.push_back(PackedVarying(field, interpolation, output->name)); |
| 2645 | } |
| 2646 | } |
| 2647 | else |
| 2648 | { |
| 2649 | packedVaryings.push_back(PackedVarying(*output, interpolation)); |
| 2650 | } |
| 2651 | continue; |
| 2652 | } |
| 2653 | |
| 2654 | // Keep Transform FB varyings in the merged list always. |
| 2655 | if (!input) |
| 2656 | { |
| 2657 | continue; |
| 2658 | } |
| 2659 | |
| 2660 | for (const std::string &tfVarying : tfVaryings) |
| 2661 | { |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2662 | size_t subscript = GL_INVALID_INDEX; |
| 2663 | std::string baseName = ParseResourceName(tfVarying, &subscript); |
| 2664 | if (uniqueFullNames.count(tfVarying) > 0) |
| 2665 | { |
| 2666 | continue; |
| 2667 | } |
| 2668 | if (baseName == input->name) |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2669 | { |
| 2670 | // Transform feedback for varying structs is underspecified. |
| 2671 | // See Khronos bug 9856. |
| 2672 | // TODO(jmadill): Figure out how to be spec-compliant here. |
| 2673 | if (!input->isStruct()) |
| 2674 | { |
| 2675 | packedVaryings.push_back(PackedVarying(*input, input->interpolation)); |
| 2676 | packedVaryings.back().vertexOnly = true; |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2677 | packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript); |
| 2678 | uniqueFullNames.insert(tfVarying); |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2679 | } |
jchen10 | a9042d3 | 2017-03-17 08:50:45 +0800 | [diff] [blame] | 2680 | if (subscript == GL_INVALID_INDEX) |
| 2681 | { |
| 2682 | break; |
| 2683 | } |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2684 | } |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2685 | } |
| 2686 | } |
| 2687 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2688 | std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying); |
| 2689 | |
| 2690 | return packedVaryings; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2691 | } |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2692 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2693 | void Program::linkOutputVariables(const Context *context) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2694 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2695 | Shader *fragmentShader = mState.mAttachedFragmentShader; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2696 | ASSERT(fragmentShader != nullptr); |
| 2697 | |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2698 | ASSERT(mState.mOutputVariableTypes.empty()); |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 2699 | ASSERT(mState.mActiveOutputVariables.none()); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2700 | |
| 2701 | // Gather output variable types |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2702 | for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context)) |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2703 | { |
| 2704 | if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" && |
| 2705 | outputVariable.name != "gl_FragData") |
| 2706 | { |
| 2707 | continue; |
| 2708 | } |
| 2709 | |
| 2710 | unsigned int baseLocation = |
| 2711 | (outputVariable.location == -1 ? 0u |
| 2712 | : static_cast<unsigned int>(outputVariable.location)); |
| 2713 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount(); |
| 2714 | elementIndex++) |
| 2715 | { |
| 2716 | const unsigned int location = baseLocation + elementIndex; |
| 2717 | if (location >= mState.mOutputVariableTypes.size()) |
| 2718 | { |
| 2719 | mState.mOutputVariableTypes.resize(location + 1, GL_NONE); |
| 2720 | } |
Corentin Wallez | e755774 | 2017-06-01 13:09:57 -0400 | [diff] [blame] | 2721 | ASSERT(location < mState.mActiveOutputVariables.size()); |
| 2722 | mState.mActiveOutputVariables.set(location); |
Geoff Lang | e0cff19 | 2017-05-30 13:04:56 -0400 | [diff] [blame] | 2723 | mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type); |
| 2724 | } |
| 2725 | } |
| 2726 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2727 | // Skip this step for GLES2 shaders. |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2728 | if (fragmentShader->getShaderVersion(context) == 100) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2729 | return; |
| 2730 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 2731 | mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2732 | // TODO(jmadill): any caps validation here? |
| 2733 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 2734 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size(); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2735 | outputVariableIndex++) |
| 2736 | { |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 2737 | const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex]; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2738 | |
| 2739 | // Don't store outputs for gl_FragDepth, gl_FragColor, etc. |
| 2740 | if (outputVariable.isBuiltIn()) |
| 2741 | continue; |
| 2742 | |
| 2743 | // Since multiple output locations must be specified, use 0 for non-specified locations. |
| 2744 | int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location); |
| 2745 | |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2746 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount(); |
| 2747 | elementIndex++) |
| 2748 | { |
| 2749 | const int location = baseLocation + elementIndex; |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 2750 | ASSERT(mState.mOutputLocations.count(location) == 0); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2751 | unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX; |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 2752 | mState.mOutputLocations[location] = VariableLocation(element, outputVariableIndex); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2756 | |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 2757 | void Program::setUniformValuesFromBindingQualifiers() |
| 2758 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 2759 | for (unsigned int samplerIndex : mState.mSamplerUniformRange) |
Olli Etuaho | 48fed63 | 2017-03-16 12:05:30 +0000 | [diff] [blame] | 2760 | { |
| 2761 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
| 2762 | if (samplerUniform.binding != -1) |
| 2763 | { |
| 2764 | GLint location = mState.getUniformLocation(samplerUniform.name); |
| 2765 | ASSERT(location != -1); |
| 2766 | std::vector<GLint> boundTextureUnits; |
| 2767 | for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount(); |
| 2768 | ++elementIndex) |
| 2769 | { |
| 2770 | boundTextureUnits.push_back(samplerUniform.binding + elementIndex); |
| 2771 | } |
| 2772 | setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()), |
| 2773 | boundTextureUnits.data()); |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2778 | void Program::gatherAtomicCounterBuffers() |
| 2779 | { |
| 2780 | // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each |
| 2781 | // counter. |
| 2782 | // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer. |
| 2783 | } |
| 2784 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2785 | void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2786 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2787 | for (const sh::InterfaceBlock &computeBlock : computeBlocks) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2788 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2789 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2790 | // Only 'packed' blocks are allowed to be considered inactive. |
| 2791 | if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED) |
| 2792 | continue; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2793 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2794 | defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2795 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2796 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2797 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2798 | void Program::gatherVertexAndFragmentBlockInfo( |
| 2799 | const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks, |
| 2800 | const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks) |
| 2801 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2802 | std::set<std::string> visitedList; |
| 2803 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2804 | for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2805 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2806 | // Only 'packed' blocks are allowed to be considered inactive. |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2807 | if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED) |
| 2808 | continue; |
| 2809 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2810 | defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2811 | visitedList.insert(vertexBlock.name); |
| 2812 | } |
| 2813 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2814 | for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2815 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2816 | // Only 'packed' blocks are allowed to be considered inactive. |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2817 | if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED) |
| 2818 | continue; |
| 2819 | |
| 2820 | if (visitedList.count(fragmentBlock.name) > 0) |
| 2821 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2822 | if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2823 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2824 | for (InterfaceBlock &block : mState.mUniformBlocks) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2825 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2826 | if (block.name == fragmentBlock.name) |
| 2827 | { |
| 2828 | block.fragmentStaticUse = fragmentBlock.staticUse; |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | else |
| 2833 | { |
| 2834 | ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER); |
| 2835 | for (InterfaceBlock &block : mState.mShaderStorageBlocks) |
| 2836 | { |
| 2837 | if (block.name == fragmentBlock.name) |
| 2838 | { |
| 2839 | block.fragmentStaticUse = fragmentBlock.staticUse; |
| 2840 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | continue; |
| 2845 | } |
| 2846 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2847 | defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2848 | visitedList.insert(fragmentBlock.name); |
| 2849 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | void Program::gatherInterfaceBlockInfo(const Context *context) |
| 2853 | { |
| 2854 | ASSERT(mState.mUniformBlocks.empty()); |
| 2855 | ASSERT(mState.mShaderStorageBlocks.empty()); |
| 2856 | |
| 2857 | if (mState.mAttachedComputeShader) |
| 2858 | { |
| 2859 | Shader *computeShader = mState.getAttachedComputeShader(); |
| 2860 | |
| 2861 | gatherComputeBlockInfo(computeShader->getUniformBlocks(context)); |
| 2862 | gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context)); |
| 2863 | return; |
| 2864 | } |
| 2865 | |
| 2866 | Shader *vertexShader = mState.getAttachedVertexShader(); |
| 2867 | Shader *fragmentShader = mState.getAttachedFragmentShader(); |
| 2868 | |
| 2869 | gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context), |
| 2870 | fragmentShader->getUniformBlocks(context)); |
| 2871 | if (context->getClientVersion() >= Version(3, 1)) |
| 2872 | { |
| 2873 | gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context), |
| 2874 | fragmentShader->getShaderStorageBlocks(context)); |
| 2875 | } |
| 2876 | |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2877 | // Set initial bindings from shader. |
| 2878 | for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++) |
| 2879 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2880 | InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex]; |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2881 | bindUniformBlock(blockIndex, uniformBlock.binding); |
| 2882 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2883 | } |
| 2884 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2885 | template <typename VarT> |
| 2886 | void Program::defineUniformBlockMembers(const std::vector<VarT> &fields, |
| 2887 | const std::string &prefix, |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 2888 | const std::string &mappedPrefix, |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2889 | int blockIndex) |
| 2890 | { |
| 2891 | for (const VarT &field : fields) |
| 2892 | { |
| 2893 | const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name); |
| 2894 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 2895 | const std::string &fullMappedName = |
| 2896 | (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName); |
| 2897 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2898 | if (field.isStruct()) |
| 2899 | { |
| 2900 | for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++) |
| 2901 | { |
| 2902 | const std::string uniformElementName = |
| 2903 | fullName + (field.isArray() ? ArrayString(arrayElement) : ""); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 2904 | const std::string uniformElementMappedName = |
| 2905 | fullMappedName + (field.isArray() ? ArrayString(arrayElement) : ""); |
| 2906 | defineUniformBlockMembers(field.fields, uniformElementName, |
| 2907 | uniformElementMappedName, blockIndex); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | else |
| 2911 | { |
| 2912 | // If getBlockMemberInfo returns false, the uniform is optimized out. |
| 2913 | sh::BlockMemberInfo memberInfo; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 2914 | if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo)) |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2915 | { |
| 2916 | continue; |
| 2917 | } |
| 2918 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2919 | LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1, |
jchen10 | eaef1e5 | 2017-06-13 10:44:11 +0800 | [diff] [blame] | 2920 | -1, blockIndex, memberInfo); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 2921 | newUniform.mappedName = fullMappedName; |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2922 | |
| 2923 | // Since block uniforms have no location, we don't need to store them in the uniform |
| 2924 | // locations list. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2925 | mState.mUniforms.push_back(newUniform); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2926 | } |
| 2927 | } |
| 2928 | } |
| 2929 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2930 | void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2931 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2932 | size_t blockSize = 0; |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2933 | std::vector<unsigned int> blockIndexes; |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2934 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2935 | if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2936 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2937 | int blockIndex = static_cast<int>(mState.mUniformBlocks.size()); |
| 2938 | // Track the first and last uniform index to determine the range of active uniforms in the |
| 2939 | // block. |
| 2940 | size_t firstBlockUniformIndex = mState.mUniforms.size(); |
| 2941 | defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), |
| 2942 | interfaceBlock.fieldMappedPrefix(), blockIndex); |
| 2943 | size_t lastBlockUniformIndex = mState.mUniforms.size(); |
| 2944 | |
| 2945 | for (size_t blockUniformIndex = firstBlockUniformIndex; |
| 2946 | blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex) |
| 2947 | { |
| 2948 | blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex)); |
| 2949 | } |
| 2950 | } |
| 2951 | else |
| 2952 | { |
| 2953 | // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index. |
| 2954 | ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2955 | } |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2956 | // ESSL 3.10 section 4.4.4 page 58: |
| 2957 | // Any uniform or shader storage block declared without a binding qualifier is initially |
| 2958 | // assigned to block binding point zero. |
| 2959 | int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2960 | if (interfaceBlock.arraySize > 0) |
| 2961 | { |
| 2962 | for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement) |
| 2963 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2964 | // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE |
| 2965 | // of UniformBlock and ShaderStorageBlock. |
| 2966 | if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM) |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2967 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2968 | // Don't define this block at all if it's not active in the implementation. |
| 2969 | if (!mProgram->getUniformBlockSize( |
| 2970 | interfaceBlock.name + ArrayString(arrayElement), |
| 2971 | interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize)) |
| 2972 | { |
| 2973 | continue; |
| 2974 | } |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 2975 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 2976 | |
| 2977 | InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement, |
| 2978 | blockBinding + arrayElement); |
| 2979 | block.memberIndexes = blockIndexes; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2980 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2981 | switch (shaderType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2982 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2983 | case GL_VERTEX_SHADER: |
| 2984 | { |
| 2985 | block.vertexStaticUse = interfaceBlock.staticUse; |
| 2986 | break; |
| 2987 | } |
| 2988 | case GL_FRAGMENT_SHADER: |
| 2989 | { |
| 2990 | block.fragmentStaticUse = interfaceBlock.staticUse; |
| 2991 | break; |
| 2992 | } |
| 2993 | case GL_COMPUTE_SHADER: |
| 2994 | { |
| 2995 | block.computeStaticUse = interfaceBlock.staticUse; |
| 2996 | break; |
| 2997 | } |
| 2998 | default: |
| 2999 | UNREACHABLE(); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3000 | } |
| 3001 | |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3002 | // Since all block elements in an array share the same active interface blocks, they |
| 3003 | // will all be active once any block member is used. So, since interfaceBlock.name[0] |
| 3004 | // was active, here we will add every block element in the array. |
Qin Jiajia | 0350a64 | 2016-11-01 17:01:51 +0800 | [diff] [blame] | 3005 | block.dataSize = static_cast<unsigned int>(blockSize); |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3006 | if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM) |
| 3007 | { |
| 3008 | mState.mUniformBlocks.push_back(block); |
| 3009 | } |
| 3010 | else |
| 3011 | { |
| 3012 | ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER); |
| 3013 | mState.mShaderStorageBlocks.push_back(block); |
| 3014 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | else |
| 3018 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3019 | // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE |
| 3020 | // of UniformBlock and ShaderStorageBlock. |
| 3021 | if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM) |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 3022 | { |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3023 | if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName, |
| 3024 | &blockSize)) |
| 3025 | { |
| 3026 | return; |
| 3027 | } |
jchen10 | af713a2 | 2017-04-19 09:10:56 +0800 | [diff] [blame] | 3028 | } |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3029 | |
| 3030 | InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0, |
| 3031 | blockBinding); |
| 3032 | block.memberIndexes = blockIndexes; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3033 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 3034 | switch (shaderType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3035 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 3036 | case GL_VERTEX_SHADER: |
| 3037 | { |
| 3038 | block.vertexStaticUse = interfaceBlock.staticUse; |
| 3039 | break; |
| 3040 | } |
| 3041 | case GL_FRAGMENT_SHADER: |
| 3042 | { |
| 3043 | block.fragmentStaticUse = interfaceBlock.staticUse; |
| 3044 | break; |
| 3045 | } |
| 3046 | case GL_COMPUTE_SHADER: |
| 3047 | { |
| 3048 | block.computeStaticUse = interfaceBlock.staticUse; |
| 3049 | break; |
| 3050 | } |
| 3051 | default: |
| 3052 | UNREACHABLE(); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3053 | } |
| 3054 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 3055 | block.dataSize = static_cast<unsigned int>(blockSize); |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 3056 | if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM) |
| 3057 | { |
| 3058 | mState.mUniformBlocks.push_back(block); |
| 3059 | } |
| 3060 | else |
| 3061 | { |
| 3062 | ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER); |
| 3063 | mState.mShaderStorageBlocks.push_back(block); |
| 3064 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3068 | void Program::updateSamplerUniform(const VariableLocation &locationInfo, |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3069 | GLsizei clampedCount, |
| 3070 | const GLint *v) |
| 3071 | { |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 3072 | ASSERT(mState.isSamplerUniformIndex(locationInfo.index)); |
| 3073 | GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index); |
| 3074 | std::vector<GLuint> *boundTextureUnits = |
| 3075 | &mState.mSamplerBindings[samplerIndex].boundTextureUnits; |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3076 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 3077 | std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element); |
Jamie Madill | d68248b | 2017-09-11 14:34:14 -0400 | [diff] [blame] | 3078 | |
| 3079 | // Invalidate the validation cache. |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 3080 | mCachedValidateSamplersResult.reset(); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | template <typename T> |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3084 | GLsizei Program::clampUniformCount(const VariableLocation &locationInfo, |
| 3085 | GLsizei count, |
| 3086 | int vectorSize, |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 3087 | const T *v) |
| 3088 | { |
Jamie Madill | 134f93d | 2017-08-31 17:11:00 -0400 | [diff] [blame] | 3089 | if (count == 1) |
| 3090 | return 1; |
| 3091 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3092 | const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3093 | |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 3094 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 3095 | // 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] | 3096 | unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element; |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3097 | GLsizei maxElementCount = |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3098 | static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents()); |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3099 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3100 | if (count * vectorSize > maxElementCount) |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3101 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3102 | return maxElementCount / vectorSize; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3103 | } |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 3104 | |
| 3105 | return count; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | template <size_t cols, size_t rows, typename T> |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3109 | GLsizei Program::clampMatrixUniformCount(GLint location, |
| 3110 | GLsizei count, |
| 3111 | GLboolean transpose, |
| 3112 | const T *v) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3113 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3114 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 3115 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3116 | if (!transpose) |
| 3117 | { |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3118 | return clampUniformCount(locationInfo, count, cols * rows, v); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3119 | } |
| 3120 | |
Jamie Madill | be5e2ec | 2017-08-31 13:28:28 -0400 | [diff] [blame] | 3121 | const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index]; |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 3122 | |
| 3123 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 3124 | // 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] | 3125 | unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element; |
| 3126 | return std::min(count, static_cast<GLsizei>(remainingElements)); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3127 | } |
| 3128 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3129 | // Driver differences mean that doing the uniform value cast ourselves gives consistent results. |
| 3130 | // 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] | 3131 | template <typename DestT> |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3132 | void Program::getUniformInternal(const Context *context, |
| 3133 | DestT *dataOut, |
| 3134 | GLint location, |
| 3135 | GLenum nativeType, |
| 3136 | int components) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3137 | { |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3138 | switch (nativeType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3139 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3140 | case GL_BOOL: |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3141 | { |
| 3142 | GLint tempValue[16] = {0}; |
| 3143 | mProgram->getUniformiv(context, location, tempValue); |
| 3144 | UniformStateQueryCastLoop<GLboolean>( |
| 3145 | dataOut, reinterpret_cast<const uint8_t *>(tempValue), components); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3146 | break; |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3147 | } |
| 3148 | case GL_INT: |
| 3149 | { |
| 3150 | GLint tempValue[16] = {0}; |
| 3151 | mProgram->getUniformiv(context, location, tempValue); |
| 3152 | UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue), |
| 3153 | components); |
| 3154 | break; |
| 3155 | } |
| 3156 | case GL_UNSIGNED_INT: |
| 3157 | { |
| 3158 | GLuint tempValue[16] = {0}; |
| 3159 | mProgram->getUniformuiv(context, location, tempValue); |
| 3160 | UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue), |
| 3161 | components); |
| 3162 | break; |
| 3163 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3164 | case GL_FLOAT: |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3165 | { |
| 3166 | GLfloat tempValue[16] = {0}; |
| 3167 | mProgram->getUniformfv(context, location, tempValue); |
| 3168 | UniformStateQueryCastLoop<GLfloat>( |
| 3169 | dataOut, reinterpret_cast<const uint8_t *>(tempValue), components); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3170 | break; |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3171 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3172 | default: |
| 3173 | UNREACHABLE(); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 3174 | break; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 3175 | } |
| 3176 | } |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 3177 | |
| 3178 | bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const |
| 3179 | { |
| 3180 | // Must be called after samplers are validated. |
| 3181 | ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value()); |
| 3182 | |
| 3183 | for (const auto &binding : mState.mSamplerBindings) |
| 3184 | { |
| 3185 | GLenum textureType = binding.textureType; |
| 3186 | for (const auto &unit : binding.boundTextureUnits) |
| 3187 | { |
| 3188 | GLenum programTextureID = state.getSamplerTextureId(unit, textureType); |
| 3189 | if (programTextureID == textureID) |
| 3190 | { |
| 3191 | // TODO(jmadill): Check for appropriate overlap. |
| 3192 | return true; |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | return false; |
| 3198 | } |
| 3199 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 3200 | } // namespace gl |