blob: b0750f07fd02b35787c06ab1b109a603ea732fc1 [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 Madill78bcd2b2018-10-16 15:05:20 -0400923 resolveLink(context);
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}
958
Jamie Madillef300b12016-10-07 15:12:09 -0400959void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400961 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800962 ShaderType shaderType = shader->getType();
963 ASSERT(shaderType != ShaderType::InvalidEnum);
964
965 mState.mAttachedShaders[shaderType] = shader;
966 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967}
968
Jamie Madillc1d770e2017-04-13 17:31:24 -0400969void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400971 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800972 ShaderType shaderType = shader->getType();
973 ASSERT(shaderType != ShaderType::InvalidEnum);
974
975 ASSERT(mState.mAttachedShaders[shaderType] == shader);
976 shader->release(context);
977 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978}
979
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000980int Program::getAttachedShadersCount() const
981{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400982 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800983 int numAttachedShaders = 0;
984 for (const Shader *shader : mState.mAttachedShaders)
985 {
986 if (shader)
987 {
988 ++numAttachedShaders;
989 }
990 }
991
992 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000993}
994
Jiawei Shao385b3e02018-03-21 09:43:28 +0800995const Shader *Program::getAttachedShader(ShaderType shaderType) const
996{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400997 ASSERT(mLinkResolved);
Jiawei Shao385b3e02018-03-21 09:43:28 +0800998 return mState.getAttachedShader(shaderType);
999}
1000
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001void Program::bindAttributeLocation(GLuint index, const char *name)
1002{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001003 ASSERT(mLinkResolved);
Geoff Langd8605522016-04-13 10:19:12 -04001004 mAttributeBindings.bindLocation(index, name);
1005}
1006
1007void Program::bindUniformLocation(GLuint index, const char *name)
1008{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001009 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001010 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011}
1012
Sami Väisänen46eaa942016-06-29 10:26:37 +03001013void Program::bindFragmentInputLocation(GLint index, const char *name)
1014{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001015 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001016 mFragmentInputBindings.bindLocation(index, name);
1017}
1018
Olli Etuaho0ca09752018-09-24 11:00:50 +03001019void Program::bindFragmentOutputLocation(GLuint index, const char *name)
1020{
1021 mFragmentOutputLocations.bindLocation(index, name);
1022}
1023
1024void Program::bindFragmentOutputIndex(GLuint index, const char *name)
1025{
1026 mFragmentOutputIndexes.bindLocation(index, name);
1027}
1028
jchen103fd614d2018-08-13 12:21:58 +08001029BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001030{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001031 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001032 BindingInfo ret;
1033 ret.type = GL_NONE;
1034 ret.valid = false;
1035
Jiawei Shao385b3e02018-03-21 09:43:28 +08001036 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001037 ASSERT(fragmentShader);
1038
1039 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001040 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001041
1042 for (const auto &binding : mFragmentInputBindings)
1043 {
1044 if (binding.second != static_cast<GLuint>(index))
1045 continue;
1046
1047 ret.valid = true;
1048
Olli Etuahod2551232017-10-26 20:03:33 +03001049 size_t nameLengthWithoutArrayIndex;
1050 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001051
1052 for (const auto &in : inputs)
1053 {
Olli Etuahod2551232017-10-26 20:03:33 +03001054 if (in.name.length() == nameLengthWithoutArrayIndex &&
1055 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001056 {
1057 if (in.isArray())
1058 {
1059 // The client wants to bind either "name" or "name[0]".
1060 // GL ES 3.1 spec refers to active array names with language such as:
1061 // "if the string identifies the base name of an active array, where the
1062 // string would exactly match the name of the variable if the suffix "[0]"
1063 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001064 if (arrayIndex == GL_INVALID_INDEX)
1065 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001066
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001067 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001068 }
1069 else
1070 {
1071 ret.name = in.mappedName;
1072 }
1073 ret.type = in.type;
1074 return ret;
1075 }
1076 }
1077 }
1078
1079 return ret;
1080}
1081
jchen103fd614d2018-08-13 12:21:58 +08001082void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001083 GLenum genMode,
1084 GLint components,
1085 const GLfloat *coeffs)
1086{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001087 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001088 // If the location is -1 then the command is silently ignored
1089 if (index == -1)
1090 return;
1091
jchen103fd614d2018-08-13 12:21:58 +08001092 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001093
1094 // If the input doesn't exist then then the command is silently ignored
1095 // This could happen through optimization for example, the shader translator
1096 // decides that a variable is not actually being used and optimizes it away.
1097 if (binding.name.empty())
1098 return;
1099
1100 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1101}
1102
Martin Radev4c4c8e72016-08-04 12:25:34 +03001103// The attached shaders are checked for linking errors by matching up their variables.
1104// Uniform, input and output variables get collected.
1105// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001106Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001107{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001108 const auto &data = context->getContextState();
1109
Jamie Madill6c58b062017-08-01 13:44:25 -04001110 auto *platform = ANGLEPlatformCurrent();
1111 double startTime = platform->currentTime(platform);
1112
Jamie Madill6c1f6712017-02-14 19:08:04 -05001113 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001114 mInfoLog.reset();
1115
1116 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001117 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001118 {
1119 return NoError();
1120 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001121
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -04001122 egl::BlobCache::Key programHash = {0};
Jamie Madill6bc264a2018-03-31 15:36:05 -04001123 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001124
Jamie Madill32447362017-06-28 14:53:52 -04001125 if (cache)
1126 {
Jamie Madill785e8a02018-10-04 17:42:00 -04001127 angle::Result result = cache->getProgram(context, this, &mState, &programHash);
1128 mLinked = (result == angle::Result::Continue());
1129 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001130 }
1131
1132 if (mLinked)
1133 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001134 double delta = platform->currentTime(platform) - startTime;
1135 int us = static_cast<int>(delta * 1000000.0);
1136 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001137 return NoError();
1138 }
1139
1140 // Cache load failed, fall through to normal linking.
1141 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001142
Jamie Madill6bc264a2018-03-31 15:36:05 -04001143 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001144 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001145
jchen107ae70d82018-07-06 13:47:01 +08001146 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001147 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001148 {
jchen107ae70d82018-07-06 13:47:01 +08001149 resources.reset(
1150 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1151 {&mState.mUniformBlocks, &mState.mUniforms},
1152 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1153 {&mState.mAtomicCounterBuffers},
1154 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001155
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001156 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001157 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1158 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001159 {
1160 return NoError();
1161 }
1162
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001163 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001164 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1165 context->getExtensions().webglCompatibility, mInfoLog,
1166 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001167 {
1168 return NoError();
1169 }
1170
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001171 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1172 // A link error will be generated if the sum of the number of active image uniforms used in
1173 // all shaders, the number of active shader storage blocks, and the number of active
1174 // fragment shader outputs exceeds the implementation-dependent value of
1175 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1176 if (combinedImageUniforms + combinedShaderStorageBlocks >
1177 context->getCaps().maxCombinedShaderOutputResources)
1178 {
1179 mInfoLog
1180 << "The sum of the number of active image uniforms, active shader storage blocks "
1181 "and active fragment shader outputs exceeds "
1182 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1183 << context->getCaps().maxCombinedShaderOutputResources << ")";
1184 return NoError();
1185 }
1186
jchen103fd614d2018-08-13 12:21:58 +08001187 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1188 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001189 }
1190 else
1191 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001192 // Map the varyings to the register file
1193 // In WebGL, we use a slightly different handling for packing variables.
1194 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1195 if (data.getLimitations().noFlexibleVaryingPacking)
1196 {
1197 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1198 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1199 }
1200 else if (data.getExtensions().webglCompatibility)
1201 {
1202 packMode = PackMode::WEBGL_STRICT;
1203 }
1204
jchen107ae70d82018-07-06 13:47:01 +08001205 resources.reset(
1206 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1207 {&mState.mUniformBlocks, &mState.mUniforms},
1208 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1209 {&mState.mAtomicCounterBuffers},
1210 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001211
jchen103fd614d2018-08-13 12:21:58 +08001212 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001213 {
1214 return NoError();
1215 }
1216
jchen103fd614d2018-08-13 12:21:58 +08001217 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001218 {
1219 return NoError();
1220 }
1221
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001222 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001223 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1224 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001225 {
1226 return NoError();
1227 }
1228
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001229 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001230 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1231 context->getExtensions().webglCompatibility, mInfoLog,
1232 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001233 {
1234 return NoError();
1235 }
1236
jchen103fd614d2018-08-13 12:21:58 +08001237 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001238 {
1239 return NoError();
1240 }
1241
Olli Etuaho0ca09752018-09-24 11:00:50 +03001242 if (!linkOutputVariables(context->getCaps(), context->getExtensions(),
1243 context->getClientVersion(), combinedImageUniforms,
1244 combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001245 {
1246 return NoError();
1247 }
1248
jchen103fd614d2018-08-13 12:21:58 +08001249 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001250
Jiawei Shao016105b2018-04-12 16:38:31 +08001251 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001252 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001253
jchen103fd614d2018-08-13 12:21:58 +08001254 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1255 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001256
jchen103fd614d2018-08-13 12:21:58 +08001257 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1258 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001259 {
1260 return NoError();
1261 }
1262
jchen107ae70d82018-07-06 13:47:01 +08001263 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001264 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001265 {
1266 return NoError();
1267 }
1268
Martin Radev4c4c8e72016-08-04 12:25:34 +03001269 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001270 }
1271
jchen107ae70d82018-07-06 13:47:01 +08001272 mLinkingState.reset(new LinkingState());
1273 mLinkingState->context = context;
1274 mLinkingState->programHash = programHash;
1275 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1276 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001277 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001278
1279 return NoError();
1280}
1281
1282bool Program::isLinking() const
1283{
1284 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1285}
1286
Jamie Madill785e8a02018-10-04 17:42:00 -04001287void Program::resolveLinkImpl(const Context *context)
jchen107ae70d82018-07-06 13:47:01 +08001288{
1289 ASSERT(mLinkingState.get());
1290
Jamie Madill785e8a02018-10-04 17:42:00 -04001291 angle::Result result = mLinkingState->linkEvent->wait(context);
1292
1293 mLinked = result == angle::Result::Continue();
jchen105055fba2018-08-17 09:57:07 +08001294 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001295 auto linkingState = std::move(mLinkingState);
1296 if (!mLinked)
1297 {
1298 return;
1299 }
1300
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001301 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001302
Yunchao Heece12532017-11-21 15:50:21 +08001303 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1304 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001305 ASSERT(mLinked);
1306 updateLinkedShaderStages();
1307
Jamie Madill54164b02017-08-28 15:17:37 -04001308 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001309 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1310 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001311
Jamie Madill7e4eff12018-08-08 15:49:26 -04001312 // Must be called after markUnusedUniformLocations.
1313 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001314 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001315
1316 setUniformValuesFromBindingQualifiers();
1317
Jamie Madill32447362017-06-28 14:53:52 -04001318 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001319 auto *cache = linkingState->context->getMemoryProgramCache();
1320 if (cache &&
1321 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1322 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001323 {
jchen107ae70d82018-07-06 13:47:01 +08001324 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001325 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001326}
1327
Yunchao He85072e82017-11-14 15:43:28 +08001328void Program::updateLinkedShaderStages()
1329{
Yunchao Heece12532017-11-21 15:50:21 +08001330 mState.mLinkedShaderStages.reset();
1331
Jiawei Shao016105b2018-04-12 16:38:31 +08001332 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001333 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001334 if (shader)
1335 {
1336 mState.mLinkedShaderStages.set(shader->getType());
1337 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001338 }
Yunchao He85072e82017-11-14 15:43:28 +08001339}
1340
James Darpinian30b604d2018-03-12 17:26:57 -07001341void ProgramState::updateTransformFeedbackStrides()
1342{
1343 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1344 {
1345 mTransformFeedbackStrides.resize(1);
1346 size_t totalSize = 0;
1347 for (auto &varying : mLinkedTransformFeedbackVaryings)
1348 {
1349 totalSize += varying.size() * VariableExternalSize(varying.type);
1350 }
1351 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1352 }
1353 else
1354 {
1355 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1356 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1357 {
1358 auto &varying = mLinkedTransformFeedbackVaryings[i];
1359 mTransformFeedbackStrides[i] =
1360 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1361 }
1362 }
1363}
1364
Jamie Madill7e4eff12018-08-08 15:49:26 -04001365void ProgramState::updateActiveSamplers()
1366{
1367 for (SamplerBinding &samplerBinding : mSamplerBindings)
1368 {
1369 if (samplerBinding.unreferenced)
1370 continue;
1371
1372 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1373 {
1374 mActiveSamplerRefCounts[textureUnit]++;
1375 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1376 mActiveSamplersMask.set(textureUnit);
1377 }
1378 }
1379}
1380
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001381void ProgramState::updateActiveImages()
1382{
1383 for (ImageBinding &imageBinding : mImageBindings)
1384 {
1385 if (imageBinding.unreferenced)
1386 continue;
1387
1388 for (GLint imageUnit : imageBinding.boundImageUnits)
1389 {
1390 mActiveImagesMask.set(imageUnit);
1391 }
1392 }
1393}
1394
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001395// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001396void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001398 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001399 mState.mAttributesTypeMask.reset();
1400 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001401 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001402 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001403 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001404 mState.mUniforms.clear();
1405 mState.mUniformLocations.clear();
1406 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001407 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001408 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001409 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001410 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001411 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001412 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001413 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001414 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001415 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001416 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001417 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001418 mState.mNumViews = -1;
1419 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1420 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001421 mState.mGeometryShaderInvocations = 1;
1422 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 mValidated = false;
1425
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001426 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001427 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001428}
1429
Jamie Madilla2c74982016-12-12 11:20:42 -05001430Error Program::loadBinary(const Context *context,
1431 GLenum binaryFormat,
1432 const void *binary,
1433 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001434{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001435 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001436 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001437
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001439 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001441 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1442 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001443 {
Jamie Madillf6113162015-05-07 11:49:21 -04001444 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001445 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446 }
1447
Jamie Madill4f86d052017-06-05 12:59:26 -04001448 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
Jamie Madill785e8a02018-10-04 17:42:00 -04001449 angle::Result result =
1450 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog);
1451 mLinked = result == angle::Result::Continue();
1452 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001453
1454 // Currently we require the full shader text to compute the program hash.
Jamie Madill785e8a02018-10-04 17:42:00 -04001455 // We could also store the binary in the internal program cache.
Jamie Madill32447362017-06-28 14:53:52 -04001456
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 Madill956ab4d2018-10-10 16:13:03 -04001565void Program::deleteSelf(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001566{
Jamie Madill956ab4d2018-10-10 16:13:03 -04001567 ASSERT(mRefCount == 0 && mDeleteStatus);
1568 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001569}
1570
1571unsigned int Program::getRefCount() const
1572{
1573 return mRefCount;
1574}
1575
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001576int Program::getInfoLogLength() const
1577{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001578 ASSERT(mLinkResolved);
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001579 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001580}
1581
Geoff Lange1a27752015-10-05 13:16:04 -04001582void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001583{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001584 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001585 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001586}
1587
Geoff Lange1a27752015-10-05 13:16:04 -04001588void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001589{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001590 ASSERT(mLinkResolved);
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001591 int total = 0;
1592
Jiawei Shao016105b2018-04-12 16:38:31 +08001593 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001594 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001595 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001596 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001597 shaders[total] = shader->getHandle();
1598 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001599 }
1600 }
1601
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001602 if (count)
1603 {
1604 *count = total;
1605 }
1606}
1607
Geoff Lange1a27752015-10-05 13:16:04 -04001608GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001609{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001610 ASSERT(mLinkResolved);
Jamie Madill34ca4f52017-06-13 11:49:39 -04001611 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001612}
1613
Jamie Madill63805b42015-08-25 13:17:39 -04001614bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001615{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001616 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001617 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1618 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619}
1620
jchen10fd7c3b52017-03-21 15:36:03 +08001621void Program::getActiveAttribute(GLuint index,
1622 GLsizei bufsize,
1623 GLsizei *length,
1624 GLint *size,
1625 GLenum *type,
1626 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001627{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001628 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001629 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001630 {
1631 if (bufsize > 0)
1632 {
1633 name[0] = '\0';
1634 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001636 if (length)
1637 {
1638 *length = 0;
1639 }
1640
1641 *type = GL_NONE;
1642 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001643 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001644 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001645
jchen1036e120e2017-03-14 14:53:58 +08001646 ASSERT(index < mState.mAttributes.size());
1647 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001648
1649 if (bufsize > 0)
1650 {
jchen10fd7c3b52017-03-21 15:36:03 +08001651 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001652 }
1653
1654 // Always a single 'type' instance
1655 *size = 1;
1656 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001657}
1658
Geoff Lange1a27752015-10-05 13:16:04 -04001659GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001660{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001661 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001662 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001663 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001664 return 0;
1665 }
1666
jchen1036e120e2017-03-14 14:53:58 +08001667 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001668}
1669
Geoff Lange1a27752015-10-05 13:16:04 -04001670GLint Program::getActiveAttributeMaxLength() 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
1678 size_t maxLength = 0;
1679
Jamie Madill48ef11b2016-04-27 15:21:52 -04001680 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001681 {
jchen1036e120e2017-03-14 14:53:58 +08001682 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001683 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001684
Jamie Madillc349ec02015-08-21 16:53:12 -04001685 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686}
1687
jchen107ae70d82018-07-06 13:47:01 +08001688const std::vector<sh::Attribute> &Program::getAttributes() const
1689{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001690 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001691 return mState.mAttributes;
1692}
1693
jchen107ae70d82018-07-06 13:47:01 +08001694const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1695{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001696 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001697 return mState.mSamplerBindings;
1698}
1699
jchen107ae70d82018-07-06 13:47:01 +08001700const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1701{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001702 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001703 return mState.mComputeShaderLocalSize;
1704}
1705
1706PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1707{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001708 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001709 return mState.mGeometryShaderInputPrimitiveType;
1710}
1711PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1712{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001713 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001714 return mState.mGeometryShaderOutputPrimitiveType;
1715}
1716GLint Program::getGeometryShaderInvocations() const
1717{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001718 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001719 return mState.mGeometryShaderInvocations;
1720}
1721GLint Program::getGeometryShaderMaxVertices() const
1722{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001723 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001724 return mState.mGeometryShaderMaxVertices;
1725}
1726
jchen1015015f72017-03-16 13:54:21 +08001727GLuint Program::getInputResourceIndex(const GLchar *name) const
1728{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001729 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001730 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001731}
1732
1733GLuint Program::getOutputResourceIndex(const GLchar *name) const
1734{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001735 ASSERT(mLinkResolved);
jchen1015015f72017-03-16 13:54:21 +08001736 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1737}
1738
jchen10fd7c3b52017-03-21 15:36:03 +08001739size_t Program::getOutputResourceCount() const
1740{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001741 ASSERT(mLinkResolved);
jchen10fd7c3b52017-03-21 15:36:03 +08001742 return (mLinked ? mState.mOutputVariables.size() : 0);
1743}
1744
jchen107ae70d82018-07-06 13:47:01 +08001745const std::vector<GLenum> &Program::getOutputVariableTypes() const
1746{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001747 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001748 return mState.mOutputVariableTypes;
1749}
1750DrawBufferMask Program::getActiveOutputVariables() const
1751{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001752 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001753 return mState.mActiveOutputVariables;
1754}
1755
jchen10baf5d942017-08-28 20:45:48 +08001756template <typename T>
1757void Program::getResourceName(GLuint index,
1758 const std::vector<T> &resources,
1759 GLsizei bufSize,
1760 GLsizei *length,
1761 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001762{
1763 if (length)
1764 {
1765 *length = 0;
1766 }
1767
1768 if (!mLinked)
1769 {
1770 if (bufSize > 0)
1771 {
1772 name[0] = '\0';
1773 }
1774 return;
1775 }
jchen10baf5d942017-08-28 20:45:48 +08001776 ASSERT(index < resources.size());
1777 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001778
1779 if (bufSize > 0)
1780 {
Olli Etuahod2551232017-10-26 20:03:33 +03001781 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001782 }
1783}
1784
jchen10baf5d942017-08-28 20:45:48 +08001785void Program::getInputResourceName(GLuint index,
1786 GLsizei bufSize,
1787 GLsizei *length,
1788 GLchar *name) const
1789{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001790 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001791 getResourceName(index, mState.mAttributes, bufSize, length, name);
1792}
1793
1794void Program::getOutputResourceName(GLuint index,
1795 GLsizei bufSize,
1796 GLsizei *length,
1797 GLchar *name) const
1798{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001799 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001800 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1801}
1802
1803void Program::getUniformResourceName(GLuint index,
1804 GLsizei bufSize,
1805 GLsizei *length,
1806 GLchar *name) const
1807{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001808 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001809 getResourceName(index, mState.mUniforms, bufSize, length, name);
1810}
1811
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001812void Program::getBufferVariableResourceName(GLuint index,
1813 GLsizei bufSize,
1814 GLsizei *length,
1815 GLchar *name) const
1816{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001817 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001818 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1819}
1820
jchen10880683b2017-04-12 16:21:55 +08001821const sh::Attribute &Program::getInputResource(GLuint index) const
1822{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001823 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001824 ASSERT(index < mState.mAttributes.size());
1825 return mState.mAttributes[index];
1826}
1827
1828const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1829{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001830 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001831 ASSERT(index < mState.mOutputVariables.size());
1832 return mState.mOutputVariables[index];
1833}
1834
jchen107ae70d82018-07-06 13:47:01 +08001835const ProgramBindings &Program::getAttributeBindings() const
1836{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001837 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001838 return mAttributeBindings;
1839}
1840const ProgramBindings &Program::getUniformLocationBindings() const
1841{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001842 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001843 return mUniformLocationBindings;
1844}
1845const ProgramBindings &Program::getFragmentInputBindings() const
1846{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001847 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001848 return mFragmentInputBindings;
1849}
1850
1851int Program::getNumViews() const
1852{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001853 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001854 return mState.getNumViews();
1855}
jchen107ae70d82018-07-06 13:47:01 +08001856
1857ComponentTypeMask Program::getDrawBufferTypeMask() const
1858{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001859 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001860 return mState.mDrawBufferTypeMask;
1861}
1862ComponentTypeMask Program::getAttributesTypeMask() const
1863{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001864 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001865 return mState.mAttributesTypeMask;
1866}
1867AttributesMask Program::getAttributesMask() const
1868{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001869 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001870 return mState.mAttributesMask;
1871}
1872
1873const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1874{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001875 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001876 return mState.mTransformFeedbackStrides;
1877}
1878
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879GLint Program::getFragDataLocation(const std::string &name) const
1880{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001881 ASSERT(mLinkResolved);
Olli Etuaho0ca09752018-09-24 11:00:50 +03001882 GLint primaryLocation =
1883 GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
1884 if (primaryLocation != -1)
1885 {
1886 return primaryLocation;
1887 }
1888 return GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name);
1889}
1890
1891GLint Program::getFragDataIndex(const std::string &name) const
1892{
1893 ASSERT(mLinkResolved);
1894 if (GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name) != -1)
1895 {
1896 return 0;
1897 }
1898 if (GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name) != -1)
1899 {
1900 return 1;
1901 }
1902 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001903}
1904
Geoff Lange1a27752015-10-05 13:16:04 -04001905void Program::getActiveUniform(GLuint index,
1906 GLsizei bufsize,
1907 GLsizei *length,
1908 GLint *size,
1909 GLenum *type,
1910 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001911{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001912 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001914 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001915 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001916 ASSERT(index < mState.mUniforms.size());
1917 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918
1919 if (bufsize > 0)
1920 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001922 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923 }
1924
Olli Etuaho465835d2017-09-26 13:34:10 +03001925 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001927 }
1928 else
1929 {
1930 if (bufsize > 0)
1931 {
1932 name[0] = '\0';
1933 }
1934
1935 if (length)
1936 {
1937 *length = 0;
1938 }
1939
1940 *size = 0;
1941 *type = GL_NONE;
1942 }
1943}
1944
Geoff Lange1a27752015-10-05 13:16:04 -04001945GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001946{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001947 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001949 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001950 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001951 }
1952 else
1953 {
1954 return 0;
1955 }
1956}
1957
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001958size_t Program::getActiveBufferVariableCount() const
1959{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001960 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001961 return mLinked ? mState.mBufferVariables.size() : 0;
1962}
1963
Geoff Lange1a27752015-10-05 13:16:04 -04001964GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001965{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001966 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001967 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968
1969 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001970 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001971 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001973 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001974 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001975 size_t length = uniform.name.length() + 1u;
1976 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 {
1978 length += 3; // Counting in "[0]".
1979 }
1980 maxLength = std::max(length, maxLength);
1981 }
1982 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001983 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986}
1987
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988bool Program::isValidUniformLocation(GLint location) const
1989{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001990 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04001991 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001992 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001993 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001994}
1995
Jamie Madill62d31cb2015-09-11 13:25:51 -04001996const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001998 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001999 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05002000 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001}
2002
Jamie Madillac4e9c32017-01-13 14:07:12 -05002003const VariableLocation &Program::getUniformLocation(GLint location) const
2004{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002005 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05002006 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
2007 return mState.mUniformLocations[location];
2008}
2009
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002010const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
2011{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002012 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002013 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
2014 return mState.mBufferVariables[index];
2015}
2016
Jamie Madill62d31cb2015-09-11 13:25:51 -04002017GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002019 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002020 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021}
2022
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002024{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002025 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002026 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002027}
2028
2029void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2030{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002031 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002032 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2033 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002034 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035}
2036
2037void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2038{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002039 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002040 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2041 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002042 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043}
2044
2045void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2046{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002047 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002048 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2049 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002050 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051}
2052
2053void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2054{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002055 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002056 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2057 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002058 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059}
2060
Jamie Madill81c2e252017-09-09 23:32:46 -04002061Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002062{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002063 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002064 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2065 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2066
Jamie Madill81c2e252017-09-09 23:32:46 -04002067 mProgram->setUniform1iv(location, clampedCount, v);
2068
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002069 if (mState.isSamplerUniformIndex(locationInfo.index))
2070 {
2071 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002072 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002073 }
2074
Jamie Madill81c2e252017-09-09 23:32:46 -04002075 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076}
2077
2078void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2079{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002080 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002081 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2082 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002083 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084}
2085
2086void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2087{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002088 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002089 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2090 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002091 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092}
2093
2094void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2095{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002096 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002097 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2098 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002099 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100}
2101
2102void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2103{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002104 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002105 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2106 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002107 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108}
2109
2110void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2111{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002112 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002113 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2114 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002115 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116}
2117
2118void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2119{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002120 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002121 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2122 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002123 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124}
2125
2126void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2127{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002128 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002129 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2130 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002131 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132}
2133
Jamie Madill493f9572018-05-24 19:52:15 -04002134void Program::setUniformMatrix2fv(GLint location,
2135 GLsizei count,
2136 GLboolean transpose,
2137 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002138{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002139 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002140 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002141 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142}
2143
Jamie Madill493f9572018-05-24 19:52:15 -04002144void Program::setUniformMatrix3fv(GLint location,
2145 GLsizei count,
2146 GLboolean transpose,
2147 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002149 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002150 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002151 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152}
2153
Jamie Madill493f9572018-05-24 19:52:15 -04002154void Program::setUniformMatrix4fv(GLint location,
2155 GLsizei count,
2156 GLboolean transpose,
2157 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002159 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002160 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002161 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162}
2163
Jamie Madill493f9572018-05-24 19:52:15 -04002164void Program::setUniformMatrix2x3fv(GLint location,
2165 GLsizei count,
2166 GLboolean transpose,
2167 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002169 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002170 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002171 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172}
2173
Jamie Madill493f9572018-05-24 19:52:15 -04002174void Program::setUniformMatrix2x4fv(GLint location,
2175 GLsizei count,
2176 GLboolean transpose,
2177 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002179 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002180 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002181 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182}
2183
Jamie Madill493f9572018-05-24 19:52:15 -04002184void Program::setUniformMatrix3x2fv(GLint location,
2185 GLsizei count,
2186 GLboolean transpose,
2187 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002189 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002190 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002191 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192}
2193
Jamie Madill493f9572018-05-24 19:52:15 -04002194void Program::setUniformMatrix3x4fv(GLint location,
2195 GLsizei count,
2196 GLboolean transpose,
2197 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002199 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002200 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002201 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202}
2203
Jamie Madill493f9572018-05-24 19:52:15 -04002204void Program::setUniformMatrix4x2fv(GLint location,
2205 GLsizei count,
2206 GLboolean transpose,
2207 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002209 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002210 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002211 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002212}
2213
Jamie Madill493f9572018-05-24 19:52:15 -04002214void Program::setUniformMatrix4x3fv(GLint location,
2215 GLsizei count,
2216 GLboolean transpose,
2217 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002219 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002220 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002221 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002222}
2223
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002224GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2225{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002226 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002227 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2228 const std::vector<GLuint> &boundTextureUnits =
2229 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2230 return boundTextureUnits[uniformLocation.arrayIndex];
2231}
2232
Jamie Madill54164b02017-08-28 15:17:37 -04002233void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002235 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002236 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2237 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002238
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002239 if (uniform.isSampler())
2240 {
2241 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2242 return;
2243 }
2244
2245 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002246 if (nativeType == GL_FLOAT)
2247 {
2248 mProgram->getUniformfv(context, location, v);
2249 }
2250 else
2251 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002252 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002253 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002254}
2255
Jamie Madill54164b02017-08-28 15:17:37 -04002256void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002257{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002258 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002259 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2260 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002261
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002262 if (uniform.isSampler())
2263 {
2264 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2265 return;
2266 }
2267
2268 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002269 if (nativeType == GL_INT || nativeType == GL_BOOL)
2270 {
2271 mProgram->getUniformiv(context, location, v);
2272 }
2273 else
2274 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002275 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002276 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002277}
2278
Jamie Madill54164b02017-08-28 15:17:37 -04002279void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002281 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002282 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2283 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002284
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002285 if (uniform.isSampler())
2286 {
2287 *v = getSamplerUniformBinding(uniformLocation);
2288 return;
2289 }
2290
2291 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002292 if (nativeType == GL_UNSIGNED_INT)
2293 {
2294 mProgram->getUniformuiv(context, location, v);
2295 }
2296 else
2297 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002298 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002299 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300}
2301
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302void Program::flagForDeletion()
2303{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002304 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 mDeleteStatus = true;
2306}
2307
2308bool Program::isFlaggedForDeletion() const
2309{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002310 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 return mDeleteStatus;
2312}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002313
Brandon Jones43a53e22014-08-28 16:23:22 -07002314void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002315{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002316 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002317 mInfoLog.reset();
2318
Geoff Lang7dd2e102014-11-10 15:19:26 -05002319 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002320 {
Geoff Lang92019432017-11-20 13:09:34 -05002321 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002322 }
2323 else
2324 {
Jamie Madillf6113162015-05-07 11:49:21 -04002325 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002326 }
2327}
2328
Jamie Madillcc73f242018-08-01 11:34:48 -04002329bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002330{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002331 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002332
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002333 // if any two active samplers in a program are of different types, but refer to the same
2334 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2335 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002336 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002337 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002338 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002339 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002340 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002341 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002342 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2343 "image unit ("
2344 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002345 }
2346
Jamie Madill7e4eff12018-08-08 15:49:26 -04002347 mCachedValidateSamplersResult = false;
2348 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002349 }
2350 }
2351
2352 mCachedValidateSamplersResult = true;
2353 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354}
2355
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002356bool Program::isValidated() const
2357{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002358 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 return mValidated;
2360}
2361
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002362void Program::getActiveUniformBlockName(const GLuint blockIndex,
2363 GLsizei bufSize,
2364 GLsizei *length,
2365 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002366{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002367 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002368 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2369}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002371void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2372 GLsizei bufSize,
2373 GLsizei *length,
2374 GLchar *blockName) const
2375{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002376 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002377 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002378}
2379
Qin Jiajia9bf55522018-01-29 13:56:23 +08002380template <typename T>
2381GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002382{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002383 int maxLength = 0;
2384
2385 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002386 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002387 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002388 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002389 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002391 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002392 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393 }
2394 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002395 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396
2397 return maxLength;
2398}
2399
Qin Jiajia9bf55522018-01-29 13:56:23 +08002400GLint Program::getActiveUniformBlockMaxNameLength() const
2401{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002402 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002403 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2404}
2405
2406GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2407{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002408 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002409 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2410}
2411
Geoff Lange1a27752015-10-05 13:16:04 -04002412GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002413{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002414 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002415 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2416}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002417
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002418GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2419{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002420 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002421 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002422}
2423
Jiajia Qin729b2c62017-08-14 09:36:11 +08002424const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002425{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002426 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002427 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2428 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002429}
2430
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002431const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2432{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002433 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002434 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2435 return mState.mShaderStorageBlocks[index];
2436}
2437
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002438void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2439{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002440 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002441 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002442 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002443 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002444}
2445
2446GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2447{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002448 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002449 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002450}
2451
Jiajia Qin729b2c62017-08-14 09:36:11 +08002452GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2453{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002454 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002455 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2456}
2457
Jamie Madill493f9572018-05-24 19:52:15 -04002458void Program::setTransformFeedbackVaryings(GLsizei count,
2459 const GLchar *const *varyings,
2460 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002461{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002462 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002463 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002464 for (GLsizei i = 0; i < count; i++)
2465 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002466 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002467 }
2468
Jamie Madill48ef11b2016-04-27 15:21:52 -04002469 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002470}
2471
Jamie Madill493f9572018-05-24 19:52:15 -04002472void Program::getTransformFeedbackVarying(GLuint index,
2473 GLsizei bufSize,
2474 GLsizei *length,
2475 GLsizei *size,
2476 GLenum *type,
2477 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002478{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002479 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002480 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002481 {
jchen10a9042d32017-03-17 08:50:45 +08002482 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2483 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2484 std::string varName = var.nameWithArrayIndex();
2485 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002486 if (length)
2487 {
2488 *length = lastNameIdx;
2489 }
2490 if (size)
2491 {
jchen10a9042d32017-03-17 08:50:45 +08002492 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002493 }
2494 if (type)
2495 {
jchen10a9042d32017-03-17 08:50:45 +08002496 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002497 }
2498 if (name)
2499 {
jchen10a9042d32017-03-17 08:50:45 +08002500 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002501 name[lastNameIdx] = '\0';
2502 }
2503 }
2504}
2505
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002506GLsizei Program::getTransformFeedbackVaryingCount() const
2507{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002508 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002509 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002510 {
jchen10a9042d32017-03-17 08:50:45 +08002511 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002512 }
2513 else
2514 {
2515 return 0;
2516 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002517}
2518
2519GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2520{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002521 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002522 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002523 {
2524 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002525 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002526 {
jchen10a9042d32017-03-17 08:50:45 +08002527 maxSize =
2528 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002529 }
2530
2531 return maxSize;
2532 }
2533 else
2534 {
2535 return 0;
2536 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002537}
2538
2539GLenum Program::getTransformFeedbackBufferMode() const
2540{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002541 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002542 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002543}
2544
jchen103fd614d2018-08-13 12:21:58 +08002545bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002546{
Jiawei Shao016105b2018-04-12 16:38:31 +08002547 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2548 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2549 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2550 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002551
Jamie Madill493f9572018-05-24 19:52:15 -04002552 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002553 bool isGraphicsShaderAttached =
2554 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002555 // Check whether we both have a compute and non-compute shaders attached.
2556 // If there are of both types attached, then linking should fail.
2557 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2558 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2559 {
2560 infoLog << "Both compute and graphics shaders are attached to the same program.";
2561 return false;
2562 }
2563
2564 if (computeShader)
2565 {
jchen103fd614d2018-08-13 12:21:58 +08002566 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002567 {
2568 infoLog << "Attached compute shader is not compiled.";
2569 return false;
2570 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002571 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002572
jchen103fd614d2018-08-13 12:21:58 +08002573 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002574
2575 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2576 // If the work group size is not specified, a link time error should occur.
2577 if (!mState.mComputeShaderLocalSize.isDeclared())
2578 {
2579 infoLog << "Work group size is not specified.";
2580 return false;
2581 }
2582 }
2583 else
2584 {
jchen103fd614d2018-08-13 12:21:58 +08002585 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002586 {
2587 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2588 return false;
2589 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002590 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002591
jchen103fd614d2018-08-13 12:21:58 +08002592 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002593 {
2594 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2595 return false;
2596 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002597 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002598
jchen103fd614d2018-08-13 12:21:58 +08002599 int vertexShaderVersion = vertexShader->getShaderVersion();
2600 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002601 {
2602 infoLog << "Fragment shader version does not match vertex shader version.";
2603 return false;
2604 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002605
2606 if (geometryShader)
2607 {
2608 // [GL_EXT_geometry_shader] Chapter 7
2609 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2610 // Language Specification, as well as any of the following reasons:
2611 // * One or more of the shader objects attached to <program> are not compiled
2612 // successfully.
2613 // * The shaders do not use the same shader language version.
2614 // * <program> contains objects to form a geometry shader, and
2615 // - <program> is not separable and contains no objects to form a vertex shader; or
2616 // - the input primitive type, output primitive type, or maximum output vertex count
2617 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002618 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002619 {
2620 infoLog << "The attached geometry shader isn't compiled.";
2621 return false;
2622 }
2623
jchen103fd614d2018-08-13 12:21:58 +08002624 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002625 {
2626 mInfoLog << "Geometry shader version does not match vertex shader version.";
2627 return false;
2628 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002629 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002630
Jamie Madill493f9572018-05-24 19:52:15 -04002631 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002632 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002633 if (!inputPrimitive.valid())
2634 {
2635 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2636 return false;
2637 }
2638
Jamie Madill493f9572018-05-24 19:52:15 -04002639 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002640 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002641 if (!outputPrimitive.valid())
2642 {
2643 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2644 return false;
2645 }
2646
jchen103fd614d2018-08-13 12:21:58 +08002647 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002648 if (!maxVertices.valid())
2649 {
2650 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2651 return false;
2652 }
2653
2654 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2655 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2656 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002657 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002658 }
Jiawei Shao73618602017-12-20 15:47:15 +08002659 }
2660
2661 return true;
2662}
2663
jchen10910a3da2017-11-15 09:40:11 +08002664GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2665{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002666 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002667 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2668 {
2669 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2670 if (tf.nameWithArrayIndex() == name)
2671 {
2672 return tfIndex;
2673 }
2674 }
2675 return GL_INVALID_INDEX;
2676}
2677
2678const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2679{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002680 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002681 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2682 return mState.mLinkedTransformFeedbackVaryings[index];
2683}
2684
jchen103fd614d2018-08-13 12:21:58 +08002685bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002686{
Jiawei Shao016105b2018-04-12 16:38:31 +08002687 Shader *previousShader = nullptr;
2688 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002689 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002690 Shader *currentShader = mState.mAttachedShaders[shaderType];
2691 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002692 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002693 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002694 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002695
2696 if (previousShader)
2697 {
jchen103fd614d2018-08-13 12:21:58 +08002698 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002699 {
2700 return false;
2701 }
2702 }
2703 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002704 }
2705
jchen103fd614d2018-08-13 12:21:58 +08002706 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002707 {
2708 return false;
2709 }
2710
jchen103fd614d2018-08-13 12:21:58 +08002711 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002712 {
2713 return false;
2714 }
2715
2716 return true;
2717}
2718
2719// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2720// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002721bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002722 gl::Shader *consumingShader,
2723 gl::InfoLog &infoLog) const
2724{
jchen103fd614d2018-08-13 12:21:58 +08002725 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002726
jchen103fd614d2018-08-13 12:21:58 +08002727 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2728 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002729
Jiawei Shao385b3e02018-03-21 09:43:28 +08002730 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002731
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002732 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002733 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002734 bool matched = false;
2735
2736 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002737 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002738 {
2739 continue;
2740 }
2741
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002742 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002743 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002744 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002745 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002746 ASSERT(!output.isBuiltIn());
2747
2748 std::string mismatchedStructFieldName;
2749 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002750 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002751 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002752 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002753 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002754 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2755 mismatchedStructFieldName, generatingShader->getType(),
2756 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002757 return false;
2758 }
2759
Geoff Lang7dd2e102014-11-10 15:19:26 -05002760 matched = true;
2761 break;
2762 }
2763 }
2764
Olli Etuaho107c7242018-03-20 15:45:35 +02002765 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2766 // whether the input is statically used - a statically used input should fail this test even
2767 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002768 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002769 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002770 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2771 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2772 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002773 return false;
2774 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002775 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002776
Jiawei Shaod063aff2018-02-22 10:19:09 +08002777 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002778
Jiawei Shaod063aff2018-02-22 10:19:09 +08002779 return true;
2780}
2781
jchen103fd614d2018-08-13 12:21:58 +08002782bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002783{
Jiawei Shao016105b2018-04-12 16:38:31 +08002784 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002785
2786 std::map<GLuint, std::string> staticFragmentInputLocations;
2787
2788 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002789 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002790 for (const sh::Varying &input : fragmentInputVaryings)
2791 {
2792 if (input.isBuiltIn() || !input.staticUse)
2793 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002794 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002795 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002796
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002797 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002798 if (inputBinding == -1)
2799 continue;
2800
2801 const auto it = staticFragmentInputLocations.find(inputBinding);
2802 if (it == std::end(staticFragmentInputLocations))
2803 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002804 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002805 }
2806 else
2807 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002808 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002809 << it->second;
2810 return false;
2811 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002812 }
2813
2814 return true;
2815}
2816
jchen103fd614d2018-08-13 12:21:58 +08002817bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002818 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002819 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002820 GLuint *combinedImageUniformsCount,
2821 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002822{
Olli Etuahob78707c2017-03-09 15:03:11 +00002823 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002824 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002825 {
2826 return false;
2827 }
2828
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002829 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002830
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002831 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002832
jchen10eaef1e52017-06-13 10:44:11 +08002833 if (!linkAtomicCounterBuffers())
2834 {
2835 return false;
2836 }
2837
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002838 return true;
2839}
2840
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002841void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002842{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002843 ASSERT(combinedImageUniforms);
2844
Jamie Madill982f6e02017-06-07 14:33:04 -04002845 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2846 unsigned int low = high;
2847
jchen10eaef1e52017-06-13 10:44:11 +08002848 for (auto counterIter = mState.mUniforms.rbegin();
2849 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2850 {
2851 --low;
2852 }
2853
2854 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2855
2856 high = low;
2857
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002858 for (auto imageIter = mState.mUniforms.rbegin();
2859 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2860 {
2861 --low;
2862 }
2863
2864 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002865 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002866 // If uniform is a image type, insert it into the mImageBindings array.
2867 for (unsigned int imageIndex : mState.mImageUniformRange)
2868 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002869 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2870 // cannot load values into a uniform defined as an image. if declare without a
2871 // binding qualifier, any uniform image variable (include all elements of
2872 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002873 auto &imageUniform = mState.mUniforms[imageIndex];
2874 if (imageUniform.binding == -1)
2875 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002876 mState.mImageBindings.emplace_back(
2877 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002878 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002879 else
2880 {
2881 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002882 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002883 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002884
2885 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2886 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002887 }
2888
2889 high = low;
2890
2891 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002892 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002893 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002894 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002895 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002896
2897 mState.mSamplerUniformRange = RangeUI(low, high);
2898
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002899 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002900 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002901 {
2902 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002903 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002904 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2905 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002906 }
2907}
2908
jchen10eaef1e52017-06-13 10:44:11 +08002909bool Program::linkAtomicCounterBuffers()
2910{
2911 for (unsigned int index : mState.mAtomicCounterUniformRange)
2912 {
Jamie Madill493f9572018-05-24 19:52:15 -04002913 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002914 uniform.blockInfo.offset = uniform.offset;
2915 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2916 uniform.blockInfo.matrixStride = 0;
2917 uniform.blockInfo.isRowMajorMatrix = false;
2918
Jamie Madill493f9572018-05-24 19:52:15 -04002919 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002920 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2921 ++bufferIndex)
2922 {
2923 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2924 if (buffer.binding == uniform.binding)
2925 {
2926 buffer.memberIndexes.push_back(index);
2927 uniform.bufferIndex = bufferIndex;
2928 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002929 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002930 break;
2931 }
2932 }
2933 if (!found)
2934 {
2935 AtomicCounterBuffer atomicCounterBuffer;
2936 atomicCounterBuffer.binding = uniform.binding;
2937 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002938 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002939 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2940 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2941 }
2942 }
2943 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002944 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002945
2946 return true;
2947}
2948
Jamie Madilleb979bf2016-11-15 12:28:46 -05002949// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002950bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002951{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002952 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002953
jchen103fd614d2018-08-13 12:21:58 +08002954 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002955
Geoff Lang7dd2e102014-11-10 15:19:26 -05002956 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002957 if (shaderVersion >= 300)
2958 {
2959 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2960 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002961 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002962 }
2963 else
2964 {
2965 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002966 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002967 }
jchen103fd614d2018-08-13 12:21:58 +08002968 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002969
2970 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002971 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002972 {
Jamie Madillf6113162015-05-07 11:49:21 -04002973 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002974 return false;
2975 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002976
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002977 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002978
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002979 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002980 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002981 {
Olli Etuahod2551232017-10-26 20:03:33 +03002982 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2983 // structures, so we don't need to worry about adjusting their names or generating entries
2984 // for each member/element (unlike uniforms for example).
2985 ASSERT(!attribute.isArray() && !attribute.isStruct());
2986
Jamie Madilleb979bf2016-11-15 12:28:46 -05002987 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002988 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002989 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002990 attribute.location = bindingLocation;
2991 }
2992
2993 if (attribute.location != -1)
2994 {
2995 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002996 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002997
Jamie Madill63805b42015-08-25 13:17:39 -04002998 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002999 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003000 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3001 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003002
3003 return false;
3004 }
3005
Jamie Madill63805b42015-08-25 13:17:39 -04003006 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003007 {
Jamie Madill63805b42015-08-25 13:17:39 -04003008 const int regLocation = attribute.location + reg;
3009 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003010
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003011 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3012 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3013 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003014 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003015 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003016 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003017 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003018 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003019 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003020 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003021 return false;
3022 }
3023 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003024 else
3025 {
Jamie Madill63805b42015-08-25 13:17:39 -04003026 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003027 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003028
Jamie Madill63805b42015-08-25 13:17:39 -04003029 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003030 }
3031 }
3032 }
3033
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003034 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003035 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003036 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003037 // Not set by glBindAttribLocation or by location layout qualifier
3038 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003039 {
Jamie Madill63805b42015-08-25 13:17:39 -04003040 int regs = VariableRegisterCount(attribute.type);
3041 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003042
Jamie Madill63805b42015-08-25 13:17:39 -04003043 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003044 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003045 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003046 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003047 }
3048
Jamie Madillc349ec02015-08-21 16:53:12 -04003049 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003050 }
3051 }
3052
Brandon Jonesc405ae72017-12-06 14:15:03 -08003053 ASSERT(mState.mAttributesTypeMask.none());
3054 ASSERT(mState.mAttributesMask.none());
3055
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003056 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3057 // shader versions we're only processing active attributes to begin with.
3058 if (shaderVersion >= 300)
3059 {
3060 for (auto attributeIter = mState.mAttributes.begin();
3061 attributeIter != mState.mAttributes.end();)
3062 {
3063 if (attributeIter->active)
3064 {
3065 ++attributeIter;
3066 }
3067 else
3068 {
3069 attributeIter = mState.mAttributes.erase(attributeIter);
3070 }
3071 }
3072 }
3073
Jamie Madill48ef11b2016-04-27 15:21:52 -04003074 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003075 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003076 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003077 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003078 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003079
Jamie Madillbd159f02017-10-09 19:39:06 -04003080 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003081 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003082 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3083 mState.mActiveAttribLocationsMask.set(location);
3084 mState.mMaxActiveAttribLocation =
3085 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003086
3087 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3088 if (!attribute.isBuiltIn())
3089 {
3090 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3091 location);
3092 mState.mAttributesMask.set(location);
3093 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003094 }
3095 }
3096
Geoff Lang7dd2e102014-11-10 15:19:26 -05003097 return true;
3098}
3099
jchen103fd614d2018-08-13 12:21:58 +08003100bool Program::linkInterfaceBlocks(const Caps &caps,
3101 const Version &version,
3102 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003103 InfoLog &infoLog,
3104 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003105{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003106 ASSERT(combinedShaderStorageBlocksCount);
3107
Jiawei Shao54aafe52018-04-27 14:54:57 +08003108 GLuint combinedUniformBlocksCount = 0u;
3109 GLuint numShadersHasUniformBlocks = 0u;
3110 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3111 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003112 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003113 Shader *shader = mState.mAttachedShaders[shaderType];
3114 if (!shader)
3115 {
3116 continue;
3117 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003118
jchen103fd614d2018-08-13 12:21:58 +08003119 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003120 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003121 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003122 if (!ValidateInterfaceBlocksCount(
3123 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3124 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3125 {
3126 return false;
3127 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003128
Jiawei Shao54aafe52018-04-27 14:54:57 +08003129 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3130 ++numShadersHasUniformBlocks;
3131 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003132 }
3133
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003134 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3135 {
3136 infoLog << "The sum of the number of active uniform blocks exceeds "
3137 "MAX_COMBINED_UNIFORM_BLOCKS ("
3138 << caps.maxCombinedUniformBlocks << ").";
3139 return false;
3140 }
3141
Jiawei Shao54aafe52018-04-27 14:54:57 +08003142 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3143 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003144 {
3145 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003146 }
Jamie Madille473dee2015-08-18 14:49:01 -04003147
jchen103fd614d2018-08-13 12:21:58 +08003148 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003149 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003150 *combinedShaderStorageBlocksCount = 0u;
3151 GLuint numShadersHasShaderStorageBlocks = 0u;
3152 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3153 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003154 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003155 Shader *shader = mState.mAttachedShaders[shaderType];
3156 if (!shader)
3157 {
3158 continue;
3159 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003160
jchen103fd614d2018-08-13 12:21:58 +08003161 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003162 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003163 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003164 if (!ValidateInterfaceBlocksCount(
3165 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3166 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3167 {
3168 return false;
3169 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003170
Jiawei Shao54aafe52018-04-27 14:54:57 +08003171 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3172 ++numShadersHasShaderStorageBlocks;
3173 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003174 }
3175
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003176 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003177 {
3178 infoLog << "The sum of the number of active shader storage blocks exceeds "
3179 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3180 << caps.maxCombinedShaderStorageBlocks << ").";
3181 return false;
3182 }
3183
Jiawei Shao54aafe52018-04-27 14:54:57 +08003184 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3185 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003186 {
3187 return false;
3188 }
3189 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003190
Geoff Lang7dd2e102014-11-10 15:19:26 -05003191 return true;
3192}
3193
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003194LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3195 const sh::ShaderVariable &variable2,
3196 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003197 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003198 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003199{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003200 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003201 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003202 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003203 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003204 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003205 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003206 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003207 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003208 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003209 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003210 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003211 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003212 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003213 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003214 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003215 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003216
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003217 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003218 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003219 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003220 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003221 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003222 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3223 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003224 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3225 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003226
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003227 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003228 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003229 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003230 }
3231
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003232 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003233 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003234 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003235 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003236 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3237 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003238 }
3239 }
3240
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003241 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003242}
3243
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003244LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3245 const sh::Varying &inputVarying,
3246 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003247 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003248 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003249{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003250 if (validateGeometryShaderInputVarying)
3251 {
3252 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3253 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3254 // or outputs.
3255 ASSERT(inputVarying.arraySizes.size() == 1u);
3256
3257 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3258 // varying cannot match a geometry shader input varying.
3259 // [GL_EXT_geometry_shader] Section 7.4.1:
3260 // Geometry shader per-vertex input variables and blocks are required to be declared as
3261 // arrays, with each element representing input or output values for a single vertex of a
3262 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3263 // are treated as though they were not declared as arrays.
3264 if (outputVarying.isArray())
3265 {
3266 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3267 }
3268 }
3269
3270 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3271 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003272 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003273 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3274 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003275 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003276 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003277 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003278 }
3279
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003280 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003281 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003282 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003283 }
3284
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003285 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003286 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003287 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003288 }
3289
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003290 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003291}
3292
jchen103fd614d2018-08-13 12:21:58 +08003293bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003294{
Jiawei Shao016105b2018-04-12 16:38:31 +08003295 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3296 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003297 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3298 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3299 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003300
3301 if (shaderVersion != 100)
3302 {
3303 // Only ESSL 1.0 has restrictions on matching input and output invariance
3304 return true;
3305 }
3306
3307 bool glPositionIsInvariant = false;
3308 bool glPointSizeIsInvariant = false;
3309 bool glFragCoordIsInvariant = false;
3310 bool glPointCoordIsInvariant = false;
3311
3312 for (const sh::Varying &varying : vertexVaryings)
3313 {
3314 if (!varying.isBuiltIn())
3315 {
3316 continue;
3317 }
3318 if (varying.name.compare("gl_Position") == 0)
3319 {
3320 glPositionIsInvariant = varying.isInvariant;
3321 }
3322 else if (varying.name.compare("gl_PointSize") == 0)
3323 {
3324 glPointSizeIsInvariant = varying.isInvariant;
3325 }
3326 }
3327
3328 for (const sh::Varying &varying : fragmentVaryings)
3329 {
3330 if (!varying.isBuiltIn())
3331 {
3332 continue;
3333 }
3334 if (varying.name.compare("gl_FragCoord") == 0)
3335 {
3336 glFragCoordIsInvariant = varying.isInvariant;
3337 }
3338 else if (varying.name.compare("gl_PointCoord") == 0)
3339 {
3340 glPointCoordIsInvariant = varying.isInvariant;
3341 }
3342 }
3343
3344 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3345 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3346 // Not requiring invariance to match is supported by:
3347 // dEQP, WebGL CTS, Nexus 5X GLES
3348 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3349 {
3350 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3351 "declared invariant.";
3352 return false;
3353 }
3354 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3355 {
3356 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3357 "declared invariant.";
3358 return false;
3359 }
3360
3361 return true;
3362}
3363
jchen103fd614d2018-08-13 12:21:58 +08003364bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003365 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003366 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003367 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003368{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003369
jchen108225e732017-11-14 16:29:03 +08003370 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003371 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003372 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003373 {
jchen103fd614d2018-08-13 12:21:58 +08003374 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003375 {
Geoff Lang1a683462015-09-29 15:09:59 -04003376 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003377 return false;
3378 }
jchen103fd614d2018-08-13 12:21:58 +08003379 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003380 {
3381 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3382 {
3383 infoLog << "Two transform feedback varyings include the same array element ("
3384 << tfVaryingName << ").";
3385 return false;
3386 }
3387 }
3388 else
3389 {
3390 if (uniqueNames.count(tfVaryingName) > 0)
3391 {
3392 infoLog << "Two transform feedback varyings specify the same output variable ("
3393 << tfVaryingName << ").";
3394 return false;
3395 }
3396 }
3397 uniqueNames.insert(tfVaryingName);
3398 }
3399
3400 // Validate against program varyings.
3401 size_t totalComponents = 0;
3402 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3403 {
3404 std::vector<unsigned int> subscripts;
3405 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3406
3407 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3408 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003409 {
3410 infoLog << "Transform feedback varying " << tfVaryingName
3411 << " does not exist in the vertex shader.";
3412 return false;
3413 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003414
jchen108225e732017-11-14 16:29:03 +08003415 // Validate the matching variable.
3416 if (var->isStruct())
3417 {
3418 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3419 return false;
3420 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003421
jchen108225e732017-11-14 16:29:03 +08003422 size_t elementCount = 0;
3423 size_t componentCount = 0;
3424
3425 if (var->isArray())
3426 {
jchen103fd614d2018-08-13 12:21:58 +08003427 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003428 {
3429 infoLog << "Capture of arrays is undefined and not supported.";
3430 return false;
3431 }
3432
3433 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3434 ASSERT(!var->isArrayOfArrays());
3435
3436 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3437 {
3438 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3439 return false;
3440 }
3441 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3442 }
3443 else
3444 {
3445 if (!subscripts.empty())
3446 {
3447 infoLog << "Varying '" << baseName
3448 << "' is not an array to be captured by element.";
3449 return false;
3450 }
3451 elementCount = 1;
3452 }
3453
3454 // TODO(jmadill): Investigate implementation limits on D3D11
3455 componentCount = VariableComponentCount(var->type) * elementCount;
3456 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3457 componentCount > caps.maxTransformFeedbackSeparateComponents)
3458 {
3459 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3460 << componentCount << ") exceed the maximum separate components ("
3461 << caps.maxTransformFeedbackSeparateComponents << ").";
3462 return false;
3463 }
3464
3465 totalComponents += componentCount;
3466 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3467 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3468 {
3469 infoLog << "Transform feedback varying total components (" << totalComponents
3470 << ") exceed the maximum interleaved components ("
3471 << caps.maxTransformFeedbackInterleavedComponents << ").";
3472 return false;
3473 }
3474 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003475 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003476}
3477
jchen103fd614d2018-08-13 12:21:58 +08003478bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003479{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003480 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003481 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003482
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003483 for (const auto &attrib : attributes)
3484 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003485 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003486 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003487 Shader *shader = mState.mAttachedShaders[shaderType];
3488 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003489 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003490 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003491 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003492
jchen103fd614d2018-08-13 12:21:58 +08003493 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003494 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003495 {
3496 if (uniform.name == attrib.name)
3497 {
3498 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3499 return false;
3500 }
3501 }
3502 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003503 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003504
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003505 return true;
3506}
3507
Jamie Madill3c1da042017-11-27 18:33:40 -05003508void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003509{
3510 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003511 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003512 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003513 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003514 std::vector<unsigned int> subscripts;
3515 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003516 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003517 if (!subscripts.empty())
3518 {
3519 subscript = subscripts.back();
3520 }
Jamie Madill192745a2016-12-22 15:58:21 -05003521 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003522 {
Jamie Madill192745a2016-12-22 15:58:21 -05003523 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003524 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003525 {
jchen10a9042d32017-03-17 08:50:45 +08003526 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3527 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003528 break;
3529 }
jchen108225e732017-11-14 16:29:03 +08003530 else if (varying->isStruct())
3531 {
3532 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3533 if (field != nullptr)
3534 {
3535 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3536 break;
3537 }
3538 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003539 }
3540 }
James Darpinian30b604d2018-03-12 17:26:57 -07003541 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003542}
3543
jchen103fd614d2018-08-13 12:21:58 +08003544ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003545{
Jamie Madill3c1da042017-11-27 18:33:40 -05003546 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003547
Jiawei Shao016105b2018-04-12 16:38:31 +08003548 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003549 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003550 {
Jamie Madill192745a2016-12-22 15:58:21 -05003551 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003552 }
3553
Jiawei Shao016105b2018-04-12 16:38:31 +08003554 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003555 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003556 {
Jamie Madill192745a2016-12-22 15:58:21 -05003557 merged[varying.name].fragment = &varying;
3558 }
3559
3560 return merged;
3561}
3562
Olli Etuaho0ca09752018-09-24 11:00:50 +03003563bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3564{
3565 return a.getArraySizeProduct() > b.getArraySizeProduct();
3566}
3567
3568int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3569{
3570 if (outputVariable.location != -1)
3571 {
3572 return outputVariable.location;
3573 }
3574 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3575 if (apiLocation != -1)
3576 {
3577 return apiLocation;
3578 }
3579 return -1;
3580}
3581
3582bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3583{
3584 if (outputVariable.index != -1)
3585 {
3586 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3587 return (outputVariable.index == 1);
3588 }
3589 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3590 if (apiIndex != -1)
3591 {
3592 // Index layout qualifier from the shader takes precedence, so the index from the API is
3593 // checked only if the index was not set in the shader. This is not specified in the EXT
3594 // spec, but is specified in desktop OpenGL specs.
3595 return (apiIndex == 1);
3596 }
3597 // EXT_blend_func_extended: Outputs get index 0 by default.
3598 return false;
3599}
3600
jchen103fd614d2018-08-13 12:21:58 +08003601bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003602 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003603 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003604 GLuint combinedImageUniformsCount,
3605 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003606{
Jiawei Shao016105b2018-04-12 16:38:31 +08003607 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003608 ASSERT(fragmentShader != nullptr);
3609
Geoff Lange0cff192017-05-30 13:04:56 -04003610 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003611 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003612 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003613
jchen103fd614d2018-08-13 12:21:58 +08003614 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003615 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003616 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003617 {
3618 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3619 outputVariable.name != "gl_FragData")
3620 {
3621 continue;
3622 }
3623
3624 unsigned int baseLocation =
3625 (outputVariable.location == -1 ? 0u
3626 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003627
3628 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3629 // structures, so we may use getBasicTypeElementCount().
3630 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3631 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003632 {
3633 const unsigned int location = baseLocation + elementIndex;
3634 if (location >= mState.mOutputVariableTypes.size())
3635 {
3636 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3637 }
Corentin Walleze7557742017-06-01 13:09:57 -04003638 ASSERT(location < mState.mActiveOutputVariables.size());
3639 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003640 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003641 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003642 }
3643 }
3644
jchen103fd614d2018-08-13 12:21:58 +08003645 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003646 {
3647 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3648 // A link error will be generated if the sum of the number of active image uniforms used in
3649 // all shaders, the number of active shader storage blocks, and the number of active
3650 // fragment shader outputs exceeds the implementation-dependent value of
3651 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3652 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3653 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003654 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003655 {
3656 mInfoLog
3657 << "The sum of the number of active image uniforms, active shader storage blocks "
3658 "and active fragment shader outputs exceeds "
3659 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003660 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003661 return false;
3662 }
3663 }
3664
Jamie Madill80a6fc02015-08-21 16:53:16 -04003665 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003666 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003667 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003668
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003669 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003670 // TODO(jmadill): any caps validation here?
3671
Olli Etuaho0ca09752018-09-24 11:00:50 +03003672 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3673 {
3674 if (outputVariable.isArray())
3675 {
3676 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3677 // Resources and including [0] at the end of array variable names.
3678 outputVariable.name += "[0]";
3679 outputVariable.mappedName += "[0]";
3680 }
3681 }
3682
3683 bool hasSecondaryOutputs = false;
3684
3685 // Reserve locations for output variables whose location is fixed in the shader or through the
3686 // API.
jchen1015015f72017-03-16 13:54:21 +08003687 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003688 outputVariableIndex++)
3689 {
jchen1015015f72017-03-16 13:54:21 +08003690 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003691
3692 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3693 if (outputVariable.isBuiltIn())
3694 continue;
3695
Olli Etuaho0ca09752018-09-24 11:00:50 +03003696 int baseLocation = getOutputLocationForLink(outputVariable);
3697 if (baseLocation == -1)
3698 {
3699 // Here we're only reserving locations for variables whose location is fixed.
3700 continue;
3701 }
3702
3703 auto *outputLocations = &mState.mOutputLocations;
3704 if (isOutputSecondaryForLink(outputVariable))
3705 {
3706 outputLocations = &mState.mSecondaryOutputLocations;
3707 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3708 // an output has an index specified it will always also have the location specified.
3709 hasSecondaryOutputs = true;
3710 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003711
Olli Etuaho465835d2017-09-26 13:34:10 +03003712 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3713 // structures, so we may use getBasicTypeElementCount().
3714 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003715 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3716 if (outputLocationsNeeded > outputLocations->size())
3717 {
3718 outputLocations->resize(outputLocationsNeeded);
3719 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003720 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003721 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003722 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3723 ASSERT(location < outputLocations->size());
3724 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003725 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003726 mInfoLog << "Location of variable " << outputVariable.name
3727 << " conflicts with another variable.";
3728 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003729 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003730 if (outputVariable.isArray())
3731 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003732 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003733 }
3734 else
3735 {
3736 VariableLocation locationInfo;
3737 locationInfo.index = outputVariableIndex;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003738 (*outputLocations)[location] = locationInfo;
3739 }
3740 }
3741 }
3742
3743 // Here we assign locations for the output variables that don't yet have them. Note that we're
3744 // not necessarily able to fit the variables optimally, since then we might have to try
3745 // different arrangements of output arrays. Now we just assign the locations in the order that
3746 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3747 // finding locations for the output variables, so this should be acceptable at least for now.
3748 GLuint maxLocation = caps.maxDrawBuffers;
3749 if (hasSecondaryOutputs)
3750 {
3751 // EXT_blend_func_extended: Program outputs will be validated against
3752 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3753 maxLocation = extensions.maxDualSourceDrawBuffers;
3754 }
3755
3756 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3757 outputVariableIndex++)
3758 {
3759 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3760
3761 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3762 if (outputVariable.isBuiltIn())
3763 continue;
3764
3765 if (getOutputLocationForLink(outputVariable) != -1)
3766 {
3767 continue;
3768 }
3769
3770 auto *outputLocations = &mState.mOutputLocations;
3771 if (isOutputSecondaryForLink(outputVariable))
3772 {
3773 outputLocations = &mState.mSecondaryOutputLocations;
3774 }
3775
3776 int baseLocation = 0;
3777 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3778 bool elementsFit = false;
3779 while (!elementsFit)
3780 {
3781 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3782 elementsFit = true;
3783 if (baseLocation + elementCount > maxLocation)
3784 {
3785 // EXT_blend_func_extended: Linking can fail:
3786 // "if the explicit binding assignments do not leave enough space for the linker to
3787 // automatically assign a location for a varying out array, which requires multiple
3788 // contiguous locations."
3789 mInfoLog << "Could not fit output variable into available locations: "
3790 << outputVariable.name;
3791 return false;
3792 }
3793 unsigned int outputLocationsNeeded =
3794 static_cast<unsigned int>(baseLocation) + elementCount;
3795 if (outputLocationsNeeded > outputLocations->size())
3796 {
3797 outputLocations->resize(outputLocationsNeeded);
3798 }
3799 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3800 {
3801 const unsigned int location =
3802 static_cast<unsigned int>(baseLocation) + elementIndex;
3803 ASSERT(location < outputLocations->size());
3804 if (outputLocations->at(location).used())
3805 {
3806 elementsFit = false;
3807 break;
3808 }
3809 }
3810 if (elementsFit)
3811 {
3812 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3813 {
3814 const unsigned int location =
3815 static_cast<unsigned int>(baseLocation) + elementIndex;
3816 if (outputVariable.isArray())
3817 {
3818 (*outputLocations)[location] =
3819 VariableLocation(elementIndex, outputVariableIndex);
3820 }
3821 else
3822 {
3823 VariableLocation locationInfo;
3824 locationInfo.index = outputVariableIndex;
3825 (*outputLocations)[location] = locationInfo;
3826 }
3827 }
3828 }
3829 else
3830 {
3831 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003832 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003833 }
3834 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003835
3836 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003837}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003838
Olli Etuaho48fed632017-03-16 12:05:30 +00003839void Program::setUniformValuesFromBindingQualifiers()
3840{
Jamie Madill982f6e02017-06-07 14:33:04 -04003841 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003842 {
3843 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3844 if (samplerUniform.binding != -1)
3845 {
Olli Etuahod2551232017-10-26 20:03:33 +03003846 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003847 ASSERT(location != -1);
3848 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003849 for (unsigned int elementIndex = 0;
3850 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003851 {
3852 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3853 }
3854 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3855 boundTextureUnits.data());
3856 }
3857 }
3858}
3859
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003860void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003861{
jchen10af713a22017-04-19 09:10:56 +08003862 // Set initial bindings from shader.
3863 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3864 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003865 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003866 bindUniformBlock(blockIndex, uniformBlock.binding);
3867 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003868}
3869
Jamie Madille7d84322017-01-10 18:21:59 -05003870void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003871 GLsizei clampedCount,
3872 const GLint *v)
3873{
Jamie Madill81c2e252017-09-09 23:32:46 -04003874 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3875 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003876 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3877 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003878
Jamie Madill7e4eff12018-08-08 15:49:26 -04003879 if (samplerBinding.unreferenced)
3880 return;
3881
3882 // Update the sampler uniforms.
3883 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3884 {
3885 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3886 GLint newSamplerIndex = v[arrayIndex];
3887
3888 if (oldSamplerIndex == newSamplerIndex)
3889 continue;
3890
3891 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3892
3893 // Update the reference counts.
3894 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3895 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3896 ASSERT(oldRefCount > 0);
3897 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3898 oldRefCount--;
3899 newRefCount++;
3900
3901 // Check for binding type change.
3902 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3903 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3904
3905 if (newRefCount == 1)
3906 {
3907 newSamplerType = samplerBinding.textureType;
3908 mState.mActiveSamplersMask.set(newSamplerIndex);
3909 }
3910 else if (newSamplerType != samplerBinding.textureType)
3911 {
3912 // Conflict detected. Ensure we reset it properly.
3913 newSamplerType = TextureType::InvalidEnum;
3914 }
3915
3916 // Unset previously active sampler.
3917 if (oldRefCount == 0)
3918 {
3919 oldSamplerType = TextureType::InvalidEnum;
3920 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3921 }
3922 else if (oldSamplerType == TextureType::InvalidEnum)
3923 {
3924 // Previous conflict. Check if this new change fixed the conflict.
3925 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3926 }
3927 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003928
3929 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003930 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003931}
3932
Jamie Madill7e4eff12018-08-08 15:49:26 -04003933TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3934{
3935 TextureType foundType = TextureType::InvalidEnum;
3936
3937 for (const SamplerBinding &binding : mSamplerBindings)
3938 {
3939 if (binding.unreferenced)
3940 continue;
3941
3942 // A conflict exists if samplers of different types are sourced by the same texture unit.
3943 // We need to check all bound textures to detect this error case.
3944 for (GLuint textureUnit : binding.boundTextureUnits)
3945 {
3946 if (textureUnit == textureUnitIndex)
3947 {
3948 if (foundType == TextureType::InvalidEnum)
3949 {
3950 foundType = binding.textureType;
3951 }
3952 else if (foundType != binding.textureType)
3953 {
3954 return TextureType::InvalidEnum;
3955 }
3956 }
3957 }
3958 }
3959
3960 return foundType;
3961}
3962
Jamie Madille7d84322017-01-10 18:21:59 -05003963template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003964GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3965 GLsizei count,
3966 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003967 const T *v)
3968{
Jamie Madill134f93d2017-08-31 17:11:00 -04003969 if (count == 1)
3970 return 1;
3971
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003972 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003973
Corentin Wallez15ac5342016-11-03 17:06:39 -04003974 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3975 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003976 unsigned int remainingElements =
3977 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003978 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003979 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003980
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003981 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003982 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003983 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003984 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003985
3986 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003987}
3988
3989template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003990GLsizei Program::clampMatrixUniformCount(GLint location,
3991 GLsizei count,
3992 GLboolean transpose,
3993 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003994{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003995 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3996
Jamie Madill62d31cb2015-09-11 13:25:51 -04003997 if (!transpose)
3998 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003999 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004000 }
4001
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004002 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004003
4004 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4005 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004006 unsigned int remainingElements =
4007 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004008 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004009}
4010
Jamie Madill54164b02017-08-28 15:17:37 -04004011// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4012// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004013template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004014void Program::getUniformInternal(const Context *context,
4015 DestT *dataOut,
4016 GLint location,
4017 GLenum nativeType,
4018 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004019{
Jamie Madill54164b02017-08-28 15:17:37 -04004020 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004021 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004022 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004023 {
4024 GLint tempValue[16] = {0};
4025 mProgram->getUniformiv(context, location, tempValue);
4026 UniformStateQueryCastLoop<GLboolean>(
4027 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004028 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004029 }
4030 case GL_INT:
4031 {
4032 GLint tempValue[16] = {0};
4033 mProgram->getUniformiv(context, location, tempValue);
4034 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4035 components);
4036 break;
4037 }
4038 case GL_UNSIGNED_INT:
4039 {
4040 GLuint tempValue[16] = {0};
4041 mProgram->getUniformuiv(context, location, tempValue);
4042 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4043 components);
4044 break;
4045 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004046 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004047 {
4048 GLfloat tempValue[16] = {0};
4049 mProgram->getUniformfv(context, location, tempValue);
4050 UniformStateQueryCastLoop<GLfloat>(
4051 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004052 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004053 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004054 default:
4055 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004056 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004057 }
4058}
Jamie Madilla4595b82017-01-11 17:36:34 -05004059
4060bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4061{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004062 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004063 // Must be called after samplers are validated.
4064 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4065
4066 for (const auto &binding : mState.mSamplerBindings)
4067 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004068 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004069 for (const auto &unit : binding.boundTextureUnits)
4070 {
4071 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4072 if (programTextureID == textureID)
4073 {
4074 // TODO(jmadill): Check for appropriate overlap.
4075 return true;
4076 }
4077 }
4078 }
4079
4080 return false;
4081}
4082
Jamie Madill6f755b22018-10-09 12:48:54 -04004083angle::Result Program::syncState(const Context *context)
Jamie Madill70aeda42018-08-20 12:17:40 -04004084{
4085 if (mDirtyBits.any())
4086 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004087 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004088 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4089 mDirtyBits.reset();
4090 }
4091
Jamie Madill6f755b22018-10-09 12:48:54 -04004092 return angle::Result::Continue();
Jamie Madill70aeda42018-08-20 12:17:40 -04004093}
Jamie Madilla2c74982016-12-12 11:20:42 -05004094} // namespace gl