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