blob: 168b994b611cbbafe05481a81e19a108cd958aed [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 Madillb0a838b2016-11-13 20:02:12 -05001423 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001424#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425}
1426
Jamie Madilla2c74982016-12-12 11:20:42 -05001427Error Program::saveBinary(const Context *context,
1428 GLenum *binaryFormat,
1429 void *binary,
1430 GLsizei bufSize,
1431 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432{
jchen107ae70d82018-07-06 13:47:01 +08001433 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434 if (binaryFormat)
1435 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001436 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437 }
1438
Jamie Madill4f86d052017-06-05 12:59:26 -04001439 angle::MemoryBuffer memoryBuf;
1440 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001441
Jamie Madill4f86d052017-06-05 12:59:26 -04001442 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1443 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444
1445 if (streamLength > bufSize)
1446 {
1447 if (length)
1448 {
1449 *length = 0;
1450 }
1451
Jamie Madill493f9572018-05-24 19:52:15 -04001452 // TODO: This should be moved to the validation layer but computing the size of the binary
1453 // before saving it causes the save to happen twice. It may be possible to write the binary
1454 // to a separate buffer, validate sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001455 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456 }
1457
1458 if (binary)
1459 {
Jamie Madill493f9572018-05-24 19:52:15 -04001460 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461
Jamie Madill48ef11b2016-04-27 15:21:52 -04001462 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463 ptr += streamLength;
1464
1465 ASSERT(ptr - streamLength == binary);
1466 }
1467
1468 if (length)
1469 {
1470 *length = streamLength;
1471 }
1472
He Yunchaoacd18982017-01-04 10:46:42 +08001473 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
Jamie Madillffe00c02017-06-27 16:26:55 -04001476GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477{
jchen107ae70d82018-07-06 13:47:01 +08001478 resolveLink();
Geoff Langb26ab822018-05-29 11:19:00 -04001479 if (!mLinked)
1480 {
1481 return 0;
1482 }
1483
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001485 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486 if (error.isError())
1487 {
1488 return 0;
1489 }
1490
1491 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001492}
1493
Geoff Langc5629752015-12-07 16:29:04 -05001494void Program::setBinaryRetrievableHint(bool retrievable)
1495{
jchen107ae70d82018-07-06 13:47:01 +08001496 resolveLink();
Geoff Langc5629752015-12-07 16:29:04 -05001497 // TODO(jmadill) : replace with dirty bits
1498 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001499 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001500}
1501
1502bool Program::getBinaryRetrievableHint() const
1503{
jchen107ae70d82018-07-06 13:47:01 +08001504 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001505 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001506}
1507
Yunchao He61afff12017-03-14 15:34:03 +08001508void Program::setSeparable(bool separable)
1509{
jchen107ae70d82018-07-06 13:47:01 +08001510 resolveLink();
Yunchao He61afff12017-03-14 15:34:03 +08001511 // TODO(yunchao) : replace with dirty bits
1512 if (mState.mSeparable != separable)
1513 {
1514 mProgram->setSeparable(separable);
1515 mState.mSeparable = separable;
1516 }
1517}
1518
1519bool Program::isSeparable() const
1520{
jchen107ae70d82018-07-06 13:47:01 +08001521 resolveLink();
Yunchao He61afff12017-03-14 15:34:03 +08001522 return mState.mSeparable;
1523}
1524
Jamie Madill6c1f6712017-02-14 19:08:04 -05001525void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001526{
jchen107ae70d82018-07-06 13:47:01 +08001527 resolveLink();
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001528 mRefCount--;
1529
1530 if (mRefCount == 0 && mDeleteStatus)
1531 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001532 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001533 }
1534}
1535
1536void Program::addRef()
1537{
jchen107ae70d82018-07-06 13:47:01 +08001538 resolveLink();
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001539 mRefCount++;
1540}
1541
1542unsigned int Program::getRefCount() const
1543{
1544 return mRefCount;
1545}
1546
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001547int Program::getInfoLogLength() const
1548{
jchen107ae70d82018-07-06 13:47:01 +08001549 resolveLink();
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001550 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001551}
1552
Geoff Lange1a27752015-10-05 13:16:04 -04001553void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001554{
jchen107ae70d82018-07-06 13:47:01 +08001555 resolveLink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001556 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001557}
1558
Geoff Lange1a27752015-10-05 13:16:04 -04001559void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001560{
jchen107ae70d82018-07-06 13:47:01 +08001561 resolveLink();
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001562 int total = 0;
1563
Jiawei Shao016105b2018-04-12 16:38:31 +08001564 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001565 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001566 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001567 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001568 shaders[total] = shader->getHandle();
1569 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001570 }
1571 }
1572
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001573 if (count)
1574 {
1575 *count = total;
1576 }
1577}
1578
Geoff Lange1a27752015-10-05 13:16:04 -04001579GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580{
jchen107ae70d82018-07-06 13:47:01 +08001581 resolveLink();
Jamie Madill34ca4f52017-06-13 11:49:39 -04001582 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
Jamie Madill63805b42015-08-25 13:17:39 -04001585bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001586{
jchen107ae70d82018-07-06 13:47:01 +08001587 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001588 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1589 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590}
1591
jchen10fd7c3b52017-03-21 15:36:03 +08001592void Program::getActiveAttribute(GLuint index,
1593 GLsizei bufsize,
1594 GLsizei *length,
1595 GLint *size,
1596 GLenum *type,
1597 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001598{
jchen107ae70d82018-07-06 13:47:01 +08001599 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001600 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001601 {
1602 if (bufsize > 0)
1603 {
1604 name[0] = '\0';
1605 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001607 if (length)
1608 {
1609 *length = 0;
1610 }
1611
1612 *type = GL_NONE;
1613 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001614 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001615 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001616
jchen1036e120e2017-03-14 14:53:58 +08001617 ASSERT(index < mState.mAttributes.size());
1618 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001619
1620 if (bufsize > 0)
1621 {
jchen10fd7c3b52017-03-21 15:36:03 +08001622 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001623 }
1624
1625 // Always a single 'type' instance
1626 *size = 1;
1627 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001628}
1629
Geoff Lange1a27752015-10-05 13:16:04 -04001630GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001631{
jchen107ae70d82018-07-06 13:47:01 +08001632 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001633 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001634 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001635 return 0;
1636 }
1637
jchen1036e120e2017-03-14 14:53:58 +08001638 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001639}
1640
Geoff Lange1a27752015-10-05 13:16:04 -04001641GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001642{
jchen107ae70d82018-07-06 13:47:01 +08001643 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001644 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001645 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001646 return 0;
1647 }
1648
1649 size_t maxLength = 0;
1650
Jamie Madill48ef11b2016-04-27 15:21:52 -04001651 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001652 {
jchen1036e120e2017-03-14 14:53:58 +08001653 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001654 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655
Jamie Madillc349ec02015-08-21 16:53:12 -04001656 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001657}
1658
jchen107ae70d82018-07-06 13:47:01 +08001659const std::vector<sh::Attribute> &Program::getAttributes() const
1660{
1661 resolveLink();
1662 return mState.mAttributes;
1663}
1664
1665const AttributesMask &Program::getActiveAttribLocationsMask() const
1666{
1667 resolveLink();
1668 return mState.mActiveAttribLocationsMask;
1669}
1670
1671const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1672{
1673 resolveLink();
1674 return mState.mSamplerBindings;
1675}
1676
1677const std::vector<ImageBinding> &Program::getImageBindings() const
1678{
1679 resolveLink();
1680 return mState.mImageBindings;
1681}
1682const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1683{
1684 resolveLink();
1685 return mState.mComputeShaderLocalSize;
1686}
1687
1688PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1689{
1690 resolveLink();
1691 return mState.mGeometryShaderInputPrimitiveType;
1692}
1693PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1694{
1695 resolveLink();
1696 return mState.mGeometryShaderOutputPrimitiveType;
1697}
1698GLint Program::getGeometryShaderInvocations() const
1699{
1700 resolveLink();
1701 return mState.mGeometryShaderInvocations;
1702}
1703GLint Program::getGeometryShaderMaxVertices() const
1704{
1705 resolveLink();
1706 return mState.mGeometryShaderMaxVertices;
1707}
1708
1709const ProgramState &Program::getState() const
1710{
1711 resolveLink();
1712 return mState;
1713}
1714
jchen1015015f72017-03-16 13:54:21 +08001715GLuint Program::getInputResourceIndex(const GLchar *name) const
1716{
jchen107ae70d82018-07-06 13:47:01 +08001717 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001718 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001719}
1720
1721GLuint Program::getOutputResourceIndex(const GLchar *name) const
1722{
jchen107ae70d82018-07-06 13:47:01 +08001723 resolveLink();
jchen1015015f72017-03-16 13:54:21 +08001724 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1725}
1726
jchen10fd7c3b52017-03-21 15:36:03 +08001727size_t Program::getOutputResourceCount() const
1728{
jchen107ae70d82018-07-06 13:47:01 +08001729 resolveLink();
jchen10fd7c3b52017-03-21 15:36:03 +08001730 return (mLinked ? mState.mOutputVariables.size() : 0);
1731}
1732
jchen107ae70d82018-07-06 13:47:01 +08001733const std::vector<GLenum> &Program::getOutputVariableTypes() const
1734{
1735 resolveLink();
1736 return mState.mOutputVariableTypes;
1737}
1738DrawBufferMask Program::getActiveOutputVariables() const
1739{
1740 resolveLink();
1741 return mState.mActiveOutputVariables;
1742}
1743
jchen10baf5d942017-08-28 20:45:48 +08001744template <typename T>
1745void Program::getResourceName(GLuint index,
1746 const std::vector<T> &resources,
1747 GLsizei bufSize,
1748 GLsizei *length,
1749 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001750{
1751 if (length)
1752 {
1753 *length = 0;
1754 }
1755
1756 if (!mLinked)
1757 {
1758 if (bufSize > 0)
1759 {
1760 name[0] = '\0';
1761 }
1762 return;
1763 }
jchen10baf5d942017-08-28 20:45:48 +08001764 ASSERT(index < resources.size());
1765 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001766
1767 if (bufSize > 0)
1768 {
Olli Etuahod2551232017-10-26 20:03:33 +03001769 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001770 }
1771}
1772
jchen10baf5d942017-08-28 20:45:48 +08001773void Program::getInputResourceName(GLuint index,
1774 GLsizei bufSize,
1775 GLsizei *length,
1776 GLchar *name) const
1777{
jchen107ae70d82018-07-06 13:47:01 +08001778 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001779 getResourceName(index, mState.mAttributes, bufSize, length, name);
1780}
1781
1782void Program::getOutputResourceName(GLuint index,
1783 GLsizei bufSize,
1784 GLsizei *length,
1785 GLchar *name) const
1786{
jchen107ae70d82018-07-06 13:47:01 +08001787 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001788 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1789}
1790
1791void Program::getUniformResourceName(GLuint index,
1792 GLsizei bufSize,
1793 GLsizei *length,
1794 GLchar *name) const
1795{
jchen107ae70d82018-07-06 13:47:01 +08001796 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001797 getResourceName(index, mState.mUniforms, bufSize, length, name);
1798}
1799
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001800void Program::getBufferVariableResourceName(GLuint index,
1801 GLsizei bufSize,
1802 GLsizei *length,
1803 GLchar *name) const
1804{
jchen107ae70d82018-07-06 13:47:01 +08001805 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001806 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1807}
1808
jchen10880683b2017-04-12 16:21:55 +08001809const sh::Attribute &Program::getInputResource(GLuint index) const
1810{
jchen107ae70d82018-07-06 13:47:01 +08001811 resolveLink();
jchen10880683b2017-04-12 16:21:55 +08001812 ASSERT(index < mState.mAttributes.size());
1813 return mState.mAttributes[index];
1814}
1815
1816const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1817{
jchen107ae70d82018-07-06 13:47:01 +08001818 resolveLink();
jchen10880683b2017-04-12 16:21:55 +08001819 ASSERT(index < mState.mOutputVariables.size());
1820 return mState.mOutputVariables[index];
1821}
1822
jchen107ae70d82018-07-06 13:47:01 +08001823const ProgramBindings &Program::getAttributeBindings() const
1824{
1825 resolveLink();
1826 return mAttributeBindings;
1827}
1828const ProgramBindings &Program::getUniformLocationBindings() const
1829{
1830 resolveLink();
1831 return mUniformLocationBindings;
1832}
1833const ProgramBindings &Program::getFragmentInputBindings() const
1834{
1835 resolveLink();
1836 return mFragmentInputBindings;
1837}
1838
1839int Program::getNumViews() const
1840{
1841 resolveLink();
1842 return mState.getNumViews();
1843}
1844bool Program::usesMultiview() const
1845{
1846 resolveLink();
1847 return mState.usesMultiview();
1848}
1849
1850ComponentTypeMask Program::getDrawBufferTypeMask() const
1851{
1852 resolveLink();
1853 return mState.mDrawBufferTypeMask;
1854}
1855ComponentTypeMask Program::getAttributesTypeMask() const
1856{
1857 resolveLink();
1858 return mState.mAttributesTypeMask;
1859}
1860AttributesMask Program::getAttributesMask() const
1861{
1862 resolveLink();
1863 return mState.mAttributesMask;
1864}
1865
1866const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1867{
1868 resolveLink();
1869 return mState.mTransformFeedbackStrides;
1870}
1871
Geoff Lang7dd2e102014-11-10 15:19:26 -05001872GLint Program::getFragDataLocation(const std::string &name) const
1873{
jchen107ae70d82018-07-06 13:47:01 +08001874 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001875 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001876}
1877
Geoff Lange1a27752015-10-05 13:16:04 -04001878void Program::getActiveUniform(GLuint index,
1879 GLsizei bufsize,
1880 GLsizei *length,
1881 GLint *size,
1882 GLenum *type,
1883 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001884{
jchen107ae70d82018-07-06 13:47:01 +08001885 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001886 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001887 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001888 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001889 ASSERT(index < mState.mUniforms.size());
1890 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891
1892 if (bufsize > 0)
1893 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001894 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001895 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896 }
1897
Olli Etuaho465835d2017-09-26 13:34:10 +03001898 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001900 }
1901 else
1902 {
1903 if (bufsize > 0)
1904 {
1905 name[0] = '\0';
1906 }
1907
1908 if (length)
1909 {
1910 *length = 0;
1911 }
1912
1913 *size = 0;
1914 *type = GL_NONE;
1915 }
1916}
1917
Geoff Lange1a27752015-10-05 13:16:04 -04001918GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001919{
jchen107ae70d82018-07-06 13:47:01 +08001920 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001921 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001922 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001923 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001924 }
1925 else
1926 {
1927 return 0;
1928 }
1929}
1930
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001931size_t Program::getActiveBufferVariableCount() const
1932{
jchen107ae70d82018-07-06 13:47:01 +08001933 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001934 return mLinked ? mState.mBufferVariables.size() : 0;
1935}
1936
Geoff Lange1a27752015-10-05 13:16:04 -04001937GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001938{
jchen107ae70d82018-07-06 13:47:01 +08001939 resolveLink();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001941
1942 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001943 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001944 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001946 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001948 size_t length = uniform.name.length() + 1u;
1949 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001950 {
1951 length += 3; // Counting in "[0]".
1952 }
1953 maxLength = std::max(length, maxLength);
1954 }
1955 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001956 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957
Jamie Madill62d31cb2015-09-11 13:25:51 -04001958 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959}
1960
Geoff Lang7dd2e102014-11-10 15:19:26 -05001961bool Program::isValidUniformLocation(GLint location) const
1962{
jchen107ae70d82018-07-06 13:47:01 +08001963 resolveLink();
Jamie Madille2e406c2016-06-02 13:04:10 -04001964 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001965 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001966 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001967}
1968
Jamie Madill62d31cb2015-09-11 13:25:51 -04001969const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970{
jchen107ae70d82018-07-06 13:47:01 +08001971 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001972 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001973 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001974}
1975
Jamie Madillac4e9c32017-01-13 14:07:12 -05001976const VariableLocation &Program::getUniformLocation(GLint location) const
1977{
jchen107ae70d82018-07-06 13:47:01 +08001978 resolveLink();
Jamie Madillac4e9c32017-01-13 14:07:12 -05001979 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1980 return mState.mUniformLocations[location];
1981}
1982
jchen107ae70d82018-07-06 13:47:01 +08001983const std::vector<VariableLocation> &Program::getUniformLocations() const
1984{
1985 resolveLink();
1986 return mState.mUniformLocations;
1987}
1988const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1989{
1990 resolveLink();
1991 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1992 return mState.mUniforms[index];
1993}
1994
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001995const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1996{
jchen107ae70d82018-07-06 13:47:01 +08001997 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001998 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1999 return mState.mBufferVariables[index];
2000}
2001
Jamie Madill62d31cb2015-09-11 13:25:51 -04002002GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003{
jchen107ae70d82018-07-06 13:47:01 +08002004 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03002005 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006}
2007
Jamie Madill62d31cb2015-09-11 13:25:51 -04002008GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009{
jchen107ae70d82018-07-06 13:47:01 +08002010 resolveLink();
Jamie Madille7d84322017-01-10 18:21:59 -05002011 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012}
2013
2014void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2015{
jchen107ae70d82018-07-06 13:47:01 +08002016 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002017 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2018 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002019 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020}
2021
2022void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2023{
jchen107ae70d82018-07-06 13:47:01 +08002024 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002025 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2026 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002027 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028}
2029
2030void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2031{
jchen107ae70d82018-07-06 13:47:01 +08002032 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002033 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2034 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002035 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036}
2037
2038void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2039{
jchen107ae70d82018-07-06 13:47:01 +08002040 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002041 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2042 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002043 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044}
2045
Jamie Madill81c2e252017-09-09 23:32:46 -04002046Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047{
jchen107ae70d82018-07-06 13:47:01 +08002048 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002049 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2050 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2051
Jamie Madill81c2e252017-09-09 23:32:46 -04002052 mProgram->setUniform1iv(location, clampedCount, v);
2053
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002054 if (mState.isSamplerUniformIndex(locationInfo.index))
2055 {
2056 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002057 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002058 }
2059
Jamie Madill81c2e252017-09-09 23:32:46 -04002060 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061}
2062
2063void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2064{
jchen107ae70d82018-07-06 13:47:01 +08002065 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002066 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2067 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002068 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069}
2070
2071void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2072{
jchen107ae70d82018-07-06 13:47:01 +08002073 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002074 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2075 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002076 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077}
2078
2079void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2080{
jchen107ae70d82018-07-06 13:47:01 +08002081 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002082 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2083 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002084 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085}
2086
2087void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2088{
jchen107ae70d82018-07-06 13:47:01 +08002089 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002090 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2091 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002092 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093}
2094
2095void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2096{
jchen107ae70d82018-07-06 13:47:01 +08002097 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002098 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2099 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002100 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101}
2102
2103void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2104{
jchen107ae70d82018-07-06 13:47:01 +08002105 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002106 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2107 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002108 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109}
2110
2111void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2112{
jchen107ae70d82018-07-06 13:47:01 +08002113 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002114 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2115 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002116 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117}
2118
Jamie Madill493f9572018-05-24 19:52:15 -04002119void Program::setUniformMatrix2fv(GLint location,
2120 GLsizei count,
2121 GLboolean transpose,
2122 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123{
jchen107ae70d82018-07-06 13:47:01 +08002124 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002125 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002126 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127}
2128
Jamie Madill493f9572018-05-24 19:52:15 -04002129void Program::setUniformMatrix3fv(GLint location,
2130 GLsizei count,
2131 GLboolean transpose,
2132 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133{
jchen107ae70d82018-07-06 13:47:01 +08002134 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002135 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002136 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137}
2138
Jamie Madill493f9572018-05-24 19:52:15 -04002139void Program::setUniformMatrix4fv(GLint location,
2140 GLsizei count,
2141 GLboolean transpose,
2142 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143{
jchen107ae70d82018-07-06 13:47:01 +08002144 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002145 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002146 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147}
2148
Jamie Madill493f9572018-05-24 19:52:15 -04002149void Program::setUniformMatrix2x3fv(GLint location,
2150 GLsizei count,
2151 GLboolean transpose,
2152 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153{
jchen107ae70d82018-07-06 13:47:01 +08002154 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002155 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002156 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157}
2158
Jamie Madill493f9572018-05-24 19:52:15 -04002159void Program::setUniformMatrix2x4fv(GLint location,
2160 GLsizei count,
2161 GLboolean transpose,
2162 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163{
jchen107ae70d82018-07-06 13:47:01 +08002164 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002165 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002166 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167}
2168
Jamie Madill493f9572018-05-24 19:52:15 -04002169void Program::setUniformMatrix3x2fv(GLint location,
2170 GLsizei count,
2171 GLboolean transpose,
2172 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173{
jchen107ae70d82018-07-06 13:47:01 +08002174 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002175 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002176 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177}
2178
Jamie Madill493f9572018-05-24 19:52:15 -04002179void Program::setUniformMatrix3x4fv(GLint location,
2180 GLsizei count,
2181 GLboolean transpose,
2182 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183{
jchen107ae70d82018-07-06 13:47:01 +08002184 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002185 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002186 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187}
2188
Jamie Madill493f9572018-05-24 19:52:15 -04002189void Program::setUniformMatrix4x2fv(GLint location,
2190 GLsizei count,
2191 GLboolean transpose,
2192 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193{
jchen107ae70d82018-07-06 13:47:01 +08002194 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002195 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002196 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197}
2198
Jamie Madill493f9572018-05-24 19:52:15 -04002199void Program::setUniformMatrix4x3fv(GLint location,
2200 GLsizei count,
2201 GLboolean transpose,
2202 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203{
jchen107ae70d82018-07-06 13:47:01 +08002204 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002205 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002206 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207}
2208
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002209GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2210{
jchen107ae70d82018-07-06 13:47:01 +08002211 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002212 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2213 const std::vector<GLuint> &boundTextureUnits =
2214 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2215 return boundTextureUnits[uniformLocation.arrayIndex];
2216}
2217
Jamie Madill54164b02017-08-28 15:17:37 -04002218void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002219{
jchen107ae70d82018-07-06 13:47:01 +08002220 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002221 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2222 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002223
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002224 if (uniform.isSampler())
2225 {
2226 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2227 return;
2228 }
2229
2230 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002231 if (nativeType == GL_FLOAT)
2232 {
2233 mProgram->getUniformfv(context, location, v);
2234 }
2235 else
2236 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002237 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002238 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002239}
2240
Jamie Madill54164b02017-08-28 15:17:37 -04002241void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002242{
jchen107ae70d82018-07-06 13:47:01 +08002243 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002244 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2245 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002246
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002247 if (uniform.isSampler())
2248 {
2249 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2250 return;
2251 }
2252
2253 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002254 if (nativeType == GL_INT || nativeType == GL_BOOL)
2255 {
2256 mProgram->getUniformiv(context, location, v);
2257 }
2258 else
2259 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002260 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002261 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002262}
2263
Jamie Madill54164b02017-08-28 15:17:37 -04002264void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265{
jchen107ae70d82018-07-06 13:47:01 +08002266 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002267 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2268 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002269
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002270 if (uniform.isSampler())
2271 {
2272 *v = getSamplerUniformBinding(uniformLocation);
2273 return;
2274 }
2275
2276 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002277 if (nativeType == GL_UNSIGNED_INT)
2278 {
2279 mProgram->getUniformuiv(context, location, v);
2280 }
2281 else
2282 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002283 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002284 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002285}
2286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287void Program::flagForDeletion()
2288{
jchen107ae70d82018-07-06 13:47:01 +08002289 resolveLink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 mDeleteStatus = true;
2291}
2292
2293bool Program::isFlaggedForDeletion() const
2294{
jchen107ae70d82018-07-06 13:47:01 +08002295 resolveLink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 return mDeleteStatus;
2297}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002298
Brandon Jones43a53e22014-08-28 16:23:22 -07002299void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002300{
jchen107ae70d82018-07-06 13:47:01 +08002301 resolveLink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002302 mInfoLog.reset();
2303
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002305 {
Geoff Lang92019432017-11-20 13:09:34 -05002306 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002307 }
2308 else
2309 {
Jamie Madillf6113162015-05-07 11:49:21 -04002310 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002311 }
2312}
2313
Jamie Madillcc73f242018-08-01 11:34:48 -04002314bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315{
jchen107ae70d82018-07-06 13:47:01 +08002316 resolveLink();
2317
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002318 // if any two active samplers in a program are of different types, but refer to the same
2319 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2320 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002321 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002322 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002323 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002324 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002325 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002326 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002327 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2328 "image unit ("
2329 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002330 }
2331
Jamie Madill7e4eff12018-08-08 15:49:26 -04002332 mCachedValidateSamplersResult = false;
2333 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002334 }
2335 }
2336
2337 mCachedValidateSamplersResult = true;
2338 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002339}
2340
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002341bool Program::isValidated() const
2342{
jchen107ae70d82018-07-06 13:47:01 +08002343 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002344 return mValidated;
2345}
2346
jchen1058f67be2017-10-27 08:59:27 +08002347GLuint Program::getActiveAtomicCounterBufferCount() const
2348{
jchen107ae70d82018-07-06 13:47:01 +08002349 resolveLink();
jchen1058f67be2017-10-27 08:59:27 +08002350 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2351}
2352
Jiajia Qin729b2c62017-08-14 09:36:11 +08002353GLuint Program::getActiveShaderStorageBlockCount() const
2354{
jchen107ae70d82018-07-06 13:47:01 +08002355 resolveLink();
Jiajia Qin729b2c62017-08-14 09:36:11 +08002356 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2357}
2358
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002359void Program::getActiveUniformBlockName(const GLuint blockIndex,
2360 GLsizei bufSize,
2361 GLsizei *length,
2362 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363{
jchen107ae70d82018-07-06 13:47:01 +08002364 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002365 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2366}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002367
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002368void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2369 GLsizei bufSize,
2370 GLsizei *length,
2371 GLchar *blockName) const
2372{
jchen107ae70d82018-07-06 13:47:01 +08002373 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002374 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002375}
2376
Qin Jiajia9bf55522018-01-29 13:56:23 +08002377template <typename T>
2378GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002379{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002380 int maxLength = 0;
2381
2382 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002383 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002384 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002385 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002386 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002388 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002389 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390 }
2391 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002392 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393
2394 return maxLength;
2395}
2396
Qin Jiajia9bf55522018-01-29 13:56:23 +08002397GLint Program::getActiveUniformBlockMaxNameLength() const
2398{
jchen107ae70d82018-07-06 13:47:01 +08002399 resolveLink();
Qin Jiajia9bf55522018-01-29 13:56:23 +08002400 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2401}
2402
2403GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2404{
jchen107ae70d82018-07-06 13:47:01 +08002405 resolveLink();
Qin Jiajia9bf55522018-01-29 13:56:23 +08002406 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2407}
2408
Geoff Lange1a27752015-10-05 13:16:04 -04002409GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002410{
jchen107ae70d82018-07-06 13:47:01 +08002411 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002412 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2413}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002414
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002415GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2416{
jchen107ae70d82018-07-06 13:47:01 +08002417 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002418 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002419}
2420
Jiajia Qin729b2c62017-08-14 09:36:11 +08002421const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002422{
jchen107ae70d82018-07-06 13:47:01 +08002423 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002424 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2425 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002426}
2427
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002428const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2429{
jchen107ae70d82018-07-06 13:47:01 +08002430 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002431 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2432 return mState.mShaderStorageBlocks[index];
2433}
2434
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002435void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2436{
jchen107ae70d82018-07-06 13:47:01 +08002437 resolveLink();
jchen107a20b972017-06-13 14:25:26 +08002438 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002439 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04002440 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002441}
2442
2443GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2444{
jchen107ae70d82018-07-06 13:47:01 +08002445 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002446 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002447}
2448
Jiajia Qin729b2c62017-08-14 09:36:11 +08002449GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2450{
jchen107ae70d82018-07-06 13:47:01 +08002451 resolveLink();
Jiajia Qin729b2c62017-08-14 09:36:11 +08002452 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2453}
2454
Jamie Madill493f9572018-05-24 19:52:15 -04002455void Program::setTransformFeedbackVaryings(GLsizei count,
2456 const GLchar *const *varyings,
2457 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002458{
jchen107ae70d82018-07-06 13:47:01 +08002459 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002460 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002461 for (GLsizei i = 0; i < count; i++)
2462 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002463 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002464 }
2465
Jamie Madill48ef11b2016-04-27 15:21:52 -04002466 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002467}
2468
Jamie Madill493f9572018-05-24 19:52:15 -04002469void Program::getTransformFeedbackVarying(GLuint index,
2470 GLsizei bufSize,
2471 GLsizei *length,
2472 GLsizei *size,
2473 GLenum *type,
2474 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002475{
jchen107ae70d82018-07-06 13:47:01 +08002476 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002478 {
jchen10a9042d32017-03-17 08:50:45 +08002479 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2480 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2481 std::string varName = var.nameWithArrayIndex();
2482 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002483 if (length)
2484 {
2485 *length = lastNameIdx;
2486 }
2487 if (size)
2488 {
jchen10a9042d32017-03-17 08:50:45 +08002489 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002490 }
2491 if (type)
2492 {
jchen10a9042d32017-03-17 08:50:45 +08002493 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002494 }
2495 if (name)
2496 {
jchen10a9042d32017-03-17 08:50:45 +08002497 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002498 name[lastNameIdx] = '\0';
2499 }
2500 }
2501}
2502
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002503GLsizei Program::getTransformFeedbackVaryingCount() const
2504{
jchen107ae70d82018-07-06 13:47:01 +08002505 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002506 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002507 {
jchen10a9042d32017-03-17 08:50:45 +08002508 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002509 }
2510 else
2511 {
2512 return 0;
2513 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002514}
2515
2516GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2517{
jchen107ae70d82018-07-06 13:47:01 +08002518 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002519 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002520 {
2521 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002522 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002523 {
jchen10a9042d32017-03-17 08:50:45 +08002524 maxSize =
2525 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002526 }
2527
2528 return maxSize;
2529 }
2530 else
2531 {
2532 return 0;
2533 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002534}
2535
2536GLenum Program::getTransformFeedbackBufferMode() const
2537{
jchen107ae70d82018-07-06 13:47:01 +08002538 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002539 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002540}
2541
jchen103fd614d2018-08-13 12:21:58 +08002542bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002543{
Jiawei Shao016105b2018-04-12 16:38:31 +08002544 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2545 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2546 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2547 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002548
Jamie Madill493f9572018-05-24 19:52:15 -04002549 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002550 bool isGraphicsShaderAttached =
2551 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002552 // Check whether we both have a compute and non-compute shaders attached.
2553 // If there are of both types attached, then linking should fail.
2554 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2555 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2556 {
2557 infoLog << "Both compute and graphics shaders are attached to the same program.";
2558 return false;
2559 }
2560
2561 if (computeShader)
2562 {
jchen103fd614d2018-08-13 12:21:58 +08002563 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002564 {
2565 infoLog << "Attached compute shader is not compiled.";
2566 return false;
2567 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002568 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002569
jchen103fd614d2018-08-13 12:21:58 +08002570 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002571
2572 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2573 // If the work group size is not specified, a link time error should occur.
2574 if (!mState.mComputeShaderLocalSize.isDeclared())
2575 {
2576 infoLog << "Work group size is not specified.";
2577 return false;
2578 }
2579 }
2580 else
2581 {
jchen103fd614d2018-08-13 12:21:58 +08002582 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002583 {
2584 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2585 return false;
2586 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002587 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002588
jchen103fd614d2018-08-13 12:21:58 +08002589 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002590 {
2591 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2592 return false;
2593 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002594 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002595
jchen103fd614d2018-08-13 12:21:58 +08002596 int vertexShaderVersion = vertexShader->getShaderVersion();
2597 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002598 {
2599 infoLog << "Fragment shader version does not match vertex shader version.";
2600 return false;
2601 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002602
2603 if (geometryShader)
2604 {
2605 // [GL_EXT_geometry_shader] Chapter 7
2606 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2607 // Language Specification, as well as any of the following reasons:
2608 // * One or more of the shader objects attached to <program> are not compiled
2609 // successfully.
2610 // * The shaders do not use the same shader language version.
2611 // * <program> contains objects to form a geometry shader, and
2612 // - <program> is not separable and contains no objects to form a vertex shader; or
2613 // - the input primitive type, output primitive type, or maximum output vertex count
2614 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002615 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002616 {
2617 infoLog << "The attached geometry shader isn't compiled.";
2618 return false;
2619 }
2620
jchen103fd614d2018-08-13 12:21:58 +08002621 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002622 {
2623 mInfoLog << "Geometry shader version does not match vertex shader version.";
2624 return false;
2625 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002626 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002627
Jamie Madill493f9572018-05-24 19:52:15 -04002628 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002629 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002630 if (!inputPrimitive.valid())
2631 {
2632 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2633 return false;
2634 }
2635
Jamie Madill493f9572018-05-24 19:52:15 -04002636 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002637 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002638 if (!outputPrimitive.valid())
2639 {
2640 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2641 return false;
2642 }
2643
jchen103fd614d2018-08-13 12:21:58 +08002644 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002645 if (!maxVertices.valid())
2646 {
2647 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2648 return false;
2649 }
2650
2651 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2652 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2653 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002654 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002655 }
Jiawei Shao73618602017-12-20 15:47:15 +08002656 }
2657
2658 return true;
2659}
2660
jchen10910a3da2017-11-15 09:40:11 +08002661GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2662{
jchen107ae70d82018-07-06 13:47:01 +08002663 resolveLink();
jchen10910a3da2017-11-15 09:40:11 +08002664 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2665 {
2666 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2667 if (tf.nameWithArrayIndex() == name)
2668 {
2669 return tfIndex;
2670 }
2671 }
2672 return GL_INVALID_INDEX;
2673}
2674
2675const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2676{
jchen107ae70d82018-07-06 13:47:01 +08002677 resolveLink();
jchen10910a3da2017-11-15 09:40:11 +08002678 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2679 return mState.mLinkedTransformFeedbackVaryings[index];
2680}
2681
jchen103fd614d2018-08-13 12:21:58 +08002682bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002683{
Jiawei Shao016105b2018-04-12 16:38:31 +08002684 Shader *previousShader = nullptr;
2685 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002686 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002687 Shader *currentShader = mState.mAttachedShaders[shaderType];
2688 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002689 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002690 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002691 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002692
2693 if (previousShader)
2694 {
jchen103fd614d2018-08-13 12:21:58 +08002695 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002696 {
2697 return false;
2698 }
2699 }
2700 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002701 }
2702
jchen103fd614d2018-08-13 12:21:58 +08002703 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002704 {
2705 return false;
2706 }
2707
jchen103fd614d2018-08-13 12:21:58 +08002708 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002709 {
2710 return false;
2711 }
2712
2713 return true;
2714}
2715
2716// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2717// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002718bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002719 gl::Shader *consumingShader,
2720 gl::InfoLog &infoLog) const
2721{
jchen103fd614d2018-08-13 12:21:58 +08002722 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002723
jchen103fd614d2018-08-13 12:21:58 +08002724 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2725 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726
Jiawei Shao385b3e02018-03-21 09:43:28 +08002727 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002728
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002729 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002730 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002731 bool matched = false;
2732
2733 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002734 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002735 {
2736 continue;
2737 }
2738
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002739 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002740 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002741 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002742 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002743 ASSERT(!output.isBuiltIn());
2744
2745 std::string mismatchedStructFieldName;
2746 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002747 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002748 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002749 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002750 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002751 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2752 mismatchedStructFieldName, generatingShader->getType(),
2753 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002754 return false;
2755 }
2756
Geoff Lang7dd2e102014-11-10 15:19:26 -05002757 matched = true;
2758 break;
2759 }
2760 }
2761
Olli Etuaho107c7242018-03-20 15:45:35 +02002762 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2763 // whether the input is statically used - a statically used input should fail this test even
2764 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002765 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002766 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002767 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2768 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2769 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002770 return false;
2771 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002772 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002773
Jiawei Shaod063aff2018-02-22 10:19:09 +08002774 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002775
Jiawei Shaod063aff2018-02-22 10:19:09 +08002776 return true;
2777}
2778
jchen103fd614d2018-08-13 12:21:58 +08002779bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002780{
Jiawei Shao016105b2018-04-12 16:38:31 +08002781 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002782
2783 std::map<GLuint, std::string> staticFragmentInputLocations;
2784
2785 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002786 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002787 for (const sh::Varying &input : fragmentInputVaryings)
2788 {
2789 if (input.isBuiltIn() || !input.staticUse)
2790 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002791 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002792 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002793
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002794 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002795 if (inputBinding == -1)
2796 continue;
2797
2798 const auto it = staticFragmentInputLocations.find(inputBinding);
2799 if (it == std::end(staticFragmentInputLocations))
2800 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002801 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002802 }
2803 else
2804 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002805 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002806 << it->second;
2807 return false;
2808 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002809 }
2810
2811 return true;
2812}
2813
jchen103fd614d2018-08-13 12:21:58 +08002814bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002815 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002816 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002817 GLuint *combinedImageUniformsCount,
2818 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002819{
Olli Etuahob78707c2017-03-09 15:03:11 +00002820 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002821 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822 {
2823 return false;
2824 }
2825
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002826 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002827
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002828 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002829
jchen10eaef1e52017-06-13 10:44:11 +08002830 if (!linkAtomicCounterBuffers())
2831 {
2832 return false;
2833 }
2834
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002835 return true;
2836}
2837
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002838void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002839{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002840 ASSERT(combinedImageUniforms);
2841
Jamie Madill982f6e02017-06-07 14:33:04 -04002842 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2843 unsigned int low = high;
2844
jchen10eaef1e52017-06-13 10:44:11 +08002845 for (auto counterIter = mState.mUniforms.rbegin();
2846 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2847 {
2848 --low;
2849 }
2850
2851 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2852
2853 high = low;
2854
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002855 for (auto imageIter = mState.mUniforms.rbegin();
2856 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2857 {
2858 --low;
2859 }
2860
2861 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002862 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002863 // If uniform is a image type, insert it into the mImageBindings array.
2864 for (unsigned int imageIndex : mState.mImageUniformRange)
2865 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002866 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2867 // cannot load values into a uniform defined as an image. if declare without a
2868 // binding qualifier, any uniform image variable (include all elements of
2869 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002870 auto &imageUniform = mState.mUniforms[imageIndex];
2871 if (imageUniform.binding == -1)
2872 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002873 mState.mImageBindings.emplace_back(
2874 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002875 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002876 else
2877 {
2878 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002879 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002880 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002881
2882 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2883 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002884 }
2885
2886 high = low;
2887
2888 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002889 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002890 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002891 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002892 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002893
2894 mState.mSamplerUniformRange = RangeUI(low, high);
2895
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002896 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002897 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002898 {
2899 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002900 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002901 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2902 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002903 }
2904}
2905
jchen10eaef1e52017-06-13 10:44:11 +08002906bool Program::linkAtomicCounterBuffers()
2907{
2908 for (unsigned int index : mState.mAtomicCounterUniformRange)
2909 {
Jamie Madill493f9572018-05-24 19:52:15 -04002910 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002911 uniform.blockInfo.offset = uniform.offset;
2912 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2913 uniform.blockInfo.matrixStride = 0;
2914 uniform.blockInfo.isRowMajorMatrix = false;
2915
Jamie Madill493f9572018-05-24 19:52:15 -04002916 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002917 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2918 ++bufferIndex)
2919 {
2920 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2921 if (buffer.binding == uniform.binding)
2922 {
2923 buffer.memberIndexes.push_back(index);
2924 uniform.bufferIndex = bufferIndex;
2925 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002926 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002927 break;
2928 }
2929 }
2930 if (!found)
2931 {
2932 AtomicCounterBuffer atomicCounterBuffer;
2933 atomicCounterBuffer.binding = uniform.binding;
2934 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002935 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002936 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2937 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2938 }
2939 }
2940 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002941 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002942
2943 return true;
2944}
2945
Jamie Madilleb979bf2016-11-15 12:28:46 -05002946// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002947bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002948{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002949 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002950
jchen103fd614d2018-08-13 12:21:58 +08002951 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002952
Geoff Lang7dd2e102014-11-10 15:19:26 -05002953 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002954 if (shaderVersion >= 300)
2955 {
2956 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2957 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002958 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002959 }
2960 else
2961 {
2962 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002963 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002964 }
jchen103fd614d2018-08-13 12:21:58 +08002965 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002966
2967 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002968 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002969 {
Jamie Madillf6113162015-05-07 11:49:21 -04002970 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002971 return false;
2972 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002973
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002974 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002975
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002976 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002977 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002978 {
Olli Etuahod2551232017-10-26 20:03:33 +03002979 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2980 // structures, so we don't need to worry about adjusting their names or generating entries
2981 // for each member/element (unlike uniforms for example).
2982 ASSERT(!attribute.isArray() && !attribute.isStruct());
2983
Jamie Madilleb979bf2016-11-15 12:28:46 -05002984 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002985 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002986 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002987 attribute.location = bindingLocation;
2988 }
2989
2990 if (attribute.location != -1)
2991 {
2992 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002993 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002994
Jamie Madill63805b42015-08-25 13:17:39 -04002995 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002996 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002997 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
2998 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002999
3000 return false;
3001 }
3002
Jamie Madill63805b42015-08-25 13:17:39 -04003003 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003004 {
Jamie Madill63805b42015-08-25 13:17:39 -04003005 const int regLocation = attribute.location + reg;
3006 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003007
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003008 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3009 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3010 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003011 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003012 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003013 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003014 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003015 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003016 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003017 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003018 return false;
3019 }
3020 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003021 else
3022 {
Jamie Madill63805b42015-08-25 13:17:39 -04003023 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003024 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003025
Jamie Madill63805b42015-08-25 13:17:39 -04003026 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003027 }
3028 }
3029 }
3030
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003031 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003032 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003033 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003034 // Not set by glBindAttribLocation or by location layout qualifier
3035 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003036 {
Jamie Madill63805b42015-08-25 13:17:39 -04003037 int regs = VariableRegisterCount(attribute.type);
3038 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003039
Jamie Madill63805b42015-08-25 13:17:39 -04003040 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003041 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003042 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003043 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003044 }
3045
Jamie Madillc349ec02015-08-21 16:53:12 -04003046 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003047 }
3048 }
3049
Brandon Jonesc405ae72017-12-06 14:15:03 -08003050 ASSERT(mState.mAttributesTypeMask.none());
3051 ASSERT(mState.mAttributesMask.none());
3052
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003053 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3054 // shader versions we're only processing active attributes to begin with.
3055 if (shaderVersion >= 300)
3056 {
3057 for (auto attributeIter = mState.mAttributes.begin();
3058 attributeIter != mState.mAttributes.end();)
3059 {
3060 if (attributeIter->active)
3061 {
3062 ++attributeIter;
3063 }
3064 else
3065 {
3066 attributeIter = mState.mAttributes.erase(attributeIter);
3067 }
3068 }
3069 }
3070
Jamie Madill48ef11b2016-04-27 15:21:52 -04003071 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003072 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003073 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003074 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003075 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003076
Jamie Madillbd159f02017-10-09 19:39:06 -04003077 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003078 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003079 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3080 mState.mActiveAttribLocationsMask.set(location);
3081 mState.mMaxActiveAttribLocation =
3082 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003083
3084 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3085 if (!attribute.isBuiltIn())
3086 {
3087 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3088 location);
3089 mState.mAttributesMask.set(location);
3090 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003091 }
3092 }
3093
Geoff Lang7dd2e102014-11-10 15:19:26 -05003094 return true;
3095}
3096
jchen103fd614d2018-08-13 12:21:58 +08003097bool Program::linkInterfaceBlocks(const Caps &caps,
3098 const Version &version,
3099 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003100 InfoLog &infoLog,
3101 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003102{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003103 ASSERT(combinedShaderStorageBlocksCount);
3104
Jiawei Shao54aafe52018-04-27 14:54:57 +08003105 GLuint combinedUniformBlocksCount = 0u;
3106 GLuint numShadersHasUniformBlocks = 0u;
3107 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3108 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003109 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003110 Shader *shader = mState.mAttachedShaders[shaderType];
3111 if (!shader)
3112 {
3113 continue;
3114 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003115
jchen103fd614d2018-08-13 12:21:58 +08003116 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003117 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003118 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003119 if (!ValidateInterfaceBlocksCount(
3120 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3121 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3122 {
3123 return false;
3124 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003125
Jiawei Shao54aafe52018-04-27 14:54:57 +08003126 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3127 ++numShadersHasUniformBlocks;
3128 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003129 }
3130
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003131 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3132 {
3133 infoLog << "The sum of the number of active uniform blocks exceeds "
3134 "MAX_COMBINED_UNIFORM_BLOCKS ("
3135 << caps.maxCombinedUniformBlocks << ").";
3136 return false;
3137 }
3138
Jiawei Shao54aafe52018-04-27 14:54:57 +08003139 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3140 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003141 {
3142 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003143 }
Jamie Madille473dee2015-08-18 14:49:01 -04003144
jchen103fd614d2018-08-13 12:21:58 +08003145 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003146 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003147 *combinedShaderStorageBlocksCount = 0u;
3148 GLuint numShadersHasShaderStorageBlocks = 0u;
3149 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3150 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003151 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003152 Shader *shader = mState.mAttachedShaders[shaderType];
3153 if (!shader)
3154 {
3155 continue;
3156 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003157
jchen103fd614d2018-08-13 12:21:58 +08003158 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003159 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003160 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003161 if (!ValidateInterfaceBlocksCount(
3162 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3163 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3164 {
3165 return false;
3166 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003167
Jiawei Shao54aafe52018-04-27 14:54:57 +08003168 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3169 ++numShadersHasShaderStorageBlocks;
3170 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003171 }
3172
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003173 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003174 {
3175 infoLog << "The sum of the number of active shader storage blocks exceeds "
3176 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3177 << caps.maxCombinedShaderStorageBlocks << ").";
3178 return false;
3179 }
3180
Jiawei Shao54aafe52018-04-27 14:54:57 +08003181 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3182 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003183 {
3184 return false;
3185 }
3186 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003187
Geoff Lang7dd2e102014-11-10 15:19:26 -05003188 return true;
3189}
3190
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003191LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3192 const sh::ShaderVariable &variable2,
3193 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003194 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003195 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003196{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003197 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003198 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003199 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003200 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003201 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003202 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003203 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003204 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003205 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003206 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003207 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003208 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003209 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003210 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003211 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003212 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003213
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003214 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003215 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003216 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003217 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003218 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003219 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3220 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003221 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3222 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003223
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003224 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003225 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003226 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003227 }
3228
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003229 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003230 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003231 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003232 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003233 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3234 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003235 }
3236 }
3237
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003238 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003239}
3240
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003241LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3242 const sh::Varying &inputVarying,
3243 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003244 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003245 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003246{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003247 if (validateGeometryShaderInputVarying)
3248 {
3249 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3250 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3251 // or outputs.
3252 ASSERT(inputVarying.arraySizes.size() == 1u);
3253
3254 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3255 // varying cannot match a geometry shader input varying.
3256 // [GL_EXT_geometry_shader] Section 7.4.1:
3257 // Geometry shader per-vertex input variables and blocks are required to be declared as
3258 // arrays, with each element representing input or output values for a single vertex of a
3259 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3260 // are treated as though they were not declared as arrays.
3261 if (outputVarying.isArray())
3262 {
3263 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3264 }
3265 }
3266
3267 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3268 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003269 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003270 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3271 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003272 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003273 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003274 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003275 }
3276
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003277 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003278 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003279 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003280 }
3281
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003282 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003283 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003284 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003285 }
3286
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003287 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003288}
3289
jchen103fd614d2018-08-13 12:21:58 +08003290bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003291{
Jiawei Shao016105b2018-04-12 16:38:31 +08003292 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3293 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003294 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3295 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3296 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003297
3298 if (shaderVersion != 100)
3299 {
3300 // Only ESSL 1.0 has restrictions on matching input and output invariance
3301 return true;
3302 }
3303
3304 bool glPositionIsInvariant = false;
3305 bool glPointSizeIsInvariant = false;
3306 bool glFragCoordIsInvariant = false;
3307 bool glPointCoordIsInvariant = false;
3308
3309 for (const sh::Varying &varying : vertexVaryings)
3310 {
3311 if (!varying.isBuiltIn())
3312 {
3313 continue;
3314 }
3315 if (varying.name.compare("gl_Position") == 0)
3316 {
3317 glPositionIsInvariant = varying.isInvariant;
3318 }
3319 else if (varying.name.compare("gl_PointSize") == 0)
3320 {
3321 glPointSizeIsInvariant = varying.isInvariant;
3322 }
3323 }
3324
3325 for (const sh::Varying &varying : fragmentVaryings)
3326 {
3327 if (!varying.isBuiltIn())
3328 {
3329 continue;
3330 }
3331 if (varying.name.compare("gl_FragCoord") == 0)
3332 {
3333 glFragCoordIsInvariant = varying.isInvariant;
3334 }
3335 else if (varying.name.compare("gl_PointCoord") == 0)
3336 {
3337 glPointCoordIsInvariant = varying.isInvariant;
3338 }
3339 }
3340
3341 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3342 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3343 // Not requiring invariance to match is supported by:
3344 // dEQP, WebGL CTS, Nexus 5X GLES
3345 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3346 {
3347 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3348 "declared invariant.";
3349 return false;
3350 }
3351 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3352 {
3353 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3354 "declared invariant.";
3355 return false;
3356 }
3357
3358 return true;
3359}
3360
jchen103fd614d2018-08-13 12:21:58 +08003361bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003362 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003363 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003364 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003365{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003366
jchen108225e732017-11-14 16:29:03 +08003367 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003368 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003369 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003370 {
jchen103fd614d2018-08-13 12:21:58 +08003371 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003372 {
Geoff Lang1a683462015-09-29 15:09:59 -04003373 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003374 return false;
3375 }
jchen103fd614d2018-08-13 12:21:58 +08003376 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003377 {
3378 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3379 {
3380 infoLog << "Two transform feedback varyings include the same array element ("
3381 << tfVaryingName << ").";
3382 return false;
3383 }
3384 }
3385 else
3386 {
3387 if (uniqueNames.count(tfVaryingName) > 0)
3388 {
3389 infoLog << "Two transform feedback varyings specify the same output variable ("
3390 << tfVaryingName << ").";
3391 return false;
3392 }
3393 }
3394 uniqueNames.insert(tfVaryingName);
3395 }
3396
3397 // Validate against program varyings.
3398 size_t totalComponents = 0;
3399 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3400 {
3401 std::vector<unsigned int> subscripts;
3402 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3403
3404 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3405 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003406 {
3407 infoLog << "Transform feedback varying " << tfVaryingName
3408 << " does not exist in the vertex shader.";
3409 return false;
3410 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003411
jchen108225e732017-11-14 16:29:03 +08003412 // Validate the matching variable.
3413 if (var->isStruct())
3414 {
3415 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3416 return false;
3417 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003418
jchen108225e732017-11-14 16:29:03 +08003419 size_t elementCount = 0;
3420 size_t componentCount = 0;
3421
3422 if (var->isArray())
3423 {
jchen103fd614d2018-08-13 12:21:58 +08003424 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003425 {
3426 infoLog << "Capture of arrays is undefined and not supported.";
3427 return false;
3428 }
3429
3430 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3431 ASSERT(!var->isArrayOfArrays());
3432
3433 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3434 {
3435 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3436 return false;
3437 }
3438 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3439 }
3440 else
3441 {
3442 if (!subscripts.empty())
3443 {
3444 infoLog << "Varying '" << baseName
3445 << "' is not an array to be captured by element.";
3446 return false;
3447 }
3448 elementCount = 1;
3449 }
3450
3451 // TODO(jmadill): Investigate implementation limits on D3D11
3452 componentCount = VariableComponentCount(var->type) * elementCount;
3453 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3454 componentCount > caps.maxTransformFeedbackSeparateComponents)
3455 {
3456 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3457 << componentCount << ") exceed the maximum separate components ("
3458 << caps.maxTransformFeedbackSeparateComponents << ").";
3459 return false;
3460 }
3461
3462 totalComponents += componentCount;
3463 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3464 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3465 {
3466 infoLog << "Transform feedback varying total components (" << totalComponents
3467 << ") exceed the maximum interleaved components ("
3468 << caps.maxTransformFeedbackInterleavedComponents << ").";
3469 return false;
3470 }
3471 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003472 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003473}
3474
jchen103fd614d2018-08-13 12:21:58 +08003475bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003476{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003477 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003478 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003479
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003480 for (const auto &attrib : attributes)
3481 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003482 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003483 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003484 Shader *shader = mState.mAttachedShaders[shaderType];
3485 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003486 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003487 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003488 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003489
jchen103fd614d2018-08-13 12:21:58 +08003490 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003491 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003492 {
3493 if (uniform.name == attrib.name)
3494 {
3495 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3496 return false;
3497 }
3498 }
3499 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003500 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003501
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003502 return true;
3503}
3504
Jamie Madill3c1da042017-11-27 18:33:40 -05003505void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003506{
3507 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003508 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003509 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003510 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003511 std::vector<unsigned int> subscripts;
3512 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003513 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003514 if (!subscripts.empty())
3515 {
3516 subscript = subscripts.back();
3517 }
Jamie Madill192745a2016-12-22 15:58:21 -05003518 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003519 {
Jamie Madill192745a2016-12-22 15:58:21 -05003520 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003521 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003522 {
jchen10a9042d32017-03-17 08:50:45 +08003523 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3524 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003525 break;
3526 }
jchen108225e732017-11-14 16:29:03 +08003527 else if (varying->isStruct())
3528 {
3529 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3530 if (field != nullptr)
3531 {
3532 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3533 break;
3534 }
3535 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003536 }
3537 }
James Darpinian30b604d2018-03-12 17:26:57 -07003538 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003539}
3540
jchen103fd614d2018-08-13 12:21:58 +08003541ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003542{
Jamie Madill3c1da042017-11-27 18:33:40 -05003543 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003544
Jiawei Shao016105b2018-04-12 16:38:31 +08003545 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003546 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003547 {
Jamie Madill192745a2016-12-22 15:58:21 -05003548 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003549 }
3550
Jiawei Shao016105b2018-04-12 16:38:31 +08003551 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003552 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003553 {
Jamie Madill192745a2016-12-22 15:58:21 -05003554 merged[varying.name].fragment = &varying;
3555 }
3556
3557 return merged;
3558}
3559
jchen103fd614d2018-08-13 12:21:58 +08003560bool Program::linkOutputVariables(const Caps &caps,
3561 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003562 GLuint combinedImageUniformsCount,
3563 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003564{
Jiawei Shao016105b2018-04-12 16:38:31 +08003565 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003566 ASSERT(fragmentShader != nullptr);
3567
Geoff Lange0cff192017-05-30 13:04:56 -04003568 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003569 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003570 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003571
jchen103fd614d2018-08-13 12:21:58 +08003572 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003573 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003574 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003575 {
3576 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3577 outputVariable.name != "gl_FragData")
3578 {
3579 continue;
3580 }
3581
3582 unsigned int baseLocation =
3583 (outputVariable.location == -1 ? 0u
3584 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003585
3586 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3587 // structures, so we may use getBasicTypeElementCount().
3588 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3589 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003590 {
3591 const unsigned int location = baseLocation + elementIndex;
3592 if (location >= mState.mOutputVariableTypes.size())
3593 {
3594 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3595 }
Corentin Walleze7557742017-06-01 13:09:57 -04003596 ASSERT(location < mState.mActiveOutputVariables.size());
3597 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003598 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003599 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003600 }
3601 }
3602
jchen103fd614d2018-08-13 12:21:58 +08003603 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003604 {
3605 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3606 // A link error will be generated if the sum of the number of active image uniforms used in
3607 // all shaders, the number of active shader storage blocks, and the number of active
3608 // fragment shader outputs exceeds the implementation-dependent value of
3609 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3610 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3611 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003612 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003613 {
3614 mInfoLog
3615 << "The sum of the number of active image uniforms, active shader storage blocks "
3616 "and active fragment shader outputs exceeds "
3617 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003618 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003619 return false;
3620 }
3621 }
3622
Jamie Madill80a6fc02015-08-21 16:53:16 -04003623 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003624 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003625 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003626
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003627 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003628 // TODO(jmadill): any caps validation here?
3629
jchen1015015f72017-03-16 13:54:21 +08003630 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003631 outputVariableIndex++)
3632 {
jchen1015015f72017-03-16 13:54:21 +08003633 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003634
Olli Etuahod2551232017-10-26 20:03:33 +03003635 if (outputVariable.isArray())
3636 {
3637 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3638 // Resources and including [0] at the end of array variable names.
3639 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3640 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3641 }
3642
Jamie Madill80a6fc02015-08-21 16:53:16 -04003643 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3644 if (outputVariable.isBuiltIn())
3645 continue;
3646
3647 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003648 unsigned int baseLocation =
3649 (outputVariable.location == -1 ? 0u
3650 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003651
Olli Etuaho465835d2017-09-26 13:34:10 +03003652 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3653 // structures, so we may use getBasicTypeElementCount().
3654 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3655 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003656 {
Olli Etuahod2551232017-10-26 20:03:33 +03003657 const unsigned int location = baseLocation + elementIndex;
3658 if (location >= mState.mOutputLocations.size())
3659 {
3660 mState.mOutputLocations.resize(location + 1);
3661 }
3662 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003663 if (outputVariable.isArray())
3664 {
3665 mState.mOutputLocations[location] =
3666 VariableLocation(elementIndex, outputVariableIndex);
3667 }
3668 else
3669 {
3670 VariableLocation locationInfo;
3671 locationInfo.index = outputVariableIndex;
3672 mState.mOutputLocations[location] = locationInfo;
3673 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003674 }
3675 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003676
3677 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003678}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003679
Olli Etuaho48fed632017-03-16 12:05:30 +00003680void Program::setUniformValuesFromBindingQualifiers()
3681{
Jamie Madill982f6e02017-06-07 14:33:04 -04003682 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003683 {
3684 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3685 if (samplerUniform.binding != -1)
3686 {
Olli Etuahod2551232017-10-26 20:03:33 +03003687 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003688 ASSERT(location != -1);
3689 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003690 for (unsigned int elementIndex = 0;
3691 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003692 {
3693 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3694 }
3695 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3696 boundTextureUnits.data());
3697 }
3698 }
3699}
3700
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003701void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003702{
jchen10af713a22017-04-19 09:10:56 +08003703 // Set initial bindings from shader.
3704 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3705 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003706 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003707 bindUniformBlock(blockIndex, uniformBlock.binding);
3708 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003709}
3710
Jamie Madille7d84322017-01-10 18:21:59 -05003711void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003712 GLsizei clampedCount,
3713 const GLint *v)
3714{
Jamie Madill81c2e252017-09-09 23:32:46 -04003715 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3716 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003717 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3718 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003719
Jamie Madill7e4eff12018-08-08 15:49:26 -04003720 if (samplerBinding.unreferenced)
3721 return;
3722
3723 // Update the sampler uniforms.
3724 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3725 {
3726 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3727 GLint newSamplerIndex = v[arrayIndex];
3728
3729 if (oldSamplerIndex == newSamplerIndex)
3730 continue;
3731
3732 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3733
3734 // Update the reference counts.
3735 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3736 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3737 ASSERT(oldRefCount > 0);
3738 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3739 oldRefCount--;
3740 newRefCount++;
3741
3742 // Check for binding type change.
3743 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3744 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3745
3746 if (newRefCount == 1)
3747 {
3748 newSamplerType = samplerBinding.textureType;
3749 mState.mActiveSamplersMask.set(newSamplerIndex);
3750 }
3751 else if (newSamplerType != samplerBinding.textureType)
3752 {
3753 // Conflict detected. Ensure we reset it properly.
3754 newSamplerType = TextureType::InvalidEnum;
3755 }
3756
3757 // Unset previously active sampler.
3758 if (oldRefCount == 0)
3759 {
3760 oldSamplerType = TextureType::InvalidEnum;
3761 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3762 }
3763 else if (oldSamplerType == TextureType::InvalidEnum)
3764 {
3765 // Previous conflict. Check if this new change fixed the conflict.
3766 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3767 }
3768 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003769
3770 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003771 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003772}
3773
Jamie Madill7e4eff12018-08-08 15:49:26 -04003774TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3775{
3776 TextureType foundType = TextureType::InvalidEnum;
3777
3778 for (const SamplerBinding &binding : mSamplerBindings)
3779 {
3780 if (binding.unreferenced)
3781 continue;
3782
3783 // A conflict exists if samplers of different types are sourced by the same texture unit.
3784 // We need to check all bound textures to detect this error case.
3785 for (GLuint textureUnit : binding.boundTextureUnits)
3786 {
3787 if (textureUnit == textureUnitIndex)
3788 {
3789 if (foundType == TextureType::InvalidEnum)
3790 {
3791 foundType = binding.textureType;
3792 }
3793 else if (foundType != binding.textureType)
3794 {
3795 return TextureType::InvalidEnum;
3796 }
3797 }
3798 }
3799 }
3800
3801 return foundType;
3802}
3803
Jamie Madille7d84322017-01-10 18:21:59 -05003804template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003805GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3806 GLsizei count,
3807 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003808 const T *v)
3809{
Jamie Madill134f93d2017-08-31 17:11:00 -04003810 if (count == 1)
3811 return 1;
3812
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003813 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003814
Corentin Wallez15ac5342016-11-03 17:06:39 -04003815 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3816 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003817 unsigned int remainingElements =
3818 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003819 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003820 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003821
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003822 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003823 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003824 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003825 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003826
3827 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003828}
3829
3830template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003831GLsizei Program::clampMatrixUniformCount(GLint location,
3832 GLsizei count,
3833 GLboolean transpose,
3834 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003835{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003836 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3837
Jamie Madill62d31cb2015-09-11 13:25:51 -04003838 if (!transpose)
3839 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003840 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003841 }
3842
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003843 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003844
3845 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3846 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003847 unsigned int remainingElements =
3848 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003849 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003850}
3851
Jamie Madill54164b02017-08-28 15:17:37 -04003852// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3853// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003854template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003855void Program::getUniformInternal(const Context *context,
3856 DestT *dataOut,
3857 GLint location,
3858 GLenum nativeType,
3859 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003860{
Jamie Madill54164b02017-08-28 15:17:37 -04003861 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003862 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003863 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003864 {
3865 GLint tempValue[16] = {0};
3866 mProgram->getUniformiv(context, location, tempValue);
3867 UniformStateQueryCastLoop<GLboolean>(
3868 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003869 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003870 }
3871 case GL_INT:
3872 {
3873 GLint tempValue[16] = {0};
3874 mProgram->getUniformiv(context, location, tempValue);
3875 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3876 components);
3877 break;
3878 }
3879 case GL_UNSIGNED_INT:
3880 {
3881 GLuint tempValue[16] = {0};
3882 mProgram->getUniformuiv(context, location, tempValue);
3883 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3884 components);
3885 break;
3886 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003887 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003888 {
3889 GLfloat tempValue[16] = {0};
3890 mProgram->getUniformfv(context, location, tempValue);
3891 UniformStateQueryCastLoop<GLfloat>(
3892 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003893 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003894 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003895 default:
3896 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003897 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003898 }
3899}
Jamie Madilla4595b82017-01-11 17:36:34 -05003900
3901bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3902{
jchen107ae70d82018-07-06 13:47:01 +08003903 resolveLink();
Jamie Madilla4595b82017-01-11 17:36:34 -05003904 // Must be called after samplers are validated.
3905 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3906
3907 for (const auto &binding : mState.mSamplerBindings)
3908 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003909 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003910 for (const auto &unit : binding.boundTextureUnits)
3911 {
3912 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3913 if (programTextureID == textureID)
3914 {
3915 // TODO(jmadill): Check for appropriate overlap.
3916 return true;
3917 }
3918 }
3919 }
3920
3921 return false;
3922}
3923
Jamie Madilla2c74982016-12-12 11:20:42 -05003924} // namespace gl