blob: 2422d2b1e839cef9f6491cc9795fef3fdda9c859 [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;
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -0400603 egl::BlobCache::Key programHash;
jchen107ae70d82018-07-06 13:47:01 +0800604 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.
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800773ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0), unreferenced(false)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500774{
775}
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800776ImageBinding::ImageBinding(GLuint imageUnit, size_t count, bool unreferenced)
777 : unreferenced(unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500778{
779 for (size_t index = 0; index < count; ++index)
780 {
781 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
782 }
783}
784
785ImageBinding::ImageBinding(const ImageBinding &other) = default;
786
787ImageBinding::~ImageBinding() = default;
788
Jamie Madill3c1da042017-11-27 18:33:40 -0500789// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400790ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500791 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800792 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500793 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400794 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500795 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800796 mImageUniformRange(0, 0),
797 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300798 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800799 mNumViews(-1),
800 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400801 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
802 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800803 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400804 mGeometryShaderMaxVertices(0),
805 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400806{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300807 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400808 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400809}
810
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400812{
Jiawei Shao016105b2018-04-12 16:38:31 +0800813 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400814}
815
Jamie Madill48ef11b2016-04-27 15:21:52 -0400816const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500817{
818 return mLabel;
819}
820
Jiawei Shao385b3e02018-03-21 09:43:28 +0800821Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
822{
Jiawei Shao016105b2018-04-12 16:38:31 +0800823 ASSERT(shaderType != ShaderType::InvalidEnum);
824 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800825}
826
Jamie Madille7d84322017-01-10 18:21:59 -0500827GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400828{
jchen1015015f72017-03-16 13:54:21 +0800829 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830}
831
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800832GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
833{
834 return GetResourceIndexFromName(mBufferVariables, name);
835}
836
Jamie Madille7d84322017-01-10 18:21:59 -0500837GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
838{
839 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
840 return mUniformLocations[location].index;
841}
842
843Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
844{
845 GLuint index = getUniformIndexFromLocation(location);
846 if (!isSamplerUniformIndex(index))
847 {
848 return Optional<GLuint>::Invalid();
849 }
850
851 return getSamplerIndexFromUniformIndex(index);
852}
853
854bool ProgramState::isSamplerUniformIndex(GLuint index) const
855{
Jamie Madill982f6e02017-06-07 14:33:04 -0400856 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500857}
858
859GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
860{
861 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400862 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500863}
864
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800865bool ProgramState::isImageUniformIndex(GLuint index) const
866{
867 return mImageUniformRange.contains(index);
868}
869
870GLuint ProgramState::getImageIndexFromUniformIndex(GLuint uniformIndex) const
871{
872 ASSERT(isImageUniformIndex(uniformIndex));
873 return uniformIndex - mImageUniformRange.low();
874}
875
Jamie Madill34ca4f52017-06-13 11:49:39 -0400876GLuint ProgramState::getAttributeLocation(const std::string &name) const
877{
878 for (const sh::Attribute &attribute : mAttributes)
879 {
880 if (attribute.name == name)
881 {
882 return attribute.location;
883 }
884 }
885
886 return static_cast<GLuint>(-1);
887}
888
Jiawei Shao016105b2018-04-12 16:38:31 +0800889bool ProgramState::hasAttachedShader() const
890{
891 for (const Shader *shader : mAttachedShaders)
892 {
893 if (shader)
894 {
895 return true;
896 }
897 }
898 return false;
899}
900
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500901Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400902 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400903 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800905 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500906 mDeleteStatus(false),
907 mRefCount(0),
908 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500909 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500910{
911 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000912
Geoff Lang7dd2e102014-11-10 15:19:26 -0500913 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
916Program::~Program()
917{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400918 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
Jamie Madill4928b7c2017-06-20 12:57:39 -0400921void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500922{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400923 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800924 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500925 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800926 if (mState.mAttachedShaders[shaderType])
927 {
928 mState.mAttachedShaders[shaderType]->release(context);
929 mState.mAttachedShaders[shaderType] = nullptr;
930 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800931 }
932
Jamie Madill21061022018-07-12 23:56:30 -0400933 // TODO(jmadill): Handle error in the Context. http://anglebug.com/2491
Jamie Madillb7d924a2018-03-10 11:16:54 -0500934 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400935
Jiawei Shao016105b2018-04-12 16:38:31 +0800936 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400937 SafeDelete(mProgram);
938
939 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500940}
jchen107ae70d82018-07-06 13:47:01 +0800941GLuint Program::id() const
942{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400943 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800944 return mHandle;
945}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500946
Geoff Lang70d0f492015-12-10 17:45:46 -0500947void Program::setLabel(const std::string &label)
948{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400949 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500951}
952
953const std::string &Program::getLabel() const
954{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400955 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400956 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500957}
jchen107ae70d82018-07-06 13:47:01 +0800958rx::ProgramImpl *Program::getImplementation() const
959{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400960 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800961 return mProgram;
962}
Geoff Lang70d0f492015-12-10 17:45:46 -0500963
Jamie Madillef300b12016-10-07 15:12:09 -0400964void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400966 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800967 ShaderType shaderType = shader->getType();
968 ASSERT(shaderType != ShaderType::InvalidEnum);
969
970 mState.mAttachedShaders[shaderType] = shader;
971 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
Jamie Madillc1d770e2017-04-13 17:31:24 -0400974void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400976 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800977 ShaderType shaderType = shader->getType();
978 ASSERT(shaderType != ShaderType::InvalidEnum);
979
980 ASSERT(mState.mAttachedShaders[shaderType] == shader);
981 shader->release(context);
982 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000985int Program::getAttachedShadersCount() const
986{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400987 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800988 int numAttachedShaders = 0;
989 for (const Shader *shader : mState.mAttachedShaders)
990 {
991 if (shader)
992 {
993 ++numAttachedShaders;
994 }
995 }
996
997 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000998}
999
Jiawei Shao385b3e02018-03-21 09:43:28 +08001000const Shader *Program::getAttachedShader(ShaderType shaderType) const
1001{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001002 ASSERT(mLinkResolved);
Jiawei Shao385b3e02018-03-21 09:43:28 +08001003 return mState.getAttachedShader(shaderType);
1004}
1005
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006void Program::bindAttributeLocation(GLuint index, const char *name)
1007{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001008 ASSERT(mLinkResolved);
Geoff Langd8605522016-04-13 10:19:12 -04001009 mAttributeBindings.bindLocation(index, name);
1010}
1011
1012void Program::bindUniformLocation(GLuint index, const char *name)
1013{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001014 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001015 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
Sami Väisänen46eaa942016-06-29 10:26:37 +03001018void Program::bindFragmentInputLocation(GLint index, const char *name)
1019{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001020 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001021 mFragmentInputBindings.bindLocation(index, name);
1022}
1023
Olli Etuaho0ca09752018-09-24 11:00:50 +03001024void Program::bindFragmentOutputLocation(GLuint index, const char *name)
1025{
1026 mFragmentOutputLocations.bindLocation(index, name);
1027}
1028
1029void Program::bindFragmentOutputIndex(GLuint index, const char *name)
1030{
1031 mFragmentOutputIndexes.bindLocation(index, name);
1032}
1033
jchen103fd614d2018-08-13 12:21:58 +08001034BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001035{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001036 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001037 BindingInfo ret;
1038 ret.type = GL_NONE;
1039 ret.valid = false;
1040
Jiawei Shao385b3e02018-03-21 09:43:28 +08001041 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001042 ASSERT(fragmentShader);
1043
1044 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001045 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001046
1047 for (const auto &binding : mFragmentInputBindings)
1048 {
1049 if (binding.second != static_cast<GLuint>(index))
1050 continue;
1051
1052 ret.valid = true;
1053
Olli Etuahod2551232017-10-26 20:03:33 +03001054 size_t nameLengthWithoutArrayIndex;
1055 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001056
1057 for (const auto &in : inputs)
1058 {
Olli Etuahod2551232017-10-26 20:03:33 +03001059 if (in.name.length() == nameLengthWithoutArrayIndex &&
1060 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001061 {
1062 if (in.isArray())
1063 {
1064 // The client wants to bind either "name" or "name[0]".
1065 // GL ES 3.1 spec refers to active array names with language such as:
1066 // "if the string identifies the base name of an active array, where the
1067 // string would exactly match the name of the variable if the suffix "[0]"
1068 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001069 if (arrayIndex == GL_INVALID_INDEX)
1070 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001071
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001072 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001073 }
1074 else
1075 {
1076 ret.name = in.mappedName;
1077 }
1078 ret.type = in.type;
1079 return ret;
1080 }
1081 }
1082 }
1083
1084 return ret;
1085}
1086
jchen103fd614d2018-08-13 12:21:58 +08001087void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001088 GLenum genMode,
1089 GLint components,
1090 const GLfloat *coeffs)
1091{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001092 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001093 // If the location is -1 then the command is silently ignored
1094 if (index == -1)
1095 return;
1096
jchen103fd614d2018-08-13 12:21:58 +08001097 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001098
1099 // If the input doesn't exist then then the command is silently ignored
1100 // This could happen through optimization for example, the shader translator
1101 // decides that a variable is not actually being used and optimizes it away.
1102 if (binding.name.empty())
1103 return;
1104
1105 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1106}
1107
Martin Radev4c4c8e72016-08-04 12:25:34 +03001108// The attached shaders are checked for linking errors by matching up their variables.
1109// Uniform, input and output variables get collected.
1110// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001111Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001112{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001113 const auto &data = context->getContextState();
1114
Jamie Madill6c58b062017-08-01 13:44:25 -04001115 auto *platform = ANGLEPlatformCurrent();
1116 double startTime = platform->currentTime(platform);
1117
Jamie Madill6c1f6712017-02-14 19:08:04 -05001118 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001119 mInfoLog.reset();
1120
1121 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001122 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001123 {
1124 return NoError();
1125 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001126
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -04001127 egl::BlobCache::Key programHash = {0};
Jamie Madill6bc264a2018-03-31 15:36:05 -04001128 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001129
Jamie Madill32447362017-06-28 14:53:52 -04001130 if (cache)
1131 {
Jamie Madill785e8a02018-10-04 17:42:00 -04001132 angle::Result result = cache->getProgram(context, this, &mState, &programHash);
1133 mLinked = (result == angle::Result::Continue());
1134 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001135 }
1136
1137 if (mLinked)
1138 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001139 double delta = platform->currentTime(platform) - startTime;
1140 int us = static_cast<int>(delta * 1000000.0);
1141 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001142 return NoError();
1143 }
1144
1145 // Cache load failed, fall through to normal linking.
1146 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001147
Jamie Madill6bc264a2018-03-31 15:36:05 -04001148 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001149 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001150
jchen107ae70d82018-07-06 13:47:01 +08001151 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001152 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001153 {
jchen107ae70d82018-07-06 13:47:01 +08001154 resources.reset(
1155 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1156 {&mState.mUniformBlocks, &mState.mUniforms},
1157 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1158 {&mState.mAtomicCounterBuffers},
1159 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001160
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001161 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001162 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1163 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001164 {
1165 return NoError();
1166 }
1167
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001168 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001169 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1170 context->getExtensions().webglCompatibility, mInfoLog,
1171 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001172 {
1173 return NoError();
1174 }
1175
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001176 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1177 // A link error will be generated if the sum of the number of active image uniforms used in
1178 // all shaders, the number of active shader storage blocks, and the number of active
1179 // fragment shader outputs exceeds the implementation-dependent value of
1180 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1181 if (combinedImageUniforms + combinedShaderStorageBlocks >
1182 context->getCaps().maxCombinedShaderOutputResources)
1183 {
1184 mInfoLog
1185 << "The sum of the number of active image uniforms, active shader storage blocks "
1186 "and active fragment shader outputs exceeds "
1187 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1188 << context->getCaps().maxCombinedShaderOutputResources << ")";
1189 return NoError();
1190 }
1191
jchen103fd614d2018-08-13 12:21:58 +08001192 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1193 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001194 }
1195 else
1196 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001197 // Map the varyings to the register file
1198 // In WebGL, we use a slightly different handling for packing variables.
1199 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1200 if (data.getLimitations().noFlexibleVaryingPacking)
1201 {
1202 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1203 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1204 }
1205 else if (data.getExtensions().webglCompatibility)
1206 {
1207 packMode = PackMode::WEBGL_STRICT;
1208 }
1209
jchen107ae70d82018-07-06 13:47:01 +08001210 resources.reset(
1211 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1212 {&mState.mUniformBlocks, &mState.mUniforms},
1213 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1214 {&mState.mAtomicCounterBuffers},
1215 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001216
jchen103fd614d2018-08-13 12:21:58 +08001217 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001218 {
1219 return NoError();
1220 }
1221
jchen103fd614d2018-08-13 12:21:58 +08001222 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001223 {
1224 return NoError();
1225 }
1226
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001227 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001228 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1229 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001230 {
1231 return NoError();
1232 }
1233
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001234 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001235 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1236 context->getExtensions().webglCompatibility, mInfoLog,
1237 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001238 {
1239 return NoError();
1240 }
1241
jchen103fd614d2018-08-13 12:21:58 +08001242 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001243 {
1244 return NoError();
1245 }
1246
Olli Etuaho0ca09752018-09-24 11:00:50 +03001247 if (!linkOutputVariables(context->getCaps(), context->getExtensions(),
1248 context->getClientVersion(), combinedImageUniforms,
1249 combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001250 {
1251 return NoError();
1252 }
1253
jchen103fd614d2018-08-13 12:21:58 +08001254 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001255
Jiawei Shao016105b2018-04-12 16:38:31 +08001256 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001257 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001258
jchen103fd614d2018-08-13 12:21:58 +08001259 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1260 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001261
jchen103fd614d2018-08-13 12:21:58 +08001262 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1263 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001264 {
1265 return NoError();
1266 }
1267
jchen107ae70d82018-07-06 13:47:01 +08001268 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001269 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001270 {
1271 return NoError();
1272 }
1273
Martin Radev4c4c8e72016-08-04 12:25:34 +03001274 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001275 }
1276
jchen107ae70d82018-07-06 13:47:01 +08001277 mLinkingState.reset(new LinkingState());
1278 mLinkingState->context = context;
1279 mLinkingState->programHash = programHash;
1280 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1281 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001282 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001283
1284 return NoError();
1285}
1286
1287bool Program::isLinking() const
1288{
1289 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1290}
1291
Jamie Madill785e8a02018-10-04 17:42:00 -04001292void Program::resolveLinkImpl(const Context *context)
jchen107ae70d82018-07-06 13:47:01 +08001293{
1294 ASSERT(mLinkingState.get());
1295
Jamie Madill785e8a02018-10-04 17:42:00 -04001296 angle::Result result = mLinkingState->linkEvent->wait(context);
1297
1298 mLinked = result == angle::Result::Continue();
jchen105055fba2018-08-17 09:57:07 +08001299 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001300 auto linkingState = std::move(mLinkingState);
1301 if (!mLinked)
1302 {
1303 return;
1304 }
1305
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001306 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001307
Yunchao Heece12532017-11-21 15:50:21 +08001308 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1309 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001310 ASSERT(mLinked);
1311 updateLinkedShaderStages();
1312
Jamie Madill54164b02017-08-28 15:17:37 -04001313 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001314 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1315 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001316
Jamie Madill7e4eff12018-08-08 15:49:26 -04001317 // Must be called after markUnusedUniformLocations.
1318 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001319 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001320
1321 setUniformValuesFromBindingQualifiers();
1322
Jamie Madill32447362017-06-28 14:53:52 -04001323 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001324 auto *cache = linkingState->context->getMemoryProgramCache();
1325 if (cache &&
1326 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1327 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001328 {
jchen107ae70d82018-07-06 13:47:01 +08001329 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001330 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001331}
1332
Yunchao He85072e82017-11-14 15:43:28 +08001333void Program::updateLinkedShaderStages()
1334{
Yunchao Heece12532017-11-21 15:50:21 +08001335 mState.mLinkedShaderStages.reset();
1336
Jiawei Shao016105b2018-04-12 16:38:31 +08001337 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001338 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001339 if (shader)
1340 {
1341 mState.mLinkedShaderStages.set(shader->getType());
1342 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001343 }
Yunchao He85072e82017-11-14 15:43:28 +08001344}
1345
James Darpinian30b604d2018-03-12 17:26:57 -07001346void ProgramState::updateTransformFeedbackStrides()
1347{
1348 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1349 {
1350 mTransformFeedbackStrides.resize(1);
1351 size_t totalSize = 0;
1352 for (auto &varying : mLinkedTransformFeedbackVaryings)
1353 {
1354 totalSize += varying.size() * VariableExternalSize(varying.type);
1355 }
1356 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1357 }
1358 else
1359 {
1360 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1361 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1362 {
1363 auto &varying = mLinkedTransformFeedbackVaryings[i];
1364 mTransformFeedbackStrides[i] =
1365 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1366 }
1367 }
1368}
1369
Jamie Madill7e4eff12018-08-08 15:49:26 -04001370void ProgramState::updateActiveSamplers()
1371{
1372 for (SamplerBinding &samplerBinding : mSamplerBindings)
1373 {
1374 if (samplerBinding.unreferenced)
1375 continue;
1376
1377 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1378 {
1379 mActiveSamplerRefCounts[textureUnit]++;
1380 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1381 mActiveSamplersMask.set(textureUnit);
1382 }
1383 }
1384}
1385
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001386void ProgramState::updateActiveImages()
1387{
1388 for (ImageBinding &imageBinding : mImageBindings)
1389 {
1390 if (imageBinding.unreferenced)
1391 continue;
1392
1393 for (GLint imageUnit : imageBinding.boundImageUnits)
1394 {
1395 mActiveImagesMask.set(imageUnit);
1396 }
1397 }
1398}
1399
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001400// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001401void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001403 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001404 mState.mAttributesTypeMask.reset();
1405 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001406 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001407 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001408 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001409 mState.mUniforms.clear();
1410 mState.mUniformLocations.clear();
1411 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001412 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001413 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001414 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001415 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001416 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001417 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001418 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001419 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001420 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001421 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001422 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001423 mState.mNumViews = -1;
1424 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1425 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001426 mState.mGeometryShaderInvocations = 1;
1427 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429 mValidated = false;
1430
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001431 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001432 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001433}
1434
Jamie Madilla2c74982016-12-12 11:20:42 -05001435Error Program::loadBinary(const Context *context,
1436 GLenum binaryFormat,
1437 const void *binary,
1438 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001439{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001440 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001441 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001442
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001444 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001446 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1447 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001448 {
Jamie Madillf6113162015-05-07 11:49:21 -04001449 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001450 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451 }
1452
Jamie Madill4f86d052017-06-05 12:59:26 -04001453 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
Jamie Madill785e8a02018-10-04 17:42:00 -04001454 angle::Result result =
1455 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog);
1456 mLinked = result == angle::Result::Continue();
1457 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001458
1459 // Currently we require the full shader text to compute the program hash.
Jamie Madill785e8a02018-10-04 17:42:00 -04001460 // We could also store the binary in the internal program cache.
Jamie Madill32447362017-06-28 14:53:52 -04001461
Jamie Madill70aeda42018-08-20 12:17:40 -04001462 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1463 ++uniformBlockIndex)
1464 {
1465 mDirtyBits.set(uniformBlockIndex);
1466 }
1467
Jamie Madillb0a838b2016-11-13 20:02:12 -05001468 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001469#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470}
1471
Jamie Madilla2c74982016-12-12 11:20:42 -05001472Error Program::saveBinary(const Context *context,
1473 GLenum *binaryFormat,
1474 void *binary,
1475 GLsizei bufSize,
1476 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001478 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479 if (binaryFormat)
1480 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001481 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001482 }
1483
Jamie Madill4f86d052017-06-05 12:59:26 -04001484 angle::MemoryBuffer memoryBuf;
1485 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486
Jamie Madill4f86d052017-06-05 12:59:26 -04001487 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1488 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489
1490 if (streamLength > bufSize)
1491 {
1492 if (length)
1493 {
1494 *length = 0;
1495 }
1496
Jamie Madill493f9572018-05-24 19:52:15 -04001497 // TODO: This should be moved to the validation layer but computing the size of the binary
1498 // before saving it causes the save to happen twice. It may be possible to write the binary
1499 // to a separate buffer, validate sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001500 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501 }
1502
1503 if (binary)
1504 {
Jamie Madill493f9572018-05-24 19:52:15 -04001505 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001506
Jamie Madill48ef11b2016-04-27 15:21:52 -04001507 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001508 ptr += streamLength;
1509
1510 ASSERT(ptr - streamLength == binary);
1511 }
1512
1513 if (length)
1514 {
1515 *length = streamLength;
1516 }
1517
He Yunchaoacd18982017-01-04 10:46:42 +08001518 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001519}
1520
Jamie Madillffe00c02017-06-27 16:26:55 -04001521GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001523 ASSERT(mLinkResolved);
Geoff Langb26ab822018-05-29 11:19:00 -04001524 if (!mLinked)
1525 {
1526 return 0;
1527 }
1528
Geoff Lang7dd2e102014-11-10 15:19:26 -05001529 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001530 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001531 if (error.isError())
1532 {
1533 return 0;
1534 }
1535
1536 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001537}
1538
Geoff Langc5629752015-12-07 16:29:04 -05001539void Program::setBinaryRetrievableHint(bool retrievable)
1540{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001541 ASSERT(mLinkResolved);
Geoff Langc5629752015-12-07 16:29:04 -05001542 // TODO(jmadill) : replace with dirty bits
1543 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001544 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001545}
1546
1547bool Program::getBinaryRetrievableHint() const
1548{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001549 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001550 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001551}
1552
Yunchao He61afff12017-03-14 15:34:03 +08001553void Program::setSeparable(bool separable)
1554{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001555 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001556 // TODO(yunchao) : replace with dirty bits
1557 if (mState.mSeparable != separable)
1558 {
1559 mProgram->setSeparable(separable);
1560 mState.mSeparable = separable;
1561 }
1562}
1563
1564bool Program::isSeparable() const
1565{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001566 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001567 return mState.mSeparable;
1568}
1569
Jamie Madill6c1f6712017-02-14 19:08:04 -05001570void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001571{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001572 ASSERT(mLinkResolved);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001573 mRefCount--;
1574
1575 if (mRefCount == 0 && mDeleteStatus)
1576 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001577 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001578 }
1579}
1580
1581void Program::addRef()
1582{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001583 ASSERT(mLinkResolved);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001584 mRefCount++;
1585}
1586
1587unsigned int Program::getRefCount() const
1588{
1589 return mRefCount;
1590}
1591
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001592int Program::getInfoLogLength() const
1593{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001594 ASSERT(mLinkResolved);
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001595 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001596}
1597
Geoff Lange1a27752015-10-05 13:16:04 -04001598void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001599{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001600 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001601 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001602}
1603
Geoff Lange1a27752015-10-05 13:16:04 -04001604void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001605{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001606 ASSERT(mLinkResolved);
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001607 int total = 0;
1608
Jiawei Shao016105b2018-04-12 16:38:31 +08001609 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001610 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001611 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001612 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001613 shaders[total] = shader->getHandle();
1614 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001615 }
1616 }
1617
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001618 if (count)
1619 {
1620 *count = total;
1621 }
1622}
1623
Geoff Lange1a27752015-10-05 13:16:04 -04001624GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001626 ASSERT(mLinkResolved);
Jamie Madill34ca4f52017-06-13 11:49:39 -04001627 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628}
1629
Jamie Madill63805b42015-08-25 13:17:39 -04001630bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001631{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001632 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001633 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1634 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635}
1636
jchen10fd7c3b52017-03-21 15:36:03 +08001637void Program::getActiveAttribute(GLuint index,
1638 GLsizei bufsize,
1639 GLsizei *length,
1640 GLint *size,
1641 GLenum *type,
1642 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001643{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001644 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001645 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001646 {
1647 if (bufsize > 0)
1648 {
1649 name[0] = '\0';
1650 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001652 if (length)
1653 {
1654 *length = 0;
1655 }
1656
1657 *type = GL_NONE;
1658 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001659 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001660 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001661
jchen1036e120e2017-03-14 14:53:58 +08001662 ASSERT(index < mState.mAttributes.size());
1663 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001664
1665 if (bufsize > 0)
1666 {
jchen10fd7c3b52017-03-21 15:36:03 +08001667 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001668 }
1669
1670 // Always a single 'type' instance
1671 *size = 1;
1672 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001673}
1674
Geoff Lange1a27752015-10-05 13:16:04 -04001675GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001676{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001677 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001678 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001679 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001680 return 0;
1681 }
1682
jchen1036e120e2017-03-14 14:53:58 +08001683 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001684}
1685
Geoff Lange1a27752015-10-05 13:16:04 -04001686GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001687{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001688 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001689 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001690 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001691 return 0;
1692 }
1693
1694 size_t maxLength = 0;
1695
Jamie Madill48ef11b2016-04-27 15:21:52 -04001696 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001697 {
jchen1036e120e2017-03-14 14:53:58 +08001698 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700
Jamie Madillc349ec02015-08-21 16:53:12 -04001701 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702}
1703
jchen107ae70d82018-07-06 13:47:01 +08001704const std::vector<sh::Attribute> &Program::getAttributes() const
1705{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001706 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001707 return mState.mAttributes;
1708}
1709
jchen107ae70d82018-07-06 13:47:01 +08001710const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1711{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001712 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001713 return mState.mSamplerBindings;
1714}
1715
jchen107ae70d82018-07-06 13:47:01 +08001716const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1717{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001718 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001719 return mState.mComputeShaderLocalSize;
1720}
1721
1722PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1723{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001724 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001725 return mState.mGeometryShaderInputPrimitiveType;
1726}
1727PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1728{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001729 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001730 return mState.mGeometryShaderOutputPrimitiveType;
1731}
1732GLint Program::getGeometryShaderInvocations() const
1733{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001734 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001735 return mState.mGeometryShaderInvocations;
1736}
1737GLint Program::getGeometryShaderMaxVertices() const
1738{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001739 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001740 return mState.mGeometryShaderMaxVertices;
1741}
1742
jchen1015015f72017-03-16 13:54:21 +08001743GLuint Program::getInputResourceIndex(const GLchar *name) const
1744{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001745 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001746 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001747}
1748
1749GLuint Program::getOutputResourceIndex(const GLchar *name) const
1750{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001751 ASSERT(mLinkResolved);
jchen1015015f72017-03-16 13:54:21 +08001752 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1753}
1754
jchen10fd7c3b52017-03-21 15:36:03 +08001755size_t Program::getOutputResourceCount() const
1756{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001757 ASSERT(mLinkResolved);
jchen10fd7c3b52017-03-21 15:36:03 +08001758 return (mLinked ? mState.mOutputVariables.size() : 0);
1759}
1760
jchen107ae70d82018-07-06 13:47:01 +08001761const std::vector<GLenum> &Program::getOutputVariableTypes() const
1762{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001763 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001764 return mState.mOutputVariableTypes;
1765}
1766DrawBufferMask Program::getActiveOutputVariables() const
1767{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001768 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001769 return mState.mActiveOutputVariables;
1770}
1771
jchen10baf5d942017-08-28 20:45:48 +08001772template <typename T>
1773void Program::getResourceName(GLuint index,
1774 const std::vector<T> &resources,
1775 GLsizei bufSize,
1776 GLsizei *length,
1777 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001778{
1779 if (length)
1780 {
1781 *length = 0;
1782 }
1783
1784 if (!mLinked)
1785 {
1786 if (bufSize > 0)
1787 {
1788 name[0] = '\0';
1789 }
1790 return;
1791 }
jchen10baf5d942017-08-28 20:45:48 +08001792 ASSERT(index < resources.size());
1793 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001794
1795 if (bufSize > 0)
1796 {
Olli Etuahod2551232017-10-26 20:03:33 +03001797 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001798 }
1799}
1800
jchen10baf5d942017-08-28 20:45:48 +08001801void Program::getInputResourceName(GLuint index,
1802 GLsizei bufSize,
1803 GLsizei *length,
1804 GLchar *name) const
1805{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001806 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001807 getResourceName(index, mState.mAttributes, bufSize, length, name);
1808}
1809
1810void Program::getOutputResourceName(GLuint index,
1811 GLsizei bufSize,
1812 GLsizei *length,
1813 GLchar *name) const
1814{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001815 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001816 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1817}
1818
1819void Program::getUniformResourceName(GLuint index,
1820 GLsizei bufSize,
1821 GLsizei *length,
1822 GLchar *name) const
1823{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001824 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001825 getResourceName(index, mState.mUniforms, bufSize, length, name);
1826}
1827
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001828void Program::getBufferVariableResourceName(GLuint index,
1829 GLsizei bufSize,
1830 GLsizei *length,
1831 GLchar *name) const
1832{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001833 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001834 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1835}
1836
jchen10880683b2017-04-12 16:21:55 +08001837const sh::Attribute &Program::getInputResource(GLuint index) const
1838{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001839 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001840 ASSERT(index < mState.mAttributes.size());
1841 return mState.mAttributes[index];
1842}
1843
1844const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1845{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001846 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001847 ASSERT(index < mState.mOutputVariables.size());
1848 return mState.mOutputVariables[index];
1849}
1850
jchen107ae70d82018-07-06 13:47:01 +08001851const ProgramBindings &Program::getAttributeBindings() const
1852{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001853 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001854 return mAttributeBindings;
1855}
1856const ProgramBindings &Program::getUniformLocationBindings() const
1857{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001858 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001859 return mUniformLocationBindings;
1860}
1861const ProgramBindings &Program::getFragmentInputBindings() const
1862{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001863 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001864 return mFragmentInputBindings;
1865}
1866
1867int Program::getNumViews() const
1868{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001869 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001870 return mState.getNumViews();
1871}
jchen107ae70d82018-07-06 13:47:01 +08001872
1873ComponentTypeMask Program::getDrawBufferTypeMask() const
1874{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001875 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001876 return mState.mDrawBufferTypeMask;
1877}
1878ComponentTypeMask Program::getAttributesTypeMask() const
1879{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001880 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001881 return mState.mAttributesTypeMask;
1882}
1883AttributesMask Program::getAttributesMask() const
1884{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001885 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001886 return mState.mAttributesMask;
1887}
1888
1889const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1890{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001891 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001892 return mState.mTransformFeedbackStrides;
1893}
1894
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895GLint Program::getFragDataLocation(const std::string &name) const
1896{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001897 ASSERT(mLinkResolved);
Olli Etuaho0ca09752018-09-24 11:00:50 +03001898 GLint primaryLocation =
1899 GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
1900 if (primaryLocation != -1)
1901 {
1902 return primaryLocation;
1903 }
1904 return GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name);
1905}
1906
1907GLint Program::getFragDataIndex(const std::string &name) const
1908{
1909 ASSERT(mLinkResolved);
1910 if (GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name) != -1)
1911 {
1912 return 0;
1913 }
1914 if (GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name) != -1)
1915 {
1916 return 1;
1917 }
1918 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001919}
1920
Geoff Lange1a27752015-10-05 13:16:04 -04001921void Program::getActiveUniform(GLuint index,
1922 GLsizei bufsize,
1923 GLsizei *length,
1924 GLint *size,
1925 GLenum *type,
1926 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001927{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001928 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001930 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001932 ASSERT(index < mState.mUniforms.size());
1933 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934
1935 if (bufsize > 0)
1936 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001938 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001939 }
1940
Olli Etuaho465835d2017-09-26 13:34:10 +03001941 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001943 }
1944 else
1945 {
1946 if (bufsize > 0)
1947 {
1948 name[0] = '\0';
1949 }
1950
1951 if (length)
1952 {
1953 *length = 0;
1954 }
1955
1956 *size = 0;
1957 *type = GL_NONE;
1958 }
1959}
1960
Geoff Lange1a27752015-10-05 13:16:04 -04001961GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001962{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001963 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001965 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001966 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001967 }
1968 else
1969 {
1970 return 0;
1971 }
1972}
1973
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001974size_t Program::getActiveBufferVariableCount() const
1975{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001976 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001977 return mLinked ? mState.mBufferVariables.size() : 0;
1978}
1979
Geoff Lange1a27752015-10-05 13:16:04 -04001980GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001981{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001982 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001983 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984
1985 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001986 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001987 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001989 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991 size_t length = uniform.name.length() + 1u;
1992 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993 {
1994 length += 3; // Counting in "[0]".
1995 }
1996 maxLength = std::max(length, maxLength);
1997 }
1998 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001999 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000
Jamie Madill62d31cb2015-09-11 13:25:51 -04002001 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002}
2003
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004bool Program::isValidUniformLocation(GLint location) const
2005{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002006 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04002007 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04002008 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04002009 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04002010}
2011
Jamie Madill62d31cb2015-09-11 13:25:51 -04002012const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002013{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002014 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002015 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05002016 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002017}
2018
Jamie Madillac4e9c32017-01-13 14:07:12 -05002019const VariableLocation &Program::getUniformLocation(GLint location) const
2020{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002021 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05002022 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
2023 return mState.mUniformLocations[location];
2024}
2025
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002026const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
2027{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002028 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002029 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
2030 return mState.mBufferVariables[index];
2031}
2032
Jamie Madill62d31cb2015-09-11 13:25:51 -04002033GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002035 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002036 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037}
2038
Jamie Madill62d31cb2015-09-11 13:25:51 -04002039GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002041 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002042 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043}
2044
2045void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2046{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002047 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002048 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2049 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002050 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051}
2052
2053void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2054{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002055 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002056 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2057 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002058 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059}
2060
2061void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2062{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002063 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002064 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2065 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002066 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067}
2068
2069void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2070{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002071 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002072 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2073 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002074 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075}
2076
Jamie Madill81c2e252017-09-09 23:32:46 -04002077Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002079 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002080 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2081 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2082
Jamie Madill81c2e252017-09-09 23:32:46 -04002083 mProgram->setUniform1iv(location, clampedCount, v);
2084
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002085 if (mState.isSamplerUniformIndex(locationInfo.index))
2086 {
2087 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002088 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002089 }
2090
Jamie Madill81c2e252017-09-09 23:32:46 -04002091 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092}
2093
2094void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2095{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002096 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002097 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2098 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002099 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100}
2101
2102void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2103{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002104 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002105 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2106 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002107 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108}
2109
2110void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2111{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002112 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002113 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2114 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002115 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116}
2117
2118void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2119{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002120 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002121 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2122 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002123 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124}
2125
2126void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2127{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002128 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002129 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2130 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002131 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132}
2133
2134void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2135{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002136 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002137 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2138 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002139 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140}
2141
2142void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2143{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002144 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002145 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2146 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002147 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148}
2149
Jamie Madill493f9572018-05-24 19:52:15 -04002150void Program::setUniformMatrix2fv(GLint location,
2151 GLsizei count,
2152 GLboolean transpose,
2153 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002155 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002156 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002157 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158}
2159
Jamie Madill493f9572018-05-24 19:52:15 -04002160void Program::setUniformMatrix3fv(GLint location,
2161 GLsizei count,
2162 GLboolean transpose,
2163 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002165 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002166 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002167 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168}
2169
Jamie Madill493f9572018-05-24 19:52:15 -04002170void Program::setUniformMatrix4fv(GLint location,
2171 GLsizei count,
2172 GLboolean transpose,
2173 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002175 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002176 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002177 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178}
2179
Jamie Madill493f9572018-05-24 19:52:15 -04002180void Program::setUniformMatrix2x3fv(GLint location,
2181 GLsizei count,
2182 GLboolean transpose,
2183 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002184{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002185 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002186 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002187 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188}
2189
Jamie Madill493f9572018-05-24 19:52:15 -04002190void Program::setUniformMatrix2x4fv(GLint location,
2191 GLsizei count,
2192 GLboolean transpose,
2193 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002194{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002195 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002196 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002197 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198}
2199
Jamie Madill493f9572018-05-24 19:52:15 -04002200void Program::setUniformMatrix3x2fv(GLint location,
2201 GLsizei count,
2202 GLboolean transpose,
2203 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002204{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002205 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002206 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002207 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208}
2209
Jamie Madill493f9572018-05-24 19:52:15 -04002210void Program::setUniformMatrix3x4fv(GLint location,
2211 GLsizei count,
2212 GLboolean transpose,
2213 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002215 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002216 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002217 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218}
2219
Jamie Madill493f9572018-05-24 19:52:15 -04002220void Program::setUniformMatrix4x2fv(GLint location,
2221 GLsizei count,
2222 GLboolean transpose,
2223 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002225 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002226 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002227 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228}
2229
Jamie Madill493f9572018-05-24 19:52:15 -04002230void Program::setUniformMatrix4x3fv(GLint location,
2231 GLsizei count,
2232 GLboolean transpose,
2233 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002235 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002236 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002237 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002238}
2239
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002240GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2241{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002242 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002243 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2244 const std::vector<GLuint> &boundTextureUnits =
2245 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2246 return boundTextureUnits[uniformLocation.arrayIndex];
2247}
2248
Jamie Madill54164b02017-08-28 15:17:37 -04002249void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002250{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002251 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002252 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2253 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002254
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002255 if (uniform.isSampler())
2256 {
2257 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2258 return;
2259 }
2260
2261 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002262 if (nativeType == GL_FLOAT)
2263 {
2264 mProgram->getUniformfv(context, location, v);
2265 }
2266 else
2267 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002268 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002269 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002270}
2271
Jamie Madill54164b02017-08-28 15:17:37 -04002272void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002273{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002274 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002275 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2276 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002277
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002278 if (uniform.isSampler())
2279 {
2280 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2281 return;
2282 }
2283
2284 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002285 if (nativeType == GL_INT || nativeType == GL_BOOL)
2286 {
2287 mProgram->getUniformiv(context, location, v);
2288 }
2289 else
2290 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002291 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002292 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293}
2294
Jamie Madill54164b02017-08-28 15:17:37 -04002295void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002297 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002298 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2299 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002300
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002301 if (uniform.isSampler())
2302 {
2303 *v = getSamplerUniformBinding(uniformLocation);
2304 return;
2305 }
2306
2307 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002308 if (nativeType == GL_UNSIGNED_INT)
2309 {
2310 mProgram->getUniformuiv(context, location, v);
2311 }
2312 else
2313 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002314 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002315 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002316}
2317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318void Program::flagForDeletion()
2319{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002320 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 mDeleteStatus = true;
2322}
2323
2324bool Program::isFlaggedForDeletion() const
2325{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002326 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 return mDeleteStatus;
2328}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002329
Brandon Jones43a53e22014-08-28 16:23:22 -07002330void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002331{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002332 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002333 mInfoLog.reset();
2334
Geoff Lang7dd2e102014-11-10 15:19:26 -05002335 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002336 {
Geoff Lang92019432017-11-20 13:09:34 -05002337 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002338 }
2339 else
2340 {
Jamie Madillf6113162015-05-07 11:49:21 -04002341 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002342 }
2343}
2344
Jamie Madillcc73f242018-08-01 11:34:48 -04002345bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002346{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002347 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002348
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002349 // if any two active samplers in a program are of different types, but refer to the same
2350 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2351 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002352 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002353 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002354 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002355 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002356 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002357 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002358 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2359 "image unit ("
2360 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002361 }
2362
Jamie Madill7e4eff12018-08-08 15:49:26 -04002363 mCachedValidateSamplersResult = false;
2364 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002365 }
2366 }
2367
2368 mCachedValidateSamplersResult = true;
2369 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370}
2371
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002372bool Program::isValidated() const
2373{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002374 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 return mValidated;
2376}
2377
jchen1058f67be2017-10-27 08:59:27 +08002378GLuint Program::getActiveAtomicCounterBufferCount() const
2379{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002380 ASSERT(mLinkResolved);
jchen1058f67be2017-10-27 08:59:27 +08002381 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2382}
2383
Jiajia Qin729b2c62017-08-14 09:36:11 +08002384GLuint Program::getActiveShaderStorageBlockCount() const
2385{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002386 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002387 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2388}
2389
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002390void Program::getActiveUniformBlockName(const GLuint blockIndex,
2391 GLsizei bufSize,
2392 GLsizei *length,
2393 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002394{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002395 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002396 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2397}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002399void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2400 GLsizei bufSize,
2401 GLsizei *length,
2402 GLchar *blockName) const
2403{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002404 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002405 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002406}
2407
Qin Jiajia9bf55522018-01-29 13:56:23 +08002408template <typename T>
2409GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002410{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 int maxLength = 0;
2412
2413 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002414 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002415 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002416 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002417 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002418 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002419 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002420 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421 }
2422 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002423 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002424
2425 return maxLength;
2426}
2427
Qin Jiajia9bf55522018-01-29 13:56:23 +08002428GLint Program::getActiveUniformBlockMaxNameLength() const
2429{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002430 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002431 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2432}
2433
2434GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2435{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002436 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002437 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2438}
2439
Geoff Lange1a27752015-10-05 13:16:04 -04002440GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002442 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002443 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2444}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002445
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002446GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2447{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002448 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002449 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002450}
2451
Jiajia Qin729b2c62017-08-14 09:36:11 +08002452const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002453{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002454 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002455 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2456 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002457}
2458
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002459const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2460{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002461 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002462 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2463 return mState.mShaderStorageBlocks[index];
2464}
2465
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002466void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2467{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002468 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002469 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002470 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002471 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002472}
2473
2474GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2475{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002476 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002477 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002478}
2479
Jiajia Qin729b2c62017-08-14 09:36:11 +08002480GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2481{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002482 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002483 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2484}
2485
Jamie Madill493f9572018-05-24 19:52:15 -04002486void Program::setTransformFeedbackVaryings(GLsizei count,
2487 const GLchar *const *varyings,
2488 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002489{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002490 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002491 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002492 for (GLsizei i = 0; i < count; i++)
2493 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002494 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002495 }
2496
Jamie Madill48ef11b2016-04-27 15:21:52 -04002497 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002498}
2499
Jamie Madill493f9572018-05-24 19:52:15 -04002500void Program::getTransformFeedbackVarying(GLuint index,
2501 GLsizei bufSize,
2502 GLsizei *length,
2503 GLsizei *size,
2504 GLenum *type,
2505 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002506{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002507 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002508 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002509 {
jchen10a9042d32017-03-17 08:50:45 +08002510 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2511 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2512 std::string varName = var.nameWithArrayIndex();
2513 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002514 if (length)
2515 {
2516 *length = lastNameIdx;
2517 }
2518 if (size)
2519 {
jchen10a9042d32017-03-17 08:50:45 +08002520 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002521 }
2522 if (type)
2523 {
jchen10a9042d32017-03-17 08:50:45 +08002524 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002525 }
2526 if (name)
2527 {
jchen10a9042d32017-03-17 08:50:45 +08002528 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002529 name[lastNameIdx] = '\0';
2530 }
2531 }
2532}
2533
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002534GLsizei Program::getTransformFeedbackVaryingCount() const
2535{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002536 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002537 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002538 {
jchen10a9042d32017-03-17 08:50:45 +08002539 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002540 }
2541 else
2542 {
2543 return 0;
2544 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002545}
2546
2547GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2548{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002549 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002550 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002551 {
2552 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002553 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002554 {
jchen10a9042d32017-03-17 08:50:45 +08002555 maxSize =
2556 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002557 }
2558
2559 return maxSize;
2560 }
2561 else
2562 {
2563 return 0;
2564 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002565}
2566
2567GLenum Program::getTransformFeedbackBufferMode() const
2568{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002569 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002570 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002571}
2572
jchen103fd614d2018-08-13 12:21:58 +08002573bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002574{
Jiawei Shao016105b2018-04-12 16:38:31 +08002575 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2576 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2577 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2578 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002579
Jamie Madill493f9572018-05-24 19:52:15 -04002580 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002581 bool isGraphicsShaderAttached =
2582 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002583 // Check whether we both have a compute and non-compute shaders attached.
2584 // If there are of both types attached, then linking should fail.
2585 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2586 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2587 {
2588 infoLog << "Both compute and graphics shaders are attached to the same program.";
2589 return false;
2590 }
2591
2592 if (computeShader)
2593 {
jchen103fd614d2018-08-13 12:21:58 +08002594 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002595 {
2596 infoLog << "Attached compute shader is not compiled.";
2597 return false;
2598 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002599 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002600
jchen103fd614d2018-08-13 12:21:58 +08002601 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002602
2603 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2604 // If the work group size is not specified, a link time error should occur.
2605 if (!mState.mComputeShaderLocalSize.isDeclared())
2606 {
2607 infoLog << "Work group size is not specified.";
2608 return false;
2609 }
2610 }
2611 else
2612 {
jchen103fd614d2018-08-13 12:21:58 +08002613 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002614 {
2615 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2616 return false;
2617 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002618 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002619
jchen103fd614d2018-08-13 12:21:58 +08002620 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002621 {
2622 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2623 return false;
2624 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002625 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002626
jchen103fd614d2018-08-13 12:21:58 +08002627 int vertexShaderVersion = vertexShader->getShaderVersion();
2628 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002629 {
2630 infoLog << "Fragment shader version does not match vertex shader version.";
2631 return false;
2632 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002633
2634 if (geometryShader)
2635 {
2636 // [GL_EXT_geometry_shader] Chapter 7
2637 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2638 // Language Specification, as well as any of the following reasons:
2639 // * One or more of the shader objects attached to <program> are not compiled
2640 // successfully.
2641 // * The shaders do not use the same shader language version.
2642 // * <program> contains objects to form a geometry shader, and
2643 // - <program> is not separable and contains no objects to form a vertex shader; or
2644 // - the input primitive type, output primitive type, or maximum output vertex count
2645 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002646 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002647 {
2648 infoLog << "The attached geometry shader isn't compiled.";
2649 return false;
2650 }
2651
jchen103fd614d2018-08-13 12:21:58 +08002652 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002653 {
2654 mInfoLog << "Geometry shader version does not match vertex shader version.";
2655 return false;
2656 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002657 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002658
Jamie Madill493f9572018-05-24 19:52:15 -04002659 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002660 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002661 if (!inputPrimitive.valid())
2662 {
2663 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2664 return false;
2665 }
2666
Jamie Madill493f9572018-05-24 19:52:15 -04002667 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002668 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002669 if (!outputPrimitive.valid())
2670 {
2671 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2672 return false;
2673 }
2674
jchen103fd614d2018-08-13 12:21:58 +08002675 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002676 if (!maxVertices.valid())
2677 {
2678 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2679 return false;
2680 }
2681
2682 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2683 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2684 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002685 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002686 }
Jiawei Shao73618602017-12-20 15:47:15 +08002687 }
2688
2689 return true;
2690}
2691
jchen10910a3da2017-11-15 09:40:11 +08002692GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2693{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002694 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002695 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2696 {
2697 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2698 if (tf.nameWithArrayIndex() == name)
2699 {
2700 return tfIndex;
2701 }
2702 }
2703 return GL_INVALID_INDEX;
2704}
2705
2706const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2707{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002708 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002709 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2710 return mState.mLinkedTransformFeedbackVaryings[index];
2711}
2712
jchen103fd614d2018-08-13 12:21:58 +08002713bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002714{
Jiawei Shao016105b2018-04-12 16:38:31 +08002715 Shader *previousShader = nullptr;
2716 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002717 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002718 Shader *currentShader = mState.mAttachedShaders[shaderType];
2719 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002720 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002721 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002722 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002723
2724 if (previousShader)
2725 {
jchen103fd614d2018-08-13 12:21:58 +08002726 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002727 {
2728 return false;
2729 }
2730 }
2731 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002732 }
2733
jchen103fd614d2018-08-13 12:21:58 +08002734 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002735 {
2736 return false;
2737 }
2738
jchen103fd614d2018-08-13 12:21:58 +08002739 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002740 {
2741 return false;
2742 }
2743
2744 return true;
2745}
2746
2747// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2748// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002749bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002750 gl::Shader *consumingShader,
2751 gl::InfoLog &infoLog) const
2752{
jchen103fd614d2018-08-13 12:21:58 +08002753 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002754
jchen103fd614d2018-08-13 12:21:58 +08002755 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2756 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002757
Jiawei Shao385b3e02018-03-21 09:43:28 +08002758 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002759
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002760 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002761 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002762 bool matched = false;
2763
2764 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002765 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002766 {
2767 continue;
2768 }
2769
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002770 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002771 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002772 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002773 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002774 ASSERT(!output.isBuiltIn());
2775
2776 std::string mismatchedStructFieldName;
2777 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002778 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002779 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002780 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002781 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002782 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2783 mismatchedStructFieldName, generatingShader->getType(),
2784 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002785 return false;
2786 }
2787
Geoff Lang7dd2e102014-11-10 15:19:26 -05002788 matched = true;
2789 break;
2790 }
2791 }
2792
Olli Etuaho107c7242018-03-20 15:45:35 +02002793 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2794 // whether the input is statically used - a statically used input should fail this test even
2795 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002796 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002797 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002798 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2799 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2800 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002801 return false;
2802 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002803 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002804
Jiawei Shaod063aff2018-02-22 10:19:09 +08002805 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002806
Jiawei Shaod063aff2018-02-22 10:19:09 +08002807 return true;
2808}
2809
jchen103fd614d2018-08-13 12:21:58 +08002810bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002811{
Jiawei Shao016105b2018-04-12 16:38:31 +08002812 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002813
2814 std::map<GLuint, std::string> staticFragmentInputLocations;
2815
2816 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002817 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002818 for (const sh::Varying &input : fragmentInputVaryings)
2819 {
2820 if (input.isBuiltIn() || !input.staticUse)
2821 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002822 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002823 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002824
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002825 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002826 if (inputBinding == -1)
2827 continue;
2828
2829 const auto it = staticFragmentInputLocations.find(inputBinding);
2830 if (it == std::end(staticFragmentInputLocations))
2831 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002832 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002833 }
2834 else
2835 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002836 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002837 << it->second;
2838 return false;
2839 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002840 }
2841
2842 return true;
2843}
2844
jchen103fd614d2018-08-13 12:21:58 +08002845bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002846 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002847 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002848 GLuint *combinedImageUniformsCount,
2849 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002850{
Olli Etuahob78707c2017-03-09 15:03:11 +00002851 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002852 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002853 {
2854 return false;
2855 }
2856
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002857 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002858
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002859 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002860
jchen10eaef1e52017-06-13 10:44:11 +08002861 if (!linkAtomicCounterBuffers())
2862 {
2863 return false;
2864 }
2865
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002866 return true;
2867}
2868
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002869void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002870{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002871 ASSERT(combinedImageUniforms);
2872
Jamie Madill982f6e02017-06-07 14:33:04 -04002873 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2874 unsigned int low = high;
2875
jchen10eaef1e52017-06-13 10:44:11 +08002876 for (auto counterIter = mState.mUniforms.rbegin();
2877 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2878 {
2879 --low;
2880 }
2881
2882 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2883
2884 high = low;
2885
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002886 for (auto imageIter = mState.mUniforms.rbegin();
2887 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2888 {
2889 --low;
2890 }
2891
2892 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002893 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002894 // If uniform is a image type, insert it into the mImageBindings array.
2895 for (unsigned int imageIndex : mState.mImageUniformRange)
2896 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002897 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2898 // cannot load values into a uniform defined as an image. if declare without a
2899 // binding qualifier, any uniform image variable (include all elements of
2900 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002901 auto &imageUniform = mState.mUniforms[imageIndex];
2902 if (imageUniform.binding == -1)
2903 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002904 mState.mImageBindings.emplace_back(
2905 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002906 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002907 else
2908 {
2909 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002910 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002911 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002912
2913 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2914 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002915 }
2916
2917 high = low;
2918
2919 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002920 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002921 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002922 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002923 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002924
2925 mState.mSamplerUniformRange = RangeUI(low, high);
2926
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002927 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002928 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002929 {
2930 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002931 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002932 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2933 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002934 }
2935}
2936
jchen10eaef1e52017-06-13 10:44:11 +08002937bool Program::linkAtomicCounterBuffers()
2938{
2939 for (unsigned int index : mState.mAtomicCounterUniformRange)
2940 {
Jamie Madill493f9572018-05-24 19:52:15 -04002941 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002942 uniform.blockInfo.offset = uniform.offset;
2943 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2944 uniform.blockInfo.matrixStride = 0;
2945 uniform.blockInfo.isRowMajorMatrix = false;
2946
Jamie Madill493f9572018-05-24 19:52:15 -04002947 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002948 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2949 ++bufferIndex)
2950 {
2951 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2952 if (buffer.binding == uniform.binding)
2953 {
2954 buffer.memberIndexes.push_back(index);
2955 uniform.bufferIndex = bufferIndex;
2956 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002957 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002958 break;
2959 }
2960 }
2961 if (!found)
2962 {
2963 AtomicCounterBuffer atomicCounterBuffer;
2964 atomicCounterBuffer.binding = uniform.binding;
2965 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002966 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002967 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2968 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2969 }
2970 }
2971 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002972 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002973
2974 return true;
2975}
2976
Jamie Madilleb979bf2016-11-15 12:28:46 -05002977// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002978bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002979{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002980 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002981
jchen103fd614d2018-08-13 12:21:58 +08002982 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002983
Geoff Lang7dd2e102014-11-10 15:19:26 -05002984 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002985 if (shaderVersion >= 300)
2986 {
2987 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2988 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002989 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002990 }
2991 else
2992 {
2993 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002994 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002995 }
jchen103fd614d2018-08-13 12:21:58 +08002996 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002997
2998 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002999 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04003000 {
Jamie Madillf6113162015-05-07 11:49:21 -04003001 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04003002 return false;
3003 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003004
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003005 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00003006
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003007 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003008 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04003009 {
Olli Etuahod2551232017-10-26 20:03:33 +03003010 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
3011 // structures, so we don't need to worry about adjusting their names or generating entries
3012 // for each member/element (unlike uniforms for example).
3013 ASSERT(!attribute.isArray() && !attribute.isStruct());
3014
Jamie Madilleb979bf2016-11-15 12:28:46 -05003015 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04003016 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04003017 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003018 attribute.location = bindingLocation;
3019 }
3020
3021 if (attribute.location != -1)
3022 {
3023 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04003024 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003025
Jamie Madill63805b42015-08-25 13:17:39 -04003026 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003027 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003028 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3029 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003030
3031 return false;
3032 }
3033
Jamie Madill63805b42015-08-25 13:17:39 -04003034 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003035 {
Jamie Madill63805b42015-08-25 13:17:39 -04003036 const int regLocation = attribute.location + reg;
3037 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003038
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003039 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3040 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3041 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003042 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003043 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003044 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003045 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003046 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003047 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003048 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003049 return false;
3050 }
3051 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003052 else
3053 {
Jamie Madill63805b42015-08-25 13:17:39 -04003054 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003055 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003056
Jamie Madill63805b42015-08-25 13:17:39 -04003057 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003058 }
3059 }
3060 }
3061
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003062 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003063 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003064 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003065 // Not set by glBindAttribLocation or by location layout qualifier
3066 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003067 {
Jamie Madill63805b42015-08-25 13:17:39 -04003068 int regs = VariableRegisterCount(attribute.type);
3069 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003070
Jamie Madill63805b42015-08-25 13:17:39 -04003071 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003072 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003073 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003074 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003075 }
3076
Jamie Madillc349ec02015-08-21 16:53:12 -04003077 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003078 }
3079 }
3080
Brandon Jonesc405ae72017-12-06 14:15:03 -08003081 ASSERT(mState.mAttributesTypeMask.none());
3082 ASSERT(mState.mAttributesMask.none());
3083
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003084 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3085 // shader versions we're only processing active attributes to begin with.
3086 if (shaderVersion >= 300)
3087 {
3088 for (auto attributeIter = mState.mAttributes.begin();
3089 attributeIter != mState.mAttributes.end();)
3090 {
3091 if (attributeIter->active)
3092 {
3093 ++attributeIter;
3094 }
3095 else
3096 {
3097 attributeIter = mState.mAttributes.erase(attributeIter);
3098 }
3099 }
3100 }
3101
Jamie Madill48ef11b2016-04-27 15:21:52 -04003102 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003103 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003104 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003105 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003106 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003107
Jamie Madillbd159f02017-10-09 19:39:06 -04003108 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003109 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003110 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3111 mState.mActiveAttribLocationsMask.set(location);
3112 mState.mMaxActiveAttribLocation =
3113 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003114
3115 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3116 if (!attribute.isBuiltIn())
3117 {
3118 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3119 location);
3120 mState.mAttributesMask.set(location);
3121 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003122 }
3123 }
3124
Geoff Lang7dd2e102014-11-10 15:19:26 -05003125 return true;
3126}
3127
jchen103fd614d2018-08-13 12:21:58 +08003128bool Program::linkInterfaceBlocks(const Caps &caps,
3129 const Version &version,
3130 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003131 InfoLog &infoLog,
3132 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003133{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003134 ASSERT(combinedShaderStorageBlocksCount);
3135
Jiawei Shao54aafe52018-04-27 14:54:57 +08003136 GLuint combinedUniformBlocksCount = 0u;
3137 GLuint numShadersHasUniformBlocks = 0u;
3138 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3139 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003140 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003141 Shader *shader = mState.mAttachedShaders[shaderType];
3142 if (!shader)
3143 {
3144 continue;
3145 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003146
jchen103fd614d2018-08-13 12:21:58 +08003147 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003148 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003149 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003150 if (!ValidateInterfaceBlocksCount(
3151 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3152 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3153 {
3154 return false;
3155 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003156
Jiawei Shao54aafe52018-04-27 14:54:57 +08003157 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3158 ++numShadersHasUniformBlocks;
3159 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003160 }
3161
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003162 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3163 {
3164 infoLog << "The sum of the number of active uniform blocks exceeds "
3165 "MAX_COMBINED_UNIFORM_BLOCKS ("
3166 << caps.maxCombinedUniformBlocks << ").";
3167 return false;
3168 }
3169
Jiawei Shao54aafe52018-04-27 14:54:57 +08003170 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3171 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003172 {
3173 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003174 }
Jamie Madille473dee2015-08-18 14:49:01 -04003175
jchen103fd614d2018-08-13 12:21:58 +08003176 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003177 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003178 *combinedShaderStorageBlocksCount = 0u;
3179 GLuint numShadersHasShaderStorageBlocks = 0u;
3180 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3181 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003182 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003183 Shader *shader = mState.mAttachedShaders[shaderType];
3184 if (!shader)
3185 {
3186 continue;
3187 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003188
jchen103fd614d2018-08-13 12:21:58 +08003189 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003190 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003191 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003192 if (!ValidateInterfaceBlocksCount(
3193 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3194 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3195 {
3196 return false;
3197 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003198
Jiawei Shao54aafe52018-04-27 14:54:57 +08003199 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3200 ++numShadersHasShaderStorageBlocks;
3201 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003202 }
3203
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003204 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003205 {
3206 infoLog << "The sum of the number of active shader storage blocks exceeds "
3207 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3208 << caps.maxCombinedShaderStorageBlocks << ").";
3209 return false;
3210 }
3211
Jiawei Shao54aafe52018-04-27 14:54:57 +08003212 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3213 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003214 {
3215 return false;
3216 }
3217 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003218
Geoff Lang7dd2e102014-11-10 15:19:26 -05003219 return true;
3220}
3221
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003222LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3223 const sh::ShaderVariable &variable2,
3224 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003225 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003226 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003227{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003228 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003229 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003230 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003231 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003232 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003233 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003234 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003235 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003236 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003237 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003238 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003239 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003240 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003241 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003242 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003243 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003244
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003245 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003246 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003247 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003248 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003249 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003250 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3251 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003252 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3253 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003254
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003255 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003256 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003257 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003258 }
3259
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003260 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003261 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003262 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003263 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003264 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3265 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003266 }
3267 }
3268
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003269 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003270}
3271
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003272LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3273 const sh::Varying &inputVarying,
3274 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003275 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003276 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003277{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003278 if (validateGeometryShaderInputVarying)
3279 {
3280 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3281 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3282 // or outputs.
3283 ASSERT(inputVarying.arraySizes.size() == 1u);
3284
3285 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3286 // varying cannot match a geometry shader input varying.
3287 // [GL_EXT_geometry_shader] Section 7.4.1:
3288 // Geometry shader per-vertex input variables and blocks are required to be declared as
3289 // arrays, with each element representing input or output values for a single vertex of a
3290 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3291 // are treated as though they were not declared as arrays.
3292 if (outputVarying.isArray())
3293 {
3294 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3295 }
3296 }
3297
3298 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3299 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003300 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003301 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3302 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003303 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003304 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003305 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003306 }
3307
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003308 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003309 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003310 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003311 }
3312
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003313 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003314 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003315 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003316 }
3317
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003318 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003319}
3320
jchen103fd614d2018-08-13 12:21:58 +08003321bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003322{
Jiawei Shao016105b2018-04-12 16:38:31 +08003323 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3324 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003325 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3326 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3327 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003328
3329 if (shaderVersion != 100)
3330 {
3331 // Only ESSL 1.0 has restrictions on matching input and output invariance
3332 return true;
3333 }
3334
3335 bool glPositionIsInvariant = false;
3336 bool glPointSizeIsInvariant = false;
3337 bool glFragCoordIsInvariant = false;
3338 bool glPointCoordIsInvariant = false;
3339
3340 for (const sh::Varying &varying : vertexVaryings)
3341 {
3342 if (!varying.isBuiltIn())
3343 {
3344 continue;
3345 }
3346 if (varying.name.compare("gl_Position") == 0)
3347 {
3348 glPositionIsInvariant = varying.isInvariant;
3349 }
3350 else if (varying.name.compare("gl_PointSize") == 0)
3351 {
3352 glPointSizeIsInvariant = varying.isInvariant;
3353 }
3354 }
3355
3356 for (const sh::Varying &varying : fragmentVaryings)
3357 {
3358 if (!varying.isBuiltIn())
3359 {
3360 continue;
3361 }
3362 if (varying.name.compare("gl_FragCoord") == 0)
3363 {
3364 glFragCoordIsInvariant = varying.isInvariant;
3365 }
3366 else if (varying.name.compare("gl_PointCoord") == 0)
3367 {
3368 glPointCoordIsInvariant = varying.isInvariant;
3369 }
3370 }
3371
3372 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3373 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3374 // Not requiring invariance to match is supported by:
3375 // dEQP, WebGL CTS, Nexus 5X GLES
3376 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3377 {
3378 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3379 "declared invariant.";
3380 return false;
3381 }
3382 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3383 {
3384 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3385 "declared invariant.";
3386 return false;
3387 }
3388
3389 return true;
3390}
3391
jchen103fd614d2018-08-13 12:21:58 +08003392bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003393 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003394 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003395 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003396{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003397
jchen108225e732017-11-14 16:29:03 +08003398 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003399 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003400 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003401 {
jchen103fd614d2018-08-13 12:21:58 +08003402 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003403 {
Geoff Lang1a683462015-09-29 15:09:59 -04003404 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003405 return false;
3406 }
jchen103fd614d2018-08-13 12:21:58 +08003407 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003408 {
3409 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3410 {
3411 infoLog << "Two transform feedback varyings include the same array element ("
3412 << tfVaryingName << ").";
3413 return false;
3414 }
3415 }
3416 else
3417 {
3418 if (uniqueNames.count(tfVaryingName) > 0)
3419 {
3420 infoLog << "Two transform feedback varyings specify the same output variable ("
3421 << tfVaryingName << ").";
3422 return false;
3423 }
3424 }
3425 uniqueNames.insert(tfVaryingName);
3426 }
3427
3428 // Validate against program varyings.
3429 size_t totalComponents = 0;
3430 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3431 {
3432 std::vector<unsigned int> subscripts;
3433 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3434
3435 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3436 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003437 {
3438 infoLog << "Transform feedback varying " << tfVaryingName
3439 << " does not exist in the vertex shader.";
3440 return false;
3441 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003442
jchen108225e732017-11-14 16:29:03 +08003443 // Validate the matching variable.
3444 if (var->isStruct())
3445 {
3446 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3447 return false;
3448 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003449
jchen108225e732017-11-14 16:29:03 +08003450 size_t elementCount = 0;
3451 size_t componentCount = 0;
3452
3453 if (var->isArray())
3454 {
jchen103fd614d2018-08-13 12:21:58 +08003455 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003456 {
3457 infoLog << "Capture of arrays is undefined and not supported.";
3458 return false;
3459 }
3460
3461 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3462 ASSERT(!var->isArrayOfArrays());
3463
3464 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3465 {
3466 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3467 return false;
3468 }
3469 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3470 }
3471 else
3472 {
3473 if (!subscripts.empty())
3474 {
3475 infoLog << "Varying '" << baseName
3476 << "' is not an array to be captured by element.";
3477 return false;
3478 }
3479 elementCount = 1;
3480 }
3481
3482 // TODO(jmadill): Investigate implementation limits on D3D11
3483 componentCount = VariableComponentCount(var->type) * elementCount;
3484 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3485 componentCount > caps.maxTransformFeedbackSeparateComponents)
3486 {
3487 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3488 << componentCount << ") exceed the maximum separate components ("
3489 << caps.maxTransformFeedbackSeparateComponents << ").";
3490 return false;
3491 }
3492
3493 totalComponents += componentCount;
3494 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3495 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3496 {
3497 infoLog << "Transform feedback varying total components (" << totalComponents
3498 << ") exceed the maximum interleaved components ("
3499 << caps.maxTransformFeedbackInterleavedComponents << ").";
3500 return false;
3501 }
3502 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003503 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003504}
3505
jchen103fd614d2018-08-13 12:21:58 +08003506bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003507{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003508 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003509 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003510
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003511 for (const auto &attrib : attributes)
3512 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003513 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003514 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003515 Shader *shader = mState.mAttachedShaders[shaderType];
3516 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003517 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003518 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003519 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003520
jchen103fd614d2018-08-13 12:21:58 +08003521 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003522 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003523 {
3524 if (uniform.name == attrib.name)
3525 {
3526 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3527 return false;
3528 }
3529 }
3530 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003531 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003532
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003533 return true;
3534}
3535
Jamie Madill3c1da042017-11-27 18:33:40 -05003536void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003537{
3538 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003539 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003540 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003541 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003542 std::vector<unsigned int> subscripts;
3543 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003544 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003545 if (!subscripts.empty())
3546 {
3547 subscript = subscripts.back();
3548 }
Jamie Madill192745a2016-12-22 15:58:21 -05003549 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003550 {
Jamie Madill192745a2016-12-22 15:58:21 -05003551 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003552 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003553 {
jchen10a9042d32017-03-17 08:50:45 +08003554 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3555 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003556 break;
3557 }
jchen108225e732017-11-14 16:29:03 +08003558 else if (varying->isStruct())
3559 {
3560 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3561 if (field != nullptr)
3562 {
3563 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3564 break;
3565 }
3566 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003567 }
3568 }
James Darpinian30b604d2018-03-12 17:26:57 -07003569 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003570}
3571
jchen103fd614d2018-08-13 12:21:58 +08003572ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003573{
Jamie Madill3c1da042017-11-27 18:33:40 -05003574 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003575
Jiawei Shao016105b2018-04-12 16:38:31 +08003576 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003577 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003578 {
Jamie Madill192745a2016-12-22 15:58:21 -05003579 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003580 }
3581
Jiawei Shao016105b2018-04-12 16:38:31 +08003582 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003583 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003584 {
Jamie Madill192745a2016-12-22 15:58:21 -05003585 merged[varying.name].fragment = &varying;
3586 }
3587
3588 return merged;
3589}
3590
Olli Etuaho0ca09752018-09-24 11:00:50 +03003591bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3592{
3593 return a.getArraySizeProduct() > b.getArraySizeProduct();
3594}
3595
3596int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3597{
3598 if (outputVariable.location != -1)
3599 {
3600 return outputVariable.location;
3601 }
3602 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3603 if (apiLocation != -1)
3604 {
3605 return apiLocation;
3606 }
3607 return -1;
3608}
3609
3610bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3611{
3612 if (outputVariable.index != -1)
3613 {
3614 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3615 return (outputVariable.index == 1);
3616 }
3617 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3618 if (apiIndex != -1)
3619 {
3620 // Index layout qualifier from the shader takes precedence, so the index from the API is
3621 // checked only if the index was not set in the shader. This is not specified in the EXT
3622 // spec, but is specified in desktop OpenGL specs.
3623 return (apiIndex == 1);
3624 }
3625 // EXT_blend_func_extended: Outputs get index 0 by default.
3626 return false;
3627}
3628
jchen103fd614d2018-08-13 12:21:58 +08003629bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003630 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003631 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003632 GLuint combinedImageUniformsCount,
3633 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003634{
Jiawei Shao016105b2018-04-12 16:38:31 +08003635 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003636 ASSERT(fragmentShader != nullptr);
3637
Geoff Lange0cff192017-05-30 13:04:56 -04003638 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003639 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003640 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003641
jchen103fd614d2018-08-13 12:21:58 +08003642 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003643 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003644 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003645 {
3646 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3647 outputVariable.name != "gl_FragData")
3648 {
3649 continue;
3650 }
3651
3652 unsigned int baseLocation =
3653 (outputVariable.location == -1 ? 0u
3654 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003655
3656 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3657 // structures, so we may use getBasicTypeElementCount().
3658 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3659 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003660 {
3661 const unsigned int location = baseLocation + elementIndex;
3662 if (location >= mState.mOutputVariableTypes.size())
3663 {
3664 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3665 }
Corentin Walleze7557742017-06-01 13:09:57 -04003666 ASSERT(location < mState.mActiveOutputVariables.size());
3667 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003668 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003669 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003670 }
3671 }
3672
jchen103fd614d2018-08-13 12:21:58 +08003673 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003674 {
3675 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3676 // A link error will be generated if the sum of the number of active image uniforms used in
3677 // all shaders, the number of active shader storage blocks, and the number of active
3678 // fragment shader outputs exceeds the implementation-dependent value of
3679 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3680 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3681 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003682 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003683 {
3684 mInfoLog
3685 << "The sum of the number of active image uniforms, active shader storage blocks "
3686 "and active fragment shader outputs exceeds "
3687 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003688 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003689 return false;
3690 }
3691 }
3692
Jamie Madill80a6fc02015-08-21 16:53:16 -04003693 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003694 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003695 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003696
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003697 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003698 // TODO(jmadill): any caps validation here?
3699
Olli Etuaho0ca09752018-09-24 11:00:50 +03003700 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3701 {
3702 if (outputVariable.isArray())
3703 {
3704 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3705 // Resources and including [0] at the end of array variable names.
3706 outputVariable.name += "[0]";
3707 outputVariable.mappedName += "[0]";
3708 }
3709 }
3710
3711 bool hasSecondaryOutputs = false;
3712
3713 // Reserve locations for output variables whose location is fixed in the shader or through the
3714 // API.
jchen1015015f72017-03-16 13:54:21 +08003715 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003716 outputVariableIndex++)
3717 {
jchen1015015f72017-03-16 13:54:21 +08003718 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003719
3720 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3721 if (outputVariable.isBuiltIn())
3722 continue;
3723
Olli Etuaho0ca09752018-09-24 11:00:50 +03003724 int baseLocation = getOutputLocationForLink(outputVariable);
3725 if (baseLocation == -1)
3726 {
3727 // Here we're only reserving locations for variables whose location is fixed.
3728 continue;
3729 }
3730
3731 auto *outputLocations = &mState.mOutputLocations;
3732 if (isOutputSecondaryForLink(outputVariable))
3733 {
3734 outputLocations = &mState.mSecondaryOutputLocations;
3735 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3736 // an output has an index specified it will always also have the location specified.
3737 hasSecondaryOutputs = true;
3738 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003739
Olli Etuaho465835d2017-09-26 13:34:10 +03003740 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3741 // structures, so we may use getBasicTypeElementCount().
3742 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003743 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3744 if (outputLocationsNeeded > outputLocations->size())
3745 {
3746 outputLocations->resize(outputLocationsNeeded);
3747 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003748 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003749 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003750 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3751 ASSERT(location < outputLocations->size());
3752 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003753 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003754 mInfoLog << "Location of variable " << outputVariable.name
3755 << " conflicts with another variable.";
3756 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003757 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003758 if (outputVariable.isArray())
3759 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003760 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003761 }
3762 else
3763 {
3764 VariableLocation locationInfo;
3765 locationInfo.index = outputVariableIndex;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003766 (*outputLocations)[location] = locationInfo;
3767 }
3768 }
3769 }
3770
3771 // Here we assign locations for the output variables that don't yet have them. Note that we're
3772 // not necessarily able to fit the variables optimally, since then we might have to try
3773 // different arrangements of output arrays. Now we just assign the locations in the order that
3774 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3775 // finding locations for the output variables, so this should be acceptable at least for now.
3776 GLuint maxLocation = caps.maxDrawBuffers;
3777 if (hasSecondaryOutputs)
3778 {
3779 // EXT_blend_func_extended: Program outputs will be validated against
3780 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3781 maxLocation = extensions.maxDualSourceDrawBuffers;
3782 }
3783
3784 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3785 outputVariableIndex++)
3786 {
3787 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3788
3789 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3790 if (outputVariable.isBuiltIn())
3791 continue;
3792
3793 if (getOutputLocationForLink(outputVariable) != -1)
3794 {
3795 continue;
3796 }
3797
3798 auto *outputLocations = &mState.mOutputLocations;
3799 if (isOutputSecondaryForLink(outputVariable))
3800 {
3801 outputLocations = &mState.mSecondaryOutputLocations;
3802 }
3803
3804 int baseLocation = 0;
3805 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3806 bool elementsFit = false;
3807 while (!elementsFit)
3808 {
3809 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3810 elementsFit = true;
3811 if (baseLocation + elementCount > maxLocation)
3812 {
3813 // EXT_blend_func_extended: Linking can fail:
3814 // "if the explicit binding assignments do not leave enough space for the linker to
3815 // automatically assign a location for a varying out array, which requires multiple
3816 // contiguous locations."
3817 mInfoLog << "Could not fit output variable into available locations: "
3818 << outputVariable.name;
3819 return false;
3820 }
3821 unsigned int outputLocationsNeeded =
3822 static_cast<unsigned int>(baseLocation) + elementCount;
3823 if (outputLocationsNeeded > outputLocations->size())
3824 {
3825 outputLocations->resize(outputLocationsNeeded);
3826 }
3827 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3828 {
3829 const unsigned int location =
3830 static_cast<unsigned int>(baseLocation) + elementIndex;
3831 ASSERT(location < outputLocations->size());
3832 if (outputLocations->at(location).used())
3833 {
3834 elementsFit = false;
3835 break;
3836 }
3837 }
3838 if (elementsFit)
3839 {
3840 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3841 {
3842 const unsigned int location =
3843 static_cast<unsigned int>(baseLocation) + elementIndex;
3844 if (outputVariable.isArray())
3845 {
3846 (*outputLocations)[location] =
3847 VariableLocation(elementIndex, outputVariableIndex);
3848 }
3849 else
3850 {
3851 VariableLocation locationInfo;
3852 locationInfo.index = outputVariableIndex;
3853 (*outputLocations)[location] = locationInfo;
3854 }
3855 }
3856 }
3857 else
3858 {
3859 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003860 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003861 }
3862 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003863
3864 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003865}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003866
Olli Etuaho48fed632017-03-16 12:05:30 +00003867void Program::setUniformValuesFromBindingQualifiers()
3868{
Jamie Madill982f6e02017-06-07 14:33:04 -04003869 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003870 {
3871 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3872 if (samplerUniform.binding != -1)
3873 {
Olli Etuahod2551232017-10-26 20:03:33 +03003874 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003875 ASSERT(location != -1);
3876 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003877 for (unsigned int elementIndex = 0;
3878 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003879 {
3880 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3881 }
3882 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3883 boundTextureUnits.data());
3884 }
3885 }
3886}
3887
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003888void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003889{
jchen10af713a22017-04-19 09:10:56 +08003890 // Set initial bindings from shader.
3891 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3892 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003893 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003894 bindUniformBlock(blockIndex, uniformBlock.binding);
3895 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003896}
3897
Jamie Madille7d84322017-01-10 18:21:59 -05003898void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003899 GLsizei clampedCount,
3900 const GLint *v)
3901{
Jamie Madill81c2e252017-09-09 23:32:46 -04003902 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3903 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003904 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3905 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003906
Jamie Madill7e4eff12018-08-08 15:49:26 -04003907 if (samplerBinding.unreferenced)
3908 return;
3909
3910 // Update the sampler uniforms.
3911 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3912 {
3913 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3914 GLint newSamplerIndex = v[arrayIndex];
3915
3916 if (oldSamplerIndex == newSamplerIndex)
3917 continue;
3918
3919 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3920
3921 // Update the reference counts.
3922 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3923 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3924 ASSERT(oldRefCount > 0);
3925 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3926 oldRefCount--;
3927 newRefCount++;
3928
3929 // Check for binding type change.
3930 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3931 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3932
3933 if (newRefCount == 1)
3934 {
3935 newSamplerType = samplerBinding.textureType;
3936 mState.mActiveSamplersMask.set(newSamplerIndex);
3937 }
3938 else if (newSamplerType != samplerBinding.textureType)
3939 {
3940 // Conflict detected. Ensure we reset it properly.
3941 newSamplerType = TextureType::InvalidEnum;
3942 }
3943
3944 // Unset previously active sampler.
3945 if (oldRefCount == 0)
3946 {
3947 oldSamplerType = TextureType::InvalidEnum;
3948 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3949 }
3950 else if (oldSamplerType == TextureType::InvalidEnum)
3951 {
3952 // Previous conflict. Check if this new change fixed the conflict.
3953 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3954 }
3955 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003956
3957 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003958 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003959}
3960
Jamie Madill7e4eff12018-08-08 15:49:26 -04003961TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3962{
3963 TextureType foundType = TextureType::InvalidEnum;
3964
3965 for (const SamplerBinding &binding : mSamplerBindings)
3966 {
3967 if (binding.unreferenced)
3968 continue;
3969
3970 // A conflict exists if samplers of different types are sourced by the same texture unit.
3971 // We need to check all bound textures to detect this error case.
3972 for (GLuint textureUnit : binding.boundTextureUnits)
3973 {
3974 if (textureUnit == textureUnitIndex)
3975 {
3976 if (foundType == TextureType::InvalidEnum)
3977 {
3978 foundType = binding.textureType;
3979 }
3980 else if (foundType != binding.textureType)
3981 {
3982 return TextureType::InvalidEnum;
3983 }
3984 }
3985 }
3986 }
3987
3988 return foundType;
3989}
3990
Jamie Madille7d84322017-01-10 18:21:59 -05003991template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003992GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3993 GLsizei count,
3994 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003995 const T *v)
3996{
Jamie Madill134f93d2017-08-31 17:11:00 -04003997 if (count == 1)
3998 return 1;
3999
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004000 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04004001
Corentin Wallez15ac5342016-11-03 17:06:39 -04004002 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4003 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004004 unsigned int remainingElements =
4005 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004006 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004007 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004008
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004009 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004010 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004011 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004012 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004013
4014 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004015}
4016
4017template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004018GLsizei Program::clampMatrixUniformCount(GLint location,
4019 GLsizei count,
4020 GLboolean transpose,
4021 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004022{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004023 const VariableLocation &locationInfo = mState.mUniformLocations[location];
4024
Jamie Madill62d31cb2015-09-11 13:25:51 -04004025 if (!transpose)
4026 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004027 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004028 }
4029
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004030 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004031
4032 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4033 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004034 unsigned int remainingElements =
4035 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004036 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004037}
4038
Jamie Madill54164b02017-08-28 15:17:37 -04004039// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4040// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004041template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004042void Program::getUniformInternal(const Context *context,
4043 DestT *dataOut,
4044 GLint location,
4045 GLenum nativeType,
4046 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004047{
Jamie Madill54164b02017-08-28 15:17:37 -04004048 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004049 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004050 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004051 {
4052 GLint tempValue[16] = {0};
4053 mProgram->getUniformiv(context, location, tempValue);
4054 UniformStateQueryCastLoop<GLboolean>(
4055 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004056 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004057 }
4058 case GL_INT:
4059 {
4060 GLint tempValue[16] = {0};
4061 mProgram->getUniformiv(context, location, tempValue);
4062 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4063 components);
4064 break;
4065 }
4066 case GL_UNSIGNED_INT:
4067 {
4068 GLuint tempValue[16] = {0};
4069 mProgram->getUniformuiv(context, location, tempValue);
4070 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4071 components);
4072 break;
4073 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004074 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004075 {
4076 GLfloat tempValue[16] = {0};
4077 mProgram->getUniformfv(context, location, tempValue);
4078 UniformStateQueryCastLoop<GLfloat>(
4079 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004080 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004081 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004082 default:
4083 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004084 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004085 }
4086}
Jamie Madilla4595b82017-01-11 17:36:34 -05004087
4088bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4089{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004090 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004091 // Must be called after samplers are validated.
4092 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4093
4094 for (const auto &binding : mState.mSamplerBindings)
4095 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004096 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004097 for (const auto &unit : binding.boundTextureUnits)
4098 {
4099 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4100 if (programTextureID == textureID)
4101 {
4102 // TODO(jmadill): Check for appropriate overlap.
4103 return true;
4104 }
4105 }
4106 }
4107
4108 return false;
4109}
4110
Jamie Madill70aeda42018-08-20 12:17:40 -04004111Error Program::syncState(const Context *context)
4112{
4113 if (mDirtyBits.any())
4114 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004115 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004116 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4117 mDirtyBits.reset();
4118 }
4119
4120 return NoError();
4121}
Jamie Madilla2c74982016-12-12 11:20:42 -05004122} // namespace gl