blob: 834f737a4c9a58f34108560b8a8f1edb26fe6b5b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
Olli Etuahod2551232017-10-26 20:03:33 +030017#include "common/string_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/MemoryProgramCache.h"
Jamie Madill7af0de52017-11-06 17:09:33 -050022#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill437d2662014-12-05 14:23:35 -050023#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040024#include "libANGLE/Uniform.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040025#include "libANGLE/VaryingPacking.h"
jchen103fd614d2018-08-13 12:21:58 +080026#include "libANGLE/Version.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040028#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040029#include "libANGLE/queryconversions.h"
30#include "libANGLE/renderer/GLImplFactory.h"
31#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040032#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050033
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000036
Geoff Lang7dd2e102014-11-10 15:19:26 -050037namespace
38{
39
Jamie Madill62d31cb2015-09-11 13:25:51 -040040// This simplified cast function doesn't need to worry about advanced concepts like
41// depth range values, or casting to bool.
42template <typename DestT, typename SrcT>
43DestT UniformStateQueryCast(SrcT value);
44
45// From-Float-To-Integer Casts
46template <>
47GLint UniformStateQueryCast(GLfloat value)
48{
49 return clampCast<GLint>(roundf(value));
50}
51
52template <>
53GLuint UniformStateQueryCast(GLfloat value)
54{
55 return clampCast<GLuint>(roundf(value));
56}
57
58// From-Integer-to-Integer Casts
59template <>
60GLint UniformStateQueryCast(GLuint value)
61{
62 return clampCast<GLint>(value);
63}
64
65template <>
66GLuint UniformStateQueryCast(GLint value)
67{
68 return clampCast<GLuint>(value);
69}
70
71// From-Boolean-to-Anything Casts
72template <>
73GLfloat UniformStateQueryCast(GLboolean value)
74{
Geoff Lang92019432017-11-20 13:09:34 -050075 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040076}
77
78template <>
79GLint UniformStateQueryCast(GLboolean value)
80{
Geoff Lang92019432017-11-20 13:09:34 -050081 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040082}
83
84template <>
85GLuint UniformStateQueryCast(GLboolean value)
86{
Geoff Lang92019432017-11-20 13:09:34 -050087 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040088}
89
90// Default to static_cast
91template <typename DestT, typename SrcT>
92DestT UniformStateQueryCast(SrcT value)
93{
94 return static_cast<DestT>(value);
95}
96
97template <typename SrcT, typename DestT>
98void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
99{
100 for (int comp = 0; comp < components; ++comp)
101 {
102 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
103 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
104 size_t offset = comp * 4;
105 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
106 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
107 }
108}
109
jchen1015015f72017-03-16 13:54:21 +0800110template <typename VarT>
111GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
112{
Olli Etuahod2551232017-10-26 20:03:33 +0300113 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800114 for (size_t index = 0; index < list.size(); index++)
115 {
116 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300117 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800118 {
Olli Etuahod2551232017-10-26 20:03:33 +0300119 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800120 }
121 }
122
123 return GL_INVALID_INDEX;
124}
125
Olli Etuahod2551232017-10-26 20:03:33 +0300126template <typename VarT>
127GLint GetVariableLocation(const std::vector<VarT> &list,
128 const std::vector<VariableLocation> &locationList,
129 const std::string &name)
130{
131 size_t nameLengthWithoutArrayIndex;
132 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
133
134 for (size_t location = 0u; location < locationList.size(); ++location)
135 {
136 const VariableLocation &variableLocation = locationList[location];
137 if (!variableLocation.used())
138 {
139 continue;
140 }
141
142 const VarT &variable = list[variableLocation.index];
143
144 if (angle::BeginsWith(variable.name, name))
145 {
146 if (name.length() == variable.name.length())
147 {
148 ASSERT(name == variable.name);
149 // GLES 3.1 November 2016 page 87.
150 // The string exactly matches the name of the active variable.
151 return static_cast<GLint>(location);
152 }
153 if (name.length() + 3u == variable.name.length() && variable.isArray())
154 {
155 ASSERT(name + "[0]" == variable.name);
156 // The string identifies the base name of an active array, where the string would
157 // exactly match the name of the variable if the suffix "[0]" were appended to the
158 // string.
159 return static_cast<GLint>(location);
160 }
161 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300162 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300163 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
164 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
165 {
166 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
167 // The string identifies an active element of the array, where the string ends with the
168 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
169 // zeroes, or whitespace) identifying an array element, and the "]" character, the
170 // integer is less than the number of active elements of the array variable, and where
171 // the string would exactly match the enumerated name of the array if the decimal
172 // integer were replaced with zero.
173 return static_cast<GLint>(location);
174 }
175 }
176
177 return -1;
178}
179
jchen10fd7c3b52017-03-21 15:36:03 +0800180void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
181{
182 ASSERT(bufSize > 0);
183 strncpy(buffer, string.c_str(), bufSize);
184 buffer[bufSize - 1] = '\0';
185
186 if (length)
187 {
188 *length = static_cast<GLsizei>(strlen(buffer));
189 }
190}
191
jchen10a9042d32017-03-17 08:50:45 +0800192bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
193{
Olli Etuahoc8538042017-09-27 11:20:15 +0300194 std::vector<unsigned int> subscripts;
195 std::string baseName = ParseResourceName(name, &subscripts);
196 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800197 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300198 std::vector<unsigned int> arrayIndices;
199 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
200 if (baseName == arrayName &&
201 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800202 {
203 return true;
204 }
205 }
206 return false;
207}
208
Jiawei Shao40786bd2018-04-18 13:58:57 +0800209std::string GetInterfaceBlockLimitName(ShaderType shaderType, sh::BlockType blockType)
210{
211 std::ostringstream stream;
212 stream << "GL_MAX_" << GetShaderTypeString(shaderType) << "_";
213
214 switch (blockType)
215 {
216 case sh::BlockType::BLOCK_UNIFORM:
217 stream << "UNIFORM_BUFFERS";
218 break;
219 case sh::BlockType::BLOCK_BUFFER:
220 stream << "SHADER_STORAGE_BLOCKS";
221 break;
222 default:
223 UNREACHABLE();
224 return "";
225 }
226
227 if (shaderType == ShaderType::Geometry)
228 {
229 stream << "_EXT";
230 }
231
232 return stream.str();
233}
234
235const char *GetInterfaceBlockTypeString(sh::BlockType blockType)
236{
237 switch (blockType)
238 {
239 case sh::BlockType::BLOCK_UNIFORM:
240 return "uniform block";
241 case sh::BlockType::BLOCK_BUFFER:
242 return "shader storage block";
243 default:
244 UNREACHABLE();
245 return "";
246 }
247}
248
249void LogInterfaceBlocksExceedLimit(InfoLog &infoLog,
250 ShaderType shaderType,
251 sh::BlockType blockType,
252 GLuint limit)
253{
254 infoLog << GetShaderTypeString(shaderType) << " shader "
255 << GetInterfaceBlockTypeString(blockType) << " count exceeds "
256 << GetInterfaceBlockLimitName(shaderType, blockType) << " (" << limit << ")";
257}
258
Jiawei Shao427071e2018-03-19 09:21:37 +0800259bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800260 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800261 ShaderType shaderType,
262 sh::BlockType blockType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800263 GLuint *combinedInterfaceBlocksCount,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800264 InfoLog &infoLog)
265{
266 GLuint blockCount = 0;
267 for (const sh::InterfaceBlock &block : interfaceBlocks)
268 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800269 if (IsActiveInterfaceBlock(block))
Jiajia Qin729b2c62017-08-14 09:36:11 +0800270 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800271 blockCount += std::max(block.arraySize, 1u);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800272 if (blockCount > maxInterfaceBlocks)
273 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800274 LogInterfaceBlocksExceedLimit(infoLog, shaderType, blockType, maxInterfaceBlocks);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800275 return false;
276 }
277 }
278 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800279
280 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
281 // If a uniform block is used by multiple shader stages, each such use counts separately
282 // against this combined limit.
283 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
284 // If a shader storage block in a program is referenced by multiple shaders, each such
285 // reference counts separately against this combined limit.
286 if (combinedInterfaceBlocksCount)
287 {
288 *combinedInterfaceBlocksCount += blockCount;
289 }
290
Jiajia Qin729b2c62017-08-14 09:36:11 +0800291 return true;
292}
293
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800294GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
295{
296 std::vector<unsigned int> subscripts;
297 std::string baseName = ParseResourceName(name, &subscripts);
298
299 unsigned int numBlocks = static_cast<unsigned int>(list.size());
300 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
301 {
302 const auto &block = list[blockIndex];
303 if (block.name == baseName)
304 {
305 const bool arrayElementZero =
306 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
307 const bool arrayElementMatches =
308 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
309 if (arrayElementMatches || arrayElementZero)
310 {
311 return blockIndex;
312 }
313 }
314 }
315
316 return GL_INVALID_INDEX;
317}
318
319void GetInterfaceBlockName(const GLuint index,
320 const std::vector<InterfaceBlock> &list,
321 GLsizei bufSize,
322 GLsizei *length,
323 GLchar *name)
324{
325 ASSERT(index < list.size());
326
327 const auto &block = list[index];
328
329 if (bufSize > 0)
330 {
331 std::string blockName = block.name;
332
333 if (block.isArray)
334 {
335 blockName += ArrayString(block.arrayElement);
336 }
337 CopyStringToBuffer(name, blockName, bufSize, length);
338 }
339}
340
jchen103fd614d2018-08-13 12:21:58 +0800341void InitUniformBlockLinker(const ProgramState &state, UniformBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500342{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800343 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500344 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800345 Shader *shader = state.getAttachedShader(shaderType);
346 if (shader)
347 {
jchen103fd614d2018-08-13 12:21:58 +0800348 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800349 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500350 }
351}
352
jchen103fd614d2018-08-13 12:21:58 +0800353void InitShaderStorageBlockLinker(const ProgramState &state, ShaderStorageBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500354{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800355 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500356 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800357 Shader *shader = state.getAttachedShader(shaderType);
358 if (shader != nullptr)
359 {
jchen103fd614d2018-08-13 12:21:58 +0800360 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800361 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500362 }
363}
364
jchen108225e732017-11-14 16:29:03 +0800365// Find the matching varying or field by name.
366const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
367 const std::string &name)
368{
369 const sh::ShaderVariable *var = nullptr;
370 for (const auto &ref : varyings)
371 {
372 const sh::Varying *varying = ref.second.get();
373 if (varying->name == name)
374 {
375 var = varying;
376 break;
377 }
378 var = FindShaderVarField(*varying, name);
379 if (var != nullptr)
380 {
381 break;
382 }
383 }
384 return var;
385}
386
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800387void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
388{
389 ASSERT(mismatchedFieldName);
390 if (mismatchedFieldName->empty())
391 {
392 *mismatchedFieldName = parentName;
393 }
394 else
395 {
396 std::ostringstream stream;
397 stream << parentName << "." << *mismatchedFieldName;
398 *mismatchedFieldName = stream.str();
399 }
400}
401
402const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
403{
404 switch (linkError)
405 {
406 case LinkMismatchError::TYPE_MISMATCH:
407 return "Type";
408 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
409 return "Array size";
410 case LinkMismatchError::PRECISION_MISMATCH:
411 return "Precision";
412 case LinkMismatchError::STRUCT_NAME_MISMATCH:
413 return "Structure name";
414 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
415 return "Field number";
416 case LinkMismatchError::FIELD_NAME_MISMATCH:
417 return "Field name";
418
419 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
420 return "Interpolation type";
421 case LinkMismatchError::INVARIANCE_MISMATCH:
422 return "Invariance";
423
424 case LinkMismatchError::BINDING_MISMATCH:
425 return "Binding layout qualifier";
426 case LinkMismatchError::LOCATION_MISMATCH:
427 return "Location layout qualifier";
428 case LinkMismatchError::OFFSET_MISMATCH:
429 return "Offset layout qualilfier";
430
431 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
432 return "Layout qualifier";
433 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
434 return "Matrix Packing";
435 default:
436 UNREACHABLE();
437 return "";
438 }
439}
440
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800441LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
442 const sh::InterfaceBlockField &blockField2,
443 bool webglCompatibility,
444 std::string *mismatchedBlockFieldName)
445{
446 if (blockField1.name != blockField2.name)
447 {
448 return LinkMismatchError::FIELD_NAME_MISMATCH;
449 }
450
451 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
452 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
453 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
454 if (linkError != LinkMismatchError::NO_MISMATCH)
455 {
456 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
457 return linkError;
458 }
459
460 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
461 {
462 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
463 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
464 }
465
466 return LinkMismatchError::NO_MISMATCH;
467}
468
469LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
470 const sh::InterfaceBlock &interfaceBlock2,
471 bool webglCompatibility,
472 std::string *mismatchedBlockFieldName)
473{
474 // validate blocks for the same member types
475 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
476 {
477 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
478 }
479 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
480 {
481 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
482 }
483 if (interfaceBlock1.layout != interfaceBlock2.layout ||
484 interfaceBlock1.binding != interfaceBlock2.binding)
485 {
486 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
487 }
488 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
489 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
490 {
491 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
492 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
493
494 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
495 member1, member2, webglCompatibility, mismatchedBlockFieldName);
496 if (linkError != LinkMismatchError::NO_MISMATCH)
497 {
498 return linkError;
499 }
500 }
501 return LinkMismatchError::NO_MISMATCH;
502}
503
Jiawei Shao40786bd2018-04-18 13:58:57 +0800504using ShaderInterfaceBlock = std::pair<ShaderType, const sh::InterfaceBlock *>;
505using InterfaceBlockMap = std::map<std::string, ShaderInterfaceBlock>;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800506
Jiawei Shao40786bd2018-04-18 13:58:57 +0800507void InitializeInterfaceBlockMap(const std::vector<sh::InterfaceBlock> &interfaceBlocks,
508 ShaderType shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800509 InterfaceBlockMap *linkedInterfaceBlocks)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800510{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800511 ASSERT(linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800512
513 for (const sh::InterfaceBlock &interfaceBlock : interfaceBlocks)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800514 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800515 (*linkedInterfaceBlocks)[interfaceBlock.name] = std::make_pair(shaderType, &interfaceBlock);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800516 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800517}
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800518
Jiawei Shao40786bd2018-04-18 13:58:57 +0800519bool ValidateGraphicsInterfaceBlocksPerShader(
520 const std::vector<sh::InterfaceBlock> &interfaceBlocksToLink,
521 ShaderType shaderType,
522 bool webglCompatibility,
523 InterfaceBlockMap *linkedBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800524 InfoLog &infoLog)
525{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800526 ASSERT(linkedBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800527
528 for (const sh::InterfaceBlock &block : interfaceBlocksToLink)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800529 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800530 const auto &entry = linkedBlocks->find(block.name);
531 if (entry != linkedBlocks->end())
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800532 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800533 const sh::InterfaceBlock &linkedBlock = *(entry->second.second);
534 std::string mismatchedStructFieldName;
535 LinkMismatchError linkError = AreMatchingInterfaceBlocks(
536 block, linkedBlock, webglCompatibility, &mismatchedStructFieldName);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800537 if (linkError != LinkMismatchError::NO_MISMATCH)
538 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800539 LogLinkMismatch(infoLog, block.name, GetInterfaceBlockTypeString(block.blockType),
540 linkError, mismatchedStructFieldName, entry->second.first,
541 shaderType);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800542 return false;
543 }
544 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800545 else
546 {
547 (*linkedBlocks)[block.name] = std::make_pair(shaderType, &block);
548 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800549 }
550
551 return true;
552}
553
Jiawei Shao54aafe52018-04-27 14:54:57 +0800554bool ValidateInterfaceBlocksMatch(
555 GLuint numShadersHasInterfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800556 const ShaderMap<const std::vector<sh::InterfaceBlock> *> &shaderInterfaceBlocks,
557 InfoLog &infoLog,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800558 bool webglCompatibility)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800559{
Jiawei Shao54aafe52018-04-27 14:54:57 +0800560 if (numShadersHasInterfaceBlocks < 2u)
561 {
562 return true;
563 }
564
565 ASSERT(!shaderInterfaceBlocks[ShaderType::Compute]);
566
Jiawei Shao40786bd2018-04-18 13:58:57 +0800567 // Check that interface blocks defined in the graphics shaders are identical
568
569 InterfaceBlockMap linkedInterfaceBlocks;
Jiawei Shao40786bd2018-04-18 13:58:57 +0800570
571 bool interfaceBlockMapInitialized = false;
572 for (ShaderType shaderType : kAllGraphicsShaderTypes)
573 {
574 if (!shaderInterfaceBlocks[shaderType])
575 {
576 continue;
577 }
578
579 if (!interfaceBlockMapInitialized)
580 {
581 InitializeInterfaceBlockMap(*shaderInterfaceBlocks[shaderType], shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800582 &linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800583 interfaceBlockMapInitialized = true;
584 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800585 else if (!ValidateGraphicsInterfaceBlocksPerShader(*shaderInterfaceBlocks[shaderType],
586 shaderType, webglCompatibility,
587 &linkedInterfaceBlocks, infoLog))
Jiawei Shao40786bd2018-04-18 13:58:57 +0800588 {
589 return false;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800590 }
591 }
592
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800593 return true;
594}
595
Jamie Madill62d31cb2015-09-11 13:25:51 -0400596} // anonymous namespace
597
jchen107ae70d82018-07-06 13:47:01 +0800598// Saves the linking context for later use in resolveLink().
599struct Program::LinkingState
600{
601 const Context *context;
602 std::unique_ptr<ProgramLinkedResources> resources;
603 ProgramHash programHash;
604 std::unique_ptr<rx::LinkEvent> linkEvent;
605};
606
Jamie Madill4a3c2342015-10-08 12:58:45 -0400607const char *const g_fakepath = "C:\\fakepath";
608
Jamie Madill3c1da042017-11-27 18:33:40 -0500609// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400610InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000611{
612}
613
614InfoLog::~InfoLog()
615{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000616}
617
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400618size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000619{
Jamie Madill23176ce2017-07-31 14:14:33 -0400620 if (!mLazyStream)
621 {
622 return 0;
623 }
624
625 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400626 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000627}
628
Geoff Lange1a27752015-10-05 13:16:04 -0400629void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000630{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400631 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000632
633 if (bufSize > 0)
634 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400635 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400636
Jamie Madill23176ce2017-07-31 14:14:33 -0400637 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000638 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400639 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
640 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000641 }
642
643 infoLog[index] = '\0';
644 }
645
646 if (length)
647 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400648 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000649 }
650}
651
652// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300653// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000654// messages, so lets remove all occurrences of this fake file path from the log.
655void InfoLog::appendSanitized(const char *message)
656{
Jamie Madill23176ce2017-07-31 14:14:33 -0400657 ensureInitialized();
658
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000659 std::string msg(message);
660
661 size_t found;
662 do
663 {
664 found = msg.find(g_fakepath);
665 if (found != std::string::npos)
666 {
667 msg.erase(found, strlen(g_fakepath));
668 }
Jamie Madill493f9572018-05-24 19:52:15 -0400669 } while (found != std::string::npos);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000670
Jamie Madill23176ce2017-07-31 14:14:33 -0400671 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000672}
673
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000674void InfoLog::reset()
675{
Jiawei Shao02f15232017-12-27 10:10:28 +0800676 if (mLazyStream)
677 {
678 mLazyStream.reset(nullptr);
679 }
680}
681
682bool InfoLog::empty() const
683{
684 if (!mLazyStream)
685 {
686 return true;
687 }
688
689 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000690}
691
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800692void LogLinkMismatch(InfoLog &infoLog,
693 const std::string &variableName,
694 const char *variableType,
695 LinkMismatchError linkError,
696 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800697 ShaderType shaderType1,
698 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800699{
700 std::ostringstream stream;
701 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
702 << variableName;
703
704 if (!mismatchedStructOrBlockFieldName.empty())
705 {
706 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
707 }
708
709 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
710 << GetShaderTypeString(shaderType2) << " shaders.";
711
712 infoLog << stream.str();
713}
714
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800715bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
716{
717 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200718 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800719}
720
Jamie Madill3c1da042017-11-27 18:33:40 -0500721// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300722VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000723{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724}
725
Olli Etuahoc8538042017-09-27 11:20:15 +0300726VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300727 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500728{
Olli Etuahoc8538042017-09-27 11:20:15 +0300729 ASSERT(arrayIndex != GL_INVALID_INDEX);
730}
731
Jamie Madill3c1da042017-11-27 18:33:40 -0500732// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800733SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500734 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
735{
736}
737
738SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
739
740SamplerBinding::~SamplerBinding() = default;
741
Jamie Madill3c1da042017-11-27 18:33:40 -0500742// ProgramBindings implementation.
743ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500744{
745}
746
Jamie Madill3c1da042017-11-27 18:33:40 -0500747ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500748{
749}
750
Jamie Madill3c1da042017-11-27 18:33:40 -0500751void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400752{
753 mBindings[name] = index;
754}
755
Jamie Madill3c1da042017-11-27 18:33:40 -0500756int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400757{
758 auto iter = mBindings.find(name);
759 return (iter != mBindings.end()) ? iter->second : -1;
760}
761
Jamie Madill3c1da042017-11-27 18:33:40 -0500762ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400763{
764 return mBindings.begin();
765}
766
Jamie Madill3c1da042017-11-27 18:33:40 -0500767ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400768{
769 return mBindings.end();
770}
771
Jamie Madill3c1da042017-11-27 18:33:40 -0500772// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500773ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
774{
775}
776ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
777{
778 for (size_t index = 0; index < count; ++index)
779 {
780 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
781 }
782}
783
784ImageBinding::ImageBinding(const ImageBinding &other) = default;
785
786ImageBinding::~ImageBinding() = default;
787
Jamie Madill3c1da042017-11-27 18:33:40 -0500788// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400789ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500790 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800791 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500792 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400793 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500794 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800795 mImageUniformRange(0, 0),
796 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300797 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800798 mNumViews(-1),
799 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400800 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
801 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800802 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400803 mGeometryShaderMaxVertices(0),
804 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400805{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300806 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400807 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400808}
809
Jamie Madill48ef11b2016-04-27 15:21:52 -0400810ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400811{
Jiawei Shao016105b2018-04-12 16:38:31 +0800812 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400813}
814
Jamie Madill48ef11b2016-04-27 15:21:52 -0400815const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500816{
817 return mLabel;
818}
819
Jiawei Shao385b3e02018-03-21 09:43:28 +0800820Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
821{
Jiawei Shao016105b2018-04-12 16:38:31 +0800822 ASSERT(shaderType != ShaderType::InvalidEnum);
823 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800824}
825
Jamie Madille7d84322017-01-10 18:21:59 -0500826GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400827{
jchen1015015f72017-03-16 13:54:21 +0800828 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400829}
830
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800831GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
832{
833 return GetResourceIndexFromName(mBufferVariables, name);
834}
835
Jamie Madille7d84322017-01-10 18:21:59 -0500836GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
837{
838 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
839 return mUniformLocations[location].index;
840}
841
842Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
843{
844 GLuint index = getUniformIndexFromLocation(location);
845 if (!isSamplerUniformIndex(index))
846 {
847 return Optional<GLuint>::Invalid();
848 }
849
850 return getSamplerIndexFromUniformIndex(index);
851}
852
853bool ProgramState::isSamplerUniformIndex(GLuint index) const
854{
Jamie Madill982f6e02017-06-07 14:33:04 -0400855 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500856}
857
858GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
859{
860 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400861 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500862}
863
Jamie Madill34ca4f52017-06-13 11:49:39 -0400864GLuint ProgramState::getAttributeLocation(const std::string &name) const
865{
866 for (const sh::Attribute &attribute : mAttributes)
867 {
868 if (attribute.name == name)
869 {
870 return attribute.location;
871 }
872 }
873
874 return static_cast<GLuint>(-1);
875}
876
Jiawei Shao016105b2018-04-12 16:38:31 +0800877bool ProgramState::hasAttachedShader() const
878{
879 for (const Shader *shader : mAttachedShaders)
880 {
881 if (shader)
882 {
883 return true;
884 }
885 }
886 return false;
887}
888
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500889Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400890 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400891 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800893 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894 mDeleteStatus(false),
895 mRefCount(0),
896 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500897 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898{
899 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000900
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
904Program::~Program()
905{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400906 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
Jamie Madill4928b7c2017-06-20 12:57:39 -0400909void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500910{
jchen107ae70d82018-07-06 13:47:01 +0800911 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800912 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500913 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800914 if (mState.mAttachedShaders[shaderType])
915 {
916 mState.mAttachedShaders[shaderType]->release(context);
917 mState.mAttachedShaders[shaderType] = nullptr;
918 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800919 }
920
Jamie Madill21061022018-07-12 23:56:30 -0400921 // TODO(jmadill): Handle error in the Context. http://anglebug.com/2491
Jamie Madillb7d924a2018-03-10 11:16:54 -0500922 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400923
Jiawei Shao016105b2018-04-12 16:38:31 +0800924 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400925 SafeDelete(mProgram);
926
927 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500928}
jchen107ae70d82018-07-06 13:47:01 +0800929GLuint Program::id() const
930{
931 resolveLink();
932 return mHandle;
933}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500934
Geoff Lang70d0f492015-12-10 17:45:46 -0500935void Program::setLabel(const std::string &label)
936{
jchen107ae70d82018-07-06 13:47:01 +0800937 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400938 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500939}
940
941const std::string &Program::getLabel() const
942{
jchen107ae70d82018-07-06 13:47:01 +0800943 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400944 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500945}
jchen107ae70d82018-07-06 13:47:01 +0800946rx::ProgramImpl *Program::getImplementation() const
947{
948 resolveLink();
949 return mProgram;
950}
Geoff Lang70d0f492015-12-10 17:45:46 -0500951
Jamie Madillef300b12016-10-07 15:12:09 -0400952void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953{
jchen107ae70d82018-07-06 13:47:01 +0800954 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800955 ShaderType shaderType = shader->getType();
956 ASSERT(shaderType != ShaderType::InvalidEnum);
957
958 mState.mAttachedShaders[shaderType] = shader;
959 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
Jamie Madillc1d770e2017-04-13 17:31:24 -0400962void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
jchen107ae70d82018-07-06 13:47:01 +0800964 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800965 ShaderType shaderType = shader->getType();
966 ASSERT(shaderType != ShaderType::InvalidEnum);
967
968 ASSERT(mState.mAttachedShaders[shaderType] == shader);
969 shader->release(context);
970 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000973int Program::getAttachedShadersCount() const
974{
jchen107ae70d82018-07-06 13:47:01 +0800975 resolveLink();
Jiawei Shao016105b2018-04-12 16:38:31 +0800976 int numAttachedShaders = 0;
977 for (const Shader *shader : mState.mAttachedShaders)
978 {
979 if (shader)
980 {
981 ++numAttachedShaders;
982 }
983 }
984
985 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000986}
987
Jiawei Shao385b3e02018-03-21 09:43:28 +0800988const Shader *Program::getAttachedShader(ShaderType shaderType) const
989{
jchen107ae70d82018-07-06 13:47:01 +0800990 resolveLink();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800991 return mState.getAttachedShader(shaderType);
992}
993
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994void Program::bindAttributeLocation(GLuint index, const char *name)
995{
jchen107ae70d82018-07-06 13:47:01 +0800996 resolveLink();
Geoff Langd8605522016-04-13 10:19:12 -0400997 mAttributeBindings.bindLocation(index, name);
998}
999
1000void Program::bindUniformLocation(GLuint index, const char *name)
1001{
jchen107ae70d82018-07-06 13:47:01 +08001002 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001003 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
Sami Väisänen46eaa942016-06-29 10:26:37 +03001006void Program::bindFragmentInputLocation(GLint index, const char *name)
1007{
jchen107ae70d82018-07-06 13:47:01 +08001008 resolveLink();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001009 mFragmentInputBindings.bindLocation(index, name);
1010}
1011
jchen103fd614d2018-08-13 12:21:58 +08001012BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001013{
jchen107ae70d82018-07-06 13:47:01 +08001014 resolveLink();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001015 BindingInfo ret;
1016 ret.type = GL_NONE;
1017 ret.valid = false;
1018
Jiawei Shao385b3e02018-03-21 09:43:28 +08001019 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001020 ASSERT(fragmentShader);
1021
1022 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001023 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001024
1025 for (const auto &binding : mFragmentInputBindings)
1026 {
1027 if (binding.second != static_cast<GLuint>(index))
1028 continue;
1029
1030 ret.valid = true;
1031
Olli Etuahod2551232017-10-26 20:03:33 +03001032 size_t nameLengthWithoutArrayIndex;
1033 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001034
1035 for (const auto &in : inputs)
1036 {
Olli Etuahod2551232017-10-26 20:03:33 +03001037 if (in.name.length() == nameLengthWithoutArrayIndex &&
1038 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001039 {
1040 if (in.isArray())
1041 {
1042 // The client wants to bind either "name" or "name[0]".
1043 // GL ES 3.1 spec refers to active array names with language such as:
1044 // "if the string identifies the base name of an active array, where the
1045 // string would exactly match the name of the variable if the suffix "[0]"
1046 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001047 if (arrayIndex == GL_INVALID_INDEX)
1048 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001049
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001050 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001051 }
1052 else
1053 {
1054 ret.name = in.mappedName;
1055 }
1056 ret.type = in.type;
1057 return ret;
1058 }
1059 }
1060 }
1061
1062 return ret;
1063}
1064
jchen103fd614d2018-08-13 12:21:58 +08001065void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001066 GLenum genMode,
1067 GLint components,
1068 const GLfloat *coeffs)
1069{
jchen107ae70d82018-07-06 13:47:01 +08001070 resolveLink();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001071 // If the location is -1 then the command is silently ignored
1072 if (index == -1)
1073 return;
1074
jchen103fd614d2018-08-13 12:21:58 +08001075 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001076
1077 // If the input doesn't exist then then the command is silently ignored
1078 // This could happen through optimization for example, the shader translator
1079 // decides that a variable is not actually being used and optimizes it away.
1080 if (binding.name.empty())
1081 return;
1082
1083 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1084}
1085
Martin Radev4c4c8e72016-08-04 12:25:34 +03001086// The attached shaders are checked for linking errors by matching up their variables.
1087// Uniform, input and output variables get collected.
1088// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001089Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001090{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001091 const auto &data = context->getContextState();
1092
Jamie Madill6c58b062017-08-01 13:44:25 -04001093 auto *platform = ANGLEPlatformCurrent();
1094 double startTime = platform->currentTime(platform);
1095
Jamie Madill6c1f6712017-02-14 19:08:04 -05001096 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001097 mInfoLog.reset();
1098
1099 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001100 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001101 {
1102 return NoError();
1103 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001104
jchen107ae70d82018-07-06 13:47:01 +08001105 ProgramHash programHash = {0};
Jamie Madill6bc264a2018-03-31 15:36:05 -04001106 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001107
Jamie Madill32447362017-06-28 14:53:52 -04001108 if (cache)
1109 {
1110 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001111 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001112 }
1113
1114 if (mLinked)
1115 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001116 double delta = platform->currentTime(platform) - startTime;
1117 int us = static_cast<int>(delta * 1000000.0);
1118 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001119 return NoError();
1120 }
1121
1122 // Cache load failed, fall through to normal linking.
1123 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001124
Jamie Madill6bc264a2018-03-31 15:36:05 -04001125 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001126 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001127
jchen107ae70d82018-07-06 13:47:01 +08001128 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001129 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001130 {
jchen107ae70d82018-07-06 13:47:01 +08001131 resources.reset(
1132 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1133 {&mState.mUniformBlocks, &mState.mUniforms},
1134 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1135 {&mState.mAtomicCounterBuffers},
1136 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001137
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001138 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001139 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1140 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001141 {
1142 return NoError();
1143 }
1144
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001145 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001146 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1147 context->getExtensions().webglCompatibility, mInfoLog,
1148 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001149 {
1150 return NoError();
1151 }
1152
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001153 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1154 // A link error will be generated if the sum of the number of active image uniforms used in
1155 // all shaders, the number of active shader storage blocks, and the number of active
1156 // fragment shader outputs exceeds the implementation-dependent value of
1157 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1158 if (combinedImageUniforms + combinedShaderStorageBlocks >
1159 context->getCaps().maxCombinedShaderOutputResources)
1160 {
1161 mInfoLog
1162 << "The sum of the number of active image uniforms, active shader storage blocks "
1163 "and active fragment shader outputs exceeds "
1164 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1165 << context->getCaps().maxCombinedShaderOutputResources << ")";
1166 return NoError();
1167 }
1168
jchen103fd614d2018-08-13 12:21:58 +08001169 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1170 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001171 }
1172 else
1173 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001174 // Map the varyings to the register file
1175 // In WebGL, we use a slightly different handling for packing variables.
1176 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1177 if (data.getLimitations().noFlexibleVaryingPacking)
1178 {
1179 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1180 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1181 }
1182 else if (data.getExtensions().webglCompatibility)
1183 {
1184 packMode = PackMode::WEBGL_STRICT;
1185 }
1186
jchen107ae70d82018-07-06 13:47:01 +08001187 resources.reset(
1188 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1189 {&mState.mUniformBlocks, &mState.mUniforms},
1190 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1191 {&mState.mAtomicCounterBuffers},
1192 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001193
jchen103fd614d2018-08-13 12:21:58 +08001194 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001195 {
1196 return NoError();
1197 }
1198
jchen103fd614d2018-08-13 12:21:58 +08001199 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001200 {
1201 return NoError();
1202 }
1203
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001204 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001205 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1206 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001207 {
1208 return NoError();
1209 }
1210
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001211 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001212 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1213 context->getExtensions().webglCompatibility, mInfoLog,
1214 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001215 {
1216 return NoError();
1217 }
1218
jchen103fd614d2018-08-13 12:21:58 +08001219 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001220 {
1221 return NoError();
1222 }
1223
jchen103fd614d2018-08-13 12:21:58 +08001224 if (!linkOutputVariables(context->getCaps(), context->getClientVersion(),
1225 combinedImageUniforms, combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001226 {
1227 return NoError();
1228 }
1229
jchen103fd614d2018-08-13 12:21:58 +08001230 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001231
Jiawei Shao016105b2018-04-12 16:38:31 +08001232 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001233 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001234
jchen103fd614d2018-08-13 12:21:58 +08001235 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1236 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001237
jchen103fd614d2018-08-13 12:21:58 +08001238 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1239 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001240 {
1241 return NoError();
1242 }
1243
jchen107ae70d82018-07-06 13:47:01 +08001244 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001245 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001246 {
1247 return NoError();
1248 }
1249
Martin Radev4c4c8e72016-08-04 12:25:34 +03001250 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001251 }
1252
jchen107ae70d82018-07-06 13:47:01 +08001253 mLinkingState.reset(new LinkingState());
1254 mLinkingState->context = context;
1255 mLinkingState->programHash = programHash;
1256 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1257 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001258 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001259
1260 return NoError();
1261}
1262
1263bool Program::isLinking() const
1264{
1265 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1266}
1267
1268bool Program::isLinked() const
1269{
1270 resolveLink();
1271 return mLinked;
1272}
1273
jchen107ae70d82018-07-06 13:47:01 +08001274void Program::resolveLinkImpl()
1275{
1276 ASSERT(mLinkingState.get());
1277
1278 mLinked = mLinkingState->linkEvent->wait();
jchen105055fba2018-08-17 09:57:07 +08001279 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001280 auto linkingState = std::move(mLinkingState);
1281 if (!mLinked)
1282 {
1283 return;
1284 }
1285
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001286 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001287
Yunchao Heece12532017-11-21 15:50:21 +08001288 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1289 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001290 ASSERT(mLinked);
1291 updateLinkedShaderStages();
1292
Jamie Madill54164b02017-08-28 15:17:37 -04001293 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001294 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001295
Jamie Madill7e4eff12018-08-08 15:49:26 -04001296 // Must be called after markUnusedUniformLocations.
1297 mState.updateActiveSamplers();
1298
1299 setUniformValuesFromBindingQualifiers();
1300
Jamie Madill32447362017-06-28 14:53:52 -04001301 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001302 auto *cache = linkingState->context->getMemoryProgramCache();
1303 if (cache &&
1304 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1305 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001306 {
jchen107ae70d82018-07-06 13:47:01 +08001307 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001308 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001309}
1310
Yunchao He85072e82017-11-14 15:43:28 +08001311void Program::updateLinkedShaderStages()
1312{
Yunchao Heece12532017-11-21 15:50:21 +08001313 mState.mLinkedShaderStages.reset();
1314
Jiawei Shao016105b2018-04-12 16:38:31 +08001315 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001316 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001317 if (shader)
1318 {
1319 mState.mLinkedShaderStages.set(shader->getType());
1320 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001321 }
Yunchao He85072e82017-11-14 15:43:28 +08001322}
1323
James Darpinian30b604d2018-03-12 17:26:57 -07001324void ProgramState::updateTransformFeedbackStrides()
1325{
1326 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1327 {
1328 mTransformFeedbackStrides.resize(1);
1329 size_t totalSize = 0;
1330 for (auto &varying : mLinkedTransformFeedbackVaryings)
1331 {
1332 totalSize += varying.size() * VariableExternalSize(varying.type);
1333 }
1334 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1335 }
1336 else
1337 {
1338 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1339 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1340 {
1341 auto &varying = mLinkedTransformFeedbackVaryings[i];
1342 mTransformFeedbackStrides[i] =
1343 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1344 }
1345 }
1346}
1347
Jamie Madill7e4eff12018-08-08 15:49:26 -04001348void ProgramState::updateActiveSamplers()
1349{
1350 for (SamplerBinding &samplerBinding : mSamplerBindings)
1351 {
1352 if (samplerBinding.unreferenced)
1353 continue;
1354
1355 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1356 {
1357 mActiveSamplerRefCounts[textureUnit]++;
1358 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1359 mActiveSamplersMask.set(textureUnit);
1360 }
1361 }
1362}
1363
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001364// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001365void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001367 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001368 mState.mAttributesTypeMask.reset();
1369 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001370 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001371 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001372 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001373 mState.mUniforms.clear();
1374 mState.mUniformLocations.clear();
1375 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001376 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001377 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001378 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001379 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001380 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001381 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001382 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001383 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001384 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001385 mState.mImageBindings.clear();
Jamie Madill493f9572018-05-24 19:52:15 -04001386 mState.mNumViews = -1;
1387 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1388 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001389 mState.mGeometryShaderInvocations = 1;
1390 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392 mValidated = false;
1393
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001394 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001395 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001396}
1397
Jamie Madilla2c74982016-12-12 11:20:42 -05001398Error Program::loadBinary(const Context *context,
1399 GLenum binaryFormat,
1400 const void *binary,
1401 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001402{
jchen107ae70d82018-07-06 13:47:01 +08001403 resolveLink();
Jamie Madill6c1f6712017-02-14 19:08:04 -05001404 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001405
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001407 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001409 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1410 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001411 {
Jamie Madillf6113162015-05-07 11:49:21 -04001412 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001413 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 }
1415
Jamie Madill4f86d052017-06-05 12:59:26 -04001416 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1417 ANGLE_TRY_RESULT(
1418 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001419
1420 // Currently we require the full shader text to compute the program hash.
1421 // TODO(jmadill): Store the binary in the internal program cache.
1422
Jamie Madill70aeda42018-08-20 12:17:40 -04001423 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1424 ++uniformBlockIndex)
1425 {
1426 mDirtyBits.set(uniformBlockIndex);
1427 }
1428
Jamie Madillb0a838b2016-11-13 20:02:12 -05001429 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001430#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431}
1432
Jamie Madilla2c74982016-12-12 11:20:42 -05001433Error Program::saveBinary(const Context *context,
1434 GLenum *binaryFormat,
1435 void *binary,
1436 GLsizei bufSize,
1437 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438{
jchen107ae70d82018-07-06 13:47:01 +08001439 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440 if (binaryFormat)
1441 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001442 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443 }
1444
Jamie Madill4f86d052017-06-05 12:59:26 -04001445 angle::MemoryBuffer memoryBuf;
1446 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447
Jamie Madill4f86d052017-06-05 12:59:26 -04001448 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1449 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450
1451 if (streamLength > bufSize)
1452 {
1453 if (length)
1454 {
1455 *length = 0;
1456 }
1457
Jamie Madill493f9572018-05-24 19:52:15 -04001458 // TODO: This should be moved to the validation layer but computing the size of the binary
1459 // before saving it causes the save to happen twice. It may be possible to write the binary
1460 // to a separate buffer, validate sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001461 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462 }
1463
1464 if (binary)
1465 {
Jamie Madill493f9572018-05-24 19:52:15 -04001466 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467
Jamie Madill48ef11b2016-04-27 15:21:52 -04001468 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 ptr += streamLength;
1470
1471 ASSERT(ptr - streamLength == binary);
1472 }
1473
1474 if (length)
1475 {
1476 *length = streamLength;
1477 }
1478
He Yunchaoacd18982017-01-04 10:46:42 +08001479 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
Jamie Madillffe00c02017-06-27 16:26:55 -04001482GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483{
jchen107ae70d82018-07-06 13:47:01 +08001484 resolveLink();
Geoff Langb26ab822018-05-29 11:19:00 -04001485 if (!mLinked)
1486 {
1487 return 0;
1488 }
1489
Geoff Lang7dd2e102014-11-10 15:19:26 -05001490 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001491 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 if (error.isError())
1493 {
1494 return 0;
1495 }
1496
1497 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001498}
1499
Geoff Langc5629752015-12-07 16:29:04 -05001500void Program::setBinaryRetrievableHint(bool retrievable)
1501{
jchen107ae70d82018-07-06 13:47:01 +08001502 resolveLink();
Geoff Langc5629752015-12-07 16:29:04 -05001503 // TODO(jmadill) : replace with dirty bits
1504 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001505 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001506}
1507
1508bool Program::getBinaryRetrievableHint() const
1509{
jchen107ae70d82018-07-06 13:47:01 +08001510 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001511 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001512}
1513
Yunchao He61afff12017-03-14 15:34:03 +08001514void Program::setSeparable(bool separable)
1515{
jchen107ae70d82018-07-06 13:47:01 +08001516 resolveLink();
Yunchao He61afff12017-03-14 15:34:03 +08001517 // TODO(yunchao) : replace with dirty bits
1518 if (mState.mSeparable != separable)
1519 {
1520 mProgram->setSeparable(separable);
1521 mState.mSeparable = separable;
1522 }
1523}
1524
1525bool Program::isSeparable() const
1526{
jchen107ae70d82018-07-06 13:47:01 +08001527 resolveLink();
Yunchao He61afff12017-03-14 15:34:03 +08001528 return mState.mSeparable;
1529}
1530
Jamie Madill6c1f6712017-02-14 19:08:04 -05001531void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001532{
jchen107ae70d82018-07-06 13:47:01 +08001533 resolveLink();
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001534 mRefCount--;
1535
1536 if (mRefCount == 0 && mDeleteStatus)
1537 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001538 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001539 }
1540}
1541
1542void Program::addRef()
1543{
jchen107ae70d82018-07-06 13:47:01 +08001544 resolveLink();
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001545 mRefCount++;
1546}
1547
1548unsigned int Program::getRefCount() const
1549{
1550 return mRefCount;
1551}
1552
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001553int Program::getInfoLogLength() const
1554{
jchen107ae70d82018-07-06 13:47:01 +08001555 resolveLink();
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001556 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001557}
1558
Geoff Lange1a27752015-10-05 13:16:04 -04001559void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001560{
jchen107ae70d82018-07-06 13:47:01 +08001561 resolveLink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001562 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001563}
1564
Geoff Lange1a27752015-10-05 13:16:04 -04001565void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001566{
jchen107ae70d82018-07-06 13:47:01 +08001567 resolveLink();
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001568 int total = 0;
1569
Jiawei Shao016105b2018-04-12 16:38:31 +08001570 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001571 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001572 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001573 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001574 shaders[total] = shader->getHandle();
1575 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001576 }
1577 }
1578
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001579 if (count)
1580 {
1581 *count = total;
1582 }
1583}
1584
Geoff Lange1a27752015-10-05 13:16:04 -04001585GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586{
jchen107ae70d82018-07-06 13:47:01 +08001587 resolveLink();
Jamie Madill34ca4f52017-06-13 11:49:39 -04001588 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589}
1590
Jamie Madill63805b42015-08-25 13:17:39 -04001591bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001592{
jchen107ae70d82018-07-06 13:47:01 +08001593 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001594 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1595 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001596}
1597
jchen10fd7c3b52017-03-21 15:36:03 +08001598void Program::getActiveAttribute(GLuint index,
1599 GLsizei bufsize,
1600 GLsizei *length,
1601 GLint *size,
1602 GLenum *type,
1603 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001604{
jchen107ae70d82018-07-06 13:47:01 +08001605 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001606 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001607 {
1608 if (bufsize > 0)
1609 {
1610 name[0] = '\0';
1611 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001612
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001613 if (length)
1614 {
1615 *length = 0;
1616 }
1617
1618 *type = GL_NONE;
1619 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001620 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001621 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001622
jchen1036e120e2017-03-14 14:53:58 +08001623 ASSERT(index < mState.mAttributes.size());
1624 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001625
1626 if (bufsize > 0)
1627 {
jchen10fd7c3b52017-03-21 15:36:03 +08001628 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001629 }
1630
1631 // Always a single 'type' instance
1632 *size = 1;
1633 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001634}
1635
Geoff Lange1a27752015-10-05 13:16:04 -04001636GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001637{
jchen107ae70d82018-07-06 13:47:01 +08001638 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001639 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001640 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001641 return 0;
1642 }
1643
jchen1036e120e2017-03-14 14:53:58 +08001644 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001645}
1646
Geoff Lange1a27752015-10-05 13:16:04 -04001647GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001648{
jchen107ae70d82018-07-06 13:47:01 +08001649 resolveLink();
Jamie Madillc349ec02015-08-21 16:53:12 -04001650 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001651 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001652 return 0;
1653 }
1654
1655 size_t maxLength = 0;
1656
Jamie Madill48ef11b2016-04-27 15:21:52 -04001657 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001658 {
jchen1036e120e2017-03-14 14:53:58 +08001659 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001660 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661
Jamie Madillc349ec02015-08-21 16:53:12 -04001662 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663}
1664
jchen107ae70d82018-07-06 13:47:01 +08001665const std::vector<sh::Attribute> &Program::getAttributes() const
1666{
1667 resolveLink();
1668 return mState.mAttributes;
1669}
1670
jchen107ae70d82018-07-06 13:47:01 +08001671const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1672{
1673 resolveLink();
1674 return mState.mSamplerBindings;
1675}
1676
jchen107ae70d82018-07-06 13:47:01 +08001677const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1678{
1679 resolveLink();
1680 return mState.mComputeShaderLocalSize;
1681}
1682
1683PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1684{
1685 resolveLink();
1686 return mState.mGeometryShaderInputPrimitiveType;
1687}
1688PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1689{
1690 resolveLink();
1691 return mState.mGeometryShaderOutputPrimitiveType;
1692}
1693GLint Program::getGeometryShaderInvocations() const
1694{
1695 resolveLink();
1696 return mState.mGeometryShaderInvocations;
1697}
1698GLint Program::getGeometryShaderMaxVertices() const
1699{
1700 resolveLink();
1701 return mState.mGeometryShaderMaxVertices;
1702}
1703
jchen1015015f72017-03-16 13:54:21 +08001704GLuint Program::getInputResourceIndex(const GLchar *name) const
1705{
jchen107ae70d82018-07-06 13:47:01 +08001706 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001707 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001708}
1709
1710GLuint Program::getOutputResourceIndex(const GLchar *name) const
1711{
jchen107ae70d82018-07-06 13:47:01 +08001712 resolveLink();
jchen1015015f72017-03-16 13:54:21 +08001713 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1714}
1715
jchen10fd7c3b52017-03-21 15:36:03 +08001716size_t Program::getOutputResourceCount() const
1717{
jchen107ae70d82018-07-06 13:47:01 +08001718 resolveLink();
jchen10fd7c3b52017-03-21 15:36:03 +08001719 return (mLinked ? mState.mOutputVariables.size() : 0);
1720}
1721
jchen107ae70d82018-07-06 13:47:01 +08001722const std::vector<GLenum> &Program::getOutputVariableTypes() const
1723{
1724 resolveLink();
1725 return mState.mOutputVariableTypes;
1726}
1727DrawBufferMask Program::getActiveOutputVariables() const
1728{
1729 resolveLink();
1730 return mState.mActiveOutputVariables;
1731}
1732
jchen10baf5d942017-08-28 20:45:48 +08001733template <typename T>
1734void Program::getResourceName(GLuint index,
1735 const std::vector<T> &resources,
1736 GLsizei bufSize,
1737 GLsizei *length,
1738 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001739{
1740 if (length)
1741 {
1742 *length = 0;
1743 }
1744
1745 if (!mLinked)
1746 {
1747 if (bufSize > 0)
1748 {
1749 name[0] = '\0';
1750 }
1751 return;
1752 }
jchen10baf5d942017-08-28 20:45:48 +08001753 ASSERT(index < resources.size());
1754 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001755
1756 if (bufSize > 0)
1757 {
Olli Etuahod2551232017-10-26 20:03:33 +03001758 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001759 }
1760}
1761
jchen10baf5d942017-08-28 20:45:48 +08001762void Program::getInputResourceName(GLuint index,
1763 GLsizei bufSize,
1764 GLsizei *length,
1765 GLchar *name) const
1766{
jchen107ae70d82018-07-06 13:47:01 +08001767 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001768 getResourceName(index, mState.mAttributes, bufSize, length, name);
1769}
1770
1771void Program::getOutputResourceName(GLuint index,
1772 GLsizei bufSize,
1773 GLsizei *length,
1774 GLchar *name) const
1775{
jchen107ae70d82018-07-06 13:47:01 +08001776 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001777 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1778}
1779
1780void Program::getUniformResourceName(GLuint index,
1781 GLsizei bufSize,
1782 GLsizei *length,
1783 GLchar *name) const
1784{
jchen107ae70d82018-07-06 13:47:01 +08001785 resolveLink();
jchen10baf5d942017-08-28 20:45:48 +08001786 getResourceName(index, mState.mUniforms, bufSize, length, name);
1787}
1788
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001789void Program::getBufferVariableResourceName(GLuint index,
1790 GLsizei bufSize,
1791 GLsizei *length,
1792 GLchar *name) const
1793{
jchen107ae70d82018-07-06 13:47:01 +08001794 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001795 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1796}
1797
jchen10880683b2017-04-12 16:21:55 +08001798const sh::Attribute &Program::getInputResource(GLuint index) const
1799{
jchen107ae70d82018-07-06 13:47:01 +08001800 resolveLink();
jchen10880683b2017-04-12 16:21:55 +08001801 ASSERT(index < mState.mAttributes.size());
1802 return mState.mAttributes[index];
1803}
1804
1805const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1806{
jchen107ae70d82018-07-06 13:47:01 +08001807 resolveLink();
jchen10880683b2017-04-12 16:21:55 +08001808 ASSERT(index < mState.mOutputVariables.size());
1809 return mState.mOutputVariables[index];
1810}
1811
jchen107ae70d82018-07-06 13:47:01 +08001812const ProgramBindings &Program::getAttributeBindings() const
1813{
1814 resolveLink();
1815 return mAttributeBindings;
1816}
1817const ProgramBindings &Program::getUniformLocationBindings() const
1818{
1819 resolveLink();
1820 return mUniformLocationBindings;
1821}
1822const ProgramBindings &Program::getFragmentInputBindings() const
1823{
1824 resolveLink();
1825 return mFragmentInputBindings;
1826}
1827
1828int Program::getNumViews() const
1829{
1830 resolveLink();
1831 return mState.getNumViews();
1832}
jchen107ae70d82018-07-06 13:47:01 +08001833
1834ComponentTypeMask Program::getDrawBufferTypeMask() const
1835{
1836 resolveLink();
1837 return mState.mDrawBufferTypeMask;
1838}
1839ComponentTypeMask Program::getAttributesTypeMask() const
1840{
1841 resolveLink();
1842 return mState.mAttributesTypeMask;
1843}
1844AttributesMask Program::getAttributesMask() const
1845{
1846 resolveLink();
1847 return mState.mAttributesMask;
1848}
1849
1850const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1851{
1852 resolveLink();
1853 return mState.mTransformFeedbackStrides;
1854}
1855
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856GLint Program::getFragDataLocation(const std::string &name) const
1857{
jchen107ae70d82018-07-06 13:47:01 +08001858 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001859 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001860}
1861
Geoff Lange1a27752015-10-05 13:16:04 -04001862void Program::getActiveUniform(GLuint index,
1863 GLsizei bufsize,
1864 GLsizei *length,
1865 GLint *size,
1866 GLenum *type,
1867 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001868{
jchen107ae70d82018-07-06 13:47:01 +08001869 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001871 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001872 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001873 ASSERT(index < mState.mUniforms.size());
1874 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875
1876 if (bufsize > 0)
1877 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001879 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 }
1881
Olli Etuaho465835d2017-09-26 13:34:10 +03001882 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001883 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001884 }
1885 else
1886 {
1887 if (bufsize > 0)
1888 {
1889 name[0] = '\0';
1890 }
1891
1892 if (length)
1893 {
1894 *length = 0;
1895 }
1896
1897 *size = 0;
1898 *type = GL_NONE;
1899 }
1900}
1901
Geoff Lange1a27752015-10-05 13:16:04 -04001902GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001903{
jchen107ae70d82018-07-06 13:47:01 +08001904 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001906 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001907 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001908 }
1909 else
1910 {
1911 return 0;
1912 }
1913}
1914
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001915size_t Program::getActiveBufferVariableCount() const
1916{
jchen107ae70d82018-07-06 13:47:01 +08001917 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001918 return mLinked ? mState.mBufferVariables.size() : 0;
1919}
1920
Geoff Lange1a27752015-10-05 13:16:04 -04001921GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001922{
jchen107ae70d82018-07-06 13:47:01 +08001923 resolveLink();
Jamie Madill62d31cb2015-09-11 13:25:51 -04001924 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001925
1926 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001927 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001928 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001930 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001932 size_t length = uniform.name.length() + 1u;
1933 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 {
1935 length += 3; // Counting in "[0]".
1936 }
1937 maxLength = std::max(length, maxLength);
1938 }
1939 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001940 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001941
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943}
1944
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945bool Program::isValidUniformLocation(GLint location) const
1946{
jchen107ae70d82018-07-06 13:47:01 +08001947 resolveLink();
Jamie Madille2e406c2016-06-02 13:04:10 -04001948 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001949 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001950 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001951}
1952
Jamie Madill62d31cb2015-09-11 13:25:51 -04001953const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954{
jchen107ae70d82018-07-06 13:47:01 +08001955 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001956 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001957 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001958}
1959
Jamie Madillac4e9c32017-01-13 14:07:12 -05001960const VariableLocation &Program::getUniformLocation(GLint location) const
1961{
jchen107ae70d82018-07-06 13:47:01 +08001962 resolveLink();
Jamie Madillac4e9c32017-01-13 14:07:12 -05001963 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1964 return mState.mUniformLocations[location];
1965}
1966
jchen107ae70d82018-07-06 13:47:01 +08001967const std::vector<VariableLocation> &Program::getUniformLocations() const
1968{
1969 resolveLink();
1970 return mState.mUniformLocations;
1971}
1972const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1973{
1974 resolveLink();
1975 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1976 return mState.mUniforms[index];
1977}
1978
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001979const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1980{
jchen107ae70d82018-07-06 13:47:01 +08001981 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001982 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1983 return mState.mBufferVariables[index];
1984}
1985
Jamie Madill62d31cb2015-09-11 13:25:51 -04001986GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987{
jchen107ae70d82018-07-06 13:47:01 +08001988 resolveLink();
Olli Etuahod2551232017-10-26 20:03:33 +03001989 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990}
1991
Jamie Madill62d31cb2015-09-11 13:25:51 -04001992GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993{
jchen107ae70d82018-07-06 13:47:01 +08001994 resolveLink();
Jamie Madille7d84322017-01-10 18:21:59 -05001995 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996}
1997
1998void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1999{
jchen107ae70d82018-07-06 13:47:01 +08002000 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002001 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2002 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002003 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004}
2005
2006void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2007{
jchen107ae70d82018-07-06 13:47:01 +08002008 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002009 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2010 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002011 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012}
2013
2014void Program::setUniform3fv(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, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002019 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020}
2021
2022void Program::setUniform4fv(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, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002027 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028}
2029
Jamie Madill81c2e252017-09-09 23:32:46 -04002030Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031{
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, 1, v);
2035
Jamie Madill81c2e252017-09-09 23:32:46 -04002036 mProgram->setUniform1iv(location, clampedCount, v);
2037
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002038 if (mState.isSamplerUniformIndex(locationInfo.index))
2039 {
2040 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002041 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002042 }
2043
Jamie Madill81c2e252017-09-09 23:32:46 -04002044 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045}
2046
2047void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2048{
jchen107ae70d82018-07-06 13:47:01 +08002049 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002050 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2051 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002052 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053}
2054
2055void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2056{
jchen107ae70d82018-07-06 13:47:01 +08002057 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002058 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2059 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002060 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061}
2062
2063void Program::setUniform4iv(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, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002068 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069}
2070
2071void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *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, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002076 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077}
2078
2079void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *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, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002084 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085}
2086
2087void Program::setUniform3uiv(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, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002092 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093}
2094
2095void Program::setUniform4uiv(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, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002100 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101}
2102
Jamie Madill493f9572018-05-24 19:52:15 -04002103void Program::setUniformMatrix2fv(GLint location,
2104 GLsizei count,
2105 GLboolean transpose,
2106 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107{
jchen107ae70d82018-07-06 13:47:01 +08002108 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002109 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002110 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111}
2112
Jamie Madill493f9572018-05-24 19:52:15 -04002113void Program::setUniformMatrix3fv(GLint location,
2114 GLsizei count,
2115 GLboolean transpose,
2116 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117{
jchen107ae70d82018-07-06 13:47:01 +08002118 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002119 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002120 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121}
2122
Jamie Madill493f9572018-05-24 19:52:15 -04002123void Program::setUniformMatrix4fv(GLint location,
2124 GLsizei count,
2125 GLboolean transpose,
2126 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127{
jchen107ae70d82018-07-06 13:47:01 +08002128 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002129 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002130 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131}
2132
Jamie Madill493f9572018-05-24 19:52:15 -04002133void Program::setUniformMatrix2x3fv(GLint location,
2134 GLsizei count,
2135 GLboolean transpose,
2136 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137{
jchen107ae70d82018-07-06 13:47:01 +08002138 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002139 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002140 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002141}
2142
Jamie Madill493f9572018-05-24 19:52:15 -04002143void Program::setUniformMatrix2x4fv(GLint location,
2144 GLsizei count,
2145 GLboolean transpose,
2146 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147{
jchen107ae70d82018-07-06 13:47:01 +08002148 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002149 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002150 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151}
2152
Jamie Madill493f9572018-05-24 19:52:15 -04002153void Program::setUniformMatrix3x2fv(GLint location,
2154 GLsizei count,
2155 GLboolean transpose,
2156 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157{
jchen107ae70d82018-07-06 13:47:01 +08002158 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002159 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002160 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161}
2162
Jamie Madill493f9572018-05-24 19:52:15 -04002163void Program::setUniformMatrix3x4fv(GLint location,
2164 GLsizei count,
2165 GLboolean transpose,
2166 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167{
jchen107ae70d82018-07-06 13:47:01 +08002168 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002169 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002170 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171}
2172
Jamie Madill493f9572018-05-24 19:52:15 -04002173void Program::setUniformMatrix4x2fv(GLint location,
2174 GLsizei count,
2175 GLboolean transpose,
2176 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177{
jchen107ae70d82018-07-06 13:47:01 +08002178 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002179 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002180 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002181}
2182
Jamie Madill493f9572018-05-24 19:52:15 -04002183void Program::setUniformMatrix4x3fv(GLint location,
2184 GLsizei count,
2185 GLboolean transpose,
2186 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187{
jchen107ae70d82018-07-06 13:47:01 +08002188 resolveLink();
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002189 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002190 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002191}
2192
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002193GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2194{
jchen107ae70d82018-07-06 13:47:01 +08002195 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002196 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2197 const std::vector<GLuint> &boundTextureUnits =
2198 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2199 return boundTextureUnits[uniformLocation.arrayIndex];
2200}
2201
Jamie Madill54164b02017-08-28 15:17:37 -04002202void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203{
jchen107ae70d82018-07-06 13:47:01 +08002204 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002205 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2206 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002207
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002208 if (uniform.isSampler())
2209 {
2210 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2211 return;
2212 }
2213
2214 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002215 if (nativeType == GL_FLOAT)
2216 {
2217 mProgram->getUniformfv(context, location, v);
2218 }
2219 else
2220 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002221 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002222 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002223}
2224
Jamie Madill54164b02017-08-28 15:17:37 -04002225void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002226{
jchen107ae70d82018-07-06 13:47:01 +08002227 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002228 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2229 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002230
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002231 if (uniform.isSampler())
2232 {
2233 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2234 return;
2235 }
2236
2237 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002238 if (nativeType == GL_INT || nativeType == GL_BOOL)
2239 {
2240 mProgram->getUniformiv(context, location, v);
2241 }
2242 else
2243 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002244 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002245 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002246}
2247
Jamie Madill54164b02017-08-28 15:17:37 -04002248void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249{
jchen107ae70d82018-07-06 13:47:01 +08002250 resolveLink();
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002251 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2252 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002253
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002254 if (uniform.isSampler())
2255 {
2256 *v = getSamplerUniformBinding(uniformLocation);
2257 return;
2258 }
2259
2260 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002261 if (nativeType == GL_UNSIGNED_INT)
2262 {
2263 mProgram->getUniformuiv(context, location, v);
2264 }
2265 else
2266 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002267 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002268 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269}
2270
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271void Program::flagForDeletion()
2272{
jchen107ae70d82018-07-06 13:47:01 +08002273 resolveLink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 mDeleteStatus = true;
2275}
2276
2277bool Program::isFlaggedForDeletion() const
2278{
jchen107ae70d82018-07-06 13:47:01 +08002279 resolveLink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 return mDeleteStatus;
2281}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002282
Brandon Jones43a53e22014-08-28 16:23:22 -07002283void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002284{
jchen107ae70d82018-07-06 13:47:01 +08002285 resolveLink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002286 mInfoLog.reset();
2287
Geoff Lang7dd2e102014-11-10 15:19:26 -05002288 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002289 {
Geoff Lang92019432017-11-20 13:09:34 -05002290 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002291 }
2292 else
2293 {
Jamie Madillf6113162015-05-07 11:49:21 -04002294 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002295 }
2296}
2297
Jamie Madillcc73f242018-08-01 11:34:48 -04002298bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002299{
jchen107ae70d82018-07-06 13:47:01 +08002300 resolveLink();
2301
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002302 // if any two active samplers in a program are of different types, but refer to the same
2303 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2304 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002305 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002306 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002307 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002308 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002309 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002310 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002311 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2312 "image unit ("
2313 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002314 }
2315
Jamie Madill7e4eff12018-08-08 15:49:26 -04002316 mCachedValidateSamplersResult = false;
2317 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002318 }
2319 }
2320
2321 mCachedValidateSamplersResult = true;
2322 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323}
2324
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002325bool Program::isValidated() const
2326{
jchen107ae70d82018-07-06 13:47:01 +08002327 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002328 return mValidated;
2329}
2330
jchen1058f67be2017-10-27 08:59:27 +08002331GLuint Program::getActiveAtomicCounterBufferCount() const
2332{
jchen107ae70d82018-07-06 13:47:01 +08002333 resolveLink();
jchen1058f67be2017-10-27 08:59:27 +08002334 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2335}
2336
Jiajia Qin729b2c62017-08-14 09:36:11 +08002337GLuint Program::getActiveShaderStorageBlockCount() const
2338{
jchen107ae70d82018-07-06 13:47:01 +08002339 resolveLink();
Jiajia Qin729b2c62017-08-14 09:36:11 +08002340 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2341}
2342
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002343void Program::getActiveUniformBlockName(const GLuint blockIndex,
2344 GLsizei bufSize,
2345 GLsizei *length,
2346 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002347{
jchen107ae70d82018-07-06 13:47:01 +08002348 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002349 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2350}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002352void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2353 GLsizei bufSize,
2354 GLsizei *length,
2355 GLchar *blockName) const
2356{
jchen107ae70d82018-07-06 13:47:01 +08002357 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002358 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002359}
2360
Qin Jiajia9bf55522018-01-29 13:56:23 +08002361template <typename T>
2362GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002363{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 int maxLength = 0;
2365
2366 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002367 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002368 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002370 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002372 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002373 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374 }
2375 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002376 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002377
2378 return maxLength;
2379}
2380
Qin Jiajia9bf55522018-01-29 13:56:23 +08002381GLint Program::getActiveUniformBlockMaxNameLength() const
2382{
jchen107ae70d82018-07-06 13:47:01 +08002383 resolveLink();
Qin Jiajia9bf55522018-01-29 13:56:23 +08002384 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2385}
2386
2387GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2388{
jchen107ae70d82018-07-06 13:47:01 +08002389 resolveLink();
Qin Jiajia9bf55522018-01-29 13:56:23 +08002390 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2391}
2392
Geoff Lange1a27752015-10-05 13:16:04 -04002393GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002394{
jchen107ae70d82018-07-06 13:47:01 +08002395 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002396 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2397}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002398
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002399GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2400{
jchen107ae70d82018-07-06 13:47:01 +08002401 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002402 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002403}
2404
Jiajia Qin729b2c62017-08-14 09:36:11 +08002405const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002406{
jchen107ae70d82018-07-06 13:47:01 +08002407 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002408 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2409 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002410}
2411
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002412const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2413{
jchen107ae70d82018-07-06 13:47:01 +08002414 resolveLink();
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002415 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2416 return mState.mShaderStorageBlocks[index];
2417}
2418
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002419void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2420{
jchen107ae70d82018-07-06 13:47:01 +08002421 resolveLink();
jchen107a20b972017-06-13 14:25:26 +08002422 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002423 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002424 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002425}
2426
2427GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2428{
jchen107ae70d82018-07-06 13:47:01 +08002429 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002430 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002431}
2432
Jiajia Qin729b2c62017-08-14 09:36:11 +08002433GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2434{
jchen107ae70d82018-07-06 13:47:01 +08002435 resolveLink();
Jiajia Qin729b2c62017-08-14 09:36:11 +08002436 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2437}
2438
Jamie Madill493f9572018-05-24 19:52:15 -04002439void Program::setTransformFeedbackVaryings(GLsizei count,
2440 const GLchar *const *varyings,
2441 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002442{
jchen107ae70d82018-07-06 13:47:01 +08002443 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002444 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002445 for (GLsizei i = 0; i < count; i++)
2446 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002447 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002448 }
2449
Jamie Madill48ef11b2016-04-27 15:21:52 -04002450 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002451}
2452
Jamie Madill493f9572018-05-24 19:52:15 -04002453void Program::getTransformFeedbackVarying(GLuint index,
2454 GLsizei bufSize,
2455 GLsizei *length,
2456 GLsizei *size,
2457 GLenum *type,
2458 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002459{
jchen107ae70d82018-07-06 13:47:01 +08002460 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002461 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002462 {
jchen10a9042d32017-03-17 08:50:45 +08002463 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2464 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2465 std::string varName = var.nameWithArrayIndex();
2466 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002467 if (length)
2468 {
2469 *length = lastNameIdx;
2470 }
2471 if (size)
2472 {
jchen10a9042d32017-03-17 08:50:45 +08002473 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002474 }
2475 if (type)
2476 {
jchen10a9042d32017-03-17 08:50:45 +08002477 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002478 }
2479 if (name)
2480 {
jchen10a9042d32017-03-17 08:50:45 +08002481 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002482 name[lastNameIdx] = '\0';
2483 }
2484 }
2485}
2486
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002487GLsizei Program::getTransformFeedbackVaryingCount() const
2488{
jchen107ae70d82018-07-06 13:47:01 +08002489 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002491 {
jchen10a9042d32017-03-17 08:50:45 +08002492 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002493 }
2494 else
2495 {
2496 return 0;
2497 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002498}
2499
2500GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2501{
jchen107ae70d82018-07-06 13:47:01 +08002502 resolveLink();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002503 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002504 {
2505 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002506 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002507 {
jchen10a9042d32017-03-17 08:50:45 +08002508 maxSize =
2509 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002510 }
2511
2512 return maxSize;
2513 }
2514 else
2515 {
2516 return 0;
2517 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002518}
2519
2520GLenum Program::getTransformFeedbackBufferMode() const
2521{
jchen107ae70d82018-07-06 13:47:01 +08002522 resolveLink();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002523 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002524}
2525
jchen103fd614d2018-08-13 12:21:58 +08002526bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002527{
Jiawei Shao016105b2018-04-12 16:38:31 +08002528 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2529 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2530 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2531 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002532
Jamie Madill493f9572018-05-24 19:52:15 -04002533 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002534 bool isGraphicsShaderAttached =
2535 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002536 // Check whether we both have a compute and non-compute shaders attached.
2537 // If there are of both types attached, then linking should fail.
2538 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2539 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2540 {
2541 infoLog << "Both compute and graphics shaders are attached to the same program.";
2542 return false;
2543 }
2544
2545 if (computeShader)
2546 {
jchen103fd614d2018-08-13 12:21:58 +08002547 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002548 {
2549 infoLog << "Attached compute shader is not compiled.";
2550 return false;
2551 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002552 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002553
jchen103fd614d2018-08-13 12:21:58 +08002554 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002555
2556 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2557 // If the work group size is not specified, a link time error should occur.
2558 if (!mState.mComputeShaderLocalSize.isDeclared())
2559 {
2560 infoLog << "Work group size is not specified.";
2561 return false;
2562 }
2563 }
2564 else
2565 {
jchen103fd614d2018-08-13 12:21:58 +08002566 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002567 {
2568 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2569 return false;
2570 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002571 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002572
jchen103fd614d2018-08-13 12:21:58 +08002573 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002574 {
2575 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2576 return false;
2577 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002578 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002579
jchen103fd614d2018-08-13 12:21:58 +08002580 int vertexShaderVersion = vertexShader->getShaderVersion();
2581 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002582 {
2583 infoLog << "Fragment shader version does not match vertex shader version.";
2584 return false;
2585 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002586
2587 if (geometryShader)
2588 {
2589 // [GL_EXT_geometry_shader] Chapter 7
2590 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2591 // Language Specification, as well as any of the following reasons:
2592 // * One or more of the shader objects attached to <program> are not compiled
2593 // successfully.
2594 // * The shaders do not use the same shader language version.
2595 // * <program> contains objects to form a geometry shader, and
2596 // - <program> is not separable and contains no objects to form a vertex shader; or
2597 // - the input primitive type, output primitive type, or maximum output vertex count
2598 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002599 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002600 {
2601 infoLog << "The attached geometry shader isn't compiled.";
2602 return false;
2603 }
2604
jchen103fd614d2018-08-13 12:21:58 +08002605 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002606 {
2607 mInfoLog << "Geometry shader version does not match vertex shader version.";
2608 return false;
2609 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002610 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002611
Jamie Madill493f9572018-05-24 19:52:15 -04002612 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002613 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002614 if (!inputPrimitive.valid())
2615 {
2616 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2617 return false;
2618 }
2619
Jamie Madill493f9572018-05-24 19:52:15 -04002620 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002621 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002622 if (!outputPrimitive.valid())
2623 {
2624 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2625 return false;
2626 }
2627
jchen103fd614d2018-08-13 12:21:58 +08002628 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002629 if (!maxVertices.valid())
2630 {
2631 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2632 return false;
2633 }
2634
2635 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2636 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2637 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002638 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002639 }
Jiawei Shao73618602017-12-20 15:47:15 +08002640 }
2641
2642 return true;
2643}
2644
jchen10910a3da2017-11-15 09:40:11 +08002645GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2646{
jchen107ae70d82018-07-06 13:47:01 +08002647 resolveLink();
jchen10910a3da2017-11-15 09:40:11 +08002648 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2649 {
2650 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2651 if (tf.nameWithArrayIndex() == name)
2652 {
2653 return tfIndex;
2654 }
2655 }
2656 return GL_INVALID_INDEX;
2657}
2658
2659const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2660{
jchen107ae70d82018-07-06 13:47:01 +08002661 resolveLink();
jchen10910a3da2017-11-15 09:40:11 +08002662 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2663 return mState.mLinkedTransformFeedbackVaryings[index];
2664}
2665
jchen103fd614d2018-08-13 12:21:58 +08002666bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002667{
Jiawei Shao016105b2018-04-12 16:38:31 +08002668 Shader *previousShader = nullptr;
2669 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002670 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002671 Shader *currentShader = mState.mAttachedShaders[shaderType];
2672 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002673 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002674 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002675 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002676
2677 if (previousShader)
2678 {
jchen103fd614d2018-08-13 12:21:58 +08002679 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002680 {
2681 return false;
2682 }
2683 }
2684 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002685 }
2686
jchen103fd614d2018-08-13 12:21:58 +08002687 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002688 {
2689 return false;
2690 }
2691
jchen103fd614d2018-08-13 12:21:58 +08002692 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002693 {
2694 return false;
2695 }
2696
2697 return true;
2698}
2699
2700// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2701// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002702bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002703 gl::Shader *consumingShader,
2704 gl::InfoLog &infoLog) const
2705{
jchen103fd614d2018-08-13 12:21:58 +08002706 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002707
jchen103fd614d2018-08-13 12:21:58 +08002708 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2709 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002710
Jiawei Shao385b3e02018-03-21 09:43:28 +08002711 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002712
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002713 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002714 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002715 bool matched = false;
2716
2717 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002718 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002719 {
2720 continue;
2721 }
2722
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002723 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002724 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002725 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002727 ASSERT(!output.isBuiltIn());
2728
2729 std::string mismatchedStructFieldName;
2730 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002731 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002732 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002733 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002734 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002735 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2736 mismatchedStructFieldName, generatingShader->getType(),
2737 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002738 return false;
2739 }
2740
Geoff Lang7dd2e102014-11-10 15:19:26 -05002741 matched = true;
2742 break;
2743 }
2744 }
2745
Olli Etuaho107c7242018-03-20 15:45:35 +02002746 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2747 // whether the input is statically used - a statically used input should fail this test even
2748 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002749 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002750 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002751 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2752 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2753 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002754 return false;
2755 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002756 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002757
Jiawei Shaod063aff2018-02-22 10:19:09 +08002758 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002759
Jiawei Shaod063aff2018-02-22 10:19:09 +08002760 return true;
2761}
2762
jchen103fd614d2018-08-13 12:21:58 +08002763bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002764{
Jiawei Shao016105b2018-04-12 16:38:31 +08002765 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002766
2767 std::map<GLuint, std::string> staticFragmentInputLocations;
2768
2769 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002770 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002771 for (const sh::Varying &input : fragmentInputVaryings)
2772 {
2773 if (input.isBuiltIn() || !input.staticUse)
2774 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002775 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002776 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002777
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002778 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002779 if (inputBinding == -1)
2780 continue;
2781
2782 const auto it = staticFragmentInputLocations.find(inputBinding);
2783 if (it == std::end(staticFragmentInputLocations))
2784 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002785 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002786 }
2787 else
2788 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002789 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002790 << it->second;
2791 return false;
2792 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002793 }
2794
2795 return true;
2796}
2797
jchen103fd614d2018-08-13 12:21:58 +08002798bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002799 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002800 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002801 GLuint *combinedImageUniformsCount,
2802 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002803{
Olli Etuahob78707c2017-03-09 15:03:11 +00002804 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002805 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002806 {
2807 return false;
2808 }
2809
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002810 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002811
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002812 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002813
jchen10eaef1e52017-06-13 10:44:11 +08002814 if (!linkAtomicCounterBuffers())
2815 {
2816 return false;
2817 }
2818
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002819 return true;
2820}
2821
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002822void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002823{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002824 ASSERT(combinedImageUniforms);
2825
Jamie Madill982f6e02017-06-07 14:33:04 -04002826 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2827 unsigned int low = high;
2828
jchen10eaef1e52017-06-13 10:44:11 +08002829 for (auto counterIter = mState.mUniforms.rbegin();
2830 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2831 {
2832 --low;
2833 }
2834
2835 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2836
2837 high = low;
2838
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002839 for (auto imageIter = mState.mUniforms.rbegin();
2840 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2841 {
2842 --low;
2843 }
2844
2845 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002846 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002847 // If uniform is a image type, insert it into the mImageBindings array.
2848 for (unsigned int imageIndex : mState.mImageUniformRange)
2849 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002850 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2851 // cannot load values into a uniform defined as an image. if declare without a
2852 // binding qualifier, any uniform image variable (include all elements of
2853 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002854 auto &imageUniform = mState.mUniforms[imageIndex];
2855 if (imageUniform.binding == -1)
2856 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002857 mState.mImageBindings.emplace_back(
2858 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002859 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002860 else
2861 {
2862 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002863 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002864 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002865
2866 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2867 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002868 }
2869
2870 high = low;
2871
2872 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002873 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002874 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002875 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002876 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002877
2878 mState.mSamplerUniformRange = RangeUI(low, high);
2879
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002880 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002881 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002882 {
2883 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002884 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002885 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2886 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002887 }
2888}
2889
jchen10eaef1e52017-06-13 10:44:11 +08002890bool Program::linkAtomicCounterBuffers()
2891{
2892 for (unsigned int index : mState.mAtomicCounterUniformRange)
2893 {
Jamie Madill493f9572018-05-24 19:52:15 -04002894 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002895 uniform.blockInfo.offset = uniform.offset;
2896 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2897 uniform.blockInfo.matrixStride = 0;
2898 uniform.blockInfo.isRowMajorMatrix = false;
2899
Jamie Madill493f9572018-05-24 19:52:15 -04002900 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002901 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2902 ++bufferIndex)
2903 {
2904 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2905 if (buffer.binding == uniform.binding)
2906 {
2907 buffer.memberIndexes.push_back(index);
2908 uniform.bufferIndex = bufferIndex;
2909 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002910 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002911 break;
2912 }
2913 }
2914 if (!found)
2915 {
2916 AtomicCounterBuffer atomicCounterBuffer;
2917 atomicCounterBuffer.binding = uniform.binding;
2918 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002919 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002920 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2921 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2922 }
2923 }
2924 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002925 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002926
2927 return true;
2928}
2929
Jamie Madilleb979bf2016-11-15 12:28:46 -05002930// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002931bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002932{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002933 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002934
jchen103fd614d2018-08-13 12:21:58 +08002935 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002936
Geoff Lang7dd2e102014-11-10 15:19:26 -05002937 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002938 if (shaderVersion >= 300)
2939 {
2940 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2941 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002942 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002943 }
2944 else
2945 {
2946 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002947 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002948 }
jchen103fd614d2018-08-13 12:21:58 +08002949 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002950
2951 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002952 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002953 {
Jamie Madillf6113162015-05-07 11:49:21 -04002954 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002955 return false;
2956 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002957
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002958 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002959
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002960 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002961 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002962 {
Olli Etuahod2551232017-10-26 20:03:33 +03002963 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2964 // structures, so we don't need to worry about adjusting their names or generating entries
2965 // for each member/element (unlike uniforms for example).
2966 ASSERT(!attribute.isArray() && !attribute.isStruct());
2967
Jamie Madilleb979bf2016-11-15 12:28:46 -05002968 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002969 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002970 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002971 attribute.location = bindingLocation;
2972 }
2973
2974 if (attribute.location != -1)
2975 {
2976 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002977 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002978
Jamie Madill63805b42015-08-25 13:17:39 -04002979 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002980 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002981 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
2982 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002983
2984 return false;
2985 }
2986
Jamie Madill63805b42015-08-25 13:17:39 -04002987 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002988 {
Jamie Madill63805b42015-08-25 13:17:39 -04002989 const int regLocation = attribute.location + reg;
2990 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002991
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002992 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
2993 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
2994 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04002995 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002996 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002997 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002998 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002999 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003000 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003001 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003002 return false;
3003 }
3004 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003005 else
3006 {
Jamie Madill63805b42015-08-25 13:17:39 -04003007 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003008 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003009
Jamie Madill63805b42015-08-25 13:17:39 -04003010 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003011 }
3012 }
3013 }
3014
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003015 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003016 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003017 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003018 // Not set by glBindAttribLocation or by location layout qualifier
3019 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003020 {
Jamie Madill63805b42015-08-25 13:17:39 -04003021 int regs = VariableRegisterCount(attribute.type);
3022 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003023
Jamie Madill63805b42015-08-25 13:17:39 -04003024 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003025 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003026 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003027 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003028 }
3029
Jamie Madillc349ec02015-08-21 16:53:12 -04003030 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003031 }
3032 }
3033
Brandon Jonesc405ae72017-12-06 14:15:03 -08003034 ASSERT(mState.mAttributesTypeMask.none());
3035 ASSERT(mState.mAttributesMask.none());
3036
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003037 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3038 // shader versions we're only processing active attributes to begin with.
3039 if (shaderVersion >= 300)
3040 {
3041 for (auto attributeIter = mState.mAttributes.begin();
3042 attributeIter != mState.mAttributes.end();)
3043 {
3044 if (attributeIter->active)
3045 {
3046 ++attributeIter;
3047 }
3048 else
3049 {
3050 attributeIter = mState.mAttributes.erase(attributeIter);
3051 }
3052 }
3053 }
3054
Jamie Madill48ef11b2016-04-27 15:21:52 -04003055 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003056 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003057 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003058 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003059 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003060
Jamie Madillbd159f02017-10-09 19:39:06 -04003061 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003062 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003063 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3064 mState.mActiveAttribLocationsMask.set(location);
3065 mState.mMaxActiveAttribLocation =
3066 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003067
3068 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3069 if (!attribute.isBuiltIn())
3070 {
3071 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3072 location);
3073 mState.mAttributesMask.set(location);
3074 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003075 }
3076 }
3077
Geoff Lang7dd2e102014-11-10 15:19:26 -05003078 return true;
3079}
3080
jchen103fd614d2018-08-13 12:21:58 +08003081bool Program::linkInterfaceBlocks(const Caps &caps,
3082 const Version &version,
3083 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003084 InfoLog &infoLog,
3085 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003086{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003087 ASSERT(combinedShaderStorageBlocksCount);
3088
Jiawei Shao54aafe52018-04-27 14:54:57 +08003089 GLuint combinedUniformBlocksCount = 0u;
3090 GLuint numShadersHasUniformBlocks = 0u;
3091 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3092 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003093 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003094 Shader *shader = mState.mAttachedShaders[shaderType];
3095 if (!shader)
3096 {
3097 continue;
3098 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003099
jchen103fd614d2018-08-13 12:21:58 +08003100 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003101 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003102 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003103 if (!ValidateInterfaceBlocksCount(
3104 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3105 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3106 {
3107 return false;
3108 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003109
Jiawei Shao54aafe52018-04-27 14:54:57 +08003110 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3111 ++numShadersHasUniformBlocks;
3112 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003113 }
3114
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003115 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3116 {
3117 infoLog << "The sum of the number of active uniform blocks exceeds "
3118 "MAX_COMBINED_UNIFORM_BLOCKS ("
3119 << caps.maxCombinedUniformBlocks << ").";
3120 return false;
3121 }
3122
Jiawei Shao54aafe52018-04-27 14:54:57 +08003123 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3124 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003125 {
3126 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003127 }
Jamie Madille473dee2015-08-18 14:49:01 -04003128
jchen103fd614d2018-08-13 12:21:58 +08003129 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003130 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003131 *combinedShaderStorageBlocksCount = 0u;
3132 GLuint numShadersHasShaderStorageBlocks = 0u;
3133 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3134 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003135 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003136 Shader *shader = mState.mAttachedShaders[shaderType];
3137 if (!shader)
3138 {
3139 continue;
3140 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003141
jchen103fd614d2018-08-13 12:21:58 +08003142 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003143 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003144 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003145 if (!ValidateInterfaceBlocksCount(
3146 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3147 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3148 {
3149 return false;
3150 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003151
Jiawei Shao54aafe52018-04-27 14:54:57 +08003152 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3153 ++numShadersHasShaderStorageBlocks;
3154 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003155 }
3156
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003157 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003158 {
3159 infoLog << "The sum of the number of active shader storage blocks exceeds "
3160 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3161 << caps.maxCombinedShaderStorageBlocks << ").";
3162 return false;
3163 }
3164
Jiawei Shao54aafe52018-04-27 14:54:57 +08003165 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3166 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003167 {
3168 return false;
3169 }
3170 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003171
Geoff Lang7dd2e102014-11-10 15:19:26 -05003172 return true;
3173}
3174
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003175LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3176 const sh::ShaderVariable &variable2,
3177 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003178 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003179 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003180{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003181 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003182 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003183 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003184 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003185 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003186 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003187 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003188 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003189 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003190 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003191 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003192 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003193 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003194 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003195 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003196 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003197
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003198 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003199 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003200 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003201 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003202 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003203 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3204 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003205 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3206 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003207
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003208 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003209 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003210 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003211 }
3212
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003213 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003214 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003215 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003216 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003217 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3218 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003219 }
3220 }
3221
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003222 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003223}
3224
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003225LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3226 const sh::Varying &inputVarying,
3227 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003228 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003229 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003230{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003231 if (validateGeometryShaderInputVarying)
3232 {
3233 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3234 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3235 // or outputs.
3236 ASSERT(inputVarying.arraySizes.size() == 1u);
3237
3238 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3239 // varying cannot match a geometry shader input varying.
3240 // [GL_EXT_geometry_shader] Section 7.4.1:
3241 // Geometry shader per-vertex input variables and blocks are required to be declared as
3242 // arrays, with each element representing input or output values for a single vertex of a
3243 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3244 // are treated as though they were not declared as arrays.
3245 if (outputVarying.isArray())
3246 {
3247 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3248 }
3249 }
3250
3251 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3252 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003253 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003254 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3255 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003256 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003257 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003258 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003259 }
3260
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003261 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003262 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003263 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003264 }
3265
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003266 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003267 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003268 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003269 }
3270
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003271 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003272}
3273
jchen103fd614d2018-08-13 12:21:58 +08003274bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003275{
Jiawei Shao016105b2018-04-12 16:38:31 +08003276 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3277 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003278 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3279 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3280 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003281
3282 if (shaderVersion != 100)
3283 {
3284 // Only ESSL 1.0 has restrictions on matching input and output invariance
3285 return true;
3286 }
3287
3288 bool glPositionIsInvariant = false;
3289 bool glPointSizeIsInvariant = false;
3290 bool glFragCoordIsInvariant = false;
3291 bool glPointCoordIsInvariant = false;
3292
3293 for (const sh::Varying &varying : vertexVaryings)
3294 {
3295 if (!varying.isBuiltIn())
3296 {
3297 continue;
3298 }
3299 if (varying.name.compare("gl_Position") == 0)
3300 {
3301 glPositionIsInvariant = varying.isInvariant;
3302 }
3303 else if (varying.name.compare("gl_PointSize") == 0)
3304 {
3305 glPointSizeIsInvariant = varying.isInvariant;
3306 }
3307 }
3308
3309 for (const sh::Varying &varying : fragmentVaryings)
3310 {
3311 if (!varying.isBuiltIn())
3312 {
3313 continue;
3314 }
3315 if (varying.name.compare("gl_FragCoord") == 0)
3316 {
3317 glFragCoordIsInvariant = varying.isInvariant;
3318 }
3319 else if (varying.name.compare("gl_PointCoord") == 0)
3320 {
3321 glPointCoordIsInvariant = varying.isInvariant;
3322 }
3323 }
3324
3325 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3326 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3327 // Not requiring invariance to match is supported by:
3328 // dEQP, WebGL CTS, Nexus 5X GLES
3329 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3330 {
3331 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3332 "declared invariant.";
3333 return false;
3334 }
3335 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3336 {
3337 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3338 "declared invariant.";
3339 return false;
3340 }
3341
3342 return true;
3343}
3344
jchen103fd614d2018-08-13 12:21:58 +08003345bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003346 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003347 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003348 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003349{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003350
jchen108225e732017-11-14 16:29:03 +08003351 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003352 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003353 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003354 {
jchen103fd614d2018-08-13 12:21:58 +08003355 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003356 {
Geoff Lang1a683462015-09-29 15:09:59 -04003357 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003358 return false;
3359 }
jchen103fd614d2018-08-13 12:21:58 +08003360 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003361 {
3362 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3363 {
3364 infoLog << "Two transform feedback varyings include the same array element ("
3365 << tfVaryingName << ").";
3366 return false;
3367 }
3368 }
3369 else
3370 {
3371 if (uniqueNames.count(tfVaryingName) > 0)
3372 {
3373 infoLog << "Two transform feedback varyings specify the same output variable ("
3374 << tfVaryingName << ").";
3375 return false;
3376 }
3377 }
3378 uniqueNames.insert(tfVaryingName);
3379 }
3380
3381 // Validate against program varyings.
3382 size_t totalComponents = 0;
3383 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3384 {
3385 std::vector<unsigned int> subscripts;
3386 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3387
3388 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3389 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003390 {
3391 infoLog << "Transform feedback varying " << tfVaryingName
3392 << " does not exist in the vertex shader.";
3393 return false;
3394 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003395
jchen108225e732017-11-14 16:29:03 +08003396 // Validate the matching variable.
3397 if (var->isStruct())
3398 {
3399 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3400 return false;
3401 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003402
jchen108225e732017-11-14 16:29:03 +08003403 size_t elementCount = 0;
3404 size_t componentCount = 0;
3405
3406 if (var->isArray())
3407 {
jchen103fd614d2018-08-13 12:21:58 +08003408 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003409 {
3410 infoLog << "Capture of arrays is undefined and not supported.";
3411 return false;
3412 }
3413
3414 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3415 ASSERT(!var->isArrayOfArrays());
3416
3417 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3418 {
3419 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3420 return false;
3421 }
3422 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3423 }
3424 else
3425 {
3426 if (!subscripts.empty())
3427 {
3428 infoLog << "Varying '" << baseName
3429 << "' is not an array to be captured by element.";
3430 return false;
3431 }
3432 elementCount = 1;
3433 }
3434
3435 // TODO(jmadill): Investigate implementation limits on D3D11
3436 componentCount = VariableComponentCount(var->type) * elementCount;
3437 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3438 componentCount > caps.maxTransformFeedbackSeparateComponents)
3439 {
3440 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3441 << componentCount << ") exceed the maximum separate components ("
3442 << caps.maxTransformFeedbackSeparateComponents << ").";
3443 return false;
3444 }
3445
3446 totalComponents += componentCount;
3447 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3448 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3449 {
3450 infoLog << "Transform feedback varying total components (" << totalComponents
3451 << ") exceed the maximum interleaved components ("
3452 << caps.maxTransformFeedbackInterleavedComponents << ").";
3453 return false;
3454 }
3455 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003456 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003457}
3458
jchen103fd614d2018-08-13 12:21:58 +08003459bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003460{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003461 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003462 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003463
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003464 for (const auto &attrib : attributes)
3465 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003466 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003467 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003468 Shader *shader = mState.mAttachedShaders[shaderType];
3469 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003470 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003471 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003472 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003473
jchen103fd614d2018-08-13 12:21:58 +08003474 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003475 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003476 {
3477 if (uniform.name == attrib.name)
3478 {
3479 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3480 return false;
3481 }
3482 }
3483 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003484 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003485
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003486 return true;
3487}
3488
Jamie Madill3c1da042017-11-27 18:33:40 -05003489void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003490{
3491 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003492 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003493 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003494 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003495 std::vector<unsigned int> subscripts;
3496 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003497 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003498 if (!subscripts.empty())
3499 {
3500 subscript = subscripts.back();
3501 }
Jamie Madill192745a2016-12-22 15:58:21 -05003502 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003503 {
Jamie Madill192745a2016-12-22 15:58:21 -05003504 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003505 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003506 {
jchen10a9042d32017-03-17 08:50:45 +08003507 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3508 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003509 break;
3510 }
jchen108225e732017-11-14 16:29:03 +08003511 else if (varying->isStruct())
3512 {
3513 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3514 if (field != nullptr)
3515 {
3516 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3517 break;
3518 }
3519 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003520 }
3521 }
James Darpinian30b604d2018-03-12 17:26:57 -07003522 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003523}
3524
jchen103fd614d2018-08-13 12:21:58 +08003525ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003526{
Jamie Madill3c1da042017-11-27 18:33:40 -05003527 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003528
Jiawei Shao016105b2018-04-12 16:38:31 +08003529 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003530 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003531 {
Jamie Madill192745a2016-12-22 15:58:21 -05003532 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003533 }
3534
Jiawei Shao016105b2018-04-12 16:38:31 +08003535 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003536 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003537 {
Jamie Madill192745a2016-12-22 15:58:21 -05003538 merged[varying.name].fragment = &varying;
3539 }
3540
3541 return merged;
3542}
3543
jchen103fd614d2018-08-13 12:21:58 +08003544bool Program::linkOutputVariables(const Caps &caps,
3545 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003546 GLuint combinedImageUniformsCount,
3547 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003548{
Jiawei Shao016105b2018-04-12 16:38:31 +08003549 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003550 ASSERT(fragmentShader != nullptr);
3551
Geoff Lange0cff192017-05-30 13:04:56 -04003552 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003553 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003554 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003555
jchen103fd614d2018-08-13 12:21:58 +08003556 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003557 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003558 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003559 {
3560 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3561 outputVariable.name != "gl_FragData")
3562 {
3563 continue;
3564 }
3565
3566 unsigned int baseLocation =
3567 (outputVariable.location == -1 ? 0u
3568 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003569
3570 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3571 // structures, so we may use getBasicTypeElementCount().
3572 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3573 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003574 {
3575 const unsigned int location = baseLocation + elementIndex;
3576 if (location >= mState.mOutputVariableTypes.size())
3577 {
3578 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3579 }
Corentin Walleze7557742017-06-01 13:09:57 -04003580 ASSERT(location < mState.mActiveOutputVariables.size());
3581 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003582 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003583 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003584 }
3585 }
3586
jchen103fd614d2018-08-13 12:21:58 +08003587 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003588 {
3589 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3590 // A link error will be generated if the sum of the number of active image uniforms used in
3591 // all shaders, the number of active shader storage blocks, and the number of active
3592 // fragment shader outputs exceeds the implementation-dependent value of
3593 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3594 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3595 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003596 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003597 {
3598 mInfoLog
3599 << "The sum of the number of active image uniforms, active shader storage blocks "
3600 "and active fragment shader outputs exceeds "
3601 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003602 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003603 return false;
3604 }
3605 }
3606
Jamie Madill80a6fc02015-08-21 16:53:16 -04003607 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003608 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003609 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003610
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003611 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003612 // TODO(jmadill): any caps validation here?
3613
jchen1015015f72017-03-16 13:54:21 +08003614 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003615 outputVariableIndex++)
3616 {
jchen1015015f72017-03-16 13:54:21 +08003617 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003618
Olli Etuahod2551232017-10-26 20:03:33 +03003619 if (outputVariable.isArray())
3620 {
3621 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3622 // Resources and including [0] at the end of array variable names.
3623 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3624 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3625 }
3626
Jamie Madill80a6fc02015-08-21 16:53:16 -04003627 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3628 if (outputVariable.isBuiltIn())
3629 continue;
3630
3631 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003632 unsigned int baseLocation =
3633 (outputVariable.location == -1 ? 0u
3634 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003635
Olli Etuaho465835d2017-09-26 13:34:10 +03003636 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3637 // structures, so we may use getBasicTypeElementCount().
3638 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3639 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003640 {
Olli Etuahod2551232017-10-26 20:03:33 +03003641 const unsigned int location = baseLocation + elementIndex;
3642 if (location >= mState.mOutputLocations.size())
3643 {
3644 mState.mOutputLocations.resize(location + 1);
3645 }
3646 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003647 if (outputVariable.isArray())
3648 {
3649 mState.mOutputLocations[location] =
3650 VariableLocation(elementIndex, outputVariableIndex);
3651 }
3652 else
3653 {
3654 VariableLocation locationInfo;
3655 locationInfo.index = outputVariableIndex;
3656 mState.mOutputLocations[location] = locationInfo;
3657 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003658 }
3659 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003660
3661 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003662}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003663
Olli Etuaho48fed632017-03-16 12:05:30 +00003664void Program::setUniformValuesFromBindingQualifiers()
3665{
Jamie Madill982f6e02017-06-07 14:33:04 -04003666 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003667 {
3668 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3669 if (samplerUniform.binding != -1)
3670 {
Olli Etuahod2551232017-10-26 20:03:33 +03003671 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003672 ASSERT(location != -1);
3673 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003674 for (unsigned int elementIndex = 0;
3675 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003676 {
3677 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3678 }
3679 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3680 boundTextureUnits.data());
3681 }
3682 }
3683}
3684
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003685void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003686{
jchen10af713a22017-04-19 09:10:56 +08003687 // Set initial bindings from shader.
3688 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3689 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003690 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003691 bindUniformBlock(blockIndex, uniformBlock.binding);
3692 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003693}
3694
Jamie Madille7d84322017-01-10 18:21:59 -05003695void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003696 GLsizei clampedCount,
3697 const GLint *v)
3698{
Jamie Madill81c2e252017-09-09 23:32:46 -04003699 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3700 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003701 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3702 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003703
Jamie Madill7e4eff12018-08-08 15:49:26 -04003704 if (samplerBinding.unreferenced)
3705 return;
3706
3707 // Update the sampler uniforms.
3708 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3709 {
3710 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3711 GLint newSamplerIndex = v[arrayIndex];
3712
3713 if (oldSamplerIndex == newSamplerIndex)
3714 continue;
3715
3716 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3717
3718 // Update the reference counts.
3719 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3720 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3721 ASSERT(oldRefCount > 0);
3722 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3723 oldRefCount--;
3724 newRefCount++;
3725
3726 // Check for binding type change.
3727 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3728 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3729
3730 if (newRefCount == 1)
3731 {
3732 newSamplerType = samplerBinding.textureType;
3733 mState.mActiveSamplersMask.set(newSamplerIndex);
3734 }
3735 else if (newSamplerType != samplerBinding.textureType)
3736 {
3737 // Conflict detected. Ensure we reset it properly.
3738 newSamplerType = TextureType::InvalidEnum;
3739 }
3740
3741 // Unset previously active sampler.
3742 if (oldRefCount == 0)
3743 {
3744 oldSamplerType = TextureType::InvalidEnum;
3745 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3746 }
3747 else if (oldSamplerType == TextureType::InvalidEnum)
3748 {
3749 // Previous conflict. Check if this new change fixed the conflict.
3750 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3751 }
3752 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003753
3754 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003755 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003756}
3757
Jamie Madill7e4eff12018-08-08 15:49:26 -04003758TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3759{
3760 TextureType foundType = TextureType::InvalidEnum;
3761
3762 for (const SamplerBinding &binding : mSamplerBindings)
3763 {
3764 if (binding.unreferenced)
3765 continue;
3766
3767 // A conflict exists if samplers of different types are sourced by the same texture unit.
3768 // We need to check all bound textures to detect this error case.
3769 for (GLuint textureUnit : binding.boundTextureUnits)
3770 {
3771 if (textureUnit == textureUnitIndex)
3772 {
3773 if (foundType == TextureType::InvalidEnum)
3774 {
3775 foundType = binding.textureType;
3776 }
3777 else if (foundType != binding.textureType)
3778 {
3779 return TextureType::InvalidEnum;
3780 }
3781 }
3782 }
3783 }
3784
3785 return foundType;
3786}
3787
Jamie Madille7d84322017-01-10 18:21:59 -05003788template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003789GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3790 GLsizei count,
3791 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003792 const T *v)
3793{
Jamie Madill134f93d2017-08-31 17:11:00 -04003794 if (count == 1)
3795 return 1;
3796
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003797 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003798
Corentin Wallez15ac5342016-11-03 17:06:39 -04003799 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3800 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003801 unsigned int remainingElements =
3802 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003803 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003804 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003805
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003806 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003807 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003808 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003809 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003810
3811 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003812}
3813
3814template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003815GLsizei Program::clampMatrixUniformCount(GLint location,
3816 GLsizei count,
3817 GLboolean transpose,
3818 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003819{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003820 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3821
Jamie Madill62d31cb2015-09-11 13:25:51 -04003822 if (!transpose)
3823 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003824 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003825 }
3826
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003827 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003828
3829 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3830 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003831 unsigned int remainingElements =
3832 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003833 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003834}
3835
Jamie Madill54164b02017-08-28 15:17:37 -04003836// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3837// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003838template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003839void Program::getUniformInternal(const Context *context,
3840 DestT *dataOut,
3841 GLint location,
3842 GLenum nativeType,
3843 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003844{
Jamie Madill54164b02017-08-28 15:17:37 -04003845 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003846 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003847 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003848 {
3849 GLint tempValue[16] = {0};
3850 mProgram->getUniformiv(context, location, tempValue);
3851 UniformStateQueryCastLoop<GLboolean>(
3852 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003853 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003854 }
3855 case GL_INT:
3856 {
3857 GLint tempValue[16] = {0};
3858 mProgram->getUniformiv(context, location, tempValue);
3859 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3860 components);
3861 break;
3862 }
3863 case GL_UNSIGNED_INT:
3864 {
3865 GLuint tempValue[16] = {0};
3866 mProgram->getUniformuiv(context, location, tempValue);
3867 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3868 components);
3869 break;
3870 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003871 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003872 {
3873 GLfloat tempValue[16] = {0};
3874 mProgram->getUniformfv(context, location, tempValue);
3875 UniformStateQueryCastLoop<GLfloat>(
3876 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003877 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003878 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003879 default:
3880 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003881 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003882 }
3883}
Jamie Madilla4595b82017-01-11 17:36:34 -05003884
3885bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3886{
jchen107ae70d82018-07-06 13:47:01 +08003887 resolveLink();
Jamie Madilla4595b82017-01-11 17:36:34 -05003888 // Must be called after samplers are validated.
3889 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3890
3891 for (const auto &binding : mState.mSamplerBindings)
3892 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003893 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003894 for (const auto &unit : binding.boundTextureUnits)
3895 {
3896 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3897 if (programTextureID == textureID)
3898 {
3899 // TODO(jmadill): Check for appropriate overlap.
3900 return true;
3901 }
3902 }
3903 }
3904
3905 return false;
3906}
3907
Jamie Madill70aeda42018-08-20 12:17:40 -04003908Error Program::syncState(const Context *context)
3909{
3910 if (mDirtyBits.any())
3911 {
3912 resolveLink();
3913 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
3914 mDirtyBits.reset();
3915 }
3916
3917 return NoError();
3918}
Jamie Madilla2c74982016-12-12 11:20:42 -05003919} // namespace gl