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