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