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