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