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