blob: 7771425b77a147d1ca3c046aac676452417a1925 [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 Madillb980c562018-11-27 11:34:27 -0500610InfoLog::InfoLog() {}
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000611
Jamie Madillb980c562018-11-27 11:34:27 -0500612InfoLog::~InfoLog() {}
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000613
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400614size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000615{
Jamie Madill23176ce2017-07-31 14:14:33 -0400616 if (!mLazyStream)
617 {
618 return 0;
619 }
620
621 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400622 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000623}
624
Geoff Lange1a27752015-10-05 13:16:04 -0400625void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000626{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400627 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000628
629 if (bufSize > 0)
630 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400631 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400632
Jamie Madill23176ce2017-07-31 14:14:33 -0400633 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000634 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400635 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
636 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000637 }
638
639 infoLog[index] = '\0';
640 }
641
642 if (length)
643 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400644 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000645 }
646}
647
648// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300649// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000650// messages, so lets remove all occurrences of this fake file path from the log.
651void InfoLog::appendSanitized(const char *message)
652{
Jamie Madill23176ce2017-07-31 14:14:33 -0400653 ensureInitialized();
654
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000655 std::string msg(message);
656
657 size_t found;
658 do
659 {
660 found = msg.find(g_fakepath);
661 if (found != std::string::npos)
662 {
663 msg.erase(found, strlen(g_fakepath));
664 }
Jamie Madill493f9572018-05-24 19:52:15 -0400665 } while (found != std::string::npos);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000666
Jamie Madill23176ce2017-07-31 14:14:33 -0400667 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000668}
669
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000670void InfoLog::reset()
671{
Jiawei Shao02f15232017-12-27 10:10:28 +0800672 if (mLazyStream)
673 {
674 mLazyStream.reset(nullptr);
675 }
676}
677
678bool InfoLog::empty() const
679{
680 if (!mLazyStream)
681 {
682 return true;
683 }
684
685 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000686}
687
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800688void LogLinkMismatch(InfoLog &infoLog,
689 const std::string &variableName,
690 const char *variableType,
691 LinkMismatchError linkError,
692 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800693 ShaderType shaderType1,
694 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800695{
696 std::ostringstream stream;
697 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
698 << variableName;
699
700 if (!mismatchedStructOrBlockFieldName.empty())
701 {
702 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
703 }
704
705 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
706 << GetShaderTypeString(shaderType2) << " shaders.";
707
708 infoLog << stream.str();
709}
710
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800711bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
712{
713 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200714 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800715}
716
Jamie Madill3c1da042017-11-27 18:33:40 -0500717// VariableLocation implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500718VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false) {}
Geoff Lang7dd2e102014-11-10 15:19:26 -0500719
Olli Etuahoc8538042017-09-27 11:20:15 +0300720VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300721 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500722{
Olli Etuahoc8538042017-09-27 11:20:15 +0300723 ASSERT(arrayIndex != GL_INVALID_INDEX);
724}
725
Jamie Madill3c1da042017-11-27 18:33:40 -0500726// SamplerBindings implementation.
James Darpiniane4109f22018-12-13 16:25:53 -0800727SamplerBinding::SamplerBinding(TextureType textureTypeIn,
728 SamplerFormat formatIn,
729 size_t elementCount,
730 bool unreferenced)
731 : textureType(textureTypeIn),
732 format(formatIn),
733 boundTextureUnits(elementCount, 0),
734 unreferenced(unreferenced)
Jamie Madillb980c562018-11-27 11:34:27 -0500735{}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500736
737SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
738
739SamplerBinding::~SamplerBinding() = default;
740
Jamie Madill3c1da042017-11-27 18:33:40 -0500741// ProgramBindings implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500742ProgramBindings::ProgramBindings() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500743
Jamie Madillb980c562018-11-27 11:34:27 -0500744ProgramBindings::~ProgramBindings() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500745
Jamie Madill3c1da042017-11-27 18:33:40 -0500746void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400747{
748 mBindings[name] = index;
749}
750
Jamie Madill3c1da042017-11-27 18:33:40 -0500751int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400752{
753 auto iter = mBindings.find(name);
754 return (iter != mBindings.end()) ? iter->second : -1;
755}
756
Jamie Madill3c1da042017-11-27 18:33:40 -0500757ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400758{
759 return mBindings.begin();
760}
761
Jamie Madill3c1da042017-11-27 18:33:40 -0500762ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400763{
764 return mBindings.end();
765}
766
Jamie Madill3c1da042017-11-27 18:33:40 -0500767// ImageBinding implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500768ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0), unreferenced(false) {}
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800769ImageBinding::ImageBinding(GLuint imageUnit, size_t count, bool unreferenced)
770 : unreferenced(unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500771{
772 for (size_t index = 0; index < count; ++index)
773 {
774 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
775 }
776}
777
778ImageBinding::ImageBinding(const ImageBinding &other) = default;
779
780ImageBinding::~ImageBinding() = default;
781
Jamie Madill3c1da042017-11-27 18:33:40 -0500782// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400783ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500784 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800785 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500786 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400787 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500788 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800789 mImageUniformRange(0, 0),
790 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300791 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800792 mNumViews(-1),
793 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400794 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
795 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800796 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400797 mGeometryShaderMaxVertices(0),
Austin Eng1bf18ce2018-10-19 15:34:02 -0700798 mDrawIDLocation(-1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400799 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400800{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300801 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400802 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400803}
804
Jamie Madill48ef11b2016-04-27 15:21:52 -0400805ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400806{
Jiawei Shao016105b2018-04-12 16:38:31 +0800807 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400808}
809
Jamie Madill48ef11b2016-04-27 15:21:52 -0400810const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500811{
812 return mLabel;
813}
814
Jiawei Shao385b3e02018-03-21 09:43:28 +0800815Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
816{
Jiawei Shao016105b2018-04-12 16:38:31 +0800817 ASSERT(shaderType != ShaderType::InvalidEnum);
818 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800819}
820
Jamie Madille7d84322017-01-10 18:21:59 -0500821GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400822{
jchen1015015f72017-03-16 13:54:21 +0800823 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400824}
825
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800826GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
827{
828 return GetResourceIndexFromName(mBufferVariables, name);
829}
830
Jamie Madille7d84322017-01-10 18:21:59 -0500831GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
832{
833 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
834 return mUniformLocations[location].index;
835}
836
837Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
838{
839 GLuint index = getUniformIndexFromLocation(location);
840 if (!isSamplerUniformIndex(index))
841 {
842 return Optional<GLuint>::Invalid();
843 }
844
845 return getSamplerIndexFromUniformIndex(index);
846}
847
848bool ProgramState::isSamplerUniformIndex(GLuint index) const
849{
Jamie Madill982f6e02017-06-07 14:33:04 -0400850 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500851}
852
853GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
854{
855 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400856 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500857}
858
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800859bool ProgramState::isImageUniformIndex(GLuint index) const
860{
861 return mImageUniformRange.contains(index);
862}
863
864GLuint ProgramState::getImageIndexFromUniformIndex(GLuint uniformIndex) const
865{
866 ASSERT(isImageUniformIndex(uniformIndex));
867 return uniformIndex - mImageUniformRange.low();
868}
869
Jamie Madill34ca4f52017-06-13 11:49:39 -0400870GLuint ProgramState::getAttributeLocation(const std::string &name) const
871{
872 for (const sh::Attribute &attribute : mAttributes)
873 {
874 if (attribute.name == name)
875 {
876 return attribute.location;
877 }
878 }
879
880 return static_cast<GLuint>(-1);
881}
882
Jiawei Shao016105b2018-04-12 16:38:31 +0800883bool ProgramState::hasAttachedShader() const
884{
885 for (const Shader *shader : mAttachedShaders)
886 {
887 if (shader)
888 {
889 return true;
890 }
891 }
892 return false;
893}
894
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500895Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400896 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400897 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800899 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500900 mDeleteStatus(false),
901 mRefCount(0),
902 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500903 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904{
905 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000906
Geoff Lang7dd2e102014-11-10 15:19:26 -0500907 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908}
909
910Program::~Program()
911{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400912 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
Jamie Madill4928b7c2017-06-20 12:57:39 -0400915void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500916{
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400917 resolveLink(context);
Jiawei Shao016105b2018-04-12 16:38:31 +0800918 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500919 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800920 if (mState.mAttachedShaders[shaderType])
921 {
922 mState.mAttachedShaders[shaderType]->release(context);
923 mState.mAttachedShaders[shaderType] = nullptr;
924 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800925 }
926
Jamie Madillf4a789f2018-10-18 16:56:20 -0400927 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400928
Jiawei Shao016105b2018-04-12 16:38:31 +0800929 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400930 SafeDelete(mProgram);
931
932 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500933}
jchen107ae70d82018-07-06 13:47:01 +0800934GLuint Program::id() const
935{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400936 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800937 return mHandle;
938}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500939
Jamie Madille90d4ee2018-11-28 14:04:00 -0500940void Program::setLabel(const Context *context, const std::string &label)
Geoff Lang70d0f492015-12-10 17:45:46 -0500941{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400942 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500944}
945
946const std::string &Program::getLabel() const
947{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400948 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400949 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500950}
951
Jamie Madillef300b12016-10-07 15:12:09 -0400952void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400954 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800955 ShaderType shaderType = shader->getType();
956 ASSERT(shaderType != ShaderType::InvalidEnum);
957
958 mState.mAttachedShaders[shaderType] = shader;
959 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
Jamie Madillc1d770e2017-04-13 17:31:24 -0400962void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400964 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800965 ShaderType shaderType = shader->getType();
966 ASSERT(shaderType != ShaderType::InvalidEnum);
967
968 ASSERT(mState.mAttachedShaders[shaderType] == shader);
969 shader->release(context);
970 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000973int Program::getAttachedShadersCount() const
974{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400975 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800976 int numAttachedShaders = 0;
977 for (const Shader *shader : mState.mAttachedShaders)
978 {
979 if (shader)
980 {
981 ++numAttachedShaders;
982 }
983 }
984
985 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000986}
987
Jiawei Shao385b3e02018-03-21 09:43:28 +0800988const Shader *Program::getAttachedShader(ShaderType shaderType) const
989{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400990 ASSERT(mLinkResolved);
Jiawei Shao385b3e02018-03-21 09:43:28 +0800991 return mState.getAttachedShader(shaderType);
992}
993
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994void Program::bindAttributeLocation(GLuint index, const char *name)
995{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400996 ASSERT(mLinkResolved);
Geoff Langd8605522016-04-13 10:19:12 -0400997 mAttributeBindings.bindLocation(index, name);
998}
999
1000void Program::bindUniformLocation(GLuint index, const char *name)
1001{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001002 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001003 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
Sami Väisänen46eaa942016-06-29 10:26:37 +03001006void Program::bindFragmentInputLocation(GLint index, const char *name)
1007{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001008 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001009 mFragmentInputBindings.bindLocation(index, name);
1010}
1011
Olli Etuaho0ca09752018-09-24 11:00:50 +03001012void Program::bindFragmentOutputLocation(GLuint index, const char *name)
1013{
1014 mFragmentOutputLocations.bindLocation(index, name);
1015}
1016
1017void Program::bindFragmentOutputIndex(GLuint index, const char *name)
1018{
1019 mFragmentOutputIndexes.bindLocation(index, name);
1020}
1021
jchen103fd614d2018-08-13 12:21:58 +08001022BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001023{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001024 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001025 BindingInfo ret;
1026 ret.type = GL_NONE;
1027 ret.valid = false;
1028
Jiawei Shao385b3e02018-03-21 09:43:28 +08001029 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001030 ASSERT(fragmentShader);
1031
1032 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001033 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001034
1035 for (const auto &binding : mFragmentInputBindings)
1036 {
1037 if (binding.second != static_cast<GLuint>(index))
1038 continue;
1039
1040 ret.valid = true;
1041
Olli Etuahod2551232017-10-26 20:03:33 +03001042 size_t nameLengthWithoutArrayIndex;
1043 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001044
1045 for (const auto &in : inputs)
1046 {
Olli Etuahod2551232017-10-26 20:03:33 +03001047 if (in.name.length() == nameLengthWithoutArrayIndex &&
1048 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001049 {
1050 if (in.isArray())
1051 {
1052 // The client wants to bind either "name" or "name[0]".
1053 // GL ES 3.1 spec refers to active array names with language such as:
1054 // "if the string identifies the base name of an active array, where the
1055 // string would exactly match the name of the variable if the suffix "[0]"
1056 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001057 if (arrayIndex == GL_INVALID_INDEX)
1058 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001059
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001060 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001061 }
1062 else
1063 {
1064 ret.name = in.mappedName;
1065 }
1066 ret.type = in.type;
1067 return ret;
1068 }
1069 }
1070 }
1071
1072 return ret;
1073}
1074
jchen103fd614d2018-08-13 12:21:58 +08001075void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001076 GLenum genMode,
1077 GLint components,
1078 const GLfloat *coeffs)
1079{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001080 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001081 // If the location is -1 then the command is silently ignored
1082 if (index == -1)
1083 return;
1084
jchen103fd614d2018-08-13 12:21:58 +08001085 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001086
1087 // If the input doesn't exist then then the command is silently ignored
1088 // This could happen through optimization for example, the shader translator
1089 // decides that a variable is not actually being used and optimizes it away.
1090 if (binding.name.empty())
1091 return;
1092
1093 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1094}
1095
Martin Radev4c4c8e72016-08-04 12:25:34 +03001096// The attached shaders are checked for linking errors by matching up their variables.
1097// Uniform, input and output variables get collected.
1098// The code gets compiled into binaries.
Jamie Madillf4a789f2018-10-18 16:56:20 -04001099angle::Result Program::link(const Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001100{
Jamie Madillc3dc5d42018-12-30 12:12:04 -05001101 const auto &data = context->getState();
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001102
Jamie Madill6c58b062017-08-01 13:44:25 -04001103 auto *platform = ANGLEPlatformCurrent();
1104 double startTime = platform->currentTime(platform);
1105
Jamie Madill6c1f6712017-02-14 19:08:04 -05001106 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001107 mInfoLog.reset();
1108
1109 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001110 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001111 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001112 return angle::Result::Continue;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001113 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001114
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -04001115 egl::BlobCache::Key programHash = {0};
Jamie Madillb980c562018-11-27 11:34:27 -05001116 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001117
Jamie Madill32447362017-06-28 14:53:52 -04001118 if (cache)
1119 {
Jamie Madill785e8a02018-10-04 17:42:00 -04001120 angle::Result result = cache->getProgram(context, this, &mState, &programHash);
Jamie Madill7c985f52018-11-29 18:16:17 -05001121 mLinked = (result == angle::Result::Continue);
Jamie Madill785e8a02018-10-04 17:42:00 -04001122 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001123 }
1124
1125 if (mLinked)
1126 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001127 double delta = platform->currentTime(platform) - startTime;
1128 int us = static_cast<int>(delta * 1000000.0);
1129 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill7c985f52018-11-29 18:16:17 -05001130 return angle::Result::Continue;
Jamie Madill32447362017-06-28 14:53:52 -04001131 }
1132
1133 // Cache load failed, fall through to normal linking.
1134 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001135
Jamie Madill6bc264a2018-03-31 15:36:05 -04001136 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001137 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001138
jchen107ae70d82018-07-06 13:47:01 +08001139 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001140 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001141 {
jchen107ae70d82018-07-06 13:47:01 +08001142 resources.reset(
1143 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1144 {&mState.mUniformBlocks, &mState.mUniforms},
1145 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1146 {&mState.mAtomicCounterBuffers},
1147 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001148
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001149 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001150 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1151 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001152 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001153 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001154 }
1155
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001156 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001157 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1158 context->getExtensions().webglCompatibility, mInfoLog,
1159 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001160 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001161 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001162 }
1163
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001164 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1165 // A link error will be generated if the sum of the number of active image uniforms used in
1166 // all shaders, the number of active shader storage blocks, and the number of active
1167 // fragment shader outputs exceeds the implementation-dependent value of
1168 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1169 if (combinedImageUniforms + combinedShaderStorageBlocks >
1170 context->getCaps().maxCombinedShaderOutputResources)
1171 {
1172 mInfoLog
1173 << "The sum of the number of active image uniforms, active shader storage blocks "
1174 "and active fragment shader outputs exceeds "
1175 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1176 << context->getCaps().maxCombinedShaderOutputResources << ")";
Jamie Madill7c985f52018-11-29 18:16:17 -05001177 return angle::Result::Continue;
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001178 }
1179
jchen103fd614d2018-08-13 12:21:58 +08001180 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1181 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001182 }
1183 else
1184 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001185 // Map the varyings to the register file
1186 // In WebGL, we use a slightly different handling for packing variables.
1187 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1188 if (data.getLimitations().noFlexibleVaryingPacking)
1189 {
1190 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1191 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1192 }
1193 else if (data.getExtensions().webglCompatibility)
1194 {
1195 packMode = PackMode::WEBGL_STRICT;
1196 }
1197
jchen107ae70d82018-07-06 13:47:01 +08001198 resources.reset(
1199 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1200 {&mState.mUniformBlocks, &mState.mUniforms},
1201 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1202 {&mState.mAtomicCounterBuffers},
1203 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001204
jchen103fd614d2018-08-13 12:21:58 +08001205 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001206 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001207 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001208 }
1209
jchen103fd614d2018-08-13 12:21:58 +08001210 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001211 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001212 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001213 }
1214
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001215 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001216 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1217 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001218 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001219 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001220 }
1221
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001222 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001223 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1224 context->getExtensions().webglCompatibility, mInfoLog,
1225 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001226 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001227 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001228 }
1229
jchen103fd614d2018-08-13 12:21:58 +08001230 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001231 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001232 return angle::Result::Continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001233 }
1234
Olli Etuaho0ca09752018-09-24 11:00:50 +03001235 if (!linkOutputVariables(context->getCaps(), context->getExtensions(),
1236 context->getClientVersion(), combinedImageUniforms,
1237 combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001238 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001239 return angle::Result::Continue;
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001240 }
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 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001253 return angle::Result::Continue;
Jamie Madill192745a2016-12-22 15:58:21 -05001254 }
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 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001259 return angle::Result::Continue;
Olli Etuaho39e78122017-08-29 14:34:22 +03001260 }
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
Jamie Madill7c985f52018-11-29 18:16:17 -05001272 return angle::Result::Continue;
jchen107ae70d82018-07-06 13:47:01 +08001273}
1274
1275bool Program::isLinking() const
1276{
1277 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1278}
1279
Jamie Madill785e8a02018-10-04 17:42:00 -04001280void Program::resolveLinkImpl(const Context *context)
jchen107ae70d82018-07-06 13:47:01 +08001281{
1282 ASSERT(mLinkingState.get());
1283
Jamie Madill785e8a02018-10-04 17:42:00 -04001284 angle::Result result = mLinkingState->linkEvent->wait(context);
1285
Jamie Madill7c985f52018-11-29 18:16:17 -05001286 mLinked = result == angle::Result::Continue;
jchen105055fba2018-08-17 09:57:07 +08001287 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001288 auto linkingState = std::move(mLinkingState);
1289 if (!mLinked)
1290 {
1291 return;
1292 }
1293
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001294 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001295
Yunchao Heece12532017-11-21 15:50:21 +08001296 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1297 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001298 ASSERT(mLinked);
1299 updateLinkedShaderStages();
1300
Jamie Madill54164b02017-08-28 15:17:37 -04001301 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001302 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1303 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001304
Jamie Madill7e4eff12018-08-08 15:49:26 -04001305 // Must be called after markUnusedUniformLocations.
1306 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001307 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001308
1309 setUniformValuesFromBindingQualifiers();
1310
Austin Eng1bf18ce2018-10-19 15:34:02 -07001311 if (context->getExtensions().multiDraw)
1312 {
1313 mState.mDrawIDLocation = getUniformLocation("gl_DrawID");
1314 }
1315
Jamie Madill32447362017-06-28 14:53:52 -04001316 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001317 auto *cache = linkingState->context->getMemoryProgramCache();
1318 if (cache &&
1319 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1320 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001321 {
jchen107ae70d82018-07-06 13:47:01 +08001322 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001323 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001324}
1325
Yunchao He85072e82017-11-14 15:43:28 +08001326void Program::updateLinkedShaderStages()
1327{
Yunchao Heece12532017-11-21 15:50:21 +08001328 mState.mLinkedShaderStages.reset();
1329
Jiawei Shao016105b2018-04-12 16:38:31 +08001330 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001331 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001332 if (shader)
1333 {
1334 mState.mLinkedShaderStages.set(shader->getType());
1335 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001336 }
Yunchao He85072e82017-11-14 15:43:28 +08001337}
1338
James Darpinian30b604d2018-03-12 17:26:57 -07001339void ProgramState::updateTransformFeedbackStrides()
1340{
1341 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1342 {
1343 mTransformFeedbackStrides.resize(1);
1344 size_t totalSize = 0;
1345 for (auto &varying : mLinkedTransformFeedbackVaryings)
1346 {
1347 totalSize += varying.size() * VariableExternalSize(varying.type);
1348 }
1349 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1350 }
1351 else
1352 {
1353 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1354 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1355 {
1356 auto &varying = mLinkedTransformFeedbackVaryings[i];
1357 mTransformFeedbackStrides[i] =
1358 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1359 }
1360 }
1361}
1362
Jamie Madill7e4eff12018-08-08 15:49:26 -04001363void ProgramState::updateActiveSamplers()
1364{
James Darpiniane4109f22018-12-13 16:25:53 -08001365 mActiveSamplerRefCounts.fill(0);
1366
Jamie Madill7e4eff12018-08-08 15:49:26 -04001367 for (SamplerBinding &samplerBinding : mSamplerBindings)
1368 {
1369 if (samplerBinding.unreferenced)
1370 continue;
1371
1372 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1373 {
James Darpiniane4109f22018-12-13 16:25:53 -08001374 if (++mActiveSamplerRefCounts[textureUnit] == 1)
1375 {
1376 mActiveSamplerTypes[textureUnit] = samplerBinding.textureType;
1377 mActiveSamplerFormats[textureUnit] = samplerBinding.format;
1378 }
1379 else
1380 {
1381 if (mActiveSamplerTypes[textureUnit] != samplerBinding.textureType)
1382 {
1383 mActiveSamplerTypes[textureUnit] = TextureType::InvalidEnum;
1384 }
1385 if (mActiveSamplerFormats[textureUnit] != samplerBinding.format)
1386 {
1387 mActiveSamplerFormats[textureUnit] = SamplerFormat::InvalidEnum;
1388 }
1389 }
Jamie Madill7e4eff12018-08-08 15:49:26 -04001390 mActiveSamplersMask.set(textureUnit);
1391 }
1392 }
1393}
1394
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001395void ProgramState::updateActiveImages()
1396{
1397 for (ImageBinding &imageBinding : mImageBindings)
1398 {
1399 if (imageBinding.unreferenced)
1400 continue;
1401
1402 for (GLint imageUnit : imageBinding.boundImageUnits)
1403 {
1404 mActiveImagesMask.set(imageUnit);
1405 }
1406 }
1407}
1408
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001409// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001410void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001412 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001413 mState.mAttributesTypeMask.reset();
1414 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001415 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001416 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001417 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001418 mState.mUniforms.clear();
1419 mState.mUniformLocations.clear();
1420 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001421 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001422 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001423 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001424 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001425 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001426 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001427 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001428 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001429 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001430 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001431 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001432 mState.mNumViews = -1;
1433 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1434 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001435 mState.mGeometryShaderInvocations = 1;
1436 mState.mGeometryShaderMaxVertices = 0;
Austin Eng1bf18ce2018-10-19 15:34:02 -07001437 mState.mDrawIDLocation = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439 mValidated = false;
1440
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001441 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001442 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001443}
1444
Jamie Madillf4a789f2018-10-18 16:56:20 -04001445angle::Result Program::loadBinary(const Context *context,
1446 GLenum binaryFormat,
1447 const void *binary,
1448 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001449{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001450 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001451 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001452
Geoff Lang7dd2e102014-11-10 15:19:26 -05001453#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
Jamie Madill7c985f52018-11-29 18:16:17 -05001454 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001456 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1457 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001458 {
Jamie Madillf6113162015-05-07 11:49:21 -04001459 mInfoLog << "Invalid program binary format.";
Jamie Madill7c985f52018-11-29 18:16:17 -05001460 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461 }
1462
Jamie Madill4f86d052017-06-05 12:59:26 -04001463 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
Jamie Madill785e8a02018-10-04 17:42:00 -04001464 angle::Result result =
1465 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog);
Jamie Madill7c985f52018-11-29 18:16:17 -05001466 mLinked = result == angle::Result::Continue;
Jamie Madill785e8a02018-10-04 17:42:00 -04001467 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001468
1469 // Currently we require the full shader text to compute the program hash.
Jamie Madill785e8a02018-10-04 17:42:00 -04001470 // We could also store the binary in the internal program cache.
Jamie Madill32447362017-06-28 14:53:52 -04001471
Jamie Madill70aeda42018-08-20 12:17:40 -04001472 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1473 ++uniformBlockIndex)
1474 {
1475 mDirtyBits.set(uniformBlockIndex);
1476 }
1477
Jamie Madill7c985f52018-11-29 18:16:17 -05001478 return angle::Result::Continue;
Jamie Madilla2c74982016-12-12 11:20:42 -05001479#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
Jamie Madillf4a789f2018-10-18 16:56:20 -04001482angle::Result Program::saveBinary(Context *context,
1483 GLenum *binaryFormat,
1484 void *binary,
1485 GLsizei bufSize,
1486 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001488 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489 if (binaryFormat)
1490 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001491 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 }
1493
Jamie Madill4f86d052017-06-05 12:59:26 -04001494 angle::MemoryBuffer memoryBuf;
1495 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496
Jamie Madill4f86d052017-06-05 12:59:26 -04001497 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1498 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499
1500 if (streamLength > bufSize)
1501 {
1502 if (length)
1503 {
1504 *length = 0;
1505 }
1506
Jamie Madill493f9572018-05-24 19:52:15 -04001507 // TODO: This should be moved to the validation layer but computing the size of the binary
1508 // before saving it causes the save to happen twice. It may be possible to write the binary
1509 // to a separate buffer, validate sizes and then copy it.
Jamie Madillf4a789f2018-10-18 16:56:20 -04001510 ANGLE_CHECK(context, false, "Insufficient buffer size", GL_INVALID_OPERATION);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511 }
1512
1513 if (binary)
1514 {
Jamie Madill493f9572018-05-24 19:52:15 -04001515 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001516
Jamie Madill48ef11b2016-04-27 15:21:52 -04001517 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518 ptr += streamLength;
1519
1520 ASSERT(ptr - streamLength == binary);
1521 }
1522
1523 if (length)
1524 {
1525 *length = streamLength;
1526 }
1527
Jamie Madill7c985f52018-11-29 18:16:17 -05001528 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001529}
1530
Jamie Madillf4a789f2018-10-18 16:56:20 -04001531GLint Program::getBinaryLength(Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001532{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001533 ASSERT(mLinkResolved);
Geoff Langb26ab822018-05-29 11:19:00 -04001534 if (!mLinked)
1535 {
1536 return 0;
1537 }
1538
Geoff Lang7dd2e102014-11-10 15:19:26 -05001539 GLint length;
Jamie Madillf4a789f2018-10-18 16:56:20 -04001540 angle::Result result =
1541 saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Jamie Madill7c985f52018-11-29 18:16:17 -05001542 if (result != angle::Result::Continue)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543 {
1544 return 0;
1545 }
1546
1547 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001548}
1549
Geoff Langc5629752015-12-07 16:29:04 -05001550void Program::setBinaryRetrievableHint(bool retrievable)
1551{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001552 ASSERT(mLinkResolved);
Geoff Langc5629752015-12-07 16:29:04 -05001553 // TODO(jmadill) : replace with dirty bits
1554 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001555 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001556}
1557
1558bool Program::getBinaryRetrievableHint() const
1559{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001560 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001561 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001562}
1563
Yunchao He61afff12017-03-14 15:34:03 +08001564void Program::setSeparable(bool separable)
1565{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001566 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001567 // TODO(yunchao) : replace with dirty bits
1568 if (mState.mSeparable != separable)
1569 {
1570 mProgram->setSeparable(separable);
1571 mState.mSeparable = separable;
1572 }
1573}
1574
1575bool Program::isSeparable() const
1576{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001577 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001578 return mState.mSeparable;
1579}
1580
Jamie Madill956ab4d2018-10-10 16:13:03 -04001581void Program::deleteSelf(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001582{
Jamie Madill956ab4d2018-10-10 16:13:03 -04001583 ASSERT(mRefCount == 0 && mDeleteStatus);
1584 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001585}
1586
1587unsigned int Program::getRefCount() const
1588{
1589 return mRefCount;
1590}
1591
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001592int Program::getInfoLogLength() const
1593{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001594 ASSERT(mLinkResolved);
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001595 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001596}
1597
Geoff Lange1a27752015-10-05 13:16:04 -04001598void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001599{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001600 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001601 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001602}
1603
Geoff Lange1a27752015-10-05 13:16:04 -04001604void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001605{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001606 ASSERT(mLinkResolved);
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001607 int total = 0;
1608
Jiawei Shao016105b2018-04-12 16:38:31 +08001609 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001610 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001611 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001612 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001613 shaders[total] = shader->getHandle();
1614 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001615 }
1616 }
1617
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001618 if (count)
1619 {
1620 *count = total;
1621 }
1622}
1623
Geoff Lange1a27752015-10-05 13:16:04 -04001624GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001626 ASSERT(mLinkResolved);
Jamie Madill34ca4f52017-06-13 11:49:39 -04001627 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628}
1629
Jamie Madill63805b42015-08-25 13:17:39 -04001630bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001631{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001632 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001633 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1634 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635}
1636
jchen10fd7c3b52017-03-21 15:36:03 +08001637void Program::getActiveAttribute(GLuint index,
1638 GLsizei bufsize,
1639 GLsizei *length,
1640 GLint *size,
1641 GLenum *type,
1642 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001643{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001644 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001645 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001646 {
1647 if (bufsize > 0)
1648 {
1649 name[0] = '\0';
1650 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001652 if (length)
1653 {
1654 *length = 0;
1655 }
1656
1657 *type = GL_NONE;
1658 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001659 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001660 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001661
jchen1036e120e2017-03-14 14:53:58 +08001662 ASSERT(index < mState.mAttributes.size());
1663 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001664
1665 if (bufsize > 0)
1666 {
jchen10fd7c3b52017-03-21 15:36:03 +08001667 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001668 }
1669
1670 // Always a single 'type' instance
1671 *size = 1;
1672 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001673}
1674
Geoff Lange1a27752015-10-05 13:16:04 -04001675GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001676{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001677 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001678 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001679 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001680 return 0;
1681 }
1682
jchen1036e120e2017-03-14 14:53:58 +08001683 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001684}
1685
Geoff Lange1a27752015-10-05 13:16:04 -04001686GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001687{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001688 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001689 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001690 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001691 return 0;
1692 }
1693
1694 size_t maxLength = 0;
1695
Jamie Madill48ef11b2016-04-27 15:21:52 -04001696 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001697 {
jchen1036e120e2017-03-14 14:53:58 +08001698 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700
Jamie Madillc349ec02015-08-21 16:53:12 -04001701 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702}
1703
jchen107ae70d82018-07-06 13:47:01 +08001704const std::vector<sh::Attribute> &Program::getAttributes() const
1705{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001706 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001707 return mState.mAttributes;
1708}
1709
jchen107ae70d82018-07-06 13:47:01 +08001710const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1711{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001712 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001713 return mState.mSamplerBindings;
1714}
1715
jchen107ae70d82018-07-06 13:47:01 +08001716const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1717{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001718 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001719 return mState.mComputeShaderLocalSize;
1720}
1721
1722PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1723{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001724 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001725 return mState.mGeometryShaderInputPrimitiveType;
1726}
1727PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1728{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001729 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001730 return mState.mGeometryShaderOutputPrimitiveType;
1731}
1732GLint Program::getGeometryShaderInvocations() const
1733{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001734 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001735 return mState.mGeometryShaderInvocations;
1736}
1737GLint Program::getGeometryShaderMaxVertices() const
1738{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001739 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001740 return mState.mGeometryShaderMaxVertices;
1741}
1742
jchen1015015f72017-03-16 13:54:21 +08001743GLuint Program::getInputResourceIndex(const GLchar *name) const
1744{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001745 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001746 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001747}
1748
1749GLuint Program::getOutputResourceIndex(const GLchar *name) const
1750{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001751 ASSERT(mLinkResolved);
jchen1015015f72017-03-16 13:54:21 +08001752 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1753}
1754
jchen10fd7c3b52017-03-21 15:36:03 +08001755size_t Program::getOutputResourceCount() const
1756{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001757 ASSERT(mLinkResolved);
jchen10fd7c3b52017-03-21 15:36:03 +08001758 return (mLinked ? mState.mOutputVariables.size() : 0);
1759}
1760
jchen107ae70d82018-07-06 13:47:01 +08001761const std::vector<GLenum> &Program::getOutputVariableTypes() const
1762{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001763 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001764 return mState.mOutputVariableTypes;
1765}
jchen107ae70d82018-07-06 13:47:01 +08001766
jchen10baf5d942017-08-28 20:45:48 +08001767template <typename T>
1768void Program::getResourceName(GLuint index,
1769 const std::vector<T> &resources,
1770 GLsizei bufSize,
1771 GLsizei *length,
1772 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001773{
1774 if (length)
1775 {
1776 *length = 0;
1777 }
1778
1779 if (!mLinked)
1780 {
1781 if (bufSize > 0)
1782 {
1783 name[0] = '\0';
1784 }
1785 return;
1786 }
jchen10baf5d942017-08-28 20:45:48 +08001787 ASSERT(index < resources.size());
1788 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001789
1790 if (bufSize > 0)
1791 {
Olli Etuahod2551232017-10-26 20:03:33 +03001792 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001793 }
1794}
1795
jchen10baf5d942017-08-28 20:45:48 +08001796void Program::getInputResourceName(GLuint index,
1797 GLsizei bufSize,
1798 GLsizei *length,
1799 GLchar *name) const
1800{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001801 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001802 getResourceName(index, mState.mAttributes, bufSize, length, name);
1803}
1804
1805void Program::getOutputResourceName(GLuint index,
1806 GLsizei bufSize,
1807 GLsizei *length,
1808 GLchar *name) const
1809{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001810 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001811 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1812}
1813
1814void Program::getUniformResourceName(GLuint index,
1815 GLsizei bufSize,
1816 GLsizei *length,
1817 GLchar *name) const
1818{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001819 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001820 getResourceName(index, mState.mUniforms, bufSize, length, name);
1821}
1822
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001823void Program::getBufferVariableResourceName(GLuint index,
1824 GLsizei bufSize,
1825 GLsizei *length,
1826 GLchar *name) const
1827{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001828 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001829 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1830}
1831
jchen10880683b2017-04-12 16:21:55 +08001832const sh::Attribute &Program::getInputResource(GLuint index) const
1833{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001834 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001835 ASSERT(index < mState.mAttributes.size());
1836 return mState.mAttributes[index];
1837}
1838
1839const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1840{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001841 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001842 ASSERT(index < mState.mOutputVariables.size());
1843 return mState.mOutputVariables[index];
1844}
1845
jchen107ae70d82018-07-06 13:47:01 +08001846const ProgramBindings &Program::getAttributeBindings() const
1847{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001848 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001849 return mAttributeBindings;
1850}
1851const ProgramBindings &Program::getUniformLocationBindings() const
1852{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001853 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001854 return mUniformLocationBindings;
1855}
1856const ProgramBindings &Program::getFragmentInputBindings() const
1857{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001858 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001859 return mFragmentInputBindings;
1860}
1861
jchen107ae70d82018-07-06 13:47:01 +08001862ComponentTypeMask Program::getDrawBufferTypeMask() const
1863{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001864 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001865 return mState.mDrawBufferTypeMask;
1866}
1867ComponentTypeMask Program::getAttributesTypeMask() const
1868{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001869 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001870 return mState.mAttributesTypeMask;
1871}
1872AttributesMask Program::getAttributesMask() const
1873{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001874 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001875 return mState.mAttributesMask;
1876}
1877
1878const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1879{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001880 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001881 return mState.mTransformFeedbackStrides;
1882}
1883
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884GLint Program::getFragDataLocation(const std::string &name) const
1885{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001886 ASSERT(mLinkResolved);
Olli Etuaho0ca09752018-09-24 11:00:50 +03001887 GLint primaryLocation =
1888 GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
1889 if (primaryLocation != -1)
1890 {
1891 return primaryLocation;
1892 }
1893 return GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name);
1894}
1895
1896GLint Program::getFragDataIndex(const std::string &name) const
1897{
1898 ASSERT(mLinkResolved);
1899 if (GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name) != -1)
1900 {
1901 return 0;
1902 }
1903 if (GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name) != -1)
1904 {
1905 return 1;
1906 }
1907 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001908}
1909
Geoff Lange1a27752015-10-05 13:16:04 -04001910void Program::getActiveUniform(GLuint index,
1911 GLsizei bufsize,
1912 GLsizei *length,
1913 GLint *size,
1914 GLenum *type,
1915 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001916{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001917 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001919 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001920 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001921 ASSERT(index < mState.mUniforms.size());
1922 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923
1924 if (bufsize > 0)
1925 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001927 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928 }
1929
Olli Etuaho465835d2017-09-26 13:34:10 +03001930 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001932 }
1933 else
1934 {
1935 if (bufsize > 0)
1936 {
1937 name[0] = '\0';
1938 }
1939
1940 if (length)
1941 {
1942 *length = 0;
1943 }
1944
1945 *size = 0;
1946 *type = GL_NONE;
1947 }
1948}
1949
Geoff Lange1a27752015-10-05 13:16:04 -04001950GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001951{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001952 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001953 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001954 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001955 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001956 }
1957 else
1958 {
1959 return 0;
1960 }
1961}
1962
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001963size_t Program::getActiveBufferVariableCount() const
1964{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001965 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001966 return mLinked ? mState.mBufferVariables.size() : 0;
1967}
1968
Geoff Lange1a27752015-10-05 13:16:04 -04001969GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001970{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001971 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001972 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001973
1974 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001975 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001976 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001980 size_t length = uniform.name.length() + 1u;
1981 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001982 {
1983 length += 3; // Counting in "[0]".
1984 }
1985 maxLength = std::max(length, maxLength);
1986 }
1987 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001988 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001989
Jamie Madill62d31cb2015-09-11 13:25:51 -04001990 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001991}
1992
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993bool Program::isValidUniformLocation(GLint location) const
1994{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001995 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04001996 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001997 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001998 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001999}
2000
Jamie Madill62d31cb2015-09-11 13:25:51 -04002001const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002003 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002004 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05002005 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006}
2007
Jamie Madillac4e9c32017-01-13 14:07:12 -05002008const VariableLocation &Program::getUniformLocation(GLint location) const
2009{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002010 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05002011 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
2012 return mState.mUniformLocations[location];
2013}
2014
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002015const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
2016{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002017 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002018 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
2019 return mState.mBufferVariables[index];
2020}
2021
Jamie Madill62d31cb2015-09-11 13:25:51 -04002022GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002024 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002025 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026}
2027
Jamie Madill62d31cb2015-09-11 13:25:51 -04002028GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002030 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002031 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032}
2033
2034void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2035{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002036 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002037 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2038 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002039 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040}
2041
2042void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2043{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002044 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002045 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2046 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002047 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048}
2049
2050void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2051{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002052 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002053 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2054 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002055 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056}
2057
2058void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *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, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002063 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064}
2065
Jamie Madille3e680c2018-12-03 17:49:08 -05002066void Program::setUniform1iv(Context *context, GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067{
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, 1, v);
2071
Jamie Madill81c2e252017-09-09 23:32:46 -04002072 mProgram->setUniform1iv(location, clampedCount, v);
2073
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002074 if (mState.isSamplerUniformIndex(locationInfo.index))
2075 {
Jamie Madille3e680c2018-12-03 17:49:08 -05002076 updateSamplerUniform(context, locationInfo, clampedCount, v);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002077 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078}
2079
2080void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2081{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002082 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002083 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2084 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002085 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086}
2087
2088void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2089{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002090 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002091 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2092 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002093 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094}
2095
2096void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2097{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002098 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002099 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2100 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002101 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102}
2103
2104void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2105{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002106 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002107 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2108 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002109 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110}
2111
2112void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2113{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002114 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002115 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2116 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002117 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118}
2119
2120void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2121{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002122 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002123 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2124 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002125 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126}
2127
2128void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2129{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002130 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002131 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2132 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002133 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134}
2135
Jamie Madill493f9572018-05-24 19:52:15 -04002136void Program::setUniformMatrix2fv(GLint location,
2137 GLsizei count,
2138 GLboolean transpose,
2139 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002141 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002142 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002143 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144}
2145
Jamie Madill493f9572018-05-24 19:52:15 -04002146void Program::setUniformMatrix3fv(GLint location,
2147 GLsizei count,
2148 GLboolean transpose,
2149 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002151 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002152 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002153 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154}
2155
Jamie Madill493f9572018-05-24 19:52:15 -04002156void Program::setUniformMatrix4fv(GLint location,
2157 GLsizei count,
2158 GLboolean transpose,
2159 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002160{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002161 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002162 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002163 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164}
2165
Jamie Madill493f9572018-05-24 19:52:15 -04002166void Program::setUniformMatrix2x3fv(GLint location,
2167 GLsizei count,
2168 GLboolean transpose,
2169 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002171 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002172 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002173 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174}
2175
Jamie Madill493f9572018-05-24 19:52:15 -04002176void Program::setUniformMatrix2x4fv(GLint location,
2177 GLsizei count,
2178 GLboolean transpose,
2179 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002181 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002182 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002183 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002184}
2185
Jamie Madill493f9572018-05-24 19:52:15 -04002186void Program::setUniformMatrix3x2fv(GLint location,
2187 GLsizei count,
2188 GLboolean transpose,
2189 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002191 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002192 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002193 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002194}
2195
Jamie Madill493f9572018-05-24 19:52:15 -04002196void Program::setUniformMatrix3x4fv(GLint location,
2197 GLsizei count,
2198 GLboolean transpose,
2199 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002200{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002201 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002202 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002203 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002204}
2205
Jamie Madill493f9572018-05-24 19:52:15 -04002206void Program::setUniformMatrix4x2fv(GLint location,
2207 GLsizei count,
2208 GLboolean transpose,
2209 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002211 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002212 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002213 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214}
2215
Jamie Madill493f9572018-05-24 19:52:15 -04002216void Program::setUniformMatrix4x3fv(GLint location,
2217 GLsizei count,
2218 GLboolean transpose,
2219 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002220{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002221 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002222 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002223 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224}
2225
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002226GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2227{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002228 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002229 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2230 const std::vector<GLuint> &boundTextureUnits =
2231 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2232 return boundTextureUnits[uniformLocation.arrayIndex];
2233}
2234
Jamie Madill54164b02017-08-28 15:17:37 -04002235void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002237 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002238 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2239 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002240
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002241 if (uniform.isSampler())
2242 {
2243 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2244 return;
2245 }
2246
2247 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002248 if (nativeType == GL_FLOAT)
2249 {
2250 mProgram->getUniformfv(context, location, v);
2251 }
2252 else
2253 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002254 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002255 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002256}
2257
Jamie Madill54164b02017-08-28 15:17:37 -04002258void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002260 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002261 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2262 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002263
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002264 if (uniform.isSampler())
2265 {
2266 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2267 return;
2268 }
2269
2270 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002271 if (nativeType == GL_INT || nativeType == GL_BOOL)
2272 {
2273 mProgram->getUniformiv(context, location, v);
2274 }
2275 else
2276 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002277 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002278 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002279}
2280
Jamie Madill54164b02017-08-28 15:17:37 -04002281void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002282{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002283 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002284 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2285 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002286
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002287 if (uniform.isSampler())
2288 {
2289 *v = getSamplerUniformBinding(uniformLocation);
2290 return;
2291 }
2292
2293 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002294 if (nativeType == GL_UNSIGNED_INT)
2295 {
2296 mProgram->getUniformuiv(context, location, v);
2297 }
2298 else
2299 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002300 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002301 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002302}
2303
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304void Program::flagForDeletion()
2305{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002306 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 mDeleteStatus = true;
2308}
2309
2310bool Program::isFlaggedForDeletion() const
2311{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002312 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 return mDeleteStatus;
2314}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002315
Brandon Jones43a53e22014-08-28 16:23:22 -07002316void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002317{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002318 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002319 mInfoLog.reset();
2320
Geoff Lang7dd2e102014-11-10 15:19:26 -05002321 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002322 {
Geoff Lang92019432017-11-20 13:09:34 -05002323 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002324 }
2325 else
2326 {
Jamie Madillf6113162015-05-07 11:49:21 -04002327 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002328 }
2329}
2330
Jamie Madillcc73f242018-08-01 11:34:48 -04002331bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002332{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002333 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002334
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002335 // if any two active samplers in a program are of different types, but refer to the same
2336 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2337 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002338 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002339 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002340 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002341 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002342 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002343 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002344 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2345 "image unit ("
2346 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002347 }
2348
Jamie Madill7e4eff12018-08-08 15:49:26 -04002349 mCachedValidateSamplersResult = false;
2350 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002351 }
2352 }
2353
2354 mCachedValidateSamplersResult = true;
2355 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002356}
2357
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002358bool Program::isValidated() const
2359{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002360 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361 return mValidated;
2362}
2363
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002364void Program::getActiveUniformBlockName(const GLuint blockIndex,
2365 GLsizei bufSize,
2366 GLsizei *length,
2367 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002369 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002370 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2371}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002372
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002373void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2374 GLsizei bufSize,
2375 GLsizei *length,
2376 GLchar *blockName) const
2377{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002378 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002379 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002380}
2381
Qin Jiajia9bf55522018-01-29 13:56:23 +08002382template <typename T>
2383GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002384{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002385 int maxLength = 0;
2386
2387 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002388 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002389 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002391 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002392 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002393 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002394 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002395 }
2396 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002397 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398
2399 return maxLength;
2400}
2401
Qin Jiajia9bf55522018-01-29 13:56:23 +08002402GLint Program::getActiveUniformBlockMaxNameLength() const
2403{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002404 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002405 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2406}
2407
2408GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2409{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002410 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002411 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2412}
2413
Geoff Lange1a27752015-10-05 13:16:04 -04002414GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002416 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002417 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2418}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002419
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002420GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2421{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002422 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002423 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002424}
2425
Jiajia Qin729b2c62017-08-14 09:36:11 +08002426const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002427{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002428 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002429 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2430 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002431}
2432
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002433const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2434{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002435 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002436 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2437 return mState.mShaderStorageBlocks[index];
2438}
2439
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002440void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2441{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002442 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002443 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002444 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002445 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002446}
2447
2448GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2449{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002450 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002451 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002452}
2453
Jiajia Qin729b2c62017-08-14 09:36:11 +08002454GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2455{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002456 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002457 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2458}
2459
Jamie Madill493f9572018-05-24 19:52:15 -04002460void Program::setTransformFeedbackVaryings(GLsizei count,
2461 const GLchar *const *varyings,
2462 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002463{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002464 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002465 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002466 for (GLsizei i = 0; i < count; i++)
2467 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002468 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002469 }
2470
Jamie Madill48ef11b2016-04-27 15:21:52 -04002471 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002472}
2473
Jamie Madill493f9572018-05-24 19:52:15 -04002474void Program::getTransformFeedbackVarying(GLuint index,
2475 GLsizei bufSize,
2476 GLsizei *length,
2477 GLsizei *size,
2478 GLenum *type,
2479 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002480{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002481 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002482 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002483 {
jchen10a9042d32017-03-17 08:50:45 +08002484 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2485 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2486 std::string varName = var.nameWithArrayIndex();
2487 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002488 if (length)
2489 {
2490 *length = lastNameIdx;
2491 }
2492 if (size)
2493 {
jchen10a9042d32017-03-17 08:50:45 +08002494 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002495 }
2496 if (type)
2497 {
jchen10a9042d32017-03-17 08:50:45 +08002498 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002499 }
2500 if (name)
2501 {
jchen10a9042d32017-03-17 08:50:45 +08002502 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002503 name[lastNameIdx] = '\0';
2504 }
2505 }
2506}
2507
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002508GLsizei Program::getTransformFeedbackVaryingCount() const
2509{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002510 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002511 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002512 {
jchen10a9042d32017-03-17 08:50:45 +08002513 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002514 }
2515 else
2516 {
2517 return 0;
2518 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002519}
2520
2521GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2522{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002523 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002524 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002525 {
2526 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002527 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002528 {
jchen10a9042d32017-03-17 08:50:45 +08002529 maxSize =
2530 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002531 }
2532
2533 return maxSize;
2534 }
2535 else
2536 {
2537 return 0;
2538 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002539}
2540
2541GLenum Program::getTransformFeedbackBufferMode() const
2542{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002543 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002544 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002545}
2546
jchen103fd614d2018-08-13 12:21:58 +08002547bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002548{
Jiawei Shao016105b2018-04-12 16:38:31 +08002549 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2550 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2551 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2552 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002553
Jamie Madill493f9572018-05-24 19:52:15 -04002554 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002555 bool isGraphicsShaderAttached =
2556 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002557 // Check whether we both have a compute and non-compute shaders attached.
2558 // If there are of both types attached, then linking should fail.
2559 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2560 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2561 {
2562 infoLog << "Both compute and graphics shaders are attached to the same program.";
2563 return false;
2564 }
2565
2566 if (computeShader)
2567 {
jchen103fd614d2018-08-13 12:21:58 +08002568 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002569 {
2570 infoLog << "Attached compute shader is not compiled.";
2571 return false;
2572 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002573 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002574
jchen103fd614d2018-08-13 12:21:58 +08002575 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002576
2577 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2578 // If the work group size is not specified, a link time error should occur.
2579 if (!mState.mComputeShaderLocalSize.isDeclared())
2580 {
2581 infoLog << "Work group size is not specified.";
2582 return false;
2583 }
2584 }
2585 else
2586 {
jchen103fd614d2018-08-13 12:21:58 +08002587 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002588 {
2589 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2590 return false;
2591 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002592 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002593
jchen103fd614d2018-08-13 12:21:58 +08002594 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002595 {
2596 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2597 return false;
2598 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002599 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002600
jchen103fd614d2018-08-13 12:21:58 +08002601 int vertexShaderVersion = vertexShader->getShaderVersion();
2602 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002603 {
2604 infoLog << "Fragment shader version does not match vertex shader version.";
2605 return false;
2606 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002607
2608 if (geometryShader)
2609 {
2610 // [GL_EXT_geometry_shader] Chapter 7
2611 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2612 // Language Specification, as well as any of the following reasons:
2613 // * One or more of the shader objects attached to <program> are not compiled
2614 // successfully.
2615 // * The shaders do not use the same shader language version.
2616 // * <program> contains objects to form a geometry shader, and
2617 // - <program> is not separable and contains no objects to form a vertex shader; or
2618 // - the input primitive type, output primitive type, or maximum output vertex count
2619 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002620 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002621 {
2622 infoLog << "The attached geometry shader isn't compiled.";
2623 return false;
2624 }
2625
jchen103fd614d2018-08-13 12:21:58 +08002626 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002627 {
2628 mInfoLog << "Geometry shader version does not match vertex shader version.";
2629 return false;
2630 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002631 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002632
Jamie Madill493f9572018-05-24 19:52:15 -04002633 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002634 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002635 if (!inputPrimitive.valid())
2636 {
2637 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2638 return false;
2639 }
2640
Jamie Madill493f9572018-05-24 19:52:15 -04002641 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002642 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002643 if (!outputPrimitive.valid())
2644 {
2645 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2646 return false;
2647 }
2648
jchen103fd614d2018-08-13 12:21:58 +08002649 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002650 if (!maxVertices.valid())
2651 {
2652 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2653 return false;
2654 }
2655
2656 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2657 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2658 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002659 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002660 }
Jiawei Shao73618602017-12-20 15:47:15 +08002661 }
2662
2663 return true;
2664}
2665
jchen10910a3da2017-11-15 09:40:11 +08002666GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2667{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002668 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002669 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2670 {
2671 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2672 if (tf.nameWithArrayIndex() == name)
2673 {
2674 return tfIndex;
2675 }
2676 }
2677 return GL_INVALID_INDEX;
2678}
2679
2680const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2681{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002682 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002683 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2684 return mState.mLinkedTransformFeedbackVaryings[index];
2685}
2686
Austin Eng1bf18ce2018-10-19 15:34:02 -07002687bool Program::hasDrawIDUniform() const
2688{
2689 ASSERT(mLinkResolved);
2690 return mState.mDrawIDLocation >= 0;
2691}
2692
2693void Program::setDrawIDUniform(GLint drawid)
2694{
2695 ASSERT(mLinkResolved);
2696 ASSERT(mState.mDrawIDLocation >= 0);
2697 mProgram->setUniform1iv(mState.mDrawIDLocation, 1, &drawid);
2698}
2699
jchen103fd614d2018-08-13 12:21:58 +08002700bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002701{
Jiawei Shao016105b2018-04-12 16:38:31 +08002702 Shader *previousShader = nullptr;
2703 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002704 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002705 Shader *currentShader = mState.mAttachedShaders[shaderType];
2706 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002707 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002708 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002709 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002710
2711 if (previousShader)
2712 {
jchen103fd614d2018-08-13 12:21:58 +08002713 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002714 {
2715 return false;
2716 }
2717 }
2718 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002719 }
2720
jchen103fd614d2018-08-13 12:21:58 +08002721 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002722 {
2723 return false;
2724 }
2725
jchen103fd614d2018-08-13 12:21:58 +08002726 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002727 {
2728 return false;
2729 }
2730
2731 return true;
2732}
2733
2734// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2735// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002736bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002737 gl::Shader *consumingShader,
2738 gl::InfoLog &infoLog) const
2739{
jchen103fd614d2018-08-13 12:21:58 +08002740 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002741
jchen103fd614d2018-08-13 12:21:58 +08002742 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2743 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002744
Jiawei Shao385b3e02018-03-21 09:43:28 +08002745 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002746
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002747 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002748 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002749 bool matched = false;
2750
2751 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002752 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002753 {
2754 continue;
2755 }
2756
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002757 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002758 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002759 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002760 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002761 ASSERT(!output.isBuiltIn());
2762
2763 std::string mismatchedStructFieldName;
2764 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002765 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002766 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002767 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002768 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002769 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2770 mismatchedStructFieldName, generatingShader->getType(),
2771 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002772 return false;
2773 }
2774
Geoff Lang7dd2e102014-11-10 15:19:26 -05002775 matched = true;
2776 break;
2777 }
2778 }
2779
Olli Etuaho107c7242018-03-20 15:45:35 +02002780 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2781 // whether the input is statically used - a statically used input should fail this test even
2782 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002783 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002784 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002785 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2786 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2787 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002788 return false;
2789 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002790 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002791
Jiawei Shaod063aff2018-02-22 10:19:09 +08002792 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002793
Jiawei Shaod063aff2018-02-22 10:19:09 +08002794 return true;
2795}
2796
jchen103fd614d2018-08-13 12:21:58 +08002797bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002798{
Jiawei Shao016105b2018-04-12 16:38:31 +08002799 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002800
2801 std::map<GLuint, std::string> staticFragmentInputLocations;
2802
2803 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002804 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002805 for (const sh::Varying &input : fragmentInputVaryings)
2806 {
2807 if (input.isBuiltIn() || !input.staticUse)
2808 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002809 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002810 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002811
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002812 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002813 if (inputBinding == -1)
2814 continue;
2815
2816 const auto it = staticFragmentInputLocations.find(inputBinding);
2817 if (it == std::end(staticFragmentInputLocations))
2818 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002819 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002820 }
2821 else
2822 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002823 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002824 << it->second;
2825 return false;
2826 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002827 }
2828
2829 return true;
2830}
2831
jchen103fd614d2018-08-13 12:21:58 +08002832bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002833 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002834 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002835 GLuint *combinedImageUniformsCount,
2836 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002837{
Olli Etuahob78707c2017-03-09 15:03:11 +00002838 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002839 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002840 {
2841 return false;
2842 }
2843
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002844 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002845
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002846 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002847
jchen10eaef1e52017-06-13 10:44:11 +08002848 if (!linkAtomicCounterBuffers())
2849 {
2850 return false;
2851 }
2852
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002853 return true;
2854}
2855
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002856void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002857{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002858 ASSERT(combinedImageUniforms);
2859
Jamie Madill982f6e02017-06-07 14:33:04 -04002860 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2861 unsigned int low = high;
2862
jchen10eaef1e52017-06-13 10:44:11 +08002863 for (auto counterIter = mState.mUniforms.rbegin();
2864 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2865 {
2866 --low;
2867 }
2868
2869 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2870
2871 high = low;
2872
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002873 for (auto imageIter = mState.mUniforms.rbegin();
2874 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2875 {
2876 --low;
2877 }
2878
2879 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002880 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002881 // If uniform is a image type, insert it into the mImageBindings array.
2882 for (unsigned int imageIndex : mState.mImageUniformRange)
2883 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002884 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2885 // cannot load values into a uniform defined as an image. if declare without a
2886 // binding qualifier, any uniform image variable (include all elements of
2887 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002888 auto &imageUniform = mState.mUniforms[imageIndex];
2889 if (imageUniform.binding == -1)
2890 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002891 mState.mImageBindings.emplace_back(
2892 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002893 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002894 else
2895 {
2896 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002897 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002898 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002899
2900 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2901 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002902 }
2903
2904 high = low;
2905
2906 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002907 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002908 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002909 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002910 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002911
2912 mState.mSamplerUniformRange = RangeUI(low, high);
2913
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002914 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002915 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002916 {
2917 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002918 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002919 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
James Darpiniane4109f22018-12-13 16:25:53 -08002920 SamplerFormat format = samplerUniform.typeInfo->samplerFormat;
2921 mState.mSamplerBindings.emplace_back(textureType, format, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002922 }
2923}
2924
jchen10eaef1e52017-06-13 10:44:11 +08002925bool Program::linkAtomicCounterBuffers()
2926{
2927 for (unsigned int index : mState.mAtomicCounterUniformRange)
2928 {
Jamie Madill493f9572018-05-24 19:52:15 -04002929 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002930 uniform.blockInfo.offset = uniform.offset;
2931 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2932 uniform.blockInfo.matrixStride = 0;
2933 uniform.blockInfo.isRowMajorMatrix = false;
2934
Jamie Madill493f9572018-05-24 19:52:15 -04002935 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002936 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2937 ++bufferIndex)
2938 {
2939 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2940 if (buffer.binding == uniform.binding)
2941 {
2942 buffer.memberIndexes.push_back(index);
2943 uniform.bufferIndex = bufferIndex;
2944 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002945 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002946 break;
2947 }
2948 }
2949 if (!found)
2950 {
2951 AtomicCounterBuffer atomicCounterBuffer;
2952 atomicCounterBuffer.binding = uniform.binding;
2953 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002954 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002955 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2956 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2957 }
2958 }
2959 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002960 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002961
2962 return true;
2963}
2964
Jamie Madilleb979bf2016-11-15 12:28:46 -05002965// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002966bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002967{
Jamie Madillb980c562018-11-27 11:34:27 -05002968 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002969
jchen103fd614d2018-08-13 12:21:58 +08002970 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002971
Geoff Lang7dd2e102014-11-10 15:19:26 -05002972 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002973 if (shaderVersion >= 300)
2974 {
2975 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2976 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002977 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002978 }
2979 else
2980 {
2981 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002982 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002983 }
jchen103fd614d2018-08-13 12:21:58 +08002984 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002985
2986 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002987 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002988 {
Jamie Madillf6113162015-05-07 11:49:21 -04002989 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002990 return false;
2991 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002992
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002993 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002994
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002995 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002996 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002997 {
Olli Etuahod2551232017-10-26 20:03:33 +03002998 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2999 // structures, so we don't need to worry about adjusting their names or generating entries
3000 // for each member/element (unlike uniforms for example).
3001 ASSERT(!attribute.isArray() && !attribute.isStruct());
3002
Jamie Madilleb979bf2016-11-15 12:28:46 -05003003 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04003004 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04003005 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003006 attribute.location = bindingLocation;
3007 }
3008
3009 if (attribute.location != -1)
3010 {
3011 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04003012 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003013
Jamie Madill63805b42015-08-25 13:17:39 -04003014 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003015 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003016 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3017 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003018
3019 return false;
3020 }
3021
Jamie Madill63805b42015-08-25 13:17:39 -04003022 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003023 {
Jamie Madill63805b42015-08-25 13:17:39 -04003024 const int regLocation = attribute.location + reg;
3025 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003026
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003027 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3028 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3029 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003030 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003031 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003032 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003033 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003034 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003035 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003036 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003037 return false;
3038 }
3039 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003040 else
3041 {
Jamie Madill63805b42015-08-25 13:17:39 -04003042 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003043 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003044
Jamie Madill63805b42015-08-25 13:17:39 -04003045 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003046 }
3047 }
3048 }
3049
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003050 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003051 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003052 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003053 // Not set by glBindAttribLocation or by location layout qualifier
3054 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003055 {
Jamie Madill63805b42015-08-25 13:17:39 -04003056 int regs = VariableRegisterCount(attribute.type);
3057 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003058
Jamie Madill63805b42015-08-25 13:17:39 -04003059 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003060 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003061 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003062 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003063 }
3064
Jamie Madillc349ec02015-08-21 16:53:12 -04003065 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003066 }
3067 }
3068
Brandon Jonesc405ae72017-12-06 14:15:03 -08003069 ASSERT(mState.mAttributesTypeMask.none());
3070 ASSERT(mState.mAttributesMask.none());
3071
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003072 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3073 // shader versions we're only processing active attributes to begin with.
3074 if (shaderVersion >= 300)
3075 {
3076 for (auto attributeIter = mState.mAttributes.begin();
3077 attributeIter != mState.mAttributes.end();)
3078 {
3079 if (attributeIter->active)
3080 {
3081 ++attributeIter;
3082 }
3083 else
3084 {
3085 attributeIter = mState.mAttributes.erase(attributeIter);
3086 }
3087 }
3088 }
3089
Jamie Madill48ef11b2016-04-27 15:21:52 -04003090 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003091 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003092 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003093 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003094 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003095
Jamie Madillbd159f02017-10-09 19:39:06 -04003096 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003097 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003098 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3099 mState.mActiveAttribLocationsMask.set(location);
3100 mState.mMaxActiveAttribLocation =
3101 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003102
3103 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3104 if (!attribute.isBuiltIn())
3105 {
3106 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3107 location);
3108 mState.mAttributesMask.set(location);
3109 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003110 }
3111 }
3112
Geoff Lang7dd2e102014-11-10 15:19:26 -05003113 return true;
3114}
3115
jchen103fd614d2018-08-13 12:21:58 +08003116bool Program::linkInterfaceBlocks(const Caps &caps,
3117 const Version &version,
3118 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003119 InfoLog &infoLog,
3120 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003121{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003122 ASSERT(combinedShaderStorageBlocksCount);
3123
Jiawei Shao54aafe52018-04-27 14:54:57 +08003124 GLuint combinedUniformBlocksCount = 0u;
3125 GLuint numShadersHasUniformBlocks = 0u;
3126 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3127 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003128 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003129 Shader *shader = mState.mAttachedShaders[shaderType];
3130 if (!shader)
3131 {
3132 continue;
3133 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003134
jchen103fd614d2018-08-13 12:21:58 +08003135 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003136 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003137 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003138 if (!ValidateInterfaceBlocksCount(
3139 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3140 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3141 {
3142 return false;
3143 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003144
Jiawei Shao54aafe52018-04-27 14:54:57 +08003145 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3146 ++numShadersHasUniformBlocks;
3147 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003148 }
3149
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003150 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3151 {
3152 infoLog << "The sum of the number of active uniform blocks exceeds "
3153 "MAX_COMBINED_UNIFORM_BLOCKS ("
3154 << caps.maxCombinedUniformBlocks << ").";
3155 return false;
3156 }
3157
Jiawei Shao54aafe52018-04-27 14:54:57 +08003158 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3159 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003160 {
3161 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003162 }
Jamie Madille473dee2015-08-18 14:49:01 -04003163
jchen103fd614d2018-08-13 12:21:58 +08003164 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003165 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003166 *combinedShaderStorageBlocksCount = 0u;
3167 GLuint numShadersHasShaderStorageBlocks = 0u;
3168 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3169 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003170 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003171 Shader *shader = mState.mAttachedShaders[shaderType];
3172 if (!shader)
3173 {
3174 continue;
3175 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003176
jchen103fd614d2018-08-13 12:21:58 +08003177 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003178 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003179 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003180 if (!ValidateInterfaceBlocksCount(
3181 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3182 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3183 {
3184 return false;
3185 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003186
Jiawei Shao54aafe52018-04-27 14:54:57 +08003187 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3188 ++numShadersHasShaderStorageBlocks;
3189 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003190 }
3191
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003192 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003193 {
3194 infoLog << "The sum of the number of active shader storage blocks exceeds "
3195 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3196 << caps.maxCombinedShaderStorageBlocks << ").";
3197 return false;
3198 }
3199
Jiawei Shao54aafe52018-04-27 14:54:57 +08003200 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3201 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003202 {
3203 return false;
3204 }
3205 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003206
Geoff Lang7dd2e102014-11-10 15:19:26 -05003207 return true;
3208}
3209
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003210LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3211 const sh::ShaderVariable &variable2,
3212 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003213 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003214 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003215{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003216 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003217 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003218 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003219 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003220 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003221 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003222 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003223 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003224 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003225 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003226 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003227 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003228 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003229 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003230 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003231 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003232
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003233 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003234 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003235 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003236 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003237 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003238 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3239 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003240 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3241 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003242
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003243 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003244 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003245 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003246 }
3247
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003248 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003249 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003250 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003251 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003252 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3253 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003254 }
3255 }
3256
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003257 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003258}
3259
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003260LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3261 const sh::Varying &inputVarying,
3262 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003263 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003264 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003265{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003266 if (validateGeometryShaderInputVarying)
3267 {
3268 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3269 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3270 // or outputs.
3271 ASSERT(inputVarying.arraySizes.size() == 1u);
3272
3273 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3274 // varying cannot match a geometry shader input varying.
3275 // [GL_EXT_geometry_shader] Section 7.4.1:
3276 // Geometry shader per-vertex input variables and blocks are required to be declared as
3277 // arrays, with each element representing input or output values for a single vertex of a
3278 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3279 // are treated as though they were not declared as arrays.
3280 if (outputVarying.isArray())
3281 {
3282 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3283 }
3284 }
3285
3286 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3287 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003288 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003289 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3290 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003291 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003292 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003293 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003294 }
3295
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003296 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003297 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003298 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003299 }
3300
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003301 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003302 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003303 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003304 }
3305
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003306 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003307}
3308
jchen103fd614d2018-08-13 12:21:58 +08003309bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003310{
Jiawei Shao016105b2018-04-12 16:38:31 +08003311 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3312 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003313 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3314 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3315 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003316
3317 if (shaderVersion != 100)
3318 {
3319 // Only ESSL 1.0 has restrictions on matching input and output invariance
3320 return true;
3321 }
3322
3323 bool glPositionIsInvariant = false;
3324 bool glPointSizeIsInvariant = false;
3325 bool glFragCoordIsInvariant = false;
3326 bool glPointCoordIsInvariant = false;
3327
3328 for (const sh::Varying &varying : vertexVaryings)
3329 {
3330 if (!varying.isBuiltIn())
3331 {
3332 continue;
3333 }
3334 if (varying.name.compare("gl_Position") == 0)
3335 {
3336 glPositionIsInvariant = varying.isInvariant;
3337 }
3338 else if (varying.name.compare("gl_PointSize") == 0)
3339 {
3340 glPointSizeIsInvariant = varying.isInvariant;
3341 }
3342 }
3343
3344 for (const sh::Varying &varying : fragmentVaryings)
3345 {
3346 if (!varying.isBuiltIn())
3347 {
3348 continue;
3349 }
3350 if (varying.name.compare("gl_FragCoord") == 0)
3351 {
3352 glFragCoordIsInvariant = varying.isInvariant;
3353 }
3354 else if (varying.name.compare("gl_PointCoord") == 0)
3355 {
3356 glPointCoordIsInvariant = varying.isInvariant;
3357 }
3358 }
3359
3360 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3361 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3362 // Not requiring invariance to match is supported by:
3363 // dEQP, WebGL CTS, Nexus 5X GLES
3364 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3365 {
3366 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3367 "declared invariant.";
3368 return false;
3369 }
3370 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3371 {
3372 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3373 "declared invariant.";
3374 return false;
3375 }
3376
3377 return true;
3378}
3379
jchen103fd614d2018-08-13 12:21:58 +08003380bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003381 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003382 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003383 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003384{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003385
jchen108225e732017-11-14 16:29:03 +08003386 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003387 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003388 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003389 {
jchen103fd614d2018-08-13 12:21:58 +08003390 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003391 {
Geoff Lang1a683462015-09-29 15:09:59 -04003392 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003393 return false;
3394 }
jchen103fd614d2018-08-13 12:21:58 +08003395 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003396 {
3397 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3398 {
3399 infoLog << "Two transform feedback varyings include the same array element ("
3400 << tfVaryingName << ").";
3401 return false;
3402 }
3403 }
3404 else
3405 {
3406 if (uniqueNames.count(tfVaryingName) > 0)
3407 {
3408 infoLog << "Two transform feedback varyings specify the same output variable ("
3409 << tfVaryingName << ").";
3410 return false;
3411 }
3412 }
3413 uniqueNames.insert(tfVaryingName);
3414 }
3415
3416 // Validate against program varyings.
3417 size_t totalComponents = 0;
3418 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3419 {
3420 std::vector<unsigned int> subscripts;
3421 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3422
3423 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3424 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003425 {
3426 infoLog << "Transform feedback varying " << tfVaryingName
3427 << " does not exist in the vertex shader.";
3428 return false;
3429 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003430
jchen108225e732017-11-14 16:29:03 +08003431 // Validate the matching variable.
3432 if (var->isStruct())
3433 {
3434 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3435 return false;
3436 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003437
jchen108225e732017-11-14 16:29:03 +08003438 size_t elementCount = 0;
3439 size_t componentCount = 0;
3440
3441 if (var->isArray())
3442 {
jchen103fd614d2018-08-13 12:21:58 +08003443 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003444 {
3445 infoLog << "Capture of arrays is undefined and not supported.";
3446 return false;
3447 }
3448
3449 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3450 ASSERT(!var->isArrayOfArrays());
3451
3452 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3453 {
3454 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3455 return false;
3456 }
3457 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3458 }
3459 else
3460 {
3461 if (!subscripts.empty())
3462 {
3463 infoLog << "Varying '" << baseName
3464 << "' is not an array to be captured by element.";
3465 return false;
3466 }
3467 elementCount = 1;
3468 }
3469
3470 // TODO(jmadill): Investigate implementation limits on D3D11
3471 componentCount = VariableComponentCount(var->type) * elementCount;
3472 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3473 componentCount > caps.maxTransformFeedbackSeparateComponents)
3474 {
3475 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3476 << componentCount << ") exceed the maximum separate components ("
3477 << caps.maxTransformFeedbackSeparateComponents << ").";
3478 return false;
3479 }
3480
3481 totalComponents += componentCount;
3482 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3483 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3484 {
3485 infoLog << "Transform feedback varying total components (" << totalComponents
3486 << ") exceed the maximum interleaved components ("
3487 << caps.maxTransformFeedbackInterleavedComponents << ").";
3488 return false;
3489 }
3490 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003491 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003492}
3493
jchen103fd614d2018-08-13 12:21:58 +08003494bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003495{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003496 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003497 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003498
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003499 for (const auto &attrib : attributes)
3500 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003501 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003502 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003503 Shader *shader = mState.mAttachedShaders[shaderType];
3504 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003505 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003506 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003507 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003508
jchen103fd614d2018-08-13 12:21:58 +08003509 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003510 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003511 {
3512 if (uniform.name == attrib.name)
3513 {
3514 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3515 return false;
3516 }
3517 }
3518 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003519 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003520
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003521 return true;
3522}
3523
Jamie Madill3c1da042017-11-27 18:33:40 -05003524void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003525{
3526 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003527 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003528 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003529 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003530 std::vector<unsigned int> subscripts;
3531 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003532 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003533 if (!subscripts.empty())
3534 {
3535 subscript = subscripts.back();
3536 }
Jamie Madill192745a2016-12-22 15:58:21 -05003537 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003538 {
Jamie Madill192745a2016-12-22 15:58:21 -05003539 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003540 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003541 {
jchen10a9042d32017-03-17 08:50:45 +08003542 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3543 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003544 break;
3545 }
jchen108225e732017-11-14 16:29:03 +08003546 else if (varying->isStruct())
3547 {
3548 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3549 if (field != nullptr)
3550 {
3551 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3552 break;
3553 }
3554 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003555 }
3556 }
James Darpinian30b604d2018-03-12 17:26:57 -07003557 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003558}
3559
jchen103fd614d2018-08-13 12:21:58 +08003560ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003561{
Jamie Madill3c1da042017-11-27 18:33:40 -05003562 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003563
Jiawei Shao016105b2018-04-12 16:38:31 +08003564 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003565 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003566 {
Jamie Madill192745a2016-12-22 15:58:21 -05003567 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003568 }
3569
Jiawei Shao016105b2018-04-12 16:38:31 +08003570 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003571 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003572 {
Jamie Madill192745a2016-12-22 15:58:21 -05003573 merged[varying.name].fragment = &varying;
3574 }
3575
3576 return merged;
3577}
3578
Olli Etuaho0ca09752018-09-24 11:00:50 +03003579bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3580{
3581 return a.getArraySizeProduct() > b.getArraySizeProduct();
3582}
3583
3584int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3585{
3586 if (outputVariable.location != -1)
3587 {
3588 return outputVariable.location;
3589 }
3590 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3591 if (apiLocation != -1)
3592 {
3593 return apiLocation;
3594 }
3595 return -1;
3596}
3597
3598bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3599{
3600 if (outputVariable.index != -1)
3601 {
3602 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3603 return (outputVariable.index == 1);
3604 }
3605 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3606 if (apiIndex != -1)
3607 {
3608 // Index layout qualifier from the shader takes precedence, so the index from the API is
3609 // checked only if the index was not set in the shader. This is not specified in the EXT
3610 // spec, but is specified in desktop OpenGL specs.
3611 return (apiIndex == 1);
3612 }
3613 // EXT_blend_func_extended: Outputs get index 0 by default.
3614 return false;
3615}
3616
jchen103fd614d2018-08-13 12:21:58 +08003617bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003618 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003619 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003620 GLuint combinedImageUniformsCount,
3621 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003622{
Jiawei Shao016105b2018-04-12 16:38:31 +08003623 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003624 ASSERT(fragmentShader != nullptr);
3625
Geoff Lange0cff192017-05-30 13:04:56 -04003626 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003627 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003628 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003629
jchen103fd614d2018-08-13 12:21:58 +08003630 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003631 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003632 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003633 {
3634 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3635 outputVariable.name != "gl_FragData")
3636 {
3637 continue;
3638 }
3639
3640 unsigned int baseLocation =
3641 (outputVariable.location == -1 ? 0u
3642 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003643
3644 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3645 // structures, so we may use getBasicTypeElementCount().
3646 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3647 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003648 {
3649 const unsigned int location = baseLocation + elementIndex;
3650 if (location >= mState.mOutputVariableTypes.size())
3651 {
3652 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3653 }
Corentin Walleze7557742017-06-01 13:09:57 -04003654 ASSERT(location < mState.mActiveOutputVariables.size());
3655 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003656 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003657 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003658 }
3659 }
3660
jchen103fd614d2018-08-13 12:21:58 +08003661 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003662 {
3663 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3664 // A link error will be generated if the sum of the number of active image uniforms used in
3665 // all shaders, the number of active shader storage blocks, and the number of active
3666 // fragment shader outputs exceeds the implementation-dependent value of
3667 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3668 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3669 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003670 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003671 {
3672 mInfoLog
3673 << "The sum of the number of active image uniforms, active shader storage blocks "
3674 "and active fragment shader outputs exceeds "
3675 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003676 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003677 return false;
3678 }
3679 }
3680
Jamie Madill80a6fc02015-08-21 16:53:16 -04003681 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003682 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003683 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003684
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003685 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003686 // TODO(jmadill): any caps validation here?
3687
Olli Etuaho0ca09752018-09-24 11:00:50 +03003688 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3689 {
3690 if (outputVariable.isArray())
3691 {
3692 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3693 // Resources and including [0] at the end of array variable names.
3694 outputVariable.name += "[0]";
3695 outputVariable.mappedName += "[0]";
3696 }
3697 }
3698
3699 bool hasSecondaryOutputs = false;
3700
3701 // Reserve locations for output variables whose location is fixed in the shader or through the
3702 // API.
jchen1015015f72017-03-16 13:54:21 +08003703 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003704 outputVariableIndex++)
3705 {
jchen1015015f72017-03-16 13:54:21 +08003706 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003707
3708 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3709 if (outputVariable.isBuiltIn())
3710 continue;
3711
Olli Etuaho0ca09752018-09-24 11:00:50 +03003712 int baseLocation = getOutputLocationForLink(outputVariable);
3713 if (baseLocation == -1)
3714 {
3715 // Here we're only reserving locations for variables whose location is fixed.
3716 continue;
3717 }
3718
3719 auto *outputLocations = &mState.mOutputLocations;
3720 if (isOutputSecondaryForLink(outputVariable))
3721 {
3722 outputLocations = &mState.mSecondaryOutputLocations;
3723 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3724 // an output has an index specified it will always also have the location specified.
3725 hasSecondaryOutputs = true;
3726 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003727
Olli Etuaho465835d2017-09-26 13:34:10 +03003728 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3729 // structures, so we may use getBasicTypeElementCount().
Jamie Madillb980c562018-11-27 11:34:27 -05003730 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003731 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3732 if (outputLocationsNeeded > outputLocations->size())
3733 {
3734 outputLocations->resize(outputLocationsNeeded);
3735 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003736 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003737 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003738 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3739 ASSERT(location < outputLocations->size());
3740 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003741 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003742 mInfoLog << "Location of variable " << outputVariable.name
3743 << " conflicts with another variable.";
3744 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003745 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003746 if (outputVariable.isArray())
3747 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003748 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003749 }
3750 else
3751 {
3752 VariableLocation locationInfo;
Jamie Madillb980c562018-11-27 11:34:27 -05003753 locationInfo.index = outputVariableIndex;
3754 (*outputLocations)[location] = locationInfo;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003755 }
3756 }
3757 }
3758
3759 // Here we assign locations for the output variables that don't yet have them. Note that we're
3760 // not necessarily able to fit the variables optimally, since then we might have to try
3761 // different arrangements of output arrays. Now we just assign the locations in the order that
3762 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3763 // finding locations for the output variables, so this should be acceptable at least for now.
3764 GLuint maxLocation = caps.maxDrawBuffers;
3765 if (hasSecondaryOutputs)
3766 {
3767 // EXT_blend_func_extended: Program outputs will be validated against
3768 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3769 maxLocation = extensions.maxDualSourceDrawBuffers;
3770 }
3771
3772 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3773 outputVariableIndex++)
3774 {
3775 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3776
3777 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3778 if (outputVariable.isBuiltIn())
3779 continue;
3780
3781 if (getOutputLocationForLink(outputVariable) != -1)
3782 {
3783 continue;
3784 }
3785
3786 auto *outputLocations = &mState.mOutputLocations;
3787 if (isOutputSecondaryForLink(outputVariable))
3788 {
3789 outputLocations = &mState.mSecondaryOutputLocations;
3790 }
3791
3792 int baseLocation = 0;
3793 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3794 bool elementsFit = false;
3795 while (!elementsFit)
3796 {
3797 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3798 elementsFit = true;
3799 if (baseLocation + elementCount > maxLocation)
3800 {
3801 // EXT_blend_func_extended: Linking can fail:
3802 // "if the explicit binding assignments do not leave enough space for the linker to
3803 // automatically assign a location for a varying out array, which requires multiple
3804 // contiguous locations."
3805 mInfoLog << "Could not fit output variable into available locations: "
3806 << outputVariable.name;
3807 return false;
3808 }
3809 unsigned int outputLocationsNeeded =
3810 static_cast<unsigned int>(baseLocation) + elementCount;
3811 if (outputLocationsNeeded > outputLocations->size())
3812 {
3813 outputLocations->resize(outputLocationsNeeded);
3814 }
3815 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3816 {
3817 const unsigned int location =
3818 static_cast<unsigned int>(baseLocation) + elementIndex;
3819 ASSERT(location < outputLocations->size());
3820 if (outputLocations->at(location).used())
3821 {
3822 elementsFit = false;
3823 break;
3824 }
3825 }
3826 if (elementsFit)
3827 {
3828 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3829 {
3830 const unsigned int location =
3831 static_cast<unsigned int>(baseLocation) + elementIndex;
3832 if (outputVariable.isArray())
3833 {
3834 (*outputLocations)[location] =
3835 VariableLocation(elementIndex, outputVariableIndex);
3836 }
3837 else
3838 {
3839 VariableLocation locationInfo;
3840 locationInfo.index = outputVariableIndex;
3841 (*outputLocations)[location] = locationInfo;
3842 }
3843 }
3844 }
3845 else
3846 {
3847 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003848 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003849 }
3850 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003851
3852 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003853}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003854
Olli Etuaho48fed632017-03-16 12:05:30 +00003855void Program::setUniformValuesFromBindingQualifiers()
3856{
Jamie Madill982f6e02017-06-07 14:33:04 -04003857 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003858 {
3859 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3860 if (samplerUniform.binding != -1)
3861 {
Olli Etuahod2551232017-10-26 20:03:33 +03003862 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003863 ASSERT(location != -1);
3864 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003865 for (unsigned int elementIndex = 0;
3866 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003867 {
3868 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3869 }
Jamie Madille3e680c2018-12-03 17:49:08 -05003870
3871 // Here we pass nullptr to avoid a large chain of calls that need a non-const Context.
3872 // We know it's safe not to notify the Context because this is only called after link.
3873 setUniform1iv(nullptr, location, static_cast<GLsizei>(boundTextureUnits.size()),
Olli Etuaho48fed632017-03-16 12:05:30 +00003874 boundTextureUnits.data());
3875 }
3876 }
3877}
3878
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003879void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003880{
jchen10af713a22017-04-19 09:10:56 +08003881 // Set initial bindings from shader.
3882 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3883 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003884 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003885 bindUniformBlock(blockIndex, uniformBlock.binding);
3886 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003887}
3888
Jamie Madille3e680c2018-12-03 17:49:08 -05003889void Program::updateSamplerUniform(Context *context,
3890 const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003891 GLsizei clampedCount,
3892 const GLint *v)
3893{
Jamie Madill81c2e252017-09-09 23:32:46 -04003894 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
Jamie Madillb980c562018-11-27 11:34:27 -05003895 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003896 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3897 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003898
Jamie Madill7e4eff12018-08-08 15:49:26 -04003899 if (samplerBinding.unreferenced)
3900 return;
3901
3902 // Update the sampler uniforms.
3903 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3904 {
Jamie Madille3e680c2018-12-03 17:49:08 -05003905 GLint oldTextureUnit = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3906 GLint newTextureUnit = v[arrayIndex];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003907
Jamie Madille3e680c2018-12-03 17:49:08 -05003908 if (oldTextureUnit == newTextureUnit)
Jamie Madill7e4eff12018-08-08 15:49:26 -04003909 continue;
3910
Jamie Madille3e680c2018-12-03 17:49:08 -05003911 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newTextureUnit;
Jamie Madill7e4eff12018-08-08 15:49:26 -04003912
3913 // Update the reference counts.
Jamie Madille3e680c2018-12-03 17:49:08 -05003914 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldTextureUnit];
3915 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newTextureUnit];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003916 ASSERT(oldRefCount > 0);
3917 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3918 oldRefCount--;
3919 newRefCount++;
3920
3921 // Check for binding type change.
James Darpiniane4109f22018-12-13 16:25:53 -08003922 TextureType &newSamplerType = mState.mActiveSamplerTypes[newTextureUnit];
3923 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldTextureUnit];
3924 SamplerFormat &newSamplerFormat = mState.mActiveSamplerFormats[newTextureUnit];
3925 SamplerFormat &oldSamplerFormat = mState.mActiveSamplerFormats[oldTextureUnit];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003926
3927 if (newRefCount == 1)
3928 {
James Darpiniane4109f22018-12-13 16:25:53 -08003929 newSamplerType = samplerBinding.textureType;
3930 newSamplerFormat = samplerBinding.format;
Jamie Madille3e680c2018-12-03 17:49:08 -05003931 mState.mActiveSamplersMask.set(newTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003932 }
James Darpiniane4109f22018-12-13 16:25:53 -08003933 else
Jamie Madill7e4eff12018-08-08 15:49:26 -04003934 {
James Darpiniane4109f22018-12-13 16:25:53 -08003935 if (newSamplerType != samplerBinding.textureType)
3936 {
3937 // Conflict detected. Ensure we reset it properly.
3938 newSamplerType = TextureType::InvalidEnum;
3939 }
3940 if (newSamplerFormat != samplerBinding.format)
3941 {
3942 newSamplerFormat = SamplerFormat::InvalidEnum;
3943 }
Jamie Madill7e4eff12018-08-08 15:49:26 -04003944 }
3945
3946 // Unset previously active sampler.
3947 if (oldRefCount == 0)
3948 {
James Darpiniane4109f22018-12-13 16:25:53 -08003949 oldSamplerType = TextureType::InvalidEnum;
3950 oldSamplerFormat = SamplerFormat::InvalidEnum;
Jamie Madille3e680c2018-12-03 17:49:08 -05003951 mState.mActiveSamplersMask.reset(oldTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003952 }
James Darpiniane4109f22018-12-13 16:25:53 -08003953 else
Jamie Madill7e4eff12018-08-08 15:49:26 -04003954 {
James Darpiniane4109f22018-12-13 16:25:53 -08003955 if (oldSamplerType == TextureType::InvalidEnum ||
3956 oldSamplerFormat == SamplerFormat::InvalidEnum)
3957 {
3958 // Previous conflict. Check if this new change fixed the conflict.
3959 mState.setSamplerUniformTextureTypeAndFormat(oldTextureUnit);
3960 }
Jamie Madille3e680c2018-12-03 17:49:08 -05003961 }
3962
3963 // Notify context.
3964 if (context)
3965 {
3966 context->onSamplerUniformChange(newTextureUnit);
3967 context->onSamplerUniformChange(oldTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003968 }
3969 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003970
3971 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003972 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003973}
3974
James Darpiniane4109f22018-12-13 16:25:53 -08003975void ProgramState::setSamplerUniformTextureTypeAndFormat(size_t textureUnitIndex)
Jamie Madill7e4eff12018-08-08 15:49:26 -04003976{
James Darpiniane4109f22018-12-13 16:25:53 -08003977 bool foundBinding = false;
3978 TextureType foundType = TextureType::InvalidEnum;
3979 SamplerFormat foundFormat = SamplerFormat::InvalidEnum;
Jamie Madill7e4eff12018-08-08 15:49:26 -04003980
3981 for (const SamplerBinding &binding : mSamplerBindings)
3982 {
3983 if (binding.unreferenced)
3984 continue;
3985
3986 // A conflict exists if samplers of different types are sourced by the same texture unit.
3987 // We need to check all bound textures to detect this error case.
3988 for (GLuint textureUnit : binding.boundTextureUnits)
3989 {
3990 if (textureUnit == textureUnitIndex)
3991 {
James Darpiniane4109f22018-12-13 16:25:53 -08003992 if (!foundBinding)
Jamie Madill7e4eff12018-08-08 15:49:26 -04003993 {
James Darpiniane4109f22018-12-13 16:25:53 -08003994 foundBinding = true;
3995 foundType = binding.textureType;
3996 foundFormat = binding.format;
Jamie Madill7e4eff12018-08-08 15:49:26 -04003997 }
James Darpiniane4109f22018-12-13 16:25:53 -08003998 else
Jamie Madill7e4eff12018-08-08 15:49:26 -04003999 {
James Darpiniane4109f22018-12-13 16:25:53 -08004000 if (foundType != binding.textureType)
4001 {
4002 foundType = TextureType::InvalidEnum;
4003 }
4004 if (foundFormat != binding.format)
4005 {
4006 foundFormat = SamplerFormat::InvalidEnum;
4007 }
Jamie Madill7e4eff12018-08-08 15:49:26 -04004008 }
4009 }
4010 }
4011 }
4012
James Darpiniane4109f22018-12-13 16:25:53 -08004013 mActiveSamplerTypes[textureUnitIndex] = foundType;
4014 mActiveSamplerFormats[textureUnitIndex] = foundFormat;
Jamie Madill7e4eff12018-08-08 15:49:26 -04004015}
4016
Jamie Madille7d84322017-01-10 18:21:59 -05004017template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004018GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
4019 GLsizei count,
4020 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05004021 const T *v)
4022{
Jamie Madill134f93d2017-08-31 17:11:00 -04004023 if (count == 1)
4024 return 1;
4025
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004026 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04004027
Corentin Wallez15ac5342016-11-03 17:06:39 -04004028 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4029 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004030 unsigned int remainingElements =
4031 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004032 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004033 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004034
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004035 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004036 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004037 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004038 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004039
4040 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004041}
4042
4043template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004044GLsizei Program::clampMatrixUniformCount(GLint location,
4045 GLsizei count,
4046 GLboolean transpose,
4047 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004048{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004049 const VariableLocation &locationInfo = mState.mUniformLocations[location];
4050
Jamie Madill62d31cb2015-09-11 13:25:51 -04004051 if (!transpose)
4052 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004053 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004054 }
4055
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004056 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004057
4058 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4059 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004060 unsigned int remainingElements =
4061 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004062 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004063}
4064
Jamie Madill54164b02017-08-28 15:17:37 -04004065// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4066// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004067template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004068void Program::getUniformInternal(const Context *context,
4069 DestT *dataOut,
4070 GLint location,
4071 GLenum nativeType,
4072 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004073{
Jamie Madill54164b02017-08-28 15:17:37 -04004074 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004075 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004076 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004077 {
4078 GLint tempValue[16] = {0};
4079 mProgram->getUniformiv(context, location, tempValue);
4080 UniformStateQueryCastLoop<GLboolean>(
4081 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004082 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004083 }
4084 case GL_INT:
4085 {
4086 GLint tempValue[16] = {0};
4087 mProgram->getUniformiv(context, location, tempValue);
4088 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4089 components);
4090 break;
4091 }
4092 case GL_UNSIGNED_INT:
4093 {
4094 GLuint tempValue[16] = {0};
4095 mProgram->getUniformuiv(context, location, tempValue);
4096 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4097 components);
4098 break;
4099 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004100 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004101 {
4102 GLfloat tempValue[16] = {0};
4103 mProgram->getUniformfv(context, location, tempValue);
4104 UniformStateQueryCastLoop<GLfloat>(
4105 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004106 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004107 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004108 default:
4109 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004110 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004111 }
4112}
Jamie Madilla4595b82017-01-11 17:36:34 -05004113
4114bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4115{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004116 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004117 // Must be called after samplers are validated.
4118 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4119
4120 for (const auto &binding : mState.mSamplerBindings)
4121 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004122 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004123 for (const auto &unit : binding.boundTextureUnits)
4124 {
4125 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4126 if (programTextureID == textureID)
4127 {
4128 // TODO(jmadill): Check for appropriate overlap.
4129 return true;
4130 }
4131 }
4132 }
4133
4134 return false;
4135}
4136
Jamie Madill6f755b22018-10-09 12:48:54 -04004137angle::Result Program::syncState(const Context *context)
Jamie Madill70aeda42018-08-20 12:17:40 -04004138{
4139 if (mDirtyBits.any())
4140 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004141 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004142 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4143 mDirtyBits.reset();
4144 }
4145
Jamie Madill7c985f52018-11-29 18:16:17 -05004146 return angle::Result::Continue;
Jamie Madill70aeda42018-08-20 12:17:40 -04004147}
Jamie Madilla2c74982016-12-12 11:20:42 -05004148} // namespace gl