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