blob: 1bdcf457b9c47da44c1b0109244cad0e7f3f17ac [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"
jchen103fd614d2018-08-13 12:21:58 +080026#include "libANGLE/Version.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040028#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040029#include "libANGLE/queryconversions.h"
30#include "libANGLE/renderer/GLImplFactory.h"
31#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040032#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050033
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000036
Geoff Lang7dd2e102014-11-10 15:19:26 -050037namespace
38{
39
Jamie Madill62d31cb2015-09-11 13:25:51 -040040// This simplified cast function doesn't need to worry about advanced concepts like
41// depth range values, or casting to bool.
42template <typename DestT, typename SrcT>
43DestT UniformStateQueryCast(SrcT value);
44
45// From-Float-To-Integer Casts
46template <>
47GLint UniformStateQueryCast(GLfloat value)
48{
49 return clampCast<GLint>(roundf(value));
50}
51
52template <>
53GLuint UniformStateQueryCast(GLfloat value)
54{
55 return clampCast<GLuint>(roundf(value));
56}
57
58// From-Integer-to-Integer Casts
59template <>
60GLint UniformStateQueryCast(GLuint value)
61{
62 return clampCast<GLint>(value);
63}
64
65template <>
66GLuint UniformStateQueryCast(GLint value)
67{
68 return clampCast<GLuint>(value);
69}
70
71// From-Boolean-to-Anything Casts
72template <>
73GLfloat UniformStateQueryCast(GLboolean value)
74{
Geoff Lang92019432017-11-20 13:09:34 -050075 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040076}
77
78template <>
79GLint UniformStateQueryCast(GLboolean value)
80{
Geoff Lang92019432017-11-20 13:09:34 -050081 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040082}
83
84template <>
85GLuint UniformStateQueryCast(GLboolean value)
86{
Geoff Lang92019432017-11-20 13:09:34 -050087 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040088}
89
90// Default to static_cast
91template <typename DestT, typename SrcT>
92DestT UniformStateQueryCast(SrcT value)
93{
94 return static_cast<DestT>(value);
95}
96
97template <typename SrcT, typename DestT>
98void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
99{
100 for (int comp = 0; comp < components; ++comp)
101 {
102 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
103 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
104 size_t offset = comp * 4;
105 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
106 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
107 }
108}
109
jchen1015015f72017-03-16 13:54:21 +0800110template <typename VarT>
111GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
112{
Olli Etuahod2551232017-10-26 20:03:33 +0300113 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800114 for (size_t index = 0; index < list.size(); index++)
115 {
116 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300117 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800118 {
Olli Etuahod2551232017-10-26 20:03:33 +0300119 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800120 }
121 }
122
123 return GL_INVALID_INDEX;
124}
125
Olli Etuahod2551232017-10-26 20:03:33 +0300126template <typename VarT>
127GLint GetVariableLocation(const std::vector<VarT> &list,
128 const std::vector<VariableLocation> &locationList,
129 const std::string &name)
130{
131 size_t nameLengthWithoutArrayIndex;
132 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
133
134 for (size_t location = 0u; location < locationList.size(); ++location)
135 {
136 const VariableLocation &variableLocation = locationList[location];
137 if (!variableLocation.used())
138 {
139 continue;
140 }
141
142 const VarT &variable = list[variableLocation.index];
143
144 if (angle::BeginsWith(variable.name, name))
145 {
146 if (name.length() == variable.name.length())
147 {
148 ASSERT(name == variable.name);
149 // GLES 3.1 November 2016 page 87.
150 // The string exactly matches the name of the active variable.
151 return static_cast<GLint>(location);
152 }
153 if (name.length() + 3u == variable.name.length() && variable.isArray())
154 {
155 ASSERT(name + "[0]" == variable.name);
156 // The string identifies the base name of an active array, where the string would
157 // exactly match the name of the variable if the suffix "[0]" were appended to the
158 // string.
159 return static_cast<GLint>(location);
160 }
161 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300162 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300163 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
164 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
165 {
166 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
167 // The string identifies an active element of the array, where the string ends with the
168 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
169 // zeroes, or whitespace) identifying an array element, and the "]" character, the
170 // integer is less than the number of active elements of the array variable, and where
171 // the string would exactly match the enumerated name of the array if the decimal
172 // integer were replaced with zero.
173 return static_cast<GLint>(location);
174 }
175 }
176
177 return -1;
178}
179
jchen10fd7c3b52017-03-21 15:36:03 +0800180void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
181{
182 ASSERT(bufSize > 0);
183 strncpy(buffer, string.c_str(), bufSize);
184 buffer[bufSize - 1] = '\0';
185
186 if (length)
187 {
188 *length = static_cast<GLsizei>(strlen(buffer));
189 }
190}
191
jchen10a9042d32017-03-17 08:50:45 +0800192bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
193{
Olli Etuahoc8538042017-09-27 11:20:15 +0300194 std::vector<unsigned int> subscripts;
195 std::string baseName = ParseResourceName(name, &subscripts);
196 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800197 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300198 std::vector<unsigned int> arrayIndices;
199 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
200 if (baseName == arrayName &&
201 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800202 {
203 return true;
204 }
205 }
206 return false;
207}
208
Jiawei Shao40786bd2018-04-18 13:58:57 +0800209std::string GetInterfaceBlockLimitName(ShaderType shaderType, sh::BlockType blockType)
210{
211 std::ostringstream stream;
212 stream << "GL_MAX_" << GetShaderTypeString(shaderType) << "_";
213
214 switch (blockType)
215 {
216 case sh::BlockType::BLOCK_UNIFORM:
217 stream << "UNIFORM_BUFFERS";
218 break;
219 case sh::BlockType::BLOCK_BUFFER:
220 stream << "SHADER_STORAGE_BLOCKS";
221 break;
222 default:
223 UNREACHABLE();
224 return "";
225 }
226
227 if (shaderType == ShaderType::Geometry)
228 {
229 stream << "_EXT";
230 }
231
232 return stream.str();
233}
234
235const char *GetInterfaceBlockTypeString(sh::BlockType blockType)
236{
237 switch (blockType)
238 {
239 case sh::BlockType::BLOCK_UNIFORM:
240 return "uniform block";
241 case sh::BlockType::BLOCK_BUFFER:
242 return "shader storage block";
243 default:
244 UNREACHABLE();
245 return "";
246 }
247}
248
249void LogInterfaceBlocksExceedLimit(InfoLog &infoLog,
250 ShaderType shaderType,
251 sh::BlockType blockType,
252 GLuint limit)
253{
254 infoLog << GetShaderTypeString(shaderType) << " shader "
255 << GetInterfaceBlockTypeString(blockType) << " count exceeds "
256 << GetInterfaceBlockLimitName(shaderType, blockType) << " (" << limit << ")";
257}
258
Jiawei Shao427071e2018-03-19 09:21:37 +0800259bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800260 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800261 ShaderType shaderType,
262 sh::BlockType blockType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800263 GLuint *combinedInterfaceBlocksCount,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800264 InfoLog &infoLog)
265{
266 GLuint blockCount = 0;
267 for (const sh::InterfaceBlock &block : interfaceBlocks)
268 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800269 if (IsActiveInterfaceBlock(block))
Jiajia Qin729b2c62017-08-14 09:36:11 +0800270 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800271 blockCount += std::max(block.arraySize, 1u);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800272 if (blockCount > maxInterfaceBlocks)
273 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800274 LogInterfaceBlocksExceedLimit(infoLog, shaderType, blockType, maxInterfaceBlocks);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800275 return false;
276 }
277 }
278 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800279
280 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
281 // If a uniform block is used by multiple shader stages, each such use counts separately
282 // against this combined limit.
283 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
284 // If a shader storage block in a program is referenced by multiple shaders, each such
285 // reference counts separately against this combined limit.
286 if (combinedInterfaceBlocksCount)
287 {
288 *combinedInterfaceBlocksCount += blockCount;
289 }
290
Jiajia Qin729b2c62017-08-14 09:36:11 +0800291 return true;
292}
293
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800294GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
295{
296 std::vector<unsigned int> subscripts;
297 std::string baseName = ParseResourceName(name, &subscripts);
298
299 unsigned int numBlocks = static_cast<unsigned int>(list.size());
300 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
301 {
302 const auto &block = list[blockIndex];
303 if (block.name == baseName)
304 {
305 const bool arrayElementZero =
306 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
307 const bool arrayElementMatches =
308 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
309 if (arrayElementMatches || arrayElementZero)
310 {
311 return blockIndex;
312 }
313 }
314 }
315
316 return GL_INVALID_INDEX;
317}
318
319void GetInterfaceBlockName(const GLuint index,
320 const std::vector<InterfaceBlock> &list,
321 GLsizei bufSize,
322 GLsizei *length,
323 GLchar *name)
324{
325 ASSERT(index < list.size());
326
327 const auto &block = list[index];
328
329 if (bufSize > 0)
330 {
331 std::string blockName = block.name;
332
333 if (block.isArray)
334 {
335 blockName += ArrayString(block.arrayElement);
336 }
337 CopyStringToBuffer(name, blockName, bufSize, length);
338 }
339}
340
jchen103fd614d2018-08-13 12:21:58 +0800341void InitUniformBlockLinker(const ProgramState &state, UniformBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500342{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800343 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500344 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800345 Shader *shader = state.getAttachedShader(shaderType);
346 if (shader)
347 {
jchen103fd614d2018-08-13 12:21:58 +0800348 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800349 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500350 }
351}
352
jchen103fd614d2018-08-13 12:21:58 +0800353void InitShaderStorageBlockLinker(const ProgramState &state, ShaderStorageBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500354{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800355 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500356 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800357 Shader *shader = state.getAttachedShader(shaderType);
358 if (shader != nullptr)
359 {
jchen103fd614d2018-08-13 12:21:58 +0800360 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800361 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500362 }
363}
364
jchen108225e732017-11-14 16:29:03 +0800365// Find the matching varying or field by name.
366const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
367 const std::string &name)
368{
369 const sh::ShaderVariable *var = nullptr;
370 for (const auto &ref : varyings)
371 {
372 const sh::Varying *varying = ref.second.get();
373 if (varying->name == name)
374 {
375 var = varying;
376 break;
377 }
378 var = FindShaderVarField(*varying, name);
379 if (var != nullptr)
380 {
381 break;
382 }
383 }
384 return var;
385}
386
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800387void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
388{
389 ASSERT(mismatchedFieldName);
390 if (mismatchedFieldName->empty())
391 {
392 *mismatchedFieldName = parentName;
393 }
394 else
395 {
396 std::ostringstream stream;
397 stream << parentName << "." << *mismatchedFieldName;
398 *mismatchedFieldName = stream.str();
399 }
400}
401
402const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
403{
404 switch (linkError)
405 {
406 case LinkMismatchError::TYPE_MISMATCH:
407 return "Type";
408 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
409 return "Array size";
410 case LinkMismatchError::PRECISION_MISMATCH:
411 return "Precision";
412 case LinkMismatchError::STRUCT_NAME_MISMATCH:
413 return "Structure name";
414 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
415 return "Field number";
416 case LinkMismatchError::FIELD_NAME_MISMATCH:
417 return "Field name";
418
419 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
420 return "Interpolation type";
421 case LinkMismatchError::INVARIANCE_MISMATCH:
422 return "Invariance";
423
424 case LinkMismatchError::BINDING_MISMATCH:
425 return "Binding layout qualifier";
426 case LinkMismatchError::LOCATION_MISMATCH:
427 return "Location layout qualifier";
428 case LinkMismatchError::OFFSET_MISMATCH:
429 return "Offset layout qualilfier";
430
431 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
432 return "Layout qualifier";
433 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
434 return "Matrix Packing";
435 default:
436 UNREACHABLE();
437 return "";
438 }
439}
440
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800441LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
442 const sh::InterfaceBlockField &blockField2,
443 bool webglCompatibility,
444 std::string *mismatchedBlockFieldName)
445{
446 if (blockField1.name != blockField2.name)
447 {
448 return LinkMismatchError::FIELD_NAME_MISMATCH;
449 }
450
451 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
452 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
453 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
454 if (linkError != LinkMismatchError::NO_MISMATCH)
455 {
456 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
457 return linkError;
458 }
459
460 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
461 {
462 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
463 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
464 }
465
466 return LinkMismatchError::NO_MISMATCH;
467}
468
469LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
470 const sh::InterfaceBlock &interfaceBlock2,
471 bool webglCompatibility,
472 std::string *mismatchedBlockFieldName)
473{
474 // validate blocks for the same member types
475 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
476 {
477 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
478 }
479 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
480 {
481 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
482 }
483 if (interfaceBlock1.layout != interfaceBlock2.layout ||
484 interfaceBlock1.binding != interfaceBlock2.binding)
485 {
486 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
487 }
488 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
489 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
490 {
491 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
492 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
493
494 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
495 member1, member2, webglCompatibility, mismatchedBlockFieldName);
496 if (linkError != LinkMismatchError::NO_MISMATCH)
497 {
498 return linkError;
499 }
500 }
501 return LinkMismatchError::NO_MISMATCH;
502}
503
Jiawei Shao40786bd2018-04-18 13:58:57 +0800504using ShaderInterfaceBlock = std::pair<ShaderType, const sh::InterfaceBlock *>;
505using InterfaceBlockMap = std::map<std::string, ShaderInterfaceBlock>;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800506
Jiawei Shao40786bd2018-04-18 13:58:57 +0800507void InitializeInterfaceBlockMap(const std::vector<sh::InterfaceBlock> &interfaceBlocks,
508 ShaderType shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800509 InterfaceBlockMap *linkedInterfaceBlocks)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800510{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800511 ASSERT(linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800512
513 for (const sh::InterfaceBlock &interfaceBlock : interfaceBlocks)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800514 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800515 (*linkedInterfaceBlocks)[interfaceBlock.name] = std::make_pair(shaderType, &interfaceBlock);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800516 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800517}
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800518
Jiawei Shao40786bd2018-04-18 13:58:57 +0800519bool ValidateGraphicsInterfaceBlocksPerShader(
520 const std::vector<sh::InterfaceBlock> &interfaceBlocksToLink,
521 ShaderType shaderType,
522 bool webglCompatibility,
523 InterfaceBlockMap *linkedBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800524 InfoLog &infoLog)
525{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800526 ASSERT(linkedBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800527
528 for (const sh::InterfaceBlock &block : interfaceBlocksToLink)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800529 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800530 const auto &entry = linkedBlocks->find(block.name);
531 if (entry != linkedBlocks->end())
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800532 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800533 const sh::InterfaceBlock &linkedBlock = *(entry->second.second);
534 std::string mismatchedStructFieldName;
535 LinkMismatchError linkError = AreMatchingInterfaceBlocks(
536 block, linkedBlock, webglCompatibility, &mismatchedStructFieldName);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800537 if (linkError != LinkMismatchError::NO_MISMATCH)
538 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800539 LogLinkMismatch(infoLog, block.name, GetInterfaceBlockTypeString(block.blockType),
540 linkError, mismatchedStructFieldName, entry->second.first,
541 shaderType);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800542 return false;
543 }
544 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800545 else
546 {
547 (*linkedBlocks)[block.name] = std::make_pair(shaderType, &block);
548 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800549 }
550
551 return true;
552}
553
Jiawei Shao54aafe52018-04-27 14:54:57 +0800554bool ValidateInterfaceBlocksMatch(
555 GLuint numShadersHasInterfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800556 const ShaderMap<const std::vector<sh::InterfaceBlock> *> &shaderInterfaceBlocks,
557 InfoLog &infoLog,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800558 bool webglCompatibility)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800559{
Jiawei Shao54aafe52018-04-27 14:54:57 +0800560 if (numShadersHasInterfaceBlocks < 2u)
561 {
562 return true;
563 }
564
565 ASSERT(!shaderInterfaceBlocks[ShaderType::Compute]);
566
Jiawei Shao40786bd2018-04-18 13:58:57 +0800567 // Check that interface blocks defined in the graphics shaders are identical
568
569 InterfaceBlockMap linkedInterfaceBlocks;
Jiawei Shao40786bd2018-04-18 13:58:57 +0800570
571 bool interfaceBlockMapInitialized = false;
572 for (ShaderType shaderType : kAllGraphicsShaderTypes)
573 {
574 if (!shaderInterfaceBlocks[shaderType])
575 {
576 continue;
577 }
578
579 if (!interfaceBlockMapInitialized)
580 {
581 InitializeInterfaceBlockMap(*shaderInterfaceBlocks[shaderType], shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800582 &linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800583 interfaceBlockMapInitialized = true;
584 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800585 else if (!ValidateGraphicsInterfaceBlocksPerShader(*shaderInterfaceBlocks[shaderType],
586 shaderType, webglCompatibility,
587 &linkedInterfaceBlocks, infoLog))
Jiawei Shao40786bd2018-04-18 13:58:57 +0800588 {
589 return false;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800590 }
591 }
592
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800593 return true;
594}
595
Jamie Madill62d31cb2015-09-11 13:25:51 -0400596} // anonymous namespace
597
jchen107ae70d82018-07-06 13:47:01 +0800598// Saves the linking context for later use in resolveLink().
599struct Program::LinkingState
600{
601 const Context *context;
602 std::unique_ptr<ProgramLinkedResources> resources;
603 ProgramHash programHash;
604 std::unique_ptr<rx::LinkEvent> linkEvent;
605};
606
Jamie Madill4a3c2342015-10-08 12:58:45 -0400607const char *const g_fakepath = "C:\\fakepath";
608
Jamie Madill3c1da042017-11-27 18:33:40 -0500609// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400610InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000611{
612}
613
614InfoLog::~InfoLog()
615{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000616}
617
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400618size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000619{
Jamie Madill23176ce2017-07-31 14:14:33 -0400620 if (!mLazyStream)
621 {
622 return 0;
623 }
624
625 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400626 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000627}
628
Geoff Lange1a27752015-10-05 13:16:04 -0400629void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000630{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400631 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000632
633 if (bufSize > 0)
634 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400635 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400636
Jamie Madill23176ce2017-07-31 14:14:33 -0400637 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000638 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400639 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
640 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000641 }
642
643 infoLog[index] = '\0';
644 }
645
646 if (length)
647 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400648 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000649 }
650}
651
652// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300653// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000654// messages, so lets remove all occurrences of this fake file path from the log.
655void InfoLog::appendSanitized(const char *message)
656{
Jamie Madill23176ce2017-07-31 14:14:33 -0400657 ensureInitialized();
658
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000659 std::string msg(message);
660
661 size_t found;
662 do
663 {
664 found = msg.find(g_fakepath);
665 if (found != std::string::npos)
666 {
667 msg.erase(found, strlen(g_fakepath));
668 }
Jamie Madill493f9572018-05-24 19:52:15 -0400669 } while (found != std::string::npos);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000670
Jamie Madill23176ce2017-07-31 14:14:33 -0400671 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000672}
673
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000674void InfoLog::reset()
675{
Jiawei Shao02f15232017-12-27 10:10:28 +0800676 if (mLazyStream)
677 {
678 mLazyStream.reset(nullptr);
679 }
680}
681
682bool InfoLog::empty() const
683{
684 if (!mLazyStream)
685 {
686 return true;
687 }
688
689 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000690}
691
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800692void LogLinkMismatch(InfoLog &infoLog,
693 const std::string &variableName,
694 const char *variableType,
695 LinkMismatchError linkError,
696 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800697 ShaderType shaderType1,
698 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800699{
700 std::ostringstream stream;
701 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
702 << variableName;
703
704 if (!mismatchedStructOrBlockFieldName.empty())
705 {
706 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
707 }
708
709 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
710 << GetShaderTypeString(shaderType2) << " shaders.";
711
712 infoLog << stream.str();
713}
714
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800715bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
716{
717 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200718 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800719}
720
Jamie Madill3c1da042017-11-27 18:33:40 -0500721// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300722VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000723{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724}
725
Olli Etuahoc8538042017-09-27 11:20:15 +0300726VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300727 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500728{
Olli Etuahoc8538042017-09-27 11:20:15 +0300729 ASSERT(arrayIndex != GL_INVALID_INDEX);
730}
731
Jamie Madill3c1da042017-11-27 18:33:40 -0500732// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800733SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500734 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
735{
736}
737
738SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
739
740SamplerBinding::~SamplerBinding() = default;
741
Jamie Madill3c1da042017-11-27 18:33:40 -0500742// ProgramBindings implementation.
743ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500744{
745}
746
Jamie Madill3c1da042017-11-27 18:33:40 -0500747ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500748{
749}
750
Jamie Madill3c1da042017-11-27 18:33:40 -0500751void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400752{
753 mBindings[name] = index;
754}
755
Jamie Madill3c1da042017-11-27 18:33:40 -0500756int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400757{
758 auto iter = mBindings.find(name);
759 return (iter != mBindings.end()) ? iter->second : -1;
760}
761
Jamie Madill3c1da042017-11-27 18:33:40 -0500762ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400763{
764 return mBindings.begin();
765}
766
Jamie Madill3c1da042017-11-27 18:33:40 -0500767ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400768{
769 return mBindings.end();
770}
771
Jamie Madill3c1da042017-11-27 18:33:40 -0500772// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500773ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
774{
775}
776ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
777{
778 for (size_t index = 0; index < count; ++index)
779 {
780 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
781 }
782}
783
784ImageBinding::ImageBinding(const ImageBinding &other) = default;
785
786ImageBinding::~ImageBinding() = default;
787
Jamie Madill3c1da042017-11-27 18:33:40 -0500788// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400789ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500790 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800791 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500792 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400793 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500794 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800795 mImageUniformRange(0, 0),
796 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300797 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800798 mNumViews(-1),
799 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400800 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
801 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800802 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400803 mGeometryShaderMaxVertices(0),
804 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400805{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300806 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400807 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400808}
809
Jamie Madill48ef11b2016-04-27 15:21:52 -0400810ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400811{
Jiawei Shao016105b2018-04-12 16:38:31 +0800812 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400813}
814
Jamie Madill48ef11b2016-04-27 15:21:52 -0400815const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500816{
817 return mLabel;
818}
819
Jiawei Shao385b3e02018-03-21 09:43:28 +0800820Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
821{
Jiawei Shao016105b2018-04-12 16:38:31 +0800822 ASSERT(shaderType != ShaderType::InvalidEnum);
823 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800824}
825
Jamie Madille7d84322017-01-10 18:21:59 -0500826GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400827{
jchen1015015f72017-03-16 13:54:21 +0800828 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400829}
830
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800831GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
832{
833 return GetResourceIndexFromName(mBufferVariables, name);
834}
835
Jamie Madille7d84322017-01-10 18:21:59 -0500836GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
837{
838 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
839 return mUniformLocations[location].index;
840}
841
842Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
843{
844 GLuint index = getUniformIndexFromLocation(location);
845 if (!isSamplerUniformIndex(index))
846 {
847 return Optional<GLuint>::Invalid();
848 }
849
850 return getSamplerIndexFromUniformIndex(index);
851}
852
853bool ProgramState::isSamplerUniformIndex(GLuint index) const
854{
Jamie Madill982f6e02017-06-07 14:33:04 -0400855 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500856}
857
858GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
859{
860 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400861 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500862}
863
Jamie Madill34ca4f52017-06-13 11:49:39 -0400864GLuint ProgramState::getAttributeLocation(const std::string &name) const
865{
866 for (const sh::Attribute &attribute : mAttributes)
867 {
868 if (attribute.name == name)
869 {
870 return attribute.location;
871 }
872 }
873
874 return static_cast<GLuint>(-1);
875}
876
Jiawei Shao016105b2018-04-12 16:38:31 +0800877bool ProgramState::hasAttachedShader() const
878{
879 for (const Shader *shader : mAttachedShaders)
880 {
881 if (shader)
882 {
883 return true;
884 }
885 }
886 return false;
887}
888
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500889Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400890 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400891 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800893 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894 mDeleteStatus(false),
895 mRefCount(0),
896 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500897 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898{
899 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000900
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
904Program::~Program()
905{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400906 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
Jamie Madill4928b7c2017-06-20 12:57:39 -0400909void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500910{
jchen107ae70d82018-07-06 13:47:01 +0800911 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800912 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500913 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800914 if (mState.mAttachedShaders[shaderType])
915 {
916 mState.mAttachedShaders[shaderType]->release(context);
917 mState.mAttachedShaders[shaderType] = nullptr;
918 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800919 }
920
Jamie Madill21061022018-07-12 23:56:30 -0400921 // TODO(jmadill): Handle error in the Context. http://anglebug.com/2491
Jamie Madillb7d924a2018-03-10 11:16:54 -0500922 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400923
Jiawei Shao016105b2018-04-12 16:38:31 +0800924 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400925 SafeDelete(mProgram);
926
927 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500928}
jchen107ae70d82018-07-06 13:47:01 +0800929GLuint Program::id() const
930{
931 resolveLink();
932 return mHandle;
933}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500934
Geoff Lang70d0f492015-12-10 17:45:46 -0500935void Program::setLabel(const std::string &label)
936{
jchen107ae70d82018-07-06 13:47:01 +0800937 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400938 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500939}
940
941const std::string &Program::getLabel() const
942{
jchen107ae70d82018-07-06 13:47:01 +0800943 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400944 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500945}
jchen107ae70d82018-07-06 13:47:01 +0800946rx::ProgramImpl *Program::getImplementation() const
947{
948 resolveLink();
949 return mProgram;
950}
Geoff Lang70d0f492015-12-10 17:45:46 -0500951
Jamie Madillef300b12016-10-07 15:12:09 -0400952void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953{
jchen107ae70d82018-07-06 13:47:01 +0800954 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800955 ShaderType shaderType = shader->getType();
956 ASSERT(shaderType != ShaderType::InvalidEnum);
957
958 mState.mAttachedShaders[shaderType] = shader;
959 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
Jamie Madillc1d770e2017-04-13 17:31:24 -0400962void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
jchen107ae70d82018-07-06 13:47:01 +0800964 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800965 ShaderType shaderType = shader->getType();
966 ASSERT(shaderType != ShaderType::InvalidEnum);
967
968 ASSERT(mState.mAttachedShaders[shaderType] == shader);
969 shader->release(context);
970 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000973int Program::getAttachedShadersCount() const
974{
jchen107ae70d82018-07-06 13:47:01 +0800975 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800976 int numAttachedShaders = 0;
977 for (const Shader *shader : mState.mAttachedShaders)
978 {
979 if (shader)
980 {
981 ++numAttachedShaders;
982 }
983 }
984
985 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000986}
987
Jiawei Shao385b3e02018-03-21 09:43:28 +0800988const Shader *Program::getAttachedShader(ShaderType shaderType) const
989{
jchen107ae70d82018-07-06 13:47:01 +0800990 resolveLink();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800991 return mState.getAttachedShader(shaderType);
992}
993
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994void Program::bindAttributeLocation(GLuint index, const char *name)
995{
jchen107ae70d82018-07-06 13:47:01 +0800996 resolveLink();
Geoff Langd8605522016-04-13 10:19:12 -0400997 mAttributeBindings.bindLocation(index, name);
998}
999
1000void Program::bindUniformLocation(GLuint index, const char *name)
1001{
jchen107ae70d82018-07-06 13:47:01 +08001002 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001003 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
Sami Väisänen46eaa942016-06-29 10:26:37 +03001006void Program::bindFragmentInputLocation(GLint index, const char *name)
1007{
jchen107ae70d82018-07-06 13:47:01 +08001008 resolveLink();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001009 mFragmentInputBindings.bindLocation(index, name);
1010}
1011
jchen103fd614d2018-08-13 12:21:58 +08001012BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001013{
jchen107ae70d82018-07-06 13:47:01 +08001014 resolveLink();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001015 BindingInfo ret;
1016 ret.type = GL_NONE;
1017 ret.valid = false;
1018
Jiawei Shao385b3e02018-03-21 09:43:28 +08001019 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001020 ASSERT(fragmentShader);
1021
1022 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001023 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001024
1025 for (const auto &binding : mFragmentInputBindings)
1026 {
1027 if (binding.second != static_cast<GLuint>(index))
1028 continue;
1029
1030 ret.valid = true;
1031
Olli Etuahod2551232017-10-26 20:03:33 +03001032 size_t nameLengthWithoutArrayIndex;
1033 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001034
1035 for (const auto &in : inputs)
1036 {
Olli Etuahod2551232017-10-26 20:03:33 +03001037 if (in.name.length() == nameLengthWithoutArrayIndex &&
1038 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001039 {
1040 if (in.isArray())
1041 {
1042 // The client wants to bind either "name" or "name[0]".
1043 // GL ES 3.1 spec refers to active array names with language such as:
1044 // "if the string identifies the base name of an active array, where the
1045 // string would exactly match the name of the variable if the suffix "[0]"
1046 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001047 if (arrayIndex == GL_INVALID_INDEX)
1048 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001049
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001050 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001051 }
1052 else
1053 {
1054 ret.name = in.mappedName;
1055 }
1056 ret.type = in.type;
1057 return ret;
1058 }
1059 }
1060 }
1061
1062 return ret;
1063}
1064
jchen103fd614d2018-08-13 12:21:58 +08001065void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001066 GLenum genMode,
1067 GLint components,
1068 const GLfloat *coeffs)
1069{
jchen107ae70d82018-07-06 13:47:01 +08001070 resolveLink();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001071 // If the location is -1 then the command is silently ignored
1072 if (index == -1)
1073 return;
1074
jchen103fd614d2018-08-13 12:21:58 +08001075 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001076
1077 // If the input doesn't exist then then the command is silently ignored
1078 // This could happen through optimization for example, the shader translator
1079 // decides that a variable is not actually being used and optimizes it away.
1080 if (binding.name.empty())
1081 return;
1082
1083 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1084}
1085
Martin Radev4c4c8e72016-08-04 12:25:34 +03001086// The attached shaders are checked for linking errors by matching up their variables.
1087// Uniform, input and output variables get collected.
1088// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001089Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001090{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001091 const auto &data = context->getContextState();
1092
Jamie Madill6c58b062017-08-01 13:44:25 -04001093 auto *platform = ANGLEPlatformCurrent();
1094 double startTime = platform->currentTime(platform);
1095
Jamie Madill6c1f6712017-02-14 19:08:04 -05001096 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001097 mInfoLog.reset();
1098
1099 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001100 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001101 {
1102 return NoError();
1103 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001104
jchen107ae70d82018-07-06 13:47:01 +08001105 ProgramHash programHash = {0};
Jamie Madill6bc264a2018-03-31 15:36:05 -04001106 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001107
Jamie Madill32447362017-06-28 14:53:52 -04001108 if (cache)
1109 {
1110 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001111 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001112 }
1113
1114 if (mLinked)
1115 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001116 double delta = platform->currentTime(platform) - startTime;
1117 int us = static_cast<int>(delta * 1000000.0);
1118 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001119 return NoError();
1120 }
1121
1122 // Cache load failed, fall through to normal linking.
1123 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001124
Jamie Madill6bc264a2018-03-31 15:36:05 -04001125 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001126 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001127
jchen107ae70d82018-07-06 13:47:01 +08001128 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001129 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001130 {
jchen107ae70d82018-07-06 13:47:01 +08001131 resources.reset(
1132 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1133 {&mState.mUniformBlocks, &mState.mUniforms},
1134 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1135 {&mState.mAtomicCounterBuffers},
1136 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001137
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001138 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001139 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1140 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001141 {
1142 return NoError();
1143 }
1144
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001145 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001146 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1147 context->getExtensions().webglCompatibility, mInfoLog,
1148 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001149 {
1150 return NoError();
1151 }
1152
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001153 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1154 // A link error will be generated if the sum of the number of active image uniforms used in
1155 // all shaders, the number of active shader storage blocks, and the number of active
1156 // fragment shader outputs exceeds the implementation-dependent value of
1157 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1158 if (combinedImageUniforms + combinedShaderStorageBlocks >
1159 context->getCaps().maxCombinedShaderOutputResources)
1160 {
1161 mInfoLog
1162 << "The sum of the number of active image uniforms, active shader storage blocks "
1163 "and active fragment shader outputs exceeds "
1164 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1165 << context->getCaps().maxCombinedShaderOutputResources << ")";
1166 return NoError();
1167 }
1168
jchen103fd614d2018-08-13 12:21:58 +08001169 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1170 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001171 }
1172 else
1173 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001174 // Map the varyings to the register file
1175 // In WebGL, we use a slightly different handling for packing variables.
1176 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1177 if (data.getLimitations().noFlexibleVaryingPacking)
1178 {
1179 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1180 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1181 }
1182 else if (data.getExtensions().webglCompatibility)
1183 {
1184 packMode = PackMode::WEBGL_STRICT;
1185 }
1186
jchen107ae70d82018-07-06 13:47:01 +08001187 resources.reset(
1188 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1189 {&mState.mUniformBlocks, &mState.mUniforms},
1190 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1191 {&mState.mAtomicCounterBuffers},
1192 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001193
jchen103fd614d2018-08-13 12:21:58 +08001194 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001195 {
1196 return NoError();
1197 }
1198
jchen103fd614d2018-08-13 12:21:58 +08001199 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001200 {
1201 return NoError();
1202 }
1203
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001204 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001205 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1206 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001207 {
1208 return NoError();
1209 }
1210
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001211 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001212 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1213 context->getExtensions().webglCompatibility, mInfoLog,
1214 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001215 {
1216 return NoError();
1217 }
1218
jchen103fd614d2018-08-13 12:21:58 +08001219 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001220 {
1221 return NoError();
1222 }
1223
jchen103fd614d2018-08-13 12:21:58 +08001224 if (!linkOutputVariables(context->getCaps(), context->getClientVersion(),
1225 combinedImageUniforms, combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001226 {
1227 return NoError();
1228 }
1229
jchen103fd614d2018-08-13 12:21:58 +08001230 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001231
Jiawei Shao016105b2018-04-12 16:38:31 +08001232 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001233 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001234
jchen103fd614d2018-08-13 12:21:58 +08001235 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1236 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001237
jchen103fd614d2018-08-13 12:21:58 +08001238 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1239 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001240 {
1241 return NoError();
1242 }
1243
jchen107ae70d82018-07-06 13:47:01 +08001244 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001245 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001246 {
1247 return NoError();
1248 }
1249
Martin Radev4c4c8e72016-08-04 12:25:34 +03001250 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001251 }
1252
jchen107ae70d82018-07-06 13:47:01 +08001253 mLinkingState.reset(new LinkingState());
1254 mLinkingState->context = context;
1255 mLinkingState->programHash = programHash;
1256 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1257 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001258 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001259
1260 return NoError();
1261}
1262
1263bool Program::isLinking() const
1264{
1265 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1266}
1267
1268bool Program::isLinked() const
1269{
1270 resolveLink();
1271 return mLinked;
1272}
1273
jchen107ae70d82018-07-06 13:47:01 +08001274void Program::resolveLinkImpl()
1275{
1276 ASSERT(mLinkingState.get());
1277
1278 mLinked = mLinkingState->linkEvent->wait();
jchen105055fba2018-08-17 09:57:07 +08001279 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001280 auto linkingState = std::move(mLinkingState);
1281 if (!mLinked)
1282 {
1283 return;
1284 }
1285
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001286 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001287
Yunchao Heece12532017-11-21 15:50:21 +08001288 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1289 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001290 ASSERT(mLinked);
1291 updateLinkedShaderStages();
1292
Jamie Madill54164b02017-08-28 15:17:37 -04001293 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001294 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001295
Jamie Madill7e4eff12018-08-08 15:49:26 -04001296 // Must be called after markUnusedUniformLocations.
1297 mState.updateActiveSamplers();
1298
1299 setUniformValuesFromBindingQualifiers();
1300
Jamie Madill32447362017-06-28 14:53:52 -04001301 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001302 auto *cache = linkingState->context->getMemoryProgramCache();
1303 if (cache &&
1304 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1305 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001306 {
jchen107ae70d82018-07-06 13:47:01 +08001307 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001308 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001309}
1310
Yunchao He85072e82017-11-14 15:43:28 +08001311void Program::updateLinkedShaderStages()
1312{
Yunchao Heece12532017-11-21 15:50:21 +08001313 mState.mLinkedShaderStages.reset();
1314
Jiawei Shao016105b2018-04-12 16:38:31 +08001315 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001316 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001317 if (shader)
1318 {
1319 mState.mLinkedShaderStages.set(shader->getType());
1320 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001321 }
Yunchao He85072e82017-11-14 15:43:28 +08001322}
1323
James Darpinian30b604d2018-03-12 17:26:57 -07001324void ProgramState::updateTransformFeedbackStrides()
1325{
1326 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1327 {
1328 mTransformFeedbackStrides.resize(1);
1329 size_t totalSize = 0;
1330 for (auto &varying : mLinkedTransformFeedbackVaryings)
1331 {
1332 totalSize += varying.size() * VariableExternalSize(varying.type);
1333 }
1334 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1335 }
1336 else
1337 {
1338 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1339 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1340 {
1341 auto &varying = mLinkedTransformFeedbackVaryings[i];
1342 mTransformFeedbackStrides[i] =
1343 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1344 }
1345 }
1346}
1347
Jamie Madill7e4eff12018-08-08 15:49:26 -04001348void ProgramState::updateActiveSamplers()
1349{
1350 for (SamplerBinding &samplerBinding : mSamplerBindings)
1351 {
1352 if (samplerBinding.unreferenced)
1353 continue;
1354
1355 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1356 {
1357 mActiveSamplerRefCounts[textureUnit]++;
1358 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1359 mActiveSamplersMask.set(textureUnit);
1360 }
1361 }
1362}
1363
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001364// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001365void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001367 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001368 mState.mAttributesTypeMask.reset();
1369 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001370 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001371 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001372 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001373 mState.mUniforms.clear();
1374 mState.mUniformLocations.clear();
1375 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001376 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001377 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001378 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001379 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001380 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001381 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001382 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001383 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001384 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001385 mState.mImageBindings.clear();
Jamie Madill493f9572018-05-24 19:52:15 -04001386 mState.mNumViews = -1;
1387 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1388 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001389 mState.mGeometryShaderInvocations = 1;
1390 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392 mValidated = false;
1393
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001394 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001395 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001396}
1397
Jamie Madilla2c74982016-12-12 11:20:42 -05001398Error Program::loadBinary(const Context *context,
1399 GLenum binaryFormat,
1400 const void *binary,
1401 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001402{
jchen107ae70d82018-07-06 13:47:01 +08001403 resolveLink();
Jamie Madill6c1f6712017-02-14 19:08:04 -05001404 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001405
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001407 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001409 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1410 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001411 {
Jamie Madillf6113162015-05-07 11:49:21 -04001412 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001413 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 }
1415
Jamie Madill4f86d052017-06-05 12:59:26 -04001416 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1417 ANGLE_TRY_RESULT(
1418 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001419
1420 // Currently we require the full shader text to compute the program hash.
1421 // TODO(jmadill): Store the binary in the internal program cache.
1422
Jamie Madill70aeda42018-08-20 12:17:40 -04001423 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1424 ++uniformBlockIndex)
1425 {
1426 mDirtyBits.set(uniformBlockIndex);
1427 }
1428
Jamie Madillb0a838b2016-11-13 20:02:12 -05001429 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001430#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431}
1432
Jamie Madilla2c74982016-12-12 11:20:42 -05001433Error Program::saveBinary(const Context *context,
1434 GLenum *binaryFormat,
1435 void *binary,
1436 GLsizei bufSize,
1437 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438{
jchen107ae70d82018-07-06 13:47:01 +08001439 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440 if (binaryFormat)
1441 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001442 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443 }
1444
Jamie Madill4f86d052017-06-05 12:59:26 -04001445 angle::MemoryBuffer memoryBuf;
1446 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447
Jamie Madill4f86d052017-06-05 12:59:26 -04001448 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1449 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450
1451 if (streamLength > bufSize)
1452 {
1453 if (length)
1454 {
1455 *length = 0;
1456 }
1457
Jamie Madill493f9572018-05-24 19:52:15 -04001458 // TODO: This should be moved to the validation layer but computing the size of the binary
1459 // before saving it causes the save to happen twice. It may be possible to write the binary
1460 // to a separate buffer, validate sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001461 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462 }
1463
1464 if (binary)
1465 {
Jamie Madill493f9572018-05-24 19:52:15 -04001466 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467
Jamie Madill48ef11b2016-04-27 15:21:52 -04001468 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 ptr += streamLength;
1470
1471 ASSERT(ptr - streamLength == binary);
1472 }
1473
1474 if (length)
1475 {
1476 *length = streamLength;
1477 }
1478
He Yunchaoacd18982017-01-04 10:46:42 +08001479 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
Jamie Madillffe00c02017-06-27 16:26:55 -04001482GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483{
jchen107ae70d82018-07-06 13:47:01 +08001484 resolveLink();
Geoff Langb26ab822018-05-29 11:19:00 -04001485 if (!mLinked)
1486 {
1487 return 0;
1488 }
1489
Geoff Lang7dd2e102014-11-10 15:19:26 -05001490 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001491 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 if (error.isError())
1493 {
1494 return 0;
1495 }
1496
1497 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001498}
1499
Geoff Langc5629752015-12-07 16:29:04 -05001500void Program::setBinaryRetrievableHint(bool retrievable)
1501{
jchen107ae70d82018-07-06 13:47:01 +08001502 resolveLink();
Geoff Langc5629752015-12-07 16:29:04 -05001503 // TODO(jmadill) : replace with dirty bits
1504 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001505 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001506}
1507
1508bool Program::getBinaryRetrievableHint() const
1509{
jchen107ae70d82018-07-06 13:47:01 +08001510 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001511 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001512}
1513
Yunchao He61afff12017-03-14 15:34:03 +08001514void Program::setSeparable(bool separable)
1515{
jchen107ae70d82018-07-06 13:47:01 +08001516 resolveLink();
Yunchao He61afff12017-03-14 15:34:03 +08001517 // TODO(yunchao) : replace with dirty bits
1518 if (mState.mSeparable != separable)
1519 {
1520 mProgram->setSeparable(separable);
1521 mState.mSeparable = separable;
1522 }
1523}
1524
1525bool Program::isSeparable() const
1526{
jchen107ae70d82018-07-06 13:47:01 +08001527 resolveLink();
Yunchao He61afff12017-03-14 15:34:03 +08001528 return mState.mSeparable;
1529}
1530
Jamie Madill6c1f6712017-02-14 19:08:04 -05001531void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001532{
jchen107ae70d82018-07-06 13:47:01 +08001533 resolveLink();
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001534 mRefCount--;
1535
1536 if (mRefCount == 0 && mDeleteStatus)
1537 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001538 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001539 }
1540}
1541
1542void Program::addRef()
1543{
jchen107ae70d82018-07-06 13:47:01 +08001544 resolveLink();
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001545 mRefCount++;
1546}
1547
1548unsigned int Program::getRefCount() const
1549{
1550 return mRefCount;
1551}
1552
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001553int Program::getInfoLogLength() const
1554{
jchen107ae70d82018-07-06 13:47:01 +08001555 resolveLink();
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001556 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001557}
1558
Geoff Lange1a27752015-10-05 13:16:04 -04001559void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001560{
jchen107ae70d82018-07-06 13:47:01 +08001561 resolveLink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001562 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001563}
1564
Geoff Lange1a27752015-10-05 13:16:04 -04001565void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001566{
jchen107ae70d82018-07-06 13:47:01 +08001567 resolveLink();
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001568 int total = 0;
1569
Jiawei Shao016105b2018-04-12 16:38:31 +08001570 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001571 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001572 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001573 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001574 shaders[total] = shader->getHandle();
1575 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001576 }
1577 }
1578
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001579 if (count)
1580 {
1581 *count = total;
1582 }
1583}
1584
Geoff Lange1a27752015-10-05 13:16:04 -04001585GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586{
jchen107ae70d82018-07-06 13:47:01 +08001587 resolveLink();
Jamie Madill34ca4f52017-06-13 11:49:39 -04001588 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589}
1590
Jamie Madill63805b42015-08-25 13:17:39 -04001591bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001592{
jchen107ae70d82018-07-06 13:47:01 +08001593 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001594 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1595 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001596}
1597
jchen10fd7c3b52017-03-21 15:36:03 +08001598void Program::getActiveAttribute(GLuint index,
1599 GLsizei bufsize,
1600 GLsizei *length,
1601 GLint *size,
1602 GLenum *type,
1603 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001604{
jchen107ae70d82018-07-06 13:47:01 +08001605 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001606 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001607 {
1608 if (bufsize > 0)
1609 {
1610 name[0] = '\0';
1611 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001612
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001613 if (length)
1614 {
1615 *length = 0;
1616 }
1617
1618 *type = GL_NONE;
1619 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001620 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001621 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001622
jchen1036e120e2017-03-14 14:53:58 +08001623 ASSERT(index < mState.mAttributes.size());
1624 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001625
1626 if (bufsize > 0)
1627 {
jchen10fd7c3b52017-03-21 15:36:03 +08001628 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001629 }
1630
1631 // Always a single 'type' instance
1632 *size = 1;
1633 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001634}
1635
Geoff Lange1a27752015-10-05 13:16:04 -04001636GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001637{
jchen107ae70d82018-07-06 13:47:01 +08001638 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001639 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001640 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001641 return 0;
1642 }
1643
jchen1036e120e2017-03-14 14:53:58 +08001644 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001645}
1646
Geoff Lange1a27752015-10-05 13:16:04 -04001647GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001648{
jchen107ae70d82018-07-06 13:47:01 +08001649 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001650 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001651 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001652 return 0;
1653 }
1654
1655 size_t maxLength = 0;
1656
Jamie Madill48ef11b2016-04-27 15:21:52 -04001657 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001658 {
jchen1036e120e2017-03-14 14:53:58 +08001659 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001660 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661
Jamie Madillc349ec02015-08-21 16:53:12 -04001662 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663}
1664
jchen107ae70d82018-07-06 13:47:01 +08001665const std::vector<sh::Attribute> &Program::getAttributes() const
1666{
1667 resolveLink();
1668 return mState.mAttributes;
1669}
1670
1671const AttributesMask &Program::getActiveAttribLocationsMask() const
1672{
1673 resolveLink();
1674 return mState.mActiveAttribLocationsMask;
1675}
1676
1677const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1678{
1679 resolveLink();
1680 return mState.mSamplerBindings;
1681}
1682
1683const std::vector<ImageBinding> &Program::getImageBindings() const
1684{
1685 resolveLink();
1686 return mState.mImageBindings;
1687}
1688const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1689{
1690 resolveLink();
1691 return mState.mComputeShaderLocalSize;
1692}
1693
1694PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1695{
1696 resolveLink();
1697 return mState.mGeometryShaderInputPrimitiveType;
1698}
1699PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1700{
1701 resolveLink();
1702 return mState.mGeometryShaderOutputPrimitiveType;
1703}
1704GLint Program::getGeometryShaderInvocations() const
1705{
1706 resolveLink();
1707 return mState.mGeometryShaderInvocations;
1708}
1709GLint Program::getGeometryShaderMaxVertices() const
1710{
1711 resolveLink();
1712 return mState.mGeometryShaderMaxVertices;
1713}
1714
1715const ProgramState &Program::getState() const
1716{
1717 resolveLink();
1718 return mState;
1719}
1720
jchen1015015f72017-03-16 13:54:21 +08001721GLuint Program::getInputResourceIndex(const GLchar *name) const
1722{
jchen107ae70d82018-07-06 13:47:01 +08001723 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001724 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001725}
1726
1727GLuint Program::getOutputResourceIndex(const GLchar *name) const
1728{
jchen107ae70d82018-07-06 13:47:01 +08001729 resolveLink();
jchen1015015f72017-03-16 13:54:21 +08001730 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1731}
1732
jchen10fd7c3b52017-03-21 15:36:03 +08001733size_t Program::getOutputResourceCount() const
1734{
jchen107ae70d82018-07-06 13:47:01 +08001735 resolveLink();
jchen10fd7c3b52017-03-21 15:36:03 +08001736 return (mLinked ? mState.mOutputVariables.size() : 0);
1737}
1738
jchen107ae70d82018-07-06 13:47:01 +08001739const std::vector<GLenum> &Program::getOutputVariableTypes() const
1740{
1741 resolveLink();
1742 return mState.mOutputVariableTypes;
1743}
1744DrawBufferMask Program::getActiveOutputVariables() const
1745{
1746 resolveLink();
1747 return mState.mActiveOutputVariables;
1748}
1749
jchen10baf5d942017-08-28 20:45:48 +08001750template <typename T>
1751void Program::getResourceName(GLuint index,
1752 const std::vector<T> &resources,
1753 GLsizei bufSize,
1754 GLsizei *length,
1755 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001756{
1757 if (length)
1758 {
1759 *length = 0;
1760 }
1761
1762 if (!mLinked)
1763 {
1764 if (bufSize > 0)
1765 {
1766 name[0] = '\0';
1767 }
1768 return;
1769 }
jchen10baf5d942017-08-28 20:45:48 +08001770 ASSERT(index < resources.size());
1771 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001772
1773 if (bufSize > 0)
1774 {
Olli Etuahod2551232017-10-26 20:03:33 +03001775 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001776 }
1777}
1778
jchen10baf5d942017-08-28 20:45:48 +08001779void Program::getInputResourceName(GLuint index,
1780 GLsizei bufSize,
1781 GLsizei *length,
1782 GLchar *name) const
1783{
jchen107ae70d82018-07-06 13:47:01 +08001784 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001785 getResourceName(index, mState.mAttributes, bufSize, length, name);
1786}
1787
1788void Program::getOutputResourceName(GLuint index,
1789 GLsizei bufSize,
1790 GLsizei *length,
1791 GLchar *name) const
1792{
jchen107ae70d82018-07-06 13:47:01 +08001793 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001794 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1795}
1796
1797void Program::getUniformResourceName(GLuint index,
1798 GLsizei bufSize,
1799 GLsizei *length,
1800 GLchar *name) const
1801{
jchen107ae70d82018-07-06 13:47:01 +08001802 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001803 getResourceName(index, mState.mUniforms, bufSize, length, name);
1804}
1805
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001806void Program::getBufferVariableResourceName(GLuint index,
1807 GLsizei bufSize,
1808 GLsizei *length,
1809 GLchar *name) const
1810{
jchen107ae70d82018-07-06 13:47:01 +08001811 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001812 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1813}
1814
jchen10880683b2017-04-12 16:21:55 +08001815const sh::Attribute &Program::getInputResource(GLuint index) const
1816{
jchen107ae70d82018-07-06 13:47:01 +08001817 resolveLink();
jchen10880683b2017-04-12 16:21:55 +08001818 ASSERT(index < mState.mAttributes.size());
1819 return mState.mAttributes[index];
1820}
1821
1822const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1823{
jchen107ae70d82018-07-06 13:47:01 +08001824 resolveLink();
jchen10880683b2017-04-12 16:21:55 +08001825 ASSERT(index < mState.mOutputVariables.size());
1826 return mState.mOutputVariables[index];
1827}
1828
jchen107ae70d82018-07-06 13:47:01 +08001829const ProgramBindings &Program::getAttributeBindings() const
1830{
1831 resolveLink();
1832 return mAttributeBindings;
1833}
1834const ProgramBindings &Program::getUniformLocationBindings() const
1835{
1836 resolveLink();
1837 return mUniformLocationBindings;
1838}
1839const ProgramBindings &Program::getFragmentInputBindings() const
1840{
1841 resolveLink();
1842 return mFragmentInputBindings;
1843}
1844
1845int Program::getNumViews() const
1846{
1847 resolveLink();
1848 return mState.getNumViews();
1849}
1850bool Program::usesMultiview() const
1851{
1852 resolveLink();
1853 return mState.usesMultiview();
1854}
1855
1856ComponentTypeMask Program::getDrawBufferTypeMask() const
1857{
1858 resolveLink();
1859 return mState.mDrawBufferTypeMask;
1860}
1861ComponentTypeMask Program::getAttributesTypeMask() const
1862{
1863 resolveLink();
1864 return mState.mAttributesTypeMask;
1865}
1866AttributesMask Program::getAttributesMask() const
1867{
1868 resolveLink();
1869 return mState.mAttributesMask;
1870}
1871
1872const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1873{
1874 resolveLink();
1875 return mState.mTransformFeedbackStrides;
1876}
1877
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878GLint Program::getFragDataLocation(const std::string &name) const
1879{
jchen107ae70d82018-07-06 13:47:01 +08001880 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001881 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001882}
1883
Geoff Lange1a27752015-10-05 13:16:04 -04001884void Program::getActiveUniform(GLuint index,
1885 GLsizei bufsize,
1886 GLsizei *length,
1887 GLint *size,
1888 GLenum *type,
1889 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001890{
jchen107ae70d82018-07-06 13:47:01 +08001891 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001893 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001894 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001895 ASSERT(index < mState.mUniforms.size());
1896 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897
1898 if (bufsize > 0)
1899 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001900 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001901 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902 }
1903
Olli Etuaho465835d2017-09-26 13:34:10 +03001904 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001906 }
1907 else
1908 {
1909 if (bufsize > 0)
1910 {
1911 name[0] = '\0';
1912 }
1913
1914 if (length)
1915 {
1916 *length = 0;
1917 }
1918
1919 *size = 0;
1920 *type = GL_NONE;
1921 }
1922}
1923
Geoff Lange1a27752015-10-05 13:16:04 -04001924GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001925{
jchen107ae70d82018-07-06 13:47:01 +08001926 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001928 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001929 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001930 }
1931 else
1932 {
1933 return 0;
1934 }
1935}
1936
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001937size_t Program::getActiveBufferVariableCount() const
1938{
jchen107ae70d82018-07-06 13:47:01 +08001939 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001940 return mLinked ? mState.mBufferVariables.size() : 0;
1941}
1942
Geoff Lange1a27752015-10-05 13:16:04 -04001943GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001944{
jchen107ae70d82018-07-06 13:47:01 +08001945 resolveLink();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001946 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947
1948 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001949 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001950 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001952 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001953 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001954 size_t length = uniform.name.length() + 1u;
1955 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001956 {
1957 length += 3; // Counting in "[0]".
1958 }
1959 maxLength = std::max(length, maxLength);
1960 }
1961 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001962 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001963
Jamie Madill62d31cb2015-09-11 13:25:51 -04001964 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965}
1966
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967bool Program::isValidUniformLocation(GLint location) const
1968{
jchen107ae70d82018-07-06 13:47:01 +08001969 resolveLink();
Jamie Madille2e406c2016-06-02 13:04:10 -04001970 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001971 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001972 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001973}
1974
Jamie Madill62d31cb2015-09-11 13:25:51 -04001975const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001976{
jchen107ae70d82018-07-06 13:47:01 +08001977 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001978 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001979 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980}
1981
Jamie Madillac4e9c32017-01-13 14:07:12 -05001982const VariableLocation &Program::getUniformLocation(GLint location) const
1983{
jchen107ae70d82018-07-06 13:47:01 +08001984 resolveLink();
Jamie Madillac4e9c32017-01-13 14:07:12 -05001985 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1986 return mState.mUniformLocations[location];
1987}
1988
jchen107ae70d82018-07-06 13:47:01 +08001989const std::vector<VariableLocation> &Program::getUniformLocations() const
1990{
1991 resolveLink();
1992 return mState.mUniformLocations;
1993}
1994const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1995{
1996 resolveLink();
1997 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1998 return mState.mUniforms[index];
1999}
2000
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002001const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
2002{
jchen107ae70d82018-07-06 13:47:01 +08002003 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002004 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
2005 return mState.mBufferVariables[index];
2006}
2007
Jamie Madill62d31cb2015-09-11 13:25:51 -04002008GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009{
jchen107ae70d82018-07-06 13:47:01 +08002010 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03002011 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012}
2013
Jamie Madill62d31cb2015-09-11 13:25:51 -04002014GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015{
jchen107ae70d82018-07-06 13:47:01 +08002016 resolveLink();
Jamie Madille7d84322017-01-10 18:21:59 -05002017 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018}
2019
2020void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2021{
jchen107ae70d82018-07-06 13:47:01 +08002022 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002023 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2024 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002025 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026}
2027
2028void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2029{
jchen107ae70d82018-07-06 13:47:01 +08002030 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002031 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2032 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002033 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034}
2035
2036void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2037{
jchen107ae70d82018-07-06 13:47:01 +08002038 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002039 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2040 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002041 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042}
2043
2044void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2045{
jchen107ae70d82018-07-06 13:47:01 +08002046 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002047 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2048 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002049 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050}
2051
Jamie Madill81c2e252017-09-09 23:32:46 -04002052Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053{
jchen107ae70d82018-07-06 13:47:01 +08002054 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002055 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2056 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2057
Jamie Madill81c2e252017-09-09 23:32:46 -04002058 mProgram->setUniform1iv(location, clampedCount, v);
2059
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002060 if (mState.isSamplerUniformIndex(locationInfo.index))
2061 {
2062 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002063 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002064 }
2065
Jamie Madill81c2e252017-09-09 23:32:46 -04002066 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067}
2068
2069void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2070{
jchen107ae70d82018-07-06 13:47:01 +08002071 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002072 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2073 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002074 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075}
2076
2077void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2078{
jchen107ae70d82018-07-06 13:47:01 +08002079 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002080 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2081 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002082 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083}
2084
2085void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2086{
jchen107ae70d82018-07-06 13:47:01 +08002087 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002088 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2089 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002090 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091}
2092
2093void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2094{
jchen107ae70d82018-07-06 13:47:01 +08002095 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002096 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2097 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002098 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099}
2100
2101void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2102{
jchen107ae70d82018-07-06 13:47:01 +08002103 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002104 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2105 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002106 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107}
2108
2109void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2110{
jchen107ae70d82018-07-06 13:47:01 +08002111 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002112 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2113 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002114 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115}
2116
2117void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2118{
jchen107ae70d82018-07-06 13:47:01 +08002119 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002120 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2121 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002122 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123}
2124
Jamie Madill493f9572018-05-24 19:52:15 -04002125void Program::setUniformMatrix2fv(GLint location,
2126 GLsizei count,
2127 GLboolean transpose,
2128 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129{
jchen107ae70d82018-07-06 13:47:01 +08002130 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002131 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002132 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133}
2134
Jamie Madill493f9572018-05-24 19:52:15 -04002135void Program::setUniformMatrix3fv(GLint location,
2136 GLsizei count,
2137 GLboolean transpose,
2138 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139{
jchen107ae70d82018-07-06 13:47:01 +08002140 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002141 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002142 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143}
2144
Jamie Madill493f9572018-05-24 19:52:15 -04002145void Program::setUniformMatrix4fv(GLint location,
2146 GLsizei count,
2147 GLboolean transpose,
2148 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149{
jchen107ae70d82018-07-06 13:47:01 +08002150 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002151 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002152 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153}
2154
Jamie Madill493f9572018-05-24 19:52:15 -04002155void Program::setUniformMatrix2x3fv(GLint location,
2156 GLsizei count,
2157 GLboolean transpose,
2158 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002159{
jchen107ae70d82018-07-06 13:47:01 +08002160 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002161 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002162 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163}
2164
Jamie Madill493f9572018-05-24 19:52:15 -04002165void Program::setUniformMatrix2x4fv(GLint location,
2166 GLsizei count,
2167 GLboolean transpose,
2168 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169{
jchen107ae70d82018-07-06 13:47:01 +08002170 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002171 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002172 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173}
2174
Jamie Madill493f9572018-05-24 19:52:15 -04002175void Program::setUniformMatrix3x2fv(GLint location,
2176 GLsizei count,
2177 GLboolean transpose,
2178 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179{
jchen107ae70d82018-07-06 13:47:01 +08002180 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002181 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002182 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183}
2184
Jamie Madill493f9572018-05-24 19:52:15 -04002185void Program::setUniformMatrix3x4fv(GLint location,
2186 GLsizei count,
2187 GLboolean transpose,
2188 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189{
jchen107ae70d82018-07-06 13:47:01 +08002190 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002191 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002192 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193}
2194
Jamie Madill493f9572018-05-24 19:52:15 -04002195void Program::setUniformMatrix4x2fv(GLint location,
2196 GLsizei count,
2197 GLboolean transpose,
2198 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002199{
jchen107ae70d82018-07-06 13:47:01 +08002200 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002201 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002202 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203}
2204
Jamie Madill493f9572018-05-24 19:52:15 -04002205void Program::setUniformMatrix4x3fv(GLint location,
2206 GLsizei count,
2207 GLboolean transpose,
2208 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209{
jchen107ae70d82018-07-06 13:47:01 +08002210 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002211 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002212 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002213}
2214
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002215GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2216{
jchen107ae70d82018-07-06 13:47:01 +08002217 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002218 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2219 const std::vector<GLuint> &boundTextureUnits =
2220 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2221 return boundTextureUnits[uniformLocation.arrayIndex];
2222}
2223
Jamie Madill54164b02017-08-28 15:17:37 -04002224void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002225{
jchen107ae70d82018-07-06 13:47:01 +08002226 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002227 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2228 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002229
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002230 if (uniform.isSampler())
2231 {
2232 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2233 return;
2234 }
2235
2236 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002237 if (nativeType == GL_FLOAT)
2238 {
2239 mProgram->getUniformfv(context, location, v);
2240 }
2241 else
2242 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002243 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002244 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245}
2246
Jamie Madill54164b02017-08-28 15:17:37 -04002247void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248{
jchen107ae70d82018-07-06 13:47:01 +08002249 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002250 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2251 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002252
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002253 if (uniform.isSampler())
2254 {
2255 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2256 return;
2257 }
2258
2259 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002260 if (nativeType == GL_INT || nativeType == GL_BOOL)
2261 {
2262 mProgram->getUniformiv(context, location, v);
2263 }
2264 else
2265 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002266 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002267 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268}
2269
Jamie Madill54164b02017-08-28 15:17:37 -04002270void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271{
jchen107ae70d82018-07-06 13:47:01 +08002272 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002273 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2274 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002275
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002276 if (uniform.isSampler())
2277 {
2278 *v = getSamplerUniformBinding(uniformLocation);
2279 return;
2280 }
2281
2282 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002283 if (nativeType == GL_UNSIGNED_INT)
2284 {
2285 mProgram->getUniformuiv(context, location, v);
2286 }
2287 else
2288 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002289 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002290 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002291}
2292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293void Program::flagForDeletion()
2294{
jchen107ae70d82018-07-06 13:47:01 +08002295 resolveLink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 mDeleteStatus = true;
2297}
2298
2299bool Program::isFlaggedForDeletion() const
2300{
jchen107ae70d82018-07-06 13:47:01 +08002301 resolveLink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 return mDeleteStatus;
2303}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002304
Brandon Jones43a53e22014-08-28 16:23:22 -07002305void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002306{
jchen107ae70d82018-07-06 13:47:01 +08002307 resolveLink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002308 mInfoLog.reset();
2309
Geoff Lang7dd2e102014-11-10 15:19:26 -05002310 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002311 {
Geoff Lang92019432017-11-20 13:09:34 -05002312 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002313 }
2314 else
2315 {
Jamie Madillf6113162015-05-07 11:49:21 -04002316 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002317 }
2318}
2319
Jamie Madillcc73f242018-08-01 11:34:48 -04002320bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002321{
jchen107ae70d82018-07-06 13:47:01 +08002322 resolveLink();
2323
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002324 // if any two active samplers in a program are of different types, but refer to the same
2325 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2326 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002327 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002328 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002329 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002330 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002331 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002332 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002333 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2334 "image unit ("
2335 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002336 }
2337
Jamie Madill7e4eff12018-08-08 15:49:26 -04002338 mCachedValidateSamplersResult = false;
2339 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002340 }
2341 }
2342
2343 mCachedValidateSamplersResult = true;
2344 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345}
2346
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002347bool Program::isValidated() const
2348{
jchen107ae70d82018-07-06 13:47:01 +08002349 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 return mValidated;
2351}
2352
jchen1058f67be2017-10-27 08:59:27 +08002353GLuint Program::getActiveAtomicCounterBufferCount() const
2354{
jchen107ae70d82018-07-06 13:47:01 +08002355 resolveLink();
jchen1058f67be2017-10-27 08:59:27 +08002356 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2357}
2358
Jiajia Qin729b2c62017-08-14 09:36:11 +08002359GLuint Program::getActiveShaderStorageBlockCount() const
2360{
jchen107ae70d82018-07-06 13:47:01 +08002361 resolveLink();
Jiajia Qin729b2c62017-08-14 09:36:11 +08002362 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2363}
2364
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002365void Program::getActiveUniformBlockName(const GLuint blockIndex,
2366 GLsizei bufSize,
2367 GLsizei *length,
2368 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369{
jchen107ae70d82018-07-06 13:47:01 +08002370 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002371 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2372}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002374void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2375 GLsizei bufSize,
2376 GLsizei *length,
2377 GLchar *blockName) const
2378{
jchen107ae70d82018-07-06 13:47:01 +08002379 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002380 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002381}
2382
Qin Jiajia9bf55522018-01-29 13:56:23 +08002383template <typename T>
2384GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002385{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386 int maxLength = 0;
2387
2388 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002389 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002390 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002392 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002394 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002395 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396 }
2397 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002398 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002399
2400 return maxLength;
2401}
2402
Qin Jiajia9bf55522018-01-29 13:56:23 +08002403GLint Program::getActiveUniformBlockMaxNameLength() const
2404{
jchen107ae70d82018-07-06 13:47:01 +08002405 resolveLink();
Qin Jiajia9bf55522018-01-29 13:56:23 +08002406 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2407}
2408
2409GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2410{
jchen107ae70d82018-07-06 13:47:01 +08002411 resolveLink();
Qin Jiajia9bf55522018-01-29 13:56:23 +08002412 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2413}
2414
Geoff Lange1a27752015-10-05 13:16:04 -04002415GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002416{
jchen107ae70d82018-07-06 13:47:01 +08002417 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002418 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2419}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002420
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002421GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2422{
jchen107ae70d82018-07-06 13:47:01 +08002423 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002424 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002425}
2426
Jiajia Qin729b2c62017-08-14 09:36:11 +08002427const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002428{
jchen107ae70d82018-07-06 13:47:01 +08002429 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002430 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2431 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002432}
2433
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002434const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2435{
jchen107ae70d82018-07-06 13:47:01 +08002436 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002437 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2438 return mState.mShaderStorageBlocks[index];
2439}
2440
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002441void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2442{
jchen107ae70d82018-07-06 13:47:01 +08002443 resolveLink();
jchen107a20b972017-06-13 14:25:26 +08002444 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002445 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002446 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002447}
2448
2449GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2450{
jchen107ae70d82018-07-06 13:47:01 +08002451 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002452 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002453}
2454
Jiajia Qin729b2c62017-08-14 09:36:11 +08002455GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2456{
jchen107ae70d82018-07-06 13:47:01 +08002457 resolveLink();
Jiajia Qin729b2c62017-08-14 09:36:11 +08002458 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2459}
2460
Jamie Madill493f9572018-05-24 19:52:15 -04002461void Program::setTransformFeedbackVaryings(GLsizei count,
2462 const GLchar *const *varyings,
2463 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002464{
jchen107ae70d82018-07-06 13:47:01 +08002465 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002466 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002467 for (GLsizei i = 0; i < count; i++)
2468 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002469 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002470 }
2471
Jamie Madill48ef11b2016-04-27 15:21:52 -04002472 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002473}
2474
Jamie Madill493f9572018-05-24 19:52:15 -04002475void Program::getTransformFeedbackVarying(GLuint index,
2476 GLsizei bufSize,
2477 GLsizei *length,
2478 GLsizei *size,
2479 GLenum *type,
2480 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002481{
jchen107ae70d82018-07-06 13:47:01 +08002482 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002483 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002484 {
jchen10a9042d32017-03-17 08:50:45 +08002485 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2486 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2487 std::string varName = var.nameWithArrayIndex();
2488 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002489 if (length)
2490 {
2491 *length = lastNameIdx;
2492 }
2493 if (size)
2494 {
jchen10a9042d32017-03-17 08:50:45 +08002495 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002496 }
2497 if (type)
2498 {
jchen10a9042d32017-03-17 08:50:45 +08002499 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002500 }
2501 if (name)
2502 {
jchen10a9042d32017-03-17 08:50:45 +08002503 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002504 name[lastNameIdx] = '\0';
2505 }
2506 }
2507}
2508
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002509GLsizei Program::getTransformFeedbackVaryingCount() const
2510{
jchen107ae70d82018-07-06 13:47:01 +08002511 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002512 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002513 {
jchen10a9042d32017-03-17 08:50:45 +08002514 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002515 }
2516 else
2517 {
2518 return 0;
2519 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002520}
2521
2522GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2523{
jchen107ae70d82018-07-06 13:47:01 +08002524 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002525 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002526 {
2527 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002528 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002529 {
jchen10a9042d32017-03-17 08:50:45 +08002530 maxSize =
2531 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002532 }
2533
2534 return maxSize;
2535 }
2536 else
2537 {
2538 return 0;
2539 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002540}
2541
2542GLenum Program::getTransformFeedbackBufferMode() const
2543{
jchen107ae70d82018-07-06 13:47:01 +08002544 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002545 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002546}
2547
jchen103fd614d2018-08-13 12:21:58 +08002548bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002549{
Jiawei Shao016105b2018-04-12 16:38:31 +08002550 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2551 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2552 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2553 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002554
Jamie Madill493f9572018-05-24 19:52:15 -04002555 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002556 bool isGraphicsShaderAttached =
2557 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002558 // Check whether we both have a compute and non-compute shaders attached.
2559 // If there are of both types attached, then linking should fail.
2560 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2561 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2562 {
2563 infoLog << "Both compute and graphics shaders are attached to the same program.";
2564 return false;
2565 }
2566
2567 if (computeShader)
2568 {
jchen103fd614d2018-08-13 12:21:58 +08002569 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002570 {
2571 infoLog << "Attached compute shader is not compiled.";
2572 return false;
2573 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002574 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002575
jchen103fd614d2018-08-13 12:21:58 +08002576 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002577
2578 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2579 // If the work group size is not specified, a link time error should occur.
2580 if (!mState.mComputeShaderLocalSize.isDeclared())
2581 {
2582 infoLog << "Work group size is not specified.";
2583 return false;
2584 }
2585 }
2586 else
2587 {
jchen103fd614d2018-08-13 12:21:58 +08002588 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002589 {
2590 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2591 return false;
2592 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002593 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002594
jchen103fd614d2018-08-13 12:21:58 +08002595 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002596 {
2597 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2598 return false;
2599 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002600 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002601
jchen103fd614d2018-08-13 12:21:58 +08002602 int vertexShaderVersion = vertexShader->getShaderVersion();
2603 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002604 {
2605 infoLog << "Fragment shader version does not match vertex shader version.";
2606 return false;
2607 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002608
2609 if (geometryShader)
2610 {
2611 // [GL_EXT_geometry_shader] Chapter 7
2612 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2613 // Language Specification, as well as any of the following reasons:
2614 // * One or more of the shader objects attached to <program> are not compiled
2615 // successfully.
2616 // * The shaders do not use the same shader language version.
2617 // * <program> contains objects to form a geometry shader, and
2618 // - <program> is not separable and contains no objects to form a vertex shader; or
2619 // - the input primitive type, output primitive type, or maximum output vertex count
2620 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002621 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002622 {
2623 infoLog << "The attached geometry shader isn't compiled.";
2624 return false;
2625 }
2626
jchen103fd614d2018-08-13 12:21:58 +08002627 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002628 {
2629 mInfoLog << "Geometry shader version does not match vertex shader version.";
2630 return false;
2631 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002632 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002633
Jamie Madill493f9572018-05-24 19:52:15 -04002634 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002635 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002636 if (!inputPrimitive.valid())
2637 {
2638 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2639 return false;
2640 }
2641
Jamie Madill493f9572018-05-24 19:52:15 -04002642 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002643 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002644 if (!outputPrimitive.valid())
2645 {
2646 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2647 return false;
2648 }
2649
jchen103fd614d2018-08-13 12:21:58 +08002650 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002651 if (!maxVertices.valid())
2652 {
2653 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2654 return false;
2655 }
2656
2657 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2658 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2659 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002660 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002661 }
Jiawei Shao73618602017-12-20 15:47:15 +08002662 }
2663
2664 return true;
2665}
2666
jchen10910a3da2017-11-15 09:40:11 +08002667GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2668{
jchen107ae70d82018-07-06 13:47:01 +08002669 resolveLink();
jchen10910a3da2017-11-15 09:40:11 +08002670 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2671 {
2672 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2673 if (tf.nameWithArrayIndex() == name)
2674 {
2675 return tfIndex;
2676 }
2677 }
2678 return GL_INVALID_INDEX;
2679}
2680
2681const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2682{
jchen107ae70d82018-07-06 13:47:01 +08002683 resolveLink();
jchen10910a3da2017-11-15 09:40:11 +08002684 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2685 return mState.mLinkedTransformFeedbackVaryings[index];
2686}
2687
jchen103fd614d2018-08-13 12:21:58 +08002688bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002689{
Jiawei Shao016105b2018-04-12 16:38:31 +08002690 Shader *previousShader = nullptr;
2691 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002692 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002693 Shader *currentShader = mState.mAttachedShaders[shaderType];
2694 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002695 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002696 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002697 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002698
2699 if (previousShader)
2700 {
jchen103fd614d2018-08-13 12:21:58 +08002701 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002702 {
2703 return false;
2704 }
2705 }
2706 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002707 }
2708
jchen103fd614d2018-08-13 12:21:58 +08002709 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002710 {
2711 return false;
2712 }
2713
jchen103fd614d2018-08-13 12:21:58 +08002714 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002715 {
2716 return false;
2717 }
2718
2719 return true;
2720}
2721
2722// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2723// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002724bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002725 gl::Shader *consumingShader,
2726 gl::InfoLog &infoLog) const
2727{
jchen103fd614d2018-08-13 12:21:58 +08002728 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002729
jchen103fd614d2018-08-13 12:21:58 +08002730 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2731 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002732
Jiawei Shao385b3e02018-03-21 09:43:28 +08002733 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002734
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002735 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002736 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002737 bool matched = false;
2738
2739 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002740 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002741 {
2742 continue;
2743 }
2744
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002745 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002746 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002747 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002748 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002749 ASSERT(!output.isBuiltIn());
2750
2751 std::string mismatchedStructFieldName;
2752 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002753 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002754 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002755 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002756 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002757 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2758 mismatchedStructFieldName, generatingShader->getType(),
2759 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002760 return false;
2761 }
2762
Geoff Lang7dd2e102014-11-10 15:19:26 -05002763 matched = true;
2764 break;
2765 }
2766 }
2767
Olli Etuaho107c7242018-03-20 15:45:35 +02002768 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2769 // whether the input is statically used - a statically used input should fail this test even
2770 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002771 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002772 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002773 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2774 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2775 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002776 return false;
2777 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002778 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002779
Jiawei Shaod063aff2018-02-22 10:19:09 +08002780 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002781
Jiawei Shaod063aff2018-02-22 10:19:09 +08002782 return true;
2783}
2784
jchen103fd614d2018-08-13 12:21:58 +08002785bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002786{
Jiawei Shao016105b2018-04-12 16:38:31 +08002787 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002788
2789 std::map<GLuint, std::string> staticFragmentInputLocations;
2790
2791 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002792 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002793 for (const sh::Varying &input : fragmentInputVaryings)
2794 {
2795 if (input.isBuiltIn() || !input.staticUse)
2796 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002797 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002798 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002799
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002800 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002801 if (inputBinding == -1)
2802 continue;
2803
2804 const auto it = staticFragmentInputLocations.find(inputBinding);
2805 if (it == std::end(staticFragmentInputLocations))
2806 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002807 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002808 }
2809 else
2810 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002811 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002812 << it->second;
2813 return false;
2814 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002815 }
2816
2817 return true;
2818}
2819
jchen103fd614d2018-08-13 12:21:58 +08002820bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002821 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002822 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002823 GLuint *combinedImageUniformsCount,
2824 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002825{
Olli Etuahob78707c2017-03-09 15:03:11 +00002826 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002827 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002828 {
2829 return false;
2830 }
2831
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002832 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002833
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002834 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002835
jchen10eaef1e52017-06-13 10:44:11 +08002836 if (!linkAtomicCounterBuffers())
2837 {
2838 return false;
2839 }
2840
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002841 return true;
2842}
2843
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002844void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002845{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002846 ASSERT(combinedImageUniforms);
2847
Jamie Madill982f6e02017-06-07 14:33:04 -04002848 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2849 unsigned int low = high;
2850
jchen10eaef1e52017-06-13 10:44:11 +08002851 for (auto counterIter = mState.mUniforms.rbegin();
2852 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2853 {
2854 --low;
2855 }
2856
2857 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2858
2859 high = low;
2860
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002861 for (auto imageIter = mState.mUniforms.rbegin();
2862 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2863 {
2864 --low;
2865 }
2866
2867 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002868 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002869 // If uniform is a image type, insert it into the mImageBindings array.
2870 for (unsigned int imageIndex : mState.mImageUniformRange)
2871 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002872 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2873 // cannot load values into a uniform defined as an image. if declare without a
2874 // binding qualifier, any uniform image variable (include all elements of
2875 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002876 auto &imageUniform = mState.mUniforms[imageIndex];
2877 if (imageUniform.binding == -1)
2878 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002879 mState.mImageBindings.emplace_back(
2880 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002881 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002882 else
2883 {
2884 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002885 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002886 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002887
2888 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2889 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002890 }
2891
2892 high = low;
2893
2894 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002895 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002896 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002897 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002898 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002899
2900 mState.mSamplerUniformRange = RangeUI(low, high);
2901
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002902 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002903 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002904 {
2905 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002906 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002907 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2908 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002909 }
2910}
2911
jchen10eaef1e52017-06-13 10:44:11 +08002912bool Program::linkAtomicCounterBuffers()
2913{
2914 for (unsigned int index : mState.mAtomicCounterUniformRange)
2915 {
Jamie Madill493f9572018-05-24 19:52:15 -04002916 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002917 uniform.blockInfo.offset = uniform.offset;
2918 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2919 uniform.blockInfo.matrixStride = 0;
2920 uniform.blockInfo.isRowMajorMatrix = false;
2921
Jamie Madill493f9572018-05-24 19:52:15 -04002922 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002923 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2924 ++bufferIndex)
2925 {
2926 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2927 if (buffer.binding == uniform.binding)
2928 {
2929 buffer.memberIndexes.push_back(index);
2930 uniform.bufferIndex = bufferIndex;
2931 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002932 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002933 break;
2934 }
2935 }
2936 if (!found)
2937 {
2938 AtomicCounterBuffer atomicCounterBuffer;
2939 atomicCounterBuffer.binding = uniform.binding;
2940 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002941 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002942 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2943 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2944 }
2945 }
2946 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002947 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002948
2949 return true;
2950}
2951
Jamie Madilleb979bf2016-11-15 12:28:46 -05002952// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002953bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002954{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002955 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002956
jchen103fd614d2018-08-13 12:21:58 +08002957 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002958
Geoff Lang7dd2e102014-11-10 15:19:26 -05002959 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002960 if (shaderVersion >= 300)
2961 {
2962 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2963 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002964 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002965 }
2966 else
2967 {
2968 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002969 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002970 }
jchen103fd614d2018-08-13 12:21:58 +08002971 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002972
2973 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002974 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002975 {
Jamie Madillf6113162015-05-07 11:49:21 -04002976 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002977 return false;
2978 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002979
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002980 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002981
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002982 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002983 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002984 {
Olli Etuahod2551232017-10-26 20:03:33 +03002985 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2986 // structures, so we don't need to worry about adjusting their names or generating entries
2987 // for each member/element (unlike uniforms for example).
2988 ASSERT(!attribute.isArray() && !attribute.isStruct());
2989
Jamie Madilleb979bf2016-11-15 12:28:46 -05002990 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002991 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002992 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002993 attribute.location = bindingLocation;
2994 }
2995
2996 if (attribute.location != -1)
2997 {
2998 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002999 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003000
Jamie Madill63805b42015-08-25 13:17:39 -04003001 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003002 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003003 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3004 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003005
3006 return false;
3007 }
3008
Jamie Madill63805b42015-08-25 13:17:39 -04003009 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003010 {
Jamie Madill63805b42015-08-25 13:17:39 -04003011 const int regLocation = attribute.location + reg;
3012 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003013
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003014 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3015 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3016 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003017 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003018 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003019 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003020 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003021 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003022 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003023 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003024 return false;
3025 }
3026 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003027 else
3028 {
Jamie Madill63805b42015-08-25 13:17:39 -04003029 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003030 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003031
Jamie Madill63805b42015-08-25 13:17:39 -04003032 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003033 }
3034 }
3035 }
3036
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003037 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003038 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003039 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003040 // Not set by glBindAttribLocation or by location layout qualifier
3041 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003042 {
Jamie Madill63805b42015-08-25 13:17:39 -04003043 int regs = VariableRegisterCount(attribute.type);
3044 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003045
Jamie Madill63805b42015-08-25 13:17:39 -04003046 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003047 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003048 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003049 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003050 }
3051
Jamie Madillc349ec02015-08-21 16:53:12 -04003052 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003053 }
3054 }
3055
Brandon Jonesc405ae72017-12-06 14:15:03 -08003056 ASSERT(mState.mAttributesTypeMask.none());
3057 ASSERT(mState.mAttributesMask.none());
3058
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003059 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3060 // shader versions we're only processing active attributes to begin with.
3061 if (shaderVersion >= 300)
3062 {
3063 for (auto attributeIter = mState.mAttributes.begin();
3064 attributeIter != mState.mAttributes.end();)
3065 {
3066 if (attributeIter->active)
3067 {
3068 ++attributeIter;
3069 }
3070 else
3071 {
3072 attributeIter = mState.mAttributes.erase(attributeIter);
3073 }
3074 }
3075 }
3076
Jamie Madill48ef11b2016-04-27 15:21:52 -04003077 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003078 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003079 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003080 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003081 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003082
Jamie Madillbd159f02017-10-09 19:39:06 -04003083 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003084 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003085 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3086 mState.mActiveAttribLocationsMask.set(location);
3087 mState.mMaxActiveAttribLocation =
3088 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003089
3090 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3091 if (!attribute.isBuiltIn())
3092 {
3093 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3094 location);
3095 mState.mAttributesMask.set(location);
3096 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003097 }
3098 }
3099
Geoff Lang7dd2e102014-11-10 15:19:26 -05003100 return true;
3101}
3102
jchen103fd614d2018-08-13 12:21:58 +08003103bool Program::linkInterfaceBlocks(const Caps &caps,
3104 const Version &version,
3105 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003106 InfoLog &infoLog,
3107 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003108{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003109 ASSERT(combinedShaderStorageBlocksCount);
3110
Jiawei Shao54aafe52018-04-27 14:54:57 +08003111 GLuint combinedUniformBlocksCount = 0u;
3112 GLuint numShadersHasUniformBlocks = 0u;
3113 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3114 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003115 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003116 Shader *shader = mState.mAttachedShaders[shaderType];
3117 if (!shader)
3118 {
3119 continue;
3120 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003121
jchen103fd614d2018-08-13 12:21:58 +08003122 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003123 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003124 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003125 if (!ValidateInterfaceBlocksCount(
3126 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3127 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3128 {
3129 return false;
3130 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003131
Jiawei Shao54aafe52018-04-27 14:54:57 +08003132 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3133 ++numShadersHasUniformBlocks;
3134 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003135 }
3136
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003137 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3138 {
3139 infoLog << "The sum of the number of active uniform blocks exceeds "
3140 "MAX_COMBINED_UNIFORM_BLOCKS ("
3141 << caps.maxCombinedUniformBlocks << ").";
3142 return false;
3143 }
3144
Jiawei Shao54aafe52018-04-27 14:54:57 +08003145 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3146 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003147 {
3148 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003149 }
Jamie Madille473dee2015-08-18 14:49:01 -04003150
jchen103fd614d2018-08-13 12:21:58 +08003151 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003152 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003153 *combinedShaderStorageBlocksCount = 0u;
3154 GLuint numShadersHasShaderStorageBlocks = 0u;
3155 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3156 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003157 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003158 Shader *shader = mState.mAttachedShaders[shaderType];
3159 if (!shader)
3160 {
3161 continue;
3162 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003163
jchen103fd614d2018-08-13 12:21:58 +08003164 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003165 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003166 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003167 if (!ValidateInterfaceBlocksCount(
3168 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3169 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3170 {
3171 return false;
3172 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003173
Jiawei Shao54aafe52018-04-27 14:54:57 +08003174 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3175 ++numShadersHasShaderStorageBlocks;
3176 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003177 }
3178
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003179 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003180 {
3181 infoLog << "The sum of the number of active shader storage blocks exceeds "
3182 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3183 << caps.maxCombinedShaderStorageBlocks << ").";
3184 return false;
3185 }
3186
Jiawei Shao54aafe52018-04-27 14:54:57 +08003187 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3188 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003189 {
3190 return false;
3191 }
3192 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003193
Geoff Lang7dd2e102014-11-10 15:19:26 -05003194 return true;
3195}
3196
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003197LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3198 const sh::ShaderVariable &variable2,
3199 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003200 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003201 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003202{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003203 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003204 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003205 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003206 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003207 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003208 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003209 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003210 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003211 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003212 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003213 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003214 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003215 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003216 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003217 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003218 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003219
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003220 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003221 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003222 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003223 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003224 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003225 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3226 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003227 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3228 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003229
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003230 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003231 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003232 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003233 }
3234
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003235 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003236 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003237 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003238 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003239 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3240 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003241 }
3242 }
3243
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003244 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003245}
3246
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003247LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3248 const sh::Varying &inputVarying,
3249 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003250 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003251 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003252{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003253 if (validateGeometryShaderInputVarying)
3254 {
3255 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3256 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3257 // or outputs.
3258 ASSERT(inputVarying.arraySizes.size() == 1u);
3259
3260 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3261 // varying cannot match a geometry shader input varying.
3262 // [GL_EXT_geometry_shader] Section 7.4.1:
3263 // Geometry shader per-vertex input variables and blocks are required to be declared as
3264 // arrays, with each element representing input or output values for a single vertex of a
3265 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3266 // are treated as though they were not declared as arrays.
3267 if (outputVarying.isArray())
3268 {
3269 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3270 }
3271 }
3272
3273 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3274 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003275 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003276 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3277 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003278 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003279 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003280 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003281 }
3282
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003283 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003284 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003285 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003286 }
3287
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003288 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003289 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003290 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003291 }
3292
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003293 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003294}
3295
jchen103fd614d2018-08-13 12:21:58 +08003296bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003297{
Jiawei Shao016105b2018-04-12 16:38:31 +08003298 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3299 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003300 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3301 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3302 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003303
3304 if (shaderVersion != 100)
3305 {
3306 // Only ESSL 1.0 has restrictions on matching input and output invariance
3307 return true;
3308 }
3309
3310 bool glPositionIsInvariant = false;
3311 bool glPointSizeIsInvariant = false;
3312 bool glFragCoordIsInvariant = false;
3313 bool glPointCoordIsInvariant = false;
3314
3315 for (const sh::Varying &varying : vertexVaryings)
3316 {
3317 if (!varying.isBuiltIn())
3318 {
3319 continue;
3320 }
3321 if (varying.name.compare("gl_Position") == 0)
3322 {
3323 glPositionIsInvariant = varying.isInvariant;
3324 }
3325 else if (varying.name.compare("gl_PointSize") == 0)
3326 {
3327 glPointSizeIsInvariant = varying.isInvariant;
3328 }
3329 }
3330
3331 for (const sh::Varying &varying : fragmentVaryings)
3332 {
3333 if (!varying.isBuiltIn())
3334 {
3335 continue;
3336 }
3337 if (varying.name.compare("gl_FragCoord") == 0)
3338 {
3339 glFragCoordIsInvariant = varying.isInvariant;
3340 }
3341 else if (varying.name.compare("gl_PointCoord") == 0)
3342 {
3343 glPointCoordIsInvariant = varying.isInvariant;
3344 }
3345 }
3346
3347 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3348 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3349 // Not requiring invariance to match is supported by:
3350 // dEQP, WebGL CTS, Nexus 5X GLES
3351 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3352 {
3353 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3354 "declared invariant.";
3355 return false;
3356 }
3357 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3358 {
3359 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3360 "declared invariant.";
3361 return false;
3362 }
3363
3364 return true;
3365}
3366
jchen103fd614d2018-08-13 12:21:58 +08003367bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003368 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003369 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003370 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003371{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003372
jchen108225e732017-11-14 16:29:03 +08003373 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003374 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003375 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003376 {
jchen103fd614d2018-08-13 12:21:58 +08003377 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003378 {
Geoff Lang1a683462015-09-29 15:09:59 -04003379 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003380 return false;
3381 }
jchen103fd614d2018-08-13 12:21:58 +08003382 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003383 {
3384 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3385 {
3386 infoLog << "Two transform feedback varyings include the same array element ("
3387 << tfVaryingName << ").";
3388 return false;
3389 }
3390 }
3391 else
3392 {
3393 if (uniqueNames.count(tfVaryingName) > 0)
3394 {
3395 infoLog << "Two transform feedback varyings specify the same output variable ("
3396 << tfVaryingName << ").";
3397 return false;
3398 }
3399 }
3400 uniqueNames.insert(tfVaryingName);
3401 }
3402
3403 // Validate against program varyings.
3404 size_t totalComponents = 0;
3405 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3406 {
3407 std::vector<unsigned int> subscripts;
3408 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3409
3410 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3411 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003412 {
3413 infoLog << "Transform feedback varying " << tfVaryingName
3414 << " does not exist in the vertex shader.";
3415 return false;
3416 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003417
jchen108225e732017-11-14 16:29:03 +08003418 // Validate the matching variable.
3419 if (var->isStruct())
3420 {
3421 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3422 return false;
3423 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003424
jchen108225e732017-11-14 16:29:03 +08003425 size_t elementCount = 0;
3426 size_t componentCount = 0;
3427
3428 if (var->isArray())
3429 {
jchen103fd614d2018-08-13 12:21:58 +08003430 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003431 {
3432 infoLog << "Capture of arrays is undefined and not supported.";
3433 return false;
3434 }
3435
3436 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3437 ASSERT(!var->isArrayOfArrays());
3438
3439 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3440 {
3441 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3442 return false;
3443 }
3444 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3445 }
3446 else
3447 {
3448 if (!subscripts.empty())
3449 {
3450 infoLog << "Varying '" << baseName
3451 << "' is not an array to be captured by element.";
3452 return false;
3453 }
3454 elementCount = 1;
3455 }
3456
3457 // TODO(jmadill): Investigate implementation limits on D3D11
3458 componentCount = VariableComponentCount(var->type) * elementCount;
3459 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3460 componentCount > caps.maxTransformFeedbackSeparateComponents)
3461 {
3462 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3463 << componentCount << ") exceed the maximum separate components ("
3464 << caps.maxTransformFeedbackSeparateComponents << ").";
3465 return false;
3466 }
3467
3468 totalComponents += componentCount;
3469 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3470 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3471 {
3472 infoLog << "Transform feedback varying total components (" << totalComponents
3473 << ") exceed the maximum interleaved components ("
3474 << caps.maxTransformFeedbackInterleavedComponents << ").";
3475 return false;
3476 }
3477 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003478 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003479}
3480
jchen103fd614d2018-08-13 12:21:58 +08003481bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003482{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003483 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003484 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003485
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003486 for (const auto &attrib : attributes)
3487 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003488 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003489 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003490 Shader *shader = mState.mAttachedShaders[shaderType];
3491 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003492 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003493 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003494 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003495
jchen103fd614d2018-08-13 12:21:58 +08003496 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003497 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003498 {
3499 if (uniform.name == attrib.name)
3500 {
3501 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3502 return false;
3503 }
3504 }
3505 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003506 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003507
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003508 return true;
3509}
3510
Jamie Madill3c1da042017-11-27 18:33:40 -05003511void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003512{
3513 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003514 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003515 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003516 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003517 std::vector<unsigned int> subscripts;
3518 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003519 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003520 if (!subscripts.empty())
3521 {
3522 subscript = subscripts.back();
3523 }
Jamie Madill192745a2016-12-22 15:58:21 -05003524 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003525 {
Jamie Madill192745a2016-12-22 15:58:21 -05003526 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003527 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003528 {
jchen10a9042d32017-03-17 08:50:45 +08003529 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3530 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003531 break;
3532 }
jchen108225e732017-11-14 16:29:03 +08003533 else if (varying->isStruct())
3534 {
3535 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3536 if (field != nullptr)
3537 {
3538 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3539 break;
3540 }
3541 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003542 }
3543 }
James Darpinian30b604d2018-03-12 17:26:57 -07003544 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003545}
3546
jchen103fd614d2018-08-13 12:21:58 +08003547ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003548{
Jamie Madill3c1da042017-11-27 18:33:40 -05003549 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003550
Jiawei Shao016105b2018-04-12 16:38:31 +08003551 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003552 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003553 {
Jamie Madill192745a2016-12-22 15:58:21 -05003554 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003555 }
3556
Jiawei Shao016105b2018-04-12 16:38:31 +08003557 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003558 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003559 {
Jamie Madill192745a2016-12-22 15:58:21 -05003560 merged[varying.name].fragment = &varying;
3561 }
3562
3563 return merged;
3564}
3565
jchen103fd614d2018-08-13 12:21:58 +08003566bool Program::linkOutputVariables(const Caps &caps,
3567 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003568 GLuint combinedImageUniformsCount,
3569 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003570{
Jiawei Shao016105b2018-04-12 16:38:31 +08003571 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003572 ASSERT(fragmentShader != nullptr);
3573
Geoff Lange0cff192017-05-30 13:04:56 -04003574 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003575 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003576 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003577
jchen103fd614d2018-08-13 12:21:58 +08003578 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003579 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003580 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003581 {
3582 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3583 outputVariable.name != "gl_FragData")
3584 {
3585 continue;
3586 }
3587
3588 unsigned int baseLocation =
3589 (outputVariable.location == -1 ? 0u
3590 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003591
3592 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3593 // structures, so we may use getBasicTypeElementCount().
3594 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3595 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003596 {
3597 const unsigned int location = baseLocation + elementIndex;
3598 if (location >= mState.mOutputVariableTypes.size())
3599 {
3600 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3601 }
Corentin Walleze7557742017-06-01 13:09:57 -04003602 ASSERT(location < mState.mActiveOutputVariables.size());
3603 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003604 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003605 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003606 }
3607 }
3608
jchen103fd614d2018-08-13 12:21:58 +08003609 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003610 {
3611 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3612 // A link error will be generated if the sum of the number of active image uniforms used in
3613 // all shaders, the number of active shader storage blocks, and the number of active
3614 // fragment shader outputs exceeds the implementation-dependent value of
3615 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3616 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3617 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003618 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003619 {
3620 mInfoLog
3621 << "The sum of the number of active image uniforms, active shader storage blocks "
3622 "and active fragment shader outputs exceeds "
3623 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003624 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003625 return false;
3626 }
3627 }
3628
Jamie Madill80a6fc02015-08-21 16:53:16 -04003629 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003630 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003631 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003632
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003633 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003634 // TODO(jmadill): any caps validation here?
3635
jchen1015015f72017-03-16 13:54:21 +08003636 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003637 outputVariableIndex++)
3638 {
jchen1015015f72017-03-16 13:54:21 +08003639 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003640
Olli Etuahod2551232017-10-26 20:03:33 +03003641 if (outputVariable.isArray())
3642 {
3643 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3644 // Resources and including [0] at the end of array variable names.
3645 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3646 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3647 }
3648
Jamie Madill80a6fc02015-08-21 16:53:16 -04003649 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3650 if (outputVariable.isBuiltIn())
3651 continue;
3652
3653 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003654 unsigned int baseLocation =
3655 (outputVariable.location == -1 ? 0u
3656 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003657
Olli Etuaho465835d2017-09-26 13:34:10 +03003658 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3659 // structures, so we may use getBasicTypeElementCount().
3660 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3661 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003662 {
Olli Etuahod2551232017-10-26 20:03:33 +03003663 const unsigned int location = baseLocation + elementIndex;
3664 if (location >= mState.mOutputLocations.size())
3665 {
3666 mState.mOutputLocations.resize(location + 1);
3667 }
3668 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003669 if (outputVariable.isArray())
3670 {
3671 mState.mOutputLocations[location] =
3672 VariableLocation(elementIndex, outputVariableIndex);
3673 }
3674 else
3675 {
3676 VariableLocation locationInfo;
3677 locationInfo.index = outputVariableIndex;
3678 mState.mOutputLocations[location] = locationInfo;
3679 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003680 }
3681 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003682
3683 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003684}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003685
Olli Etuaho48fed632017-03-16 12:05:30 +00003686void Program::setUniformValuesFromBindingQualifiers()
3687{
Jamie Madill982f6e02017-06-07 14:33:04 -04003688 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003689 {
3690 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3691 if (samplerUniform.binding != -1)
3692 {
Olli Etuahod2551232017-10-26 20:03:33 +03003693 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003694 ASSERT(location != -1);
3695 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003696 for (unsigned int elementIndex = 0;
3697 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003698 {
3699 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3700 }
3701 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3702 boundTextureUnits.data());
3703 }
3704 }
3705}
3706
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003707void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003708{
jchen10af713a22017-04-19 09:10:56 +08003709 // Set initial bindings from shader.
3710 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3711 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003712 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003713 bindUniformBlock(blockIndex, uniformBlock.binding);
3714 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003715}
3716
Jamie Madille7d84322017-01-10 18:21:59 -05003717void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003718 GLsizei clampedCount,
3719 const GLint *v)
3720{
Jamie Madill81c2e252017-09-09 23:32:46 -04003721 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3722 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003723 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3724 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003725
Jamie Madill7e4eff12018-08-08 15:49:26 -04003726 if (samplerBinding.unreferenced)
3727 return;
3728
3729 // Update the sampler uniforms.
3730 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3731 {
3732 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3733 GLint newSamplerIndex = v[arrayIndex];
3734
3735 if (oldSamplerIndex == newSamplerIndex)
3736 continue;
3737
3738 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3739
3740 // Update the reference counts.
3741 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3742 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3743 ASSERT(oldRefCount > 0);
3744 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3745 oldRefCount--;
3746 newRefCount++;
3747
3748 // Check for binding type change.
3749 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3750 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3751
3752 if (newRefCount == 1)
3753 {
3754 newSamplerType = samplerBinding.textureType;
3755 mState.mActiveSamplersMask.set(newSamplerIndex);
3756 }
3757 else if (newSamplerType != samplerBinding.textureType)
3758 {
3759 // Conflict detected. Ensure we reset it properly.
3760 newSamplerType = TextureType::InvalidEnum;
3761 }
3762
3763 // Unset previously active sampler.
3764 if (oldRefCount == 0)
3765 {
3766 oldSamplerType = TextureType::InvalidEnum;
3767 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3768 }
3769 else if (oldSamplerType == TextureType::InvalidEnum)
3770 {
3771 // Previous conflict. Check if this new change fixed the conflict.
3772 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3773 }
3774 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003775
3776 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003777 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003778}
3779
Jamie Madill7e4eff12018-08-08 15:49:26 -04003780TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3781{
3782 TextureType foundType = TextureType::InvalidEnum;
3783
3784 for (const SamplerBinding &binding : mSamplerBindings)
3785 {
3786 if (binding.unreferenced)
3787 continue;
3788
3789 // A conflict exists if samplers of different types are sourced by the same texture unit.
3790 // We need to check all bound textures to detect this error case.
3791 for (GLuint textureUnit : binding.boundTextureUnits)
3792 {
3793 if (textureUnit == textureUnitIndex)
3794 {
3795 if (foundType == TextureType::InvalidEnum)
3796 {
3797 foundType = binding.textureType;
3798 }
3799 else if (foundType != binding.textureType)
3800 {
3801 return TextureType::InvalidEnum;
3802 }
3803 }
3804 }
3805 }
3806
3807 return foundType;
3808}
3809
Jamie Madille7d84322017-01-10 18:21:59 -05003810template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003811GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3812 GLsizei count,
3813 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003814 const T *v)
3815{
Jamie Madill134f93d2017-08-31 17:11:00 -04003816 if (count == 1)
3817 return 1;
3818
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003819 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003820
Corentin Wallez15ac5342016-11-03 17:06:39 -04003821 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3822 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003823 unsigned int remainingElements =
3824 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003825 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003826 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003827
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003828 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003829 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003830 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003831 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003832
3833 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003834}
3835
3836template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003837GLsizei Program::clampMatrixUniformCount(GLint location,
3838 GLsizei count,
3839 GLboolean transpose,
3840 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003841{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003842 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3843
Jamie Madill62d31cb2015-09-11 13:25:51 -04003844 if (!transpose)
3845 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003846 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003847 }
3848
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003849 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003850
3851 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3852 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003853 unsigned int remainingElements =
3854 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003855 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003856}
3857
Jamie Madill54164b02017-08-28 15:17:37 -04003858// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3859// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003860template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003861void Program::getUniformInternal(const Context *context,
3862 DestT *dataOut,
3863 GLint location,
3864 GLenum nativeType,
3865 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003866{
Jamie Madill54164b02017-08-28 15:17:37 -04003867 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003868 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003869 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003870 {
3871 GLint tempValue[16] = {0};
3872 mProgram->getUniformiv(context, location, tempValue);
3873 UniformStateQueryCastLoop<GLboolean>(
3874 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003875 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003876 }
3877 case GL_INT:
3878 {
3879 GLint tempValue[16] = {0};
3880 mProgram->getUniformiv(context, location, tempValue);
3881 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3882 components);
3883 break;
3884 }
3885 case GL_UNSIGNED_INT:
3886 {
3887 GLuint tempValue[16] = {0};
3888 mProgram->getUniformuiv(context, location, tempValue);
3889 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3890 components);
3891 break;
3892 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003893 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003894 {
3895 GLfloat tempValue[16] = {0};
3896 mProgram->getUniformfv(context, location, tempValue);
3897 UniformStateQueryCastLoop<GLfloat>(
3898 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003899 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003900 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003901 default:
3902 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003903 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003904 }
3905}
Jamie Madilla4595b82017-01-11 17:36:34 -05003906
3907bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3908{
jchen107ae70d82018-07-06 13:47:01 +08003909 resolveLink();
Jamie Madilla4595b82017-01-11 17:36:34 -05003910 // Must be called after samplers are validated.
3911 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3912
3913 for (const auto &binding : mState.mSamplerBindings)
3914 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003915 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003916 for (const auto &unit : binding.boundTextureUnits)
3917 {
3918 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3919 if (programTextureID == textureID)
3920 {
3921 // TODO(jmadill): Check for appropriate overlap.
3922 return true;
3923 }
3924 }
3925 }
3926
3927 return false;
3928}
3929
Jamie Madill70aeda42018-08-20 12:17:40 -04003930Error Program::syncState(const Context *context)
3931{
3932 if (mDirtyBits.any())
3933 {
3934 resolveLink();
3935 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
3936 mDirtyBits.reset();
3937 }
3938
3939 return NoError();
3940}
Jamie Madilla2c74982016-12-12 11:20:42 -05003941} // namespace gl