blob: 5e237c2aad45ff2316c3302fe00be999f03f0d22 [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
Geoff Lang70d0f492015-12-10 17:45:46 -0500934void Program::setLabel(const std::string &label)
935{
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 Madillf4a789f2018-10-18 16:56:20 -04001106 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);
1115 mLinked = (result == angle::Result::Continue());
1116 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 Madillf4a789f2018-10-18 16:56:20 -04001124 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 Madillf4a789f2018-10-18 16:56:20 -04001147 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 Madillf4a789f2018-10-18 16:56:20 -04001155 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 Madillf4a789f2018-10-18 16:56:20 -04001171 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 Madillf4a789f2018-10-18 16:56:20 -04001201 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 Madillf4a789f2018-10-18 16:56:20 -04001206 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 Madillf4a789f2018-10-18 16:56:20 -04001213 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 Madillf4a789f2018-10-18 16:56:20 -04001221 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 Madillf4a789f2018-10-18 16:56:20 -04001226 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 Madillf4a789f2018-10-18 16:56:20 -04001233 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 Madillf4a789f2018-10-18 16:56:20 -04001247 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 Madillf4a789f2018-10-18 16:56:20 -04001253 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 Madillf4a789f2018-10-18 16:56:20 -04001266 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
1280 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 Madillf4a789f2018-10-18 16:56:20 -04001432 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 Madillf4a789f2018-10-18 16:56:20 -04001438 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);
1444 mLinked = result == angle::Result::Continue();
1445 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 Madillf4a789f2018-10-18 16:56:20 -04001456 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 Madillf4a789f2018-10-18 16:56:20 -04001506 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);
1520 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}
1744DrawBufferMask Program::getActiveOutputVariables() const
1745{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001746 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001747 return mState.mActiveOutputVariables;
1748}
1749
jchen10baf5d942017-08-28 20:45:48 +08001750template <typename T>
1751void Program::getResourceName(GLuint index,
1752 const std::vector<T> &resources,
1753 GLsizei bufSize,
1754 GLsizei *length,
1755 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001756{
1757 if (length)
1758 {
1759 *length = 0;
1760 }
1761
1762 if (!mLinked)
1763 {
1764 if (bufSize > 0)
1765 {
1766 name[0] = '\0';
1767 }
1768 return;
1769 }
jchen10baf5d942017-08-28 20:45:48 +08001770 ASSERT(index < resources.size());
1771 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001772
1773 if (bufSize > 0)
1774 {
Olli Etuahod2551232017-10-26 20:03:33 +03001775 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001776 }
1777}
1778
jchen10baf5d942017-08-28 20:45:48 +08001779void Program::getInputResourceName(GLuint index,
1780 GLsizei bufSize,
1781 GLsizei *length,
1782 GLchar *name) const
1783{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001784 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001785 getResourceName(index, mState.mAttributes, bufSize, length, name);
1786}
1787
1788void Program::getOutputResourceName(GLuint index,
1789 GLsizei bufSize,
1790 GLsizei *length,
1791 GLchar *name) const
1792{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001793 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001794 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1795}
1796
1797void Program::getUniformResourceName(GLuint index,
1798 GLsizei bufSize,
1799 GLsizei *length,
1800 GLchar *name) const
1801{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001802 ASSERT(mLinkResolved);
jchen10baf5d942017-08-28 20:45:48 +08001803 getResourceName(index, mState.mUniforms, bufSize, length, name);
1804}
1805
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001806void Program::getBufferVariableResourceName(GLuint index,
1807 GLsizei bufSize,
1808 GLsizei *length,
1809 GLchar *name) const
1810{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001811 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001812 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1813}
1814
jchen10880683b2017-04-12 16:21:55 +08001815const sh::Attribute &Program::getInputResource(GLuint index) const
1816{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001817 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001818 ASSERT(index < mState.mAttributes.size());
1819 return mState.mAttributes[index];
1820}
1821
1822const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1823{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001824 ASSERT(mLinkResolved);
jchen10880683b2017-04-12 16:21:55 +08001825 ASSERT(index < mState.mOutputVariables.size());
1826 return mState.mOutputVariables[index];
1827}
1828
jchen107ae70d82018-07-06 13:47:01 +08001829const ProgramBindings &Program::getAttributeBindings() const
1830{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001831 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001832 return mAttributeBindings;
1833}
1834const ProgramBindings &Program::getUniformLocationBindings() const
1835{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001836 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001837 return mUniformLocationBindings;
1838}
1839const ProgramBindings &Program::getFragmentInputBindings() const
1840{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001841 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001842 return mFragmentInputBindings;
1843}
1844
1845int Program::getNumViews() const
1846{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001847 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001848 return mState.getNumViews();
1849}
jchen107ae70d82018-07-06 13:47:01 +08001850
1851ComponentTypeMask Program::getDrawBufferTypeMask() const
1852{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001853 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001854 return mState.mDrawBufferTypeMask;
1855}
1856ComponentTypeMask Program::getAttributesTypeMask() const
1857{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001858 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001859 return mState.mAttributesTypeMask;
1860}
1861AttributesMask Program::getAttributesMask() const
1862{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001863 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001864 return mState.mAttributesMask;
1865}
1866
1867const std::vector<GLsizei> &Program::getTransformFeedbackStrides() const
1868{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001869 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001870 return mState.mTransformFeedbackStrides;
1871}
1872
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873GLint Program::getFragDataLocation(const std::string &name) const
1874{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001875 ASSERT(mLinkResolved);
Olli Etuaho0ca09752018-09-24 11:00:50 +03001876 GLint primaryLocation =
1877 GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
1878 if (primaryLocation != -1)
1879 {
1880 return primaryLocation;
1881 }
1882 return GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name);
1883}
1884
1885GLint Program::getFragDataIndex(const std::string &name) const
1886{
1887 ASSERT(mLinkResolved);
1888 if (GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name) != -1)
1889 {
1890 return 0;
1891 }
1892 if (GetVariableLocation(mState.mOutputVariables, mState.mSecondaryOutputLocations, name) != -1)
1893 {
1894 return 1;
1895 }
1896 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001897}
1898
Geoff Lange1a27752015-10-05 13:16:04 -04001899void Program::getActiveUniform(GLuint index,
1900 GLsizei bufsize,
1901 GLsizei *length,
1902 GLint *size,
1903 GLenum *type,
1904 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001905{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001906 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001908 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001909 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001910 ASSERT(index < mState.mUniforms.size());
1911 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912
1913 if (bufsize > 0)
1914 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001915 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001916 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917 }
1918
Olli Etuaho465835d2017-09-26 13:34:10 +03001919 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001920 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001921 }
1922 else
1923 {
1924 if (bufsize > 0)
1925 {
1926 name[0] = '\0';
1927 }
1928
1929 if (length)
1930 {
1931 *length = 0;
1932 }
1933
1934 *size = 0;
1935 *type = GL_NONE;
1936 }
1937}
1938
Geoff Lange1a27752015-10-05 13:16:04 -04001939GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001940{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001941 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001942 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001943 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001944 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001945 }
1946 else
1947 {
1948 return 0;
1949 }
1950}
1951
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001952size_t Program::getActiveBufferVariableCount() const
1953{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001954 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001955 return mLinked ? mState.mBufferVariables.size() : 0;
1956}
1957
Geoff Lange1a27752015-10-05 13:16:04 -04001958GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001959{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001960 ASSERT(mLinkResolved);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001961 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001962
1963 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001964 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001965 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001967 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001969 size_t length = uniform.name.length() + 1u;
1970 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971 {
1972 length += 3; // Counting in "[0]".
1973 }
1974 maxLength = std::max(length, maxLength);
1975 }
1976 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001977 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001978
Jamie Madill62d31cb2015-09-11 13:25:51 -04001979 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980}
1981
Geoff Lang7dd2e102014-11-10 15:19:26 -05001982bool Program::isValidUniformLocation(GLint location) const
1983{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001984 ASSERT(mLinkResolved);
Jamie Madille2e406c2016-06-02 13:04:10 -04001985 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001986 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001987 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001988}
1989
Jamie Madill62d31cb2015-09-11 13:25:51 -04001990const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001991{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001992 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001993 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001994 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001995}
1996
Jamie Madillac4e9c32017-01-13 14:07:12 -05001997const VariableLocation &Program::getUniformLocation(GLint location) const
1998{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001999 ASSERT(mLinkResolved);
Jamie Madillac4e9c32017-01-13 14:07:12 -05002000 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
2001 return mState.mUniformLocations[location];
2002}
2003
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002004const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
2005{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002006 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002007 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
2008 return mState.mBufferVariables[index];
2009}
2010
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002013 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03002014 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015}
2016
Jamie Madill62d31cb2015-09-11 13:25:51 -04002017GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002019 ASSERT(mLinkResolved);
Jamie Madille7d84322017-01-10 18:21:59 -05002020 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021}
2022
2023void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
2024{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002025 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002026 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2027 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002028 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029}
2030
2031void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
2032{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002033 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002034 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2035 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002036 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037}
2038
2039void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
2040{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002041 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002042 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2043 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002044 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045}
2046
2047void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
2048{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002049 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002050 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2051 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002052 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053}
2054
Jamie Madill81c2e252017-09-09 23:32:46 -04002055Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002057 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002058 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2059 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
2060
Jamie Madill81c2e252017-09-09 23:32:46 -04002061 mProgram->setUniform1iv(location, clampedCount, v);
2062
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002063 if (mState.isSamplerUniformIndex(locationInfo.index))
2064 {
2065 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04002066 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002067 }
2068
Jamie Madill81c2e252017-09-09 23:32:46 -04002069 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070}
2071
2072void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2073{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002074 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002075 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2076 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002077 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078}
2079
2080void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2081{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002082 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002083 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2084 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002085 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086}
2087
2088void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2089{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002090 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002091 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2092 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002093 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094}
2095
2096void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2097{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002098 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002099 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2100 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002101 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102}
2103
2104void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2105{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002106 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002107 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2108 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002109 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110}
2111
2112void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2113{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002114 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002115 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2116 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002117 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118}
2119
2120void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2121{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002122 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002123 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2124 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002125 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126}
2127
Jamie Madill493f9572018-05-24 19:52:15 -04002128void Program::setUniformMatrix2fv(GLint location,
2129 GLsizei count,
2130 GLboolean transpose,
2131 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002133 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002134 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002135 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002136}
2137
Jamie Madill493f9572018-05-24 19:52:15 -04002138void Program::setUniformMatrix3fv(GLint location,
2139 GLsizei count,
2140 GLboolean transpose,
2141 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002143 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002144 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002145 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146}
2147
Jamie Madill493f9572018-05-24 19:52:15 -04002148void Program::setUniformMatrix4fv(GLint location,
2149 GLsizei count,
2150 GLboolean transpose,
2151 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002153 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002154 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002155 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002156}
2157
Jamie Madill493f9572018-05-24 19:52:15 -04002158void Program::setUniformMatrix2x3fv(GLint location,
2159 GLsizei count,
2160 GLboolean transpose,
2161 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002163 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002164 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002165 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002166}
2167
Jamie Madill493f9572018-05-24 19:52:15 -04002168void Program::setUniformMatrix2x4fv(GLint location,
2169 GLsizei count,
2170 GLboolean transpose,
2171 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002173 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002174 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002175 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176}
2177
Jamie Madill493f9572018-05-24 19:52:15 -04002178void Program::setUniformMatrix3x2fv(GLint location,
2179 GLsizei count,
2180 GLboolean transpose,
2181 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002183 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002184 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002185 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002186}
2187
Jamie Madill493f9572018-05-24 19:52:15 -04002188void Program::setUniformMatrix3x4fv(GLint location,
2189 GLsizei count,
2190 GLboolean transpose,
2191 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002193 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002194 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002195 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002196}
2197
Jamie Madill493f9572018-05-24 19:52:15 -04002198void Program::setUniformMatrix4x2fv(GLint location,
2199 GLsizei count,
2200 GLboolean transpose,
2201 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002203 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002204 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002205 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002206}
2207
Jamie Madill493f9572018-05-24 19:52:15 -04002208void Program::setUniformMatrix4x3fv(GLint location,
2209 GLsizei count,
2210 GLboolean transpose,
2211 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002212{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002213 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002214 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002215 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002216}
2217
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002218GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2219{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002220 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002221 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2222 const std::vector<GLuint> &boundTextureUnits =
2223 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2224 return boundTextureUnits[uniformLocation.arrayIndex];
2225}
2226
Jamie Madill54164b02017-08-28 15:17:37 -04002227void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002229 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002230 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2231 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002232
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002233 if (uniform.isSampler())
2234 {
2235 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2236 return;
2237 }
2238
2239 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002240 if (nativeType == GL_FLOAT)
2241 {
2242 mProgram->getUniformfv(context, location, v);
2243 }
2244 else
2245 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002246 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002247 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248}
2249
Jamie Madill54164b02017-08-28 15:17:37 -04002250void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002251{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002252 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002253 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2254 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002255
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002256 if (uniform.isSampler())
2257 {
2258 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2259 return;
2260 }
2261
2262 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002263 if (nativeType == GL_INT || nativeType == GL_BOOL)
2264 {
2265 mProgram->getUniformiv(context, location, v);
2266 }
2267 else
2268 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002269 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002270 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271}
2272
Jamie Madill54164b02017-08-28 15:17:37 -04002273void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002274{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002275 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002276 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2277 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002278
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002279 if (uniform.isSampler())
2280 {
2281 *v = getSamplerUniformBinding(uniformLocation);
2282 return;
2283 }
2284
2285 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002286 if (nativeType == GL_UNSIGNED_INT)
2287 {
2288 mProgram->getUniformuiv(context, location, v);
2289 }
2290 else
2291 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002292 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002293 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294}
2295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296void Program::flagForDeletion()
2297{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002298 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 mDeleteStatus = true;
2300}
2301
2302bool Program::isFlaggedForDeletion() const
2303{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002304 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 return mDeleteStatus;
2306}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002307
Brandon Jones43a53e22014-08-28 16:23:22 -07002308void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002309{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002310 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002311 mInfoLog.reset();
2312
Geoff Lang7dd2e102014-11-10 15:19:26 -05002313 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002314 {
Geoff Lang92019432017-11-20 13:09:34 -05002315 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002316 }
2317 else
2318 {
Jamie Madillf6113162015-05-07 11:49:21 -04002319 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002320 }
2321}
2322
Jamie Madillcc73f242018-08-01 11:34:48 -04002323bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002324{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002325 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002326
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002327 // if any two active samplers in a program are of different types, but refer to the same
2328 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2329 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002330 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002331 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002332 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002333 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002334 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002335 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002336 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2337 "image unit ("
2338 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002339 }
2340
Jamie Madill7e4eff12018-08-08 15:49:26 -04002341 mCachedValidateSamplersResult = false;
2342 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002343 }
2344 }
2345
2346 mCachedValidateSamplersResult = true;
2347 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002348}
2349
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002350bool Program::isValidated() const
2351{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002352 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002353 return mValidated;
2354}
2355
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002356void Program::getActiveUniformBlockName(const GLuint blockIndex,
2357 GLsizei bufSize,
2358 GLsizei *length,
2359 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002360{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002361 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002362 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2363}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002365void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2366 GLsizei bufSize,
2367 GLsizei *length,
2368 GLchar *blockName) const
2369{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002370 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002371 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002372}
2373
Qin Jiajia9bf55522018-01-29 13:56:23 +08002374template <typename T>
2375GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002376{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002377 int maxLength = 0;
2378
2379 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002380 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002381 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002382 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002383 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002384 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002385 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002386 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387 }
2388 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002389 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390
2391 return maxLength;
2392}
2393
Qin Jiajia9bf55522018-01-29 13:56:23 +08002394GLint Program::getActiveUniformBlockMaxNameLength() const
2395{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002396 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002397 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2398}
2399
2400GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2401{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002402 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002403 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2404}
2405
Geoff Lange1a27752015-10-05 13:16:04 -04002406GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002407{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002408 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002409 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2410}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002411
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002412GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2413{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002414 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002415 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002416}
2417
Jiajia Qin729b2c62017-08-14 09:36:11 +08002418const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002419{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002420 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002421 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2422 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002423}
2424
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002425const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2426{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002427 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002428 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2429 return mState.mShaderStorageBlocks[index];
2430}
2431
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002432void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2433{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002434 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002435 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002436 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002437 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002438}
2439
2440GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2441{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002442 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002443 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002444}
2445
Jiajia Qin729b2c62017-08-14 09:36:11 +08002446GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2447{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002448 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002449 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2450}
2451
Jamie Madill493f9572018-05-24 19:52:15 -04002452void Program::setTransformFeedbackVaryings(GLsizei count,
2453 const GLchar *const *varyings,
2454 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002455{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002456 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002457 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002458 for (GLsizei i = 0; i < count; i++)
2459 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002460 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002461 }
2462
Jamie Madill48ef11b2016-04-27 15:21:52 -04002463 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002464}
2465
Jamie Madill493f9572018-05-24 19:52:15 -04002466void Program::getTransformFeedbackVarying(GLuint index,
2467 GLsizei bufSize,
2468 GLsizei *length,
2469 GLsizei *size,
2470 GLenum *type,
2471 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002472{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002473 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002474 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002475 {
jchen10a9042d32017-03-17 08:50:45 +08002476 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2477 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2478 std::string varName = var.nameWithArrayIndex();
2479 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002480 if (length)
2481 {
2482 *length = lastNameIdx;
2483 }
2484 if (size)
2485 {
jchen10a9042d32017-03-17 08:50:45 +08002486 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002487 }
2488 if (type)
2489 {
jchen10a9042d32017-03-17 08:50:45 +08002490 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002491 }
2492 if (name)
2493 {
jchen10a9042d32017-03-17 08:50:45 +08002494 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002495 name[lastNameIdx] = '\0';
2496 }
2497 }
2498}
2499
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002500GLsizei Program::getTransformFeedbackVaryingCount() const
2501{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002502 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002503 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002504 {
jchen10a9042d32017-03-17 08:50:45 +08002505 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002506 }
2507 else
2508 {
2509 return 0;
2510 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002511}
2512
2513GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2514{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002515 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002516 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002517 {
2518 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002519 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002520 {
jchen10a9042d32017-03-17 08:50:45 +08002521 maxSize =
2522 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002523 }
2524
2525 return maxSize;
2526 }
2527 else
2528 {
2529 return 0;
2530 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002531}
2532
2533GLenum Program::getTransformFeedbackBufferMode() const
2534{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002535 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002536 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002537}
2538
jchen103fd614d2018-08-13 12:21:58 +08002539bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002540{
Jiawei Shao016105b2018-04-12 16:38:31 +08002541 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2542 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2543 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2544 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002545
Jamie Madill493f9572018-05-24 19:52:15 -04002546 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002547 bool isGraphicsShaderAttached =
2548 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002549 // Check whether we both have a compute and non-compute shaders attached.
2550 // If there are of both types attached, then linking should fail.
2551 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2552 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2553 {
2554 infoLog << "Both compute and graphics shaders are attached to the same program.";
2555 return false;
2556 }
2557
2558 if (computeShader)
2559 {
jchen103fd614d2018-08-13 12:21:58 +08002560 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002561 {
2562 infoLog << "Attached compute shader is not compiled.";
2563 return false;
2564 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002565 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002566
jchen103fd614d2018-08-13 12:21:58 +08002567 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002568
2569 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2570 // If the work group size is not specified, a link time error should occur.
2571 if (!mState.mComputeShaderLocalSize.isDeclared())
2572 {
2573 infoLog << "Work group size is not specified.";
2574 return false;
2575 }
2576 }
2577 else
2578 {
jchen103fd614d2018-08-13 12:21:58 +08002579 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002580 {
2581 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2582 return false;
2583 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002584 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002585
jchen103fd614d2018-08-13 12:21:58 +08002586 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002587 {
2588 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2589 return false;
2590 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002591 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002592
jchen103fd614d2018-08-13 12:21:58 +08002593 int vertexShaderVersion = vertexShader->getShaderVersion();
2594 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002595 {
2596 infoLog << "Fragment shader version does not match vertex shader version.";
2597 return false;
2598 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002599
2600 if (geometryShader)
2601 {
2602 // [GL_EXT_geometry_shader] Chapter 7
2603 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2604 // Language Specification, as well as any of the following reasons:
2605 // * One or more of the shader objects attached to <program> are not compiled
2606 // successfully.
2607 // * The shaders do not use the same shader language version.
2608 // * <program> contains objects to form a geometry shader, and
2609 // - <program> is not separable and contains no objects to form a vertex shader; or
2610 // - the input primitive type, output primitive type, or maximum output vertex count
2611 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002612 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002613 {
2614 infoLog << "The attached geometry shader isn't compiled.";
2615 return false;
2616 }
2617
jchen103fd614d2018-08-13 12:21:58 +08002618 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002619 {
2620 mInfoLog << "Geometry shader version does not match vertex shader version.";
2621 return false;
2622 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002623 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002624
Jamie Madill493f9572018-05-24 19:52:15 -04002625 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002626 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002627 if (!inputPrimitive.valid())
2628 {
2629 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2630 return false;
2631 }
2632
Jamie Madill493f9572018-05-24 19:52:15 -04002633 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002634 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002635 if (!outputPrimitive.valid())
2636 {
2637 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2638 return false;
2639 }
2640
jchen103fd614d2018-08-13 12:21:58 +08002641 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002642 if (!maxVertices.valid())
2643 {
2644 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2645 return false;
2646 }
2647
2648 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2649 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2650 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002651 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002652 }
Jiawei Shao73618602017-12-20 15:47:15 +08002653 }
2654
2655 return true;
2656}
2657
jchen10910a3da2017-11-15 09:40:11 +08002658GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2659{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002660 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002661 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2662 {
2663 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2664 if (tf.nameWithArrayIndex() == name)
2665 {
2666 return tfIndex;
2667 }
2668 }
2669 return GL_INVALID_INDEX;
2670}
2671
2672const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2673{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002674 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002675 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2676 return mState.mLinkedTransformFeedbackVaryings[index];
2677}
2678
Austin Eng1bf18ce2018-10-19 15:34:02 -07002679bool Program::hasDrawIDUniform() const
2680{
2681 ASSERT(mLinkResolved);
2682 return mState.mDrawIDLocation >= 0;
2683}
2684
2685void Program::setDrawIDUniform(GLint drawid)
2686{
2687 ASSERT(mLinkResolved);
2688 ASSERT(mState.mDrawIDLocation >= 0);
2689 mProgram->setUniform1iv(mState.mDrawIDLocation, 1, &drawid);
2690}
2691
jchen103fd614d2018-08-13 12:21:58 +08002692bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002693{
Jiawei Shao016105b2018-04-12 16:38:31 +08002694 Shader *previousShader = nullptr;
2695 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002696 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002697 Shader *currentShader = mState.mAttachedShaders[shaderType];
2698 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002699 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002700 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002701 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002702
2703 if (previousShader)
2704 {
jchen103fd614d2018-08-13 12:21:58 +08002705 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002706 {
2707 return false;
2708 }
2709 }
2710 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002711 }
2712
jchen103fd614d2018-08-13 12:21:58 +08002713 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002714 {
2715 return false;
2716 }
2717
jchen103fd614d2018-08-13 12:21:58 +08002718 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002719 {
2720 return false;
2721 }
2722
2723 return true;
2724}
2725
2726// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2727// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002728bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002729 gl::Shader *consumingShader,
2730 gl::InfoLog &infoLog) const
2731{
jchen103fd614d2018-08-13 12:21:58 +08002732 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002733
jchen103fd614d2018-08-13 12:21:58 +08002734 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2735 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002736
Jiawei Shao385b3e02018-03-21 09:43:28 +08002737 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002738
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002739 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002740 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002741 bool matched = false;
2742
2743 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002744 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002745 {
2746 continue;
2747 }
2748
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002749 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002750 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002751 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002752 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002753 ASSERT(!output.isBuiltIn());
2754
2755 std::string mismatchedStructFieldName;
2756 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002757 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002758 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002759 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002760 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002761 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2762 mismatchedStructFieldName, generatingShader->getType(),
2763 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002764 return false;
2765 }
2766
Geoff Lang7dd2e102014-11-10 15:19:26 -05002767 matched = true;
2768 break;
2769 }
2770 }
2771
Olli Etuaho107c7242018-03-20 15:45:35 +02002772 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2773 // whether the input is statically used - a statically used input should fail this test even
2774 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002775 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002776 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002777 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2778 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2779 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002780 return false;
2781 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002782 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002783
Jiawei Shaod063aff2018-02-22 10:19:09 +08002784 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002785
Jiawei Shaod063aff2018-02-22 10:19:09 +08002786 return true;
2787}
2788
jchen103fd614d2018-08-13 12:21:58 +08002789bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002790{
Jiawei Shao016105b2018-04-12 16:38:31 +08002791 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002792
2793 std::map<GLuint, std::string> staticFragmentInputLocations;
2794
2795 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002796 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002797 for (const sh::Varying &input : fragmentInputVaryings)
2798 {
2799 if (input.isBuiltIn() || !input.staticUse)
2800 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002801 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002802 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002803
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002804 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002805 if (inputBinding == -1)
2806 continue;
2807
2808 const auto it = staticFragmentInputLocations.find(inputBinding);
2809 if (it == std::end(staticFragmentInputLocations))
2810 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002811 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002812 }
2813 else
2814 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002815 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002816 << it->second;
2817 return false;
2818 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002819 }
2820
2821 return true;
2822}
2823
jchen103fd614d2018-08-13 12:21:58 +08002824bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002825 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002826 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002827 GLuint *combinedImageUniformsCount,
2828 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002829{
Olli Etuahob78707c2017-03-09 15:03:11 +00002830 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002831 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002832 {
2833 return false;
2834 }
2835
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002836 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002837
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002838 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002839
jchen10eaef1e52017-06-13 10:44:11 +08002840 if (!linkAtomicCounterBuffers())
2841 {
2842 return false;
2843 }
2844
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002845 return true;
2846}
2847
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002848void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002849{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002850 ASSERT(combinedImageUniforms);
2851
Jamie Madill982f6e02017-06-07 14:33:04 -04002852 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2853 unsigned int low = high;
2854
jchen10eaef1e52017-06-13 10:44:11 +08002855 for (auto counterIter = mState.mUniforms.rbegin();
2856 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2857 {
2858 --low;
2859 }
2860
2861 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2862
2863 high = low;
2864
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002865 for (auto imageIter = mState.mUniforms.rbegin();
2866 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2867 {
2868 --low;
2869 }
2870
2871 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002872 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002873 // If uniform is a image type, insert it into the mImageBindings array.
2874 for (unsigned int imageIndex : mState.mImageUniformRange)
2875 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002876 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2877 // cannot load values into a uniform defined as an image. if declare without a
2878 // binding qualifier, any uniform image variable (include all elements of
2879 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002880 auto &imageUniform = mState.mUniforms[imageIndex];
2881 if (imageUniform.binding == -1)
2882 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002883 mState.mImageBindings.emplace_back(
2884 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002885 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002886 else
2887 {
2888 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002889 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002890 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002891
2892 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2893 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002894 }
2895
2896 high = low;
2897
2898 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002899 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002900 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002901 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002902 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002903
2904 mState.mSamplerUniformRange = RangeUI(low, high);
2905
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002906 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002907 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002908 {
2909 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002910 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002911 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2912 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002913 }
2914}
2915
jchen10eaef1e52017-06-13 10:44:11 +08002916bool Program::linkAtomicCounterBuffers()
2917{
2918 for (unsigned int index : mState.mAtomicCounterUniformRange)
2919 {
Jamie Madill493f9572018-05-24 19:52:15 -04002920 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002921 uniform.blockInfo.offset = uniform.offset;
2922 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2923 uniform.blockInfo.matrixStride = 0;
2924 uniform.blockInfo.isRowMajorMatrix = false;
2925
Jamie Madill493f9572018-05-24 19:52:15 -04002926 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002927 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2928 ++bufferIndex)
2929 {
2930 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2931 if (buffer.binding == uniform.binding)
2932 {
2933 buffer.memberIndexes.push_back(index);
2934 uniform.bufferIndex = bufferIndex;
2935 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002936 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002937 break;
2938 }
2939 }
2940 if (!found)
2941 {
2942 AtomicCounterBuffer atomicCounterBuffer;
2943 atomicCounterBuffer.binding = uniform.binding;
2944 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002945 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002946 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2947 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2948 }
2949 }
2950 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002951 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002952
2953 return true;
2954}
2955
Jamie Madilleb979bf2016-11-15 12:28:46 -05002956// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002957bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002958{
Jamie Madillb980c562018-11-27 11:34:27 -05002959 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002960
jchen103fd614d2018-08-13 12:21:58 +08002961 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002962
Geoff Lang7dd2e102014-11-10 15:19:26 -05002963 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002964 if (shaderVersion >= 300)
2965 {
2966 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2967 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002968 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002969 }
2970 else
2971 {
2972 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002973 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002974 }
jchen103fd614d2018-08-13 12:21:58 +08002975 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002976
2977 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002978 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002979 {
Jamie Madillf6113162015-05-07 11:49:21 -04002980 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002981 return false;
2982 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002983
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002984 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002985
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002986 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002987 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002988 {
Olli Etuahod2551232017-10-26 20:03:33 +03002989 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2990 // structures, so we don't need to worry about adjusting their names or generating entries
2991 // for each member/element (unlike uniforms for example).
2992 ASSERT(!attribute.isArray() && !attribute.isStruct());
2993
Jamie Madilleb979bf2016-11-15 12:28:46 -05002994 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002995 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002996 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002997 attribute.location = bindingLocation;
2998 }
2999
3000 if (attribute.location != -1)
3001 {
3002 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04003003 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003004
Jamie Madill63805b42015-08-25 13:17:39 -04003005 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003006 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003007 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3008 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003009
3010 return false;
3011 }
3012
Jamie Madill63805b42015-08-25 13:17:39 -04003013 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003014 {
Jamie Madill63805b42015-08-25 13:17:39 -04003015 const int regLocation = attribute.location + reg;
3016 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003017
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003018 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3019 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3020 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003021 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003022 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003023 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003024 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003025 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003026 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003027 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003028 return false;
3029 }
3030 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003031 else
3032 {
Jamie Madill63805b42015-08-25 13:17:39 -04003033 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003034 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003035
Jamie Madill63805b42015-08-25 13:17:39 -04003036 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003037 }
3038 }
3039 }
3040
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003041 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003042 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003043 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003044 // Not set by glBindAttribLocation or by location layout qualifier
3045 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003046 {
Jamie Madill63805b42015-08-25 13:17:39 -04003047 int regs = VariableRegisterCount(attribute.type);
3048 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003049
Jamie Madill63805b42015-08-25 13:17:39 -04003050 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003051 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003052 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003053 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003054 }
3055
Jamie Madillc349ec02015-08-21 16:53:12 -04003056 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003057 }
3058 }
3059
Brandon Jonesc405ae72017-12-06 14:15:03 -08003060 ASSERT(mState.mAttributesTypeMask.none());
3061 ASSERT(mState.mAttributesMask.none());
3062
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003063 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3064 // shader versions we're only processing active attributes to begin with.
3065 if (shaderVersion >= 300)
3066 {
3067 for (auto attributeIter = mState.mAttributes.begin();
3068 attributeIter != mState.mAttributes.end();)
3069 {
3070 if (attributeIter->active)
3071 {
3072 ++attributeIter;
3073 }
3074 else
3075 {
3076 attributeIter = mState.mAttributes.erase(attributeIter);
3077 }
3078 }
3079 }
3080
Jamie Madill48ef11b2016-04-27 15:21:52 -04003081 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003082 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003083 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003084 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003085 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003086
Jamie Madillbd159f02017-10-09 19:39:06 -04003087 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003088 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003089 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3090 mState.mActiveAttribLocationsMask.set(location);
3091 mState.mMaxActiveAttribLocation =
3092 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003093
3094 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3095 if (!attribute.isBuiltIn())
3096 {
3097 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3098 location);
3099 mState.mAttributesMask.set(location);
3100 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003101 }
3102 }
3103
Geoff Lang7dd2e102014-11-10 15:19:26 -05003104 return true;
3105}
3106
jchen103fd614d2018-08-13 12:21:58 +08003107bool Program::linkInterfaceBlocks(const Caps &caps,
3108 const Version &version,
3109 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003110 InfoLog &infoLog,
3111 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003112{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003113 ASSERT(combinedShaderStorageBlocksCount);
3114
Jiawei Shao54aafe52018-04-27 14:54:57 +08003115 GLuint combinedUniformBlocksCount = 0u;
3116 GLuint numShadersHasUniformBlocks = 0u;
3117 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3118 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003119 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003120 Shader *shader = mState.mAttachedShaders[shaderType];
3121 if (!shader)
3122 {
3123 continue;
3124 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003125
jchen103fd614d2018-08-13 12:21:58 +08003126 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003127 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003128 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003129 if (!ValidateInterfaceBlocksCount(
3130 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3131 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3132 {
3133 return false;
3134 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003135
Jiawei Shao54aafe52018-04-27 14:54:57 +08003136 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3137 ++numShadersHasUniformBlocks;
3138 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003139 }
3140
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003141 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3142 {
3143 infoLog << "The sum of the number of active uniform blocks exceeds "
3144 "MAX_COMBINED_UNIFORM_BLOCKS ("
3145 << caps.maxCombinedUniformBlocks << ").";
3146 return false;
3147 }
3148
Jiawei Shao54aafe52018-04-27 14:54:57 +08003149 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3150 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003151 {
3152 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003153 }
Jamie Madille473dee2015-08-18 14:49:01 -04003154
jchen103fd614d2018-08-13 12:21:58 +08003155 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003156 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003157 *combinedShaderStorageBlocksCount = 0u;
3158 GLuint numShadersHasShaderStorageBlocks = 0u;
3159 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3160 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003161 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003162 Shader *shader = mState.mAttachedShaders[shaderType];
3163 if (!shader)
3164 {
3165 continue;
3166 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003167
jchen103fd614d2018-08-13 12:21:58 +08003168 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003169 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003170 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003171 if (!ValidateInterfaceBlocksCount(
3172 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3173 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3174 {
3175 return false;
3176 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003177
Jiawei Shao54aafe52018-04-27 14:54:57 +08003178 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3179 ++numShadersHasShaderStorageBlocks;
3180 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003181 }
3182
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003183 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003184 {
3185 infoLog << "The sum of the number of active shader storage blocks exceeds "
3186 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3187 << caps.maxCombinedShaderStorageBlocks << ").";
3188 return false;
3189 }
3190
Jiawei Shao54aafe52018-04-27 14:54:57 +08003191 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3192 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003193 {
3194 return false;
3195 }
3196 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003197
Geoff Lang7dd2e102014-11-10 15:19:26 -05003198 return true;
3199}
3200
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003201LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3202 const sh::ShaderVariable &variable2,
3203 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003204 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003205 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003206{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003207 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003208 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003209 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003210 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003211 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003212 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003213 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003214 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003215 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003216 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003217 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003218 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003219 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003220 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003221 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003222 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003223
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003224 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003225 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003226 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003227 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003228 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003229 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3230 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003231 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3232 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003233
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003234 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003235 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003236 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003237 }
3238
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003239 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003240 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003241 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003242 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003243 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3244 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003245 }
3246 }
3247
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003248 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003249}
3250
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003251LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3252 const sh::Varying &inputVarying,
3253 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003254 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003255 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003256{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003257 if (validateGeometryShaderInputVarying)
3258 {
3259 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3260 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3261 // or outputs.
3262 ASSERT(inputVarying.arraySizes.size() == 1u);
3263
3264 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3265 // varying cannot match a geometry shader input varying.
3266 // [GL_EXT_geometry_shader] Section 7.4.1:
3267 // Geometry shader per-vertex input variables and blocks are required to be declared as
3268 // arrays, with each element representing input or output values for a single vertex of a
3269 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3270 // are treated as though they were not declared as arrays.
3271 if (outputVarying.isArray())
3272 {
3273 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3274 }
3275 }
3276
3277 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3278 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003279 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003280 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3281 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003282 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003283 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003284 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003285 }
3286
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003287 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003288 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003289 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003290 }
3291
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003292 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003293 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003294 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003295 }
3296
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003297 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003298}
3299
jchen103fd614d2018-08-13 12:21:58 +08003300bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003301{
Jiawei Shao016105b2018-04-12 16:38:31 +08003302 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3303 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003304 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3305 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3306 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003307
3308 if (shaderVersion != 100)
3309 {
3310 // Only ESSL 1.0 has restrictions on matching input and output invariance
3311 return true;
3312 }
3313
3314 bool glPositionIsInvariant = false;
3315 bool glPointSizeIsInvariant = false;
3316 bool glFragCoordIsInvariant = false;
3317 bool glPointCoordIsInvariant = false;
3318
3319 for (const sh::Varying &varying : vertexVaryings)
3320 {
3321 if (!varying.isBuiltIn())
3322 {
3323 continue;
3324 }
3325 if (varying.name.compare("gl_Position") == 0)
3326 {
3327 glPositionIsInvariant = varying.isInvariant;
3328 }
3329 else if (varying.name.compare("gl_PointSize") == 0)
3330 {
3331 glPointSizeIsInvariant = varying.isInvariant;
3332 }
3333 }
3334
3335 for (const sh::Varying &varying : fragmentVaryings)
3336 {
3337 if (!varying.isBuiltIn())
3338 {
3339 continue;
3340 }
3341 if (varying.name.compare("gl_FragCoord") == 0)
3342 {
3343 glFragCoordIsInvariant = varying.isInvariant;
3344 }
3345 else if (varying.name.compare("gl_PointCoord") == 0)
3346 {
3347 glPointCoordIsInvariant = varying.isInvariant;
3348 }
3349 }
3350
3351 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3352 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3353 // Not requiring invariance to match is supported by:
3354 // dEQP, WebGL CTS, Nexus 5X GLES
3355 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3356 {
3357 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3358 "declared invariant.";
3359 return false;
3360 }
3361 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3362 {
3363 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3364 "declared invariant.";
3365 return false;
3366 }
3367
3368 return true;
3369}
3370
jchen103fd614d2018-08-13 12:21:58 +08003371bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003372 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003373 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003374 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003375{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003376
jchen108225e732017-11-14 16:29:03 +08003377 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003378 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003379 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003380 {
jchen103fd614d2018-08-13 12:21:58 +08003381 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003382 {
Geoff Lang1a683462015-09-29 15:09:59 -04003383 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003384 return false;
3385 }
jchen103fd614d2018-08-13 12:21:58 +08003386 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003387 {
3388 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3389 {
3390 infoLog << "Two transform feedback varyings include the same array element ("
3391 << tfVaryingName << ").";
3392 return false;
3393 }
3394 }
3395 else
3396 {
3397 if (uniqueNames.count(tfVaryingName) > 0)
3398 {
3399 infoLog << "Two transform feedback varyings specify the same output variable ("
3400 << tfVaryingName << ").";
3401 return false;
3402 }
3403 }
3404 uniqueNames.insert(tfVaryingName);
3405 }
3406
3407 // Validate against program varyings.
3408 size_t totalComponents = 0;
3409 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3410 {
3411 std::vector<unsigned int> subscripts;
3412 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3413
3414 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3415 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003416 {
3417 infoLog << "Transform feedback varying " << tfVaryingName
3418 << " does not exist in the vertex shader.";
3419 return false;
3420 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003421
jchen108225e732017-11-14 16:29:03 +08003422 // Validate the matching variable.
3423 if (var->isStruct())
3424 {
3425 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3426 return false;
3427 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003428
jchen108225e732017-11-14 16:29:03 +08003429 size_t elementCount = 0;
3430 size_t componentCount = 0;
3431
3432 if (var->isArray())
3433 {
jchen103fd614d2018-08-13 12:21:58 +08003434 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003435 {
3436 infoLog << "Capture of arrays is undefined and not supported.";
3437 return false;
3438 }
3439
3440 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3441 ASSERT(!var->isArrayOfArrays());
3442
3443 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3444 {
3445 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3446 return false;
3447 }
3448 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3449 }
3450 else
3451 {
3452 if (!subscripts.empty())
3453 {
3454 infoLog << "Varying '" << baseName
3455 << "' is not an array to be captured by element.";
3456 return false;
3457 }
3458 elementCount = 1;
3459 }
3460
3461 // TODO(jmadill): Investigate implementation limits on D3D11
3462 componentCount = VariableComponentCount(var->type) * elementCount;
3463 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3464 componentCount > caps.maxTransformFeedbackSeparateComponents)
3465 {
3466 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3467 << componentCount << ") exceed the maximum separate components ("
3468 << caps.maxTransformFeedbackSeparateComponents << ").";
3469 return false;
3470 }
3471
3472 totalComponents += componentCount;
3473 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3474 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3475 {
3476 infoLog << "Transform feedback varying total components (" << totalComponents
3477 << ") exceed the maximum interleaved components ("
3478 << caps.maxTransformFeedbackInterleavedComponents << ").";
3479 return false;
3480 }
3481 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003482 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003483}
3484
jchen103fd614d2018-08-13 12:21:58 +08003485bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003486{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003487 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003488 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003489
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003490 for (const auto &attrib : attributes)
3491 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003492 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003493 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003494 Shader *shader = mState.mAttachedShaders[shaderType];
3495 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003496 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003497 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003498 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003499
jchen103fd614d2018-08-13 12:21:58 +08003500 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003501 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003502 {
3503 if (uniform.name == attrib.name)
3504 {
3505 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3506 return false;
3507 }
3508 }
3509 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003510 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003511
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003512 return true;
3513}
3514
Jamie Madill3c1da042017-11-27 18:33:40 -05003515void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003516{
3517 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003518 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003519 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003520 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003521 std::vector<unsigned int> subscripts;
3522 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003523 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003524 if (!subscripts.empty())
3525 {
3526 subscript = subscripts.back();
3527 }
Jamie Madill192745a2016-12-22 15:58:21 -05003528 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003529 {
Jamie Madill192745a2016-12-22 15:58:21 -05003530 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003531 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003532 {
jchen10a9042d32017-03-17 08:50:45 +08003533 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3534 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003535 break;
3536 }
jchen108225e732017-11-14 16:29:03 +08003537 else if (varying->isStruct())
3538 {
3539 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3540 if (field != nullptr)
3541 {
3542 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3543 break;
3544 }
3545 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003546 }
3547 }
James Darpinian30b604d2018-03-12 17:26:57 -07003548 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003549}
3550
jchen103fd614d2018-08-13 12:21:58 +08003551ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003552{
Jamie Madill3c1da042017-11-27 18:33:40 -05003553 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003554
Jiawei Shao016105b2018-04-12 16:38:31 +08003555 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003556 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003557 {
Jamie Madill192745a2016-12-22 15:58:21 -05003558 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003559 }
3560
Jiawei Shao016105b2018-04-12 16:38:31 +08003561 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003562 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003563 {
Jamie Madill192745a2016-12-22 15:58:21 -05003564 merged[varying.name].fragment = &varying;
3565 }
3566
3567 return merged;
3568}
3569
Olli Etuaho0ca09752018-09-24 11:00:50 +03003570bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3571{
3572 return a.getArraySizeProduct() > b.getArraySizeProduct();
3573}
3574
3575int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3576{
3577 if (outputVariable.location != -1)
3578 {
3579 return outputVariable.location;
3580 }
3581 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3582 if (apiLocation != -1)
3583 {
3584 return apiLocation;
3585 }
3586 return -1;
3587}
3588
3589bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3590{
3591 if (outputVariable.index != -1)
3592 {
3593 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3594 return (outputVariable.index == 1);
3595 }
3596 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3597 if (apiIndex != -1)
3598 {
3599 // Index layout qualifier from the shader takes precedence, so the index from the API is
3600 // checked only if the index was not set in the shader. This is not specified in the EXT
3601 // spec, but is specified in desktop OpenGL specs.
3602 return (apiIndex == 1);
3603 }
3604 // EXT_blend_func_extended: Outputs get index 0 by default.
3605 return false;
3606}
3607
jchen103fd614d2018-08-13 12:21:58 +08003608bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003609 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003610 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003611 GLuint combinedImageUniformsCount,
3612 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003613{
Jiawei Shao016105b2018-04-12 16:38:31 +08003614 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003615 ASSERT(fragmentShader != nullptr);
3616
Geoff Lange0cff192017-05-30 13:04:56 -04003617 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003618 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003619 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003620
jchen103fd614d2018-08-13 12:21:58 +08003621 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003622 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003623 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003624 {
3625 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3626 outputVariable.name != "gl_FragData")
3627 {
3628 continue;
3629 }
3630
3631 unsigned int baseLocation =
3632 (outputVariable.location == -1 ? 0u
3633 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003634
3635 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3636 // structures, so we may use getBasicTypeElementCount().
3637 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3638 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003639 {
3640 const unsigned int location = baseLocation + elementIndex;
3641 if (location >= mState.mOutputVariableTypes.size())
3642 {
3643 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3644 }
Corentin Walleze7557742017-06-01 13:09:57 -04003645 ASSERT(location < mState.mActiveOutputVariables.size());
3646 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003647 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003648 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003649 }
3650 }
3651
jchen103fd614d2018-08-13 12:21:58 +08003652 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003653 {
3654 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3655 // A link error will be generated if the sum of the number of active image uniforms used in
3656 // all shaders, the number of active shader storage blocks, and the number of active
3657 // fragment shader outputs exceeds the implementation-dependent value of
3658 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3659 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3660 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003661 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003662 {
3663 mInfoLog
3664 << "The sum of the number of active image uniforms, active shader storage blocks "
3665 "and active fragment shader outputs exceeds "
3666 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003667 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003668 return false;
3669 }
3670 }
3671
Jamie Madill80a6fc02015-08-21 16:53:16 -04003672 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003673 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003674 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003675
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003676 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003677 // TODO(jmadill): any caps validation here?
3678
Olli Etuaho0ca09752018-09-24 11:00:50 +03003679 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3680 {
3681 if (outputVariable.isArray())
3682 {
3683 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3684 // Resources and including [0] at the end of array variable names.
3685 outputVariable.name += "[0]";
3686 outputVariable.mappedName += "[0]";
3687 }
3688 }
3689
3690 bool hasSecondaryOutputs = false;
3691
3692 // Reserve locations for output variables whose location is fixed in the shader or through the
3693 // API.
jchen1015015f72017-03-16 13:54:21 +08003694 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003695 outputVariableIndex++)
3696 {
jchen1015015f72017-03-16 13:54:21 +08003697 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003698
3699 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3700 if (outputVariable.isBuiltIn())
3701 continue;
3702
Olli Etuaho0ca09752018-09-24 11:00:50 +03003703 int baseLocation = getOutputLocationForLink(outputVariable);
3704 if (baseLocation == -1)
3705 {
3706 // Here we're only reserving locations for variables whose location is fixed.
3707 continue;
3708 }
3709
3710 auto *outputLocations = &mState.mOutputLocations;
3711 if (isOutputSecondaryForLink(outputVariable))
3712 {
3713 outputLocations = &mState.mSecondaryOutputLocations;
3714 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3715 // an output has an index specified it will always also have the location specified.
3716 hasSecondaryOutputs = true;
3717 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003718
Olli Etuaho465835d2017-09-26 13:34:10 +03003719 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3720 // structures, so we may use getBasicTypeElementCount().
Jamie Madillb980c562018-11-27 11:34:27 -05003721 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003722 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3723 if (outputLocationsNeeded > outputLocations->size())
3724 {
3725 outputLocations->resize(outputLocationsNeeded);
3726 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003727 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003728 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003729 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3730 ASSERT(location < outputLocations->size());
3731 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003732 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003733 mInfoLog << "Location of variable " << outputVariable.name
3734 << " conflicts with another variable.";
3735 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003736 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003737 if (outputVariable.isArray())
3738 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003739 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003740 }
3741 else
3742 {
3743 VariableLocation locationInfo;
Jamie Madillb980c562018-11-27 11:34:27 -05003744 locationInfo.index = outputVariableIndex;
3745 (*outputLocations)[location] = locationInfo;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003746 }
3747 }
3748 }
3749
3750 // Here we assign locations for the output variables that don't yet have them. Note that we're
3751 // not necessarily able to fit the variables optimally, since then we might have to try
3752 // different arrangements of output arrays. Now we just assign the locations in the order that
3753 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3754 // finding locations for the output variables, so this should be acceptable at least for now.
3755 GLuint maxLocation = caps.maxDrawBuffers;
3756 if (hasSecondaryOutputs)
3757 {
3758 // EXT_blend_func_extended: Program outputs will be validated against
3759 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3760 maxLocation = extensions.maxDualSourceDrawBuffers;
3761 }
3762
3763 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3764 outputVariableIndex++)
3765 {
3766 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3767
3768 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3769 if (outputVariable.isBuiltIn())
3770 continue;
3771
3772 if (getOutputLocationForLink(outputVariable) != -1)
3773 {
3774 continue;
3775 }
3776
3777 auto *outputLocations = &mState.mOutputLocations;
3778 if (isOutputSecondaryForLink(outputVariable))
3779 {
3780 outputLocations = &mState.mSecondaryOutputLocations;
3781 }
3782
3783 int baseLocation = 0;
3784 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3785 bool elementsFit = false;
3786 while (!elementsFit)
3787 {
3788 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3789 elementsFit = true;
3790 if (baseLocation + elementCount > maxLocation)
3791 {
3792 // EXT_blend_func_extended: Linking can fail:
3793 // "if the explicit binding assignments do not leave enough space for the linker to
3794 // automatically assign a location for a varying out array, which requires multiple
3795 // contiguous locations."
3796 mInfoLog << "Could not fit output variable into available locations: "
3797 << outputVariable.name;
3798 return false;
3799 }
3800 unsigned int outputLocationsNeeded =
3801 static_cast<unsigned int>(baseLocation) + elementCount;
3802 if (outputLocationsNeeded > outputLocations->size())
3803 {
3804 outputLocations->resize(outputLocationsNeeded);
3805 }
3806 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3807 {
3808 const unsigned int location =
3809 static_cast<unsigned int>(baseLocation) + elementIndex;
3810 ASSERT(location < outputLocations->size());
3811 if (outputLocations->at(location).used())
3812 {
3813 elementsFit = false;
3814 break;
3815 }
3816 }
3817 if (elementsFit)
3818 {
3819 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3820 {
3821 const unsigned int location =
3822 static_cast<unsigned int>(baseLocation) + elementIndex;
3823 if (outputVariable.isArray())
3824 {
3825 (*outputLocations)[location] =
3826 VariableLocation(elementIndex, outputVariableIndex);
3827 }
3828 else
3829 {
3830 VariableLocation locationInfo;
3831 locationInfo.index = outputVariableIndex;
3832 (*outputLocations)[location] = locationInfo;
3833 }
3834 }
3835 }
3836 else
3837 {
3838 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003839 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003840 }
3841 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003842
3843 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003844}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003845
Olli Etuaho48fed632017-03-16 12:05:30 +00003846void Program::setUniformValuesFromBindingQualifiers()
3847{
Jamie Madill982f6e02017-06-07 14:33:04 -04003848 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003849 {
3850 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3851 if (samplerUniform.binding != -1)
3852 {
Olli Etuahod2551232017-10-26 20:03:33 +03003853 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003854 ASSERT(location != -1);
3855 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003856 for (unsigned int elementIndex = 0;
3857 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003858 {
3859 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3860 }
3861 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3862 boundTextureUnits.data());
3863 }
3864 }
3865}
3866
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003867void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003868{
jchen10af713a22017-04-19 09:10:56 +08003869 // Set initial bindings from shader.
3870 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3871 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003872 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003873 bindUniformBlock(blockIndex, uniformBlock.binding);
3874 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003875}
3876
Jamie Madille7d84322017-01-10 18:21:59 -05003877void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003878 GLsizei clampedCount,
3879 const GLint *v)
3880{
Jamie Madill81c2e252017-09-09 23:32:46 -04003881 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
Jamie Madillb980c562018-11-27 11:34:27 -05003882 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003883 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3884 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003885
Jamie Madill7e4eff12018-08-08 15:49:26 -04003886 if (samplerBinding.unreferenced)
3887 return;
3888
3889 // Update the sampler uniforms.
3890 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3891 {
3892 GLint oldSamplerIndex = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3893 GLint newSamplerIndex = v[arrayIndex];
3894
3895 if (oldSamplerIndex == newSamplerIndex)
3896 continue;
3897
3898 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newSamplerIndex;
3899
3900 // Update the reference counts.
3901 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldSamplerIndex];
3902 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newSamplerIndex];
3903 ASSERT(oldRefCount > 0);
3904 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3905 oldRefCount--;
3906 newRefCount++;
3907
3908 // Check for binding type change.
3909 TextureType &newSamplerType = mState.mActiveSamplerTypes[newSamplerIndex];
3910 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldSamplerIndex];
3911
3912 if (newRefCount == 1)
3913 {
3914 newSamplerType = samplerBinding.textureType;
3915 mState.mActiveSamplersMask.set(newSamplerIndex);
3916 }
3917 else if (newSamplerType != samplerBinding.textureType)
3918 {
3919 // Conflict detected. Ensure we reset it properly.
3920 newSamplerType = TextureType::InvalidEnum;
3921 }
3922
3923 // Unset previously active sampler.
3924 if (oldRefCount == 0)
3925 {
3926 oldSamplerType = TextureType::InvalidEnum;
3927 mState.mActiveSamplersMask.reset(oldSamplerIndex);
3928 }
3929 else if (oldSamplerType == TextureType::InvalidEnum)
3930 {
3931 // Previous conflict. Check if this new change fixed the conflict.
3932 oldSamplerType = mState.getSamplerUniformTextureType(oldSamplerIndex);
3933 }
3934 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003935
3936 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003937 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003938}
3939
Jamie Madill7e4eff12018-08-08 15:49:26 -04003940TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3941{
3942 TextureType foundType = TextureType::InvalidEnum;
3943
3944 for (const SamplerBinding &binding : mSamplerBindings)
3945 {
3946 if (binding.unreferenced)
3947 continue;
3948
3949 // A conflict exists if samplers of different types are sourced by the same texture unit.
3950 // We need to check all bound textures to detect this error case.
3951 for (GLuint textureUnit : binding.boundTextureUnits)
3952 {
3953 if (textureUnit == textureUnitIndex)
3954 {
3955 if (foundType == TextureType::InvalidEnum)
3956 {
3957 foundType = binding.textureType;
3958 }
3959 else if (foundType != binding.textureType)
3960 {
3961 return TextureType::InvalidEnum;
3962 }
3963 }
3964 }
3965 }
3966
3967 return foundType;
3968}
3969
Jamie Madille7d84322017-01-10 18:21:59 -05003970template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003971GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3972 GLsizei count,
3973 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003974 const T *v)
3975{
Jamie Madill134f93d2017-08-31 17:11:00 -04003976 if (count == 1)
3977 return 1;
3978
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003979 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003980
Corentin Wallez15ac5342016-11-03 17:06:39 -04003981 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3982 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003983 unsigned int remainingElements =
3984 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003985 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003986 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003987
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003988 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003989 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003990 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003991 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003992
3993 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003994}
3995
3996template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003997GLsizei Program::clampMatrixUniformCount(GLint location,
3998 GLsizei count,
3999 GLboolean transpose,
4000 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004001{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004002 const VariableLocation &locationInfo = mState.mUniformLocations[location];
4003
Jamie Madill62d31cb2015-09-11 13:25:51 -04004004 if (!transpose)
4005 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004006 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004007 }
4008
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004009 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004010
4011 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4012 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004013 unsigned int remainingElements =
4014 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004015 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004016}
4017
Jamie Madill54164b02017-08-28 15:17:37 -04004018// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4019// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004020template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004021void Program::getUniformInternal(const Context *context,
4022 DestT *dataOut,
4023 GLint location,
4024 GLenum nativeType,
4025 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004026{
Jamie Madill54164b02017-08-28 15:17:37 -04004027 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004028 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004029 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004030 {
4031 GLint tempValue[16] = {0};
4032 mProgram->getUniformiv(context, location, tempValue);
4033 UniformStateQueryCastLoop<GLboolean>(
4034 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004035 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004036 }
4037 case GL_INT:
4038 {
4039 GLint tempValue[16] = {0};
4040 mProgram->getUniformiv(context, location, tempValue);
4041 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4042 components);
4043 break;
4044 }
4045 case GL_UNSIGNED_INT:
4046 {
4047 GLuint tempValue[16] = {0};
4048 mProgram->getUniformuiv(context, location, tempValue);
4049 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4050 components);
4051 break;
4052 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004053 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004054 {
4055 GLfloat tempValue[16] = {0};
4056 mProgram->getUniformfv(context, location, tempValue);
4057 UniformStateQueryCastLoop<GLfloat>(
4058 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004059 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004060 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004061 default:
4062 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004063 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004064 }
4065}
Jamie Madilla4595b82017-01-11 17:36:34 -05004066
4067bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4068{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004069 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004070 // Must be called after samplers are validated.
4071 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4072
4073 for (const auto &binding : mState.mSamplerBindings)
4074 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004075 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004076 for (const auto &unit : binding.boundTextureUnits)
4077 {
4078 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4079 if (programTextureID == textureID)
4080 {
4081 // TODO(jmadill): Check for appropriate overlap.
4082 return true;
4083 }
4084 }
4085 }
4086
4087 return false;
4088}
4089
Jamie Madill6f755b22018-10-09 12:48:54 -04004090angle::Result Program::syncState(const Context *context)
Jamie Madill70aeda42018-08-20 12:17:40 -04004091{
4092 if (mDirtyBits.any())
4093 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004094 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004095 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4096 mDirtyBits.reset();
4097 }
4098
Jamie Madill6f755b22018-10-09 12:48:54 -04004099 return angle::Result::Continue();
Jamie Madill70aeda42018-08-20 12:17:40 -04004100}
Jamie Madilla2c74982016-12-12 11:20:42 -05004101} // namespace gl