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