blob: 52dddc9115f133fd53fbb51a6bbe9af90e3d86db [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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 Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
Olli Etuahod2551232017-10-26 20:03:33 +030017#include "common/string_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/MemoryProgramCache.h"
Jamie Madill7af0de52017-11-06 17:09:33 -050022#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill437d2662014-12-05 14:23:35 -050023#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040024#include "libANGLE/Uniform.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040025#include "libANGLE/VaryingPacking.h"
26#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040027#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040028#include "libANGLE/queryconversions.h"
29#include "libANGLE/renderer/GLImplFactory.h"
30#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040031#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050032
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033namespace gl
34{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000035
Geoff Lang7dd2e102014-11-10 15:19:26 -050036namespace
37{
38
Jamie Madill62d31cb2015-09-11 13:25:51 -040039// This simplified cast function doesn't need to worry about advanced concepts like
40// depth range values, or casting to bool.
41template <typename DestT, typename SrcT>
42DestT UniformStateQueryCast(SrcT value);
43
44// From-Float-To-Integer Casts
45template <>
46GLint UniformStateQueryCast(GLfloat value)
47{
48 return clampCast<GLint>(roundf(value));
49}
50
51template <>
52GLuint UniformStateQueryCast(GLfloat value)
53{
54 return clampCast<GLuint>(roundf(value));
55}
56
57// From-Integer-to-Integer Casts
58template <>
59GLint UniformStateQueryCast(GLuint value)
60{
61 return clampCast<GLint>(value);
62}
63
64template <>
65GLuint UniformStateQueryCast(GLint value)
66{
67 return clampCast<GLuint>(value);
68}
69
70// From-Boolean-to-Anything Casts
71template <>
72GLfloat UniformStateQueryCast(GLboolean value)
73{
Geoff Lang92019432017-11-20 13:09:34 -050074 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040075}
76
77template <>
78GLint UniformStateQueryCast(GLboolean value)
79{
Geoff Lang92019432017-11-20 13:09:34 -050080 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040081}
82
83template <>
84GLuint UniformStateQueryCast(GLboolean value)
85{
Geoff Lang92019432017-11-20 13:09:34 -050086 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040087}
88
89// Default to static_cast
90template <typename DestT, typename SrcT>
91DestT UniformStateQueryCast(SrcT value)
92{
93 return static_cast<DestT>(value);
94}
95
96template <typename SrcT, typename DestT>
97void 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
jchen1015015f72017-03-16 13:54:21 +0800109template <typename VarT>
110GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
111{
Olli Etuahod2551232017-10-26 20:03:33 +0300112 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800113 for (size_t index = 0; index < list.size(); index++)
114 {
115 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300116 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800117 {
Olli Etuahod2551232017-10-26 20:03:33 +0300118 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800119 }
120 }
121
122 return GL_INVALID_INDEX;
123}
124
Olli Etuahod2551232017-10-26 20:03:33 +0300125template <typename VarT>
126GLint 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 Etuaho1734e172017-10-27 15:30:27 +0300161 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300162 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
jchen10fd7c3b52017-03-21 15:36:03 +0800179void 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
jchen10a9042d32017-03-17 08:50:45 +0800191bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
192{
Olli Etuahoc8538042017-09-27 11:20:15 +0300193 std::vector<unsigned int> subscripts;
194 std::string baseName = ParseResourceName(name, &subscripts);
195 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800196 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300197 std::vector<unsigned int> arrayIndices;
198 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
199 if (baseName == arrayName &&
200 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800201 {
202 return true;
203 }
204 }
205 return false;
206}
207
Jiawei Shao40786bd2018-04-18 13:58:57 +0800208std::string GetInterfaceBlockLimitName(ShaderType shaderType, sh::BlockType blockType)
209{
210 std::ostringstream stream;
211 stream << "GL_MAX_" << GetShaderTypeString(shaderType) << "_";
212
213 switch (blockType)
214 {
215 case sh::BlockType::BLOCK_UNIFORM:
216 stream << "UNIFORM_BUFFERS";
217 break;
218 case sh::BlockType::BLOCK_BUFFER:
219 stream << "SHADER_STORAGE_BLOCKS";
220 break;
221 default:
222 UNREACHABLE();
223 return "";
224 }
225
226 if (shaderType == ShaderType::Geometry)
227 {
228 stream << "_EXT";
229 }
230
231 return stream.str();
232}
233
234const char *GetInterfaceBlockTypeString(sh::BlockType blockType)
235{
236 switch (blockType)
237 {
238 case sh::BlockType::BLOCK_UNIFORM:
239 return "uniform block";
240 case sh::BlockType::BLOCK_BUFFER:
241 return "shader storage block";
242 default:
243 UNREACHABLE();
244 return "";
245 }
246}
247
248void LogInterfaceBlocksExceedLimit(InfoLog &infoLog,
249 ShaderType shaderType,
250 sh::BlockType blockType,
251 GLuint limit)
252{
253 infoLog << GetShaderTypeString(shaderType) << " shader "
254 << GetInterfaceBlockTypeString(blockType) << " count exceeds "
255 << GetInterfaceBlockLimitName(shaderType, blockType) << " (" << limit << ")";
256}
257
Jiawei Shao427071e2018-03-19 09:21:37 +0800258bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800259 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800260 ShaderType shaderType,
261 sh::BlockType blockType,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800262 InfoLog &infoLog)
263{
264 GLuint blockCount = 0;
265 for (const sh::InterfaceBlock &block : interfaceBlocks)
266 {
Olli Etuaho107c7242018-03-20 15:45:35 +0200267 if (block.active || block.layout != sh::BLOCKLAYOUT_PACKED)
Jiajia Qin729b2c62017-08-14 09:36:11 +0800268 {
269 blockCount += (block.arraySize ? block.arraySize : 1);
270 if (blockCount > maxInterfaceBlocks)
271 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800272 LogInterfaceBlocksExceedLimit(infoLog, shaderType, blockType, maxInterfaceBlocks);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800273 return false;
274 }
275 }
276 }
277 return true;
278}
279
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800280GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
281{
282 std::vector<unsigned int> subscripts;
283 std::string baseName = ParseResourceName(name, &subscripts);
284
285 unsigned int numBlocks = static_cast<unsigned int>(list.size());
286 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
287 {
288 const auto &block = list[blockIndex];
289 if (block.name == baseName)
290 {
291 const bool arrayElementZero =
292 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
293 const bool arrayElementMatches =
294 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
295 if (arrayElementMatches || arrayElementZero)
296 {
297 return blockIndex;
298 }
299 }
300 }
301
302 return GL_INVALID_INDEX;
303}
304
305void GetInterfaceBlockName(const GLuint index,
306 const std::vector<InterfaceBlock> &list,
307 GLsizei bufSize,
308 GLsizei *length,
309 GLchar *name)
310{
311 ASSERT(index < list.size());
312
313 const auto &block = list[index];
314
315 if (bufSize > 0)
316 {
317 std::string blockName = block.name;
318
319 if (block.isArray)
320 {
321 blockName += ArrayString(block.arrayElement);
322 }
323 CopyStringToBuffer(name, blockName, bufSize, length);
324 }
325}
326
Jamie Madillc9727f32017-11-07 12:37:07 -0500327void InitUniformBlockLinker(const gl::Context *context,
328 const ProgramState &state,
329 UniformBlockLinker *blockLinker)
330{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800331 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500332 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800333 Shader *shader = state.getAttachedShader(shaderType);
334 if (shader)
335 {
336 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks(context));
337 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500338 }
339}
340
341void InitShaderStorageBlockLinker(const gl::Context *context,
342 const ProgramState &state,
343 ShaderStorageBlockLinker *blockLinker)
344{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800345 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500346 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800347 Shader *shader = state.getAttachedShader(shaderType);
348 if (shader != nullptr)
349 {
350 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks(context));
351 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500352 }
353}
354
jchen108225e732017-11-14 16:29:03 +0800355// Find the matching varying or field by name.
356const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
357 const std::string &name)
358{
359 const sh::ShaderVariable *var = nullptr;
360 for (const auto &ref : varyings)
361 {
362 const sh::Varying *varying = ref.second.get();
363 if (varying->name == name)
364 {
365 var = varying;
366 break;
367 }
368 var = FindShaderVarField(*varying, name);
369 if (var != nullptr)
370 {
371 break;
372 }
373 }
374 return var;
375}
376
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800377void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
378{
379 ASSERT(mismatchedFieldName);
380 if (mismatchedFieldName->empty())
381 {
382 *mismatchedFieldName = parentName;
383 }
384 else
385 {
386 std::ostringstream stream;
387 stream << parentName << "." << *mismatchedFieldName;
388 *mismatchedFieldName = stream.str();
389 }
390}
391
392const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
393{
394 switch (linkError)
395 {
396 case LinkMismatchError::TYPE_MISMATCH:
397 return "Type";
398 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
399 return "Array size";
400 case LinkMismatchError::PRECISION_MISMATCH:
401 return "Precision";
402 case LinkMismatchError::STRUCT_NAME_MISMATCH:
403 return "Structure name";
404 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
405 return "Field number";
406 case LinkMismatchError::FIELD_NAME_MISMATCH:
407 return "Field name";
408
409 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
410 return "Interpolation type";
411 case LinkMismatchError::INVARIANCE_MISMATCH:
412 return "Invariance";
413
414 case LinkMismatchError::BINDING_MISMATCH:
415 return "Binding layout qualifier";
416 case LinkMismatchError::LOCATION_MISMATCH:
417 return "Location layout qualifier";
418 case LinkMismatchError::OFFSET_MISMATCH:
419 return "Offset layout qualilfier";
420
421 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
422 return "Layout qualifier";
423 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
424 return "Matrix Packing";
425 default:
426 UNREACHABLE();
427 return "";
428 }
429}
430
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800431LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
432 const sh::InterfaceBlockField &blockField2,
433 bool webglCompatibility,
434 std::string *mismatchedBlockFieldName)
435{
436 if (blockField1.name != blockField2.name)
437 {
438 return LinkMismatchError::FIELD_NAME_MISMATCH;
439 }
440
441 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
442 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
443 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
444 if (linkError != LinkMismatchError::NO_MISMATCH)
445 {
446 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
447 return linkError;
448 }
449
450 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
451 {
452 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
453 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
454 }
455
456 return LinkMismatchError::NO_MISMATCH;
457}
458
459LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
460 const sh::InterfaceBlock &interfaceBlock2,
461 bool webglCompatibility,
462 std::string *mismatchedBlockFieldName)
463{
464 // validate blocks for the same member types
465 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
466 {
467 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
468 }
469 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
470 {
471 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
472 }
473 if (interfaceBlock1.layout != interfaceBlock2.layout ||
474 interfaceBlock1.binding != interfaceBlock2.binding)
475 {
476 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
477 }
478 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
479 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
480 {
481 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
482 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
483
484 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
485 member1, member2, webglCompatibility, mismatchedBlockFieldName);
486 if (linkError != LinkMismatchError::NO_MISMATCH)
487 {
488 return linkError;
489 }
490 }
491 return LinkMismatchError::NO_MISMATCH;
492}
493
Jiawei Shao40786bd2018-04-18 13:58:57 +0800494using ShaderInterfaceBlock = std::pair<ShaderType, const sh::InterfaceBlock *>;
495using InterfaceBlockMap = std::map<std::string, ShaderInterfaceBlock>;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800496
Jiawei Shao40786bd2018-04-18 13:58:57 +0800497void InitializeInterfaceBlockMap(const std::vector<sh::InterfaceBlock> &interfaceBlocks,
498 ShaderType shaderType,
499 InterfaceBlockMap *linkedInterfaceBlocks,
500 GLuint *blockCount)
501{
502 ASSERT(linkedInterfaceBlocks && blockCount);
503
504 for (const sh::InterfaceBlock &interfaceBlock : interfaceBlocks)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800505 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800506 (*linkedInterfaceBlocks)[interfaceBlock.name] = std::make_pair(shaderType, &interfaceBlock);
507 if (IsActiveInterfaceBlock(interfaceBlock))
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800508 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800509 *blockCount += std::max(interfaceBlock.arraySize, 1u);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800510 }
511 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800512}
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800513
Jiawei Shao40786bd2018-04-18 13:58:57 +0800514bool ValidateGraphicsInterfaceBlocksPerShader(
515 const std::vector<sh::InterfaceBlock> &interfaceBlocksToLink,
516 ShaderType shaderType,
517 bool webglCompatibility,
518 InterfaceBlockMap *linkedBlocks,
519 GLuint *combinedInterfaceBlockCount,
520 InfoLog &infoLog)
521{
522 ASSERT(linkedBlocks && combinedInterfaceBlockCount);
523
524 for (const sh::InterfaceBlock &block : interfaceBlocksToLink)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800525 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800526 const auto &entry = linkedBlocks->find(block.name);
527 if (entry != linkedBlocks->end())
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800528 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800529 const sh::InterfaceBlock &linkedBlock = *(entry->second.second);
530 std::string mismatchedStructFieldName;
531 LinkMismatchError linkError = AreMatchingInterfaceBlocks(
532 block, linkedBlock, webglCompatibility, &mismatchedStructFieldName);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800533 if (linkError != LinkMismatchError::NO_MISMATCH)
534 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800535 LogLinkMismatch(infoLog, block.name, GetInterfaceBlockTypeString(block.blockType),
536 linkError, mismatchedStructFieldName, entry->second.first,
537 shaderType);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800538 return false;
539 }
540 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800541 else
542 {
543 (*linkedBlocks)[block.name] = std::make_pair(shaderType, &block);
544 }
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800545
546 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
547 // If a uniform block is used by multiple shader stages, each such use counts separately
548 // against this combined limit.
549 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
550 // If a shader storage block in a program is referenced by multiple shaders, each such
551 // reference counts separately against this combined limit.
Jiawei Shao40786bd2018-04-18 13:58:57 +0800552 if (IsActiveInterfaceBlock(block))
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800553 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800554 *combinedInterfaceBlockCount += std::max(block.arraySize, 1u);
555 }
556 }
557
558 return true;
559}
560
561bool ValidateGraphicsInterfaceBlocks(
562 const ShaderMap<const std::vector<sh::InterfaceBlock> *> &shaderInterfaceBlocks,
563 InfoLog &infoLog,
564 bool webglCompatibility,
565 sh::BlockType blockType,
566 GLuint maxCombinedInterfaceBlocks)
567{
568 // Check that interface blocks defined in the graphics shaders are identical
569
570 InterfaceBlockMap linkedInterfaceBlocks;
571 GLuint blockCount = 0u;
572
573 bool interfaceBlockMapInitialized = false;
574 for (ShaderType shaderType : kAllGraphicsShaderTypes)
575 {
576 if (!shaderInterfaceBlocks[shaderType])
577 {
578 continue;
579 }
580
581 if (!interfaceBlockMapInitialized)
582 {
583 InitializeInterfaceBlockMap(*shaderInterfaceBlocks[shaderType], shaderType,
584 &linkedInterfaceBlocks, &blockCount);
585 interfaceBlockMapInitialized = true;
586 }
587 else if (!ValidateGraphicsInterfaceBlocksPerShader(
588 *shaderInterfaceBlocks[shaderType], shaderType, webglCompatibility,
589 &linkedInterfaceBlocks, &blockCount, infoLog))
590 {
591 return false;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800592 }
593 }
594
595 if (blockCount > maxCombinedInterfaceBlocks)
596 {
597 switch (blockType)
598 {
599 case sh::BlockType::BLOCK_UNIFORM:
600 infoLog << "The sum of the number of active uniform blocks exceeds "
601 "MAX_COMBINED_UNIFORM_BLOCKS ("
602 << maxCombinedInterfaceBlocks << ").";
603 break;
604 case sh::BlockType::BLOCK_BUFFER:
605 infoLog << "The sum of the number of active shader storage blocks exceeds "
606 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
607 << maxCombinedInterfaceBlocks << ").";
608 break;
609 default:
610 UNREACHABLE();
611 }
612 return false;
613 }
614 return true;
615}
616
Jamie Madill62d31cb2015-09-11 13:25:51 -0400617} // anonymous namespace
618
Jamie Madill4a3c2342015-10-08 12:58:45 -0400619const char *const g_fakepath = "C:\\fakepath";
620
Jamie Madill3c1da042017-11-27 18:33:40 -0500621// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400622InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000623{
624}
625
626InfoLog::~InfoLog()
627{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000628}
629
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400630size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000631{
Jamie Madill23176ce2017-07-31 14:14:33 -0400632 if (!mLazyStream)
633 {
634 return 0;
635 }
636
637 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400638 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000639}
640
Geoff Lange1a27752015-10-05 13:16:04 -0400641void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000642{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400643 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000644
645 if (bufSize > 0)
646 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400647 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400648
Jamie Madill23176ce2017-07-31 14:14:33 -0400649 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000650 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400651 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
652 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000653 }
654
655 infoLog[index] = '\0';
656 }
657
658 if (length)
659 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400660 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000661 }
662}
663
664// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300665// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000666// messages, so lets remove all occurrences of this fake file path from the log.
667void InfoLog::appendSanitized(const char *message)
668{
Jamie Madill23176ce2017-07-31 14:14:33 -0400669 ensureInitialized();
670
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000671 std::string msg(message);
672
673 size_t found;
674 do
675 {
676 found = msg.find(g_fakepath);
677 if (found != std::string::npos)
678 {
679 msg.erase(found, strlen(g_fakepath));
680 }
681 }
682 while (found != std::string::npos);
683
Jamie Madill23176ce2017-07-31 14:14:33 -0400684 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000685}
686
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000687void InfoLog::reset()
688{
Jiawei Shao02f15232017-12-27 10:10:28 +0800689 if (mLazyStream)
690 {
691 mLazyStream.reset(nullptr);
692 }
693}
694
695bool InfoLog::empty() const
696{
697 if (!mLazyStream)
698 {
699 return true;
700 }
701
702 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000703}
704
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800705void LogLinkMismatch(InfoLog &infoLog,
706 const std::string &variableName,
707 const char *variableType,
708 LinkMismatchError linkError,
709 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800710 ShaderType shaderType1,
711 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800712{
713 std::ostringstream stream;
714 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
715 << variableName;
716
717 if (!mismatchedStructOrBlockFieldName.empty())
718 {
719 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
720 }
721
722 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
723 << GetShaderTypeString(shaderType2) << " shaders.";
724
725 infoLog << stream.str();
726}
727
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800728bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
729{
730 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200731 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800732}
733
Jamie Madill3c1da042017-11-27 18:33:40 -0500734// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300735VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000736{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500737}
738
Olli Etuahoc8538042017-09-27 11:20:15 +0300739VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300740 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500741{
Olli Etuahoc8538042017-09-27 11:20:15 +0300742 ASSERT(arrayIndex != GL_INVALID_INDEX);
743}
744
Jamie Madill3c1da042017-11-27 18:33:40 -0500745// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800746SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500747 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
748{
749}
750
751SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
752
753SamplerBinding::~SamplerBinding() = default;
754
Jamie Madill3c1da042017-11-27 18:33:40 -0500755// ProgramBindings implementation.
756ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500757{
758}
759
Jamie Madill3c1da042017-11-27 18:33:40 -0500760ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500761{
762}
763
Jamie Madill3c1da042017-11-27 18:33:40 -0500764void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400765{
766 mBindings[name] = index;
767}
768
Jamie Madill3c1da042017-11-27 18:33:40 -0500769int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400770{
771 auto iter = mBindings.find(name);
772 return (iter != mBindings.end()) ? iter->second : -1;
773}
774
Jamie Madill3c1da042017-11-27 18:33:40 -0500775ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400776{
777 return mBindings.begin();
778}
779
Jamie Madill3c1da042017-11-27 18:33:40 -0500780ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400781{
782 return mBindings.end();
783}
784
Jamie Madill3c1da042017-11-27 18:33:40 -0500785// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500786ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
787{
788}
789ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
790{
791 for (size_t index = 0; index < count; ++index)
792 {
793 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
794 }
795}
796
797ImageBinding::ImageBinding(const ImageBinding &other) = default;
798
799ImageBinding::~ImageBinding() = default;
800
Jamie Madill3c1da042017-11-27 18:33:40 -0500801// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400802ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500803 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800804 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500805 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400806 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500807 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800808 mImageUniformRange(0, 0),
809 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300810 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800811 mNumViews(-1),
812 // [GL_EXT_geometry_shader] Table 20.22
813 mGeometryShaderInputPrimitiveType(GL_TRIANGLES),
814 mGeometryShaderOutputPrimitiveType(GL_TRIANGLE_STRIP),
815 mGeometryShaderInvocations(1),
816 mGeometryShaderMaxVertices(0)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400817{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300818 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400819}
820
Jamie Madill48ef11b2016-04-27 15:21:52 -0400821ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400822{
Jiawei Shao016105b2018-04-12 16:38:31 +0800823 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400824}
825
Jamie Madill48ef11b2016-04-27 15:21:52 -0400826const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500827{
828 return mLabel;
829}
830
Jiawei Shao385b3e02018-03-21 09:43:28 +0800831Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
832{
Jiawei Shao016105b2018-04-12 16:38:31 +0800833 ASSERT(shaderType != ShaderType::InvalidEnum);
834 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800835}
836
Jamie Madille7d84322017-01-10 18:21:59 -0500837GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400838{
jchen1015015f72017-03-16 13:54:21 +0800839 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400840}
841
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800842GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
843{
844 return GetResourceIndexFromName(mBufferVariables, name);
845}
846
Jamie Madille7d84322017-01-10 18:21:59 -0500847GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
848{
849 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
850 return mUniformLocations[location].index;
851}
852
853Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
854{
855 GLuint index = getUniformIndexFromLocation(location);
856 if (!isSamplerUniformIndex(index))
857 {
858 return Optional<GLuint>::Invalid();
859 }
860
861 return getSamplerIndexFromUniformIndex(index);
862}
863
864bool ProgramState::isSamplerUniformIndex(GLuint index) const
865{
Jamie Madill982f6e02017-06-07 14:33:04 -0400866 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500867}
868
869GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
870{
871 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400872 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500873}
874
Jamie Madill34ca4f52017-06-13 11:49:39 -0400875GLuint ProgramState::getAttributeLocation(const std::string &name) const
876{
877 for (const sh::Attribute &attribute : mAttributes)
878 {
879 if (attribute.name == name)
880 {
881 return attribute.location;
882 }
883 }
884
885 return static_cast<GLuint>(-1);
886}
887
Jiawei Shao016105b2018-04-12 16:38:31 +0800888bool ProgramState::hasAttachedShader() const
889{
890 for (const Shader *shader : mAttachedShaders)
891 {
892 if (shader)
893 {
894 return true;
895 }
896 }
897 return false;
898}
899
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500900Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400901 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400902 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500903 mLinked(false),
904 mDeleteStatus(false),
905 mRefCount(0),
906 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500907 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500908{
909 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000910
Geoff Lang7dd2e102014-11-10 15:19:26 -0500911 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912}
913
914Program::~Program()
915{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400916 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
Jamie Madill4928b7c2017-06-20 12:57:39 -0400919void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500920{
Jiawei Shao016105b2018-04-12 16:38:31 +0800921 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500922 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800923 if (mState.mAttachedShaders[shaderType])
924 {
925 mState.mAttachedShaders[shaderType]->release(context);
926 mState.mAttachedShaders[shaderType] = nullptr;
927 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800928 }
929
Jamie Madillb7d924a2018-03-10 11:16:54 -0500930 // TODO(jmadill): Handle error in the Context.
931 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400932
Jiawei Shao016105b2018-04-12 16:38:31 +0800933 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400934 SafeDelete(mProgram);
935
936 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500937}
938
Geoff Lang70d0f492015-12-10 17:45:46 -0500939void Program::setLabel(const std::string &label)
940{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400941 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500942}
943
944const std::string &Program::getLabel() const
945{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400946 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500947}
948
Jamie Madillef300b12016-10-07 15:12:09 -0400949void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950{
Jiawei Shao016105b2018-04-12 16:38:31 +0800951 ShaderType shaderType = shader->getType();
952 ASSERT(shaderType != ShaderType::InvalidEnum);
953
954 mState.mAttachedShaders[shaderType] = shader;
955 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
Jamie Madillc1d770e2017-04-13 17:31:24 -0400958void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959{
Jiawei Shao016105b2018-04-12 16:38:31 +0800960 ShaderType shaderType = shader->getType();
961 ASSERT(shaderType != ShaderType::InvalidEnum);
962
963 ASSERT(mState.mAttachedShaders[shaderType] == shader);
964 shader->release(context);
965 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000968int Program::getAttachedShadersCount() const
969{
Jiawei Shao016105b2018-04-12 16:38:31 +0800970 int numAttachedShaders = 0;
971 for (const Shader *shader : mState.mAttachedShaders)
972 {
973 if (shader)
974 {
975 ++numAttachedShaders;
976 }
977 }
978
979 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000980}
981
Jiawei Shao385b3e02018-03-21 09:43:28 +0800982const Shader *Program::getAttachedShader(ShaderType shaderType) const
983{
984 return mState.getAttachedShader(shaderType);
985}
986
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987void Program::bindAttributeLocation(GLuint index, const char *name)
988{
Geoff Langd8605522016-04-13 10:19:12 -0400989 mAttributeBindings.bindLocation(index, name);
990}
991
992void Program::bindUniformLocation(GLuint index, const char *name)
993{
Olli Etuahod2551232017-10-26 20:03:33 +0300994 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995}
996
Sami Väisänen46eaa942016-06-29 10:26:37 +0300997void Program::bindFragmentInputLocation(GLint index, const char *name)
998{
999 mFragmentInputBindings.bindLocation(index, name);
1000}
1001
Jamie Madillbd044ed2017-06-05 12:59:21 -04001002BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001003{
1004 BindingInfo ret;
1005 ret.type = GL_NONE;
1006 ret.valid = false;
1007
Jiawei Shao385b3e02018-03-21 09:43:28 +08001008 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001009 ASSERT(fragmentShader);
1010
1011 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +08001012 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001013
1014 for (const auto &binding : mFragmentInputBindings)
1015 {
1016 if (binding.second != static_cast<GLuint>(index))
1017 continue;
1018
1019 ret.valid = true;
1020
Olli Etuahod2551232017-10-26 20:03:33 +03001021 size_t nameLengthWithoutArrayIndex;
1022 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001023
1024 for (const auto &in : inputs)
1025 {
Olli Etuahod2551232017-10-26 20:03:33 +03001026 if (in.name.length() == nameLengthWithoutArrayIndex &&
1027 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001028 {
1029 if (in.isArray())
1030 {
1031 // The client wants to bind either "name" or "name[0]".
1032 // GL ES 3.1 spec refers to active array names with language such as:
1033 // "if the string identifies the base name of an active array, where the
1034 // string would exactly match the name of the variable if the suffix "[0]"
1035 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001036 if (arrayIndex == GL_INVALID_INDEX)
1037 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001038
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001039 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001040 }
1041 else
1042 {
1043 ret.name = in.mappedName;
1044 }
1045 ret.type = in.type;
1046 return ret;
1047 }
1048 }
1049 }
1050
1051 return ret;
1052}
1053
Jamie Madillbd044ed2017-06-05 12:59:21 -04001054void Program::pathFragmentInputGen(const Context *context,
1055 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001056 GLenum genMode,
1057 GLint components,
1058 const GLfloat *coeffs)
1059{
1060 // If the location is -1 then the command is silently ignored
1061 if (index == -1)
1062 return;
1063
Jamie Madillbd044ed2017-06-05 12:59:21 -04001064 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001065
1066 // If the input doesn't exist then then the command is silently ignored
1067 // This could happen through optimization for example, the shader translator
1068 // decides that a variable is not actually being used and optimizes it away.
1069 if (binding.name.empty())
1070 return;
1071
1072 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1073}
1074
Martin Radev4c4c8e72016-08-04 12:25:34 +03001075// The attached shaders are checked for linking errors by matching up their variables.
1076// Uniform, input and output variables get collected.
1077// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001078Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001079{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001080 const auto &data = context->getContextState();
1081
Jamie Madill6c58b062017-08-01 13:44:25 -04001082 auto *platform = ANGLEPlatformCurrent();
1083 double startTime = platform->currentTime(platform);
1084
Jamie Madill6c1f6712017-02-14 19:08:04 -05001085 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001086 mInfoLog.reset();
1087
1088 // Validate we have properly attached shaders before checking the cache.
1089 if (!linkValidateShaders(context, mInfoLog))
1090 {
1091 return NoError();
1092 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001093
Jamie Madill32447362017-06-28 14:53:52 -04001094 ProgramHash programHash;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001095 MemoryProgramCache *cache = context->getMemoryProgramCache();
Jamie Madill32447362017-06-28 14:53:52 -04001096 if (cache)
1097 {
1098 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001099 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001100 }
1101
1102 if (mLinked)
1103 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001104 double delta = platform->currentTime(platform) - startTime;
1105 int us = static_cast<int>(delta * 1000000.0);
1106 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001107 return NoError();
1108 }
1109
1110 // Cache load failed, fall through to normal linking.
1111 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001112
Jamie Madill6bc264a2018-03-31 15:36:05 -04001113 // Re-link shaders after the unlink call.
1114 ASSERT(linkValidateShaders(context, mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001115
Jiawei Shao016105b2018-04-12 16:38:31 +08001116 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001117 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001118 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001119 {
1120 return NoError();
1121 }
1122
Jiajia Qin729b2c62017-08-14 09:36:11 +08001123 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001124 {
1125 return NoError();
1126 }
1127
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001128 ProgramLinkedResources resources = {
1129 {0, PackMode::ANGLE_RELAXED},
1130 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001131 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1132 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001133
1134 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1135 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1136
1137 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001138 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001139 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001140 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001141 }
1142 }
1143 else
1144 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001145 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001146 {
1147 return NoError();
1148 }
1149
Jamie Madillbd044ed2017-06-05 12:59:21 -04001150 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001151 {
1152 return NoError();
1153 }
1154
Jamie Madillbd044ed2017-06-05 12:59:21 -04001155 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001156 {
1157 return NoError();
1158 }
1159
Jiajia Qin729b2c62017-08-14 09:36:11 +08001160 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001161 {
1162 return NoError();
1163 }
1164
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001165 if (!linkValidateGlobalNames(context, mInfoLog))
1166 {
1167 return NoError();
1168 }
1169
Jamie Madillbd044ed2017-06-05 12:59:21 -04001170 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001171
Jiawei Shao016105b2018-04-12 16:38:31 +08001172 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
1173 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +03001174
Jamie Madillbd044ed2017-06-05 12:59:21 -04001175 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001176
Jamie Madill192745a2016-12-22 15:58:21 -05001177 // Map the varyings to the register file
1178 // In WebGL, we use a slightly different handling for packing variables.
Jamie Madill61d53252018-01-31 14:49:24 -05001179 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1180 if (data.getLimitations().noFlexibleVaryingPacking)
1181 {
1182 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1183 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1184 }
1185 else if (data.getExtensions().webglCompatibility)
1186 {
1187 packMode = PackMode::WEBGL_STRICT;
1188 }
Jamie Madillc9727f32017-11-07 12:37:07 -05001189
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001190 ProgramLinkedResources resources = {
1191 {data.getCaps().maxVaryingVectors, packMode},
1192 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001193 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1194 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001195
1196 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1197 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1198
Jiawei Shao73618602017-12-20 15:47:15 +08001199 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001200 {
1201 return NoError();
1202 }
1203
jchen1085c93c42017-11-12 15:36:47 +08001204 if (!resources.varyingPacking.collectAndPackUserVaryings(
1205 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001206 {
1207 return NoError();
1208 }
1209
Jamie Madillc9727f32017-11-07 12:37:07 -05001210 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001211 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001212 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001213 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001214 }
1215
1216 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001217 }
1218
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001219 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001220
jchen10eaef1e52017-06-13 10:44:11 +08001221 setUniformValuesFromBindingQualifiers();
1222
Yunchao Heece12532017-11-21 15:50:21 +08001223 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1224 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001225 ASSERT(mLinked);
1226 updateLinkedShaderStages();
1227
Jamie Madill54164b02017-08-28 15:17:37 -04001228 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001229 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001230
Jamie Madill32447362017-06-28 14:53:52 -04001231 // Save to the program cache.
1232 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
1233 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
1234 {
1235 cache->putProgram(programHash, context, this);
1236 }
1237
Jamie Madill6c58b062017-08-01 13:44:25 -04001238 double delta = platform->currentTime(platform) - startTime;
1239 int us = static_cast<int>(delta * 1000000.0);
1240 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
1241
Martin Radev4c4c8e72016-08-04 12:25:34 +03001242 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001243}
1244
Yunchao He85072e82017-11-14 15:43:28 +08001245void Program::updateLinkedShaderStages()
1246{
Yunchao Heece12532017-11-21 15:50:21 +08001247 mState.mLinkedShaderStages.reset();
1248
Jiawei Shao016105b2018-04-12 16:38:31 +08001249 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001250 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001251 if (shader)
1252 {
1253 mState.mLinkedShaderStages.set(shader->getType());
1254 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001255 }
Yunchao He85072e82017-11-14 15:43:28 +08001256}
1257
James Darpinian30b604d2018-03-12 17:26:57 -07001258void ProgramState::updateTransformFeedbackStrides()
1259{
1260 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1261 {
1262 mTransformFeedbackStrides.resize(1);
1263 size_t totalSize = 0;
1264 for (auto &varying : mLinkedTransformFeedbackVaryings)
1265 {
1266 totalSize += varying.size() * VariableExternalSize(varying.type);
1267 }
1268 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1269 }
1270 else
1271 {
1272 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1273 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1274 {
1275 auto &varying = mLinkedTransformFeedbackVaryings[i];
1276 mTransformFeedbackStrides[i] =
1277 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1278 }
1279 }
1280}
1281
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001282// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001283void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001284{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001285 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001286 mState.mAttributesTypeMask.reset();
1287 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001288 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001289 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001290 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001291 mState.mUniforms.clear();
1292 mState.mUniformLocations.clear();
1293 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001294 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001295 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001296 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001297 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001298 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001299 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001300 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001301 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001302 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001303 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001304 mState.mNumViews = -1;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001305 mState.mGeometryShaderInputPrimitiveType = GL_TRIANGLES;
1306 mState.mGeometryShaderOutputPrimitiveType = GL_TRIANGLE_STRIP;
1307 mState.mGeometryShaderInvocations = 1;
1308 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310 mValidated = false;
1311
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001312 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001313 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001314}
1315
Geoff Lange1a27752015-10-05 13:16:04 -04001316bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001317{
1318 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319}
1320
Jiawei Shao385b3e02018-03-21 09:43:28 +08001321bool Program::hasLinkedShaderStage(ShaderType shaderType) const
1322{
1323 ASSERT(shaderType != ShaderType::InvalidEnum);
1324 return mState.mLinkedShaderStages[shaderType];
1325}
1326
Jamie Madilla2c74982016-12-12 11:20:42 -05001327Error Program::loadBinary(const Context *context,
1328 GLenum binaryFormat,
1329 const void *binary,
1330 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001331{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001332 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001333
Geoff Lang7dd2e102014-11-10 15:19:26 -05001334#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001335 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001336#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001337 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1338 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001339 {
Jamie Madillf6113162015-05-07 11:49:21 -04001340 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001341 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342 }
1343
Jamie Madill4f86d052017-06-05 12:59:26 -04001344 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1345 ANGLE_TRY_RESULT(
1346 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001347
1348 // Currently we require the full shader text to compute the program hash.
1349 // TODO(jmadill): Store the binary in the internal program cache.
1350
Jamie Madillb0a838b2016-11-13 20:02:12 -05001351 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001352#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
Jamie Madilla2c74982016-12-12 11:20:42 -05001355Error Program::saveBinary(const Context *context,
1356 GLenum *binaryFormat,
1357 void *binary,
1358 GLsizei bufSize,
1359 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001360{
1361 if (binaryFormat)
1362 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001363 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364 }
1365
Jamie Madill4f86d052017-06-05 12:59:26 -04001366 angle::MemoryBuffer memoryBuf;
1367 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368
Jamie Madill4f86d052017-06-05 12:59:26 -04001369 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1370 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371
1372 if (streamLength > bufSize)
1373 {
1374 if (length)
1375 {
1376 *length = 0;
1377 }
1378
1379 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1380 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1381 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001382 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383 }
1384
1385 if (binary)
1386 {
1387 char *ptr = reinterpret_cast<char*>(binary);
1388
Jamie Madill48ef11b2016-04-27 15:21:52 -04001389 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390 ptr += streamLength;
1391
1392 ASSERT(ptr - streamLength == binary);
1393 }
1394
1395 if (length)
1396 {
1397 *length = streamLength;
1398 }
1399
He Yunchaoacd18982017-01-04 10:46:42 +08001400 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401}
1402
Jamie Madillffe00c02017-06-27 16:26:55 -04001403GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404{
1405 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001406 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407 if (error.isError())
1408 {
1409 return 0;
1410 }
1411
1412 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001413}
1414
Geoff Langc5629752015-12-07 16:29:04 -05001415void Program::setBinaryRetrievableHint(bool retrievable)
1416{
1417 // TODO(jmadill) : replace with dirty bits
1418 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001419 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001420}
1421
1422bool Program::getBinaryRetrievableHint() const
1423{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001424 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001425}
1426
Yunchao He61afff12017-03-14 15:34:03 +08001427void Program::setSeparable(bool separable)
1428{
1429 // TODO(yunchao) : replace with dirty bits
1430 if (mState.mSeparable != separable)
1431 {
1432 mProgram->setSeparable(separable);
1433 mState.mSeparable = separable;
1434 }
1435}
1436
1437bool Program::isSeparable() const
1438{
1439 return mState.mSeparable;
1440}
1441
Jamie Madill6c1f6712017-02-14 19:08:04 -05001442void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001443{
1444 mRefCount--;
1445
1446 if (mRefCount == 0 && mDeleteStatus)
1447 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001448 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001449 }
1450}
1451
1452void Program::addRef()
1453{
1454 mRefCount++;
1455}
1456
1457unsigned int Program::getRefCount() const
1458{
1459 return mRefCount;
1460}
1461
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001462int Program::getInfoLogLength() const
1463{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001464 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001465}
1466
Geoff Lange1a27752015-10-05 13:16:04 -04001467void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001468{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001469 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001470}
1471
Geoff Lange1a27752015-10-05 13:16:04 -04001472void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001473{
1474 int total = 0;
1475
Jiawei Shao016105b2018-04-12 16:38:31 +08001476 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001477 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001478 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001479 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001480 shaders[total] = shader->getHandle();
1481 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001482 }
1483 }
1484
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001485 if (count)
1486 {
1487 *count = total;
1488 }
1489}
1490
Geoff Lange1a27752015-10-05 13:16:04 -04001491GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001493 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494}
1495
Jamie Madill63805b42015-08-25 13:17:39 -04001496bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001497{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001498 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1499 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001500}
1501
jchen10fd7c3b52017-03-21 15:36:03 +08001502void Program::getActiveAttribute(GLuint index,
1503 GLsizei bufsize,
1504 GLsizei *length,
1505 GLint *size,
1506 GLenum *type,
1507 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001508{
Jamie Madillc349ec02015-08-21 16:53:12 -04001509 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001510 {
1511 if (bufsize > 0)
1512 {
1513 name[0] = '\0';
1514 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001515
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001516 if (length)
1517 {
1518 *length = 0;
1519 }
1520
1521 *type = GL_NONE;
1522 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001523 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001524 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001525
jchen1036e120e2017-03-14 14:53:58 +08001526 ASSERT(index < mState.mAttributes.size());
1527 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001528
1529 if (bufsize > 0)
1530 {
jchen10fd7c3b52017-03-21 15:36:03 +08001531 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001532 }
1533
1534 // Always a single 'type' instance
1535 *size = 1;
1536 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001537}
1538
Geoff Lange1a27752015-10-05 13:16:04 -04001539GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001540{
Jamie Madillc349ec02015-08-21 16:53:12 -04001541 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001542 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001543 return 0;
1544 }
1545
jchen1036e120e2017-03-14 14:53:58 +08001546 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001547}
1548
Geoff Lange1a27752015-10-05 13:16:04 -04001549GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001550{
Jamie Madillc349ec02015-08-21 16:53:12 -04001551 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001552 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001553 return 0;
1554 }
1555
1556 size_t maxLength = 0;
1557
Jamie Madill48ef11b2016-04-27 15:21:52 -04001558 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001559 {
jchen1036e120e2017-03-14 14:53:58 +08001560 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001561 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562
Jamie Madillc349ec02015-08-21 16:53:12 -04001563 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564}
1565
jchen1015015f72017-03-16 13:54:21 +08001566GLuint Program::getInputResourceIndex(const GLchar *name) const
1567{
Olli Etuahod2551232017-10-26 20:03:33 +03001568 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001569}
1570
1571GLuint Program::getOutputResourceIndex(const GLchar *name) const
1572{
1573 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1574}
1575
jchen10fd7c3b52017-03-21 15:36:03 +08001576size_t Program::getOutputResourceCount() const
1577{
1578 return (mLinked ? mState.mOutputVariables.size() : 0);
1579}
1580
jchen10baf5d942017-08-28 20:45:48 +08001581template <typename T>
1582void Program::getResourceName(GLuint index,
1583 const std::vector<T> &resources,
1584 GLsizei bufSize,
1585 GLsizei *length,
1586 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001587{
1588 if (length)
1589 {
1590 *length = 0;
1591 }
1592
1593 if (!mLinked)
1594 {
1595 if (bufSize > 0)
1596 {
1597 name[0] = '\0';
1598 }
1599 return;
1600 }
jchen10baf5d942017-08-28 20:45:48 +08001601 ASSERT(index < resources.size());
1602 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001603
1604 if (bufSize > 0)
1605 {
Olli Etuahod2551232017-10-26 20:03:33 +03001606 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001607 }
1608}
1609
jchen10baf5d942017-08-28 20:45:48 +08001610void Program::getInputResourceName(GLuint index,
1611 GLsizei bufSize,
1612 GLsizei *length,
1613 GLchar *name) const
1614{
1615 getResourceName(index, mState.mAttributes, bufSize, length, name);
1616}
1617
1618void Program::getOutputResourceName(GLuint index,
1619 GLsizei bufSize,
1620 GLsizei *length,
1621 GLchar *name) const
1622{
1623 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1624}
1625
1626void Program::getUniformResourceName(GLuint index,
1627 GLsizei bufSize,
1628 GLsizei *length,
1629 GLchar *name) const
1630{
1631 getResourceName(index, mState.mUniforms, bufSize, length, name);
1632}
1633
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001634void Program::getBufferVariableResourceName(GLuint index,
1635 GLsizei bufSize,
1636 GLsizei *length,
1637 GLchar *name) const
1638{
1639 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1640}
1641
jchen10880683b2017-04-12 16:21:55 +08001642const sh::Attribute &Program::getInputResource(GLuint index) const
1643{
1644 ASSERT(index < mState.mAttributes.size());
1645 return mState.mAttributes[index];
1646}
1647
1648const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1649{
1650 ASSERT(index < mState.mOutputVariables.size());
1651 return mState.mOutputVariables[index];
1652}
1653
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654GLint Program::getFragDataLocation(const std::string &name) const
1655{
Olli Etuahod2551232017-10-26 20:03:33 +03001656 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001657}
1658
Geoff Lange1a27752015-10-05 13:16:04 -04001659void Program::getActiveUniform(GLuint index,
1660 GLsizei bufsize,
1661 GLsizei *length,
1662 GLint *size,
1663 GLenum *type,
1664 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001665{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001667 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001668 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001669 ASSERT(index < mState.mUniforms.size());
1670 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671
1672 if (bufsize > 0)
1673 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001675 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676 }
1677
Olli Etuaho465835d2017-09-26 13:34:10 +03001678 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001680 }
1681 else
1682 {
1683 if (bufsize > 0)
1684 {
1685 name[0] = '\0';
1686 }
1687
1688 if (length)
1689 {
1690 *length = 0;
1691 }
1692
1693 *size = 0;
1694 *type = GL_NONE;
1695 }
1696}
1697
Geoff Lange1a27752015-10-05 13:16:04 -04001698GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001701 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001702 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001703 }
1704 else
1705 {
1706 return 0;
1707 }
1708}
1709
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001710size_t Program::getActiveBufferVariableCount() const
1711{
1712 return mLinked ? mState.mBufferVariables.size() : 0;
1713}
1714
Geoff Lange1a27752015-10-05 13:16:04 -04001715GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001716{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718
1719 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001720 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001723 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001725 size_t length = uniform.name.length() + 1u;
1726 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001727 {
1728 length += 3; // Counting in "[0]".
1729 }
1730 maxLength = std::max(length, maxLength);
1731 }
1732 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001733 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001734
Jamie Madill62d31cb2015-09-11 13:25:51 -04001735 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001736}
1737
Geoff Lang7dd2e102014-11-10 15:19:26 -05001738bool Program::isValidUniformLocation(GLint location) const
1739{
Jamie Madille2e406c2016-06-02 13:04:10 -04001740 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001741 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001742 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001743}
1744
Jamie Madill62d31cb2015-09-11 13:25:51 -04001745const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001746{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001747 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001748 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001749}
1750
Jamie Madillac4e9c32017-01-13 14:07:12 -05001751const VariableLocation &Program::getUniformLocation(GLint location) const
1752{
1753 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1754 return mState.mUniformLocations[location];
1755}
1756
1757const std::vector<VariableLocation> &Program::getUniformLocations() const
1758{
1759 return mState.mUniformLocations;
1760}
1761
1762const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1763{
1764 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1765 return mState.mUniforms[index];
1766}
1767
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001768const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1769{
1770 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1771 return mState.mBufferVariables[index];
1772}
1773
Jamie Madill62d31cb2015-09-11 13:25:51 -04001774GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775{
Olli Etuahod2551232017-10-26 20:03:33 +03001776 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777}
1778
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780{
Jamie Madille7d84322017-01-10 18:21:59 -05001781 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782}
1783
1784void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1785{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001786 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1787 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001788 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789}
1790
1791void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1792{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001793 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1794 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001795 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001796}
1797
1798void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1799{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001800 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1801 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001802 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803}
1804
1805void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1806{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001807 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1808 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001809 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810}
1811
Jamie Madill81c2e252017-09-09 23:32:46 -04001812Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001814 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1815 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1816
Jamie Madill81c2e252017-09-09 23:32:46 -04001817 mProgram->setUniform1iv(location, clampedCount, v);
1818
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001819 if (mState.isSamplerUniformIndex(locationInfo.index))
1820 {
1821 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001822 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001823 }
1824
Jamie Madill81c2e252017-09-09 23:32:46 -04001825 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826}
1827
1828void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1829{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001830 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1831 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001832 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833}
1834
1835void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1836{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001837 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1838 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001839 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840}
1841
1842void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1843{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001844 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1845 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001846 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847}
1848
1849void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1850{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001851 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1852 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001853 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854}
1855
1856void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1857{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001858 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1859 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001860 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861}
1862
1863void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1864{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001865 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1866 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001867 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868}
1869
1870void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1871{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001872 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1873 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001874 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875}
1876
1877void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1878{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001879 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001880 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881}
1882
1883void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1884{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001885 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001886 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001887}
1888
1889void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1890{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001891 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001892 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893}
1894
1895void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1896{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001897 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001898 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001899}
1900
1901void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1902{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001903 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001904 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905}
1906
1907void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1908{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001909 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001910 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001911}
1912
1913void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1914{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001915 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001916 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917}
1918
1919void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1920{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001921 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001922 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923}
1924
1925void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1926{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001927 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001928 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929}
1930
Jamie Madill54164b02017-08-28 15:17:37 -04001931void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932{
Jamie Madill54164b02017-08-28 15:17:37 -04001933 const auto &uniformLocation = mState.getUniformLocations()[location];
1934 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1935
1936 GLenum nativeType = gl::VariableComponentType(uniform.type);
1937 if (nativeType == GL_FLOAT)
1938 {
1939 mProgram->getUniformfv(context, location, v);
1940 }
1941 else
1942 {
1943 getUniformInternal(context, v, location, nativeType,
1944 gl::VariableComponentCount(uniform.type));
1945 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001946}
1947
Jamie Madill54164b02017-08-28 15:17:37 -04001948void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949{
Jamie Madill54164b02017-08-28 15:17:37 -04001950 const auto &uniformLocation = mState.getUniformLocations()[location];
1951 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1952
1953 GLenum nativeType = gl::VariableComponentType(uniform.type);
1954 if (nativeType == GL_INT || nativeType == GL_BOOL)
1955 {
1956 mProgram->getUniformiv(context, location, v);
1957 }
1958 else
1959 {
1960 getUniformInternal(context, v, location, nativeType,
1961 gl::VariableComponentCount(uniform.type));
1962 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001963}
1964
Jamie Madill54164b02017-08-28 15:17:37 -04001965void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966{
Jamie Madill54164b02017-08-28 15:17:37 -04001967 const auto &uniformLocation = mState.getUniformLocations()[location];
1968 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1969
1970 GLenum nativeType = gl::VariableComponentType(uniform.type);
1971 if (nativeType == GL_UNSIGNED_INT)
1972 {
1973 mProgram->getUniformuiv(context, location, v);
1974 }
1975 else
1976 {
1977 getUniformInternal(context, v, location, nativeType,
1978 gl::VariableComponentCount(uniform.type));
1979 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980}
1981
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982void Program::flagForDeletion()
1983{
1984 mDeleteStatus = true;
1985}
1986
1987bool Program::isFlaggedForDeletion() const
1988{
1989 return mDeleteStatus;
1990}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001991
Brandon Jones43a53e22014-08-28 16:23:22 -07001992void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001993{
1994 mInfoLog.reset();
1995
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001997 {
Geoff Lang92019432017-11-20 13:09:34 -05001998 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001999 }
2000 else
2001 {
Jamie Madillf6113162015-05-07 11:49:21 -04002002 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002003 }
2004}
2005
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
2007{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002008 // Skip cache if we're using an infolog, so we get the full error.
2009 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
2010 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
2011 {
2012 return mCachedValidateSamplersResult.value();
2013 }
2014
2015 if (mTextureUnitTypesCache.empty())
2016 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002017 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002018 }
2019 else
2020 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002021 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(),
2022 TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002023 }
2024
2025 // if any two active samplers in a program are of different types, but refer to the same
2026 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2027 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05002028 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002029 {
Jamie Madill54164b02017-08-28 15:17:37 -04002030 if (samplerBinding.unreferenced)
2031 continue;
2032
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002033 TextureType textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002034
Jamie Madille7d84322017-01-10 18:21:59 -05002035 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002036 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002037 if (textureUnit >= caps.maxCombinedTextureImageUnits)
2038 {
2039 if (infoLog)
2040 {
2041 (*infoLog) << "Sampler uniform (" << textureUnit
2042 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
2043 << caps.maxCombinedTextureImageUnits << ")";
2044 }
2045
2046 mCachedValidateSamplersResult = false;
2047 return false;
2048 }
2049
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002050 if (mTextureUnitTypesCache[textureUnit] != TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002051 {
2052 if (textureType != mTextureUnitTypesCache[textureUnit])
2053 {
2054 if (infoLog)
2055 {
2056 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2057 "image unit ("
2058 << textureUnit << ").";
2059 }
2060
2061 mCachedValidateSamplersResult = false;
2062 return false;
2063 }
2064 }
2065 else
2066 {
2067 mTextureUnitTypesCache[textureUnit] = textureType;
2068 }
2069 }
2070 }
2071
2072 mCachedValidateSamplersResult = true;
2073 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074}
2075
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002076bool Program::isValidated() const
2077{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 return mValidated;
2079}
2080
Geoff Lange1a27752015-10-05 13:16:04 -04002081GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002083 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084}
2085
jchen1058f67be2017-10-27 08:59:27 +08002086GLuint Program::getActiveAtomicCounterBufferCount() const
2087{
2088 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2089}
2090
Jiajia Qin729b2c62017-08-14 09:36:11 +08002091GLuint Program::getActiveShaderStorageBlockCount() const
2092{
2093 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2094}
2095
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002096void Program::getActiveUniformBlockName(const GLuint blockIndex,
2097 GLsizei bufSize,
2098 GLsizei *length,
2099 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002101 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2102}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002104void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2105 GLsizei bufSize,
2106 GLsizei *length,
2107 GLchar *blockName) const
2108{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002110 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002111}
2112
Qin Jiajia9bf55522018-01-29 13:56:23 +08002113template <typename T>
2114GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002115{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116 int maxLength = 0;
2117
2118 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002119 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002120 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002122 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002124 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002125 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 }
2127 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002128 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129
2130 return maxLength;
2131}
2132
Qin Jiajia9bf55522018-01-29 13:56:23 +08002133GLint Program::getActiveUniformBlockMaxNameLength() const
2134{
2135 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2136}
2137
2138GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2139{
2140 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2141}
2142
Geoff Lange1a27752015-10-05 13:16:04 -04002143GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002145 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2146}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002147
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002148GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2149{
2150 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002151}
2152
Jiajia Qin729b2c62017-08-14 09:36:11 +08002153const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002154{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002155 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2156 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002157}
2158
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002159const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2160{
2161 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2162 return mState.mShaderStorageBlocks[index];
2163}
2164
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002165void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2166{
jchen107a20b972017-06-13 14:25:26 +08002167 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002168 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04002169 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002170}
2171
2172GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2173{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002174 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002175}
2176
Jiajia Qin729b2c62017-08-14 09:36:11 +08002177GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2178{
2179 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2180}
2181
Geoff Lang48dcae72014-02-05 16:28:24 -05002182void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
2183{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002184 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002185 for (GLsizei i = 0; i < count; i++)
2186 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002187 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002188 }
2189
Jamie Madill48ef11b2016-04-27 15:21:52 -04002190 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002191}
2192
2193void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
2194{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002195 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002196 {
jchen10a9042d32017-03-17 08:50:45 +08002197 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2198 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2199 std::string varName = var.nameWithArrayIndex();
2200 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002201 if (length)
2202 {
2203 *length = lastNameIdx;
2204 }
2205 if (size)
2206 {
jchen10a9042d32017-03-17 08:50:45 +08002207 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002208 }
2209 if (type)
2210 {
jchen10a9042d32017-03-17 08:50:45 +08002211 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002212 }
2213 if (name)
2214 {
jchen10a9042d32017-03-17 08:50:45 +08002215 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002216 name[lastNameIdx] = '\0';
2217 }
2218 }
2219}
2220
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002221GLsizei Program::getTransformFeedbackVaryingCount() const
2222{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002223 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002224 {
jchen10a9042d32017-03-17 08:50:45 +08002225 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002226 }
2227 else
2228 {
2229 return 0;
2230 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002231}
2232
2233GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2234{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002236 {
2237 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002238 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002239 {
jchen10a9042d32017-03-17 08:50:45 +08002240 maxSize =
2241 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002242 }
2243
2244 return maxSize;
2245 }
2246 else
2247 {
2248 return 0;
2249 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002250}
2251
2252GLenum Program::getTransformFeedbackBufferMode() const
2253{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002254 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255}
2256
Jiawei Shao73618602017-12-20 15:47:15 +08002257bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
2258{
Jiawei Shao016105b2018-04-12 16:38:31 +08002259 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2260 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2261 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2262 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002263
2264 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002265 bool isGraphicsShaderAttached =
2266 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002267 // Check whether we both have a compute and non-compute shaders attached.
2268 // If there are of both types attached, then linking should fail.
2269 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2270 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2271 {
2272 infoLog << "Both compute and graphics shaders are attached to the same program.";
2273 return false;
2274 }
2275
2276 if (computeShader)
2277 {
2278 if (!computeShader->isCompiled(context))
2279 {
2280 infoLog << "Attached compute shader is not compiled.";
2281 return false;
2282 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002283 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002284
2285 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
2286
2287 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2288 // If the work group size is not specified, a link time error should occur.
2289 if (!mState.mComputeShaderLocalSize.isDeclared())
2290 {
2291 infoLog << "Work group size is not specified.";
2292 return false;
2293 }
2294 }
2295 else
2296 {
2297 if (!fragmentShader || !fragmentShader->isCompiled(context))
2298 {
2299 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2300 return false;
2301 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002302 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002303
2304 if (!vertexShader || !vertexShader->isCompiled(context))
2305 {
2306 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2307 return false;
2308 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002309 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002310
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002311 int vertexShaderVersion = vertexShader->getShaderVersion(context);
2312 if (fragmentShader->getShaderVersion(context) != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002313 {
2314 infoLog << "Fragment shader version does not match vertex shader version.";
2315 return false;
2316 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002317
2318 if (geometryShader)
2319 {
2320 // [GL_EXT_geometry_shader] Chapter 7
2321 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2322 // Language Specification, as well as any of the following reasons:
2323 // * One or more of the shader objects attached to <program> are not compiled
2324 // successfully.
2325 // * The shaders do not use the same shader language version.
2326 // * <program> contains objects to form a geometry shader, and
2327 // - <program> is not separable and contains no objects to form a vertex shader; or
2328 // - the input primitive type, output primitive type, or maximum output vertex count
2329 // is not specified in the compiled geometry shader object.
2330 if (!geometryShader->isCompiled(context))
2331 {
2332 infoLog << "The attached geometry shader isn't compiled.";
2333 return false;
2334 }
2335
2336 if (geometryShader->getShaderVersion(context) != vertexShaderVersion)
2337 {
2338 mInfoLog << "Geometry shader version does not match vertex shader version.";
2339 return false;
2340 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002341 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002342
2343 Optional<GLenum> inputPrimitive =
2344 geometryShader->getGeometryShaderInputPrimitiveType(context);
2345 if (!inputPrimitive.valid())
2346 {
2347 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2348 return false;
2349 }
2350
2351 Optional<GLenum> outputPrimitive =
2352 geometryShader->getGeometryShaderOutputPrimitiveType(context);
2353 if (!outputPrimitive.valid())
2354 {
2355 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2356 return false;
2357 }
2358
2359 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices(context);
2360 if (!maxVertices.valid())
2361 {
2362 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2363 return false;
2364 }
2365
2366 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2367 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2368 mState.mGeometryShaderMaxVertices = maxVertices.value();
2369 mState.mGeometryShaderInvocations =
2370 geometryShader->getGeometryShaderInvocations(context);
2371 }
Jiawei Shao73618602017-12-20 15:47:15 +08002372 }
2373
2374 return true;
2375}
2376
jchen10910a3da2017-11-15 09:40:11 +08002377GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2378{
2379 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2380 {
2381 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2382 if (tf.nameWithArrayIndex() == name)
2383 {
2384 return tfIndex;
2385 }
2386 }
2387 return GL_INVALID_INDEX;
2388}
2389
2390const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2391{
2392 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2393 return mState.mLinkedTransformFeedbackVaryings[index];
2394}
2395
Jamie Madillbd044ed2017-06-05 12:59:21 -04002396bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002397{
Jiawei Shao016105b2018-04-12 16:38:31 +08002398 Shader *previousShader = nullptr;
2399 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002400 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002401 Shader *currentShader = mState.mAttachedShaders[shaderType];
2402 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002403 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002404 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002405 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002406
2407 if (previousShader)
2408 {
2409 if (!linkValidateShaderInterfaceMatching(context, previousShader, currentShader,
2410 infoLog))
2411 {
2412 return false;
2413 }
2414 }
2415 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002416 }
2417
2418 if (!linkValidateBuiltInVaryings(context, infoLog))
2419 {
2420 return false;
2421 }
2422
2423 if (!linkValidateFragmentInputBindings(context, infoLog))
2424 {
2425 return false;
2426 }
2427
2428 return true;
2429}
2430
2431// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2432// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
2433bool Program::linkValidateShaderInterfaceMatching(const Context *context,
2434 gl::Shader *generatingShader,
2435 gl::Shader *consumingShader,
2436 gl::InfoLog &infoLog) const
2437{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002438 ASSERT(generatingShader->getShaderVersion(context) ==
2439 consumingShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002440
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002441 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context);
2442 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002443
Jiawei Shao385b3e02018-03-21 09:43:28 +08002444 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002445
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002446 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002448 bool matched = false;
2449
2450 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002451 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002452 {
2453 continue;
2454 }
2455
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002456 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002457 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002458 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002459 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002460 ASSERT(!output.isBuiltIn());
2461
2462 std::string mismatchedStructFieldName;
2463 LinkMismatchError linkError =
2464 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002465 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002466 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002467 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002468 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2469 mismatchedStructFieldName, generatingShader->getType(),
2470 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002471 return false;
2472 }
2473
Geoff Lang7dd2e102014-11-10 15:19:26 -05002474 matched = true;
2475 break;
2476 }
2477 }
2478
Olli Etuaho107c7242018-03-20 15:45:35 +02002479 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2480 // whether the input is statically used - a statically used input should fail this test even
2481 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002482 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002483 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002484 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2485 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2486 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002487 return false;
2488 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002489 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002490
Jiawei Shaod063aff2018-02-22 10:19:09 +08002491 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002492
Jiawei Shaod063aff2018-02-22 10:19:09 +08002493 return true;
2494}
2495
2496bool Program::linkValidateFragmentInputBindings(const Context *context, gl::InfoLog &infoLog) const
2497{
Jiawei Shao016105b2018-04-12 16:38:31 +08002498 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002499
2500 std::map<GLuint, std::string> staticFragmentInputLocations;
2501
2502 const std::vector<sh::Varying> &fragmentInputVaryings =
Jiawei Shao016105b2018-04-12 16:38:31 +08002503 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings(context);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002504 for (const sh::Varying &input : fragmentInputVaryings)
2505 {
2506 if (input.isBuiltIn() || !input.staticUse)
2507 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002508 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002509 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002510
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002511 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002512 if (inputBinding == -1)
2513 continue;
2514
2515 const auto it = staticFragmentInputLocations.find(inputBinding);
2516 if (it == std::end(staticFragmentInputLocations))
2517 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002518 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002519 }
2520 else
2521 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002522 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002523 << it->second;
2524 return false;
2525 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002526 }
2527
2528 return true;
2529}
2530
Jamie Madillbd044ed2017-06-05 12:59:21 -04002531bool Program::linkUniforms(const Context *context,
2532 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002533 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002534{
Olli Etuahob78707c2017-03-09 15:03:11 +00002535 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002536 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002537 {
2538 return false;
2539 }
2540
Olli Etuahob78707c2017-03-09 15:03:11 +00002541 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002542
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002543 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002544
jchen10eaef1e52017-06-13 10:44:11 +08002545 if (!linkAtomicCounterBuffers())
2546 {
2547 return false;
2548 }
2549
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002550 return true;
2551}
2552
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002553void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002554{
Jamie Madill982f6e02017-06-07 14:33:04 -04002555 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2556 unsigned int low = high;
2557
jchen10eaef1e52017-06-13 10:44:11 +08002558 for (auto counterIter = mState.mUniforms.rbegin();
2559 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2560 {
2561 --low;
2562 }
2563
2564 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2565
2566 high = low;
2567
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002568 for (auto imageIter = mState.mUniforms.rbegin();
2569 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2570 {
2571 --low;
2572 }
2573
2574 mState.mImageUniformRange = RangeUI(low, high);
2575
2576 // If uniform is a image type, insert it into the mImageBindings array.
2577 for (unsigned int imageIndex : mState.mImageUniformRange)
2578 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002579 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2580 // cannot load values into a uniform defined as an image. if declare without a
2581 // binding qualifier, any uniform image variable (include all elements of
2582 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002583 auto &imageUniform = mState.mUniforms[imageIndex];
2584 if (imageUniform.binding == -1)
2585 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002586 mState.mImageBindings.emplace_back(
2587 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002588 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002589 else
2590 {
2591 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002592 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002593 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002594 }
2595
2596 high = low;
2597
2598 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002599 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002600 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002601 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002602 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002603
2604 mState.mSamplerUniformRange = RangeUI(low, high);
2605
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002606 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002607 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002608 {
2609 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002610 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002611 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002612 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002613 }
2614}
2615
jchen10eaef1e52017-06-13 10:44:11 +08002616bool Program::linkAtomicCounterBuffers()
2617{
2618 for (unsigned int index : mState.mAtomicCounterUniformRange)
2619 {
2620 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002621 uniform.blockInfo.offset = uniform.offset;
2622 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2623 uniform.blockInfo.matrixStride = 0;
2624 uniform.blockInfo.isRowMajorMatrix = false;
2625
jchen10eaef1e52017-06-13 10:44:11 +08002626 bool found = false;
2627 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2628 ++bufferIndex)
2629 {
2630 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2631 if (buffer.binding == uniform.binding)
2632 {
2633 buffer.memberIndexes.push_back(index);
2634 uniform.bufferIndex = bufferIndex;
2635 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002636 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002637 break;
2638 }
2639 }
2640 if (!found)
2641 {
2642 AtomicCounterBuffer atomicCounterBuffer;
2643 atomicCounterBuffer.binding = uniform.binding;
2644 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002645 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002646 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2647 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2648 }
2649 }
2650 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002651 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002652
2653 return true;
2654}
2655
Jamie Madilleb979bf2016-11-15 12:28:46 -05002656// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002657bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002658{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002659 const ContextState &data = context->getContextState();
Jiawei Shao385b3e02018-03-21 09:43:28 +08002660 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002661
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002662 int shaderVersion = vertexShader->getShaderVersion(context);
2663
Geoff Lang7dd2e102014-11-10 15:19:26 -05002664 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002665 if (shaderVersion >= 300)
2666 {
2667 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2668 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
2669 mState.mAttributes = vertexShader->getAllAttributes(context);
2670 }
2671 else
2672 {
2673 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
2674 mState.mAttributes = vertexShader->getActiveAttributes(context);
2675 }
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002676 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002677
2678 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002679 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002680 {
Jamie Madillf6113162015-05-07 11:49:21 -04002681 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002682 return false;
2683 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002684
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002685 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002686
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002687 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002688 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002689 {
Olli Etuahod2551232017-10-26 20:03:33 +03002690 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2691 // structures, so we don't need to worry about adjusting their names or generating entries
2692 // for each member/element (unlike uniforms for example).
2693 ASSERT(!attribute.isArray() && !attribute.isStruct());
2694
Jamie Madilleb979bf2016-11-15 12:28:46 -05002695 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002696 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002697 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002698 attribute.location = bindingLocation;
2699 }
2700
2701 if (attribute.location != -1)
2702 {
2703 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002704 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002705
Jamie Madill63805b42015-08-25 13:17:39 -04002706 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002707 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002708 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
2709 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002710
2711 return false;
2712 }
2713
Jamie Madill63805b42015-08-25 13:17:39 -04002714 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002715 {
Jamie Madill63805b42015-08-25 13:17:39 -04002716 const int regLocation = attribute.location + reg;
2717 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002718
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002719 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
2720 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
2721 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04002722 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002723 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002724 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002725 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002727 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002728 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002729 return false;
2730 }
2731 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002732 else
2733 {
Jamie Madill63805b42015-08-25 13:17:39 -04002734 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002735 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002736
Jamie Madill63805b42015-08-25 13:17:39 -04002737 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002738 }
2739 }
2740 }
2741
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002742 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002743 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002744 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002745 // Not set by glBindAttribLocation or by location layout qualifier
2746 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002747 {
Jamie Madill63805b42015-08-25 13:17:39 -04002748 int regs = VariableRegisterCount(attribute.type);
2749 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002750
Jamie Madill63805b42015-08-25 13:17:39 -04002751 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002752 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002753 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002754 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002755 }
2756
Jamie Madillc349ec02015-08-21 16:53:12 -04002757 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002758 }
2759 }
2760
Brandon Jonesc405ae72017-12-06 14:15:03 -08002761 ASSERT(mState.mAttributesTypeMask.none());
2762 ASSERT(mState.mAttributesMask.none());
2763
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002764 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
2765 // shader versions we're only processing active attributes to begin with.
2766 if (shaderVersion >= 300)
2767 {
2768 for (auto attributeIter = mState.mAttributes.begin();
2769 attributeIter != mState.mAttributes.end();)
2770 {
2771 if (attributeIter->active)
2772 {
2773 ++attributeIter;
2774 }
2775 else
2776 {
2777 attributeIter = mState.mAttributes.erase(attributeIter);
2778 }
2779 }
2780 }
2781
Jamie Madill48ef11b2016-04-27 15:21:52 -04002782 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002783 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002784 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04002785 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002786 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002787
Jamie Madillbd159f02017-10-09 19:39:06 -04002788 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002789 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002790 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2791 mState.mActiveAttribLocationsMask.set(location);
2792 mState.mMaxActiveAttribLocation =
2793 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08002794
2795 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
2796 if (!attribute.isBuiltIn())
2797 {
2798 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
2799 location);
2800 mState.mAttributesMask.set(location);
2801 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002802 }
2803 }
2804
Geoff Lang7dd2e102014-11-10 15:19:26 -05002805 return true;
2806}
2807
Jiajia Qin729b2c62017-08-14 09:36:11 +08002808bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002809{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002810 const auto &caps = context->getCaps();
2811
Jiawei Shao016105b2018-04-12 16:38:31 +08002812 if (mState.mAttachedShaders[ShaderType::Compute])
Martin Radev4c4c8e72016-08-04 12:25:34 +03002813 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002814 Shader &computeShader = *mState.mAttachedShaders[ShaderType::Compute];
Jiajia Qin729b2c62017-08-14 09:36:11 +08002815 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002816
Jiawei Shao40786bd2018-04-18 13:58:57 +08002817 if (!ValidateInterfaceBlocksCount(caps.maxComputeUniformBlocks, computeUniformBlocks,
2818 ShaderType::Compute, sh::BlockType::BLOCK_UNIFORM,
2819 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002820 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002821 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002822 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002823
2824 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
Jiawei Shao427071e2018-03-19 09:21:37 +08002825 if (!ValidateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +08002826 computeShaderStorageBlocks, ShaderType::Compute,
2827 sh::BlockType::BLOCK_BUFFER, infoLog))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002828 {
2829 return false;
2830 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002831 return true;
2832 }
2833
Jiawei Shao40786bd2018-04-18 13:58:57 +08002834 ShaderMap<GLuint> maxShaderUniformBlocks = {};
2835 maxShaderUniformBlocks[gl::ShaderType::Vertex] = caps.maxVertexUniformBlocks;
2836 maxShaderUniformBlocks[gl::ShaderType::Fragment] = caps.maxFragmentUniformBlocks;
2837 maxShaderUniformBlocks[gl::ShaderType::Geometry] = caps.maxGeometryUniformBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002838
Jiawei Shao40786bd2018-04-18 13:58:57 +08002839 ShaderMap<const std::vector<sh::InterfaceBlock> *> graphicsShaderUniformBlocks = {};
2840 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002841 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08002842 Shader *shader = mState.mAttachedShaders[shaderType];
2843 if (!shader)
2844 {
2845 continue;
2846 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002847
Jiawei Shao40786bd2018-04-18 13:58:57 +08002848 const auto &uniformBlocks = mState.mAttachedShaders[shaderType]->getUniformBlocks(context);
2849 if (!ValidateInterfaceBlocksCount(maxShaderUniformBlocks[shaderType], uniformBlocks,
2850 shaderType, sh::BlockType::BLOCK_UNIFORM, infoLog))
Jiawei Shao427071e2018-03-19 09:21:37 +08002851 {
2852 return false;
2853 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08002854
2855 graphicsShaderUniformBlocks[shaderType] = &uniformBlocks;
Jiawei Shao427071e2018-03-19 09:21:37 +08002856 }
2857
Jamie Madillbd044ed2017-06-05 12:59:21 -04002858 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao40786bd2018-04-18 13:58:57 +08002859 if (!ValidateGraphicsInterfaceBlocks(graphicsShaderUniformBlocks, infoLog, webglCompatibility,
2860 sh::BlockType::BLOCK_UNIFORM,
Jiajia Qin8efd1262017-12-19 09:32:55 +08002861 caps.maxCombinedUniformBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002862 {
2863 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002864 }
Jamie Madille473dee2015-08-18 14:49:01 -04002865
Jiajia Qin729b2c62017-08-14 09:36:11 +08002866 if (context->getClientVersion() >= Version(3, 1))
2867 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08002868 ShaderMap<GLuint> maxShaderStorageBlocks = {};
2869 maxShaderStorageBlocks[ShaderType::Vertex] = caps.maxVertexShaderStorageBlocks;
2870 maxShaderStorageBlocks[ShaderType::Fragment] = caps.maxFragmentShaderStorageBlocks;
2871 maxShaderStorageBlocks[ShaderType::Geometry] = caps.maxGeometryShaderStorageBlocks;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002872
Jiawei Shao40786bd2018-04-18 13:58:57 +08002873 ShaderMap<const std::vector<sh::InterfaceBlock> *> graphicsShaderStorageBlocks = {};
2874 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiajia Qin729b2c62017-08-14 09:36:11 +08002875 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08002876 Shader *shader = mState.mAttachedShaders[shaderType];
2877 if (!shader)
2878 {
2879 continue;
2880 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002881
Jiawei Shao40786bd2018-04-18 13:58:57 +08002882 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks(context);
2883 if (!ValidateInterfaceBlocksCount(maxShaderStorageBlocks[shaderType],
2884 shaderStorageBlocks, shaderType,
2885 sh::BlockType::BLOCK_BUFFER, infoLog))
Jiawei Shao427071e2018-03-19 09:21:37 +08002886 {
2887 return false;
2888 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08002889
2890 graphicsShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
Jiawei Shao427071e2018-03-19 09:21:37 +08002891 }
2892
Jiawei Shao40786bd2018-04-18 13:58:57 +08002893 if (!ValidateGraphicsInterfaceBlocks(graphicsShaderStorageBlocks, infoLog,
2894 webglCompatibility, sh::BlockType::BLOCK_BUFFER,
2895 caps.maxCombinedShaderStorageBlocks))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002896 {
2897 return false;
2898 }
2899 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002900 return true;
2901}
2902
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002903LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
2904 const sh::ShaderVariable &variable2,
2905 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002906 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002907 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002908{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002909 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002910 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002911 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002912 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002913 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002914 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002915 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002916 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002917 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002918 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002919 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002920 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002921 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04002922 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002923 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04002924 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002925
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002926 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002927 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002928 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002929 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002930 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002931 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2932 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002933 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
2934 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002935
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002936 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002937 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002938 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002939 }
2940
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002941 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08002942 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002943 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002944 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002945 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
2946 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002947 }
2948 }
2949
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002950 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002951}
2952
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002953LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
2954 const sh::Varying &inputVarying,
2955 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002956 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002957 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002958{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002959 if (validateGeometryShaderInputVarying)
2960 {
2961 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
2962 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
2963 // or outputs.
2964 ASSERT(inputVarying.arraySizes.size() == 1u);
2965
2966 // Geometry shader input varyings are not treated as arrays, so a vertex array output
2967 // varying cannot match a geometry shader input varying.
2968 // [GL_EXT_geometry_shader] Section 7.4.1:
2969 // Geometry shader per-vertex input variables and blocks are required to be declared as
2970 // arrays, with each element representing input or output values for a single vertex of a
2971 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
2972 // are treated as though they were not declared as arrays.
2973 if (outputVarying.isArray())
2974 {
2975 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
2976 }
2977 }
2978
2979 // Skip the validation on the array sizes between a vertex output varying and a geometry input
2980 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002981 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08002982 LinkValidateVariablesBase(outputVarying, inputVarying, false,
2983 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002984 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002985 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002986 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002987 }
2988
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002989 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002990 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002991 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002992 }
2993
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002994 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002995 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002996 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002997 }
2998
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002999 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003000}
3001
Jamie Madillbd044ed2017-06-05 12:59:21 -04003002bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003003{
Jiawei Shao016105b2018-04-12 16:38:31 +08003004 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3005 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jiawei Shao3d404882017-10-16 13:30:48 +08003006 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
3007 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04003008 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05003009
3010 if (shaderVersion != 100)
3011 {
3012 // Only ESSL 1.0 has restrictions on matching input and output invariance
3013 return true;
3014 }
3015
3016 bool glPositionIsInvariant = false;
3017 bool glPointSizeIsInvariant = false;
3018 bool glFragCoordIsInvariant = false;
3019 bool glPointCoordIsInvariant = false;
3020
3021 for (const sh::Varying &varying : vertexVaryings)
3022 {
3023 if (!varying.isBuiltIn())
3024 {
3025 continue;
3026 }
3027 if (varying.name.compare("gl_Position") == 0)
3028 {
3029 glPositionIsInvariant = varying.isInvariant;
3030 }
3031 else if (varying.name.compare("gl_PointSize") == 0)
3032 {
3033 glPointSizeIsInvariant = varying.isInvariant;
3034 }
3035 }
3036
3037 for (const sh::Varying &varying : fragmentVaryings)
3038 {
3039 if (!varying.isBuiltIn())
3040 {
3041 continue;
3042 }
3043 if (varying.name.compare("gl_FragCoord") == 0)
3044 {
3045 glFragCoordIsInvariant = varying.isInvariant;
3046 }
3047 else if (varying.name.compare("gl_PointCoord") == 0)
3048 {
3049 glPointCoordIsInvariant = varying.isInvariant;
3050 }
3051 }
3052
3053 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3054 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3055 // Not requiring invariance to match is supported by:
3056 // dEQP, WebGL CTS, Nexus 5X GLES
3057 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3058 {
3059 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3060 "declared invariant.";
3061 return false;
3062 }
3063 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3064 {
3065 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3066 "declared invariant.";
3067 return false;
3068 }
3069
3070 return true;
3071}
3072
jchen10a9042d32017-03-17 08:50:45 +08003073bool Program::linkValidateTransformFeedback(const gl::Context *context,
3074 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003075 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003076 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003077{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003078
jchen108225e732017-11-14 16:29:03 +08003079 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003080 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003081 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003082 {
jchen10a9042d32017-03-17 08:50:45 +08003083 if (context->getClientVersion() < Version(3, 1) &&
3084 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003085 {
Geoff Lang1a683462015-09-29 15:09:59 -04003086 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003087 return false;
3088 }
jchen108225e732017-11-14 16:29:03 +08003089 if (context->getClientVersion() >= Version(3, 1))
3090 {
3091 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3092 {
3093 infoLog << "Two transform feedback varyings include the same array element ("
3094 << tfVaryingName << ").";
3095 return false;
3096 }
3097 }
3098 else
3099 {
3100 if (uniqueNames.count(tfVaryingName) > 0)
3101 {
3102 infoLog << "Two transform feedback varyings specify the same output variable ("
3103 << tfVaryingName << ").";
3104 return false;
3105 }
3106 }
3107 uniqueNames.insert(tfVaryingName);
3108 }
3109
3110 // Validate against program varyings.
3111 size_t totalComponents = 0;
3112 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3113 {
3114 std::vector<unsigned int> subscripts;
3115 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3116
3117 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3118 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003119 {
3120 infoLog << "Transform feedback varying " << tfVaryingName
3121 << " does not exist in the vertex shader.";
3122 return false;
3123 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003124
jchen108225e732017-11-14 16:29:03 +08003125 // Validate the matching variable.
3126 if (var->isStruct())
3127 {
3128 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3129 return false;
3130 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003131
jchen108225e732017-11-14 16:29:03 +08003132 size_t elementCount = 0;
3133 size_t componentCount = 0;
3134
3135 if (var->isArray())
3136 {
3137 if (context->getClientVersion() < Version(3, 1))
3138 {
3139 infoLog << "Capture of arrays is undefined and not supported.";
3140 return false;
3141 }
3142
3143 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3144 ASSERT(!var->isArrayOfArrays());
3145
3146 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3147 {
3148 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3149 return false;
3150 }
3151 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3152 }
3153 else
3154 {
3155 if (!subscripts.empty())
3156 {
3157 infoLog << "Varying '" << baseName
3158 << "' is not an array to be captured by element.";
3159 return false;
3160 }
3161 elementCount = 1;
3162 }
3163
3164 // TODO(jmadill): Investigate implementation limits on D3D11
3165 componentCount = VariableComponentCount(var->type) * elementCount;
3166 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3167 componentCount > caps.maxTransformFeedbackSeparateComponents)
3168 {
3169 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3170 << componentCount << ") exceed the maximum separate components ("
3171 << caps.maxTransformFeedbackSeparateComponents << ").";
3172 return false;
3173 }
3174
3175 totalComponents += componentCount;
3176 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3177 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3178 {
3179 infoLog << "Transform feedback varying total components (" << totalComponents
3180 << ") exceed the maximum interleaved components ("
3181 << caps.maxTransformFeedbackInterleavedComponents << ").";
3182 return false;
3183 }
3184 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003185 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003186}
3187
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003188bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
3189{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003190 const std::vector<sh::Attribute> &attributes =
Jiawei Shao016105b2018-04-12 16:38:31 +08003191 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes(context);
3192
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003193 for (const auto &attrib : attributes)
3194 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003195 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003196 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003197 Shader *shader = mState.mAttachedShaders[shaderType];
3198 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003199 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003200 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003201 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003202
3203 const std::vector<sh::Uniform> &uniforms = shader->getUniforms(context);
3204 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003205 {
3206 if (uniform.name == attrib.name)
3207 {
3208 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3209 return false;
3210 }
3211 }
3212 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003213 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003214
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003215 return true;
3216}
3217
Jamie Madill3c1da042017-11-27 18:33:40 -05003218void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003219{
3220 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003221 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003222 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003223 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003224 std::vector<unsigned int> subscripts;
3225 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003226 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003227 if (!subscripts.empty())
3228 {
3229 subscript = subscripts.back();
3230 }
Jamie Madill192745a2016-12-22 15:58:21 -05003231 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003232 {
Jamie Madill192745a2016-12-22 15:58:21 -05003233 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003234 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003235 {
jchen10a9042d32017-03-17 08:50:45 +08003236 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3237 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003238 break;
3239 }
jchen108225e732017-11-14 16:29:03 +08003240 else if (varying->isStruct())
3241 {
3242 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3243 if (field != nullptr)
3244 {
3245 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3246 break;
3247 }
3248 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003249 }
3250 }
James Darpinian30b604d2018-03-12 17:26:57 -07003251 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003252}
3253
Jamie Madill3c1da042017-11-27 18:33:40 -05003254ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003255{
Jamie Madill3c1da042017-11-27 18:33:40 -05003256 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003257
Jiawei Shao016105b2018-04-12 16:38:31 +08003258 for (const sh::Varying &varying :
3259 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003260 {
Jamie Madill192745a2016-12-22 15:58:21 -05003261 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003262 }
3263
Jiawei Shao016105b2018-04-12 16:38:31 +08003264 for (const sh::Varying &varying :
3265 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003266 {
Jamie Madill192745a2016-12-22 15:58:21 -05003267 merged[varying.name].fragment = &varying;
3268 }
3269
3270 return merged;
3271}
3272
Jamie Madillbd044ed2017-06-05 12:59:21 -04003273void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003274{
Jiawei Shao016105b2018-04-12 16:38:31 +08003275 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003276 ASSERT(fragmentShader != nullptr);
3277
Geoff Lange0cff192017-05-30 13:04:56 -04003278 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003279 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003280 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003281
3282 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04003283 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04003284 {
3285 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3286 outputVariable.name != "gl_FragData")
3287 {
3288 continue;
3289 }
3290
3291 unsigned int baseLocation =
3292 (outputVariable.location == -1 ? 0u
3293 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003294
3295 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3296 // structures, so we may use getBasicTypeElementCount().
3297 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3298 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003299 {
3300 const unsigned int location = baseLocation + elementIndex;
3301 if (location >= mState.mOutputVariableTypes.size())
3302 {
3303 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3304 }
Corentin Walleze7557742017-06-01 13:09:57 -04003305 ASSERT(location < mState.mActiveOutputVariables.size());
3306 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003307 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003308 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003309 }
3310 }
3311
Jamie Madill80a6fc02015-08-21 16:53:16 -04003312 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04003313 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003314 return;
3315
Jamie Madillbd044ed2017-06-05 12:59:21 -04003316 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04003317 // TODO(jmadill): any caps validation here?
3318
jchen1015015f72017-03-16 13:54:21 +08003319 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003320 outputVariableIndex++)
3321 {
jchen1015015f72017-03-16 13:54:21 +08003322 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003323
Olli Etuahod2551232017-10-26 20:03:33 +03003324 if (outputVariable.isArray())
3325 {
3326 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3327 // Resources and including [0] at the end of array variable names.
3328 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3329 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3330 }
3331
Jamie Madill80a6fc02015-08-21 16:53:16 -04003332 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3333 if (outputVariable.isBuiltIn())
3334 continue;
3335
3336 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003337 unsigned int baseLocation =
3338 (outputVariable.location == -1 ? 0u
3339 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003340
Olli Etuaho465835d2017-09-26 13:34:10 +03003341 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3342 // structures, so we may use getBasicTypeElementCount().
3343 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3344 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003345 {
Olli Etuahod2551232017-10-26 20:03:33 +03003346 const unsigned int location = baseLocation + elementIndex;
3347 if (location >= mState.mOutputLocations.size())
3348 {
3349 mState.mOutputLocations.resize(location + 1);
3350 }
3351 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003352 if (outputVariable.isArray())
3353 {
3354 mState.mOutputLocations[location] =
3355 VariableLocation(elementIndex, outputVariableIndex);
3356 }
3357 else
3358 {
3359 VariableLocation locationInfo;
3360 locationInfo.index = outputVariableIndex;
3361 mState.mOutputLocations[location] = locationInfo;
3362 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003363 }
3364 }
3365}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003366
Olli Etuaho48fed632017-03-16 12:05:30 +00003367void Program::setUniformValuesFromBindingQualifiers()
3368{
Jamie Madill982f6e02017-06-07 14:33:04 -04003369 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003370 {
3371 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3372 if (samplerUniform.binding != -1)
3373 {
Olli Etuahod2551232017-10-26 20:03:33 +03003374 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003375 ASSERT(location != -1);
3376 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003377 for (unsigned int elementIndex = 0;
3378 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003379 {
3380 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3381 }
3382 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3383 boundTextureUnits.data());
3384 }
3385 }
3386}
3387
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003388void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003389{
jchen10af713a22017-04-19 09:10:56 +08003390 // Set initial bindings from shader.
3391 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3392 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003393 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003394 bindUniformBlock(blockIndex, uniformBlock.binding);
3395 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003396}
3397
Jamie Madille7d84322017-01-10 18:21:59 -05003398void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003399 GLsizei clampedCount,
3400 const GLint *v)
3401{
Jamie Madill81c2e252017-09-09 23:32:46 -04003402 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3403 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3404 std::vector<GLuint> *boundTextureUnits =
3405 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003406
Olli Etuaho1734e172017-10-27 15:30:27 +03003407 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003408
3409 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003410 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003411}
3412
3413template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003414GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3415 GLsizei count,
3416 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003417 const T *v)
3418{
Jamie Madill134f93d2017-08-31 17:11:00 -04003419 if (count == 1)
3420 return 1;
3421
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003422 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003423
Corentin Wallez15ac5342016-11-03 17:06:39 -04003424 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3425 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003426 unsigned int remainingElements =
3427 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003428 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003429 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003430
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003431 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003432 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003433 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003434 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003435
3436 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003437}
3438
3439template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003440GLsizei Program::clampMatrixUniformCount(GLint location,
3441 GLsizei count,
3442 GLboolean transpose,
3443 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003444{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003445 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3446
Jamie Madill62d31cb2015-09-11 13:25:51 -04003447 if (!transpose)
3448 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003449 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003450 }
3451
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003452 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003453
3454 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3455 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003456 unsigned int remainingElements =
3457 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003458 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003459}
3460
Jamie Madill54164b02017-08-28 15:17:37 -04003461// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3462// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003463template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003464void Program::getUniformInternal(const Context *context,
3465 DestT *dataOut,
3466 GLint location,
3467 GLenum nativeType,
3468 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003469{
Jamie Madill54164b02017-08-28 15:17:37 -04003470 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003471 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003472 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003473 {
3474 GLint tempValue[16] = {0};
3475 mProgram->getUniformiv(context, location, tempValue);
3476 UniformStateQueryCastLoop<GLboolean>(
3477 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003478 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003479 }
3480 case GL_INT:
3481 {
3482 GLint tempValue[16] = {0};
3483 mProgram->getUniformiv(context, location, tempValue);
3484 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3485 components);
3486 break;
3487 }
3488 case GL_UNSIGNED_INT:
3489 {
3490 GLuint tempValue[16] = {0};
3491 mProgram->getUniformuiv(context, location, tempValue);
3492 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3493 components);
3494 break;
3495 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003496 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003497 {
3498 GLfloat tempValue[16] = {0};
3499 mProgram->getUniformfv(context, location, tempValue);
3500 UniformStateQueryCastLoop<GLfloat>(
3501 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003502 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003503 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003504 default:
3505 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003506 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003507 }
3508}
Jamie Madilla4595b82017-01-11 17:36:34 -05003509
3510bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3511{
3512 // Must be called after samplers are validated.
3513 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3514
3515 for (const auto &binding : mState.mSamplerBindings)
3516 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003517 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003518 for (const auto &unit : binding.boundTextureUnits)
3519 {
3520 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3521 if (programTextureID == textureID)
3522 {
3523 // TODO(jmadill): Check for appropriate overlap.
3524 return true;
3525 }
3526 }
3527 }
3528
3529 return false;
3530}
3531
Jamie Madilla2c74982016-12-12 11:20:42 -05003532} // namespace gl