blob: 02558c02a8bfc2737825c0bf224978c53ae76c5f [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.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800727SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500728 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
Jamie Madillb980c562018-11-27 11:34:27 -0500729{}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500730
731SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
732
733SamplerBinding::~SamplerBinding() = default;
734
Jamie Madill3c1da042017-11-27 18:33:40 -0500735// ProgramBindings implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500736ProgramBindings::ProgramBindings() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500737
Jamie Madillb980c562018-11-27 11:34:27 -0500738ProgramBindings::~ProgramBindings() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500739
Jamie Madill3c1da042017-11-27 18:33:40 -0500740void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400741{
742 mBindings[name] = index;
743}
744
Jamie Madill3c1da042017-11-27 18:33:40 -0500745int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400746{
747 auto iter = mBindings.find(name);
748 return (iter != mBindings.end()) ? iter->second : -1;
749}
750
Jamie Madill3c1da042017-11-27 18:33:40 -0500751ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400752{
753 return mBindings.begin();
754}
755
Jamie Madill3c1da042017-11-27 18:33:40 -0500756ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400757{
758 return mBindings.end();
759}
760
Jamie Madill3c1da042017-11-27 18:33:40 -0500761// ImageBinding implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500762ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0), unreferenced(false) {}
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800763ImageBinding::ImageBinding(GLuint imageUnit, size_t count, bool unreferenced)
764 : unreferenced(unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500765{
766 for (size_t index = 0; index < count; ++index)
767 {
768 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
769 }
770}
771
772ImageBinding::ImageBinding(const ImageBinding &other) = default;
773
774ImageBinding::~ImageBinding() = default;
775
Jamie Madill3c1da042017-11-27 18:33:40 -0500776// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400777ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500778 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800779 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500780 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400781 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500782 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800783 mImageUniformRange(0, 0),
784 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300785 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800786 mNumViews(-1),
787 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400788 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
789 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800790 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400791 mGeometryShaderMaxVertices(0),
Austin Eng1bf18ce2018-10-19 15:34:02 -0700792 mDrawIDLocation(-1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400793 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400794{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300795 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400796 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400797}
798
Jamie Madill48ef11b2016-04-27 15:21:52 -0400799ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400800{
Jiawei Shao016105b2018-04-12 16:38:31 +0800801 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400802}
803
Jamie Madill48ef11b2016-04-27 15:21:52 -0400804const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500805{
806 return mLabel;
807}
808
Jiawei Shao385b3e02018-03-21 09:43:28 +0800809Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
810{
Jiawei Shao016105b2018-04-12 16:38:31 +0800811 ASSERT(shaderType != ShaderType::InvalidEnum);
812 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800813}
814
Jamie Madille7d84322017-01-10 18:21:59 -0500815GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816{
jchen1015015f72017-03-16 13:54:21 +0800817 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400818}
819
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800820GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
821{
822 return GetResourceIndexFromName(mBufferVariables, name);
823}
824
Jamie Madille7d84322017-01-10 18:21:59 -0500825GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
826{
827 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
828 return mUniformLocations[location].index;
829}
830
831Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
832{
833 GLuint index = getUniformIndexFromLocation(location);
834 if (!isSamplerUniformIndex(index))
835 {
836 return Optional<GLuint>::Invalid();
837 }
838
839 return getSamplerIndexFromUniformIndex(index);
840}
841
842bool ProgramState::isSamplerUniformIndex(GLuint index) const
843{
Jamie Madill982f6e02017-06-07 14:33:04 -0400844 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500845}
846
847GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
848{
849 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400850 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500851}
852
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800853bool ProgramState::isImageUniformIndex(GLuint index) const
854{
855 return mImageUniformRange.contains(index);
856}
857
858GLuint ProgramState::getImageIndexFromUniformIndex(GLuint uniformIndex) const
859{
860 ASSERT(isImageUniformIndex(uniformIndex));
861 return uniformIndex - mImageUniformRange.low();
862}
863
Jamie Madill34ca4f52017-06-13 11:49:39 -0400864GLuint ProgramState::getAttributeLocation(const std::string &name) const
865{
866 for (const sh::Attribute &attribute : mAttributes)
867 {
868 if (attribute.name == name)
869 {
870 return attribute.location;
871 }
872 }
873
874 return static_cast<GLuint>(-1);
875}
876
Jiawei Shao016105b2018-04-12 16:38:31 +0800877bool ProgramState::hasAttachedShader() const
878{
879 for (const Shader *shader : mAttachedShaders)
880 {
881 if (shader)
882 {
883 return true;
884 }
885 }
886 return false;
887}
888
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500889Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400890 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400891 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800893 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894 mDeleteStatus(false),
895 mRefCount(0),
896 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500897 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898{
899 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000900
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
904Program::~Program()
905{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400906 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
Jamie Madill4928b7c2017-06-20 12:57:39 -0400909void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500910{
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400911 resolveLink(context);
Jiawei Shao016105b2018-04-12 16:38:31 +0800912 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500913 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800914 if (mState.mAttachedShaders[shaderType])
915 {
916 mState.mAttachedShaders[shaderType]->release(context);
917 mState.mAttachedShaders[shaderType] = nullptr;
918 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800919 }
920
Jamie Madillf4a789f2018-10-18 16:56:20 -0400921 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400922
Jiawei Shao016105b2018-04-12 16:38:31 +0800923 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400924 SafeDelete(mProgram);
925
926 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500927}
jchen107ae70d82018-07-06 13:47:01 +0800928GLuint Program::id() const
929{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400930 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800931 return mHandle;
932}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500933
Jamie Madille90d4ee2018-11-28 14:04:00 -0500934void Program::setLabel(const Context *context, const std::string &label)
Geoff Lang70d0f492015-12-10 17:45:46 -0500935{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400936 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400937 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500938}
939
940const std::string &Program::getLabel() const
941{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400942 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500944}
945
Jamie Madillef300b12016-10-07 15:12:09 -0400946void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400948 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800949 ShaderType shaderType = shader->getType();
950 ASSERT(shaderType != ShaderType::InvalidEnum);
951
952 mState.mAttachedShaders[shaderType] = shader;
953 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
Jamie Madillc1d770e2017-04-13 17:31:24 -0400956void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400958 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800959 ShaderType shaderType = shader->getType();
960 ASSERT(shaderType != ShaderType::InvalidEnum);
961
962 ASSERT(mState.mAttachedShaders[shaderType] == shader);
963 shader->release(context);
964 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000967int Program::getAttachedShadersCount() const
968{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400969 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800970 int numAttachedShaders = 0;
971 for (const Shader *shader : mState.mAttachedShaders)
972 {
973 if (shader)
974 {
975 ++numAttachedShaders;
976 }
977 }
978
979 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000980}
981
Jiawei Shao385b3e02018-03-21 09:43:28 +0800982const Shader *Program::getAttachedShader(ShaderType shaderType) const
983{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400984 ASSERT(mLinkResolved);
Jiawei Shao385b3e02018-03-21 09:43:28 +0800985 return mState.getAttachedShader(shaderType);
986}
987
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988void Program::bindAttributeLocation(GLuint index, const char *name)
989{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400990 ASSERT(mLinkResolved);
Geoff Langd8605522016-04-13 10:19:12 -0400991 mAttributeBindings.bindLocation(index, name);
992}
993
994void Program::bindUniformLocation(GLuint index, const char *name)
995{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400996 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +0300997 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
Sami Väisänen46eaa942016-06-29 10:26:37 +03001000void Program::bindFragmentInputLocation(GLint index, const char *name)
1001{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001002 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001003 mFragmentInputBindings.bindLocation(index, name);
1004}
1005
Olli Etuaho0ca09752018-09-24 11:00:50 +03001006void Program::bindFragmentOutputLocation(GLuint index, const char *name)
1007{
1008 mFragmentOutputLocations.bindLocation(index, name);
1009}
1010
1011void Program::bindFragmentOutputIndex(GLuint index, const char *name)
1012{
1013 mFragmentOutputIndexes.bindLocation(index, name);
1014}
1015
jchen103fd614d2018-08-13 12:21:58 +08001016BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001017{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001018 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001019 BindingInfo ret;
1020 ret.type = GL_NONE;
1021 ret.valid = false;
1022
Jiawei Shao385b3e02018-03-21 09:43:28 +08001023 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001024 ASSERT(fragmentShader);
1025
1026 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001027 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001028
1029 for (const auto &binding : mFragmentInputBindings)
1030 {
1031 if (binding.second != static_cast<GLuint>(index))
1032 continue;
1033
1034 ret.valid = true;
1035
Olli Etuahod2551232017-10-26 20:03:33 +03001036 size_t nameLengthWithoutArrayIndex;
1037 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001038
1039 for (const auto &in : inputs)
1040 {
Olli Etuahod2551232017-10-26 20:03:33 +03001041 if (in.name.length() == nameLengthWithoutArrayIndex &&
1042 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001043 {
1044 if (in.isArray())
1045 {
1046 // The client wants to bind either "name" or "name[0]".
1047 // GL ES 3.1 spec refers to active array names with language such as:
1048 // "if the string identifies the base name of an active array, where the
1049 // string would exactly match the name of the variable if the suffix "[0]"
1050 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001051 if (arrayIndex == GL_INVALID_INDEX)
1052 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001053
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001054 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001055 }
1056 else
1057 {
1058 ret.name = in.mappedName;
1059 }
1060 ret.type = in.type;
1061 return ret;
1062 }
1063 }
1064 }
1065
1066 return ret;
1067}
1068
jchen103fd614d2018-08-13 12:21:58 +08001069void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001070 GLenum genMode,
1071 GLint components,
1072 const GLfloat *coeffs)
1073{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001074 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001075 // If the location is -1 then the command is silently ignored
1076 if (index == -1)
1077 return;
1078
jchen103fd614d2018-08-13 12:21:58 +08001079 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001080
1081 // If the input doesn't exist then then the command is silently ignored
1082 // This could happen through optimization for example, the shader translator
1083 // decides that a variable is not actually being used and optimizes it away.
1084 if (binding.name.empty())
1085 return;
1086
1087 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1088}
1089
Martin Radev4c4c8e72016-08-04 12:25:34 +03001090// The attached shaders are checked for linking errors by matching up their variables.
1091// Uniform, input and output variables get collected.
1092// The code gets compiled into binaries.
Jamie Madillf4a789f2018-10-18 16:56:20 -04001093angle::Result Program::link(const Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001094{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001095 const auto &data = context->getContextState();
1096
Jamie Madill6c58b062017-08-01 13:44:25 -04001097 auto *platform = ANGLEPlatformCurrent();
1098 double startTime = platform->currentTime(platform);
1099
Jamie Madill6c1f6712017-02-14 19:08:04 -05001100 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001101 mInfoLog.reset();
1102
1103 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001104 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001105 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001106 return angle::Result::Continue;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001107 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -04001109 egl::BlobCache::Key programHash = {0};
Jamie Madillb980c562018-11-27 11:34:27 -05001110 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001111
Jamie Madill32447362017-06-28 14:53:52 -04001112 if (cache)
1113 {
Jamie Madill785e8a02018-10-04 17:42:00 -04001114 angle::Result result = cache->getProgram(context, this, &mState, &programHash);
Jamie Madill7c985f52018-11-29 18:16:17 -05001115 mLinked = (result == angle::Result::Continue);
Jamie Madill785e8a02018-10-04 17:42:00 -04001116 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001117 }
1118
1119 if (mLinked)
1120 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001121 double delta = platform->currentTime(platform) - startTime;
1122 int us = static_cast<int>(delta * 1000000.0);
1123 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill7c985f52018-11-29 18:16:17 -05001124 return angle::Result::Continue;
Jamie Madill32447362017-06-28 14:53:52 -04001125 }
1126
1127 // Cache load failed, fall through to normal linking.
1128 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001129
Jamie Madill6bc264a2018-03-31 15:36:05 -04001130 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001131 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001132
jchen107ae70d82018-07-06 13:47:01 +08001133 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001134 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001135 {
jchen107ae70d82018-07-06 13:47:01 +08001136 resources.reset(
1137 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1138 {&mState.mUniformBlocks, &mState.mUniforms},
1139 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1140 {&mState.mAtomicCounterBuffers},
1141 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001142
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001143 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001144 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1145 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001146 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001147 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001148 }
1149
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001150 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001151 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1152 context->getExtensions().webglCompatibility, mInfoLog,
1153 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001154 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001155 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001156 }
1157
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001158 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1159 // A link error will be generated if the sum of the number of active image uniforms used in
1160 // all shaders, the number of active shader storage blocks, and the number of active
1161 // fragment shader outputs exceeds the implementation-dependent value of
1162 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1163 if (combinedImageUniforms + combinedShaderStorageBlocks >
1164 context->getCaps().maxCombinedShaderOutputResources)
1165 {
1166 mInfoLog
1167 << "The sum of the number of active image uniforms, active shader storage blocks "
1168 "and active fragment shader outputs exceeds "
1169 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1170 << context->getCaps().maxCombinedShaderOutputResources << ")";
Jamie Madill7c985f52018-11-29 18:16:17 -05001171 return angle::Result::Continue;
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001172 }
1173
jchen103fd614d2018-08-13 12:21:58 +08001174 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1175 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001176 }
1177 else
1178 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001179 // Map the varyings to the register file
1180 // In WebGL, we use a slightly different handling for packing variables.
1181 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1182 if (data.getLimitations().noFlexibleVaryingPacking)
1183 {
1184 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1185 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1186 }
1187 else if (data.getExtensions().webglCompatibility)
1188 {
1189 packMode = PackMode::WEBGL_STRICT;
1190 }
1191
jchen107ae70d82018-07-06 13:47:01 +08001192 resources.reset(
1193 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1194 {&mState.mUniformBlocks, &mState.mUniforms},
1195 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1196 {&mState.mAtomicCounterBuffers},
1197 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001198
jchen103fd614d2018-08-13 12:21:58 +08001199 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001200 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001201 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001202 }
1203
jchen103fd614d2018-08-13 12:21:58 +08001204 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001205 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001206 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001207 }
1208
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001209 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001210 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1211 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001212 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001213 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001214 }
1215
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001216 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001217 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1218 context->getExtensions().webglCompatibility, mInfoLog,
1219 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001220 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001221 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001222 }
1223
jchen103fd614d2018-08-13 12:21:58 +08001224 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001225 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001226 return angle::Result::Continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001227 }
1228
Olli Etuaho0ca09752018-09-24 11:00:50 +03001229 if (!linkOutputVariables(context->getCaps(), context->getExtensions(),
1230 context->getClientVersion(), combinedImageUniforms,
1231 combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001232 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001233 return angle::Result::Continue;
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001234 }
1235
jchen103fd614d2018-08-13 12:21:58 +08001236 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001237
Jiawei Shao016105b2018-04-12 16:38:31 +08001238 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001239 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001240
jchen103fd614d2018-08-13 12:21:58 +08001241 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1242 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001243
jchen103fd614d2018-08-13 12:21:58 +08001244 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1245 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001246 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001247 return angle::Result::Continue;
Jamie Madill192745a2016-12-22 15:58:21 -05001248 }
1249
jchen107ae70d82018-07-06 13:47:01 +08001250 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001251 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001252 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001253 return angle::Result::Continue;
Olli Etuaho39e78122017-08-29 14:34:22 +03001254 }
1255
Martin Radev4c4c8e72016-08-04 12:25:34 +03001256 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001257 }
1258
jchen107ae70d82018-07-06 13:47:01 +08001259 mLinkingState.reset(new LinkingState());
1260 mLinkingState->context = context;
1261 mLinkingState->programHash = programHash;
1262 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1263 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001264 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001265
Jamie Madill7c985f52018-11-29 18:16:17 -05001266 return angle::Result::Continue;
jchen107ae70d82018-07-06 13:47:01 +08001267}
1268
1269bool Program::isLinking() const
1270{
1271 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1272}
1273
Jamie Madill785e8a02018-10-04 17:42:00 -04001274void Program::resolveLinkImpl(const Context *context)
jchen107ae70d82018-07-06 13:47:01 +08001275{
1276 ASSERT(mLinkingState.get());
1277
Jamie Madill785e8a02018-10-04 17:42:00 -04001278 angle::Result result = mLinkingState->linkEvent->wait(context);
1279
Jamie Madill7c985f52018-11-29 18:16:17 -05001280 mLinked = result == angle::Result::Continue;
jchen105055fba2018-08-17 09:57:07 +08001281 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001282 auto linkingState = std::move(mLinkingState);
1283 if (!mLinked)
1284 {
1285 return;
1286 }
1287
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001288 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001289
Yunchao Heece12532017-11-21 15:50:21 +08001290 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1291 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001292 ASSERT(mLinked);
1293 updateLinkedShaderStages();
1294
Jamie Madill54164b02017-08-28 15:17:37 -04001295 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001296 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1297 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001298
Jamie Madill7e4eff12018-08-08 15:49:26 -04001299 // Must be called after markUnusedUniformLocations.
1300 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001301 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001302
1303 setUniformValuesFromBindingQualifiers();
1304
Austin Eng1bf18ce2018-10-19 15:34:02 -07001305 if (context->getExtensions().multiDraw)
1306 {
1307 mState.mDrawIDLocation = getUniformLocation("gl_DrawID");
1308 }
1309
Jamie Madill32447362017-06-28 14:53:52 -04001310 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001311 auto *cache = linkingState->context->getMemoryProgramCache();
1312 if (cache &&
1313 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1314 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001315 {
jchen107ae70d82018-07-06 13:47:01 +08001316 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001317 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001318}
1319
Yunchao He85072e82017-11-14 15:43:28 +08001320void Program::updateLinkedShaderStages()
1321{
Yunchao Heece12532017-11-21 15:50:21 +08001322 mState.mLinkedShaderStages.reset();
1323
Jiawei Shao016105b2018-04-12 16:38:31 +08001324 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001325 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001326 if (shader)
1327 {
1328 mState.mLinkedShaderStages.set(shader->getType());
1329 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001330 }
Yunchao He85072e82017-11-14 15:43:28 +08001331}
1332
James Darpinian30b604d2018-03-12 17:26:57 -07001333void ProgramState::updateTransformFeedbackStrides()
1334{
1335 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1336 {
1337 mTransformFeedbackStrides.resize(1);
1338 size_t totalSize = 0;
1339 for (auto &varying : mLinkedTransformFeedbackVaryings)
1340 {
1341 totalSize += varying.size() * VariableExternalSize(varying.type);
1342 }
1343 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1344 }
1345 else
1346 {
1347 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1348 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1349 {
1350 auto &varying = mLinkedTransformFeedbackVaryings[i];
1351 mTransformFeedbackStrides[i] =
1352 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1353 }
1354 }
1355}
1356
Jamie Madill7e4eff12018-08-08 15:49:26 -04001357void ProgramState::updateActiveSamplers()
1358{
1359 for (SamplerBinding &samplerBinding : mSamplerBindings)
1360 {
1361 if (samplerBinding.unreferenced)
1362 continue;
1363
1364 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1365 {
1366 mActiveSamplerRefCounts[textureUnit]++;
1367 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1368 mActiveSamplersMask.set(textureUnit);
1369 }
1370 }
1371}
1372
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001373void ProgramState::updateActiveImages()
1374{
1375 for (ImageBinding &imageBinding : mImageBindings)
1376 {
1377 if (imageBinding.unreferenced)
1378 continue;
1379
1380 for (GLint imageUnit : imageBinding.boundImageUnits)
1381 {
1382 mActiveImagesMask.set(imageUnit);
1383 }
1384 }
1385}
1386
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001387// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001388void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001389{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001390 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001391 mState.mAttributesTypeMask.reset();
1392 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001393 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001394 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001395 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001396 mState.mUniforms.clear();
1397 mState.mUniformLocations.clear();
1398 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001399 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001400 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001401 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001402 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001403 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001404 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001405 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001406 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001407 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001408 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001409 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001410 mState.mNumViews = -1;
1411 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1412 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001413 mState.mGeometryShaderInvocations = 1;
1414 mState.mGeometryShaderMaxVertices = 0;
Austin Eng1bf18ce2018-10-19 15:34:02 -07001415 mState.mDrawIDLocation = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417 mValidated = false;
1418
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001419 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001420 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001421}
1422
Jamie Madillf4a789f2018-10-18 16:56:20 -04001423angle::Result Program::loadBinary(const Context *context,
1424 GLenum binaryFormat,
1425 const void *binary,
1426 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001427{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001428 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001429 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001430
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
Jamie Madill7c985f52018-11-29 18:16:17 -05001432 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001434 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1435 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001436 {
Jamie Madillf6113162015-05-07 11:49:21 -04001437 mInfoLog << "Invalid program binary format.";
Jamie Madill7c985f52018-11-29 18:16:17 -05001438 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439 }
1440
Jamie Madill4f86d052017-06-05 12:59:26 -04001441 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
Jamie Madill785e8a02018-10-04 17:42:00 -04001442 angle::Result result =
1443 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog);
Jamie Madill7c985f52018-11-29 18:16:17 -05001444 mLinked = result == angle::Result::Continue;
Jamie Madill785e8a02018-10-04 17:42:00 -04001445 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001446
1447 // Currently we require the full shader text to compute the program hash.
Jamie Madill785e8a02018-10-04 17:42:00 -04001448 // We could also store the binary in the internal program cache.
Jamie Madill32447362017-06-28 14:53:52 -04001449
Jamie Madill70aeda42018-08-20 12:17:40 -04001450 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1451 ++uniformBlockIndex)
1452 {
1453 mDirtyBits.set(uniformBlockIndex);
1454 }
1455
Jamie Madill7c985f52018-11-29 18:16:17 -05001456 return angle::Result::Continue;
Jamie Madilla2c74982016-12-12 11:20:42 -05001457#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458}
1459
Jamie Madillf4a789f2018-10-18 16:56:20 -04001460angle::Result Program::saveBinary(Context *context,
1461 GLenum *binaryFormat,
1462 void *binary,
1463 GLsizei bufSize,
1464 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001466 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 if (binaryFormat)
1468 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001469 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470 }
1471
Jamie Madill4f86d052017-06-05 12:59:26 -04001472 angle::MemoryBuffer memoryBuf;
1473 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474
Jamie Madill4f86d052017-06-05 12:59:26 -04001475 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1476 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477
1478 if (streamLength > bufSize)
1479 {
1480 if (length)
1481 {
1482 *length = 0;
1483 }
1484
Jamie Madill493f9572018-05-24 19:52:15 -04001485 // TODO: This should be moved to the validation layer but computing the size of the binary
1486 // before saving it causes the save to happen twice. It may be possible to write the binary
1487 // to a separate buffer, validate sizes and then copy it.
Jamie Madillf4a789f2018-10-18 16:56:20 -04001488 ANGLE_CHECK(context, false, "Insufficient buffer size", GL_INVALID_OPERATION);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489 }
1490
1491 if (binary)
1492 {
Jamie Madill493f9572018-05-24 19:52:15 -04001493 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494
Jamie Madill48ef11b2016-04-27 15:21:52 -04001495 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496 ptr += streamLength;
1497
1498 ASSERT(ptr - streamLength == binary);
1499 }
1500
1501 if (length)
1502 {
1503 *length = streamLength;
1504 }
1505
Jamie Madill7c985f52018-11-29 18:16:17 -05001506 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001507}
1508
Jamie Madillf4a789f2018-10-18 16:56:20 -04001509GLint Program::getBinaryLength(Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001511 ASSERT(mLinkResolved);
Geoff Langb26ab822018-05-29 11:19:00 -04001512 if (!mLinked)
1513 {
1514 return 0;
1515 }
1516
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517 GLint length;
Jamie Madillf4a789f2018-10-18 16:56:20 -04001518 angle::Result result =
1519 saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Jamie Madill7c985f52018-11-29 18:16:17 -05001520 if (result != angle::Result::Continue)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521 {
1522 return 0;
1523 }
1524
1525 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001526}
1527
Geoff Langc5629752015-12-07 16:29:04 -05001528void Program::setBinaryRetrievableHint(bool retrievable)
1529{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001530 ASSERT(mLinkResolved);
Geoff Langc5629752015-12-07 16:29:04 -05001531 // TODO(jmadill) : replace with dirty bits
1532 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001533 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001534}
1535
1536bool Program::getBinaryRetrievableHint() const
1537{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001538 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001539 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001540}
1541
Yunchao He61afff12017-03-14 15:34:03 +08001542void Program::setSeparable(bool separable)
1543{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001544 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001545 // TODO(yunchao) : replace with dirty bits
1546 if (mState.mSeparable != separable)
1547 {
1548 mProgram->setSeparable(separable);
1549 mState.mSeparable = separable;
1550 }
1551}
1552
1553bool Program::isSeparable() const
1554{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001555 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001556 return mState.mSeparable;
1557}
1558
Jamie Madill956ab4d2018-10-10 16:13:03 -04001559void Program::deleteSelf(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001560{
Jamie Madill956ab4d2018-10-10 16:13:03 -04001561 ASSERT(mRefCount == 0 && mDeleteStatus);
1562 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001563}
1564
1565unsigned int Program::getRefCount() const
1566{
1567 return mRefCount;
1568}
1569
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001570int Program::getInfoLogLength() const
1571{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001572 ASSERT(mLinkResolved);
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001573 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001574}
1575
Geoff Lange1a27752015-10-05 13:16:04 -04001576void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001577{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001578 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001579 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001580}
1581
Geoff Lange1a27752015-10-05 13:16:04 -04001582void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001583{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001584 ASSERT(mLinkResolved);
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001585 int total = 0;
1586
Jiawei Shao016105b2018-04-12 16:38:31 +08001587 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001588 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001589 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001590 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001591 shaders[total] = shader->getHandle();
1592 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001593 }
1594 }
1595
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001596 if (count)
1597 {
1598 *count = total;
1599 }
1600}
1601
Geoff Lange1a27752015-10-05 13:16:04 -04001602GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001604 ASSERT(mLinkResolved);
Jamie Madill34ca4f52017-06-13 11:49:39 -04001605 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606}
1607
Jamie Madill63805b42015-08-25 13:17:39 -04001608bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001609{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001610 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001611 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1612 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001613}
1614
jchen10fd7c3b52017-03-21 15:36:03 +08001615void Program::getActiveAttribute(GLuint index,
1616 GLsizei bufsize,
1617 GLsizei *length,
1618 GLint *size,
1619 GLenum *type,
1620 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001621{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001622 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001623 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001624 {
1625 if (bufsize > 0)
1626 {
1627 name[0] = '\0';
1628 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001630 if (length)
1631 {
1632 *length = 0;
1633 }
1634
1635 *type = GL_NONE;
1636 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001637 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001638 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001639
jchen1036e120e2017-03-14 14:53:58 +08001640 ASSERT(index < mState.mAttributes.size());
1641 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001642
1643 if (bufsize > 0)
1644 {
jchen10fd7c3b52017-03-21 15:36:03 +08001645 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001646 }
1647
1648 // Always a single 'type' instance
1649 *size = 1;
1650 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001651}
1652
Geoff Lange1a27752015-10-05 13:16:04 -04001653GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001654{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001655 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001656 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001657 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001658 return 0;
1659 }
1660
jchen1036e120e2017-03-14 14:53:58 +08001661 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001662}
1663
Geoff Lange1a27752015-10-05 13:16:04 -04001664GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001665{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001666 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001667 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001668 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001669 return 0;
1670 }
1671
1672 size_t maxLength = 0;
1673
Jamie Madill48ef11b2016-04-27 15:21:52 -04001674 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001675 {
jchen1036e120e2017-03-14 14:53:58 +08001676 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001677 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678
Jamie Madillc349ec02015-08-21 16:53:12 -04001679 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680}
1681
jchen107ae70d82018-07-06 13:47:01 +08001682const std::vector<sh::Attribute> &Program::getAttributes() const
1683{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001684 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001685 return mState.mAttributes;
1686}
1687
jchen107ae70d82018-07-06 13:47:01 +08001688const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1689{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001690 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001691 return mState.mSamplerBindings;
1692}
1693
jchen107ae70d82018-07-06 13:47:01 +08001694const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1695{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001696 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001697 return mState.mComputeShaderLocalSize;
1698}
1699
1700PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1701{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001702 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001703 return mState.mGeometryShaderInputPrimitiveType;
1704}
1705PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1706{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001707 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001708 return mState.mGeometryShaderOutputPrimitiveType;
1709}
1710GLint Program::getGeometryShaderInvocations() const
1711{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001712 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001713 return mState.mGeometryShaderInvocations;
1714}
1715GLint Program::getGeometryShaderMaxVertices() const
1716{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001717 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001718 return mState.mGeometryShaderMaxVertices;
1719}
1720
jchen1015015f72017-03-16 13:54:21 +08001721GLuint Program::getInputResourceIndex(const GLchar *name) const
1722{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001723 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001724 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001725}
1726
1727GLuint Program::getOutputResourceIndex(const GLchar *name) const
1728{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001729 ASSERT(mLinkResolved);
jchen1015015f72017-03-16 13:54:21 +08001730 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1731}
1732
jchen10fd7c3b52017-03-21 15:36:03 +08001733size_t Program::getOutputResourceCount() const
1734{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001735 ASSERT(mLinkResolved);
jchen10fd7c3b52017-03-21 15:36:03 +08001736 return (mLinked ? mState.mOutputVariables.size() : 0);
1737}
1738
jchen107ae70d82018-07-06 13:47:01 +08001739const std::vector<GLenum> &Program::getOutputVariableTypes() const
1740{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001741 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001742 return mState.mOutputVariableTypes;
1743}
jchen107ae70d82018-07-06 13:47:01 +08001744
jchen10baf5d942017-08-28 20:45:48 +08001745template <typename T>
1746void Program::getResourceName(GLuint index,
1747 const std::vector<T> &resources,
1748 GLsizei bufSize,
1749 GLsizei *length,
1750 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001751{
1752 if (length)
1753 {
1754 *length = 0;
1755 }
1756
1757 if (!mLinked)
1758 {
1759 if (bufSize > 0)
1760 {
1761 name[0] = '\0';
1762 }
1763 return;
1764 }
jchen10baf5d942017-08-28 20:45:48 +08001765 ASSERT(index < resources.size());
1766 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001767
1768 if (bufSize > 0)
1769 {
Olli Etuahod2551232017-10-26 20:03:33 +03001770 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001771 }
1772}
1773
jchen10baf5d942017-08-28 20:45:48 +08001774void Program::getInputResourceName(GLuint index,
1775 GLsizei bufSize,
1776 GLsizei *length,
1777 GLchar *name) const
1778{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001779 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001780 getResourceName(index, mState.mAttributes, bufSize, length, name);
1781}
1782
1783void Program::getOutputResourceName(GLuint index,
1784 GLsizei bufSize,
1785 GLsizei *length,
1786 GLchar *name) const
1787{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001788 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001789 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1790}
1791
1792void Program::getUniformResourceName(GLuint index,
1793 GLsizei bufSize,
1794 GLsizei *length,
1795 GLchar *name) const
1796{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001797 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001798 getResourceName(index, mState.mUniforms, bufSize, length, name);
1799}
1800
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001801void Program::getBufferVariableResourceName(GLuint index,
1802 GLsizei bufSize,
1803 GLsizei *length,
1804 GLchar *name) const
1805{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001806 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001807 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1808}
1809
jchen10880683b2017-04-12 16:21:55 +08001810const sh::Attribute &Program::getInputResource(GLuint index) const
1811{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001812 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001813 ASSERT(index < mState.mAttributes.size());
1814 return mState.mAttributes[index];
1815}
1816
1817const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1818{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001819 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001820 ASSERT(index < mState.mOutputVariables.size());
1821 return mState.mOutputVariables[index];
1822}
1823
jchen107ae70d82018-07-06 13:47:01 +08001824const ProgramBindings &Program::getAttributeBindings() const
1825{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001826 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001827 return mAttributeBindings;
1828}
1829const ProgramBindings &Program::getUniformLocationBindings() const
1830{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001831 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001832 return mUniformLocationBindings;
1833}
1834const ProgramBindings &Program::getFragmentInputBindings() const
1835{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001836 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001837 return mFragmentInputBindings;
1838}
1839
jchen107ae70d82018-07-06 13:47:01 +08001840ComponentTypeMask Program::getDrawBufferTypeMask() const
1841{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001842 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001843 return mState.mDrawBufferTypeMask;
1844}
1845ComponentTypeMask Program::getAttributesTypeMask() const
1846{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001847 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001848 return mState.mAttributesTypeMask;
1849}
1850AttributesMask Program::getAttributesMask() const
1851{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001852 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001853 return mState.mAttributesMask;
1854}
1855
1856const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1857{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001858 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001859 return mState.mTransformFeedbackStrides;
1860}
1861
Geoff Lang7dd2e102014-11-10 15:19:26 -05001862GLint Program::getFragDataLocation(const std::string &name) const
1863{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001864 ASSERT(mLinkResolved);
Olli Etuaho0ca09752018-09-24 11:00:50 +03001865 GLint primaryLocation =
1866 GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
1867 if (primaryLocation != -1)
1868 {
1869 return primaryLocation;
1870 }
1871 return GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name);
1872}
1873
1874GLint Program::getFragDataIndex(const std::string &name) const
1875{
1876 ASSERT(mLinkResolved);
1877 if (GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name) != -1)
1878 {
1879 return 0;
1880 }
1881 if (GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name) != -1)
1882 {
1883 return 1;
1884 }
1885 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001886}
1887
Geoff Lange1a27752015-10-05 13:16:04 -04001888void Program::getActiveUniform(GLuint index,
1889 GLsizei bufsize,
1890 GLsizei *length,
1891 GLint *size,
1892 GLenum *type,
1893 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001894{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001895 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001897 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001899 ASSERT(index < mState.mUniforms.size());
1900 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001901
1902 if (bufsize > 0)
1903 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001905 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001906 }
1907
Olli Etuaho465835d2017-09-26 13:34:10 +03001908 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001909 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001910 }
1911 else
1912 {
1913 if (bufsize > 0)
1914 {
1915 name[0] = '\0';
1916 }
1917
1918 if (length)
1919 {
1920 *length = 0;
1921 }
1922
1923 *size = 0;
1924 *type = GL_NONE;
1925 }
1926}
1927
Geoff Lange1a27752015-10-05 13:16:04 -04001928GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001929{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001930 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001932 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001933 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001934 }
1935 else
1936 {
1937 return 0;
1938 }
1939}
1940
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001941size_t Program::getActiveBufferVariableCount() const
1942{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001943 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001944 return mLinked ? mState.mBufferVariables.size() : 0;
1945}
1946
Geoff Lange1a27752015-10-05 13:16:04 -04001947GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001948{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001949 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001950 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951
1952 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001953 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001954 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001956 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001958 size_t length = uniform.name.length() + 1u;
1959 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 {
1961 length += 3; // Counting in "[0]".
1962 }
1963 maxLength = std::max(length, maxLength);
1964 }
1965 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001966 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967
Jamie Madill62d31cb2015-09-11 13:25:51 -04001968 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001969}
1970
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971bool Program::isValidUniformLocation(GLint location) const
1972{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001973 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04001974 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001975 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001976 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001977}
1978
Jamie Madill62d31cb2015-09-11 13:25:51 -04001979const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001981 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001982 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001983 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984}
1985
Jamie Madillac4e9c32017-01-13 14:07:12 -05001986const VariableLocation &Program::getUniformLocation(GLint location) const
1987{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001988 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05001989 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1990 return mState.mUniformLocations[location];
1991}
1992
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001993const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1994{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001995 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001996 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1997 return mState.mBufferVariables[index];
1998}
1999
Jamie Madill62d31cb2015-09-11 13:25:51 -04002000GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002002 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002003 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004}
2005
Jamie Madill62d31cb2015-09-11 13:25:51 -04002006GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002008 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002009 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010}
2011
2012void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2013{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002014 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002015 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2016 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002017 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018}
2019
2020void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2021{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002022 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002023 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2024 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002025 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026}
2027
2028void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2029{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002030 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002031 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2032 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002033 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034}
2035
2036void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2037{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002038 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002039 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2040 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002041 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042}
2043
Jamie Madille3e680c2018-12-03 17:49:08 -05002044void Program::setUniform1iv(Context *context, GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002046 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002047 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2048 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2049
Jamie Madill81c2e252017-09-09 23:32:46 -04002050 mProgram->setUniform1iv(location, clampedCount, v);
2051
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002052 if (mState.isSamplerUniformIndex(locationInfo.index))
2053 {
Jamie Madille3e680c2018-12-03 17:49:08 -05002054 updateSamplerUniform(context, locationInfo, clampedCount, v);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002055 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056}
2057
2058void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2059{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002060 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002061 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2062 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002063 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064}
2065
2066void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2067{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002068 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002069 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2070 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002071 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002072}
2073
2074void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2075{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002076 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002077 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2078 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002079 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080}
2081
2082void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2083{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002084 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002085 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2086 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002087 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088}
2089
2090void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2091{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002092 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002093 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2094 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002095 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096}
2097
2098void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2099{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002100 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002101 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2102 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002103 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104}
2105
2106void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2107{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002108 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002109 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2110 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002111 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112}
2113
Jamie Madill493f9572018-05-24 19:52:15 -04002114void Program::setUniformMatrix2fv(GLint location,
2115 GLsizei count,
2116 GLboolean transpose,
2117 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002119 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002120 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002121 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122}
2123
Jamie Madill493f9572018-05-24 19:52:15 -04002124void Program::setUniformMatrix3fv(GLint location,
2125 GLsizei count,
2126 GLboolean transpose,
2127 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002128{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002129 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002130 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002131 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132}
2133
Jamie Madill493f9572018-05-24 19:52:15 -04002134void Program::setUniformMatrix4fv(GLint location,
2135 GLsizei count,
2136 GLboolean transpose,
2137 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002138{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002139 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002140 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002141 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142}
2143
Jamie Madill493f9572018-05-24 19:52:15 -04002144void Program::setUniformMatrix2x3fv(GLint location,
2145 GLsizei count,
2146 GLboolean transpose,
2147 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002149 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002150 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002151 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152}
2153
Jamie Madill493f9572018-05-24 19:52:15 -04002154void Program::setUniformMatrix2x4fv(GLint location,
2155 GLsizei count,
2156 GLboolean transpose,
2157 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002159 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002160 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002161 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162}
2163
Jamie Madill493f9572018-05-24 19:52:15 -04002164void Program::setUniformMatrix3x2fv(GLint location,
2165 GLsizei count,
2166 GLboolean transpose,
2167 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002169 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002170 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002171 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172}
2173
Jamie Madill493f9572018-05-24 19:52:15 -04002174void Program::setUniformMatrix3x4fv(GLint location,
2175 GLsizei count,
2176 GLboolean transpose,
2177 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002179 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002180 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002181 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182}
2183
Jamie Madill493f9572018-05-24 19:52:15 -04002184void Program::setUniformMatrix4x2fv(GLint location,
2185 GLsizei count,
2186 GLboolean transpose,
2187 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002189 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002190 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002191 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192}
2193
Jamie Madill493f9572018-05-24 19:52:15 -04002194void Program::setUniformMatrix4x3fv(GLint location,
2195 GLsizei count,
2196 GLboolean transpose,
2197 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002199 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002200 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002201 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202}
2203
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002204GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2205{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002206 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002207 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2208 const std::vector<GLuint> &boundTextureUnits =
2209 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2210 return boundTextureUnits[uniformLocation.arrayIndex];
2211}
2212
Jamie Madill54164b02017-08-28 15:17:37 -04002213void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002215 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002216 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2217 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002218
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002219 if (uniform.isSampler())
2220 {
2221 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2222 return;
2223 }
2224
2225 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002226 if (nativeType == GL_FLOAT)
2227 {
2228 mProgram->getUniformfv(context, location, v);
2229 }
2230 else
2231 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002232 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002233 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234}
2235
Jamie Madill54164b02017-08-28 15:17:37 -04002236void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002237{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002238 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002239 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2240 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002241
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002242 if (uniform.isSampler())
2243 {
2244 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2245 return;
2246 }
2247
2248 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002249 if (nativeType == GL_INT || nativeType == GL_BOOL)
2250 {
2251 mProgram->getUniformiv(context, location, v);
2252 }
2253 else
2254 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002255 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002256 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002257}
2258
Jamie Madill54164b02017-08-28 15:17:37 -04002259void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002261 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002262 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2263 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002264
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002265 if (uniform.isSampler())
2266 {
2267 *v = getSamplerUniformBinding(uniformLocation);
2268 return;
2269 }
2270
2271 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002272 if (nativeType == GL_UNSIGNED_INT)
2273 {
2274 mProgram->getUniformuiv(context, location, v);
2275 }
2276 else
2277 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002278 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002279 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280}
2281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282void Program::flagForDeletion()
2283{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002284 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 mDeleteStatus = true;
2286}
2287
2288bool Program::isFlaggedForDeletion() const
2289{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002290 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 return mDeleteStatus;
2292}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002293
Brandon Jones43a53e22014-08-28 16:23:22 -07002294void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002295{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002296 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002297 mInfoLog.reset();
2298
Geoff Lang7dd2e102014-11-10 15:19:26 -05002299 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002300 {
Geoff Lang92019432017-11-20 13:09:34 -05002301 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002302 }
2303 else
2304 {
Jamie Madillf6113162015-05-07 11:49:21 -04002305 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002306 }
2307}
2308
Jamie Madillcc73f242018-08-01 11:34:48 -04002309bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002310{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002311 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002312
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002313 // if any two active samplers in a program are of different types, but refer to the same
2314 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2315 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002316 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002317 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002318 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002319 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002320 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002321 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002322 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2323 "image unit ("
2324 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002325 }
2326
Jamie Madill7e4eff12018-08-08 15:49:26 -04002327 mCachedValidateSamplersResult = false;
2328 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002329 }
2330 }
2331
2332 mCachedValidateSamplersResult = true;
2333 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334}
2335
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002336bool Program::isValidated() const
2337{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002338 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002339 return mValidated;
2340}
2341
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002342void Program::getActiveUniformBlockName(const GLuint blockIndex,
2343 GLsizei bufSize,
2344 GLsizei *length,
2345 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002346{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002347 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002348 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2349}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002351void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2352 GLsizei bufSize,
2353 GLsizei *length,
2354 GLchar *blockName) const
2355{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002356 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002357 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002358}
2359
Qin Jiajia9bf55522018-01-29 13:56:23 +08002360template <typename T>
2361GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002362{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363 int maxLength = 0;
2364
2365 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002366 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002367 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002369 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002371 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002372 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373 }
2374 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002375 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002376
2377 return maxLength;
2378}
2379
Qin Jiajia9bf55522018-01-29 13:56:23 +08002380GLint Program::getActiveUniformBlockMaxNameLength() const
2381{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002382 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002383 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2384}
2385
2386GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2387{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002388 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002389 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2390}
2391
Geoff Lange1a27752015-10-05 13:16:04 -04002392GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002394 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002395 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2396}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002397
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002398GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2399{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002400 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002401 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002402}
2403
Jiajia Qin729b2c62017-08-14 09:36:11 +08002404const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002405{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002406 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002407 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2408 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002409}
2410
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002411const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2412{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002413 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002414 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2415 return mState.mShaderStorageBlocks[index];
2416}
2417
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002418void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2419{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002420 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002421 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002422 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002423 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002424}
2425
2426GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2427{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002428 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002429 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002430}
2431
Jiajia Qin729b2c62017-08-14 09:36:11 +08002432GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2433{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002434 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002435 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2436}
2437
Jamie Madill493f9572018-05-24 19:52:15 -04002438void Program::setTransformFeedbackVaryings(GLsizei count,
2439 const GLchar *const *varyings,
2440 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002441{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002442 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002443 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002444 for (GLsizei i = 0; i < count; i++)
2445 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002446 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002447 }
2448
Jamie Madill48ef11b2016-04-27 15:21:52 -04002449 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002450}
2451
Jamie Madill493f9572018-05-24 19:52:15 -04002452void Program::getTransformFeedbackVarying(GLuint index,
2453 GLsizei bufSize,
2454 GLsizei *length,
2455 GLsizei *size,
2456 GLenum *type,
2457 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002458{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002459 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002460 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002461 {
jchen10a9042d32017-03-17 08:50:45 +08002462 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2463 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2464 std::string varName = var.nameWithArrayIndex();
2465 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002466 if (length)
2467 {
2468 *length = lastNameIdx;
2469 }
2470 if (size)
2471 {
jchen10a9042d32017-03-17 08:50:45 +08002472 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002473 }
2474 if (type)
2475 {
jchen10a9042d32017-03-17 08:50:45 +08002476 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002477 }
2478 if (name)
2479 {
jchen10a9042d32017-03-17 08:50:45 +08002480 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002481 name[lastNameIdx] = '\0';
2482 }
2483 }
2484}
2485
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002486GLsizei Program::getTransformFeedbackVaryingCount() const
2487{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002488 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002489 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002490 {
jchen10a9042d32017-03-17 08:50:45 +08002491 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002492 }
2493 else
2494 {
2495 return 0;
2496 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002497}
2498
2499GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2500{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002501 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002502 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002503 {
2504 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002505 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002506 {
jchen10a9042d32017-03-17 08:50:45 +08002507 maxSize =
2508 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002509 }
2510
2511 return maxSize;
2512 }
2513 else
2514 {
2515 return 0;
2516 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002517}
2518
2519GLenum Program::getTransformFeedbackBufferMode() const
2520{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002521 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002522 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002523}
2524
jchen103fd614d2018-08-13 12:21:58 +08002525bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002526{
Jiawei Shao016105b2018-04-12 16:38:31 +08002527 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2528 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2529 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2530 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002531
Jamie Madill493f9572018-05-24 19:52:15 -04002532 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002533 bool isGraphicsShaderAttached =
2534 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002535 // Check whether we both have a compute and non-compute shaders attached.
2536 // If there are of both types attached, then linking should fail.
2537 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2538 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2539 {
2540 infoLog << "Both compute and graphics shaders are attached to the same program.";
2541 return false;
2542 }
2543
2544 if (computeShader)
2545 {
jchen103fd614d2018-08-13 12:21:58 +08002546 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002547 {
2548 infoLog << "Attached compute shader is not compiled.";
2549 return false;
2550 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002551 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002552
jchen103fd614d2018-08-13 12:21:58 +08002553 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002554
2555 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2556 // If the work group size is not specified, a link time error should occur.
2557 if (!mState.mComputeShaderLocalSize.isDeclared())
2558 {
2559 infoLog << "Work group size is not specified.";
2560 return false;
2561 }
2562 }
2563 else
2564 {
jchen103fd614d2018-08-13 12:21:58 +08002565 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002566 {
2567 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2568 return false;
2569 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002570 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002571
jchen103fd614d2018-08-13 12:21:58 +08002572 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002573 {
2574 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2575 return false;
2576 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002577 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002578
jchen103fd614d2018-08-13 12:21:58 +08002579 int vertexShaderVersion = vertexShader->getShaderVersion();
2580 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002581 {
2582 infoLog << "Fragment shader version does not match vertex shader version.";
2583 return false;
2584 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002585
2586 if (geometryShader)
2587 {
2588 // [GL_EXT_geometry_shader] Chapter 7
2589 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2590 // Language Specification, as well as any of the following reasons:
2591 // * One or more of the shader objects attached to <program> are not compiled
2592 // successfully.
2593 // * The shaders do not use the same shader language version.
2594 // * <program> contains objects to form a geometry shader, and
2595 // - <program> is not separable and contains no objects to form a vertex shader; or
2596 // - the input primitive type, output primitive type, or maximum output vertex count
2597 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002598 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002599 {
2600 infoLog << "The attached geometry shader isn't compiled.";
2601 return false;
2602 }
2603
jchen103fd614d2018-08-13 12:21:58 +08002604 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002605 {
2606 mInfoLog << "Geometry shader version does not match vertex shader version.";
2607 return false;
2608 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002609 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002610
Jamie Madill493f9572018-05-24 19:52:15 -04002611 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002612 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002613 if (!inputPrimitive.valid())
2614 {
2615 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2616 return false;
2617 }
2618
Jamie Madill493f9572018-05-24 19:52:15 -04002619 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002620 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002621 if (!outputPrimitive.valid())
2622 {
2623 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2624 return false;
2625 }
2626
jchen103fd614d2018-08-13 12:21:58 +08002627 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002628 if (!maxVertices.valid())
2629 {
2630 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2631 return false;
2632 }
2633
2634 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2635 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2636 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002637 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002638 }
Jiawei Shao73618602017-12-20 15:47:15 +08002639 }
2640
2641 return true;
2642}
2643
jchen10910a3da2017-11-15 09:40:11 +08002644GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2645{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002646 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002647 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2648 {
2649 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2650 if (tf.nameWithArrayIndex() == name)
2651 {
2652 return tfIndex;
2653 }
2654 }
2655 return GL_INVALID_INDEX;
2656}
2657
2658const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2659{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002660 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002661 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2662 return mState.mLinkedTransformFeedbackVaryings[index];
2663}
2664
Austin Eng1bf18ce2018-10-19 15:34:02 -07002665bool Program::hasDrawIDUniform() const
2666{
2667 ASSERT(mLinkResolved);
2668 return mState.mDrawIDLocation >= 0;
2669}
2670
2671void Program::setDrawIDUniform(GLint drawid)
2672{
2673 ASSERT(mLinkResolved);
2674 ASSERT(mState.mDrawIDLocation >= 0);
2675 mProgram->setUniform1iv(mState.mDrawIDLocation, 1, &drawid);
2676}
2677
jchen103fd614d2018-08-13 12:21:58 +08002678bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002679{
Jiawei Shao016105b2018-04-12 16:38:31 +08002680 Shader *previousShader = nullptr;
2681 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002682 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002683 Shader *currentShader = mState.mAttachedShaders[shaderType];
2684 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002685 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002686 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002687 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002688
2689 if (previousShader)
2690 {
jchen103fd614d2018-08-13 12:21:58 +08002691 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002692 {
2693 return false;
2694 }
2695 }
2696 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002697 }
2698
jchen103fd614d2018-08-13 12:21:58 +08002699 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002700 {
2701 return false;
2702 }
2703
jchen103fd614d2018-08-13 12:21:58 +08002704 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002705 {
2706 return false;
2707 }
2708
2709 return true;
2710}
2711
2712// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2713// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002714bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002715 gl::Shader *consumingShader,
2716 gl::InfoLog &infoLog) const
2717{
jchen103fd614d2018-08-13 12:21:58 +08002718 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002719
jchen103fd614d2018-08-13 12:21:58 +08002720 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2721 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002722
Jiawei Shao385b3e02018-03-21 09:43:28 +08002723 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002724
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002725 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002727 bool matched = false;
2728
2729 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002730 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002731 {
2732 continue;
2733 }
2734
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002735 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002736 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002737 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002738 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002739 ASSERT(!output.isBuiltIn());
2740
2741 std::string mismatchedStructFieldName;
2742 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002743 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002744 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002745 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002746 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002747 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2748 mismatchedStructFieldName, generatingShader->getType(),
2749 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002750 return false;
2751 }
2752
Geoff Lang7dd2e102014-11-10 15:19:26 -05002753 matched = true;
2754 break;
2755 }
2756 }
2757
Olli Etuaho107c7242018-03-20 15:45:35 +02002758 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2759 // whether the input is statically used - a statically used input should fail this test even
2760 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002761 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002762 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002763 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2764 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2765 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002766 return false;
2767 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002768 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002769
Jiawei Shaod063aff2018-02-22 10:19:09 +08002770 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002771
Jiawei Shaod063aff2018-02-22 10:19:09 +08002772 return true;
2773}
2774
jchen103fd614d2018-08-13 12:21:58 +08002775bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002776{
Jiawei Shao016105b2018-04-12 16:38:31 +08002777 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002778
2779 std::map<GLuint, std::string> staticFragmentInputLocations;
2780
2781 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002782 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002783 for (const sh::Varying &input : fragmentInputVaryings)
2784 {
2785 if (input.isBuiltIn() || !input.staticUse)
2786 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002787 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002788 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002789
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002790 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002791 if (inputBinding == -1)
2792 continue;
2793
2794 const auto it = staticFragmentInputLocations.find(inputBinding);
2795 if (it == std::end(staticFragmentInputLocations))
2796 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002797 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002798 }
2799 else
2800 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002801 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002802 << it->second;
2803 return false;
2804 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002805 }
2806
2807 return true;
2808}
2809
jchen103fd614d2018-08-13 12:21:58 +08002810bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002811 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002812 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002813 GLuint *combinedImageUniformsCount,
2814 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002815{
Olli Etuahob78707c2017-03-09 15:03:11 +00002816 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002817 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818 {
2819 return false;
2820 }
2821
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002822 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002823
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002824 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002825
jchen10eaef1e52017-06-13 10:44:11 +08002826 if (!linkAtomicCounterBuffers())
2827 {
2828 return false;
2829 }
2830
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002831 return true;
2832}
2833
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002834void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002835{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002836 ASSERT(combinedImageUniforms);
2837
Jamie Madill982f6e02017-06-07 14:33:04 -04002838 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2839 unsigned int low = high;
2840
jchen10eaef1e52017-06-13 10:44:11 +08002841 for (auto counterIter = mState.mUniforms.rbegin();
2842 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2843 {
2844 --low;
2845 }
2846
2847 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2848
2849 high = low;
2850
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002851 for (auto imageIter = mState.mUniforms.rbegin();
2852 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2853 {
2854 --low;
2855 }
2856
2857 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002858 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002859 // If uniform is a image type, insert it into the mImageBindings array.
2860 for (unsigned int imageIndex : mState.mImageUniformRange)
2861 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002862 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2863 // cannot load values into a uniform defined as an image. if declare without a
2864 // binding qualifier, any uniform image variable (include all elements of
2865 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002866 auto &imageUniform = mState.mUniforms[imageIndex];
2867 if (imageUniform.binding == -1)
2868 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002869 mState.mImageBindings.emplace_back(
2870 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002871 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002872 else
2873 {
2874 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002875 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002876 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002877
2878 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2879 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002880 }
2881
2882 high = low;
2883
2884 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002885 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002886 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002887 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002888 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002889
2890 mState.mSamplerUniformRange = RangeUI(low, high);
2891
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002892 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002893 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002894 {
2895 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002896 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002897 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2898 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002899 }
2900}
2901
jchen10eaef1e52017-06-13 10:44:11 +08002902bool Program::linkAtomicCounterBuffers()
2903{
2904 for (unsigned int index : mState.mAtomicCounterUniformRange)
2905 {
Jamie Madill493f9572018-05-24 19:52:15 -04002906 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002907 uniform.blockInfo.offset = uniform.offset;
2908 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2909 uniform.blockInfo.matrixStride = 0;
2910 uniform.blockInfo.isRowMajorMatrix = false;
2911
Jamie Madill493f9572018-05-24 19:52:15 -04002912 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002913 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2914 ++bufferIndex)
2915 {
2916 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2917 if (buffer.binding == uniform.binding)
2918 {
2919 buffer.memberIndexes.push_back(index);
2920 uniform.bufferIndex = bufferIndex;
2921 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002922 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002923 break;
2924 }
2925 }
2926 if (!found)
2927 {
2928 AtomicCounterBuffer atomicCounterBuffer;
2929 atomicCounterBuffer.binding = uniform.binding;
2930 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002931 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002932 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2933 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2934 }
2935 }
2936 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002937 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002938
2939 return true;
2940}
2941
Jamie Madilleb979bf2016-11-15 12:28:46 -05002942// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002943bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002944{
Jamie Madillb980c562018-11-27 11:34:27 -05002945 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002946
jchen103fd614d2018-08-13 12:21:58 +08002947 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002948
Geoff Lang7dd2e102014-11-10 15:19:26 -05002949 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002950 if (shaderVersion >= 300)
2951 {
2952 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2953 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002954 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002955 }
2956 else
2957 {
2958 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002959 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002960 }
jchen103fd614d2018-08-13 12:21:58 +08002961 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002962
2963 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002964 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002965 {
Jamie Madillf6113162015-05-07 11:49:21 -04002966 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002967 return false;
2968 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002969
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002970 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002971
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002972 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002973 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002974 {
Olli Etuahod2551232017-10-26 20:03:33 +03002975 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2976 // structures, so we don't need to worry about adjusting their names or generating entries
2977 // for each member/element (unlike uniforms for example).
2978 ASSERT(!attribute.isArray() && !attribute.isStruct());
2979
Jamie Madilleb979bf2016-11-15 12:28:46 -05002980 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002981 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002982 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002983 attribute.location = bindingLocation;
2984 }
2985
2986 if (attribute.location != -1)
2987 {
2988 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002989 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002990
Jamie Madill63805b42015-08-25 13:17:39 -04002991 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002992 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002993 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
2994 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002995
2996 return false;
2997 }
2998
Jamie Madill63805b42015-08-25 13:17:39 -04002999 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003000 {
Jamie Madill63805b42015-08-25 13:17:39 -04003001 const int regLocation = attribute.location + reg;
3002 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003003
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003004 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3005 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3006 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003007 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003008 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003009 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003010 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003011 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003012 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003013 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003014 return false;
3015 }
3016 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003017 else
3018 {
Jamie Madill63805b42015-08-25 13:17:39 -04003019 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003020 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003021
Jamie Madill63805b42015-08-25 13:17:39 -04003022 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003023 }
3024 }
3025 }
3026
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003027 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003028 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003029 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003030 // Not set by glBindAttribLocation or by location layout qualifier
3031 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003032 {
Jamie Madill63805b42015-08-25 13:17:39 -04003033 int regs = VariableRegisterCount(attribute.type);
3034 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003035
Jamie Madill63805b42015-08-25 13:17:39 -04003036 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003037 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003038 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003039 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003040 }
3041
Jamie Madillc349ec02015-08-21 16:53:12 -04003042 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003043 }
3044 }
3045
Brandon Jonesc405ae72017-12-06 14:15:03 -08003046 ASSERT(mState.mAttributesTypeMask.none());
3047 ASSERT(mState.mAttributesMask.none());
3048
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003049 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3050 // shader versions we're only processing active attributes to begin with.
3051 if (shaderVersion >= 300)
3052 {
3053 for (auto attributeIter = mState.mAttributes.begin();
3054 attributeIter != mState.mAttributes.end();)
3055 {
3056 if (attributeIter->active)
3057 {
3058 ++attributeIter;
3059 }
3060 else
3061 {
3062 attributeIter = mState.mAttributes.erase(attributeIter);
3063 }
3064 }
3065 }
3066
Jamie Madill48ef11b2016-04-27 15:21:52 -04003067 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003068 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003069 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003070 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003071 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003072
Jamie Madillbd159f02017-10-09 19:39:06 -04003073 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003074 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003075 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3076 mState.mActiveAttribLocationsMask.set(location);
3077 mState.mMaxActiveAttribLocation =
3078 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003079
3080 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3081 if (!attribute.isBuiltIn())
3082 {
3083 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3084 location);
3085 mState.mAttributesMask.set(location);
3086 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003087 }
3088 }
3089
Geoff Lang7dd2e102014-11-10 15:19:26 -05003090 return true;
3091}
3092
jchen103fd614d2018-08-13 12:21:58 +08003093bool Program::linkInterfaceBlocks(const Caps &caps,
3094 const Version &version,
3095 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003096 InfoLog &infoLog,
3097 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003098{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003099 ASSERT(combinedShaderStorageBlocksCount);
3100
Jiawei Shao54aafe52018-04-27 14:54:57 +08003101 GLuint combinedUniformBlocksCount = 0u;
3102 GLuint numShadersHasUniformBlocks = 0u;
3103 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3104 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003105 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003106 Shader *shader = mState.mAttachedShaders[shaderType];
3107 if (!shader)
3108 {
3109 continue;
3110 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003111
jchen103fd614d2018-08-13 12:21:58 +08003112 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003113 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003114 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003115 if (!ValidateInterfaceBlocksCount(
3116 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3117 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3118 {
3119 return false;
3120 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003121
Jiawei Shao54aafe52018-04-27 14:54:57 +08003122 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3123 ++numShadersHasUniformBlocks;
3124 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003125 }
3126
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003127 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3128 {
3129 infoLog << "The sum of the number of active uniform blocks exceeds "
3130 "MAX_COMBINED_UNIFORM_BLOCKS ("
3131 << caps.maxCombinedUniformBlocks << ").";
3132 return false;
3133 }
3134
Jiawei Shao54aafe52018-04-27 14:54:57 +08003135 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3136 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003137 {
3138 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003139 }
Jamie Madille473dee2015-08-18 14:49:01 -04003140
jchen103fd614d2018-08-13 12:21:58 +08003141 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003142 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003143 *combinedShaderStorageBlocksCount = 0u;
3144 GLuint numShadersHasShaderStorageBlocks = 0u;
3145 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3146 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003147 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003148 Shader *shader = mState.mAttachedShaders[shaderType];
3149 if (!shader)
3150 {
3151 continue;
3152 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003153
jchen103fd614d2018-08-13 12:21:58 +08003154 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003155 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003156 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003157 if (!ValidateInterfaceBlocksCount(
3158 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3159 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3160 {
3161 return false;
3162 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003163
Jiawei Shao54aafe52018-04-27 14:54:57 +08003164 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3165 ++numShadersHasShaderStorageBlocks;
3166 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003167 }
3168
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003169 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003170 {
3171 infoLog << "The sum of the number of active shader storage blocks exceeds "
3172 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3173 << caps.maxCombinedShaderStorageBlocks << ").";
3174 return false;
3175 }
3176
Jiawei Shao54aafe52018-04-27 14:54:57 +08003177 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3178 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003179 {
3180 return false;
3181 }
3182 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003183
Geoff Lang7dd2e102014-11-10 15:19:26 -05003184 return true;
3185}
3186
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003187LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3188 const sh::ShaderVariable &variable2,
3189 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003190 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003191 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003192{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003193 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003194 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003195 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003196 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003197 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003198 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003199 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003200 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003201 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003202 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003203 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003204 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003205 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003206 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003207 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003208 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003209
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003210 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003211 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003212 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003213 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003214 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003215 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3216 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003217 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3218 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003219
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003220 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003221 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003222 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003223 }
3224
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003225 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003226 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003227 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003228 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003229 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3230 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003231 }
3232 }
3233
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003234 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003235}
3236
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003237LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3238 const sh::Varying &inputVarying,
3239 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003240 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003241 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003242{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003243 if (validateGeometryShaderInputVarying)
3244 {
3245 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3246 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3247 // or outputs.
3248 ASSERT(inputVarying.arraySizes.size() == 1u);
3249
3250 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3251 // varying cannot match a geometry shader input varying.
3252 // [GL_EXT_geometry_shader] Section 7.4.1:
3253 // Geometry shader per-vertex input variables and blocks are required to be declared as
3254 // arrays, with each element representing input or output values for a single vertex of a
3255 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3256 // are treated as though they were not declared as arrays.
3257 if (outputVarying.isArray())
3258 {
3259 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3260 }
3261 }
3262
3263 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3264 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003265 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003266 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3267 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003268 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003269 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003270 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003271 }
3272
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003273 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003274 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003275 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003276 }
3277
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003278 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003279 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003280 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003281 }
3282
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003283 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003284}
3285
jchen103fd614d2018-08-13 12:21:58 +08003286bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003287{
Jiawei Shao016105b2018-04-12 16:38:31 +08003288 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3289 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003290 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3291 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3292 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003293
3294 if (shaderVersion != 100)
3295 {
3296 // Only ESSL 1.0 has restrictions on matching input and output invariance
3297 return true;
3298 }
3299
3300 bool glPositionIsInvariant = false;
3301 bool glPointSizeIsInvariant = false;
3302 bool glFragCoordIsInvariant = false;
3303 bool glPointCoordIsInvariant = false;
3304
3305 for (const sh::Varying &varying : vertexVaryings)
3306 {
3307 if (!varying.isBuiltIn())
3308 {
3309 continue;
3310 }
3311 if (varying.name.compare("gl_Position") == 0)
3312 {
3313 glPositionIsInvariant = varying.isInvariant;
3314 }
3315 else if (varying.name.compare("gl_PointSize") == 0)
3316 {
3317 glPointSizeIsInvariant = varying.isInvariant;
3318 }
3319 }
3320
3321 for (const sh::Varying &varying : fragmentVaryings)
3322 {
3323 if (!varying.isBuiltIn())
3324 {
3325 continue;
3326 }
3327 if (varying.name.compare("gl_FragCoord") == 0)
3328 {
3329 glFragCoordIsInvariant = varying.isInvariant;
3330 }
3331 else if (varying.name.compare("gl_PointCoord") == 0)
3332 {
3333 glPointCoordIsInvariant = varying.isInvariant;
3334 }
3335 }
3336
3337 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3338 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3339 // Not requiring invariance to match is supported by:
3340 // dEQP, WebGL CTS, Nexus 5X GLES
3341 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3342 {
3343 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3344 "declared invariant.";
3345 return false;
3346 }
3347 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3348 {
3349 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3350 "declared invariant.";
3351 return false;
3352 }
3353
3354 return true;
3355}
3356
jchen103fd614d2018-08-13 12:21:58 +08003357bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003358 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003359 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003360 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003361{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003362
jchen108225e732017-11-14 16:29:03 +08003363 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003364 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003365 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003366 {
jchen103fd614d2018-08-13 12:21:58 +08003367 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003368 {
Geoff Lang1a683462015-09-29 15:09:59 -04003369 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003370 return false;
3371 }
jchen103fd614d2018-08-13 12:21:58 +08003372 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003373 {
3374 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3375 {
3376 infoLog << "Two transform feedback varyings include the same array element ("
3377 << tfVaryingName << ").";
3378 return false;
3379 }
3380 }
3381 else
3382 {
3383 if (uniqueNames.count(tfVaryingName) > 0)
3384 {
3385 infoLog << "Two transform feedback varyings specify the same output variable ("
3386 << tfVaryingName << ").";
3387 return false;
3388 }
3389 }
3390 uniqueNames.insert(tfVaryingName);
3391 }
3392
3393 // Validate against program varyings.
3394 size_t totalComponents = 0;
3395 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3396 {
3397 std::vector<unsigned int> subscripts;
3398 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3399
3400 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3401 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003402 {
3403 infoLog << "Transform feedback varying " << tfVaryingName
3404 << " does not exist in the vertex shader.";
3405 return false;
3406 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003407
jchen108225e732017-11-14 16:29:03 +08003408 // Validate the matching variable.
3409 if (var->isStruct())
3410 {
3411 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3412 return false;
3413 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003414
jchen108225e732017-11-14 16:29:03 +08003415 size_t elementCount = 0;
3416 size_t componentCount = 0;
3417
3418 if (var->isArray())
3419 {
jchen103fd614d2018-08-13 12:21:58 +08003420 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003421 {
3422 infoLog << "Capture of arrays is undefined and not supported.";
3423 return false;
3424 }
3425
3426 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3427 ASSERT(!var->isArrayOfArrays());
3428
3429 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3430 {
3431 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3432 return false;
3433 }
3434 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3435 }
3436 else
3437 {
3438 if (!subscripts.empty())
3439 {
3440 infoLog << "Varying '" << baseName
3441 << "' is not an array to be captured by element.";
3442 return false;
3443 }
3444 elementCount = 1;
3445 }
3446
3447 // TODO(jmadill): Investigate implementation limits on D3D11
3448 componentCount = VariableComponentCount(var->type) * elementCount;
3449 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3450 componentCount > caps.maxTransformFeedbackSeparateComponents)
3451 {
3452 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3453 << componentCount << ") exceed the maximum separate components ("
3454 << caps.maxTransformFeedbackSeparateComponents << ").";
3455 return false;
3456 }
3457
3458 totalComponents += componentCount;
3459 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3460 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3461 {
3462 infoLog << "Transform feedback varying total components (" << totalComponents
3463 << ") exceed the maximum interleaved components ("
3464 << caps.maxTransformFeedbackInterleavedComponents << ").";
3465 return false;
3466 }
3467 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003468 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003469}
3470
jchen103fd614d2018-08-13 12:21:58 +08003471bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003472{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003473 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003474 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003475
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003476 for (const auto &attrib : attributes)
3477 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003478 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003479 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003480 Shader *shader = mState.mAttachedShaders[shaderType];
3481 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003482 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003483 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003484 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003485
jchen103fd614d2018-08-13 12:21:58 +08003486 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003487 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003488 {
3489 if (uniform.name == attrib.name)
3490 {
3491 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3492 return false;
3493 }
3494 }
3495 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003496 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003497
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003498 return true;
3499}
3500
Jamie Madill3c1da042017-11-27 18:33:40 -05003501void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003502{
3503 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003504 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003505 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003506 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003507 std::vector<unsigned int> subscripts;
3508 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003509 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003510 if (!subscripts.empty())
3511 {
3512 subscript = subscripts.back();
3513 }
Jamie Madill192745a2016-12-22 15:58:21 -05003514 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003515 {
Jamie Madill192745a2016-12-22 15:58:21 -05003516 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003517 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003518 {
jchen10a9042d32017-03-17 08:50:45 +08003519 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3520 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003521 break;
3522 }
jchen108225e732017-11-14 16:29:03 +08003523 else if (varying->isStruct())
3524 {
3525 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3526 if (field != nullptr)
3527 {
3528 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3529 break;
3530 }
3531 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003532 }
3533 }
James Darpinian30b604d2018-03-12 17:26:57 -07003534 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003535}
3536
jchen103fd614d2018-08-13 12:21:58 +08003537ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003538{
Jamie Madill3c1da042017-11-27 18:33:40 -05003539 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003540
Jiawei Shao016105b2018-04-12 16:38:31 +08003541 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003542 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003543 {
Jamie Madill192745a2016-12-22 15:58:21 -05003544 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003545 }
3546
Jiawei Shao016105b2018-04-12 16:38:31 +08003547 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003548 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003549 {
Jamie Madill192745a2016-12-22 15:58:21 -05003550 merged[varying.name].fragment = &varying;
3551 }
3552
3553 return merged;
3554}
3555
Olli Etuaho0ca09752018-09-24 11:00:50 +03003556bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3557{
3558 return a.getArraySizeProduct() > b.getArraySizeProduct();
3559}
3560
3561int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3562{
3563 if (outputVariable.location != -1)
3564 {
3565 return outputVariable.location;
3566 }
3567 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3568 if (apiLocation != -1)
3569 {
3570 return apiLocation;
3571 }
3572 return -1;
3573}
3574
3575bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3576{
3577 if (outputVariable.index != -1)
3578 {
3579 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3580 return (outputVariable.index == 1);
3581 }
3582 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3583 if (apiIndex != -1)
3584 {
3585 // Index layout qualifier from the shader takes precedence, so the index from the API is
3586 // checked only if the index was not set in the shader. This is not specified in the EXT
3587 // spec, but is specified in desktop OpenGL specs.
3588 return (apiIndex == 1);
3589 }
3590 // EXT_blend_func_extended: Outputs get index 0 by default.
3591 return false;
3592}
3593
jchen103fd614d2018-08-13 12:21:58 +08003594bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003595 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003596 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003597 GLuint combinedImageUniformsCount,
3598 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003599{
Jiawei Shao016105b2018-04-12 16:38:31 +08003600 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003601 ASSERT(fragmentShader != nullptr);
3602
Geoff Lange0cff192017-05-30 13:04:56 -04003603 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003604 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003605 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003606
jchen103fd614d2018-08-13 12:21:58 +08003607 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003608 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003609 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003610 {
3611 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3612 outputVariable.name != "gl_FragData")
3613 {
3614 continue;
3615 }
3616
3617 unsigned int baseLocation =
3618 (outputVariable.location == -1 ? 0u
3619 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003620
3621 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3622 // structures, so we may use getBasicTypeElementCount().
3623 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3624 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003625 {
3626 const unsigned int location = baseLocation + elementIndex;
3627 if (location >= mState.mOutputVariableTypes.size())
3628 {
3629 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3630 }
Corentin Walleze7557742017-06-01 13:09:57 -04003631 ASSERT(location < mState.mActiveOutputVariables.size());
3632 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003633 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003634 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003635 }
3636 }
3637
jchen103fd614d2018-08-13 12:21:58 +08003638 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003639 {
3640 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3641 // A link error will be generated if the sum of the number of active image uniforms used in
3642 // all shaders, the number of active shader storage blocks, and the number of active
3643 // fragment shader outputs exceeds the implementation-dependent value of
3644 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3645 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3646 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003647 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003648 {
3649 mInfoLog
3650 << "The sum of the number of active image uniforms, active shader storage blocks "
3651 "and active fragment shader outputs exceeds "
3652 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003653 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003654 return false;
3655 }
3656 }
3657
Jamie Madill80a6fc02015-08-21 16:53:16 -04003658 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003659 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003660 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003661
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003662 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003663 // TODO(jmadill): any caps validation here?
3664
Olli Etuaho0ca09752018-09-24 11:00:50 +03003665 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3666 {
3667 if (outputVariable.isArray())
3668 {
3669 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3670 // Resources and including [0] at the end of array variable names.
3671 outputVariable.name += "[0]";
3672 outputVariable.mappedName += "[0]";
3673 }
3674 }
3675
3676 bool hasSecondaryOutputs = false;
3677
3678 // Reserve locations for output variables whose location is fixed in the shader or through the
3679 // API.
jchen1015015f72017-03-16 13:54:21 +08003680 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003681 outputVariableIndex++)
3682 {
jchen1015015f72017-03-16 13:54:21 +08003683 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003684
3685 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3686 if (outputVariable.isBuiltIn())
3687 continue;
3688
Olli Etuaho0ca09752018-09-24 11:00:50 +03003689 int baseLocation = getOutputLocationForLink(outputVariable);
3690 if (baseLocation == -1)
3691 {
3692 // Here we're only reserving locations for variables whose location is fixed.
3693 continue;
3694 }
3695
3696 auto *outputLocations = &mState.mOutputLocations;
3697 if (isOutputSecondaryForLink(outputVariable))
3698 {
3699 outputLocations = &mState.mSecondaryOutputLocations;
3700 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3701 // an output has an index specified it will always also have the location specified.
3702 hasSecondaryOutputs = true;
3703 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003704
Olli Etuaho465835d2017-09-26 13:34:10 +03003705 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3706 // structures, so we may use getBasicTypeElementCount().
Jamie Madillb980c562018-11-27 11:34:27 -05003707 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003708 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3709 if (outputLocationsNeeded > outputLocations->size())
3710 {
3711 outputLocations->resize(outputLocationsNeeded);
3712 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003713 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003714 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003715 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3716 ASSERT(location < outputLocations->size());
3717 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003718 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003719 mInfoLog << "Location of variable " << outputVariable.name
3720 << " conflicts with another variable.";
3721 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003722 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003723 if (outputVariable.isArray())
3724 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003725 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003726 }
3727 else
3728 {
3729 VariableLocation locationInfo;
Jamie Madillb980c562018-11-27 11:34:27 -05003730 locationInfo.index = outputVariableIndex;
3731 (*outputLocations)[location] = locationInfo;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003732 }
3733 }
3734 }
3735
3736 // Here we assign locations for the output variables that don't yet have them. Note that we're
3737 // not necessarily able to fit the variables optimally, since then we might have to try
3738 // different arrangements of output arrays. Now we just assign the locations in the order that
3739 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3740 // finding locations for the output variables, so this should be acceptable at least for now.
3741 GLuint maxLocation = caps.maxDrawBuffers;
3742 if (hasSecondaryOutputs)
3743 {
3744 // EXT_blend_func_extended: Program outputs will be validated against
3745 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3746 maxLocation = extensions.maxDualSourceDrawBuffers;
3747 }
3748
3749 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3750 outputVariableIndex++)
3751 {
3752 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3753
3754 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3755 if (outputVariable.isBuiltIn())
3756 continue;
3757
3758 if (getOutputLocationForLink(outputVariable) != -1)
3759 {
3760 continue;
3761 }
3762
3763 auto *outputLocations = &mState.mOutputLocations;
3764 if (isOutputSecondaryForLink(outputVariable))
3765 {
3766 outputLocations = &mState.mSecondaryOutputLocations;
3767 }
3768
3769 int baseLocation = 0;
3770 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3771 bool elementsFit = false;
3772 while (!elementsFit)
3773 {
3774 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3775 elementsFit = true;
3776 if (baseLocation + elementCount > maxLocation)
3777 {
3778 // EXT_blend_func_extended: Linking can fail:
3779 // "if the explicit binding assignments do not leave enough space for the linker to
3780 // automatically assign a location for a varying out array, which requires multiple
3781 // contiguous locations."
3782 mInfoLog << "Could not fit output variable into available locations: "
3783 << outputVariable.name;
3784 return false;
3785 }
3786 unsigned int outputLocationsNeeded =
3787 static_cast<unsigned int>(baseLocation) + elementCount;
3788 if (outputLocationsNeeded > outputLocations->size())
3789 {
3790 outputLocations->resize(outputLocationsNeeded);
3791 }
3792 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3793 {
3794 const unsigned int location =
3795 static_cast<unsigned int>(baseLocation) + elementIndex;
3796 ASSERT(location < outputLocations->size());
3797 if (outputLocations->at(location).used())
3798 {
3799 elementsFit = false;
3800 break;
3801 }
3802 }
3803 if (elementsFit)
3804 {
3805 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3806 {
3807 const unsigned int location =
3808 static_cast<unsigned int>(baseLocation) + elementIndex;
3809 if (outputVariable.isArray())
3810 {
3811 (*outputLocations)[location] =
3812 VariableLocation(elementIndex, outputVariableIndex);
3813 }
3814 else
3815 {
3816 VariableLocation locationInfo;
3817 locationInfo.index = outputVariableIndex;
3818 (*outputLocations)[location] = locationInfo;
3819 }
3820 }
3821 }
3822 else
3823 {
3824 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003825 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003826 }
3827 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003828
3829 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003830}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003831
Olli Etuaho48fed632017-03-16 12:05:30 +00003832void Program::setUniformValuesFromBindingQualifiers()
3833{
Jamie Madill982f6e02017-06-07 14:33:04 -04003834 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003835 {
3836 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3837 if (samplerUniform.binding != -1)
3838 {
Olli Etuahod2551232017-10-26 20:03:33 +03003839 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003840 ASSERT(location != -1);
3841 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003842 for (unsigned int elementIndex = 0;
3843 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003844 {
3845 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3846 }
Jamie Madille3e680c2018-12-03 17:49:08 -05003847
3848 // Here we pass nullptr to avoid a large chain of calls that need a non-const Context.
3849 // We know it's safe not to notify the Context because this is only called after link.
3850 setUniform1iv(nullptr, location, static_cast<GLsizei>(boundTextureUnits.size()),
Olli Etuaho48fed632017-03-16 12:05:30 +00003851 boundTextureUnits.data());
3852 }
3853 }
3854}
3855
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003856void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003857{
jchen10af713a22017-04-19 09:10:56 +08003858 // Set initial bindings from shader.
3859 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3860 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003861 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003862 bindUniformBlock(blockIndex, uniformBlock.binding);
3863 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003864}
3865
Jamie Madille3e680c2018-12-03 17:49:08 -05003866void Program::updateSamplerUniform(Context *context,
3867 const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003868 GLsizei clampedCount,
3869 const GLint *v)
3870{
Jamie Madill81c2e252017-09-09 23:32:46 -04003871 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
Jamie Madillb980c562018-11-27 11:34:27 -05003872 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003873 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3874 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003875
Jamie Madill7e4eff12018-08-08 15:49:26 -04003876 if (samplerBinding.unreferenced)
3877 return;
3878
3879 // Update the sampler uniforms.
3880 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3881 {
Jamie Madille3e680c2018-12-03 17:49:08 -05003882 GLint oldTextureUnit = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3883 GLint newTextureUnit = v[arrayIndex];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003884
Jamie Madille3e680c2018-12-03 17:49:08 -05003885 if (oldTextureUnit == newTextureUnit)
Jamie Madill7e4eff12018-08-08 15:49:26 -04003886 continue;
3887
Jamie Madille3e680c2018-12-03 17:49:08 -05003888 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newTextureUnit;
Jamie Madill7e4eff12018-08-08 15:49:26 -04003889
3890 // Update the reference counts.
Jamie Madille3e680c2018-12-03 17:49:08 -05003891 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldTextureUnit];
3892 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newTextureUnit];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003893 ASSERT(oldRefCount > 0);
3894 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3895 oldRefCount--;
3896 newRefCount++;
3897
3898 // Check for binding type change.
Jamie Madille3e680c2018-12-03 17:49:08 -05003899 TextureType &newSamplerType = mState.mActiveSamplerTypes[newTextureUnit];
3900 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldTextureUnit];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003901
3902 if (newRefCount == 1)
3903 {
3904 newSamplerType = samplerBinding.textureType;
Jamie Madille3e680c2018-12-03 17:49:08 -05003905 mState.mActiveSamplersMask.set(newTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003906 }
3907 else if (newSamplerType != samplerBinding.textureType)
3908 {
3909 // Conflict detected. Ensure we reset it properly.
3910 newSamplerType = TextureType::InvalidEnum;
3911 }
3912
3913 // Unset previously active sampler.
3914 if (oldRefCount == 0)
3915 {
3916 oldSamplerType = TextureType::InvalidEnum;
Jamie Madille3e680c2018-12-03 17:49:08 -05003917 mState.mActiveSamplersMask.reset(oldTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003918 }
3919 else if (oldSamplerType == TextureType::InvalidEnum)
3920 {
3921 // Previous conflict. Check if this new change fixed the conflict.
Jamie Madille3e680c2018-12-03 17:49:08 -05003922 oldSamplerType = mState.getSamplerUniformTextureType(oldTextureUnit);
3923 }
3924
3925 // Notify context.
3926 if (context)
3927 {
3928 context->onSamplerUniformChange(newTextureUnit);
3929 context->onSamplerUniformChange(oldTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003930 }
3931 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003932
3933 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003934 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003935}
3936
Jamie Madill7e4eff12018-08-08 15:49:26 -04003937TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3938{
3939 TextureType foundType = TextureType::InvalidEnum;
3940
3941 for (const SamplerBinding &binding : mSamplerBindings)
3942 {
3943 if (binding.unreferenced)
3944 continue;
3945
3946 // A conflict exists if samplers of different types are sourced by the same texture unit.
3947 // We need to check all bound textures to detect this error case.
3948 for (GLuint textureUnit : binding.boundTextureUnits)
3949 {
3950 if (textureUnit == textureUnitIndex)
3951 {
3952 if (foundType == TextureType::InvalidEnum)
3953 {
3954 foundType = binding.textureType;
3955 }
3956 else if (foundType != binding.textureType)
3957 {
3958 return TextureType::InvalidEnum;
3959 }
3960 }
3961 }
3962 }
3963
3964 return foundType;
3965}
3966
Jamie Madille7d84322017-01-10 18:21:59 -05003967template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003968GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3969 GLsizei count,
3970 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003971 const T *v)
3972{
Jamie Madill134f93d2017-08-31 17:11:00 -04003973 if (count == 1)
3974 return 1;
3975
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003976 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003977
Corentin Wallez15ac5342016-11-03 17:06:39 -04003978 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3979 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003980 unsigned int remainingElements =
3981 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003982 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003983 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003984
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003985 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003986 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003987 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003988 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003989
3990 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003991}
3992
3993template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003994GLsizei Program::clampMatrixUniformCount(GLint location,
3995 GLsizei count,
3996 GLboolean transpose,
3997 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003998{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003999 const VariableLocation &locationInfo = mState.mUniformLocations[location];
4000
Jamie Madill62d31cb2015-09-11 13:25:51 -04004001 if (!transpose)
4002 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004003 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004004 }
4005
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004006 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004007
4008 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4009 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004010 unsigned int remainingElements =
4011 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004012 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004013}
4014
Jamie Madill54164b02017-08-28 15:17:37 -04004015// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4016// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004017template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004018void Program::getUniformInternal(const Context *context,
4019 DestT *dataOut,
4020 GLint location,
4021 GLenum nativeType,
4022 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004023{
Jamie Madill54164b02017-08-28 15:17:37 -04004024 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004025 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004026 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004027 {
4028 GLint tempValue[16] = {0};
4029 mProgram->getUniformiv(context, location, tempValue);
4030 UniformStateQueryCastLoop<GLboolean>(
4031 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004032 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004033 }
4034 case GL_INT:
4035 {
4036 GLint tempValue[16] = {0};
4037 mProgram->getUniformiv(context, location, tempValue);
4038 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4039 components);
4040 break;
4041 }
4042 case GL_UNSIGNED_INT:
4043 {
4044 GLuint tempValue[16] = {0};
4045 mProgram->getUniformuiv(context, location, tempValue);
4046 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4047 components);
4048 break;
4049 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004050 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004051 {
4052 GLfloat tempValue[16] = {0};
4053 mProgram->getUniformfv(context, location, tempValue);
4054 UniformStateQueryCastLoop<GLfloat>(
4055 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004056 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004057 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004058 default:
4059 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004060 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004061 }
4062}
Jamie Madilla4595b82017-01-11 17:36:34 -05004063
4064bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4065{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004066 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004067 // Must be called after samplers are validated.
4068 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4069
4070 for (const auto &binding : mState.mSamplerBindings)
4071 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004072 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004073 for (const auto &unit : binding.boundTextureUnits)
4074 {
4075 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4076 if (programTextureID == textureID)
4077 {
4078 // TODO(jmadill): Check for appropriate overlap.
4079 return true;
4080 }
4081 }
4082 }
4083
4084 return false;
4085}
4086
Jamie Madill6f755b22018-10-09 12:48:54 -04004087angle::Result Program::syncState(const Context *context)
Jamie Madill70aeda42018-08-20 12:17:40 -04004088{
4089 if (mDirtyBits.any())
4090 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004091 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004092 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4093 mDirtyBits.reset();
4094 }
4095
Jamie Madill7c985f52018-11-29 18:16:17 -05004096 return angle::Result::Continue;
Jamie Madill70aeda42018-08-20 12:17:40 -04004097}
Jamie Madilla2c74982016-12-12 11:20:42 -05004098} // namespace gl