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 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1143 | if (attribute.name == name && attribute.staticUse) |
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 | |
| 1177 | size_t attributeIndex = 0; |
| 1178 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1179 | for (const sh::Attribute &attribute : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1180 | { |
| 1181 | // Skip over inactive attributes |
| 1182 | if (attribute.staticUse) |
| 1183 | { |
| 1184 | if (static_cast<size_t>(index) == attributeIndex) |
| 1185 | { |
| 1186 | break; |
| 1187 | } |
| 1188 | attributeIndex++; |
| 1189 | } |
| 1190 | } |
| 1191 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1192 | ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size()); |
| 1193 | const sh::Attribute &attrib = mState.mAttributes[attributeIndex]; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1194 | |
| 1195 | if (bufsize > 0) |
| 1196 | { |
| 1197 | const char *string = attrib.name.c_str(); |
| 1198 | |
| 1199 | strncpy(name, string, bufsize); |
| 1200 | name[bufsize - 1] = '\0'; |
| 1201 | |
| 1202 | if (length) |
| 1203 | { |
| 1204 | *length = static_cast<GLsizei>(strlen(name)); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | // Always a single 'type' instance |
| 1209 | *size = 1; |
| 1210 | *type = attrib.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1213 | GLint Program::getActiveAttributeCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1214 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1215 | if (!mLinked) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1216 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | GLint count = 0; |
| 1221 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1222 | for (const sh::Attribute &attrib : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1223 | { |
| 1224 | count += (attrib.staticUse ? 1 : 0); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1225 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1226 | |
| 1227 | return count; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1230 | GLint Program::getActiveAttributeMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1231 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1232 | if (!mLinked) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1233 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | size_t maxLength = 0; |
| 1238 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1239 | for (const sh::Attribute &attrib : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1240 | { |
| 1241 | if (attrib.staticUse) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1242 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1243 | maxLength = std::max(attrib.name.length() + 1, maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1244 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1245 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1246 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1247 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1248 | } |
| 1249 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1250 | GLint Program::getFragDataLocation(const std::string &name) const |
| 1251 | { |
| 1252 | std::string baseName(name); |
| 1253 | unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1254 | for (auto outputPair : mState.mOutputVariables) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1255 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 1256 | const VariableLocation &outputVariable = outputPair.second; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1257 | if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element)) |
| 1258 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 1259 | return static_cast<GLint>(outputPair.first); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1260 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1261 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1262 | return -1; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1265 | void Program::getActiveUniform(GLuint index, |
| 1266 | GLsizei bufsize, |
| 1267 | GLsizei *length, |
| 1268 | GLint *size, |
| 1269 | GLenum *type, |
| 1270 | GLchar *name) const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1271 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1272 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1273 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1274 | // index must be smaller than getActiveUniformCount() |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1275 | ASSERT(index < mState.mUniforms.size()); |
| 1276 | const LinkedUniform &uniform = mState.mUniforms[index]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1277 | |
| 1278 | if (bufsize > 0) |
| 1279 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1280 | std::string string = uniform.name; |
| 1281 | if (uniform.isArray()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1282 | { |
| 1283 | string += "[0]"; |
| 1284 | } |
| 1285 | |
| 1286 | strncpy(name, string.c_str(), bufsize); |
| 1287 | name[bufsize - 1] = '\0'; |
| 1288 | |
| 1289 | if (length) |
| 1290 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1291 | *length = static_cast<GLsizei>(strlen(name)); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1295 | *size = uniform.elementCount(); |
| 1296 | *type = uniform.type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1297 | } |
| 1298 | else |
| 1299 | { |
| 1300 | if (bufsize > 0) |
| 1301 | { |
| 1302 | name[0] = '\0'; |
| 1303 | } |
| 1304 | |
| 1305 | if (length) |
| 1306 | { |
| 1307 | *length = 0; |
| 1308 | } |
| 1309 | |
| 1310 | *size = 0; |
| 1311 | *type = GL_NONE; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1315 | GLint Program::getActiveUniformCount() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1316 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1317 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1318 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1319 | return static_cast<GLint>(mState.mUniforms.size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1320 | } |
| 1321 | else |
| 1322 | { |
| 1323 | return 0; |
| 1324 | } |
| 1325 | } |
| 1326 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1327 | GLint Program::getActiveUniformMaxLength() const |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1328 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1329 | size_t maxLength = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1330 | |
| 1331 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1332 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1333 | for (const LinkedUniform &uniform : mState.mUniforms) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1334 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1335 | if (!uniform.name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1336 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1337 | size_t length = uniform.name.length() + 1u; |
| 1338 | if (uniform.isArray()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1339 | { |
| 1340 | length += 3; // Counting in "[0]". |
| 1341 | } |
| 1342 | maxLength = std::max(length, maxLength); |
| 1343 | } |
| 1344 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1345 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1346 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1347 | return static_cast<GLint>(maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | GLint Program::getActiveUniformi(GLuint index, GLenum pname) const |
| 1351 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1352 | ASSERT(static_cast<size_t>(index) < mState.mUniforms.size()); |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1353 | const LinkedUniform &uniform = mState.mUniforms[index]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1354 | switch (pname) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1355 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1356 | case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type); |
| 1357 | case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount()); |
| 1358 | case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0)); |
| 1359 | case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex; |
| 1360 | case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset; |
| 1361 | case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride; |
| 1362 | case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride; |
| 1363 | case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix); |
| 1364 | default: |
| 1365 | UNREACHABLE(); |
| 1366 | break; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1367 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | bool Program::isValidUniformLocation(GLint location) const |
| 1372 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1373 | ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size())); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1374 | return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() && |
| 1375 | mState.mUniformLocations[static_cast<size_t>(location)].used); |
Geoff Lang | d860552 | 2016-04-13 10:19:12 -0400 | [diff] [blame] | 1376 | } |
| 1377 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1378 | const LinkedUniform &Program::getUniformByLocation(GLint location) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1379 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1380 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1381 | return mState.mUniforms[mState.getUniformIndexFromLocation(location)]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1382 | } |
| 1383 | |
Jamie Madill | ac4e9c3 | 2017-01-13 14:07:12 -0500 | [diff] [blame] | 1384 | const VariableLocation &Program::getUniformLocation(GLint location) const |
| 1385 | { |
| 1386 | ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size()); |
| 1387 | return mState.mUniformLocations[location]; |
| 1388 | } |
| 1389 | |
| 1390 | const std::vector<VariableLocation> &Program::getUniformLocations() const |
| 1391 | { |
| 1392 | return mState.mUniformLocations; |
| 1393 | } |
| 1394 | |
| 1395 | const LinkedUniform &Program::getUniformByIndex(GLuint index) const |
| 1396 | { |
| 1397 | ASSERT(index < static_cast<size_t>(mState.mUniforms.size())); |
| 1398 | return mState.mUniforms[index]; |
| 1399 | } |
| 1400 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1401 | GLint Program::getUniformLocation(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1402 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1403 | return mState.getUniformLocation(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1404 | } |
| 1405 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1406 | GLuint Program::getUniformIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1407 | { |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1408 | return mState.getUniformIndexFromName(name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 1412 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1413 | GLsizei clampedCount = setUniformInternal(location, count, 1, v); |
| 1414 | mProgram->setUniform1fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 1418 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1419 | GLsizei clampedCount = setUniformInternal(location, count, 2, v); |
| 1420 | mProgram->setUniform2fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 1424 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1425 | GLsizei clampedCount = setUniformInternal(location, count, 3, v); |
| 1426 | mProgram->setUniform3fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 1430 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1431 | GLsizei clampedCount = setUniformInternal(location, count, 4, v); |
| 1432 | mProgram->setUniform4fv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 1436 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1437 | GLsizei clampedCount = setUniformInternal(location, count, 1, v); |
| 1438 | mProgram->setUniform1iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 1442 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1443 | GLsizei clampedCount = setUniformInternal(location, count, 2, v); |
| 1444 | mProgram->setUniform2iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 1448 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1449 | GLsizei clampedCount = setUniformInternal(location, count, 3, v); |
| 1450 | mProgram->setUniform3iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 1454 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1455 | GLsizei clampedCount = setUniformInternal(location, count, 4, v); |
| 1456 | mProgram->setUniform4iv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 1460 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1461 | GLsizei clampedCount = setUniformInternal(location, count, 1, v); |
| 1462 | mProgram->setUniform1uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 1466 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1467 | GLsizei clampedCount = setUniformInternal(location, count, 2, v); |
| 1468 | mProgram->setUniform2uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 1472 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1473 | GLsizei clampedCount = setUniformInternal(location, count, 3, v); |
| 1474 | mProgram->setUniform3uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 1478 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1479 | GLsizei clampedCount = setUniformInternal(location, count, 4, v); |
| 1480 | mProgram->setUniform4uiv(location, clampedCount, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1484 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1485 | GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v); |
| 1486 | mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1490 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1491 | GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v); |
| 1492 | mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1496 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1497 | GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v); |
| 1498 | mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1502 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1503 | GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v); |
| 1504 | mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1508 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1509 | GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v); |
| 1510 | mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1514 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1515 | GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v); |
| 1516 | mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1520 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1521 | GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v); |
| 1522 | mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1526 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1527 | GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v); |
| 1528 | mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 1532 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 1533 | GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v); |
| 1534 | mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1535 | } |
| 1536 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1537 | void Program::getUniformfv(GLint location, GLfloat *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1538 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1539 | getUniformInternal(location, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1540 | } |
| 1541 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1542 | void Program::getUniformiv(GLint location, GLint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1543 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1544 | getUniformInternal(location, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1545 | } |
| 1546 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1547 | void Program::getUniformuiv(GLint location, GLuint *v) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1548 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1549 | getUniformInternal(location, v); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1550 | } |
| 1551 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1552 | void Program::flagForDeletion() |
| 1553 | { |
| 1554 | mDeleteStatus = true; |
| 1555 | } |
| 1556 | |
| 1557 | bool Program::isFlaggedForDeletion() const |
| 1558 | { |
| 1559 | return mDeleteStatus; |
| 1560 | } |
daniel@transgaming.com | 86a7a13 | 2010-04-29 03:32:32 +0000 | [diff] [blame] | 1561 | |
Brandon Jones | 43a53e2 | 2014-08-28 16:23:22 -0700 | [diff] [blame] | 1562 | void Program::validate(const Caps &caps) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1563 | { |
| 1564 | mInfoLog.reset(); |
| 1565 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1566 | if (mLinked) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1567 | { |
Jamie Madill | 36cfd6a | 2015-08-18 10:46:20 -0400 | [diff] [blame] | 1568 | mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1569 | } |
| 1570 | else |
| 1571 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1572 | mInfoLog << "Program has not been successfully linked."; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1576 | bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps) |
| 1577 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1578 | // Skip cache if we're using an infolog, so we get the full error. |
| 1579 | // Also skip the cache if the sample mapping has changed, or if we haven't ever validated. |
| 1580 | if (infoLog == nullptr && mCachedValidateSamplersResult.valid()) |
| 1581 | { |
| 1582 | return mCachedValidateSamplersResult.value(); |
| 1583 | } |
| 1584 | |
| 1585 | if (mTextureUnitTypesCache.empty()) |
| 1586 | { |
| 1587 | mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE); |
| 1588 | } |
| 1589 | else |
| 1590 | { |
| 1591 | std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE); |
| 1592 | } |
| 1593 | |
| 1594 | // if any two active samplers in a program are of different types, but refer to the same |
| 1595 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 1596 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1597 | for (const auto &samplerBinding : mState.mSamplerBindings) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1598 | { |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1599 | GLenum textureType = samplerBinding.textureType; |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1600 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 1601 | for (GLuint textureUnit : samplerBinding.boundTextureUnits) |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1602 | { |
Jamie Madill | 3d3d2f2 | 2015-09-23 16:47:51 -0400 | [diff] [blame] | 1603 | if (textureUnit >= caps.maxCombinedTextureImageUnits) |
| 1604 | { |
| 1605 | if (infoLog) |
| 1606 | { |
| 1607 | (*infoLog) << "Sampler uniform (" << textureUnit |
| 1608 | << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (" |
| 1609 | << caps.maxCombinedTextureImageUnits << ")"; |
| 1610 | } |
| 1611 | |
| 1612 | mCachedValidateSamplersResult = false; |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
| 1616 | if (mTextureUnitTypesCache[textureUnit] != GL_NONE) |
| 1617 | { |
| 1618 | if (textureType != mTextureUnitTypesCache[textureUnit]) |
| 1619 | { |
| 1620 | if (infoLog) |
| 1621 | { |
| 1622 | (*infoLog) << "Samplers of conflicting types refer to the same texture " |
| 1623 | "image unit (" |
| 1624 | << textureUnit << ")."; |
| 1625 | } |
| 1626 | |
| 1627 | mCachedValidateSamplersResult = false; |
| 1628 | return false; |
| 1629 | } |
| 1630 | } |
| 1631 | else |
| 1632 | { |
| 1633 | mTextureUnitTypesCache[textureUnit] = textureType; |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | mCachedValidateSamplersResult = true; |
| 1639 | return true; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1640 | } |
| 1641 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1642 | bool Program::isValidated() const |
| 1643 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1644 | return mValidated; |
| 1645 | } |
| 1646 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1647 | GLuint Program::getActiveUniformBlockCount() const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1648 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1649 | return static_cast<GLuint>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const |
| 1653 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1654 | ASSERT( |
| 1655 | uniformBlockIndex < |
| 1656 | mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount() |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1657 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1658 | const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1659 | |
| 1660 | if (bufSize > 0) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1661 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1662 | std::string string = uniformBlock.name; |
| 1663 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1664 | if (uniformBlock.isArray) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1665 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1666 | string += ArrayString(uniformBlock.arrayElement); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | strncpy(uniformBlockName, string.c_str(), bufSize); |
| 1670 | uniformBlockName[bufSize - 1] = '\0'; |
| 1671 | |
| 1672 | if (length) |
| 1673 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1674 | *length = static_cast<GLsizei>(strlen(uniformBlockName)); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1675 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1679 | GLint Program::getActiveUniformBlockMaxLength() const |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1680 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1681 | int maxLength = 0; |
| 1682 | |
| 1683 | if (mLinked) |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1684 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1685 | unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1686 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++) |
| 1687 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1688 | const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1689 | if (!uniformBlock.name.empty()) |
| 1690 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1691 | const int length = static_cast<int>(uniformBlock.name.length()) + 1; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1692 | |
| 1693 | // Counting in "[0]". |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1694 | const int arrayLength = (uniformBlock.isArray ? 3 : 0); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1695 | |
| 1696 | maxLength = std::max(length + arrayLength, maxLength); |
| 1697 | } |
| 1698 | } |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1699 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1700 | |
| 1701 | return maxLength; |
| 1702 | } |
| 1703 | |
Geoff Lang | e1a2775 | 2015-10-05 13:16:04 -0400 | [diff] [blame] | 1704 | GLuint Program::getUniformBlockIndex(const std::string &name) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1705 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1706 | size_t subscript = GL_INVALID_INDEX; |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1707 | std::string baseName = ParseUniformName(name, &subscript); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1708 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1709 | unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size()); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1710 | for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++) |
| 1711 | { |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 1712 | const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1713 | if (uniformBlock.name == baseName) |
| 1714 | { |
| 1715 | const bool arrayElementZero = |
| 1716 | (subscript == GL_INVALID_INDEX && |
| 1717 | (!uniformBlock.isArray || uniformBlock.arrayElement == 0)); |
| 1718 | if (subscript == uniformBlock.arrayElement || arrayElementZero) |
| 1719 | { |
| 1720 | return blockIndex; |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | return GL_INVALID_INDEX; |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1728 | const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1729 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1730 | ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size())); |
| 1731 | return mState.mUniformBlocks[index]; |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1734 | void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 1735 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1736 | mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding; |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 1737 | mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 1738 | mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const |
| 1742 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1743 | return mState.getUniformBlockBinding(uniformBlockIndex); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | void Program::resetUniformBlockBindings() |
| 1747 | { |
| 1748 | for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++) |
| 1749 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1750 | mState.mUniformBlockBindings[blockId] = 0; |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1751 | } |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1752 | mState.mActiveUniformBlockBindings.reset(); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1755 | void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode) |
| 1756 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1757 | mState.mTransformFeedbackVaryingNames.resize(count); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1758 | for (GLsizei i = 0; i < count; i++) |
| 1759 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1760 | mState.mTransformFeedbackVaryingNames[i] = varyings[i]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1761 | } |
| 1762 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1763 | mState.mTransformFeedbackBufferMode = bufferMode; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const |
| 1767 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1768 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1769 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1770 | ASSERT(index < mState.mTransformFeedbackVaryingVars.size()); |
| 1771 | const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1772 | GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length())); |
| 1773 | if (length) |
| 1774 | { |
| 1775 | *length = lastNameIdx; |
| 1776 | } |
| 1777 | if (size) |
| 1778 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 1779 | *size = varying.elementCount(); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1780 | } |
| 1781 | if (type) |
| 1782 | { |
| 1783 | *type = varying.type; |
| 1784 | } |
| 1785 | if (name) |
| 1786 | { |
| 1787 | memcpy(name, varying.name.c_str(), lastNameIdx); |
| 1788 | name[lastNameIdx] = '\0'; |
| 1789 | } |
| 1790 | } |
| 1791 | } |
| 1792 | |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1793 | GLsizei Program::getTransformFeedbackVaryingCount() const |
| 1794 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1795 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1796 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1797 | return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size()); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1798 | } |
| 1799 | else |
| 1800 | { |
| 1801 | return 0; |
| 1802 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | GLsizei Program::getTransformFeedbackVaryingMaxLength() const |
| 1806 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1807 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1808 | { |
| 1809 | GLsizei maxSize = 0; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1810 | for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1811 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1812 | maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1)); |
| 1813 | } |
| 1814 | |
| 1815 | return maxSize; |
| 1816 | } |
| 1817 | else |
| 1818 | { |
| 1819 | return 0; |
| 1820 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | GLenum Program::getTransformFeedbackBufferMode() const |
| 1824 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1825 | return mState.mTransformFeedbackBufferMode; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1826 | } |
| 1827 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1828 | bool Program::linkVaryings(InfoLog &infoLog) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1829 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1830 | const Shader *vertexShader = mState.mAttachedVertexShader; |
| 1831 | const Shader *fragmentShader = mState.mAttachedFragmentShader; |
| 1832 | |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 1833 | ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion()); |
| 1834 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1835 | const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(); |
| 1836 | const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1837 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 1838 | std::map<GLuint, std::string> staticFragmentInputLocations; |
| 1839 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1840 | for (const sh::Varying &output : fragmentVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1841 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1842 | bool matched = false; |
| 1843 | |
| 1844 | // Built-in varyings obey special rules |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1845 | if (output.isBuiltIn()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1846 | { |
| 1847 | continue; |
| 1848 | } |
| 1849 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1850 | for (const sh::Varying &input : vertexVaryings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1851 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1852 | if (output.name == input.name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1853 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1854 | ASSERT(!input.isBuiltIn()); |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 1855 | if (!linkValidateVaryings(infoLog, output.name, input, output, |
| 1856 | vertexShader->getShaderVersion())) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1857 | { |
| 1858 | return false; |
| 1859 | } |
| 1860 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1861 | matched = true; |
| 1862 | break; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | // We permit unmatched, unreferenced varyings |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1867 | if (!matched && output.staticUse) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1868 | { |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1869 | infoLog << "Fragment varying " << output.name << " does not match any vertex varying"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1870 | return false; |
| 1871 | } |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 1872 | |
| 1873 | // Check for aliased path rendering input bindings (if any). |
| 1874 | // If more than one binding refer statically to the same |
| 1875 | // location the link must fail. |
| 1876 | |
| 1877 | if (!output.staticUse) |
| 1878 | continue; |
| 1879 | |
| 1880 | const auto inputBinding = mFragmentInputBindings.getBinding(output.name); |
| 1881 | if (inputBinding == -1) |
| 1882 | continue; |
| 1883 | |
| 1884 | const auto it = staticFragmentInputLocations.find(inputBinding); |
| 1885 | if (it == std::end(staticFragmentInputLocations)) |
| 1886 | { |
| 1887 | staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name)); |
| 1888 | } |
| 1889 | else |
| 1890 | { |
| 1891 | infoLog << "Binding for fragment input " << output.name << " conflicts with " |
| 1892 | << it->second; |
| 1893 | return false; |
| 1894 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1895 | } |
| 1896 | |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 1897 | if (!linkValidateBuiltInVaryings(infoLog)) |
| 1898 | { |
| 1899 | return false; |
| 1900 | } |
| 1901 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1902 | // TODO(jmadill): verify no unmatched vertex varyings? |
| 1903 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1904 | return true; |
| 1905 | } |
| 1906 | |
Olli Etuaho | 4a92ceb | 2017-02-19 17:51:24 +0000 | [diff] [blame] | 1907 | bool Program::linkUniforms(InfoLog &infoLog, |
| 1908 | const Caps &caps, |
| 1909 | const Bindings &uniformLocationBindings) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1910 | { |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1911 | UniformLinker linker(mState); |
| 1912 | if (!linker.link(infoLog, caps, uniformLocationBindings)) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1913 | { |
| 1914 | return false; |
| 1915 | } |
| 1916 | |
Olli Etuaho | b78707c | 2017-03-09 15:03:11 +0000 | [diff] [blame] | 1917 | linker.getResults(&mState.mUniforms, &mState.mUniformLocations); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 1918 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1919 | updateSamplerBindings(); |
| 1920 | |
| 1921 | return true; |
| 1922 | } |
| 1923 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 1924 | void Program::updateSamplerBindings() |
| 1925 | { |
| 1926 | mState.mSamplerUniformRange.end = static_cast<unsigned int>(mState.mUniforms.size()); |
| 1927 | mState.mSamplerUniformRange.start = mState.mSamplerUniformRange.end; |
| 1928 | auto samplerIter = mState.mUniforms.rbegin(); |
| 1929 | while (samplerIter != mState.mUniforms.rend() && samplerIter->isSampler()) |
| 1930 | { |
| 1931 | --mState.mSamplerUniformRange.start; |
| 1932 | ++samplerIter; |
| 1933 | } |
| 1934 | // If uniform is a sampler type, insert it into the mSamplerBindings array. |
| 1935 | for (unsigned int samplerIndex = mState.mSamplerUniformRange.start; |
| 1936 | samplerIndex < mState.mUniforms.size(); ++samplerIndex) |
| 1937 | { |
| 1938 | const auto &samplerUniform = mState.mUniforms[samplerIndex]; |
| 1939 | GLenum textureType = SamplerTypeToTextureType(samplerUniform.type); |
| 1940 | mState.mSamplerBindings.emplace_back( |
| 1941 | SamplerBinding(textureType, samplerUniform.elementCount())); |
| 1942 | } |
| 1943 | } |
| 1944 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 1945 | bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, |
| 1946 | const std::string &uniformName, |
| 1947 | const sh::InterfaceBlockField &vertexUniform, |
| 1948 | const sh::InterfaceBlockField &fragmentUniform) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1949 | { |
Jamie Madill | c4c74422 | 2015-11-04 09:39:47 -0500 | [diff] [blame] | 1950 | // We don't validate precision on UBO fields. See resolution of Khronos bug 10287. |
| 1951 | if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1952 | { |
| 1953 | return false; |
| 1954 | } |
| 1955 | |
| 1956 | if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout) |
| 1957 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1958 | infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1959 | return false; |
| 1960 | } |
| 1961 | |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 1965 | // Assigns locations to all attributes from the bindings and program locations. |
| 1966 | bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1967 | { |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 1968 | const auto *vertexShader = mState.getAttachedVertexShader(); |
| 1969 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1970 | unsigned int usedLocations = 0; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1971 | mState.mAttributes = vertexShader->getActiveAttributes(); |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 1972 | GLuint maxAttribs = data.getCaps().maxVertexAttributes; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1973 | |
| 1974 | // TODO(jmadill): handle aliasing robustly |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1975 | if (mState.mAttributes.size() > maxAttribs) |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1976 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1977 | infoLog << "Too many vertex attributes."; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1978 | return false; |
| 1979 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1980 | |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 1981 | std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr); |
Jamie Madill | 4e10722 | 2015-08-24 14:12:17 +0000 | [diff] [blame] | 1982 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1983 | // Link attributes that have a binding location |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1984 | for (sh::Attribute &attribute : mState.mAttributes) |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1985 | { |
| 1986 | // TODO(jmadill): do staticUse filtering step here, or not at all |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1987 | ASSERT(attribute.staticUse); |
| 1988 | |
Jamie Madill | eb979bf | 2016-11-15 12:28:46 -0500 | [diff] [blame] | 1989 | int bindingLocation = mAttributeBindings.getBinding(attribute.name); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1990 | if (attribute.location == -1 && bindingLocation != -1) |
Jamie Madill | 2d77318 | 2015-08-18 10:27:28 -0400 | [diff] [blame] | 1991 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1992 | attribute.location = bindingLocation; |
| 1993 | } |
| 1994 | |
| 1995 | if (attribute.location != -1) |
| 1996 | { |
| 1997 | // Location is set by glBindAttribLocation or by location layout qualifier |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 1998 | const int regs = VariableRegisterCount(attribute.type); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1999 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2000 | if (static_cast<GLuint>(regs + attribute.location) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2001 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2002 | infoLog << "Active attribute (" << attribute.name << ") at location " |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2003 | << attribute.location << " is too big to fit"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2004 | |
| 2005 | return false; |
| 2006 | } |
| 2007 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2008 | for (int reg = 0; reg < regs; reg++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2009 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2010 | const int regLocation = attribute.location + reg; |
| 2011 | sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2012 | |
| 2013 | // In GLSL 3.00, attribute aliasing produces a link error |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 2014 | // 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] | 2015 | if (linkedAttribute) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2016 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2017 | // TODO(jmadill): fix aliasing on ES2 |
| 2018 | // if (mProgram->getShaderVersion() >= 300) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2019 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 2020 | infoLog << "Attribute '" << attribute.name << "' aliases attribute '" |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2021 | << linkedAttribute->name << "' at location " << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2022 | return false; |
| 2023 | } |
| 2024 | } |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2025 | else |
| 2026 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2027 | usedAttribMap[regLocation] = &attribute; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2028 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2029 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2030 | usedLocations |= 1 << regLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | // Link attributes that don't have a binding location |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2036 | for (sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2037 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2038 | ASSERT(attribute.staticUse); |
| 2039 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2040 | // Not set by glBindAttribLocation or by location layout qualifier |
| 2041 | if (attribute.location == -1) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2042 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2043 | int regs = VariableRegisterCount(attribute.type); |
| 2044 | int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2045 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2046 | if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2047 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2048 | infoLog << "Too many active attributes (" << attribute.name << ")"; |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2049 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2050 | } |
| 2051 | |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2052 | attribute.location = availableIndex; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2056 | for (const sh::Attribute &attribute : mState.mAttributes) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2057 | { |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2058 | ASSERT(attribute.staticUse); |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2059 | ASSERT(attribute.location != -1); |
| 2060 | int regs = VariableRegisterCount(attribute.type); |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 2061 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2062 | for (int r = 0; r < regs; r++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2063 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2064 | mState.mActiveAttribLocationsMask.set(attribute.location + r); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2068 | return true; |
| 2069 | } |
| 2070 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2071 | bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks, |
| 2072 | const std::vector<sh::InterfaceBlock> &intefaceBlocks, |
| 2073 | const std::string &errorMessage, |
| 2074 | InfoLog &infoLog) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2075 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2076 | GLuint blockCount = 0; |
| 2077 | for (const sh::InterfaceBlock &block : intefaceBlocks) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2078 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2079 | if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED) |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2080 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2081 | if (++blockCount > maxUniformBlocks) |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2082 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2083 | infoLog << errorMessage << maxUniformBlocks << ")"; |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2084 | return false; |
| 2085 | } |
| 2086 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2087 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2088 | return true; |
| 2089 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2090 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2091 | bool Program::validateVertexAndFragmentInterfaceBlocks( |
| 2092 | const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks, |
| 2093 | const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks, |
| 2094 | InfoLog &infoLog) const |
| 2095 | { |
| 2096 | // Check that interface blocks defined in the vertex and fragment shaders are identical |
| 2097 | typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap; |
| 2098 | UniformBlockMap linkedUniformBlocks; |
| 2099 | |
| 2100 | for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks) |
| 2101 | { |
| 2102 | linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock; |
| 2103 | } |
| 2104 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2105 | for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2106 | { |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2107 | auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2108 | if (entry != linkedUniformBlocks.end()) |
| 2109 | { |
| 2110 | const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second; |
| 2111 | if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock)) |
| 2112 | { |
| 2113 | return false; |
| 2114 | } |
| 2115 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2116 | } |
| 2117 | return true; |
| 2118 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2119 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2120 | bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps) |
| 2121 | { |
| 2122 | if (mState.mAttachedComputeShader) |
| 2123 | { |
| 2124 | const Shader &computeShader = *mState.mAttachedComputeShader; |
| 2125 | const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(); |
| 2126 | |
| 2127 | if (!validateUniformBlocksCount( |
| 2128 | caps.maxComputeUniformBlocks, computeInterfaceBlocks, |
| 2129 | "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (", |
| 2130 | infoLog)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2131 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2132 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2133 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2134 | return true; |
| 2135 | } |
| 2136 | |
| 2137 | const Shader &vertexShader = *mState.mAttachedVertexShader; |
| 2138 | const Shader &fragmentShader = *mState.mAttachedFragmentShader; |
| 2139 | |
| 2140 | const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(); |
| 2141 | const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(); |
| 2142 | |
| 2143 | if (!validateUniformBlocksCount( |
| 2144 | caps.maxVertexUniformBlocks, vertexInterfaceBlocks, |
| 2145 | "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog)) |
| 2146 | { |
| 2147 | return false; |
| 2148 | } |
| 2149 | if (!validateUniformBlocksCount( |
| 2150 | caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks, |
| 2151 | "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (", |
| 2152 | infoLog)) |
| 2153 | { |
| 2154 | |
| 2155 | return false; |
| 2156 | } |
| 2157 | if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks, |
| 2158 | infoLog)) |
| 2159 | { |
| 2160 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2161 | } |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 2162 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2163 | return true; |
| 2164 | } |
| 2165 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2166 | bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog, |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2167 | const sh::InterfaceBlock &vertexInterfaceBlock, |
| 2168 | const sh::InterfaceBlock &fragmentInterfaceBlock) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2169 | { |
| 2170 | const char* blockName = vertexInterfaceBlock.name.c_str(); |
| 2171 | // validate blocks for the same member types |
| 2172 | if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size()) |
| 2173 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2174 | infoLog << "Types for interface block '" << blockName |
| 2175 | << "' differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2176 | return false; |
| 2177 | } |
| 2178 | if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize) |
| 2179 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2180 | infoLog << "Array sizes differ for interface block '" << blockName |
| 2181 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2182 | return false; |
| 2183 | } |
| 2184 | if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout) |
| 2185 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2186 | infoLog << "Layout qualifiers differ for interface block '" << blockName |
| 2187 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2188 | return false; |
| 2189 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2190 | const unsigned int numBlockMembers = |
| 2191 | static_cast<unsigned int>(vertexInterfaceBlock.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2192 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++) |
| 2193 | { |
| 2194 | const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex]; |
| 2195 | const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex]; |
| 2196 | if (vertexMember.name != fragmentMember.name) |
| 2197 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2198 | infoLog << "Name mismatch for field " << blockMemberIndex |
| 2199 | << " of interface block '" << blockName |
| 2200 | << "': (in vertex: '" << vertexMember.name |
| 2201 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2202 | return false; |
| 2203 | } |
| 2204 | std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'"; |
| 2205 | if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember)) |
| 2206 | { |
| 2207 | return false; |
| 2208 | } |
| 2209 | } |
| 2210 | return true; |
| 2211 | } |
| 2212 | |
| 2213 | bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable, |
| 2214 | const sh::ShaderVariable &fragmentVariable, bool validatePrecision) |
| 2215 | { |
| 2216 | if (vertexVariable.type != fragmentVariable.type) |
| 2217 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2218 | infoLog << "Types for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2219 | return false; |
| 2220 | } |
| 2221 | if (vertexVariable.arraySize != fragmentVariable.arraySize) |
| 2222 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2223 | infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2224 | return false; |
| 2225 | } |
| 2226 | if (validatePrecision && vertexVariable.precision != fragmentVariable.precision) |
| 2227 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2228 | infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2229 | return false; |
| 2230 | } |
| 2231 | |
| 2232 | if (vertexVariable.fields.size() != fragmentVariable.fields.size()) |
| 2233 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2234 | infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2235 | return false; |
| 2236 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2237 | const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2238 | for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++) |
| 2239 | { |
| 2240 | const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex]; |
| 2241 | const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex]; |
| 2242 | |
| 2243 | if (vertexMember.name != fragmentMember.name) |
| 2244 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2245 | infoLog << "Name mismatch for field '" << memberIndex |
| 2246 | << "' of " << variableName |
| 2247 | << ": (in vertex: '" << vertexMember.name |
| 2248 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2249 | return false; |
| 2250 | } |
| 2251 | |
| 2252 | const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." + |
| 2253 | vertexMember.name + "'"; |
| 2254 | |
| 2255 | if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision)) |
| 2256 | { |
| 2257 | return false; |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | return true; |
| 2262 | } |
| 2263 | |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2264 | bool Program::linkValidateVaryings(InfoLog &infoLog, |
| 2265 | const std::string &varyingName, |
| 2266 | const sh::Varying &vertexVarying, |
| 2267 | const sh::Varying &fragmentVarying, |
| 2268 | int shaderVersion) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2269 | { |
| 2270 | if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false)) |
| 2271 | { |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
Jamie Madill | e9cc469 | 2015-02-19 16:00:13 -0500 | [diff] [blame] | 2275 | if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2276 | { |
Yuly Novikov | a1f6dc9 | 2016-06-15 23:27:04 -0400 | [diff] [blame] | 2277 | infoLog << "Interpolation types for " << varyingName |
| 2278 | << " differ between vertex and fragment shaders."; |
| 2279 | return false; |
| 2280 | } |
| 2281 | |
| 2282 | if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant) |
| 2283 | { |
| 2284 | infoLog << "Invariance for " << varyingName |
| 2285 | << " differs between vertex and fragment shaders."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2286 | return false; |
| 2287 | } |
| 2288 | |
| 2289 | return true; |
| 2290 | } |
| 2291 | |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 2292 | bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const |
| 2293 | { |
| 2294 | const Shader *vertexShader = mState.mAttachedVertexShader; |
| 2295 | const Shader *fragmentShader = mState.mAttachedFragmentShader; |
| 2296 | const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(); |
| 2297 | const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(); |
| 2298 | int shaderVersion = vertexShader->getShaderVersion(); |
| 2299 | |
| 2300 | if (shaderVersion != 100) |
| 2301 | { |
| 2302 | // Only ESSL 1.0 has restrictions on matching input and output invariance |
| 2303 | return true; |
| 2304 | } |
| 2305 | |
| 2306 | bool glPositionIsInvariant = false; |
| 2307 | bool glPointSizeIsInvariant = false; |
| 2308 | bool glFragCoordIsInvariant = false; |
| 2309 | bool glPointCoordIsInvariant = false; |
| 2310 | |
| 2311 | for (const sh::Varying &varying : vertexVaryings) |
| 2312 | { |
| 2313 | if (!varying.isBuiltIn()) |
| 2314 | { |
| 2315 | continue; |
| 2316 | } |
| 2317 | if (varying.name.compare("gl_Position") == 0) |
| 2318 | { |
| 2319 | glPositionIsInvariant = varying.isInvariant; |
| 2320 | } |
| 2321 | else if (varying.name.compare("gl_PointSize") == 0) |
| 2322 | { |
| 2323 | glPointSizeIsInvariant = varying.isInvariant; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | for (const sh::Varying &varying : fragmentVaryings) |
| 2328 | { |
| 2329 | if (!varying.isBuiltIn()) |
| 2330 | { |
| 2331 | continue; |
| 2332 | } |
| 2333 | if (varying.name.compare("gl_FragCoord") == 0) |
| 2334 | { |
| 2335 | glFragCoordIsInvariant = varying.isInvariant; |
| 2336 | } |
| 2337 | else if (varying.name.compare("gl_PointCoord") == 0) |
| 2338 | { |
| 2339 | glPointCoordIsInvariant = varying.isInvariant; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation, |
| 2344 | // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842. |
| 2345 | // Not requiring invariance to match is supported by: |
| 2346 | // dEQP, WebGL CTS, Nexus 5X GLES |
| 2347 | if (glFragCoordIsInvariant && !glPositionIsInvariant) |
| 2348 | { |
| 2349 | infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is " |
| 2350 | "declared invariant."; |
| 2351 | return false; |
| 2352 | } |
| 2353 | if (glPointCoordIsInvariant && !glPointSizeIsInvariant) |
| 2354 | { |
| 2355 | infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is " |
| 2356 | "declared invariant."; |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
| 2360 | return true; |
| 2361 | } |
| 2362 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2363 | bool Program::linkValidateTransformFeedback(InfoLog &infoLog, |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2364 | const Program::MergedVaryings &varyings, |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2365 | const Caps &caps) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2366 | { |
| 2367 | size_t totalComponents = 0; |
| 2368 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2369 | std::set<std::string> uniqueNames; |
| 2370 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2371 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2372 | { |
| 2373 | bool found = false; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2374 | for (const auto &ref : varyings) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2375 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2376 | const sh::Varying *varying = ref.second.get(); |
| 2377 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2378 | if (tfVaryingName == varying->name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2379 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2380 | if (uniqueNames.count(tfVaryingName) > 0) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2381 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2382 | infoLog << "Two transform feedback varyings specify the same output variable (" |
| 2383 | << tfVaryingName << ")."; |
| 2384 | return false; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2385 | } |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2386 | uniqueNames.insert(tfVaryingName); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2387 | |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 2388 | if (varying->isArray()) |
| 2389 | { |
| 2390 | infoLog << "Capture of arrays is undefined and not supported."; |
| 2391 | return false; |
| 2392 | } |
| 2393 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2394 | // TODO(jmadill): Investigate implementation limits on D3D11 |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2395 | size_t componentCount = VariableComponentCount(varying->type); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2396 | if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS && |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2397 | componentCount > caps.maxTransformFeedbackSeparateComponents) |
| 2398 | { |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2399 | infoLog << "Transform feedback varying's " << varying->name << " components (" |
| 2400 | << componentCount << ") exceed the maximum separate components (" |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2401 | << caps.maxTransformFeedbackSeparateComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2402 | return false; |
| 2403 | } |
| 2404 | |
| 2405 | totalComponents += componentCount; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2406 | found = true; |
| 2407 | break; |
| 2408 | } |
| 2409 | } |
| 2410 | |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 2411 | if (tfVaryingName.find('[') != std::string::npos) |
| 2412 | { |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 2413 | infoLog << "Capture of array elements is undefined and not supported."; |
Jamie Madill | 89bb70e | 2015-08-31 14:18:39 -0400 | [diff] [blame] | 2414 | return false; |
| 2415 | } |
| 2416 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2417 | // All transform feedback varyings are expected to exist since packVaryings checks for them. |
| 2418 | ASSERT(found); |
| 2419 | } |
| 2420 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2421 | if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2422 | totalComponents > caps.maxTransformFeedbackInterleavedComponents) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2423 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2424 | infoLog << "Transform feedback varying total components (" << totalComponents |
| 2425 | << ") exceed the maximum interleaved components (" |
| 2426 | << caps.maxTransformFeedbackInterleavedComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2427 | return false; |
| 2428 | } |
| 2429 | |
| 2430 | return true; |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 2431 | } |
| 2432 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2433 | void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2434 | { |
| 2435 | // 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] | 2436 | mState.mTransformFeedbackVaryingVars.clear(); |
| 2437 | for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2438 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2439 | for (const auto &ref : varyings) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2440 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2441 | const sh::Varying *varying = ref.second.get(); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2442 | if (tfVaryingName == varying->name) |
| 2443 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2444 | mState.mTransformFeedbackVaryingVars.push_back(*varying); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2445 | break; |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2451 | Program::MergedVaryings Program::getMergedVaryings() const |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2452 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2453 | MergedVaryings merged; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2454 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2455 | for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings()) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2456 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2457 | merged[varying.name].vertex = &varying; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2458 | } |
| 2459 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2460 | for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings()) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2461 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2462 | merged[varying.name].fragment = &varying; |
| 2463 | } |
| 2464 | |
| 2465 | return merged; |
| 2466 | } |
| 2467 | |
| 2468 | std::vector<PackedVarying> Program::getPackedVaryings( |
| 2469 | const Program::MergedVaryings &mergedVaryings) const |
| 2470 | { |
| 2471 | const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames(); |
| 2472 | std::vector<PackedVarying> packedVaryings; |
| 2473 | |
| 2474 | for (const auto &ref : mergedVaryings) |
| 2475 | { |
| 2476 | const sh::Varying *input = ref.second.vertex; |
| 2477 | const sh::Varying *output = ref.second.fragment; |
| 2478 | |
| 2479 | // Only pack varyings that have a matched input or output, plus special builtins. |
| 2480 | if ((input && output) || (output && output->isBuiltIn())) |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2481 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2482 | // Will get the vertex shader interpolation by default. |
| 2483 | auto interpolation = ref.second.get()->interpolation; |
| 2484 | |
| 2485 | // Interpolation qualifiers must match. |
| 2486 | if (output->isStruct()) |
| 2487 | { |
| 2488 | ASSERT(!output->isArray()); |
| 2489 | for (const auto &field : output->fields) |
| 2490 | { |
| 2491 | ASSERT(!field.isStruct() && !field.isArray()); |
| 2492 | packedVaryings.push_back(PackedVarying(field, interpolation, output->name)); |
| 2493 | } |
| 2494 | } |
| 2495 | else |
| 2496 | { |
| 2497 | packedVaryings.push_back(PackedVarying(*output, interpolation)); |
| 2498 | } |
| 2499 | continue; |
| 2500 | } |
| 2501 | |
| 2502 | // Keep Transform FB varyings in the merged list always. |
| 2503 | if (!input) |
| 2504 | { |
| 2505 | continue; |
| 2506 | } |
| 2507 | |
| 2508 | for (const std::string &tfVarying : tfVaryings) |
| 2509 | { |
| 2510 | if (tfVarying == input->name) |
| 2511 | { |
| 2512 | // Transform feedback for varying structs is underspecified. |
| 2513 | // See Khronos bug 9856. |
| 2514 | // TODO(jmadill): Figure out how to be spec-compliant here. |
| 2515 | if (!input->isStruct()) |
| 2516 | { |
| 2517 | packedVaryings.push_back(PackedVarying(*input, input->interpolation)); |
| 2518 | packedVaryings.back().vertexOnly = true; |
| 2519 | } |
| 2520 | break; |
| 2521 | } |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2522 | } |
| 2523 | } |
| 2524 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 2525 | std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying); |
| 2526 | |
| 2527 | return packedVaryings; |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2528 | } |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2529 | |
| 2530 | void Program::linkOutputVariables() |
| 2531 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2532 | const Shader *fragmentShader = mState.mAttachedFragmentShader; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2533 | ASSERT(fragmentShader != nullptr); |
| 2534 | |
| 2535 | // Skip this step for GLES2 shaders. |
| 2536 | if (fragmentShader->getShaderVersion() == 100) |
| 2537 | return; |
| 2538 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 2539 | const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables(); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2540 | |
| 2541 | // TODO(jmadill): any caps validation here? |
| 2542 | |
| 2543 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); |
| 2544 | outputVariableIndex++) |
| 2545 | { |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 2546 | const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex]; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2547 | |
| 2548 | // Don't store outputs for gl_FragDepth, gl_FragColor, etc. |
| 2549 | if (outputVariable.isBuiltIn()) |
| 2550 | continue; |
| 2551 | |
| 2552 | // Since multiple output locations must be specified, use 0 for non-specified locations. |
| 2553 | int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location); |
| 2554 | |
| 2555 | ASSERT(outputVariable.staticUse); |
| 2556 | |
| 2557 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount(); |
| 2558 | elementIndex++) |
| 2559 | { |
| 2560 | const int location = baseLocation + elementIndex; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2561 | ASSERT(mState.mOutputVariables.count(location) == 0); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2562 | unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2563 | mState.mOutputVariables[location] = |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 2564 | VariableLocation(outputVariable.name, element, outputVariableIndex); |
| 2565 | } |
| 2566 | } |
| 2567 | } |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2568 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2569 | void Program::gatherInterfaceBlockInfo() |
| 2570 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2571 | ASSERT(mState.mUniformBlocks.empty()); |
| 2572 | |
| 2573 | if (mState.mAttachedComputeShader) |
| 2574 | { |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2575 | const Shader *computeShader = mState.getAttachedComputeShader(); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2576 | |
| 2577 | for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks()) |
| 2578 | { |
| 2579 | |
| 2580 | // Only 'packed' blocks are allowed to be considered inactive. |
| 2581 | if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED) |
| 2582 | continue; |
| 2583 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2584 | for (UniformBlock &block : mState.mUniformBlocks) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2585 | { |
| 2586 | if (block.name == computeBlock.name) |
| 2587 | { |
| 2588 | block.computeStaticUse = computeBlock.staticUse; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | defineUniformBlock(computeBlock, GL_COMPUTE_SHADER); |
| 2593 | } |
| 2594 | return; |
| 2595 | } |
| 2596 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2597 | std::set<std::string> visitedList; |
| 2598 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2599 | const Shader *vertexShader = mState.getAttachedVertexShader(); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2600 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2601 | for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks()) |
| 2602 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2603 | // Only 'packed' blocks are allowed to be considered inactive. |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2604 | if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED) |
| 2605 | continue; |
| 2606 | |
| 2607 | if (visitedList.count(vertexBlock.name) > 0) |
| 2608 | continue; |
| 2609 | |
| 2610 | defineUniformBlock(vertexBlock, GL_VERTEX_SHADER); |
| 2611 | visitedList.insert(vertexBlock.name); |
| 2612 | } |
| 2613 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2614 | const Shader *fragmentShader = mState.getAttachedFragmentShader(); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2615 | |
| 2616 | for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks()) |
| 2617 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2618 | // Only 'packed' blocks are allowed to be considered inactive. |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2619 | if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED) |
| 2620 | continue; |
| 2621 | |
| 2622 | if (visitedList.count(fragmentBlock.name) > 0) |
| 2623 | { |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2624 | for (UniformBlock &block : mState.mUniformBlocks) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2625 | { |
| 2626 | if (block.name == fragmentBlock.name) |
| 2627 | { |
| 2628 | block.fragmentStaticUse = fragmentBlock.staticUse; |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | continue; |
| 2633 | } |
| 2634 | |
| 2635 | defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER); |
| 2636 | visitedList.insert(fragmentBlock.name); |
| 2637 | } |
| 2638 | } |
| 2639 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2640 | template <typename VarT> |
| 2641 | void Program::defineUniformBlockMembers(const std::vector<VarT> &fields, |
| 2642 | const std::string &prefix, |
| 2643 | int blockIndex) |
| 2644 | { |
| 2645 | for (const VarT &field : fields) |
| 2646 | { |
| 2647 | const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name); |
| 2648 | |
| 2649 | if (field.isStruct()) |
| 2650 | { |
| 2651 | for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++) |
| 2652 | { |
| 2653 | const std::string uniformElementName = |
| 2654 | fullName + (field.isArray() ? ArrayString(arrayElement) : ""); |
| 2655 | defineUniformBlockMembers(field.fields, uniformElementName, blockIndex); |
| 2656 | } |
| 2657 | } |
| 2658 | else |
| 2659 | { |
| 2660 | // If getBlockMemberInfo returns false, the uniform is optimized out. |
| 2661 | sh::BlockMemberInfo memberInfo; |
| 2662 | if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo)) |
| 2663 | { |
| 2664 | continue; |
| 2665 | } |
| 2666 | |
Olli Etuaho | 6ca2b65 | 2017-02-19 18:05:10 +0000 | [diff] [blame] | 2667 | LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1, |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2668 | blockIndex, memberInfo); |
| 2669 | |
| 2670 | // Since block uniforms have no location, we don't need to store them in the uniform |
| 2671 | // locations list. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2672 | mState.mUniforms.push_back(newUniform); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2673 | } |
| 2674 | } |
| 2675 | } |
| 2676 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2677 | void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType) |
| 2678 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2679 | int blockIndex = static_cast<int>(mState.mUniformBlocks.size()); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2680 | size_t blockSize = 0; |
| 2681 | |
| 2682 | // 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] | 2683 | std::stringstream blockNameStr; |
| 2684 | blockNameStr << interfaceBlock.name; |
| 2685 | if (interfaceBlock.arraySize > 0) |
| 2686 | { |
| 2687 | blockNameStr << "[0]"; |
| 2688 | } |
| 2689 | if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize)) |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2690 | { |
| 2691 | return; |
| 2692 | } |
| 2693 | |
| 2694 | // Track the first and last uniform index to determine the range of active uniforms in the |
| 2695 | // block. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2696 | size_t firstBlockUniformIndex = mState.mUniforms.size(); |
Jamie Madill | 3904616 | 2016-02-08 15:05:17 -0500 | [diff] [blame] | 2697 | defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2698 | size_t lastBlockUniformIndex = mState.mUniforms.size(); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2699 | |
| 2700 | std::vector<unsigned int> blockUniformIndexes; |
| 2701 | for (size_t blockUniformIndex = firstBlockUniformIndex; |
| 2702 | blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex) |
| 2703 | { |
| 2704 | blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex)); |
| 2705 | } |
| 2706 | |
| 2707 | if (interfaceBlock.arraySize > 0) |
| 2708 | { |
| 2709 | for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement) |
| 2710 | { |
| 2711 | UniformBlock block(interfaceBlock.name, true, arrayElement); |
| 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 | |
Qin Jiajia | 0350a64 | 2016-11-01 17:01:51 +0800 | [diff] [blame] | 2735 | // Since all block elements in an array share the same active uniforms, they will all be |
| 2736 | // active once any uniform member is used. So, since interfaceBlock.name[0] was active, |
| 2737 | // here we will add every block element in the array. |
| 2738 | block.dataSize = static_cast<unsigned int>(blockSize); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2739 | mState.mUniformBlocks.push_back(block); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2740 | } |
| 2741 | } |
| 2742 | else |
| 2743 | { |
| 2744 | UniformBlock block(interfaceBlock.name, false, 0); |
| 2745 | block.memberUniformIndexes = blockUniformIndexes; |
| 2746 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2747 | switch (shaderType) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2748 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 2749 | case GL_VERTEX_SHADER: |
| 2750 | { |
| 2751 | block.vertexStaticUse = interfaceBlock.staticUse; |
| 2752 | break; |
| 2753 | } |
| 2754 | case GL_FRAGMENT_SHADER: |
| 2755 | { |
| 2756 | block.fragmentStaticUse = interfaceBlock.staticUse; |
| 2757 | break; |
| 2758 | } |
| 2759 | case GL_COMPUTE_SHADER: |
| 2760 | { |
| 2761 | block.computeStaticUse = interfaceBlock.staticUse; |
| 2762 | break; |
| 2763 | } |
| 2764 | default: |
| 2765 | UNREACHABLE(); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2766 | } |
| 2767 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 2768 | block.dataSize = static_cast<unsigned int>(blockSize); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2769 | mState.mUniformBlocks.push_back(block); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2773 | template <> |
| 2774 | void Program::updateSamplerUniform(const VariableLocation &locationInfo, |
| 2775 | const uint8_t *destPointer, |
| 2776 | GLsizei clampedCount, |
| 2777 | const GLint *v) |
| 2778 | { |
| 2779 | // Invalidate the validation cache only if we modify the sampler data. |
| 2780 | if (mState.isSamplerUniformIndex(locationInfo.index) && |
| 2781 | memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0) |
| 2782 | { |
| 2783 | GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index); |
| 2784 | std::vector<GLuint> *boundTextureUnits = |
| 2785 | &mState.mSamplerBindings[samplerIndex].boundTextureUnits; |
| 2786 | |
| 2787 | std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element); |
| 2788 | mCachedValidateSamplersResult.reset(); |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | template <typename T> |
| 2793 | void Program::updateSamplerUniform(const VariableLocation &locationInfo, |
| 2794 | const uint8_t *destPointer, |
| 2795 | GLsizei clampedCount, |
| 2796 | const T *v) |
| 2797 | { |
| 2798 | } |
| 2799 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2800 | template <typename T> |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2801 | GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2802 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2803 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 2804 | LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2805 | uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element); |
| 2806 | |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2807 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 2808 | // element index used, as reported by GetActiveUniform, will be ignored by the GL." |
| 2809 | unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element; |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2810 | GLsizei maxElementCount = |
| 2811 | static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents()); |
| 2812 | |
| 2813 | GLsizei count = countIn; |
| 2814 | GLsizei clampedCount = count * vectorSize; |
| 2815 | if (clampedCount > maxElementCount) |
| 2816 | { |
| 2817 | clampedCount = maxElementCount; |
| 2818 | count = maxElementCount / vectorSize; |
| 2819 | } |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2820 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2821 | if (VariableComponentType(linkedUniform->type) == GL_BOOL) |
| 2822 | { |
| 2823 | // Do a cast conversion for boolean types. From the spec: |
| 2824 | // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise." |
| 2825 | GLint *destAsInt = reinterpret_cast<GLint *>(destPointer); |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2826 | for (GLsizei component = 0; component < clampedCount; ++component) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2827 | { |
| 2828 | destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE); |
| 2829 | } |
| 2830 | } |
| 2831 | else |
| 2832 | { |
Jamie Madill | e7d8432 | 2017-01-10 18:21:59 -0500 | [diff] [blame] | 2833 | updateSamplerUniform(locationInfo, destPointer, clampedCount, v); |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2834 | memcpy(destPointer, v, sizeof(T) * clampedCount); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2835 | } |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2836 | |
| 2837 | return count; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | template <size_t cols, size_t rows, typename T> |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2841 | GLsizei Program::setMatrixUniformInternal(GLint location, |
| 2842 | GLsizei count, |
| 2843 | GLboolean transpose, |
| 2844 | const T *v) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2845 | { |
| 2846 | if (!transpose) |
| 2847 | { |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2848 | return setUniformInternal(location, count, cols * rows, v); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | // Perform a transposing copy. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2852 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 2853 | LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2854 | T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element)); |
Corentin Wallez | 15ac534 | 2016-11-03 17:06:39 -0400 | [diff] [blame] | 2855 | |
| 2856 | // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array |
| 2857 | // element index used, as reported by GetActiveUniform, will be ignored by the GL." |
| 2858 | unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element; |
| 2859 | GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements)); |
| 2860 | |
| 2861 | for (GLsizei element = 0; element < clampedCount; ++element) |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2862 | { |
| 2863 | size_t elementOffset = element * rows * cols; |
| 2864 | |
| 2865 | for (size_t row = 0; row < rows; ++row) |
| 2866 | { |
| 2867 | for (size_t col = 0; col < cols; ++col) |
| 2868 | { |
| 2869 | destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset]; |
| 2870 | } |
| 2871 | } |
| 2872 | } |
Corentin Wallez | 8b7d814 | 2016-11-15 13:40:37 -0500 | [diff] [blame] | 2873 | |
| 2874 | return clampedCount; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | template <typename DestT> |
| 2878 | void Program::getUniformInternal(GLint location, DestT *dataOut) const |
| 2879 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 2880 | const VariableLocation &locationInfo = mState.mUniformLocations[location]; |
| 2881 | const LinkedUniform &uniform = mState.mUniforms[locationInfo.index]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2882 | |
| 2883 | const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element); |
| 2884 | |
| 2885 | GLenum componentType = VariableComponentType(uniform.type); |
| 2886 | if (componentType == GLTypeToGLenum<DestT>::value) |
| 2887 | { |
| 2888 | memcpy(dataOut, srcPointer, uniform.getElementSize()); |
| 2889 | return; |
| 2890 | } |
| 2891 | |
Corentin Wallez | 6596c46 | 2016-03-17 17:26:58 -0400 | [diff] [blame] | 2892 | int components = VariableComponentCount(uniform.type); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 2893 | |
| 2894 | switch (componentType) |
| 2895 | { |
| 2896 | case GL_INT: |
| 2897 | UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components); |
| 2898 | break; |
| 2899 | case GL_UNSIGNED_INT: |
| 2900 | UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components); |
| 2901 | break; |
| 2902 | case GL_BOOL: |
| 2903 | UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components); |
| 2904 | break; |
| 2905 | case GL_FLOAT: |
| 2906 | UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components); |
| 2907 | break; |
| 2908 | default: |
| 2909 | UNREACHABLE(); |
| 2910 | } |
| 2911 | } |
Jamie Madill | a4595b8 | 2017-01-11 17:36:34 -0500 | [diff] [blame] | 2912 | |
| 2913 | bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const |
| 2914 | { |
| 2915 | // Must be called after samplers are validated. |
| 2916 | ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value()); |
| 2917 | |
| 2918 | for (const auto &binding : mState.mSamplerBindings) |
| 2919 | { |
| 2920 | GLenum textureType = binding.textureType; |
| 2921 | for (const auto &unit : binding.boundTextureUnits) |
| 2922 | { |
| 2923 | GLenum programTextureID = state.getSamplerTextureId(unit, textureType); |
| 2924 | if (programTextureID == textureID) |
| 2925 | { |
| 2926 | // TODO(jmadill): Check for appropriate overlap. |
| 2927 | return true; |
| 2928 | } |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | return false; |
| 2933 | } |
| 2934 | |
Jamie Madill | a2c7498 | 2016-12-12 11:20:42 -0500 | [diff] [blame] | 2935 | } // namespace gl |