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