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