blob: 16614730c63c1c94df40126880b91ca66ea22f63 [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 {
1132 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001133 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001134 }
1135
1136 if (mLinked)
1137 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001138 double delta = platform->currentTime(platform) - startTime;
1139 int us = static_cast<int>(delta * 1000000.0);
1140 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001141 return NoError();
1142 }
1143
1144 // Cache load failed, fall through to normal linking.
1145 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001146
Jamie Madill6bc264a2018-03-31 15:36:05 -04001147 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001148 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001149
jchen107ae70d82018-07-06 13:47:01 +08001150 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001151 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001152 {
jchen107ae70d82018-07-06 13:47:01 +08001153 resources.reset(
1154 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1155 {&mState.mUniformBlocks, &mState.mUniforms},
1156 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1157 {&mState.mAtomicCounterBuffers},
1158 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001159
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001160 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001161 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1162 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001163 {
1164 return NoError();
1165 }
1166
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001167 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001168 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1169 context->getExtensions().webglCompatibility, mInfoLog,
1170 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001171 {
1172 return NoError();
1173 }
1174
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001175 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1176 // A link error will be generated if the sum of the number of active image uniforms used in
1177 // all shaders, the number of active shader storage blocks, and the number of active
1178 // fragment shader outputs exceeds the implementation-dependent value of
1179 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1180 if (combinedImageUniforms + combinedShaderStorageBlocks >
1181 context->getCaps().maxCombinedShaderOutputResources)
1182 {
1183 mInfoLog
1184 << "The sum of the number of active image uniforms, active shader storage blocks "
1185 "and active fragment shader outputs exceeds "
1186 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1187 << context->getCaps().maxCombinedShaderOutputResources << ")";
1188 return NoError();
1189 }
1190
jchen103fd614d2018-08-13 12:21:58 +08001191 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1192 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001193 }
1194 else
1195 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001196 // Map the varyings to the register file
1197 // In WebGL, we use a slightly different handling for packing variables.
1198 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1199 if (data.getLimitations().noFlexibleVaryingPacking)
1200 {
1201 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1202 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1203 }
1204 else if (data.getExtensions().webglCompatibility)
1205 {
1206 packMode = PackMode::WEBGL_STRICT;
1207 }
1208
jchen107ae70d82018-07-06 13:47:01 +08001209 resources.reset(
1210 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1211 {&mState.mUniformBlocks, &mState.mUniforms},
1212 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1213 {&mState.mAtomicCounterBuffers},
1214 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001215
jchen103fd614d2018-08-13 12:21:58 +08001216 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001217 {
1218 return NoError();
1219 }
1220
jchen103fd614d2018-08-13 12:21:58 +08001221 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001222 {
1223 return NoError();
1224 }
1225
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001226 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001227 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1228 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001229 {
1230 return NoError();
1231 }
1232
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001233 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001234 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1235 context->getExtensions().webglCompatibility, mInfoLog,
1236 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001237 {
1238 return NoError();
1239 }
1240
jchen103fd614d2018-08-13 12:21:58 +08001241 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001242 {
1243 return NoError();
1244 }
1245
Olli Etuaho0ca09752018-09-24 11:00:50 +03001246 if (!linkOutputVariables(context->getCaps(), context->getExtensions(),
1247 context->getClientVersion(), combinedImageUniforms,
1248 combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001249 {
1250 return NoError();
1251 }
1252
jchen103fd614d2018-08-13 12:21:58 +08001253 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001254
Jiawei Shao016105b2018-04-12 16:38:31 +08001255 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001256 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001257
jchen103fd614d2018-08-13 12:21:58 +08001258 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1259 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001260
jchen103fd614d2018-08-13 12:21:58 +08001261 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1262 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001263 {
1264 return NoError();
1265 }
1266
jchen107ae70d82018-07-06 13:47:01 +08001267 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001268 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001269 {
1270 return NoError();
1271 }
1272
Martin Radev4c4c8e72016-08-04 12:25:34 +03001273 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001274 }
1275
jchen107ae70d82018-07-06 13:47:01 +08001276 mLinkingState.reset(new LinkingState());
1277 mLinkingState->context = context;
1278 mLinkingState->programHash = programHash;
1279 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1280 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001281 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001282
1283 return NoError();
1284}
1285
1286bool Program::isLinking() const
1287{
1288 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1289}
1290
jchen107ae70d82018-07-06 13:47:01 +08001291void Program::resolveLinkImpl()
1292{
1293 ASSERT(mLinkingState.get());
1294
1295 mLinked = mLinkingState->linkEvent->wait();
jchen105055fba2018-08-17 09:57:07 +08001296 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001297 auto linkingState = std::move(mLinkingState);
1298 if (!mLinked)
1299 {
1300 return;
1301 }
1302
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001303 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001304
Yunchao Heece12532017-11-21 15:50:21 +08001305 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1306 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001307 ASSERT(mLinked);
1308 updateLinkedShaderStages();
1309
Jamie Madill54164b02017-08-28 15:17:37 -04001310 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001311 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1312 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001313
Jamie Madill7e4eff12018-08-08 15:49:26 -04001314 // Must be called after markUnusedUniformLocations.
1315 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001316 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001317
1318 setUniformValuesFromBindingQualifiers();
1319
Jamie Madill32447362017-06-28 14:53:52 -04001320 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001321 auto *cache = linkingState->context->getMemoryProgramCache();
1322 if (cache &&
1323 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1324 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001325 {
jchen107ae70d82018-07-06 13:47:01 +08001326 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001327 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001328}
1329
Yunchao He85072e82017-11-14 15:43:28 +08001330void Program::updateLinkedShaderStages()
1331{
Yunchao Heece12532017-11-21 15:50:21 +08001332 mState.mLinkedShaderStages.reset();
1333
Jiawei Shao016105b2018-04-12 16:38:31 +08001334 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001335 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001336 if (shader)
1337 {
1338 mState.mLinkedShaderStages.set(shader->getType());
1339 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001340 }
Yunchao He85072e82017-11-14 15:43:28 +08001341}
1342
James Darpinian30b604d2018-03-12 17:26:57 -07001343void ProgramState::updateTransformFeedbackStrides()
1344{
1345 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1346 {
1347 mTransformFeedbackStrides.resize(1);
1348 size_t totalSize = 0;
1349 for (auto &varying : mLinkedTransformFeedbackVaryings)
1350 {
1351 totalSize += varying.size() * VariableExternalSize(varying.type);
1352 }
1353 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1354 }
1355 else
1356 {
1357 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1358 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1359 {
1360 auto &varying = mLinkedTransformFeedbackVaryings[i];
1361 mTransformFeedbackStrides[i] =
1362 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1363 }
1364 }
1365}
1366
Jamie Madill7e4eff12018-08-08 15:49:26 -04001367void ProgramState::updateActiveSamplers()
1368{
1369 for (SamplerBinding &samplerBinding : mSamplerBindings)
1370 {
1371 if (samplerBinding.unreferenced)
1372 continue;
1373
1374 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1375 {
1376 mActiveSamplerRefCounts[textureUnit]++;
1377 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1378 mActiveSamplersMask.set(textureUnit);
1379 }
1380 }
1381}
1382
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001383void ProgramState::updateActiveImages()
1384{
1385 for (ImageBinding &imageBinding : mImageBindings)
1386 {
1387 if (imageBinding.unreferenced)
1388 continue;
1389
1390 for (GLint imageUnit : imageBinding.boundImageUnits)
1391 {
1392 mActiveImagesMask.set(imageUnit);
1393 }
1394 }
1395}
1396
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001397// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001398void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001399{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001400 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001401 mState.mAttributesTypeMask.reset();
1402 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001403 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001404 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001405 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001406 mState.mUniforms.clear();
1407 mState.mUniformLocations.clear();
1408 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001409 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001410 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001411 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001412 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001413 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001414 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001415 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001416 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001417 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001418 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001419 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001420 mState.mNumViews = -1;
1421 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1422 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001423 mState.mGeometryShaderInvocations = 1;
1424 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426 mValidated = false;
1427
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001428 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001429 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001430}
1431
Jamie Madilla2c74982016-12-12 11:20:42 -05001432Error Program::loadBinary(const Context *context,
1433 GLenum binaryFormat,
1434 const void *binary,
1435 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001436{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001437 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001438 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001439
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001441 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001443 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1444 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001445 {
Jamie Madillf6113162015-05-07 11:49:21 -04001446 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001447 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448 }
1449
Jamie Madill4f86d052017-06-05 12:59:26 -04001450 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1451 ANGLE_TRY_RESULT(
1452 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001453
1454 // Currently we require the full shader text to compute the program hash.
1455 // TODO(jmadill): Store the binary in the internal program cache.
1456
Jamie Madill70aeda42018-08-20 12:17:40 -04001457 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1458 ++uniformBlockIndex)
1459 {
1460 mDirtyBits.set(uniformBlockIndex);
1461 }
1462
Jamie Madillb0a838b2016-11-13 20:02:12 -05001463 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001464#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465}
1466
Jamie Madilla2c74982016-12-12 11:20:42 -05001467Error Program::saveBinary(const Context *context,
1468 GLenum *binaryFormat,
1469 void *binary,
1470 GLsizei bufSize,
1471 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001473 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474 if (binaryFormat)
1475 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001476 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477 }
1478
Jamie Madill4f86d052017-06-05 12:59:26 -04001479 angle::MemoryBuffer memoryBuf;
1480 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481
Jamie Madill4f86d052017-06-05 12:59:26 -04001482 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1483 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484
1485 if (streamLength > bufSize)
1486 {
1487 if (length)
1488 {
1489 *length = 0;
1490 }
1491
Jamie Madill493f9572018-05-24 19:52:15 -04001492 // TODO: This should be moved to the validation layer but computing the size of the binary
1493 // before saving it causes the save to happen twice. It may be possible to write the binary
1494 // to a separate buffer, validate sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001495 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496 }
1497
1498 if (binary)
1499 {
Jamie Madill493f9572018-05-24 19:52:15 -04001500 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501
Jamie Madill48ef11b2016-04-27 15:21:52 -04001502 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001503 ptr += streamLength;
1504
1505 ASSERT(ptr - streamLength == binary);
1506 }
1507
1508 if (length)
1509 {
1510 *length = streamLength;
1511 }
1512
He Yunchaoacd18982017-01-04 10:46:42 +08001513 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001514}
1515
Jamie Madillffe00c02017-06-27 16:26:55 -04001516GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001518 ASSERT(mLinkResolved);
Geoff Langb26ab822018-05-29 11:19:00 -04001519 if (!mLinked)
1520 {
1521 return 0;
1522 }
1523
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001525 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001526 if (error.isError())
1527 {
1528 return 0;
1529 }
1530
1531 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001532}
1533
Geoff Langc5629752015-12-07 16:29:04 -05001534void Program::setBinaryRetrievableHint(bool retrievable)
1535{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001536 ASSERT(mLinkResolved);
Geoff Langc5629752015-12-07 16:29:04 -05001537 // TODO(jmadill) : replace with dirty bits
1538 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001539 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001540}
1541
1542bool Program::getBinaryRetrievableHint() const
1543{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001544 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001545 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001546}
1547
Yunchao He61afff12017-03-14 15:34:03 +08001548void Program::setSeparable(bool separable)
1549{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001550 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001551 // TODO(yunchao) : replace with dirty bits
1552 if (mState.mSeparable != separable)
1553 {
1554 mProgram->setSeparable(separable);
1555 mState.mSeparable = separable;
1556 }
1557}
1558
1559bool Program::isSeparable() const
1560{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001561 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001562 return mState.mSeparable;
1563}
1564
Jamie Madill6c1f6712017-02-14 19:08:04 -05001565void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001566{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001567 ASSERT(mLinkResolved);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001568 mRefCount--;
1569
1570 if (mRefCount == 0 && mDeleteStatus)
1571 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001572 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001573 }
1574}
1575
1576void Program::addRef()
1577{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001578 ASSERT(mLinkResolved);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001579 mRefCount++;
1580}
1581
1582unsigned int Program::getRefCount() const
1583{
1584 return mRefCount;
1585}
1586
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001587int Program::getInfoLogLength() const
1588{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001589 ASSERT(mLinkResolved);
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001590 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001591}
1592
Geoff Lange1a27752015-10-05 13:16:04 -04001593void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001594{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001595 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001596 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001597}
1598
Geoff Lange1a27752015-10-05 13:16:04 -04001599void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001600{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001601 ASSERT(mLinkResolved);
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001602 int total = 0;
1603
Jiawei Shao016105b2018-04-12 16:38:31 +08001604 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001605 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001606 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001607 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001608 shaders[total] = shader->getHandle();
1609 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001610 }
1611 }
1612
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001613 if (count)
1614 {
1615 *count = total;
1616 }
1617}
1618
Geoff Lange1a27752015-10-05 13:16:04 -04001619GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001621 ASSERT(mLinkResolved);
Jamie Madill34ca4f52017-06-13 11:49:39 -04001622 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623}
1624
Jamie Madill63805b42015-08-25 13:17:39 -04001625bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001626{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001627 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001628 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1629 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001630}
1631
jchen10fd7c3b52017-03-21 15:36:03 +08001632void Program::getActiveAttribute(GLuint index,
1633 GLsizei bufsize,
1634 GLsizei *length,
1635 GLint *size,
1636 GLenum *type,
1637 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001638{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001639 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001640 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001641 {
1642 if (bufsize > 0)
1643 {
1644 name[0] = '\0';
1645 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001647 if (length)
1648 {
1649 *length = 0;
1650 }
1651
1652 *type = GL_NONE;
1653 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001654 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001655 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001656
jchen1036e120e2017-03-14 14:53:58 +08001657 ASSERT(index < mState.mAttributes.size());
1658 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001659
1660 if (bufsize > 0)
1661 {
jchen10fd7c3b52017-03-21 15:36:03 +08001662 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001663 }
1664
1665 // Always a single 'type' instance
1666 *size = 1;
1667 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001668}
1669
Geoff Lange1a27752015-10-05 13:16:04 -04001670GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001671{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001672 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001673 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001674 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001675 return 0;
1676 }
1677
jchen1036e120e2017-03-14 14:53:58 +08001678 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001679}
1680
Geoff Lange1a27752015-10-05 13:16:04 -04001681GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001682{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001683 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001684 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001685 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001686 return 0;
1687 }
1688
1689 size_t maxLength = 0;
1690
Jamie Madill48ef11b2016-04-27 15:21:52 -04001691 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001692 {
jchen1036e120e2017-03-14 14:53:58 +08001693 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001694 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695
Jamie Madillc349ec02015-08-21 16:53:12 -04001696 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697}
1698
jchen107ae70d82018-07-06 13:47:01 +08001699const std::vector<sh::Attribute> &Program::getAttributes() const
1700{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001701 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001702 return mState.mAttributes;
1703}
1704
jchen107ae70d82018-07-06 13:47:01 +08001705const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1706{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001707 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001708 return mState.mSamplerBindings;
1709}
1710
jchen107ae70d82018-07-06 13:47:01 +08001711const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1712{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001713 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001714 return mState.mComputeShaderLocalSize;
1715}
1716
1717PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1718{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001719 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001720 return mState.mGeometryShaderInputPrimitiveType;
1721}
1722PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1723{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001724 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001725 return mState.mGeometryShaderOutputPrimitiveType;
1726}
1727GLint Program::getGeometryShaderInvocations() const
1728{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001729 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001730 return mState.mGeometryShaderInvocations;
1731}
1732GLint Program::getGeometryShaderMaxVertices() const
1733{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001734 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001735 return mState.mGeometryShaderMaxVertices;
1736}
1737
jchen1015015f72017-03-16 13:54:21 +08001738GLuint Program::getInputResourceIndex(const GLchar *name) const
1739{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001740 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001741 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001742}
1743
1744GLuint Program::getOutputResourceIndex(const GLchar *name) const
1745{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001746 ASSERT(mLinkResolved);
jchen1015015f72017-03-16 13:54:21 +08001747 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1748}
1749
jchen10fd7c3b52017-03-21 15:36:03 +08001750size_t Program::getOutputResourceCount() const
1751{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001752 ASSERT(mLinkResolved);
jchen10fd7c3b52017-03-21 15:36:03 +08001753 return (mLinked ? mState.mOutputVariables.size() : 0);
1754}
1755
jchen107ae70d82018-07-06 13:47:01 +08001756const std::vector<GLenum> &Program::getOutputVariableTypes() const
1757{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001758 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001759 return mState.mOutputVariableTypes;
1760}
1761DrawBufferMask Program::getActiveOutputVariables() const
1762{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001763 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001764 return mState.mActiveOutputVariables;
1765}
1766
jchen10baf5d942017-08-28 20:45:48 +08001767template <typename T>
1768void Program::getResourceName(GLuint index,
1769 const std::vector<T> &resources,
1770 GLsizei bufSize,
1771 GLsizei *length,
1772 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001773{
1774 if (length)
1775 {
1776 *length = 0;
1777 }
1778
1779 if (!mLinked)
1780 {
1781 if (bufSize > 0)
1782 {
1783 name[0] = '\0';
1784 }
1785 return;
1786 }
jchen10baf5d942017-08-28 20:45:48 +08001787 ASSERT(index < resources.size());
1788 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001789
1790 if (bufSize > 0)
1791 {
Olli Etuahod2551232017-10-26 20:03:33 +03001792 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001793 }
1794}
1795
jchen10baf5d942017-08-28 20:45:48 +08001796void Program::getInputResourceName(GLuint index,
1797 GLsizei bufSize,
1798 GLsizei *length,
1799 GLchar *name) const
1800{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001801 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001802 getResourceName(index, mState.mAttributes, bufSize, length, name);
1803}
1804
1805void Program::getOutputResourceName(GLuint index,
1806 GLsizei bufSize,
1807 GLsizei *length,
1808 GLchar *name) const
1809{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001810 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001811 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1812}
1813
1814void Program::getUniformResourceName(GLuint index,
1815 GLsizei bufSize,
1816 GLsizei *length,
1817 GLchar *name) const
1818{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001819 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001820 getResourceName(index, mState.mUniforms, bufSize, length, name);
1821}
1822
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001823void Program::getBufferVariableResourceName(GLuint index,
1824 GLsizei bufSize,
1825 GLsizei *length,
1826 GLchar *name) const
1827{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001828 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001829 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1830}
1831
jchen10880683b2017-04-12 16:21:55 +08001832const sh::Attribute &Program::getInputResource(GLuint index) const
1833{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001834 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001835 ASSERT(index < mState.mAttributes.size());
1836 return mState.mAttributes[index];
1837}
1838
1839const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1840{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001841 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001842 ASSERT(index < mState.mOutputVariables.size());
1843 return mState.mOutputVariables[index];
1844}
1845
jchen107ae70d82018-07-06 13:47:01 +08001846const ProgramBindings &Program::getAttributeBindings() const
1847{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001848 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001849 return mAttributeBindings;
1850}
1851const ProgramBindings &Program::getUniformLocationBindings() const
1852{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001853 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001854 return mUniformLocationBindings;
1855}
1856const ProgramBindings &Program::getFragmentInputBindings() const
1857{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001858 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001859 return mFragmentInputBindings;
1860}
1861
1862int Program::getNumViews() const
1863{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001864 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001865 return mState.getNumViews();
1866}
jchen107ae70d82018-07-06 13:47:01 +08001867
1868ComponentTypeMask Program::getDrawBufferTypeMask() const
1869{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001870 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001871 return mState.mDrawBufferTypeMask;
1872}
1873ComponentTypeMask Program::getAttributesTypeMask() const
1874{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001875 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001876 return mState.mAttributesTypeMask;
1877}
1878AttributesMask Program::getAttributesMask() const
1879{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001880 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001881 return mState.mAttributesMask;
1882}
1883
1884const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1885{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001886 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001887 return mState.mTransformFeedbackStrides;
1888}
1889
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890GLint Program::getFragDataLocation(const std::string &name) const
1891{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001892 ASSERT(mLinkResolved);
Olli Etuaho0ca09752018-09-24 11:00:50 +03001893 GLint primaryLocation =
1894 GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
1895 if (primaryLocation != -1)
1896 {
1897 return primaryLocation;
1898 }
1899 return GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name);
1900}
1901
1902GLint Program::getFragDataIndex(const std::string &name) const
1903{
1904 ASSERT(mLinkResolved);
1905 if (GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name) != -1)
1906 {
1907 return 0;
1908 }
1909 if (GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name) != -1)
1910 {
1911 return 1;
1912 }
1913 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001914}
1915
Geoff Lange1a27752015-10-05 13:16:04 -04001916void Program::getActiveUniform(GLuint index,
1917 GLsizei bufsize,
1918 GLsizei *length,
1919 GLint *size,
1920 GLenum *type,
1921 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001922{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001923 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001925 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001927 ASSERT(index < mState.mUniforms.size());
1928 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929
1930 if (bufsize > 0)
1931 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001932 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001933 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 }
1935
Olli Etuaho465835d2017-09-26 13:34:10 +03001936 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001938 }
1939 else
1940 {
1941 if (bufsize > 0)
1942 {
1943 name[0] = '\0';
1944 }
1945
1946 if (length)
1947 {
1948 *length = 0;
1949 }
1950
1951 *size = 0;
1952 *type = GL_NONE;
1953 }
1954}
1955
Geoff Lange1a27752015-10-05 13:16:04 -04001956GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001957{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001958 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001960 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001961 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001962 }
1963 else
1964 {
1965 return 0;
1966 }
1967}
1968
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001969size_t Program::getActiveBufferVariableCount() const
1970{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001971 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001972 return mLinked ? mState.mBufferVariables.size() : 0;
1973}
1974
Geoff Lange1a27752015-10-05 13:16:04 -04001975GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001976{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001977 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979
1980 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001981 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001982 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001983 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001985 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001986 size_t length = uniform.name.length() + 1u;
1987 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 {
1989 length += 3; // Counting in "[0]".
1990 }
1991 maxLength = std::max(length, maxLength);
1992 }
1993 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001994 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001995
Jamie Madill62d31cb2015-09-11 13:25:51 -04001996 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997}
1998
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999bool Program::isValidUniformLocation(GLint location) const
2000{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002001 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04002002 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04002003 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04002004 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04002005}
2006
Jamie Madill62d31cb2015-09-11 13:25:51 -04002007const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002009 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002010 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05002011 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012}
2013
Jamie Madillac4e9c32017-01-13 14:07:12 -05002014const VariableLocation &Program::getUniformLocation(GLint location) const
2015{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002016 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05002017 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
2018 return mState.mUniformLocations[location];
2019}
2020
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002021const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
2022{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002023 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002024 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
2025 return mState.mBufferVariables[index];
2026}
2027
Jamie Madill62d31cb2015-09-11 13:25:51 -04002028GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002030 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002031 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032}
2033
Jamie Madill62d31cb2015-09-11 13:25:51 -04002034GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002036 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002037 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038}
2039
2040void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2041{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002042 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002043 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2044 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002045 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046}
2047
2048void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2049{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002050 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002051 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2052 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002053 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054}
2055
2056void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2057{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002058 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002059 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2060 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002061 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002062}
2063
2064void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2065{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002066 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002067 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2068 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002069 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070}
2071
Jamie Madill81c2e252017-09-09 23:32:46 -04002072Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002074 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002075 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2076 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2077
Jamie Madill81c2e252017-09-09 23:32:46 -04002078 mProgram->setUniform1iv(location, clampedCount, v);
2079
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002080 if (mState.isSamplerUniformIndex(locationInfo.index))
2081 {
2082 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002083 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002084 }
2085
Jamie Madill81c2e252017-09-09 23:32:46 -04002086 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087}
2088
2089void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2090{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002091 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002092 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2093 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002094 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095}
2096
2097void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2098{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002099 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002100 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2101 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002102 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103}
2104
2105void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2106{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002107 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002108 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2109 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002110 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111}
2112
2113void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2114{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002115 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002116 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2117 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002118 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119}
2120
2121void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2122{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002123 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002124 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2125 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002126 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127}
2128
2129void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2130{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002131 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002132 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2133 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002134 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135}
2136
2137void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2138{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002139 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002140 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2141 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002142 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143}
2144
Jamie Madill493f9572018-05-24 19:52:15 -04002145void Program::setUniformMatrix2fv(GLint location,
2146 GLsizei count,
2147 GLboolean transpose,
2148 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002150 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002151 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002152 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153}
2154
Jamie Madill493f9572018-05-24 19:52:15 -04002155void Program::setUniformMatrix3fv(GLint location,
2156 GLsizei count,
2157 GLboolean transpose,
2158 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002159{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002160 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002161 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002162 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163}
2164
Jamie Madill493f9572018-05-24 19:52:15 -04002165void Program::setUniformMatrix4fv(GLint location,
2166 GLsizei count,
2167 GLboolean transpose,
2168 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002170 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002171 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002172 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173}
2174
Jamie Madill493f9572018-05-24 19:52:15 -04002175void Program::setUniformMatrix2x3fv(GLint location,
2176 GLsizei count,
2177 GLboolean transpose,
2178 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002180 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002181 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002182 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183}
2184
Jamie Madill493f9572018-05-24 19:52:15 -04002185void Program::setUniformMatrix2x4fv(GLint location,
2186 GLsizei count,
2187 GLboolean transpose,
2188 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002190 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002191 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002192 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193}
2194
Jamie Madill493f9572018-05-24 19:52:15 -04002195void Program::setUniformMatrix3x2fv(GLint location,
2196 GLsizei count,
2197 GLboolean transpose,
2198 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002199{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002200 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002201 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002202 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203}
2204
Jamie Madill493f9572018-05-24 19:52:15 -04002205void Program::setUniformMatrix3x4fv(GLint location,
2206 GLsizei count,
2207 GLboolean transpose,
2208 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002210 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002211 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002212 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002213}
2214
Jamie Madill493f9572018-05-24 19:52:15 -04002215void Program::setUniformMatrix4x2fv(GLint location,
2216 GLsizei count,
2217 GLboolean transpose,
2218 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002219{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002220 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002221 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002222 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002223}
2224
Jamie Madill493f9572018-05-24 19:52:15 -04002225void Program::setUniformMatrix4x3fv(GLint location,
2226 GLsizei count,
2227 GLboolean transpose,
2228 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002229{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002230 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002231 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002232 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233}
2234
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002235GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2236{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002237 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002238 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2239 const std::vector<GLuint> &boundTextureUnits =
2240 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2241 return boundTextureUnits[uniformLocation.arrayIndex];
2242}
2243
Jamie Madill54164b02017-08-28 15:17:37 -04002244void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002246 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002247 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2248 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002249
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002250 if (uniform.isSampler())
2251 {
2252 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2253 return;
2254 }
2255
2256 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002257 if (nativeType == GL_FLOAT)
2258 {
2259 mProgram->getUniformfv(context, location, v);
2260 }
2261 else
2262 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002263 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002264 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265}
2266
Jamie Madill54164b02017-08-28 15:17:37 -04002267void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002269 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002270 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2271 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002272
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002273 if (uniform.isSampler())
2274 {
2275 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2276 return;
2277 }
2278
2279 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002280 if (nativeType == GL_INT || nativeType == GL_BOOL)
2281 {
2282 mProgram->getUniformiv(context, location, v);
2283 }
2284 else
2285 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002286 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002287 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002288}
2289
Jamie Madill54164b02017-08-28 15:17:37 -04002290void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002291{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002292 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002293 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2294 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002295
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002296 if (uniform.isSampler())
2297 {
2298 *v = getSamplerUniformBinding(uniformLocation);
2299 return;
2300 }
2301
2302 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002303 if (nativeType == GL_UNSIGNED_INT)
2304 {
2305 mProgram->getUniformuiv(context, location, v);
2306 }
2307 else
2308 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002309 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002310 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002311}
2312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313void Program::flagForDeletion()
2314{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002315 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 mDeleteStatus = true;
2317}
2318
2319bool Program::isFlaggedForDeletion() const
2320{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002321 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 return mDeleteStatus;
2323}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002324
Brandon Jones43a53e22014-08-28 16:23:22 -07002325void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002326{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002327 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002328 mInfoLog.reset();
2329
Geoff Lang7dd2e102014-11-10 15:19:26 -05002330 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002331 {
Geoff Lang92019432017-11-20 13:09:34 -05002332 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002333 }
2334 else
2335 {
Jamie Madillf6113162015-05-07 11:49:21 -04002336 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002337 }
2338}
2339
Jamie Madillcc73f242018-08-01 11:34:48 -04002340bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002341{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002342 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002343
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002344 // if any two active samplers in a program are of different types, but refer to the same
2345 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2346 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002347 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002348 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002349 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002350 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002351 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002352 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002353 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2354 "image unit ("
2355 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002356 }
2357
Jamie Madill7e4eff12018-08-08 15:49:26 -04002358 mCachedValidateSamplersResult = false;
2359 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002360 }
2361 }
2362
2363 mCachedValidateSamplersResult = true;
2364 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365}
2366
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002367bool Program::isValidated() const
2368{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002369 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370 return mValidated;
2371}
2372
jchen1058f67be2017-10-27 08:59:27 +08002373GLuint Program::getActiveAtomicCounterBufferCount() const
2374{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002375 ASSERT(mLinkResolved);
jchen1058f67be2017-10-27 08:59:27 +08002376 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2377}
2378
Jiajia Qin729b2c62017-08-14 09:36:11 +08002379GLuint Program::getActiveShaderStorageBlockCount() const
2380{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002381 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002382 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2383}
2384
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002385void Program::getActiveUniformBlockName(const GLuint blockIndex,
2386 GLsizei bufSize,
2387 GLsizei *length,
2388 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002389{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002390 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002391 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2392}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002394void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2395 GLsizei bufSize,
2396 GLsizei *length,
2397 GLchar *blockName) const
2398{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002399 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002400 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002401}
2402
Qin Jiajia9bf55522018-01-29 13:56:23 +08002403template <typename T>
2404GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002405{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002406 int maxLength = 0;
2407
2408 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002409 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002410 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002412 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002413 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002414 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002415 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002416 }
2417 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002418 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002419
2420 return maxLength;
2421}
2422
Qin Jiajia9bf55522018-01-29 13:56:23 +08002423GLint Program::getActiveUniformBlockMaxNameLength() const
2424{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002425 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002426 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2427}
2428
2429GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2430{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002431 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002432 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2433}
2434
Geoff Lange1a27752015-10-05 13:16:04 -04002435GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002436{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002437 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002438 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2439}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002440
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002441GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2442{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002443 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002444 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002445}
2446
Jiajia Qin729b2c62017-08-14 09:36:11 +08002447const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002448{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002449 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002450 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2451 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002452}
2453
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002454const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2455{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002456 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002457 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2458 return mState.mShaderStorageBlocks[index];
2459}
2460
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002461void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2462{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002463 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002464 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002465 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002466 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002467}
2468
2469GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2470{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002471 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002472 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002473}
2474
Jiajia Qin729b2c62017-08-14 09:36:11 +08002475GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2476{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002477 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002478 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2479}
2480
Jamie Madill493f9572018-05-24 19:52:15 -04002481void Program::setTransformFeedbackVaryings(GLsizei count,
2482 const GLchar *const *varyings,
2483 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002484{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002485 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002486 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002487 for (GLsizei i = 0; i < count; i++)
2488 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002489 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002490 }
2491
Jamie Madill48ef11b2016-04-27 15:21:52 -04002492 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002493}
2494
Jamie Madill493f9572018-05-24 19:52:15 -04002495void Program::getTransformFeedbackVarying(GLuint index,
2496 GLsizei bufSize,
2497 GLsizei *length,
2498 GLsizei *size,
2499 GLenum *type,
2500 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002501{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002502 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002503 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002504 {
jchen10a9042d32017-03-17 08:50:45 +08002505 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2506 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2507 std::string varName = var.nameWithArrayIndex();
2508 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002509 if (length)
2510 {
2511 *length = lastNameIdx;
2512 }
2513 if (size)
2514 {
jchen10a9042d32017-03-17 08:50:45 +08002515 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002516 }
2517 if (type)
2518 {
jchen10a9042d32017-03-17 08:50:45 +08002519 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002520 }
2521 if (name)
2522 {
jchen10a9042d32017-03-17 08:50:45 +08002523 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002524 name[lastNameIdx] = '\0';
2525 }
2526 }
2527}
2528
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002529GLsizei Program::getTransformFeedbackVaryingCount() const
2530{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002531 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002532 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002533 {
jchen10a9042d32017-03-17 08:50:45 +08002534 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002535 }
2536 else
2537 {
2538 return 0;
2539 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002540}
2541
2542GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2543{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002544 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002545 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002546 {
2547 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002548 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002549 {
jchen10a9042d32017-03-17 08:50:45 +08002550 maxSize =
2551 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002552 }
2553
2554 return maxSize;
2555 }
2556 else
2557 {
2558 return 0;
2559 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002560}
2561
2562GLenum Program::getTransformFeedbackBufferMode() const
2563{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002564 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002565 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002566}
2567
jchen103fd614d2018-08-13 12:21:58 +08002568bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002569{
Jiawei Shao016105b2018-04-12 16:38:31 +08002570 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2571 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2572 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2573 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002574
Jamie Madill493f9572018-05-24 19:52:15 -04002575 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002576 bool isGraphicsShaderAttached =
2577 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002578 // Check whether we both have a compute and non-compute shaders attached.
2579 // If there are of both types attached, then linking should fail.
2580 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2581 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2582 {
2583 infoLog << "Both compute and graphics shaders are attached to the same program.";
2584 return false;
2585 }
2586
2587 if (computeShader)
2588 {
jchen103fd614d2018-08-13 12:21:58 +08002589 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002590 {
2591 infoLog << "Attached compute shader is not compiled.";
2592 return false;
2593 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002594 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002595
jchen103fd614d2018-08-13 12:21:58 +08002596 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002597
2598 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2599 // If the work group size is not specified, a link time error should occur.
2600 if (!mState.mComputeShaderLocalSize.isDeclared())
2601 {
2602 infoLog << "Work group size is not specified.";
2603 return false;
2604 }
2605 }
2606 else
2607 {
jchen103fd614d2018-08-13 12:21:58 +08002608 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002609 {
2610 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2611 return false;
2612 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002613 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002614
jchen103fd614d2018-08-13 12:21:58 +08002615 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002616 {
2617 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2618 return false;
2619 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002620 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002621
jchen103fd614d2018-08-13 12:21:58 +08002622 int vertexShaderVersion = vertexShader->getShaderVersion();
2623 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002624 {
2625 infoLog << "Fragment shader version does not match vertex shader version.";
2626 return false;
2627 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002628
2629 if (geometryShader)
2630 {
2631 // [GL_EXT_geometry_shader] Chapter 7
2632 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2633 // Language Specification, as well as any of the following reasons:
2634 // * One or more of the shader objects attached to <program> are not compiled
2635 // successfully.
2636 // * The shaders do not use the same shader language version.
2637 // * <program> contains objects to form a geometry shader, and
2638 // - <program> is not separable and contains no objects to form a vertex shader; or
2639 // - the input primitive type, output primitive type, or maximum output vertex count
2640 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002641 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002642 {
2643 infoLog << "The attached geometry shader isn't compiled.";
2644 return false;
2645 }
2646
jchen103fd614d2018-08-13 12:21:58 +08002647 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002648 {
2649 mInfoLog << "Geometry shader version does not match vertex shader version.";
2650 return false;
2651 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002652 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002653
Jamie Madill493f9572018-05-24 19:52:15 -04002654 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002655 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002656 if (!inputPrimitive.valid())
2657 {
2658 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2659 return false;
2660 }
2661
Jamie Madill493f9572018-05-24 19:52:15 -04002662 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002663 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002664 if (!outputPrimitive.valid())
2665 {
2666 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2667 return false;
2668 }
2669
jchen103fd614d2018-08-13 12:21:58 +08002670 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002671 if (!maxVertices.valid())
2672 {
2673 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2674 return false;
2675 }
2676
2677 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2678 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2679 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002680 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002681 }
Jiawei Shao73618602017-12-20 15:47:15 +08002682 }
2683
2684 return true;
2685}
2686
jchen10910a3da2017-11-15 09:40:11 +08002687GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2688{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002689 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002690 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2691 {
2692 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2693 if (tf.nameWithArrayIndex() == name)
2694 {
2695 return tfIndex;
2696 }
2697 }
2698 return GL_INVALID_INDEX;
2699}
2700
2701const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2702{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002703 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002704 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2705 return mState.mLinkedTransformFeedbackVaryings[index];
2706}
2707
jchen103fd614d2018-08-13 12:21:58 +08002708bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002709{
Jiawei Shao016105b2018-04-12 16:38:31 +08002710 Shader *previousShader = nullptr;
2711 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002712 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002713 Shader *currentShader = mState.mAttachedShaders[shaderType];
2714 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002715 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002716 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002717 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002718
2719 if (previousShader)
2720 {
jchen103fd614d2018-08-13 12:21:58 +08002721 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002722 {
2723 return false;
2724 }
2725 }
2726 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002727 }
2728
jchen103fd614d2018-08-13 12:21:58 +08002729 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002730 {
2731 return false;
2732 }
2733
jchen103fd614d2018-08-13 12:21:58 +08002734 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002735 {
2736 return false;
2737 }
2738
2739 return true;
2740}
2741
2742// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2743// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002744bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002745 gl::Shader *consumingShader,
2746 gl::InfoLog &infoLog) const
2747{
jchen103fd614d2018-08-13 12:21:58 +08002748 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002749
jchen103fd614d2018-08-13 12:21:58 +08002750 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2751 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002752
Jiawei Shao385b3e02018-03-21 09:43:28 +08002753 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002754
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002755 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002756 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002757 bool matched = false;
2758
2759 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002760 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002761 {
2762 continue;
2763 }
2764
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002765 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002766 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002767 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002768 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002769 ASSERT(!output.isBuiltIn());
2770
2771 std::string mismatchedStructFieldName;
2772 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002773 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002774 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002775 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002776 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002777 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2778 mismatchedStructFieldName, generatingShader->getType(),
2779 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002780 return false;
2781 }
2782
Geoff Lang7dd2e102014-11-10 15:19:26 -05002783 matched = true;
2784 break;
2785 }
2786 }
2787
Olli Etuaho107c7242018-03-20 15:45:35 +02002788 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2789 // whether the input is statically used - a statically used input should fail this test even
2790 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002791 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002792 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002793 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2794 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2795 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002796 return false;
2797 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002798 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002799
Jiawei Shaod063aff2018-02-22 10:19:09 +08002800 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002801
Jiawei Shaod063aff2018-02-22 10:19:09 +08002802 return true;
2803}
2804
jchen103fd614d2018-08-13 12:21:58 +08002805bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002806{
Jiawei Shao016105b2018-04-12 16:38:31 +08002807 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002808
2809 std::map<GLuint, std::string> staticFragmentInputLocations;
2810
2811 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002812 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002813 for (const sh::Varying &input : fragmentInputVaryings)
2814 {
2815 if (input.isBuiltIn() || !input.staticUse)
2816 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002817 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002818 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002819
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002820 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002821 if (inputBinding == -1)
2822 continue;
2823
2824 const auto it = staticFragmentInputLocations.find(inputBinding);
2825 if (it == std::end(staticFragmentInputLocations))
2826 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002827 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002828 }
2829 else
2830 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002831 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002832 << it->second;
2833 return false;
2834 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002835 }
2836
2837 return true;
2838}
2839
jchen103fd614d2018-08-13 12:21:58 +08002840bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002841 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002842 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002843 GLuint *combinedImageUniformsCount,
2844 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002845{
Olli Etuahob78707c2017-03-09 15:03:11 +00002846 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002847 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002848 {
2849 return false;
2850 }
2851
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002852 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002853
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002854 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002855
jchen10eaef1e52017-06-13 10:44:11 +08002856 if (!linkAtomicCounterBuffers())
2857 {
2858 return false;
2859 }
2860
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002861 return true;
2862}
2863
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002864void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002865{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002866 ASSERT(combinedImageUniforms);
2867
Jamie Madill982f6e02017-06-07 14:33:04 -04002868 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2869 unsigned int low = high;
2870
jchen10eaef1e52017-06-13 10:44:11 +08002871 for (auto counterIter = mState.mUniforms.rbegin();
2872 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2873 {
2874 --low;
2875 }
2876
2877 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2878
2879 high = low;
2880
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002881 for (auto imageIter = mState.mUniforms.rbegin();
2882 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2883 {
2884 --low;
2885 }
2886
2887 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002888 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002889 // If uniform is a image type, insert it into the mImageBindings array.
2890 for (unsigned int imageIndex : mState.mImageUniformRange)
2891 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002892 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2893 // cannot load values into a uniform defined as an image. if declare without a
2894 // binding qualifier, any uniform image variable (include all elements of
2895 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002896 auto &imageUniform = mState.mUniforms[imageIndex];
2897 if (imageUniform.binding == -1)
2898 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002899 mState.mImageBindings.emplace_back(
2900 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002901 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002902 else
2903 {
2904 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002905 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002906 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002907
2908 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2909 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002910 }
2911
2912 high = low;
2913
2914 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002915 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002916 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002917 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002918 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002919
2920 mState.mSamplerUniformRange = RangeUI(low, high);
2921
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002922 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002923 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002924 {
2925 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002926 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002927 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2928 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002929 }
2930}
2931
jchen10eaef1e52017-06-13 10:44:11 +08002932bool Program::linkAtomicCounterBuffers()
2933{
2934 for (unsigned int index : mState.mAtomicCounterUniformRange)
2935 {
Jamie Madill493f9572018-05-24 19:52:15 -04002936 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002937 uniform.blockInfo.offset = uniform.offset;
2938 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2939 uniform.blockInfo.matrixStride = 0;
2940 uniform.blockInfo.isRowMajorMatrix = false;
2941
Jamie Madill493f9572018-05-24 19:52:15 -04002942 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002943 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2944 ++bufferIndex)
2945 {
2946 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2947 if (buffer.binding == uniform.binding)
2948 {
2949 buffer.memberIndexes.push_back(index);
2950 uniform.bufferIndex = bufferIndex;
2951 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002952 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002953 break;
2954 }
2955 }
2956 if (!found)
2957 {
2958 AtomicCounterBuffer atomicCounterBuffer;
2959 atomicCounterBuffer.binding = uniform.binding;
2960 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002961 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002962 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2963 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2964 }
2965 }
2966 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002967 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002968
2969 return true;
2970}
2971
Jamie Madilleb979bf2016-11-15 12:28:46 -05002972// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002973bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002974{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002975 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002976
jchen103fd614d2018-08-13 12:21:58 +08002977 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002978
Geoff Lang7dd2e102014-11-10 15:19:26 -05002979 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002980 if (shaderVersion >= 300)
2981 {
2982 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2983 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002984 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002985 }
2986 else
2987 {
2988 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002989 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002990 }
jchen103fd614d2018-08-13 12:21:58 +08002991 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002992
2993 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002994 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002995 {
Jamie Madillf6113162015-05-07 11:49:21 -04002996 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002997 return false;
2998 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002999
Jamie Madilldfde6ab2016-06-09 07:07:18 -07003000 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00003001
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003002 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003003 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04003004 {
Olli Etuahod2551232017-10-26 20:03:33 +03003005 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
3006 // structures, so we don't need to worry about adjusting their names or generating entries
3007 // for each member/element (unlike uniforms for example).
3008 ASSERT(!attribute.isArray() && !attribute.isStruct());
3009
Jamie Madilleb979bf2016-11-15 12:28:46 -05003010 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04003011 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04003012 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003013 attribute.location = bindingLocation;
3014 }
3015
3016 if (attribute.location != -1)
3017 {
3018 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04003019 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003020
Jamie Madill63805b42015-08-25 13:17:39 -04003021 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003022 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003023 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3024 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003025
3026 return false;
3027 }
3028
Jamie Madill63805b42015-08-25 13:17:39 -04003029 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003030 {
Jamie Madill63805b42015-08-25 13:17:39 -04003031 const int regLocation = attribute.location + reg;
3032 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003033
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003034 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3035 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3036 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003037 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003038 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003039 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003040 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003041 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003042 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003043 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003044 return false;
3045 }
3046 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003047 else
3048 {
Jamie Madill63805b42015-08-25 13:17:39 -04003049 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003050 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003051
Jamie Madill63805b42015-08-25 13:17:39 -04003052 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003053 }
3054 }
3055 }
3056
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003057 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003058 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003059 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003060 // Not set by glBindAttribLocation or by location layout qualifier
3061 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003062 {
Jamie Madill63805b42015-08-25 13:17:39 -04003063 int regs = VariableRegisterCount(attribute.type);
3064 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003065
Jamie Madill63805b42015-08-25 13:17:39 -04003066 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003067 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003068 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003069 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003070 }
3071
Jamie Madillc349ec02015-08-21 16:53:12 -04003072 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003073 }
3074 }
3075
Brandon Jonesc405ae72017-12-06 14:15:03 -08003076 ASSERT(mState.mAttributesTypeMask.none());
3077 ASSERT(mState.mAttributesMask.none());
3078
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003079 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3080 // shader versions we're only processing active attributes to begin with.
3081 if (shaderVersion >= 300)
3082 {
3083 for (auto attributeIter = mState.mAttributes.begin();
3084 attributeIter != mState.mAttributes.end();)
3085 {
3086 if (attributeIter->active)
3087 {
3088 ++attributeIter;
3089 }
3090 else
3091 {
3092 attributeIter = mState.mAttributes.erase(attributeIter);
3093 }
3094 }
3095 }
3096
Jamie Madill48ef11b2016-04-27 15:21:52 -04003097 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003098 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003099 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003100 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003101 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003102
Jamie Madillbd159f02017-10-09 19:39:06 -04003103 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003104 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003105 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3106 mState.mActiveAttribLocationsMask.set(location);
3107 mState.mMaxActiveAttribLocation =
3108 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003109
3110 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3111 if (!attribute.isBuiltIn())
3112 {
3113 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3114 location);
3115 mState.mAttributesMask.set(location);
3116 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003117 }
3118 }
3119
Geoff Lang7dd2e102014-11-10 15:19:26 -05003120 return true;
3121}
3122
jchen103fd614d2018-08-13 12:21:58 +08003123bool Program::linkInterfaceBlocks(const Caps &caps,
3124 const Version &version,
3125 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003126 InfoLog &infoLog,
3127 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003128{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003129 ASSERT(combinedShaderStorageBlocksCount);
3130
Jiawei Shao54aafe52018-04-27 14:54:57 +08003131 GLuint combinedUniformBlocksCount = 0u;
3132 GLuint numShadersHasUniformBlocks = 0u;
3133 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3134 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003135 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003136 Shader *shader = mState.mAttachedShaders[shaderType];
3137 if (!shader)
3138 {
3139 continue;
3140 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003141
jchen103fd614d2018-08-13 12:21:58 +08003142 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003143 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003144 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003145 if (!ValidateInterfaceBlocksCount(
3146 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3147 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3148 {
3149 return false;
3150 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003151
Jiawei Shao54aafe52018-04-27 14:54:57 +08003152 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3153 ++numShadersHasUniformBlocks;
3154 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003155 }
3156
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003157 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3158 {
3159 infoLog << "The sum of the number of active uniform blocks exceeds "
3160 "MAX_COMBINED_UNIFORM_BLOCKS ("
3161 << caps.maxCombinedUniformBlocks << ").";
3162 return false;
3163 }
3164
Jiawei Shao54aafe52018-04-27 14:54:57 +08003165 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3166 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003167 {
3168 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003169 }
Jamie Madille473dee2015-08-18 14:49:01 -04003170
jchen103fd614d2018-08-13 12:21:58 +08003171 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003172 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003173 *combinedShaderStorageBlocksCount = 0u;
3174 GLuint numShadersHasShaderStorageBlocks = 0u;
3175 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3176 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003177 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003178 Shader *shader = mState.mAttachedShaders[shaderType];
3179 if (!shader)
3180 {
3181 continue;
3182 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003183
jchen103fd614d2018-08-13 12:21:58 +08003184 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003185 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003186 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003187 if (!ValidateInterfaceBlocksCount(
3188 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3189 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3190 {
3191 return false;
3192 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003193
Jiawei Shao54aafe52018-04-27 14:54:57 +08003194 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3195 ++numShadersHasShaderStorageBlocks;
3196 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003197 }
3198
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003199 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003200 {
3201 infoLog << "The sum of the number of active shader storage blocks exceeds "
3202 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3203 << caps.maxCombinedShaderStorageBlocks << ").";
3204 return false;
3205 }
3206
Jiawei Shao54aafe52018-04-27 14:54:57 +08003207 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3208 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003209 {
3210 return false;
3211 }
3212 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003213
Geoff Lang7dd2e102014-11-10 15:19:26 -05003214 return true;
3215}
3216
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003217LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3218 const sh::ShaderVariable &variable2,
3219 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003220 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003221 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003222{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003223 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003224 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003225 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003226 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003227 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003228 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003229 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003230 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003231 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003232 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003233 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003234 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003235 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003236 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003237 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003238 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003239
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003240 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003241 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003242 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003243 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003244 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003245 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3246 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003247 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3248 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003249
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003250 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003251 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003252 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003253 }
3254
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003255 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003256 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003257 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003258 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003259 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3260 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003261 }
3262 }
3263
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003264 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003265}
3266
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003267LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3268 const sh::Varying &inputVarying,
3269 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003270 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003271 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003272{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003273 if (validateGeometryShaderInputVarying)
3274 {
3275 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3276 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3277 // or outputs.
3278 ASSERT(inputVarying.arraySizes.size() == 1u);
3279
3280 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3281 // varying cannot match a geometry shader input varying.
3282 // [GL_EXT_geometry_shader] Section 7.4.1:
3283 // Geometry shader per-vertex input variables and blocks are required to be declared as
3284 // arrays, with each element representing input or output values for a single vertex of a
3285 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3286 // are treated as though they were not declared as arrays.
3287 if (outputVarying.isArray())
3288 {
3289 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3290 }
3291 }
3292
3293 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3294 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003295 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003296 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3297 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003298 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003299 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003300 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003301 }
3302
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003303 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003304 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003305 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003306 }
3307
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003308 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003309 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003310 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003311 }
3312
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003313 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003314}
3315
jchen103fd614d2018-08-13 12:21:58 +08003316bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003317{
Jiawei Shao016105b2018-04-12 16:38:31 +08003318 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3319 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003320 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3321 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3322 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003323
3324 if (shaderVersion != 100)
3325 {
3326 // Only ESSL 1.0 has restrictions on matching input and output invariance
3327 return true;
3328 }
3329
3330 bool glPositionIsInvariant = false;
3331 bool glPointSizeIsInvariant = false;
3332 bool glFragCoordIsInvariant = false;
3333 bool glPointCoordIsInvariant = false;
3334
3335 for (const sh::Varying &varying : vertexVaryings)
3336 {
3337 if (!varying.isBuiltIn())
3338 {
3339 continue;
3340 }
3341 if (varying.name.compare("gl_Position") == 0)
3342 {
3343 glPositionIsInvariant = varying.isInvariant;
3344 }
3345 else if (varying.name.compare("gl_PointSize") == 0)
3346 {
3347 glPointSizeIsInvariant = varying.isInvariant;
3348 }
3349 }
3350
3351 for (const sh::Varying &varying : fragmentVaryings)
3352 {
3353 if (!varying.isBuiltIn())
3354 {
3355 continue;
3356 }
3357 if (varying.name.compare("gl_FragCoord") == 0)
3358 {
3359 glFragCoordIsInvariant = varying.isInvariant;
3360 }
3361 else if (varying.name.compare("gl_PointCoord") == 0)
3362 {
3363 glPointCoordIsInvariant = varying.isInvariant;
3364 }
3365 }
3366
3367 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3368 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3369 // Not requiring invariance to match is supported by:
3370 // dEQP, WebGL CTS, Nexus 5X GLES
3371 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3372 {
3373 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3374 "declared invariant.";
3375 return false;
3376 }
3377 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3378 {
3379 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3380 "declared invariant.";
3381 return false;
3382 }
3383
3384 return true;
3385}
3386
jchen103fd614d2018-08-13 12:21:58 +08003387bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003388 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003389 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003390 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003391{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003392
jchen108225e732017-11-14 16:29:03 +08003393 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003394 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003395 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003396 {
jchen103fd614d2018-08-13 12:21:58 +08003397 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003398 {
Geoff Lang1a683462015-09-29 15:09:59 -04003399 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003400 return false;
3401 }
jchen103fd614d2018-08-13 12:21:58 +08003402 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003403 {
3404 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3405 {
3406 infoLog << "Two transform feedback varyings include the same array element ("
3407 << tfVaryingName << ").";
3408 return false;
3409 }
3410 }
3411 else
3412 {
3413 if (uniqueNames.count(tfVaryingName) > 0)
3414 {
3415 infoLog << "Two transform feedback varyings specify the same output variable ("
3416 << tfVaryingName << ").";
3417 return false;
3418 }
3419 }
3420 uniqueNames.insert(tfVaryingName);
3421 }
3422
3423 // Validate against program varyings.
3424 size_t totalComponents = 0;
3425 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3426 {
3427 std::vector<unsigned int> subscripts;
3428 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3429
3430 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3431 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003432 {
3433 infoLog << "Transform feedback varying " << tfVaryingName
3434 << " does not exist in the vertex shader.";
3435 return false;
3436 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003437
jchen108225e732017-11-14 16:29:03 +08003438 // Validate the matching variable.
3439 if (var->isStruct())
3440 {
3441 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3442 return false;
3443 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003444
jchen108225e732017-11-14 16:29:03 +08003445 size_t elementCount = 0;
3446 size_t componentCount = 0;
3447
3448 if (var->isArray())
3449 {
jchen103fd614d2018-08-13 12:21:58 +08003450 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003451 {
3452 infoLog << "Capture of arrays is undefined and not supported.";
3453 return false;
3454 }
3455
3456 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3457 ASSERT(!var->isArrayOfArrays());
3458
3459 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3460 {
3461 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3462 return false;
3463 }
3464 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3465 }
3466 else
3467 {
3468 if (!subscripts.empty())
3469 {
3470 infoLog << "Varying '" << baseName
3471 << "' is not an array to be captured by element.";
3472 return false;
3473 }
3474 elementCount = 1;
3475 }
3476
3477 // TODO(jmadill): Investigate implementation limits on D3D11
3478 componentCount = VariableComponentCount(var->type) * elementCount;
3479 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3480 componentCount > caps.maxTransformFeedbackSeparateComponents)
3481 {
3482 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3483 << componentCount << ") exceed the maximum separate components ("
3484 << caps.maxTransformFeedbackSeparateComponents << ").";
3485 return false;
3486 }
3487
3488 totalComponents += componentCount;
3489 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3490 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3491 {
3492 infoLog << "Transform feedback varying total components (" << totalComponents
3493 << ") exceed the maximum interleaved components ("
3494 << caps.maxTransformFeedbackInterleavedComponents << ").";
3495 return false;
3496 }
3497 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003498 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003499}
3500
jchen103fd614d2018-08-13 12:21:58 +08003501bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003502{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003503 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003504 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003505
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003506 for (const auto &attrib : attributes)
3507 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003508 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003509 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003510 Shader *shader = mState.mAttachedShaders[shaderType];
3511 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003512 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003513 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003514 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003515
jchen103fd614d2018-08-13 12:21:58 +08003516 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003517 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003518 {
3519 if (uniform.name == attrib.name)
3520 {
3521 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3522 return false;
3523 }
3524 }
3525 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003526 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003527
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003528 return true;
3529}
3530
Jamie Madill3c1da042017-11-27 18:33:40 -05003531void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003532{
3533 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003534 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003535 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003536 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003537 std::vector<unsigned int> subscripts;
3538 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003539 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003540 if (!subscripts.empty())
3541 {
3542 subscript = subscripts.back();
3543 }
Jamie Madill192745a2016-12-22 15:58:21 -05003544 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003545 {
Jamie Madill192745a2016-12-22 15:58:21 -05003546 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003547 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003548 {
jchen10a9042d32017-03-17 08:50:45 +08003549 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3550 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003551 break;
3552 }
jchen108225e732017-11-14 16:29:03 +08003553 else if (varying->isStruct())
3554 {
3555 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3556 if (field != nullptr)
3557 {
3558 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3559 break;
3560 }
3561 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003562 }
3563 }
James Darpinian30b604d2018-03-12 17:26:57 -07003564 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003565}
3566
jchen103fd614d2018-08-13 12:21:58 +08003567ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003568{
Jamie Madill3c1da042017-11-27 18:33:40 -05003569 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003570
Jiawei Shao016105b2018-04-12 16:38:31 +08003571 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003572 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003573 {
Jamie Madill192745a2016-12-22 15:58:21 -05003574 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003575 }
3576
Jiawei Shao016105b2018-04-12 16:38:31 +08003577 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003578 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003579 {
Jamie Madill192745a2016-12-22 15:58:21 -05003580 merged[varying.name].fragment = &varying;
3581 }
3582
3583 return merged;
3584}
3585
Olli Etuaho0ca09752018-09-24 11:00:50 +03003586bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3587{
3588 return a.getArraySizeProduct() > b.getArraySizeProduct();
3589}
3590
3591int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3592{
3593 if (outputVariable.location != -1)
3594 {
3595 return outputVariable.location;
3596 }
3597 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3598 if (apiLocation != -1)
3599 {
3600 return apiLocation;
3601 }
3602 return -1;
3603}
3604
3605bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3606{
3607 if (outputVariable.index != -1)
3608 {
3609 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3610 return (outputVariable.index == 1);
3611 }
3612 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3613 if (apiIndex != -1)
3614 {
3615 // Index layout qualifier from the shader takes precedence, so the index from the API is
3616 // checked only if the index was not set in the shader. This is not specified in the EXT
3617 // spec, but is specified in desktop OpenGL specs.
3618 return (apiIndex == 1);
3619 }
3620 // EXT_blend_func_extended: Outputs get index 0 by default.
3621 return false;
3622}
3623
jchen103fd614d2018-08-13 12:21:58 +08003624bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003625 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003626 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003627 GLuint combinedImageUniformsCount,
3628 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003629{
Jiawei Shao016105b2018-04-12 16:38:31 +08003630 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003631 ASSERT(fragmentShader != nullptr);
3632
Geoff Lange0cff192017-05-30 13:04:56 -04003633 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003634 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003635 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003636
jchen103fd614d2018-08-13 12:21:58 +08003637 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003638 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003639 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003640 {
3641 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3642 outputVariable.name != "gl_FragData")
3643 {
3644 continue;
3645 }
3646
3647 unsigned int baseLocation =
3648 (outputVariable.location == -1 ? 0u
3649 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003650
3651 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3652 // structures, so we may use getBasicTypeElementCount().
3653 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3654 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003655 {
3656 const unsigned int location = baseLocation + elementIndex;
3657 if (location >= mState.mOutputVariableTypes.size())
3658 {
3659 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3660 }
Corentin Walleze7557742017-06-01 13:09:57 -04003661 ASSERT(location < mState.mActiveOutputVariables.size());
3662 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003663 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003664 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003665 }
3666 }
3667
jchen103fd614d2018-08-13 12:21:58 +08003668 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003669 {
3670 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3671 // A link error will be generated if the sum of the number of active image uniforms used in
3672 // all shaders, the number of active shader storage blocks, and the number of active
3673 // fragment shader outputs exceeds the implementation-dependent value of
3674 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3675 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3676 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003677 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003678 {
3679 mInfoLog
3680 << "The sum of the number of active image uniforms, active shader storage blocks "
3681 "and active fragment shader outputs exceeds "
3682 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003683 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003684 return false;
3685 }
3686 }
3687
Jamie Madill80a6fc02015-08-21 16:53:16 -04003688 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003689 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003690 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003691
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003692 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003693 // TODO(jmadill): any caps validation here?
3694
Olli Etuaho0ca09752018-09-24 11:00:50 +03003695 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3696 {
3697 if (outputVariable.isArray())
3698 {
3699 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3700 // Resources and including [0] at the end of array variable names.
3701 outputVariable.name += "[0]";
3702 outputVariable.mappedName += "[0]";
3703 }
3704 }
3705
3706 bool hasSecondaryOutputs = false;
3707
3708 // Reserve locations for output variables whose location is fixed in the shader or through the
3709 // API.
jchen1015015f72017-03-16 13:54:21 +08003710 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003711 outputVariableIndex++)
3712 {
jchen1015015f72017-03-16 13:54:21 +08003713 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003714
3715 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3716 if (outputVariable.isBuiltIn())
3717 continue;
3718
Olli Etuaho0ca09752018-09-24 11:00:50 +03003719 int baseLocation = getOutputLocationForLink(outputVariable);
3720 if (baseLocation == -1)
3721 {
3722 // Here we're only reserving locations for variables whose location is fixed.
3723 continue;
3724 }
3725
3726 auto *outputLocations = &mState.mOutputLocations;
3727 if (isOutputSecondaryForLink(outputVariable))
3728 {
3729 outputLocations = &mState.mSecondaryOutputLocations;
3730 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3731 // an output has an index specified it will always also have the location specified.
3732 hasSecondaryOutputs = true;
3733 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003734
Olli Etuaho465835d2017-09-26 13:34:10 +03003735 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3736 // structures, so we may use getBasicTypeElementCount().
3737 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003738 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3739 if (outputLocationsNeeded > outputLocations->size())
3740 {
3741 outputLocations->resize(outputLocationsNeeded);
3742 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003743 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003744 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003745 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3746 ASSERT(location < outputLocations->size());
3747 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003748 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003749 mInfoLog << "Location of variable " << outputVariable.name
3750 << " conflicts with another variable.";
3751 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003752 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003753 if (outputVariable.isArray())
3754 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003755 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003756 }
3757 else
3758 {
3759 VariableLocation locationInfo;
3760 locationInfo.index = outputVariableIndex;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003761 (*outputLocations)[location] = locationInfo;
3762 }
3763 }
3764 }
3765
3766 // Here we assign locations for the output variables that don't yet have them. Note that we're
3767 // not necessarily able to fit the variables optimally, since then we might have to try
3768 // different arrangements of output arrays. Now we just assign the locations in the order that
3769 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3770 // finding locations for the output variables, so this should be acceptable at least for now.
3771 GLuint maxLocation = caps.maxDrawBuffers;
3772 if (hasSecondaryOutputs)
3773 {
3774 // EXT_blend_func_extended: Program outputs will be validated against
3775 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3776 maxLocation = extensions.maxDualSourceDrawBuffers;
3777 }
3778
3779 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3780 outputVariableIndex++)
3781 {
3782 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3783
3784 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3785 if (outputVariable.isBuiltIn())
3786 continue;
3787
3788 if (getOutputLocationForLink(outputVariable) != -1)
3789 {
3790 continue;
3791 }
3792
3793 auto *outputLocations = &mState.mOutputLocations;
3794 if (isOutputSecondaryForLink(outputVariable))
3795 {
3796 outputLocations = &mState.mSecondaryOutputLocations;
3797 }
3798
3799 int baseLocation = 0;
3800 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3801 bool elementsFit = false;
3802 while (!elementsFit)
3803 {
3804 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3805 elementsFit = true;
3806 if (baseLocation + elementCount > maxLocation)
3807 {
3808 // EXT_blend_func_extended: Linking can fail:
3809 // "if the explicit binding assignments do not leave enough space for the linker to
3810 // automatically assign a location for a varying out array, which requires multiple
3811 // contiguous locations."
3812 mInfoLog << "Could not fit output variable into available locations: "
3813 << outputVariable.name;
3814 return false;
3815 }
3816 unsigned int outputLocationsNeeded =
3817 static_cast<unsigned int>(baseLocation) + elementCount;
3818 if (outputLocationsNeeded > outputLocations->size())
3819 {
3820 outputLocations->resize(outputLocationsNeeded);
3821 }
3822 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3823 {
3824 const unsigned int location =
3825 static_cast<unsigned int>(baseLocation) + elementIndex;
3826 ASSERT(location < outputLocations->size());
3827 if (outputLocations->at(location).used())
3828 {
3829 elementsFit = false;
3830 break;
3831 }
3832 }
3833 if (elementsFit)
3834 {
3835 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3836 {
3837 const unsigned int location =
3838 static_cast<unsigned int>(baseLocation) + elementIndex;
3839 if (outputVariable.isArray())
3840 {
3841 (*outputLocations)[location] =
3842 VariableLocation(elementIndex, outputVariableIndex);
3843 }
3844 else
3845 {
3846 VariableLocation locationInfo;
3847 locationInfo.index = outputVariableIndex;
3848 (*outputLocations)[location] = locationInfo;
3849 }
3850 }
3851 }
3852 else
3853 {
3854 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003855 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003856 }
3857 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003858
3859 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003860}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003861
Olli Etuaho48fed632017-03-16 12:05:30 +00003862void Program::setUniformValuesFromBindingQualifiers()
3863{
Jamie Madill982f6e02017-06-07 14:33:04 -04003864 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003865 {
3866 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3867 if (samplerUniform.binding != -1)
3868 {
Olli Etuahod2551232017-10-26 20:03:33 +03003869 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003870 ASSERT(location != -1);
3871 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003872 for (unsigned int elementIndex = 0;
3873 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003874 {
3875 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3876 }
3877 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3878 boundTextureUnits.data());
3879 }
3880 }
3881}
3882
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003883void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003884{
jchen10af713a22017-04-19 09:10:56 +08003885 // Set initial bindings from shader.
3886 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3887 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003888 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003889 bindUniformBlock(blockIndex, uniformBlock.binding);
3890 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003891}
3892
Jamie Madille7d84322017-01-10 18:21:59 -05003893void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003894 GLsizei clampedCount,
3895 const GLint *v)
3896{
Jamie Madill81c2e252017-09-09 23:32:46 -04003897 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3898 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003899 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3900 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003901
Jamie Madill7e4eff12018-08-08 15:49:26 -04003902 if (samplerBinding.unreferenced)
3903 return;
3904
3905 // Update the sampler uniforms.
3906 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3907 {
3908 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3909 GLint newSamplerIndex = v[arrayIndex];
3910
3911 if (oldSamplerIndex == newSamplerIndex)
3912 continue;
3913
3914 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3915
3916 // Update the reference counts.
3917 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3918 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3919 ASSERT(oldRefCount > 0);
3920 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3921 oldRefCount--;
3922 newRefCount++;
3923
3924 // Check for binding type change.
3925 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3926 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3927
3928 if (newRefCount == 1)
3929 {
3930 newSamplerType = samplerBinding.textureType;
3931 mState.mActiveSamplersMask.set(newSamplerIndex);
3932 }
3933 else if (newSamplerType != samplerBinding.textureType)
3934 {
3935 // Conflict detected. Ensure we reset it properly.
3936 newSamplerType = TextureType::InvalidEnum;
3937 }
3938
3939 // Unset previously active sampler.
3940 if (oldRefCount == 0)
3941 {
3942 oldSamplerType = TextureType::InvalidEnum;
3943 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3944 }
3945 else if (oldSamplerType == TextureType::InvalidEnum)
3946 {
3947 // Previous conflict. Check if this new change fixed the conflict.
3948 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3949 }
3950 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003951
3952 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003953 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003954}
3955
Jamie Madill7e4eff12018-08-08 15:49:26 -04003956TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3957{
3958 TextureType foundType = TextureType::InvalidEnum;
3959
3960 for (const SamplerBinding &binding : mSamplerBindings)
3961 {
3962 if (binding.unreferenced)
3963 continue;
3964
3965 // A conflict exists if samplers of different types are sourced by the same texture unit.
3966 // We need to check all bound textures to detect this error case.
3967 for (GLuint textureUnit : binding.boundTextureUnits)
3968 {
3969 if (textureUnit == textureUnitIndex)
3970 {
3971 if (foundType == TextureType::InvalidEnum)
3972 {
3973 foundType = binding.textureType;
3974 }
3975 else if (foundType != binding.textureType)
3976 {
3977 return TextureType::InvalidEnum;
3978 }
3979 }
3980 }
3981 }
3982
3983 return foundType;
3984}
3985
Jamie Madille7d84322017-01-10 18:21:59 -05003986template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003987GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3988 GLsizei count,
3989 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003990 const T *v)
3991{
Jamie Madill134f93d2017-08-31 17:11:00 -04003992 if (count == 1)
3993 return 1;
3994
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003995 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003996
Corentin Wallez15ac5342016-11-03 17:06:39 -04003997 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3998 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003999 unsigned int remainingElements =
4000 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004001 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004002 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004003
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004004 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004005 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004006 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004007 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004008
4009 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004010}
4011
4012template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004013GLsizei Program::clampMatrixUniformCount(GLint location,
4014 GLsizei count,
4015 GLboolean transpose,
4016 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004017{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004018 const VariableLocation &locationInfo = mState.mUniformLocations[location];
4019
Jamie Madill62d31cb2015-09-11 13:25:51 -04004020 if (!transpose)
4021 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004022 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004023 }
4024
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004025 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004026
4027 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4028 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004029 unsigned int remainingElements =
4030 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004031 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004032}
4033
Jamie Madill54164b02017-08-28 15:17:37 -04004034// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4035// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004036template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004037void Program::getUniformInternal(const Context *context,
4038 DestT *dataOut,
4039 GLint location,
4040 GLenum nativeType,
4041 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004042{
Jamie Madill54164b02017-08-28 15:17:37 -04004043 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004044 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004045 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004046 {
4047 GLint tempValue[16] = {0};
4048 mProgram->getUniformiv(context, location, tempValue);
4049 UniformStateQueryCastLoop<GLboolean>(
4050 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004051 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004052 }
4053 case GL_INT:
4054 {
4055 GLint tempValue[16] = {0};
4056 mProgram->getUniformiv(context, location, tempValue);
4057 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4058 components);
4059 break;
4060 }
4061 case GL_UNSIGNED_INT:
4062 {
4063 GLuint tempValue[16] = {0};
4064 mProgram->getUniformuiv(context, location, tempValue);
4065 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4066 components);
4067 break;
4068 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004069 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004070 {
4071 GLfloat tempValue[16] = {0};
4072 mProgram->getUniformfv(context, location, tempValue);
4073 UniformStateQueryCastLoop<GLfloat>(
4074 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004075 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004076 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004077 default:
4078 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004079 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004080 }
4081}
Jamie Madilla4595b82017-01-11 17:36:34 -05004082
4083bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4084{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004085 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004086 // Must be called after samplers are validated.
4087 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4088
4089 for (const auto &binding : mState.mSamplerBindings)
4090 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004091 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004092 for (const auto &unit : binding.boundTextureUnits)
4093 {
4094 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4095 if (programTextureID == textureID)
4096 {
4097 // TODO(jmadill): Check for appropriate overlap.
4098 return true;
4099 }
4100 }
4101 }
4102
4103 return false;
4104}
4105
Jamie Madill70aeda42018-08-20 12:17:40 -04004106Error Program::syncState(const Context *context)
4107{
4108 if (mDirtyBits.any())
4109 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004110 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004111 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4112 mDirtyBits.reset();
4113 }
4114
4115 return NoError();
4116}
Jamie Madilla2c74982016-12-12 11:20:42 -05004117} // namespace gl