blob: 7738173245b5d1ed68ba3580b24d586c0c91f8b7 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
Olli Etuahod2551232017-10-26 20:03:33 +030017#include "common/string_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/MemoryProgramCache.h"
Jamie Madill7af0de52017-11-06 17:09:33 -050022#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill437d2662014-12-05 14:23:35 -050023#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040024#include "libANGLE/Uniform.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040025#include "libANGLE/VaryingPacking.h"
jchen103fd614d2018-08-13 12:21:58 +080026#include "libANGLE/Version.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040028#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040029#include "libANGLE/queryconversions.h"
30#include "libANGLE/renderer/GLImplFactory.h"
31#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040032#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050033
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000036
Geoff Lang7dd2e102014-11-10 15:19:26 -050037namespace
38{
39
Jamie Madill62d31cb2015-09-11 13:25:51 -040040// This simplified cast function doesn't need to worry about advanced concepts like
41// depth range values, or casting to bool.
42template <typename DestT, typename SrcT>
43DestT UniformStateQueryCast(SrcT value);
44
45// From-Float-To-Integer Casts
46template <>
47GLint UniformStateQueryCast(GLfloat value)
48{
49 return clampCast<GLint>(roundf(value));
50}
51
52template <>
53GLuint UniformStateQueryCast(GLfloat value)
54{
55 return clampCast<GLuint>(roundf(value));
56}
57
58// From-Integer-to-Integer Casts
59template <>
60GLint UniformStateQueryCast(GLuint value)
61{
62 return clampCast<GLint>(value);
63}
64
65template <>
66GLuint UniformStateQueryCast(GLint value)
67{
68 return clampCast<GLuint>(value);
69}
70
71// From-Boolean-to-Anything Casts
72template <>
73GLfloat UniformStateQueryCast(GLboolean value)
74{
Geoff Lang92019432017-11-20 13:09:34 -050075 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040076}
77
78template <>
79GLint UniformStateQueryCast(GLboolean value)
80{
Geoff Lang92019432017-11-20 13:09:34 -050081 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040082}
83
84template <>
85GLuint UniformStateQueryCast(GLboolean value)
86{
Geoff Lang92019432017-11-20 13:09:34 -050087 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040088}
89
90// Default to static_cast
91template <typename DestT, typename SrcT>
92DestT UniformStateQueryCast(SrcT value)
93{
94 return static_cast<DestT>(value);
95}
96
97template <typename SrcT, typename DestT>
98void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
99{
100 for (int comp = 0; comp < components; ++comp)
101 {
102 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
103 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
104 size_t offset = comp * 4;
105 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
106 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
107 }
108}
109
jchen1015015f72017-03-16 13:54:21 +0800110template <typename VarT>
111GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
112{
Olli Etuahod2551232017-10-26 20:03:33 +0300113 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800114 for (size_t index = 0; index < list.size(); index++)
115 {
116 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300117 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800118 {
Olli Etuahod2551232017-10-26 20:03:33 +0300119 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800120 }
121 }
122
123 return GL_INVALID_INDEX;
124}
125
Olli Etuahod2551232017-10-26 20:03:33 +0300126template <typename VarT>
127GLint GetVariableLocation(const std::vector<VarT> &list,
128 const std::vector<VariableLocation> &locationList,
129 const std::string &name)
130{
131 size_t nameLengthWithoutArrayIndex;
132 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
133
134 for (size_t location = 0u; location < locationList.size(); ++location)
135 {
136 const VariableLocation &variableLocation = locationList[location];
137 if (!variableLocation.used())
138 {
139 continue;
140 }
141
142 const VarT &variable = list[variableLocation.index];
143
144 if (angle::BeginsWith(variable.name, name))
145 {
146 if (name.length() == variable.name.length())
147 {
148 ASSERT(name == variable.name);
149 // GLES 3.1 November 2016 page 87.
150 // The string exactly matches the name of the active variable.
151 return static_cast<GLint>(location);
152 }
153 if (name.length() + 3u == variable.name.length() && variable.isArray())
154 {
155 ASSERT(name + "[0]" == variable.name);
156 // The string identifies the base name of an active array, where the string would
157 // exactly match the name of the variable if the suffix "[0]" were appended to the
158 // string.
159 return static_cast<GLint>(location);
160 }
161 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300162 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300163 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
164 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
165 {
166 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
167 // The string identifies an active element of the array, where the string ends with the
168 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
169 // zeroes, or whitespace) identifying an array element, and the "]" character, the
170 // integer is less than the number of active elements of the array variable, and where
171 // the string would exactly match the enumerated name of the array if the decimal
172 // integer were replaced with zero.
173 return static_cast<GLint>(location);
174 }
175 }
176
177 return -1;
178}
179
jchen10fd7c3b52017-03-21 15:36:03 +0800180void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
181{
182 ASSERT(bufSize > 0);
183 strncpy(buffer, string.c_str(), bufSize);
184 buffer[bufSize - 1] = '\0';
185
186 if (length)
187 {
188 *length = static_cast<GLsizei>(strlen(buffer));
189 }
190}
191
jchen10a9042d32017-03-17 08:50:45 +0800192bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
193{
Olli Etuahoc8538042017-09-27 11:20:15 +0300194 std::vector<unsigned int> subscripts;
195 std::string baseName = ParseResourceName(name, &subscripts);
196 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800197 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300198 std::vector<unsigned int> arrayIndices;
199 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
200 if (baseName == arrayName &&
201 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800202 {
203 return true;
204 }
205 }
206 return false;
207}
208
Jiawei Shao40786bd2018-04-18 13:58:57 +0800209std::string GetInterfaceBlockLimitName(ShaderType shaderType, sh::BlockType blockType)
210{
211 std::ostringstream stream;
212 stream << "GL_MAX_" << GetShaderTypeString(shaderType) << "_";
213
214 switch (blockType)
215 {
216 case sh::BlockType::BLOCK_UNIFORM:
217 stream << "UNIFORM_BUFFERS";
218 break;
219 case sh::BlockType::BLOCK_BUFFER:
220 stream << "SHADER_STORAGE_BLOCKS";
221 break;
222 default:
223 UNREACHABLE();
224 return "";
225 }
226
227 if (shaderType == ShaderType::Geometry)
228 {
229 stream << "_EXT";
230 }
231
232 return stream.str();
233}
234
235const char *GetInterfaceBlockTypeString(sh::BlockType blockType)
236{
237 switch (blockType)
238 {
239 case sh::BlockType::BLOCK_UNIFORM:
240 return "uniform block";
241 case sh::BlockType::BLOCK_BUFFER:
242 return "shader storage block";
243 default:
244 UNREACHABLE();
245 return "";
246 }
247}
248
249void LogInterfaceBlocksExceedLimit(InfoLog &infoLog,
250 ShaderType shaderType,
251 sh::BlockType blockType,
252 GLuint limit)
253{
254 infoLog << GetShaderTypeString(shaderType) << " shader "
255 << GetInterfaceBlockTypeString(blockType) << " count exceeds "
256 << GetInterfaceBlockLimitName(shaderType, blockType) << " (" << limit << ")";
257}
258
Jiawei Shao427071e2018-03-19 09:21:37 +0800259bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800260 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800261 ShaderType shaderType,
262 sh::BlockType blockType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800263 GLuint *combinedInterfaceBlocksCount,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800264 InfoLog &infoLog)
265{
266 GLuint blockCount = 0;
267 for (const sh::InterfaceBlock &block : interfaceBlocks)
268 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800269 if (IsActiveInterfaceBlock(block))
Jiajia Qin729b2c62017-08-14 09:36:11 +0800270 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800271 blockCount += std::max(block.arraySize, 1u);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800272 if (blockCount > maxInterfaceBlocks)
273 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800274 LogInterfaceBlocksExceedLimit(infoLog, shaderType, blockType, maxInterfaceBlocks);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800275 return false;
276 }
277 }
278 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800279
280 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
281 // If a uniform block is used by multiple shader stages, each such use counts separately
282 // against this combined limit.
283 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
284 // If a shader storage block in a program is referenced by multiple shaders, each such
285 // reference counts separately against this combined limit.
286 if (combinedInterfaceBlocksCount)
287 {
288 *combinedInterfaceBlocksCount += blockCount;
289 }
290
Jiajia Qin729b2c62017-08-14 09:36:11 +0800291 return true;
292}
293
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800294GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
295{
296 std::vector<unsigned int> subscripts;
297 std::string baseName = ParseResourceName(name, &subscripts);
298
299 unsigned int numBlocks = static_cast<unsigned int>(list.size());
300 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
301 {
302 const auto &block = list[blockIndex];
303 if (block.name == baseName)
304 {
305 const bool arrayElementZero =
306 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
307 const bool arrayElementMatches =
308 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
309 if (arrayElementMatches || arrayElementZero)
310 {
311 return blockIndex;
312 }
313 }
314 }
315
316 return GL_INVALID_INDEX;
317}
318
319void GetInterfaceBlockName(const GLuint index,
320 const std::vector<InterfaceBlock> &list,
321 GLsizei bufSize,
322 GLsizei *length,
323 GLchar *name)
324{
325 ASSERT(index < list.size());
326
327 const auto &block = list[index];
328
329 if (bufSize > 0)
330 {
331 std::string blockName = block.name;
332
333 if (block.isArray)
334 {
335 blockName += ArrayString(block.arrayElement);
336 }
337 CopyStringToBuffer(name, blockName, bufSize, length);
338 }
339}
340
jchen103fd614d2018-08-13 12:21:58 +0800341void InitUniformBlockLinker(const ProgramState &state, UniformBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500342{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800343 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500344 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800345 Shader *shader = state.getAttachedShader(shaderType);
346 if (shader)
347 {
jchen103fd614d2018-08-13 12:21:58 +0800348 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800349 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500350 }
351}
352
jchen103fd614d2018-08-13 12:21:58 +0800353void InitShaderStorageBlockLinker(const ProgramState &state, ShaderStorageBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500354{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800355 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500356 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800357 Shader *shader = state.getAttachedShader(shaderType);
358 if (shader != nullptr)
359 {
jchen103fd614d2018-08-13 12:21:58 +0800360 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800361 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500362 }
363}
364
jchen108225e732017-11-14 16:29:03 +0800365// Find the matching varying or field by name.
366const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
367 const std::string &name)
368{
369 const sh::ShaderVariable *var = nullptr;
370 for (const auto &ref : varyings)
371 {
372 const sh::Varying *varying = ref.second.get();
373 if (varying->name == name)
374 {
375 var = varying;
376 break;
377 }
378 var = FindShaderVarField(*varying, name);
379 if (var != nullptr)
380 {
381 break;
382 }
383 }
384 return var;
385}
386
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800387void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
388{
389 ASSERT(mismatchedFieldName);
390 if (mismatchedFieldName->empty())
391 {
392 *mismatchedFieldName = parentName;
393 }
394 else
395 {
396 std::ostringstream stream;
397 stream << parentName << "." << *mismatchedFieldName;
398 *mismatchedFieldName = stream.str();
399 }
400}
401
402const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
403{
404 switch (linkError)
405 {
406 case LinkMismatchError::TYPE_MISMATCH:
407 return "Type";
408 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
409 return "Array size";
410 case LinkMismatchError::PRECISION_MISMATCH:
411 return "Precision";
412 case LinkMismatchError::STRUCT_NAME_MISMATCH:
413 return "Structure name";
414 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
415 return "Field number";
416 case LinkMismatchError::FIELD_NAME_MISMATCH:
417 return "Field name";
418
419 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
420 return "Interpolation type";
421 case LinkMismatchError::INVARIANCE_MISMATCH:
422 return "Invariance";
423
424 case LinkMismatchError::BINDING_MISMATCH:
425 return "Binding layout qualifier";
426 case LinkMismatchError::LOCATION_MISMATCH:
427 return "Location layout qualifier";
428 case LinkMismatchError::OFFSET_MISMATCH:
429 return "Offset layout qualilfier";
430
431 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
432 return "Layout qualifier";
433 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
434 return "Matrix Packing";
435 default:
436 UNREACHABLE();
437 return "";
438 }
439}
440
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800441LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
442 const sh::InterfaceBlockField &blockField2,
443 bool webglCompatibility,
444 std::string *mismatchedBlockFieldName)
445{
446 if (blockField1.name != blockField2.name)
447 {
448 return LinkMismatchError::FIELD_NAME_MISMATCH;
449 }
450
451 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
452 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
453 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
454 if (linkError != LinkMismatchError::NO_MISMATCH)
455 {
456 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
457 return linkError;
458 }
459
460 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
461 {
462 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
463 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
464 }
465
466 return LinkMismatchError::NO_MISMATCH;
467}
468
469LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
470 const sh::InterfaceBlock &interfaceBlock2,
471 bool webglCompatibility,
472 std::string *mismatchedBlockFieldName)
473{
474 // validate blocks for the same member types
475 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
476 {
477 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
478 }
479 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
480 {
481 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
482 }
483 if (interfaceBlock1.layout != interfaceBlock2.layout ||
484 interfaceBlock1.binding != interfaceBlock2.binding)
485 {
486 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
487 }
488 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
489 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
490 {
491 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
492 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
493
494 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
495 member1, member2, webglCompatibility, mismatchedBlockFieldName);
496 if (linkError != LinkMismatchError::NO_MISMATCH)
497 {
498 return linkError;
499 }
500 }
501 return LinkMismatchError::NO_MISMATCH;
502}
503
Jiawei Shao40786bd2018-04-18 13:58:57 +0800504using ShaderInterfaceBlock = std::pair<ShaderType, const sh::InterfaceBlock *>;
505using InterfaceBlockMap = std::map<std::string, ShaderInterfaceBlock>;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800506
Jiawei Shao40786bd2018-04-18 13:58:57 +0800507void InitializeInterfaceBlockMap(const std::vector<sh::InterfaceBlock> &interfaceBlocks,
508 ShaderType shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800509 InterfaceBlockMap *linkedInterfaceBlocks)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800510{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800511 ASSERT(linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800512
513 for (const sh::InterfaceBlock &interfaceBlock : interfaceBlocks)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800514 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800515 (*linkedInterfaceBlocks)[interfaceBlock.name] = std::make_pair(shaderType, &interfaceBlock);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800516 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800517}
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800518
Jiawei Shao40786bd2018-04-18 13:58:57 +0800519bool ValidateGraphicsInterfaceBlocksPerShader(
520 const std::vector<sh::InterfaceBlock> &interfaceBlocksToLink,
521 ShaderType shaderType,
522 bool webglCompatibility,
523 InterfaceBlockMap *linkedBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800524 InfoLog &infoLog)
525{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800526 ASSERT(linkedBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800527
528 for (const sh::InterfaceBlock &block : interfaceBlocksToLink)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800529 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800530 const auto &entry = linkedBlocks->find(block.name);
531 if (entry != linkedBlocks->end())
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800532 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800533 const sh::InterfaceBlock &linkedBlock = *(entry->second.second);
534 std::string mismatchedStructFieldName;
535 LinkMismatchError linkError = AreMatchingInterfaceBlocks(
536 block, linkedBlock, webglCompatibility, &mismatchedStructFieldName);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800537 if (linkError != LinkMismatchError::NO_MISMATCH)
538 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800539 LogLinkMismatch(infoLog, block.name, GetInterfaceBlockTypeString(block.blockType),
540 linkError, mismatchedStructFieldName, entry->second.first,
541 shaderType);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800542 return false;
543 }
544 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800545 else
546 {
547 (*linkedBlocks)[block.name] = std::make_pair(shaderType, &block);
548 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800549 }
550
551 return true;
552}
553
Jiawei Shao54aafe52018-04-27 14:54:57 +0800554bool ValidateInterfaceBlocksMatch(
555 GLuint numShadersHasInterfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800556 const ShaderMap<const std::vector<sh::InterfaceBlock> *> &shaderInterfaceBlocks,
557 InfoLog &infoLog,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800558 bool webglCompatibility)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800559{
Jiawei Shao54aafe52018-04-27 14:54:57 +0800560 if (numShadersHasInterfaceBlocks < 2u)
561 {
562 return true;
563 }
564
565 ASSERT(!shaderInterfaceBlocks[ShaderType::Compute]);
566
Jiawei Shao40786bd2018-04-18 13:58:57 +0800567 // Check that interface blocks defined in the graphics shaders are identical
568
569 InterfaceBlockMap linkedInterfaceBlocks;
Jiawei Shao40786bd2018-04-18 13:58:57 +0800570
571 bool interfaceBlockMapInitialized = false;
572 for (ShaderType shaderType : kAllGraphicsShaderTypes)
573 {
574 if (!shaderInterfaceBlocks[shaderType])
575 {
576 continue;
577 }
578
579 if (!interfaceBlockMapInitialized)
580 {
581 InitializeInterfaceBlockMap(*shaderInterfaceBlocks[shaderType], shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800582 &linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800583 interfaceBlockMapInitialized = true;
584 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800585 else if (!ValidateGraphicsInterfaceBlocksPerShader(*shaderInterfaceBlocks[shaderType],
586 shaderType, webglCompatibility,
587 &linkedInterfaceBlocks, infoLog))
Jiawei Shao40786bd2018-04-18 13:58:57 +0800588 {
589 return false;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800590 }
591 }
592
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800593 return true;
594}
595
Jamie Madill62d31cb2015-09-11 13:25:51 -0400596} // anonymous namespace
597
jchen107ae70d82018-07-06 13:47:01 +0800598// Saves the linking context for later use in resolveLink().
599struct Program::LinkingState
600{
601 const Context *context;
602 std::unique_ptr<ProgramLinkedResources> resources;
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -0400603 egl::BlobCache::Key programHash;
jchen107ae70d82018-07-06 13:47:01 +0800604 std::unique_ptr<rx::LinkEvent> linkEvent;
605};
606
Jamie Madill4a3c2342015-10-08 12:58:45 -0400607const char *const g_fakepath = "C:\\fakepath";
608
Jamie Madill3c1da042017-11-27 18:33:40 -0500609// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400610InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000611{
612}
613
614InfoLog::~InfoLog()
615{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000616}
617
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400618size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000619{
Jamie Madill23176ce2017-07-31 14:14:33 -0400620 if (!mLazyStream)
621 {
622 return 0;
623 }
624
625 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400626 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000627}
628
Geoff Lange1a27752015-10-05 13:16:04 -0400629void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000630{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400631 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000632
633 if (bufSize > 0)
634 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400635 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400636
Jamie Madill23176ce2017-07-31 14:14:33 -0400637 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000638 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400639 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
640 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000641 }
642
643 infoLog[index] = '\0';
644 }
645
646 if (length)
647 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400648 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000649 }
650}
651
652// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300653// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000654// messages, so lets remove all occurrences of this fake file path from the log.
655void InfoLog::appendSanitized(const char *message)
656{
Jamie Madill23176ce2017-07-31 14:14:33 -0400657 ensureInitialized();
658
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000659 std::string msg(message);
660
661 size_t found;
662 do
663 {
664 found = msg.find(g_fakepath);
665 if (found != std::string::npos)
666 {
667 msg.erase(found, strlen(g_fakepath));
668 }
Jamie Madill493f9572018-05-24 19:52:15 -0400669 } while (found != std::string::npos);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000670
Jamie Madill23176ce2017-07-31 14:14:33 -0400671 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000672}
673
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000674void InfoLog::reset()
675{
Jiawei Shao02f15232017-12-27 10:10:28 +0800676 if (mLazyStream)
677 {
678 mLazyStream.reset(nullptr);
679 }
680}
681
682bool InfoLog::empty() const
683{
684 if (!mLazyStream)
685 {
686 return true;
687 }
688
689 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000690}
691
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800692void LogLinkMismatch(InfoLog &infoLog,
693 const std::string &variableName,
694 const char *variableType,
695 LinkMismatchError linkError,
696 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800697 ShaderType shaderType1,
698 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800699{
700 std::ostringstream stream;
701 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
702 << variableName;
703
704 if (!mismatchedStructOrBlockFieldName.empty())
705 {
706 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
707 }
708
709 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
710 << GetShaderTypeString(shaderType2) << " shaders.";
711
712 infoLog << stream.str();
713}
714
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800715bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
716{
717 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200718 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800719}
720
Jamie Madill3c1da042017-11-27 18:33:40 -0500721// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300722VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000723{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724}
725
Olli Etuahoc8538042017-09-27 11:20:15 +0300726VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300727 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500728{
Olli Etuahoc8538042017-09-27 11:20:15 +0300729 ASSERT(arrayIndex != GL_INVALID_INDEX);
730}
731
Jamie Madill3c1da042017-11-27 18:33:40 -0500732// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800733SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500734 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
735{
736}
737
738SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
739
740SamplerBinding::~SamplerBinding() = default;
741
Jamie Madill3c1da042017-11-27 18:33:40 -0500742// ProgramBindings implementation.
743ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500744{
745}
746
Jamie Madill3c1da042017-11-27 18:33:40 -0500747ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500748{
749}
750
Jamie Madill3c1da042017-11-27 18:33:40 -0500751void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400752{
753 mBindings[name] = index;
754}
755
Jamie Madill3c1da042017-11-27 18:33:40 -0500756int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400757{
758 auto iter = mBindings.find(name);
759 return (iter != mBindings.end()) ? iter->second : -1;
760}
761
Jamie Madill3c1da042017-11-27 18:33:40 -0500762ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400763{
764 return mBindings.begin();
765}
766
Jamie Madill3c1da042017-11-27 18:33:40 -0500767ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400768{
769 return mBindings.end();
770}
771
Jamie Madill3c1da042017-11-27 18:33:40 -0500772// ImageBinding implementation.
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800773ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0), unreferenced(false)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500774{
775}
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800776ImageBinding::ImageBinding(GLuint imageUnit, size_t count, bool unreferenced)
777 : unreferenced(unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500778{
779 for (size_t index = 0; index < count; ++index)
780 {
781 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
782 }
783}
784
785ImageBinding::ImageBinding(const ImageBinding &other) = default;
786
787ImageBinding::~ImageBinding() = default;
788
Jamie Madill3c1da042017-11-27 18:33:40 -0500789// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400790ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500791 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800792 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500793 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400794 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500795 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800796 mImageUniformRange(0, 0),
797 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300798 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800799 mNumViews(-1),
800 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400801 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
802 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800803 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400804 mGeometryShaderMaxVertices(0),
805 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400806{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300807 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400808 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400809}
810
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400812{
Jiawei Shao016105b2018-04-12 16:38:31 +0800813 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400814}
815
Jamie Madill48ef11b2016-04-27 15:21:52 -0400816const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500817{
818 return mLabel;
819}
820
Jiawei Shao385b3e02018-03-21 09:43:28 +0800821Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
822{
Jiawei Shao016105b2018-04-12 16:38:31 +0800823 ASSERT(shaderType != ShaderType::InvalidEnum);
824 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800825}
826
Jamie Madille7d84322017-01-10 18:21:59 -0500827GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400828{
jchen1015015f72017-03-16 13:54:21 +0800829 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830}
831
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800832GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
833{
834 return GetResourceIndexFromName(mBufferVariables, name);
835}
836
Jamie Madille7d84322017-01-10 18:21:59 -0500837GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
838{
839 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
840 return mUniformLocations[location].index;
841}
842
843Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
844{
845 GLuint index = getUniformIndexFromLocation(location);
846 if (!isSamplerUniformIndex(index))
847 {
848 return Optional<GLuint>::Invalid();
849 }
850
851 return getSamplerIndexFromUniformIndex(index);
852}
853
854bool ProgramState::isSamplerUniformIndex(GLuint index) const
855{
Jamie Madill982f6e02017-06-07 14:33:04 -0400856 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500857}
858
859GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
860{
861 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400862 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500863}
864
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800865bool ProgramState::isImageUniformIndex(GLuint index) const
866{
867 return mImageUniformRange.contains(index);
868}
869
870GLuint ProgramState::getImageIndexFromUniformIndex(GLuint uniformIndex) const
871{
872 ASSERT(isImageUniformIndex(uniformIndex));
873 return uniformIndex - mImageUniformRange.low();
874}
875
Jamie Madill34ca4f52017-06-13 11:49:39 -0400876GLuint ProgramState::getAttributeLocation(const std::string &name) const
877{
878 for (const sh::Attribute &attribute : mAttributes)
879 {
880 if (attribute.name == name)
881 {
882 return attribute.location;
883 }
884 }
885
886 return static_cast<GLuint>(-1);
887}
888
Jiawei Shao016105b2018-04-12 16:38:31 +0800889bool ProgramState::hasAttachedShader() const
890{
891 for (const Shader *shader : mAttachedShaders)
892 {
893 if (shader)
894 {
895 return true;
896 }
897 }
898 return false;
899}
900
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500901Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400902 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400903 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800905 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500906 mDeleteStatus(false),
907 mRefCount(0),
908 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500909 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500910{
911 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000912
Geoff Lang7dd2e102014-11-10 15:19:26 -0500913 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
916Program::~Program()
917{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400918 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
Jamie Madill4928b7c2017-06-20 12:57:39 -0400921void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500922{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400923 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800924 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500925 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800926 if (mState.mAttachedShaders[shaderType])
927 {
928 mState.mAttachedShaders[shaderType]->release(context);
929 mState.mAttachedShaders[shaderType] = nullptr;
930 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800931 }
932
Jamie Madill21061022018-07-12 23:56:30 -0400933 // TODO(jmadill): Handle error in the Context. http://anglebug.com/2491
Jamie Madillb7d924a2018-03-10 11:16:54 -0500934 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400935
Jiawei Shao016105b2018-04-12 16:38:31 +0800936 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400937 SafeDelete(mProgram);
938
939 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500940}
jchen107ae70d82018-07-06 13:47:01 +0800941GLuint Program::id() const
942{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400943 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800944 return mHandle;
945}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500946
Geoff Lang70d0f492015-12-10 17:45:46 -0500947void Program::setLabel(const std::string &label)
948{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400949 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500951}
952
953const std::string &Program::getLabel() const
954{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400955 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400956 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500957}
jchen107ae70d82018-07-06 13:47:01 +0800958rx::ProgramImpl *Program::getImplementation() const
959{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400960 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800961 return mProgram;
962}
Geoff Lang70d0f492015-12-10 17:45:46 -0500963
Jamie Madillef300b12016-10-07 15:12:09 -0400964void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400966 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800967 ShaderType shaderType = shader->getType();
968 ASSERT(shaderType != ShaderType::InvalidEnum);
969
970 mState.mAttachedShaders[shaderType] = shader;
971 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
Jamie Madillc1d770e2017-04-13 17:31:24 -0400974void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400976 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800977 ShaderType shaderType = shader->getType();
978 ASSERT(shaderType != ShaderType::InvalidEnum);
979
980 ASSERT(mState.mAttachedShaders[shaderType] == shader);
981 shader->release(context);
982 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000985int Program::getAttachedShadersCount() const
986{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400987 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800988 int numAttachedShaders = 0;
989 for (const Shader *shader : mState.mAttachedShaders)
990 {
991 if (shader)
992 {
993 ++numAttachedShaders;
994 }
995 }
996
997 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000998}
999
Jiawei Shao385b3e02018-03-21 09:43:28 +08001000const Shader *Program::getAttachedShader(ShaderType shaderType) const
1001{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001002 ASSERT(mLinkResolved);
Jiawei Shao385b3e02018-03-21 09:43:28 +08001003 return mState.getAttachedShader(shaderType);
1004}
1005
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006void Program::bindAttributeLocation(GLuint index, const char *name)
1007{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001008 ASSERT(mLinkResolved);
Geoff Langd8605522016-04-13 10:19:12 -04001009 mAttributeBindings.bindLocation(index, name);
1010}
1011
1012void Program::bindUniformLocation(GLuint index, const char *name)
1013{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001014 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001015 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
Sami Väisänen46eaa942016-06-29 10:26:37 +03001018void Program::bindFragmentInputLocation(GLint index, const char *name)
1019{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001020 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001021 mFragmentInputBindings.bindLocation(index, name);
1022}
1023
jchen103fd614d2018-08-13 12:21:58 +08001024BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001025{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001026 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001027 BindingInfo ret;
1028 ret.type = GL_NONE;
1029 ret.valid = false;
1030
Jiawei Shao385b3e02018-03-21 09:43:28 +08001031 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001032 ASSERT(fragmentShader);
1033
1034 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001035 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001036
1037 for (const auto &binding : mFragmentInputBindings)
1038 {
1039 if (binding.second != static_cast<GLuint>(index))
1040 continue;
1041
1042 ret.valid = true;
1043
Olli Etuahod2551232017-10-26 20:03:33 +03001044 size_t nameLengthWithoutArrayIndex;
1045 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001046
1047 for (const auto &in : inputs)
1048 {
Olli Etuahod2551232017-10-26 20:03:33 +03001049 if (in.name.length() == nameLengthWithoutArrayIndex &&
1050 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001051 {
1052 if (in.isArray())
1053 {
1054 // The client wants to bind either "name" or "name[0]".
1055 // GL ES 3.1 spec refers to active array names with language such as:
1056 // "if the string identifies the base name of an active array, where the
1057 // string would exactly match the name of the variable if the suffix "[0]"
1058 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001059 if (arrayIndex == GL_INVALID_INDEX)
1060 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001061
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001062 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001063 }
1064 else
1065 {
1066 ret.name = in.mappedName;
1067 }
1068 ret.type = in.type;
1069 return ret;
1070 }
1071 }
1072 }
1073
1074 return ret;
1075}
1076
jchen103fd614d2018-08-13 12:21:58 +08001077void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001078 GLenum genMode,
1079 GLint components,
1080 const GLfloat *coeffs)
1081{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001082 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001083 // If the location is -1 then the command is silently ignored
1084 if (index == -1)
1085 return;
1086
jchen103fd614d2018-08-13 12:21:58 +08001087 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001088
1089 // If the input doesn't exist then then the command is silently ignored
1090 // This could happen through optimization for example, the shader translator
1091 // decides that a variable is not actually being used and optimizes it away.
1092 if (binding.name.empty())
1093 return;
1094
1095 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1096}
1097
Martin Radev4c4c8e72016-08-04 12:25:34 +03001098// The attached shaders are checked for linking errors by matching up their variables.
1099// Uniform, input and output variables get collected.
1100// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001101Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001102{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001103 const auto &data = context->getContextState();
1104
Jamie Madill6c58b062017-08-01 13:44:25 -04001105 auto *platform = ANGLEPlatformCurrent();
1106 double startTime = platform->currentTime(platform);
1107
Jamie Madill6c1f6712017-02-14 19:08:04 -05001108 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001109 mInfoLog.reset();
1110
1111 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001112 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001113 {
1114 return NoError();
1115 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -04001117 egl::BlobCache::Key programHash = {0};
Jamie Madill6bc264a2018-03-31 15:36:05 -04001118 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001119
Jamie Madill32447362017-06-28 14:53:52 -04001120 if (cache)
1121 {
1122 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001123 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001124 }
1125
1126 if (mLinked)
1127 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001128 double delta = platform->currentTime(platform) - startTime;
1129 int us = static_cast<int>(delta * 1000000.0);
1130 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001131 return NoError();
1132 }
1133
1134 // Cache load failed, fall through to normal linking.
1135 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001136
Jamie Madill6bc264a2018-03-31 15:36:05 -04001137 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001138 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001139
jchen107ae70d82018-07-06 13:47:01 +08001140 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001141 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001142 {
jchen107ae70d82018-07-06 13:47:01 +08001143 resources.reset(
1144 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1145 {&mState.mUniformBlocks, &mState.mUniforms},
1146 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1147 {&mState.mAtomicCounterBuffers},
1148 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001149
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001150 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001151 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1152 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001153 {
1154 return NoError();
1155 }
1156
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001157 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001158 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1159 context->getExtensions().webglCompatibility, mInfoLog,
1160 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001161 {
1162 return NoError();
1163 }
1164
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001165 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1166 // A link error will be generated if the sum of the number of active image uniforms used in
1167 // all shaders, the number of active shader storage blocks, and the number of active
1168 // fragment shader outputs exceeds the implementation-dependent value of
1169 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1170 if (combinedImageUniforms + combinedShaderStorageBlocks >
1171 context->getCaps().maxCombinedShaderOutputResources)
1172 {
1173 mInfoLog
1174 << "The sum of the number of active image uniforms, active shader storage blocks "
1175 "and active fragment shader outputs exceeds "
1176 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1177 << context->getCaps().maxCombinedShaderOutputResources << ")";
1178 return NoError();
1179 }
1180
jchen103fd614d2018-08-13 12:21:58 +08001181 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1182 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001183 }
1184 else
1185 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001186 // Map the varyings to the register file
1187 // In WebGL, we use a slightly different handling for packing variables.
1188 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1189 if (data.getLimitations().noFlexibleVaryingPacking)
1190 {
1191 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1192 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1193 }
1194 else if (data.getExtensions().webglCompatibility)
1195 {
1196 packMode = PackMode::WEBGL_STRICT;
1197 }
1198
jchen107ae70d82018-07-06 13:47:01 +08001199 resources.reset(
1200 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1201 {&mState.mUniformBlocks, &mState.mUniforms},
1202 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1203 {&mState.mAtomicCounterBuffers},
1204 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001205
jchen103fd614d2018-08-13 12:21:58 +08001206 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001207 {
1208 return NoError();
1209 }
1210
jchen103fd614d2018-08-13 12:21:58 +08001211 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001212 {
1213 return NoError();
1214 }
1215
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001216 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001217 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1218 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001219 {
1220 return NoError();
1221 }
1222
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001223 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001224 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1225 context->getExtensions().webglCompatibility, mInfoLog,
1226 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001227 {
1228 return NoError();
1229 }
1230
jchen103fd614d2018-08-13 12:21:58 +08001231 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001232 {
1233 return NoError();
1234 }
1235
jchen103fd614d2018-08-13 12:21:58 +08001236 if (!linkOutputVariables(context->getCaps(), context->getClientVersion(),
1237 combinedImageUniforms, combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001238 {
1239 return NoError();
1240 }
1241
jchen103fd614d2018-08-13 12:21:58 +08001242 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001243
Jiawei Shao016105b2018-04-12 16:38:31 +08001244 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001245 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001246
jchen103fd614d2018-08-13 12:21:58 +08001247 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1248 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001249
jchen103fd614d2018-08-13 12:21:58 +08001250 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1251 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001252 {
1253 return NoError();
1254 }
1255
jchen107ae70d82018-07-06 13:47:01 +08001256 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001257 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001258 {
1259 return NoError();
1260 }
1261
Martin Radev4c4c8e72016-08-04 12:25:34 +03001262 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001263 }
1264
jchen107ae70d82018-07-06 13:47:01 +08001265 mLinkingState.reset(new LinkingState());
1266 mLinkingState->context = context;
1267 mLinkingState->programHash = programHash;
1268 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1269 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001270 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001271
1272 return NoError();
1273}
1274
1275bool Program::isLinking() const
1276{
1277 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1278}
1279
jchen107ae70d82018-07-06 13:47:01 +08001280void Program::resolveLinkImpl()
1281{
1282 ASSERT(mLinkingState.get());
1283
1284 mLinked = mLinkingState->linkEvent->wait();
jchen105055fba2018-08-17 09:57:07 +08001285 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001286 auto linkingState = std::move(mLinkingState);
1287 if (!mLinked)
1288 {
1289 return;
1290 }
1291
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001292 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001293
Yunchao Heece12532017-11-21 15:50:21 +08001294 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1295 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001296 ASSERT(mLinked);
1297 updateLinkedShaderStages();
1298
Jamie Madill54164b02017-08-28 15:17:37 -04001299 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001300 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1301 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001302
Jamie Madill7e4eff12018-08-08 15:49:26 -04001303 // Must be called after markUnusedUniformLocations.
1304 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001305 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001306
1307 setUniformValuesFromBindingQualifiers();
1308
Jamie Madill32447362017-06-28 14:53:52 -04001309 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001310 auto *cache = linkingState->context->getMemoryProgramCache();
1311 if (cache &&
1312 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1313 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001314 {
jchen107ae70d82018-07-06 13:47:01 +08001315 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001316 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001317}
1318
Yunchao He85072e82017-11-14 15:43:28 +08001319void Program::updateLinkedShaderStages()
1320{
Yunchao Heece12532017-11-21 15:50:21 +08001321 mState.mLinkedShaderStages.reset();
1322
Jiawei Shao016105b2018-04-12 16:38:31 +08001323 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001324 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001325 if (shader)
1326 {
1327 mState.mLinkedShaderStages.set(shader->getType());
1328 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001329 }
Yunchao He85072e82017-11-14 15:43:28 +08001330}
1331
James Darpinian30b604d2018-03-12 17:26:57 -07001332void ProgramState::updateTransformFeedbackStrides()
1333{
1334 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1335 {
1336 mTransformFeedbackStrides.resize(1);
1337 size_t totalSize = 0;
1338 for (auto &varying : mLinkedTransformFeedbackVaryings)
1339 {
1340 totalSize += varying.size() * VariableExternalSize(varying.type);
1341 }
1342 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1343 }
1344 else
1345 {
1346 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1347 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1348 {
1349 auto &varying = mLinkedTransformFeedbackVaryings[i];
1350 mTransformFeedbackStrides[i] =
1351 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1352 }
1353 }
1354}
1355
Jamie Madill7e4eff12018-08-08 15:49:26 -04001356void ProgramState::updateActiveSamplers()
1357{
1358 for (SamplerBinding &samplerBinding : mSamplerBindings)
1359 {
1360 if (samplerBinding.unreferenced)
1361 continue;
1362
1363 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1364 {
1365 mActiveSamplerRefCounts[textureUnit]++;
1366 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1367 mActiveSamplersMask.set(textureUnit);
1368 }
1369 }
1370}
1371
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001372void ProgramState::updateActiveImages()
1373{
1374 for (ImageBinding &imageBinding : mImageBindings)
1375 {
1376 if (imageBinding.unreferenced)
1377 continue;
1378
1379 for (GLint imageUnit : imageBinding.boundImageUnits)
1380 {
1381 mActiveImagesMask.set(imageUnit);
1382 }
1383 }
1384}
1385
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001386// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001387void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001388{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001389 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001390 mState.mAttributesTypeMask.reset();
1391 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001392 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001393 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001394 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001395 mState.mUniforms.clear();
1396 mState.mUniformLocations.clear();
1397 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001398 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001399 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001400 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001401 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001402 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001403 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001404 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001405 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001406 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001407 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001408 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001409 mState.mNumViews = -1;
1410 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1411 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001412 mState.mGeometryShaderInvocations = 1;
1413 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415 mValidated = false;
1416
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001417 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001418 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001419}
1420
Jamie Madilla2c74982016-12-12 11:20:42 -05001421Error Program::loadBinary(const Context *context,
1422 GLenum binaryFormat,
1423 const void *binary,
1424 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001425{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001426 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001427 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001428
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001430 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001432 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1433 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001434 {
Jamie Madillf6113162015-05-07 11:49:21 -04001435 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001436 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437 }
1438
Jamie Madill4f86d052017-06-05 12:59:26 -04001439 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1440 ANGLE_TRY_RESULT(
1441 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001442
1443 // Currently we require the full shader text to compute the program hash.
1444 // TODO(jmadill): Store the binary in the internal program cache.
1445
Jamie Madill70aeda42018-08-20 12:17:40 -04001446 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1447 ++uniformBlockIndex)
1448 {
1449 mDirtyBits.set(uniformBlockIndex);
1450 }
1451
Jamie Madillb0a838b2016-11-13 20:02:12 -05001452 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001453#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454}
1455
Jamie Madilla2c74982016-12-12 11:20:42 -05001456Error Program::saveBinary(const Context *context,
1457 GLenum *binaryFormat,
1458 void *binary,
1459 GLsizei bufSize,
1460 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001462 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463 if (binaryFormat)
1464 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001465 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466 }
1467
Jamie Madill4f86d052017-06-05 12:59:26 -04001468 angle::MemoryBuffer memoryBuf;
1469 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470
Jamie Madill4f86d052017-06-05 12:59:26 -04001471 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1472 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001473
1474 if (streamLength > bufSize)
1475 {
1476 if (length)
1477 {
1478 *length = 0;
1479 }
1480
Jamie Madill493f9572018-05-24 19:52:15 -04001481 // TODO: This should be moved to the validation layer but computing the size of the binary
1482 // before saving it causes the save to happen twice. It may be possible to write the binary
1483 // to a separate buffer, validate sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001484 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001485 }
1486
1487 if (binary)
1488 {
Jamie Madill493f9572018-05-24 19:52:15 -04001489 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001490
Jamie Madill48ef11b2016-04-27 15:21:52 -04001491 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 ptr += streamLength;
1493
1494 ASSERT(ptr - streamLength == binary);
1495 }
1496
1497 if (length)
1498 {
1499 *length = streamLength;
1500 }
1501
He Yunchaoacd18982017-01-04 10:46:42 +08001502 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001503}
1504
Jamie Madillffe00c02017-06-27 16:26:55 -04001505GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001506{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001507 ASSERT(mLinkResolved);
Geoff Langb26ab822018-05-29 11:19:00 -04001508 if (!mLinked)
1509 {
1510 return 0;
1511 }
1512
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001514 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001515 if (error.isError())
1516 {
1517 return 0;
1518 }
1519
1520 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001521}
1522
Geoff Langc5629752015-12-07 16:29:04 -05001523void Program::setBinaryRetrievableHint(bool retrievable)
1524{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001525 ASSERT(mLinkResolved);
Geoff Langc5629752015-12-07 16:29:04 -05001526 // TODO(jmadill) : replace with dirty bits
1527 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001528 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001529}
1530
1531bool Program::getBinaryRetrievableHint() const
1532{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001533 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001534 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001535}
1536
Yunchao He61afff12017-03-14 15:34:03 +08001537void Program::setSeparable(bool separable)
1538{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001539 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001540 // TODO(yunchao) : replace with dirty bits
1541 if (mState.mSeparable != separable)
1542 {
1543 mProgram->setSeparable(separable);
1544 mState.mSeparable = separable;
1545 }
1546}
1547
1548bool Program::isSeparable() const
1549{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001550 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001551 return mState.mSeparable;
1552}
1553
Jamie Madill6c1f6712017-02-14 19:08:04 -05001554void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001555{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001556 ASSERT(mLinkResolved);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001557 mRefCount--;
1558
1559 if (mRefCount == 0 && mDeleteStatus)
1560 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001561 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001562 }
1563}
1564
1565void Program::addRef()
1566{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001567 ASSERT(mLinkResolved);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001568 mRefCount++;
1569}
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 Etuahod2551232017-10-26 20:03:33 +03001882 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001883}
1884
Geoff Lange1a27752015-10-05 13:16:04 -04001885void Program::getActiveUniform(GLuint index,
1886 GLsizei bufsize,
1887 GLsizei *length,
1888 GLint *size,
1889 GLenum *type,
1890 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001891{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001892 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001894 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001895 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001896 ASSERT(index < mState.mUniforms.size());
1897 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898
1899 if (bufsize > 0)
1900 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001901 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001902 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001903 }
1904
Olli Etuaho465835d2017-09-26 13:34:10 +03001905 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001907 }
1908 else
1909 {
1910 if (bufsize > 0)
1911 {
1912 name[0] = '\0';
1913 }
1914
1915 if (length)
1916 {
1917 *length = 0;
1918 }
1919
1920 *size = 0;
1921 *type = GL_NONE;
1922 }
1923}
1924
Geoff Lange1a27752015-10-05 13:16:04 -04001925GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001926{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001927 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001929 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001930 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001931 }
1932 else
1933 {
1934 return 0;
1935 }
1936}
1937
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001938size_t Program::getActiveBufferVariableCount() const
1939{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001940 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001941 return mLinked ? mState.mBufferVariables.size() : 0;
1942}
1943
Geoff Lange1a27752015-10-05 13:16:04 -04001944GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001945{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001946 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001947 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948
1949 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001950 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001951 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001952 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001953 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001955 size_t length = uniform.name.length() + 1u;
1956 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 {
1958 length += 3; // Counting in "[0]".
1959 }
1960 maxLength = std::max(length, maxLength);
1961 }
1962 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001963 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964
Jamie Madill62d31cb2015-09-11 13:25:51 -04001965 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966}
1967
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968bool Program::isValidUniformLocation(GLint location) const
1969{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001970 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04001971 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001972 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001973 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001974}
1975
Jamie Madill62d31cb2015-09-11 13:25:51 -04001976const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001978 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001979 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001980 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001981}
1982
Jamie Madillac4e9c32017-01-13 14:07:12 -05001983const VariableLocation &Program::getUniformLocation(GLint location) const
1984{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001985 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05001986 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1987 return mState.mUniformLocations[location];
1988}
1989
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001990const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1991{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001992 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001993 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1994 return mState.mBufferVariables[index];
1995}
1996
Jamie Madill62d31cb2015-09-11 13:25:51 -04001997GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001998{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001999 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002000 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001}
2002
Jamie Madill62d31cb2015-09-11 13:25:51 -04002003GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002005 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002006 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007}
2008
2009void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2010{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002011 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002012 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2013 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002014 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015}
2016
2017void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2018{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002019 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002020 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2021 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002022 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023}
2024
2025void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2026{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002027 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002028 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2029 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002030 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031}
2032
2033void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2034{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002035 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002036 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2037 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002038 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039}
2040
Jamie Madill81c2e252017-09-09 23:32:46 -04002041Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002043 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002044 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2045 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2046
Jamie Madill81c2e252017-09-09 23:32:46 -04002047 mProgram->setUniform1iv(location, clampedCount, v);
2048
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002049 if (mState.isSamplerUniformIndex(locationInfo.index))
2050 {
2051 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002052 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002053 }
2054
Jamie Madill81c2e252017-09-09 23:32:46 -04002055 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056}
2057
2058void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2059{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002060 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002061 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2062 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002063 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064}
2065
2066void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2067{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002068 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002069 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2070 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002071 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002072}
2073
2074void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2075{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002076 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002077 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2078 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002079 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080}
2081
2082void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2083{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002084 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002085 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2086 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002087 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088}
2089
2090void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2091{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002092 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002093 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2094 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002095 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096}
2097
2098void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2099{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002100 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002101 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2102 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002103 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104}
2105
2106void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2107{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002108 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002109 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2110 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002111 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112}
2113
Jamie Madill493f9572018-05-24 19:52:15 -04002114void Program::setUniformMatrix2fv(GLint location,
2115 GLsizei count,
2116 GLboolean transpose,
2117 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002119 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002120 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002121 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122}
2123
Jamie Madill493f9572018-05-24 19:52:15 -04002124void Program::setUniformMatrix3fv(GLint location,
2125 GLsizei count,
2126 GLboolean transpose,
2127 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002128{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002129 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002130 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002131 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132}
2133
Jamie Madill493f9572018-05-24 19:52:15 -04002134void Program::setUniformMatrix4fv(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<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002141 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142}
2143
Jamie Madill493f9572018-05-24 19:52:15 -04002144void Program::setUniformMatrix2x3fv(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<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002151 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152}
2153
Jamie Madill493f9572018-05-24 19:52:15 -04002154void Program::setUniformMatrix2x4fv(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<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002161 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162}
2163
Jamie Madill493f9572018-05-24 19:52:15 -04002164void Program::setUniformMatrix3x2fv(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<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002171 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172}
2173
Jamie Madill493f9572018-05-24 19:52:15 -04002174void Program::setUniformMatrix3x4fv(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<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002181 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182}
2183
Jamie Madill493f9572018-05-24 19:52:15 -04002184void Program::setUniformMatrix4x2fv(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<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002191 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192}
2193
Jamie Madill493f9572018-05-24 19:52:15 -04002194void Program::setUniformMatrix4x3fv(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<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002201 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202}
2203
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002204GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2205{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002206 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002207 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2208 const std::vector<GLuint> &boundTextureUnits =
2209 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2210 return boundTextureUnits[uniformLocation.arrayIndex];
2211}
2212
Jamie Madill54164b02017-08-28 15:17:37 -04002213void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002215 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002216 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2217 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002218
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002219 if (uniform.isSampler())
2220 {
2221 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2222 return;
2223 }
2224
2225 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002226 if (nativeType == GL_FLOAT)
2227 {
2228 mProgram->getUniformfv(context, location, v);
2229 }
2230 else
2231 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002232 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002233 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234}
2235
Jamie Madill54164b02017-08-28 15:17:37 -04002236void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002237{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002238 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002239 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2240 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002241
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002242 if (uniform.isSampler())
2243 {
2244 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2245 return;
2246 }
2247
2248 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002249 if (nativeType == GL_INT || nativeType == GL_BOOL)
2250 {
2251 mProgram->getUniformiv(context, location, v);
2252 }
2253 else
2254 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002255 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002256 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002257}
2258
Jamie Madill54164b02017-08-28 15:17:37 -04002259void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002261 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002262 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2263 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002264
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002265 if (uniform.isSampler())
2266 {
2267 *v = getSamplerUniformBinding(uniformLocation);
2268 return;
2269 }
2270
2271 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002272 if (nativeType == GL_UNSIGNED_INT)
2273 {
2274 mProgram->getUniformuiv(context, location, v);
2275 }
2276 else
2277 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002278 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002279 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280}
2281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282void Program::flagForDeletion()
2283{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002284 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 mDeleteStatus = true;
2286}
2287
2288bool Program::isFlaggedForDeletion() const
2289{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002290 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 return mDeleteStatus;
2292}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002293
Brandon Jones43a53e22014-08-28 16:23:22 -07002294void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002295{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002296 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002297 mInfoLog.reset();
2298
Geoff Lang7dd2e102014-11-10 15:19:26 -05002299 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002300 {
Geoff Lang92019432017-11-20 13:09:34 -05002301 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002302 }
2303 else
2304 {
Jamie Madillf6113162015-05-07 11:49:21 -04002305 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002306 }
2307}
2308
Jamie Madillcc73f242018-08-01 11:34:48 -04002309bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002310{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002311 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002312
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002313 // if any two active samplers in a program are of different types, but refer to the same
2314 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2315 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002316 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002317 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002318 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002319 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002320 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002321 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002322 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2323 "image unit ("
2324 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002325 }
2326
Jamie Madill7e4eff12018-08-08 15:49:26 -04002327 mCachedValidateSamplersResult = false;
2328 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002329 }
2330 }
2331
2332 mCachedValidateSamplersResult = true;
2333 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334}
2335
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002336bool Program::isValidated() const
2337{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002338 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002339 return mValidated;
2340}
2341
jchen1058f67be2017-10-27 08:59:27 +08002342GLuint Program::getActiveAtomicCounterBufferCount() const
2343{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002344 ASSERT(mLinkResolved);
jchen1058f67be2017-10-27 08:59:27 +08002345 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2346}
2347
Jiajia Qin729b2c62017-08-14 09:36:11 +08002348GLuint Program::getActiveShaderStorageBlockCount() const
2349{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002350 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002351 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2352}
2353
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002354void Program::getActiveUniformBlockName(const GLuint blockIndex,
2355 GLsizei bufSize,
2356 GLsizei *length,
2357 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002359 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002360 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2361}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002362
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002363void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2364 GLsizei bufSize,
2365 GLsizei *length,
2366 GLchar *blockName) const
2367{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002368 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002369 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002370}
2371
Qin Jiajia9bf55522018-01-29 13:56:23 +08002372template <typename T>
2373GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002374{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 int maxLength = 0;
2376
2377 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002378 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002379 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002380 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002381 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002382 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002383 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002384 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002385 }
2386 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002387 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002388
2389 return maxLength;
2390}
2391
Qin Jiajia9bf55522018-01-29 13:56:23 +08002392GLint Program::getActiveUniformBlockMaxNameLength() const
2393{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002394 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002395 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2396}
2397
2398GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2399{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002400 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002401 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2402}
2403
Geoff Lange1a27752015-10-05 13:16:04 -04002404GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002405{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002406 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002407 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2408}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002409
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002410GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2411{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002412 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002413 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002414}
2415
Jiajia Qin729b2c62017-08-14 09:36:11 +08002416const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002417{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002418 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002419 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2420 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002421}
2422
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002423const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2424{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002425 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002426 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2427 return mState.mShaderStorageBlocks[index];
2428}
2429
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002430void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2431{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002432 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002433 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002434 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002435 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002436}
2437
2438GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2439{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002440 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002441 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002442}
2443
Jiajia Qin729b2c62017-08-14 09:36:11 +08002444GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2445{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002446 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002447 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2448}
2449
Jamie Madill493f9572018-05-24 19:52:15 -04002450void Program::setTransformFeedbackVaryings(GLsizei count,
2451 const GLchar *const *varyings,
2452 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002453{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002454 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002455 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002456 for (GLsizei i = 0; i < count; i++)
2457 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002458 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002459 }
2460
Jamie Madill48ef11b2016-04-27 15:21:52 -04002461 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002462}
2463
Jamie Madill493f9572018-05-24 19:52:15 -04002464void Program::getTransformFeedbackVarying(GLuint index,
2465 GLsizei bufSize,
2466 GLsizei *length,
2467 GLsizei *size,
2468 GLenum *type,
2469 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002470{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002471 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002473 {
jchen10a9042d32017-03-17 08:50:45 +08002474 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2475 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2476 std::string varName = var.nameWithArrayIndex();
2477 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002478 if (length)
2479 {
2480 *length = lastNameIdx;
2481 }
2482 if (size)
2483 {
jchen10a9042d32017-03-17 08:50:45 +08002484 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002485 }
2486 if (type)
2487 {
jchen10a9042d32017-03-17 08:50:45 +08002488 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002489 }
2490 if (name)
2491 {
jchen10a9042d32017-03-17 08:50:45 +08002492 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002493 name[lastNameIdx] = '\0';
2494 }
2495 }
2496}
2497
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002498GLsizei Program::getTransformFeedbackVaryingCount() const
2499{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002500 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002501 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002502 {
jchen10a9042d32017-03-17 08:50:45 +08002503 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002504 }
2505 else
2506 {
2507 return 0;
2508 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002509}
2510
2511GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2512{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002513 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002514 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002515 {
2516 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002517 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002518 {
jchen10a9042d32017-03-17 08:50:45 +08002519 maxSize =
2520 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002521 }
2522
2523 return maxSize;
2524 }
2525 else
2526 {
2527 return 0;
2528 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002529}
2530
2531GLenum Program::getTransformFeedbackBufferMode() const
2532{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002533 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002534 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002535}
2536
jchen103fd614d2018-08-13 12:21:58 +08002537bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002538{
Jiawei Shao016105b2018-04-12 16:38:31 +08002539 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2540 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2541 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2542 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002543
Jamie Madill493f9572018-05-24 19:52:15 -04002544 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002545 bool isGraphicsShaderAttached =
2546 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002547 // Check whether we both have a compute and non-compute shaders attached.
2548 // If there are of both types attached, then linking should fail.
2549 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2550 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2551 {
2552 infoLog << "Both compute and graphics shaders are attached to the same program.";
2553 return false;
2554 }
2555
2556 if (computeShader)
2557 {
jchen103fd614d2018-08-13 12:21:58 +08002558 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002559 {
2560 infoLog << "Attached compute shader is not compiled.";
2561 return false;
2562 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002563 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002564
jchen103fd614d2018-08-13 12:21:58 +08002565 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002566
2567 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2568 // If the work group size is not specified, a link time error should occur.
2569 if (!mState.mComputeShaderLocalSize.isDeclared())
2570 {
2571 infoLog << "Work group size is not specified.";
2572 return false;
2573 }
2574 }
2575 else
2576 {
jchen103fd614d2018-08-13 12:21:58 +08002577 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002578 {
2579 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2580 return false;
2581 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002582 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002583
jchen103fd614d2018-08-13 12:21:58 +08002584 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002585 {
2586 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2587 return false;
2588 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002589 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002590
jchen103fd614d2018-08-13 12:21:58 +08002591 int vertexShaderVersion = vertexShader->getShaderVersion();
2592 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002593 {
2594 infoLog << "Fragment shader version does not match vertex shader version.";
2595 return false;
2596 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002597
2598 if (geometryShader)
2599 {
2600 // [GL_EXT_geometry_shader] Chapter 7
2601 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2602 // Language Specification, as well as any of the following reasons:
2603 // * One or more of the shader objects attached to <program> are not compiled
2604 // successfully.
2605 // * The shaders do not use the same shader language version.
2606 // * <program> contains objects to form a geometry shader, and
2607 // - <program> is not separable and contains no objects to form a vertex shader; or
2608 // - the input primitive type, output primitive type, or maximum output vertex count
2609 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002610 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002611 {
2612 infoLog << "The attached geometry shader isn't compiled.";
2613 return false;
2614 }
2615
jchen103fd614d2018-08-13 12:21:58 +08002616 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002617 {
2618 mInfoLog << "Geometry shader version does not match vertex shader version.";
2619 return false;
2620 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002621 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002622
Jamie Madill493f9572018-05-24 19:52:15 -04002623 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002624 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002625 if (!inputPrimitive.valid())
2626 {
2627 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2628 return false;
2629 }
2630
Jamie Madill493f9572018-05-24 19:52:15 -04002631 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002632 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002633 if (!outputPrimitive.valid())
2634 {
2635 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2636 return false;
2637 }
2638
jchen103fd614d2018-08-13 12:21:58 +08002639 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002640 if (!maxVertices.valid())
2641 {
2642 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2643 return false;
2644 }
2645
2646 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2647 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2648 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002649 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002650 }
Jiawei Shao73618602017-12-20 15:47:15 +08002651 }
2652
2653 return true;
2654}
2655
jchen10910a3da2017-11-15 09:40:11 +08002656GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2657{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002658 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002659 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2660 {
2661 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2662 if (tf.nameWithArrayIndex() == name)
2663 {
2664 return tfIndex;
2665 }
2666 }
2667 return GL_INVALID_INDEX;
2668}
2669
2670const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2671{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002672 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002673 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2674 return mState.mLinkedTransformFeedbackVaryings[index];
2675}
2676
jchen103fd614d2018-08-13 12:21:58 +08002677bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002678{
Jiawei Shao016105b2018-04-12 16:38:31 +08002679 Shader *previousShader = nullptr;
2680 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002681 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002682 Shader *currentShader = mState.mAttachedShaders[shaderType];
2683 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002684 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002685 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002686 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002687
2688 if (previousShader)
2689 {
jchen103fd614d2018-08-13 12:21:58 +08002690 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002691 {
2692 return false;
2693 }
2694 }
2695 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002696 }
2697
jchen103fd614d2018-08-13 12:21:58 +08002698 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002699 {
2700 return false;
2701 }
2702
jchen103fd614d2018-08-13 12:21:58 +08002703 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002704 {
2705 return false;
2706 }
2707
2708 return true;
2709}
2710
2711// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2712// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002713bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002714 gl::Shader *consumingShader,
2715 gl::InfoLog &infoLog) const
2716{
jchen103fd614d2018-08-13 12:21:58 +08002717 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002718
jchen103fd614d2018-08-13 12:21:58 +08002719 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2720 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002721
Jiawei Shao385b3e02018-03-21 09:43:28 +08002722 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002723
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002724 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002725 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726 bool matched = false;
2727
2728 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002729 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002730 {
2731 continue;
2732 }
2733
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002734 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002735 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002736 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002737 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002738 ASSERT(!output.isBuiltIn());
2739
2740 std::string mismatchedStructFieldName;
2741 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002742 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002743 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002744 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002745 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002746 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2747 mismatchedStructFieldName, generatingShader->getType(),
2748 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002749 return false;
2750 }
2751
Geoff Lang7dd2e102014-11-10 15:19:26 -05002752 matched = true;
2753 break;
2754 }
2755 }
2756
Olli Etuaho107c7242018-03-20 15:45:35 +02002757 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2758 // whether the input is statically used - a statically used input should fail this test even
2759 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002760 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002761 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002762 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2763 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2764 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002765 return false;
2766 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002767 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002768
Jiawei Shaod063aff2018-02-22 10:19:09 +08002769 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002770
Jiawei Shaod063aff2018-02-22 10:19:09 +08002771 return true;
2772}
2773
jchen103fd614d2018-08-13 12:21:58 +08002774bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002775{
Jiawei Shao016105b2018-04-12 16:38:31 +08002776 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002777
2778 std::map<GLuint, std::string> staticFragmentInputLocations;
2779
2780 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002781 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002782 for (const sh::Varying &input : fragmentInputVaryings)
2783 {
2784 if (input.isBuiltIn() || !input.staticUse)
2785 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002786 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002787 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002788
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002789 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002790 if (inputBinding == -1)
2791 continue;
2792
2793 const auto it = staticFragmentInputLocations.find(inputBinding);
2794 if (it == std::end(staticFragmentInputLocations))
2795 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002796 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002797 }
2798 else
2799 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002800 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002801 << it->second;
2802 return false;
2803 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002804 }
2805
2806 return true;
2807}
2808
jchen103fd614d2018-08-13 12:21:58 +08002809bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002810 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002811 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002812 GLuint *combinedImageUniformsCount,
2813 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002814{
Olli Etuahob78707c2017-03-09 15:03:11 +00002815 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002816 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817 {
2818 return false;
2819 }
2820
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002821 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002823 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002824
jchen10eaef1e52017-06-13 10:44:11 +08002825 if (!linkAtomicCounterBuffers())
2826 {
2827 return false;
2828 }
2829
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002830 return true;
2831}
2832
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002833void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002834{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002835 ASSERT(combinedImageUniforms);
2836
Jamie Madill982f6e02017-06-07 14:33:04 -04002837 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2838 unsigned int low = high;
2839
jchen10eaef1e52017-06-13 10:44:11 +08002840 for (auto counterIter = mState.mUniforms.rbegin();
2841 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2842 {
2843 --low;
2844 }
2845
2846 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2847
2848 high = low;
2849
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002850 for (auto imageIter = mState.mUniforms.rbegin();
2851 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2852 {
2853 --low;
2854 }
2855
2856 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002857 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002858 // If uniform is a image type, insert it into the mImageBindings array.
2859 for (unsigned int imageIndex : mState.mImageUniformRange)
2860 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002861 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2862 // cannot load values into a uniform defined as an image. if declare without a
2863 // binding qualifier, any uniform image variable (include all elements of
2864 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002865 auto &imageUniform = mState.mUniforms[imageIndex];
2866 if (imageUniform.binding == -1)
2867 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002868 mState.mImageBindings.emplace_back(
2869 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002870 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002871 else
2872 {
2873 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002874 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002875 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002876
2877 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2878 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002879 }
2880
2881 high = low;
2882
2883 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002884 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002885 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002886 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002887 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002888
2889 mState.mSamplerUniformRange = RangeUI(low, high);
2890
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002891 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002892 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002893 {
2894 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002895 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002896 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2897 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002898 }
2899}
2900
jchen10eaef1e52017-06-13 10:44:11 +08002901bool Program::linkAtomicCounterBuffers()
2902{
2903 for (unsigned int index : mState.mAtomicCounterUniformRange)
2904 {
Jamie Madill493f9572018-05-24 19:52:15 -04002905 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002906 uniform.blockInfo.offset = uniform.offset;
2907 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2908 uniform.blockInfo.matrixStride = 0;
2909 uniform.blockInfo.isRowMajorMatrix = false;
2910
Jamie Madill493f9572018-05-24 19:52:15 -04002911 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002912 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2913 ++bufferIndex)
2914 {
2915 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2916 if (buffer.binding == uniform.binding)
2917 {
2918 buffer.memberIndexes.push_back(index);
2919 uniform.bufferIndex = bufferIndex;
2920 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002921 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002922 break;
2923 }
2924 }
2925 if (!found)
2926 {
2927 AtomicCounterBuffer atomicCounterBuffer;
2928 atomicCounterBuffer.binding = uniform.binding;
2929 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002930 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002931 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2932 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2933 }
2934 }
2935 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002936 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002937
2938 return true;
2939}
2940
Jamie Madilleb979bf2016-11-15 12:28:46 -05002941// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002942bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002943{
Jiawei Shao385b3e02018-03-21 09:43:28 +08002944 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002945
jchen103fd614d2018-08-13 12:21:58 +08002946 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002947
Geoff Lang7dd2e102014-11-10 15:19:26 -05002948 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002949 if (shaderVersion >= 300)
2950 {
2951 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2952 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002953 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002954 }
2955 else
2956 {
2957 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002958 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002959 }
jchen103fd614d2018-08-13 12:21:58 +08002960 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002961
2962 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002963 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002964 {
Jamie Madillf6113162015-05-07 11:49:21 -04002965 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002966 return false;
2967 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002968
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002969 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002970
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002971 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002972 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002973 {
Olli Etuahod2551232017-10-26 20:03:33 +03002974 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2975 // structures, so we don't need to worry about adjusting their names or generating entries
2976 // for each member/element (unlike uniforms for example).
2977 ASSERT(!attribute.isArray() && !attribute.isStruct());
2978
Jamie Madilleb979bf2016-11-15 12:28:46 -05002979 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002980 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002981 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002982 attribute.location = bindingLocation;
2983 }
2984
2985 if (attribute.location != -1)
2986 {
2987 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002988 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002989
Jamie Madill63805b42015-08-25 13:17:39 -04002990 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002991 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002992 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
2993 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002994
2995 return false;
2996 }
2997
Jamie Madill63805b42015-08-25 13:17:39 -04002998 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002999 {
Jamie Madill63805b42015-08-25 13:17:39 -04003000 const int regLocation = attribute.location + reg;
3001 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003002
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003003 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3004 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3005 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003006 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003007 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003008 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003009 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003010 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003011 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003012 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003013 return false;
3014 }
3015 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003016 else
3017 {
Jamie Madill63805b42015-08-25 13:17:39 -04003018 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003019 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003020
Jamie Madill63805b42015-08-25 13:17:39 -04003021 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003022 }
3023 }
3024 }
3025
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003026 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003027 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003028 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003029 // Not set by glBindAttribLocation or by location layout qualifier
3030 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003031 {
Jamie Madill63805b42015-08-25 13:17:39 -04003032 int regs = VariableRegisterCount(attribute.type);
3033 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003034
Jamie Madill63805b42015-08-25 13:17:39 -04003035 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003036 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003037 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003038 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003039 }
3040
Jamie Madillc349ec02015-08-21 16:53:12 -04003041 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003042 }
3043 }
3044
Brandon Jonesc405ae72017-12-06 14:15:03 -08003045 ASSERT(mState.mAttributesTypeMask.none());
3046 ASSERT(mState.mAttributesMask.none());
3047
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003048 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3049 // shader versions we're only processing active attributes to begin with.
3050 if (shaderVersion >= 300)
3051 {
3052 for (auto attributeIter = mState.mAttributes.begin();
3053 attributeIter != mState.mAttributes.end();)
3054 {
3055 if (attributeIter->active)
3056 {
3057 ++attributeIter;
3058 }
3059 else
3060 {
3061 attributeIter = mState.mAttributes.erase(attributeIter);
3062 }
3063 }
3064 }
3065
Jamie Madill48ef11b2016-04-27 15:21:52 -04003066 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003067 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003068 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003069 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003070 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003071
Jamie Madillbd159f02017-10-09 19:39:06 -04003072 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003073 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003074 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3075 mState.mActiveAttribLocationsMask.set(location);
3076 mState.mMaxActiveAttribLocation =
3077 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003078
3079 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3080 if (!attribute.isBuiltIn())
3081 {
3082 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3083 location);
3084 mState.mAttributesMask.set(location);
3085 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003086 }
3087 }
3088
Geoff Lang7dd2e102014-11-10 15:19:26 -05003089 return true;
3090}
3091
jchen103fd614d2018-08-13 12:21:58 +08003092bool Program::linkInterfaceBlocks(const Caps &caps,
3093 const Version &version,
3094 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003095 InfoLog &infoLog,
3096 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003097{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003098 ASSERT(combinedShaderStorageBlocksCount);
3099
Jiawei Shao54aafe52018-04-27 14:54:57 +08003100 GLuint combinedUniformBlocksCount = 0u;
3101 GLuint numShadersHasUniformBlocks = 0u;
3102 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3103 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003104 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003105 Shader *shader = mState.mAttachedShaders[shaderType];
3106 if (!shader)
3107 {
3108 continue;
3109 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003110
jchen103fd614d2018-08-13 12:21:58 +08003111 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003112 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003113 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003114 if (!ValidateInterfaceBlocksCount(
3115 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3116 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3117 {
3118 return false;
3119 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003120
Jiawei Shao54aafe52018-04-27 14:54:57 +08003121 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3122 ++numShadersHasUniformBlocks;
3123 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003124 }
3125
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003126 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3127 {
3128 infoLog << "The sum of the number of active uniform blocks exceeds "
3129 "MAX_COMBINED_UNIFORM_BLOCKS ("
3130 << caps.maxCombinedUniformBlocks << ").";
3131 return false;
3132 }
3133
Jiawei Shao54aafe52018-04-27 14:54:57 +08003134 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3135 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003136 {
3137 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003138 }
Jamie Madille473dee2015-08-18 14:49:01 -04003139
jchen103fd614d2018-08-13 12:21:58 +08003140 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003141 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003142 *combinedShaderStorageBlocksCount = 0u;
3143 GLuint numShadersHasShaderStorageBlocks = 0u;
3144 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3145 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003146 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003147 Shader *shader = mState.mAttachedShaders[shaderType];
3148 if (!shader)
3149 {
3150 continue;
3151 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003152
jchen103fd614d2018-08-13 12:21:58 +08003153 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003154 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003155 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003156 if (!ValidateInterfaceBlocksCount(
3157 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3158 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3159 {
3160 return false;
3161 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003162
Jiawei Shao54aafe52018-04-27 14:54:57 +08003163 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3164 ++numShadersHasShaderStorageBlocks;
3165 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003166 }
3167
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003168 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003169 {
3170 infoLog << "The sum of the number of active shader storage blocks exceeds "
3171 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3172 << caps.maxCombinedShaderStorageBlocks << ").";
3173 return false;
3174 }
3175
Jiawei Shao54aafe52018-04-27 14:54:57 +08003176 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3177 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003178 {
3179 return false;
3180 }
3181 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003182
Geoff Lang7dd2e102014-11-10 15:19:26 -05003183 return true;
3184}
3185
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003186LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3187 const sh::ShaderVariable &variable2,
3188 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003189 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003190 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003191{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003192 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003193 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003194 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003195 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003196 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003197 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003198 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003199 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003200 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003201 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003202 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003203 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003204 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003205 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003206 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003207 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003208
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003209 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003210 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003211 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003212 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003213 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003214 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3215 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003216 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3217 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003218
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003219 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003220 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003221 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003222 }
3223
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003224 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003225 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003226 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003227 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003228 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3229 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003230 }
3231 }
3232
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003233 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003234}
3235
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003236LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3237 const sh::Varying &inputVarying,
3238 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003239 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003240 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003241{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003242 if (validateGeometryShaderInputVarying)
3243 {
3244 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3245 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3246 // or outputs.
3247 ASSERT(inputVarying.arraySizes.size() == 1u);
3248
3249 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3250 // varying cannot match a geometry shader input varying.
3251 // [GL_EXT_geometry_shader] Section 7.4.1:
3252 // Geometry shader per-vertex input variables and blocks are required to be declared as
3253 // arrays, with each element representing input or output values for a single vertex of a
3254 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3255 // are treated as though they were not declared as arrays.
3256 if (outputVarying.isArray())
3257 {
3258 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3259 }
3260 }
3261
3262 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3263 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003264 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003265 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3266 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003267 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003268 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003269 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003270 }
3271
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003272 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003273 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003274 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003275 }
3276
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003277 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003278 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003279 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003280 }
3281
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003282 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003283}
3284
jchen103fd614d2018-08-13 12:21:58 +08003285bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003286{
Jiawei Shao016105b2018-04-12 16:38:31 +08003287 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3288 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003289 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3290 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3291 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003292
3293 if (shaderVersion != 100)
3294 {
3295 // Only ESSL 1.0 has restrictions on matching input and output invariance
3296 return true;
3297 }
3298
3299 bool glPositionIsInvariant = false;
3300 bool glPointSizeIsInvariant = false;
3301 bool glFragCoordIsInvariant = false;
3302 bool glPointCoordIsInvariant = false;
3303
3304 for (const sh::Varying &varying : vertexVaryings)
3305 {
3306 if (!varying.isBuiltIn())
3307 {
3308 continue;
3309 }
3310 if (varying.name.compare("gl_Position") == 0)
3311 {
3312 glPositionIsInvariant = varying.isInvariant;
3313 }
3314 else if (varying.name.compare("gl_PointSize") == 0)
3315 {
3316 glPointSizeIsInvariant = varying.isInvariant;
3317 }
3318 }
3319
3320 for (const sh::Varying &varying : fragmentVaryings)
3321 {
3322 if (!varying.isBuiltIn())
3323 {
3324 continue;
3325 }
3326 if (varying.name.compare("gl_FragCoord") == 0)
3327 {
3328 glFragCoordIsInvariant = varying.isInvariant;
3329 }
3330 else if (varying.name.compare("gl_PointCoord") == 0)
3331 {
3332 glPointCoordIsInvariant = varying.isInvariant;
3333 }
3334 }
3335
3336 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3337 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3338 // Not requiring invariance to match is supported by:
3339 // dEQP, WebGL CTS, Nexus 5X GLES
3340 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3341 {
3342 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3343 "declared invariant.";
3344 return false;
3345 }
3346 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3347 {
3348 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3349 "declared invariant.";
3350 return false;
3351 }
3352
3353 return true;
3354}
3355
jchen103fd614d2018-08-13 12:21:58 +08003356bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003357 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003358 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003359 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003360{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003361
jchen108225e732017-11-14 16:29:03 +08003362 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003363 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003364 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003365 {
jchen103fd614d2018-08-13 12:21:58 +08003366 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003367 {
Geoff Lang1a683462015-09-29 15:09:59 -04003368 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003369 return false;
3370 }
jchen103fd614d2018-08-13 12:21:58 +08003371 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003372 {
3373 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3374 {
3375 infoLog << "Two transform feedback varyings include the same array element ("
3376 << tfVaryingName << ").";
3377 return false;
3378 }
3379 }
3380 else
3381 {
3382 if (uniqueNames.count(tfVaryingName) > 0)
3383 {
3384 infoLog << "Two transform feedback varyings specify the same output variable ("
3385 << tfVaryingName << ").";
3386 return false;
3387 }
3388 }
3389 uniqueNames.insert(tfVaryingName);
3390 }
3391
3392 // Validate against program varyings.
3393 size_t totalComponents = 0;
3394 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3395 {
3396 std::vector<unsigned int> subscripts;
3397 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3398
3399 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3400 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003401 {
3402 infoLog << "Transform feedback varying " << tfVaryingName
3403 << " does not exist in the vertex shader.";
3404 return false;
3405 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003406
jchen108225e732017-11-14 16:29:03 +08003407 // Validate the matching variable.
3408 if (var->isStruct())
3409 {
3410 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3411 return false;
3412 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003413
jchen108225e732017-11-14 16:29:03 +08003414 size_t elementCount = 0;
3415 size_t componentCount = 0;
3416
3417 if (var->isArray())
3418 {
jchen103fd614d2018-08-13 12:21:58 +08003419 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003420 {
3421 infoLog << "Capture of arrays is undefined and not supported.";
3422 return false;
3423 }
3424
3425 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3426 ASSERT(!var->isArrayOfArrays());
3427
3428 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3429 {
3430 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3431 return false;
3432 }
3433 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3434 }
3435 else
3436 {
3437 if (!subscripts.empty())
3438 {
3439 infoLog << "Varying '" << baseName
3440 << "' is not an array to be captured by element.";
3441 return false;
3442 }
3443 elementCount = 1;
3444 }
3445
3446 // TODO(jmadill): Investigate implementation limits on D3D11
3447 componentCount = VariableComponentCount(var->type) * elementCount;
3448 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3449 componentCount > caps.maxTransformFeedbackSeparateComponents)
3450 {
3451 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3452 << componentCount << ") exceed the maximum separate components ("
3453 << caps.maxTransformFeedbackSeparateComponents << ").";
3454 return false;
3455 }
3456
3457 totalComponents += componentCount;
3458 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3459 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3460 {
3461 infoLog << "Transform feedback varying total components (" << totalComponents
3462 << ") exceed the maximum interleaved components ("
3463 << caps.maxTransformFeedbackInterleavedComponents << ").";
3464 return false;
3465 }
3466 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003467 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003468}
3469
jchen103fd614d2018-08-13 12:21:58 +08003470bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003471{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003472 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003473 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003474
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003475 for (const auto &attrib : attributes)
3476 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003477 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003478 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003479 Shader *shader = mState.mAttachedShaders[shaderType];
3480 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003481 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003482 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003483 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003484
jchen103fd614d2018-08-13 12:21:58 +08003485 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003486 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003487 {
3488 if (uniform.name == attrib.name)
3489 {
3490 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3491 return false;
3492 }
3493 }
3494 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003495 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003496
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003497 return true;
3498}
3499
Jamie Madill3c1da042017-11-27 18:33:40 -05003500void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003501{
3502 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003503 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003504 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003505 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003506 std::vector<unsigned int> subscripts;
3507 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003508 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003509 if (!subscripts.empty())
3510 {
3511 subscript = subscripts.back();
3512 }
Jamie Madill192745a2016-12-22 15:58:21 -05003513 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003514 {
Jamie Madill192745a2016-12-22 15:58:21 -05003515 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003516 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003517 {
jchen10a9042d32017-03-17 08:50:45 +08003518 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3519 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003520 break;
3521 }
jchen108225e732017-11-14 16:29:03 +08003522 else if (varying->isStruct())
3523 {
3524 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3525 if (field != nullptr)
3526 {
3527 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3528 break;
3529 }
3530 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003531 }
3532 }
James Darpinian30b604d2018-03-12 17:26:57 -07003533 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003534}
3535
jchen103fd614d2018-08-13 12:21:58 +08003536ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003537{
Jamie Madill3c1da042017-11-27 18:33:40 -05003538 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003539
Jiawei Shao016105b2018-04-12 16:38:31 +08003540 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003541 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003542 {
Jamie Madill192745a2016-12-22 15:58:21 -05003543 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003544 }
3545
Jiawei Shao016105b2018-04-12 16:38:31 +08003546 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003547 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003548 {
Jamie Madill192745a2016-12-22 15:58:21 -05003549 merged[varying.name].fragment = &varying;
3550 }
3551
3552 return merged;
3553}
3554
jchen103fd614d2018-08-13 12:21:58 +08003555bool Program::linkOutputVariables(const Caps &caps,
3556 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003557 GLuint combinedImageUniformsCount,
3558 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003559{
Jiawei Shao016105b2018-04-12 16:38:31 +08003560 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003561 ASSERT(fragmentShader != nullptr);
3562
Geoff Lange0cff192017-05-30 13:04:56 -04003563 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003564 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003565 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003566
jchen103fd614d2018-08-13 12:21:58 +08003567 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003568 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003569 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003570 {
3571 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3572 outputVariable.name != "gl_FragData")
3573 {
3574 continue;
3575 }
3576
3577 unsigned int baseLocation =
3578 (outputVariable.location == -1 ? 0u
3579 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003580
3581 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3582 // structures, so we may use getBasicTypeElementCount().
3583 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3584 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003585 {
3586 const unsigned int location = baseLocation + elementIndex;
3587 if (location >= mState.mOutputVariableTypes.size())
3588 {
3589 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3590 }
Corentin Walleze7557742017-06-01 13:09:57 -04003591 ASSERT(location < mState.mActiveOutputVariables.size());
3592 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003593 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003594 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003595 }
3596 }
3597
jchen103fd614d2018-08-13 12:21:58 +08003598 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003599 {
3600 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3601 // A link error will be generated if the sum of the number of active image uniforms used in
3602 // all shaders, the number of active shader storage blocks, and the number of active
3603 // fragment shader outputs exceeds the implementation-dependent value of
3604 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3605 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3606 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003607 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003608 {
3609 mInfoLog
3610 << "The sum of the number of active image uniforms, active shader storage blocks "
3611 "and active fragment shader outputs exceeds "
3612 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003613 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003614 return false;
3615 }
3616 }
3617
Jamie Madill80a6fc02015-08-21 16:53:16 -04003618 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003619 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003620 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003621
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003622 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003623 // TODO(jmadill): any caps validation here?
3624
jchen1015015f72017-03-16 13:54:21 +08003625 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003626 outputVariableIndex++)
3627 {
jchen1015015f72017-03-16 13:54:21 +08003628 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003629
Olli Etuahod2551232017-10-26 20:03:33 +03003630 if (outputVariable.isArray())
3631 {
3632 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3633 // Resources and including [0] at the end of array variable names.
3634 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3635 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3636 }
3637
Jamie Madill80a6fc02015-08-21 16:53:16 -04003638 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3639 if (outputVariable.isBuiltIn())
3640 continue;
3641
3642 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003643 unsigned int baseLocation =
3644 (outputVariable.location == -1 ? 0u
3645 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003646
Olli Etuaho465835d2017-09-26 13:34:10 +03003647 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3648 // structures, so we may use getBasicTypeElementCount().
3649 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3650 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003651 {
Olli Etuahod2551232017-10-26 20:03:33 +03003652 const unsigned int location = baseLocation + elementIndex;
3653 if (location >= mState.mOutputLocations.size())
3654 {
3655 mState.mOutputLocations.resize(location + 1);
3656 }
3657 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003658 if (outputVariable.isArray())
3659 {
3660 mState.mOutputLocations[location] =
3661 VariableLocation(elementIndex, outputVariableIndex);
3662 }
3663 else
3664 {
3665 VariableLocation locationInfo;
3666 locationInfo.index = outputVariableIndex;
3667 mState.mOutputLocations[location] = locationInfo;
3668 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003669 }
3670 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003671
3672 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003673}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003674
Olli Etuaho48fed632017-03-16 12:05:30 +00003675void Program::setUniformValuesFromBindingQualifiers()
3676{
Jamie Madill982f6e02017-06-07 14:33:04 -04003677 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003678 {
3679 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3680 if (samplerUniform.binding != -1)
3681 {
Olli Etuahod2551232017-10-26 20:03:33 +03003682 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003683 ASSERT(location != -1);
3684 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003685 for (unsigned int elementIndex = 0;
3686 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003687 {
3688 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3689 }
3690 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3691 boundTextureUnits.data());
3692 }
3693 }
3694}
3695
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003696void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003697{
jchen10af713a22017-04-19 09:10:56 +08003698 // Set initial bindings from shader.
3699 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3700 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003701 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003702 bindUniformBlock(blockIndex, uniformBlock.binding);
3703 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003704}
3705
Jamie Madille7d84322017-01-10 18:21:59 -05003706void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003707 GLsizei clampedCount,
3708 const GLint *v)
3709{
Jamie Madill81c2e252017-09-09 23:32:46 -04003710 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3711 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003712 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3713 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003714
Jamie Madill7e4eff12018-08-08 15:49:26 -04003715 if (samplerBinding.unreferenced)
3716 return;
3717
3718 // Update the sampler uniforms.
3719 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3720 {
3721 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3722 GLint newSamplerIndex = v[arrayIndex];
3723
3724 if (oldSamplerIndex == newSamplerIndex)
3725 continue;
3726
3727 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3728
3729 // Update the reference counts.
3730 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3731 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3732 ASSERT(oldRefCount > 0);
3733 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3734 oldRefCount--;
3735 newRefCount++;
3736
3737 // Check for binding type change.
3738 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3739 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3740
3741 if (newRefCount == 1)
3742 {
3743 newSamplerType = samplerBinding.textureType;
3744 mState.mActiveSamplersMask.set(newSamplerIndex);
3745 }
3746 else if (newSamplerType != samplerBinding.textureType)
3747 {
3748 // Conflict detected. Ensure we reset it properly.
3749 newSamplerType = TextureType::InvalidEnum;
3750 }
3751
3752 // Unset previously active sampler.
3753 if (oldRefCount == 0)
3754 {
3755 oldSamplerType = TextureType::InvalidEnum;
3756 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3757 }
3758 else if (oldSamplerType == TextureType::InvalidEnum)
3759 {
3760 // Previous conflict. Check if this new change fixed the conflict.
3761 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3762 }
3763 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003764
3765 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003766 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003767}
3768
Jamie Madill7e4eff12018-08-08 15:49:26 -04003769TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3770{
3771 TextureType foundType = TextureType::InvalidEnum;
3772
3773 for (const SamplerBinding &binding : mSamplerBindings)
3774 {
3775 if (binding.unreferenced)
3776 continue;
3777
3778 // A conflict exists if samplers of different types are sourced by the same texture unit.
3779 // We need to check all bound textures to detect this error case.
3780 for (GLuint textureUnit : binding.boundTextureUnits)
3781 {
3782 if (textureUnit == textureUnitIndex)
3783 {
3784 if (foundType == TextureType::InvalidEnum)
3785 {
3786 foundType = binding.textureType;
3787 }
3788 else if (foundType != binding.textureType)
3789 {
3790 return TextureType::InvalidEnum;
3791 }
3792 }
3793 }
3794 }
3795
3796 return foundType;
3797}
3798
Jamie Madille7d84322017-01-10 18:21:59 -05003799template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003800GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3801 GLsizei count,
3802 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003803 const T *v)
3804{
Jamie Madill134f93d2017-08-31 17:11:00 -04003805 if (count == 1)
3806 return 1;
3807
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003808 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003809
Corentin Wallez15ac5342016-11-03 17:06:39 -04003810 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3811 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003812 unsigned int remainingElements =
3813 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003814 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003815 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003816
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003817 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003818 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003819 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003820 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003821
3822 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003823}
3824
3825template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003826GLsizei Program::clampMatrixUniformCount(GLint location,
3827 GLsizei count,
3828 GLboolean transpose,
3829 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003830{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003831 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3832
Jamie Madill62d31cb2015-09-11 13:25:51 -04003833 if (!transpose)
3834 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003835 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003836 }
3837
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003838 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003839
3840 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3841 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003842 unsigned int remainingElements =
3843 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003844 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003845}
3846
Jamie Madill54164b02017-08-28 15:17:37 -04003847// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3848// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003849template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003850void Program::getUniformInternal(const Context *context,
3851 DestT *dataOut,
3852 GLint location,
3853 GLenum nativeType,
3854 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003855{
Jamie Madill54164b02017-08-28 15:17:37 -04003856 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003857 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003858 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003859 {
3860 GLint tempValue[16] = {0};
3861 mProgram->getUniformiv(context, location, tempValue);
3862 UniformStateQueryCastLoop<GLboolean>(
3863 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003864 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003865 }
3866 case GL_INT:
3867 {
3868 GLint tempValue[16] = {0};
3869 mProgram->getUniformiv(context, location, tempValue);
3870 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3871 components);
3872 break;
3873 }
3874 case GL_UNSIGNED_INT:
3875 {
3876 GLuint tempValue[16] = {0};
3877 mProgram->getUniformuiv(context, location, tempValue);
3878 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3879 components);
3880 break;
3881 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003882 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003883 {
3884 GLfloat tempValue[16] = {0};
3885 mProgram->getUniformfv(context, location, tempValue);
3886 UniformStateQueryCastLoop<GLfloat>(
3887 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003888 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003889 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003890 default:
3891 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003892 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003893 }
3894}
Jamie Madilla4595b82017-01-11 17:36:34 -05003895
3896bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3897{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04003898 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05003899 // Must be called after samplers are validated.
3900 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3901
3902 for (const auto &binding : mState.mSamplerBindings)
3903 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003904 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003905 for (const auto &unit : binding.boundTextureUnits)
3906 {
3907 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3908 if (programTextureID == textureID)
3909 {
3910 // TODO(jmadill): Check for appropriate overlap.
3911 return true;
3912 }
3913 }
3914 }
3915
3916 return false;
3917}
3918
Jamie Madill70aeda42018-08-20 12:17:40 -04003919Error Program::syncState(const Context *context)
3920{
3921 if (mDirtyBits.any())
3922 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04003923 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04003924 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
3925 mDirtyBits.reset();
3926 }
3927
3928 return NoError();
3929}
Jamie Madilla2c74982016-12-12 11:20:42 -05003930} // namespace gl