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