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