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