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