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