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