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