blob: 31f9367c89ccbebcbcb82cc13a926981d3bd2e10 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
Olli Etuahod2551232017-10-26 20:03:33 +030017#include "common/string_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/MemoryProgramCache.h"
Jamie Madill7af0de52017-11-06 17:09:33 -050022#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill437d2662014-12-05 14:23:35 -050023#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040024#include "libANGLE/Uniform.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040025#include "libANGLE/VaryingPacking.h"
jchen103fd614d2018-08-13 12:21:58 +080026#include "libANGLE/Version.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040028#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040029#include "libANGLE/queryconversions.h"
30#include "libANGLE/renderer/GLImplFactory.h"
31#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040032#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050033
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000036
Geoff Lang7dd2e102014-11-10 15:19:26 -050037namespace
38{
39
Jamie Madill62d31cb2015-09-11 13:25:51 -040040// This simplified cast function doesn't need to worry about advanced concepts like
41// depth range values, or casting to bool.
42template <typename DestT, typename SrcT>
43DestT UniformStateQueryCast(SrcT value);
44
45// From-Float-To-Integer Casts
46template <>
47GLint UniformStateQueryCast(GLfloat value)
48{
49 return clampCast<GLint>(roundf(value));
50}
51
52template <>
53GLuint UniformStateQueryCast(GLfloat value)
54{
55 return clampCast<GLuint>(roundf(value));
56}
57
58// From-Integer-to-Integer Casts
59template <>
60GLint UniformStateQueryCast(GLuint value)
61{
62 return clampCast<GLint>(value);
63}
64
65template <>
66GLuint UniformStateQueryCast(GLint value)
67{
68 return clampCast<GLuint>(value);
69}
70
71// From-Boolean-to-Anything Casts
72template <>
73GLfloat UniformStateQueryCast(GLboolean value)
74{
Geoff Lang92019432017-11-20 13:09:34 -050075 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040076}
77
78template <>
79GLint UniformStateQueryCast(GLboolean value)
80{
Geoff Lang92019432017-11-20 13:09:34 -050081 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040082}
83
84template <>
85GLuint UniformStateQueryCast(GLboolean value)
86{
Geoff Lang92019432017-11-20 13:09:34 -050087 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040088}
89
90// Default to static_cast
91template <typename DestT, typename SrcT>
92DestT UniformStateQueryCast(SrcT value)
93{
94 return static_cast<DestT>(value);
95}
96
97template <typename SrcT, typename DestT>
98void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
99{
100 for (int comp = 0; comp < components; ++comp)
101 {
102 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
103 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
104 size_t offset = comp * 4;
105 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
106 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
107 }
108}
109
jchen1015015f72017-03-16 13:54:21 +0800110template <typename VarT>
111GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
112{
Olli Etuahod2551232017-10-26 20:03:33 +0300113 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800114 for (size_t index = 0; index < list.size(); index++)
115 {
116 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300117 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800118 {
Olli Etuahod2551232017-10-26 20:03:33 +0300119 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800120 }
121 }
122
123 return GL_INVALID_INDEX;
124}
125
Olli Etuahod2551232017-10-26 20:03:33 +0300126template <typename VarT>
127GLint GetVariableLocation(const std::vector<VarT> &list,
128 const std::vector<VariableLocation> &locationList,
129 const std::string &name)
130{
131 size_t nameLengthWithoutArrayIndex;
132 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
133
134 for (size_t location = 0u; location < locationList.size(); ++location)
135 {
136 const VariableLocation &variableLocation = locationList[location];
137 if (!variableLocation.used())
138 {
139 continue;
140 }
141
142 const VarT &variable = list[variableLocation.index];
143
144 if (angle::BeginsWith(variable.name, name))
145 {
146 if (name.length() == variable.name.length())
147 {
148 ASSERT(name == variable.name);
149 // GLES 3.1 November 2016 page 87.
150 // The string exactly matches the name of the active variable.
151 return static_cast<GLint>(location);
152 }
153 if (name.length() + 3u == variable.name.length() && variable.isArray())
154 {
155 ASSERT(name + "[0]" == variable.name);
156 // The string identifies the base name of an active array, where the string would
157 // exactly match the name of the variable if the suffix "[0]" were appended to the
158 // string.
159 return static_cast<GLint>(location);
160 }
161 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300162 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300163 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
164 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
165 {
166 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
167 // The string identifies an active element of the array, where the string ends with the
168 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
169 // zeroes, or whitespace) identifying an array element, and the "]" character, the
170 // integer is less than the number of active elements of the array variable, and where
171 // the string would exactly match the enumerated name of the array if the decimal
172 // integer were replaced with zero.
173 return static_cast<GLint>(location);
174 }
175 }
176
177 return -1;
178}
179
jchen10fd7c3b52017-03-21 15:36:03 +0800180void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
181{
182 ASSERT(bufSize > 0);
183 strncpy(buffer, string.c_str(), bufSize);
184 buffer[bufSize - 1] = '\0';
185
186 if (length)
187 {
188 *length = static_cast<GLsizei>(strlen(buffer));
189 }
190}
191
jchen10a9042d32017-03-17 08:50:45 +0800192bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
193{
Olli Etuahoc8538042017-09-27 11:20:15 +0300194 std::vector<unsigned int> subscripts;
195 std::string baseName = ParseResourceName(name, &subscripts);
196 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800197 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300198 std::vector<unsigned int> arrayIndices;
199 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
200 if (baseName == arrayName &&
201 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800202 {
203 return true;
204 }
205 }
206 return false;
207}
208
Jiawei Shao40786bd2018-04-18 13:58:57 +0800209std::string GetInterfaceBlockLimitName(ShaderType shaderType, sh::BlockType blockType)
210{
211 std::ostringstream stream;
212 stream << "GL_MAX_" << GetShaderTypeString(shaderType) << "_";
213
214 switch (blockType)
215 {
216 case sh::BlockType::BLOCK_UNIFORM:
217 stream << "UNIFORM_BUFFERS";
218 break;
219 case sh::BlockType::BLOCK_BUFFER:
220 stream << "SHADER_STORAGE_BLOCKS";
221 break;
222 default:
223 UNREACHABLE();
224 return "";
225 }
226
227 if (shaderType == ShaderType::Geometry)
228 {
229 stream << "_EXT";
230 }
231
232 return stream.str();
233}
234
235const char *GetInterfaceBlockTypeString(sh::BlockType blockType)
236{
237 switch (blockType)
238 {
239 case sh::BlockType::BLOCK_UNIFORM:
240 return "uniform block";
241 case sh::BlockType::BLOCK_BUFFER:
242 return "shader storage block";
243 default:
244 UNREACHABLE();
245 return "";
246 }
247}
248
249void LogInterfaceBlocksExceedLimit(InfoLog &infoLog,
250 ShaderType shaderType,
251 sh::BlockType blockType,
252 GLuint limit)
253{
254 infoLog << GetShaderTypeString(shaderType) << " shader "
255 << GetInterfaceBlockTypeString(blockType) << " count exceeds "
256 << GetInterfaceBlockLimitName(shaderType, blockType) << " (" << limit << ")";
257}
258
Jiawei Shao427071e2018-03-19 09:21:37 +0800259bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800260 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800261 ShaderType shaderType,
262 sh::BlockType blockType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800263 GLuint *combinedInterfaceBlocksCount,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800264 InfoLog &infoLog)
265{
266 GLuint blockCount = 0;
267 for (const sh::InterfaceBlock &block : interfaceBlocks)
268 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800269 if (IsActiveInterfaceBlock(block))
Jiajia Qin729b2c62017-08-14 09:36:11 +0800270 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800271 blockCount += std::max(block.arraySize, 1u);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800272 if (blockCount > maxInterfaceBlocks)
273 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800274 LogInterfaceBlocksExceedLimit(infoLog, shaderType, blockType, maxInterfaceBlocks);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800275 return false;
276 }
277 }
278 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800279
280 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
281 // If a uniform block is used by multiple shader stages, each such use counts separately
282 // against this combined limit.
283 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
284 // If a shader storage block in a program is referenced by multiple shaders, each such
285 // reference counts separately against this combined limit.
286 if (combinedInterfaceBlocksCount)
287 {
288 *combinedInterfaceBlocksCount += blockCount;
289 }
290
Jiajia Qin729b2c62017-08-14 09:36:11 +0800291 return true;
292}
293
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800294GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
295{
296 std::vector<unsigned int> subscripts;
297 std::string baseName = ParseResourceName(name, &subscripts);
298
299 unsigned int numBlocks = static_cast<unsigned int>(list.size());
300 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
301 {
302 const auto &block = list[blockIndex];
303 if (block.name == baseName)
304 {
305 const bool arrayElementZero =
306 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
307 const bool arrayElementMatches =
308 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
309 if (arrayElementMatches || arrayElementZero)
310 {
311 return blockIndex;
312 }
313 }
314 }
315
316 return GL_INVALID_INDEX;
317}
318
319void GetInterfaceBlockName(const GLuint index,
320 const std::vector<InterfaceBlock> &list,
321 GLsizei bufSize,
322 GLsizei *length,
323 GLchar *name)
324{
325 ASSERT(index < list.size());
326
327 const auto &block = list[index];
328
329 if (bufSize > 0)
330 {
331 std::string blockName = block.name;
332
333 if (block.isArray)
334 {
335 blockName += ArrayString(block.arrayElement);
336 }
337 CopyStringToBuffer(name, blockName, bufSize, length);
338 }
339}
340
jchen103fd614d2018-08-13 12:21:58 +0800341void InitUniformBlockLinker(const ProgramState &state, UniformBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500342{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800343 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500344 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800345 Shader *shader = state.getAttachedShader(shaderType);
346 if (shader)
347 {
jchen103fd614d2018-08-13 12:21:58 +0800348 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800349 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500350 }
351}
352
jchen103fd614d2018-08-13 12:21:58 +0800353void InitShaderStorageBlockLinker(const ProgramState &state, ShaderStorageBlockLinker *blockLinker)
Jamie Madillc9727f32017-11-07 12:37:07 -0500354{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800355 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500356 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800357 Shader *shader = state.getAttachedShader(shaderType);
358 if (shader != nullptr)
359 {
jchen103fd614d2018-08-13 12:21:58 +0800360 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks());
Jiawei Shao385b3e02018-03-21 09:43:28 +0800361 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500362 }
363}
364
jchen108225e732017-11-14 16:29:03 +0800365// Find the matching varying or field by name.
366const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
367 const std::string &name)
368{
369 const sh::ShaderVariable *var = nullptr;
370 for (const auto &ref : varyings)
371 {
372 const sh::Varying *varying = ref.second.get();
373 if (varying->name == name)
374 {
375 var = varying;
376 break;
377 }
378 var = FindShaderVarField(*varying, name);
379 if (var != nullptr)
380 {
381 break;
382 }
383 }
384 return var;
385}
386
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800387void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
388{
389 ASSERT(mismatchedFieldName);
390 if (mismatchedFieldName->empty())
391 {
392 *mismatchedFieldName = parentName;
393 }
394 else
395 {
396 std::ostringstream stream;
397 stream << parentName << "." << *mismatchedFieldName;
398 *mismatchedFieldName = stream.str();
399 }
400}
401
402const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
403{
404 switch (linkError)
405 {
406 case LinkMismatchError::TYPE_MISMATCH:
407 return "Type";
408 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
409 return "Array size";
410 case LinkMismatchError::PRECISION_MISMATCH:
411 return "Precision";
412 case LinkMismatchError::STRUCT_NAME_MISMATCH:
413 return "Structure name";
414 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
415 return "Field number";
416 case LinkMismatchError::FIELD_NAME_MISMATCH:
417 return "Field name";
418
419 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
420 return "Interpolation type";
421 case LinkMismatchError::INVARIANCE_MISMATCH:
422 return "Invariance";
423
424 case LinkMismatchError::BINDING_MISMATCH:
425 return "Binding layout qualifier";
426 case LinkMismatchError::LOCATION_MISMATCH:
427 return "Location layout qualifier";
428 case LinkMismatchError::OFFSET_MISMATCH:
429 return "Offset layout qualilfier";
430
431 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
432 return "Layout qualifier";
433 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
434 return "Matrix Packing";
435 default:
436 UNREACHABLE();
437 return "";
438 }
439}
440
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800441LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
442 const sh::InterfaceBlockField &blockField2,
443 bool webglCompatibility,
444 std::string *mismatchedBlockFieldName)
445{
446 if (blockField1.name != blockField2.name)
447 {
448 return LinkMismatchError::FIELD_NAME_MISMATCH;
449 }
450
451 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
452 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
453 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
454 if (linkError != LinkMismatchError::NO_MISMATCH)
455 {
456 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
457 return linkError;
458 }
459
460 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
461 {
462 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
463 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
464 }
465
466 return LinkMismatchError::NO_MISMATCH;
467}
468
469LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
470 const sh::InterfaceBlock &interfaceBlock2,
471 bool webglCompatibility,
472 std::string *mismatchedBlockFieldName)
473{
474 // validate blocks for the same member types
475 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
476 {
477 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
478 }
479 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
480 {
481 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
482 }
483 if (interfaceBlock1.layout != interfaceBlock2.layout ||
484 interfaceBlock1.binding != interfaceBlock2.binding)
485 {
486 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
487 }
488 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
489 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
490 {
491 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
492 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
493
494 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
495 member1, member2, webglCompatibility, mismatchedBlockFieldName);
496 if (linkError != LinkMismatchError::NO_MISMATCH)
497 {
498 return linkError;
499 }
500 }
501 return LinkMismatchError::NO_MISMATCH;
502}
503
Jiawei Shao40786bd2018-04-18 13:58:57 +0800504using ShaderInterfaceBlock = std::pair<ShaderType, const sh::InterfaceBlock *>;
505using InterfaceBlockMap = std::map<std::string, ShaderInterfaceBlock>;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800506
Jiawei Shao40786bd2018-04-18 13:58:57 +0800507void InitializeInterfaceBlockMap(const std::vector<sh::InterfaceBlock> &interfaceBlocks,
508 ShaderType shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800509 InterfaceBlockMap *linkedInterfaceBlocks)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800510{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800511 ASSERT(linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800512
513 for (const sh::InterfaceBlock &interfaceBlock : interfaceBlocks)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800514 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800515 (*linkedInterfaceBlocks)[interfaceBlock.name] = std::make_pair(shaderType, &interfaceBlock);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800516 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800517}
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800518
Jiawei Shao40786bd2018-04-18 13:58:57 +0800519bool ValidateGraphicsInterfaceBlocksPerShader(
520 const std::vector<sh::InterfaceBlock> &interfaceBlocksToLink,
521 ShaderType shaderType,
522 bool webglCompatibility,
523 InterfaceBlockMap *linkedBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800524 InfoLog &infoLog)
525{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800526 ASSERT(linkedBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800527
528 for (const sh::InterfaceBlock &block : interfaceBlocksToLink)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800529 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800530 const auto &entry = linkedBlocks->find(block.name);
531 if (entry != linkedBlocks->end())
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800532 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800533 const sh::InterfaceBlock &linkedBlock = *(entry->second.second);
534 std::string mismatchedStructFieldName;
535 LinkMismatchError linkError = AreMatchingInterfaceBlocks(
536 block, linkedBlock, webglCompatibility, &mismatchedStructFieldName);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800537 if (linkError != LinkMismatchError::NO_MISMATCH)
538 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800539 LogLinkMismatch(infoLog, block.name, GetInterfaceBlockTypeString(block.blockType),
540 linkError, mismatchedStructFieldName, entry->second.first,
541 shaderType);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800542 return false;
543 }
544 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800545 else
546 {
547 (*linkedBlocks)[block.name] = std::make_pair(shaderType, &block);
548 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800549 }
550
551 return true;
552}
553
Jiawei Shao54aafe52018-04-27 14:54:57 +0800554bool ValidateInterfaceBlocksMatch(
555 GLuint numShadersHasInterfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800556 const ShaderMap<const std::vector<sh::InterfaceBlock> *> &shaderInterfaceBlocks,
557 InfoLog &infoLog,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800558 bool webglCompatibility)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800559{
Jiawei Shao54aafe52018-04-27 14:54:57 +0800560 if (numShadersHasInterfaceBlocks < 2u)
561 {
562 return true;
563 }
564
565 ASSERT(!shaderInterfaceBlocks[ShaderType::Compute]);
566
Jiawei Shao40786bd2018-04-18 13:58:57 +0800567 // Check that interface blocks defined in the graphics shaders are identical
568
569 InterfaceBlockMap linkedInterfaceBlocks;
Jiawei Shao40786bd2018-04-18 13:58:57 +0800570
571 bool interfaceBlockMapInitialized = false;
572 for (ShaderType shaderType : kAllGraphicsShaderTypes)
573 {
574 if (!shaderInterfaceBlocks[shaderType])
575 {
576 continue;
577 }
578
579 if (!interfaceBlockMapInitialized)
580 {
581 InitializeInterfaceBlockMap(*shaderInterfaceBlocks[shaderType], shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800582 &linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800583 interfaceBlockMapInitialized = true;
584 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800585 else if (!ValidateGraphicsInterfaceBlocksPerShader(*shaderInterfaceBlocks[shaderType],
586 shaderType, webglCompatibility,
587 &linkedInterfaceBlocks, infoLog))
Jiawei Shao40786bd2018-04-18 13:58:57 +0800588 {
589 return false;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800590 }
591 }
592
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800593 return true;
594}
595
Jamie Madill62d31cb2015-09-11 13:25:51 -0400596} // anonymous namespace
597
jchen107ae70d82018-07-06 13:47:01 +0800598// Saves the linking context for later use in resolveLink().
599struct Program::LinkingState
600{
601 const Context *context;
602 std::unique_ptr<ProgramLinkedResources> resources;
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -0400603 egl::BlobCache::Key programHash;
jchen107ae70d82018-07-06 13:47:01 +0800604 std::unique_ptr<rx::LinkEvent> linkEvent;
605};
606
Jamie Madill4a3c2342015-10-08 12:58:45 -0400607const char *const g_fakepath = "C:\\fakepath";
608
Jamie Madill3c1da042017-11-27 18:33:40 -0500609// InfoLog implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500610InfoLog::InfoLog() {}
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000611
Jamie Madillb980c562018-11-27 11:34:27 -0500612InfoLog::~InfoLog() {}
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000613
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400614size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000615{
Jamie Madill23176ce2017-07-31 14:14:33 -0400616 if (!mLazyStream)
617 {
618 return 0;
619 }
620
621 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400622 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000623}
624
Geoff Lange1a27752015-10-05 13:16:04 -0400625void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000626{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400627 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000628
629 if (bufSize > 0)
630 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400631 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400632
Jamie Madill23176ce2017-07-31 14:14:33 -0400633 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000634 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400635 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
636 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000637 }
638
639 infoLog[index] = '\0';
640 }
641
642 if (length)
643 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400644 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000645 }
646}
647
648// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300649// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000650// messages, so lets remove all occurrences of this fake file path from the log.
651void InfoLog::appendSanitized(const char *message)
652{
Jamie Madill23176ce2017-07-31 14:14:33 -0400653 ensureInitialized();
654
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000655 std::string msg(message);
656
657 size_t found;
658 do
659 {
660 found = msg.find(g_fakepath);
661 if (found != std::string::npos)
662 {
663 msg.erase(found, strlen(g_fakepath));
664 }
Jamie Madill493f9572018-05-24 19:52:15 -0400665 } while (found != std::string::npos);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000666
Jamie Madill23176ce2017-07-31 14:14:33 -0400667 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000668}
669
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000670void InfoLog::reset()
671{
Jiawei Shao02f15232017-12-27 10:10:28 +0800672 if (mLazyStream)
673 {
674 mLazyStream.reset(nullptr);
675 }
676}
677
678bool InfoLog::empty() const
679{
680 if (!mLazyStream)
681 {
682 return true;
683 }
684
685 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000686}
687
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800688void LogLinkMismatch(InfoLog &infoLog,
689 const std::string &variableName,
690 const char *variableType,
691 LinkMismatchError linkError,
692 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800693 ShaderType shaderType1,
694 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800695{
696 std::ostringstream stream;
697 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
698 << variableName;
699
700 if (!mismatchedStructOrBlockFieldName.empty())
701 {
702 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
703 }
704
705 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
706 << GetShaderTypeString(shaderType2) << " shaders.";
707
708 infoLog << stream.str();
709}
710
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800711bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
712{
713 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200714 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800715}
716
Jamie Madill3c1da042017-11-27 18:33:40 -0500717// VariableLocation implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500718VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false) {}
Geoff Lang7dd2e102014-11-10 15:19:26 -0500719
Olli Etuahoc8538042017-09-27 11:20:15 +0300720VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300721 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500722{
Olli Etuahoc8538042017-09-27 11:20:15 +0300723 ASSERT(arrayIndex != GL_INVALID_INDEX);
724}
725
Jamie Madill3c1da042017-11-27 18:33:40 -0500726// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800727SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500728 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
Jamie Madillb980c562018-11-27 11:34:27 -0500729{}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500730
731SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
732
733SamplerBinding::~SamplerBinding() = default;
734
Jamie Madill3c1da042017-11-27 18:33:40 -0500735// ProgramBindings implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500736ProgramBindings::ProgramBindings() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500737
Jamie Madillb980c562018-11-27 11:34:27 -0500738ProgramBindings::~ProgramBindings() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500739
Jamie Madill3c1da042017-11-27 18:33:40 -0500740void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400741{
742 mBindings[name] = index;
743}
744
Jamie Madill3c1da042017-11-27 18:33:40 -0500745int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400746{
747 auto iter = mBindings.find(name);
748 return (iter != mBindings.end()) ? iter->second : -1;
749}
750
Jamie Madill3c1da042017-11-27 18:33:40 -0500751ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400752{
753 return mBindings.begin();
754}
755
Jamie Madill3c1da042017-11-27 18:33:40 -0500756ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400757{
758 return mBindings.end();
759}
760
Jamie Madill3c1da042017-11-27 18:33:40 -0500761// ImageBinding implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500762ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0), unreferenced(false) {}
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800763ImageBinding::ImageBinding(GLuint imageUnit, size_t count, bool unreferenced)
764 : unreferenced(unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500765{
766 for (size_t index = 0; index < count; ++index)
767 {
768 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
769 }
770}
771
772ImageBinding::ImageBinding(const ImageBinding &other) = default;
773
774ImageBinding::~ImageBinding() = default;
775
Jamie Madill3c1da042017-11-27 18:33:40 -0500776// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400777ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500778 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800779 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500780 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400781 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500782 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800783 mImageUniformRange(0, 0),
784 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300785 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800786 mNumViews(-1),
787 // [GL_EXT_geometry_shader] Table 20.22
Jamie Madill493f9572018-05-24 19:52:15 -0400788 mGeometryShaderInputPrimitiveType(PrimitiveMode::Triangles),
789 mGeometryShaderOutputPrimitiveType(PrimitiveMode::TriangleStrip),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800790 mGeometryShaderInvocations(1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400791 mGeometryShaderMaxVertices(0),
Austin Eng1bf18ce2018-10-19 15:34:02 -0700792 mDrawIDLocation(-1),
Jamie Madill7e4eff12018-08-08 15:49:26 -0400793 mActiveSamplerRefCounts{}
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400794{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300795 mComputeShaderLocalSize.fill(1);
Jamie Madill7e4eff12018-08-08 15:49:26 -0400796 mActiveSamplerTypes.fill(TextureType::InvalidEnum);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400797}
798
Jamie Madill48ef11b2016-04-27 15:21:52 -0400799ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400800{
Jiawei Shao016105b2018-04-12 16:38:31 +0800801 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400802}
803
Jamie Madill48ef11b2016-04-27 15:21:52 -0400804const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500805{
806 return mLabel;
807}
808
Jiawei Shao385b3e02018-03-21 09:43:28 +0800809Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
810{
Jiawei Shao016105b2018-04-12 16:38:31 +0800811 ASSERT(shaderType != ShaderType::InvalidEnum);
812 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800813}
814
Jamie Madille7d84322017-01-10 18:21:59 -0500815GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816{
jchen1015015f72017-03-16 13:54:21 +0800817 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400818}
819
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800820GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
821{
822 return GetResourceIndexFromName(mBufferVariables, name);
823}
824
Jamie Madille7d84322017-01-10 18:21:59 -0500825GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
826{
827 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
828 return mUniformLocations[location].index;
829}
830
831Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
832{
833 GLuint index = getUniformIndexFromLocation(location);
834 if (!isSamplerUniformIndex(index))
835 {
836 return Optional<GLuint>::Invalid();
837 }
838
839 return getSamplerIndexFromUniformIndex(index);
840}
841
842bool ProgramState::isSamplerUniformIndex(GLuint index) const
843{
Jamie Madill982f6e02017-06-07 14:33:04 -0400844 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500845}
846
847GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
848{
849 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400850 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500851}
852
Qin Jiajia47f6dd02018-08-10 13:36:32 +0800853bool ProgramState::isImageUniformIndex(GLuint index) const
854{
855 return mImageUniformRange.contains(index);
856}
857
858GLuint ProgramState::getImageIndexFromUniformIndex(GLuint uniformIndex) const
859{
860 ASSERT(isImageUniformIndex(uniformIndex));
861 return uniformIndex - mImageUniformRange.low();
862}
863
Jamie Madill34ca4f52017-06-13 11:49:39 -0400864GLuint ProgramState::getAttributeLocation(const std::string &name) const
865{
866 for (const sh::Attribute &attribute : mAttributes)
867 {
868 if (attribute.name == name)
869 {
870 return attribute.location;
871 }
872 }
873
874 return static_cast<GLuint>(-1);
875}
876
Jiawei Shao016105b2018-04-12 16:38:31 +0800877bool ProgramState::hasAttachedShader() const
878{
879 for (const Shader *shader : mAttachedShaders)
880 {
881 if (shader)
882 {
883 return true;
884 }
885 }
886 return false;
887}
888
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500889Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400890 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400891 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892 mLinked(false),
jchen105055fba2018-08-17 09:57:07 +0800893 mLinkResolved(true),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894 mDeleteStatus(false),
895 mRefCount(0),
896 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500897 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898{
899 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000900
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
904Program::~Program()
905{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400906 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
Jamie Madill4928b7c2017-06-20 12:57:39 -0400909void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500910{
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400911 resolveLink(context);
Jiawei Shao016105b2018-04-12 16:38:31 +0800912 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500913 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800914 if (mState.mAttachedShaders[shaderType])
915 {
916 mState.mAttachedShaders[shaderType]->release(context);
917 mState.mAttachedShaders[shaderType] = nullptr;
918 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800919 }
920
Jamie Madillf4a789f2018-10-18 16:56:20 -0400921 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400922
Jiawei Shao016105b2018-04-12 16:38:31 +0800923 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400924 SafeDelete(mProgram);
925
926 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500927}
jchen107ae70d82018-07-06 13:47:01 +0800928GLuint Program::id() const
929{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400930 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +0800931 return mHandle;
932}
Jamie Madill6c1f6712017-02-14 19:08:04 -0500933
Jamie Madille90d4ee2018-11-28 14:04:00 -0500934void Program::setLabel(const Context *context, const std::string &label)
Geoff Lang70d0f492015-12-10 17:45:46 -0500935{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400936 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400937 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500938}
939
940const std::string &Program::getLabel() const
941{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400942 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500944}
945
Jamie Madillef300b12016-10-07 15:12:09 -0400946void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400948 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800949 ShaderType shaderType = shader->getType();
950 ASSERT(shaderType != ShaderType::InvalidEnum);
951
952 mState.mAttachedShaders[shaderType] = shader;
953 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
Jamie Madillc1d770e2017-04-13 17:31:24 -0400956void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400958 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800959 ShaderType shaderType = shader->getType();
960 ASSERT(shaderType != ShaderType::InvalidEnum);
961
962 ASSERT(mState.mAttachedShaders[shaderType] == shader);
963 shader->release(context);
964 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000967int Program::getAttachedShadersCount() const
968{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400969 ASSERT(mLinkResolved);
Jiawei Shao016105b2018-04-12 16:38:31 +0800970 int numAttachedShaders = 0;
971 for (const Shader *shader : mState.mAttachedShaders)
972 {
973 if (shader)
974 {
975 ++numAttachedShaders;
976 }
977 }
978
979 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000980}
981
Jiawei Shao385b3e02018-03-21 09:43:28 +0800982const Shader *Program::getAttachedShader(ShaderType shaderType) const
983{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400984 ASSERT(mLinkResolved);
Jiawei Shao385b3e02018-03-21 09:43:28 +0800985 return mState.getAttachedShader(shaderType);
986}
987
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988void Program::bindAttributeLocation(GLuint index, const char *name)
989{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400990 ASSERT(mLinkResolved);
Geoff Langd8605522016-04-13 10:19:12 -0400991 mAttributeBindings.bindLocation(index, name);
992}
993
994void Program::bindUniformLocation(GLuint index, const char *name)
995{
Jamie Madill44a6fbf2018-10-02 13:38:56 -0400996 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +0300997 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
Sami Väisänen46eaa942016-06-29 10:26:37 +03001000void Program::bindFragmentInputLocation(GLint index, const char *name)
1001{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001002 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001003 mFragmentInputBindings.bindLocation(index, name);
1004}
1005
Olli Etuaho0ca09752018-09-24 11:00:50 +03001006void Program::bindFragmentOutputLocation(GLuint index, const char *name)
1007{
1008 mFragmentOutputLocations.bindLocation(index, name);
1009}
1010
1011void Program::bindFragmentOutputIndex(GLuint index, const char *name)
1012{
1013 mFragmentOutputIndexes.bindLocation(index, name);
1014}
1015
jchen103fd614d2018-08-13 12:21:58 +08001016BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +03001017{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001018 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001019 BindingInfo ret;
1020 ret.type = GL_NONE;
1021 ret.valid = false;
1022
Jiawei Shao385b3e02018-03-21 09:43:28 +08001023 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001024 ASSERT(fragmentShader);
1025
1026 // Find the actual fragment shader varying we're interested in
jchen103fd614d2018-08-13 12:21:58 +08001027 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings();
Sami Väisänen46eaa942016-06-29 10:26:37 +03001028
1029 for (const auto &binding : mFragmentInputBindings)
1030 {
1031 if (binding.second != static_cast<GLuint>(index))
1032 continue;
1033
1034 ret.valid = true;
1035
Olli Etuahod2551232017-10-26 20:03:33 +03001036 size_t nameLengthWithoutArrayIndex;
1037 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001038
1039 for (const auto &in : inputs)
1040 {
Olli Etuahod2551232017-10-26 20:03:33 +03001041 if (in.name.length() == nameLengthWithoutArrayIndex &&
1042 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001043 {
1044 if (in.isArray())
1045 {
1046 // The client wants to bind either "name" or "name[0]".
1047 // GL ES 3.1 spec refers to active array names with language such as:
1048 // "if the string identifies the base name of an active array, where the
1049 // string would exactly match the name of the variable if the suffix "[0]"
1050 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001051 if (arrayIndex == GL_INVALID_INDEX)
1052 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001053
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001054 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001055 }
1056 else
1057 {
1058 ret.name = in.mappedName;
1059 }
1060 ret.type = in.type;
1061 return ret;
1062 }
1063 }
1064 }
1065
1066 return ret;
1067}
1068
jchen103fd614d2018-08-13 12:21:58 +08001069void Program::pathFragmentInputGen(GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001070 GLenum genMode,
1071 GLint components,
1072 const GLfloat *coeffs)
1073{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001074 ASSERT(mLinkResolved);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001075 // If the location is -1 then the command is silently ignored
1076 if (index == -1)
1077 return;
1078
jchen103fd614d2018-08-13 12:21:58 +08001079 const auto &binding = getFragmentInputBindingInfo(index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001080
1081 // If the input doesn't exist then then the command is silently ignored
1082 // This could happen through optimization for example, the shader translator
1083 // decides that a variable is not actually being used and optimizes it away.
1084 if (binding.name.empty())
1085 return;
1086
1087 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1088}
1089
Martin Radev4c4c8e72016-08-04 12:25:34 +03001090// The attached shaders are checked for linking errors by matching up their variables.
1091// Uniform, input and output variables get collected.
1092// The code gets compiled into binaries.
Jamie Madillf4a789f2018-10-18 16:56:20 -04001093angle::Result Program::link(const Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001094{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001095 const auto &data = context->getContextState();
1096
Jamie Madill6c58b062017-08-01 13:44:25 -04001097 auto *platform = ANGLEPlatformCurrent();
1098 double startTime = platform->currentTime(platform);
1099
Jamie Madill6c1f6712017-02-14 19:08:04 -05001100 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001101 mInfoLog.reset();
1102
1103 // Validate we have properly attached shaders before checking the cache.
jchen103fd614d2018-08-13 12:21:58 +08001104 if (!linkValidateShaders(mInfoLog))
Jamie Madill6bc264a2018-03-31 15:36:05 -04001105 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001106 return angle::Result::Continue;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001107 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108
Shahbaz Youssefi9137ade2018-08-27 14:22:37 -04001109 egl::BlobCache::Key programHash = {0};
Jamie Madillb980c562018-11-27 11:34:27 -05001110 MemoryProgramCache *cache = context->getMemoryProgramCache();
jchen107ae70d82018-07-06 13:47:01 +08001111
Jamie Madill32447362017-06-28 14:53:52 -04001112 if (cache)
1113 {
Jamie Madill785e8a02018-10-04 17:42:00 -04001114 angle::Result result = cache->getProgram(context, this, &mState, &programHash);
Jamie Madill7c985f52018-11-29 18:16:17 -05001115 mLinked = (result == angle::Result::Continue);
Jamie Madill785e8a02018-10-04 17:42:00 -04001116 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001117 }
1118
1119 if (mLinked)
1120 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001121 double delta = platform->currentTime(platform) - startTime;
1122 int us = static_cast<int>(delta * 1000000.0);
1123 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill7c985f52018-11-29 18:16:17 -05001124 return angle::Result::Continue;
Jamie Madill32447362017-06-28 14:53:52 -04001125 }
1126
1127 // Cache load failed, fall through to normal linking.
1128 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001129
Jamie Madill6bc264a2018-03-31 15:36:05 -04001130 // Re-link shaders after the unlink call.
jchen103fd614d2018-08-13 12:21:58 +08001131 ASSERT(linkValidateShaders(mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001132
jchen107ae70d82018-07-06 13:47:01 +08001133 std::unique_ptr<ProgramLinkedResources> resources;
Jiawei Shao016105b2018-04-12 16:38:31 +08001134 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001135 {
jchen107ae70d82018-07-06 13:47:01 +08001136 resources.reset(
1137 new ProgramLinkedResources{{0, PackMode::ANGLE_RELAXED},
1138 {&mState.mUniformBlocks, &mState.mUniforms},
1139 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1140 {&mState.mAtomicCounterBuffers},
1141 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001142
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001143 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001144 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1145 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001146 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001147 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001148 }
1149
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001150 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001151 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1152 context->getExtensions().webglCompatibility, mInfoLog,
1153 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001154 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001155 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001156 }
1157
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001158 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
1159 // A link error will be generated if the sum of the number of active image uniforms used in
1160 // all shaders, the number of active shader storage blocks, and the number of active
1161 // fragment shader outputs exceeds the implementation-dependent value of
1162 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
1163 if (combinedImageUniforms + combinedShaderStorageBlocks >
1164 context->getCaps().maxCombinedShaderOutputResources)
1165 {
1166 mInfoLog
1167 << "The sum of the number of active image uniforms, active shader storage blocks "
1168 "and active fragment shader outputs exceeds "
1169 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
1170 << context->getCaps().maxCombinedShaderOutputResources << ")";
Jamie Madill7c985f52018-11-29 18:16:17 -05001171 return angle::Result::Continue;
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001172 }
1173
jchen103fd614d2018-08-13 12:21:58 +08001174 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1175 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001176 }
1177 else
1178 {
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001179 // Map the varyings to the register file
1180 // In WebGL, we use a slightly different handling for packing variables.
1181 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1182 if (data.getLimitations().noFlexibleVaryingPacking)
1183 {
1184 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1185 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1186 }
1187 else if (data.getExtensions().webglCompatibility)
1188 {
1189 packMode = PackMode::WEBGL_STRICT;
1190 }
1191
jchen107ae70d82018-07-06 13:47:01 +08001192 resources.reset(
1193 new ProgramLinkedResources{{data.getCaps().maxVaryingVectors, packMode},
1194 {&mState.mUniformBlocks, &mState.mUniforms},
1195 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1196 {&mState.mAtomicCounterBuffers},
1197 {}});
Luc Ferrone17b5ba2018-06-04 14:28:58 -04001198
jchen103fd614d2018-08-13 12:21:58 +08001199 if (!linkAttributes(context->getCaps(), mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001200 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001201 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001202 }
1203
jchen103fd614d2018-08-13 12:21:58 +08001204 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001205 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001206 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001207 }
1208
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001209 GLuint combinedImageUniforms = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001210 if (!linkUniforms(context->getCaps(), mInfoLog, mUniformLocationBindings,
1211 &combinedImageUniforms, &resources->unusedUniforms))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001212 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001213 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001214 }
1215
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001216 GLuint combinedShaderStorageBlocks = 0u;
jchen103fd614d2018-08-13 12:21:58 +08001217 if (!linkInterfaceBlocks(context->getCaps(), context->getClientVersion(),
1218 context->getExtensions().webglCompatibility, mInfoLog,
1219 &combinedShaderStorageBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001220 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001221 return angle::Result::Continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001222 }
1223
jchen103fd614d2018-08-13 12:21:58 +08001224 if (!linkValidateGlobalNames(mInfoLog))
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001225 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001226 return angle::Result::Continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001227 }
1228
Olli Etuaho0ca09752018-09-24 11:00:50 +03001229 if (!linkOutputVariables(context->getCaps(), context->getExtensions(),
1230 context->getClientVersion(), combinedImageUniforms,
1231 combinedShaderStorageBlocks))
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001232 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001233 return angle::Result::Continue;
Jiawei Shao7a8fe152018-04-28 12:59:58 +08001234 }
1235
jchen103fd614d2018-08-13 12:21:58 +08001236 const auto &mergedVaryings = getMergedVaryings();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001237
Jiawei Shao016105b2018-04-12 16:38:31 +08001238 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
jchen103fd614d2018-08-13 12:21:58 +08001239 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews();
Martin Radev7cf61662017-07-26 17:10:53 +03001240
jchen103fd614d2018-08-13 12:21:58 +08001241 InitUniformBlockLinker(mState, &resources->uniformBlockLinker);
1242 InitShaderStorageBlockLinker(mState, &resources->shaderStorageBlockLinker);
Jamie Madillc9727f32017-11-07 12:37:07 -05001243
jchen103fd614d2018-08-13 12:21:58 +08001244 if (!linkValidateTransformFeedback(context->getClientVersion(), mInfoLog, mergedVaryings,
1245 context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001246 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001247 return angle::Result::Continue;
Jamie Madill192745a2016-12-22 15:58:21 -05001248 }
1249
jchen107ae70d82018-07-06 13:47:01 +08001250 if (!resources->varyingPacking.collectAndPackUserVaryings(
jchen1085c93c42017-11-12 15:36:47 +08001251 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001252 {
Jamie Madill7c985f52018-11-29 18:16:17 -05001253 return angle::Result::Continue;
Olli Etuaho39e78122017-08-29 14:34:22 +03001254 }
1255
Martin Radev4c4c8e72016-08-04 12:25:34 +03001256 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001257 }
1258
jchen107ae70d82018-07-06 13:47:01 +08001259 mLinkingState.reset(new LinkingState());
1260 mLinkingState->context = context;
1261 mLinkingState->programHash = programHash;
1262 mLinkingState->linkEvent = mProgram->link(context, *resources, mInfoLog);
1263 mLinkingState->resources = std::move(resources);
jchen105055fba2018-08-17 09:57:07 +08001264 mLinkResolved = false;
jchen107ae70d82018-07-06 13:47:01 +08001265
Jamie Madill7c985f52018-11-29 18:16:17 -05001266 return angle::Result::Continue;
jchen107ae70d82018-07-06 13:47:01 +08001267}
1268
1269bool Program::isLinking() const
1270{
1271 return (mLinkingState.get() && mLinkingState->linkEvent->isLinking());
1272}
1273
Jamie Madill785e8a02018-10-04 17:42:00 -04001274void Program::resolveLinkImpl(const Context *context)
jchen107ae70d82018-07-06 13:47:01 +08001275{
1276 ASSERT(mLinkingState.get());
1277
Jamie Madill785e8a02018-10-04 17:42:00 -04001278 angle::Result result = mLinkingState->linkEvent->wait(context);
1279
Jamie Madill7c985f52018-11-29 18:16:17 -05001280 mLinked = result == angle::Result::Continue;
jchen105055fba2018-08-17 09:57:07 +08001281 mLinkResolved = true;
jchen107ae70d82018-07-06 13:47:01 +08001282 auto linkingState = std::move(mLinkingState);
1283 if (!mLinked)
1284 {
1285 return;
1286 }
1287
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001288 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001289
Yunchao Heece12532017-11-21 15:50:21 +08001290 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1291 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001292 ASSERT(mLinked);
1293 updateLinkedShaderStages();
1294
Jamie Madill54164b02017-08-28 15:17:37 -04001295 // Mark implementation-specific unreferenced uniforms as ignored.
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001296 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings,
1297 &mState.mImageBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001298
Jamie Madill7e4eff12018-08-08 15:49:26 -04001299 // Must be called after markUnusedUniformLocations.
1300 mState.updateActiveSamplers();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001301 mState.updateActiveImages();
Jamie Madill7e4eff12018-08-08 15:49:26 -04001302
1303 setUniformValuesFromBindingQualifiers();
1304
Austin Eng1bf18ce2018-10-19 15:34:02 -07001305 if (context->getExtensions().multiDraw)
1306 {
1307 mState.mDrawIDLocation = getUniformLocation("gl_DrawID");
1308 }
1309
Jamie Madill32447362017-06-28 14:53:52 -04001310 // Save to the program cache.
jchen107ae70d82018-07-06 13:47:01 +08001311 auto *cache = linkingState->context->getMemoryProgramCache();
1312 if (cache &&
1313 (mState.mLinkedTransformFeedbackVaryings.empty() ||
1314 !linkingState->context->getWorkarounds().disableProgramCachingForTransformFeedback))
Jamie Madill32447362017-06-28 14:53:52 -04001315 {
jchen107ae70d82018-07-06 13:47:01 +08001316 cache->putProgram(linkingState->programHash, linkingState->context, this);
Jamie Madill32447362017-06-28 14:53:52 -04001317 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001318}
1319
Yunchao He85072e82017-11-14 15:43:28 +08001320void Program::updateLinkedShaderStages()
1321{
Yunchao Heece12532017-11-21 15:50:21 +08001322 mState.mLinkedShaderStages.reset();
1323
Jiawei Shao016105b2018-04-12 16:38:31 +08001324 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001325 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001326 if (shader)
1327 {
1328 mState.mLinkedShaderStages.set(shader->getType());
1329 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001330 }
Yunchao He85072e82017-11-14 15:43:28 +08001331}
1332
James Darpinian30b604d2018-03-12 17:26:57 -07001333void ProgramState::updateTransformFeedbackStrides()
1334{
1335 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1336 {
1337 mTransformFeedbackStrides.resize(1);
1338 size_t totalSize = 0;
1339 for (auto &varying : mLinkedTransformFeedbackVaryings)
1340 {
1341 totalSize += varying.size() * VariableExternalSize(varying.type);
1342 }
1343 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1344 }
1345 else
1346 {
1347 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1348 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1349 {
1350 auto &varying = mLinkedTransformFeedbackVaryings[i];
1351 mTransformFeedbackStrides[i] =
1352 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1353 }
1354 }
1355}
1356
Jamie Madill7e4eff12018-08-08 15:49:26 -04001357void ProgramState::updateActiveSamplers()
1358{
1359 for (SamplerBinding &samplerBinding : mSamplerBindings)
1360 {
1361 if (samplerBinding.unreferenced)
1362 continue;
1363
1364 for (GLint textureUnit : samplerBinding.boundTextureUnits)
1365 {
1366 mActiveSamplerRefCounts[textureUnit]++;
1367 mActiveSamplerTypes[textureUnit] = getSamplerUniformTextureType(textureUnit);
1368 mActiveSamplersMask.set(textureUnit);
1369 }
1370 }
1371}
1372
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001373void ProgramState::updateActiveImages()
1374{
1375 for (ImageBinding &imageBinding : mImageBindings)
1376 {
1377 if (imageBinding.unreferenced)
1378 continue;
1379
1380 for (GLint imageUnit : imageBinding.boundImageUnits)
1381 {
1382 mActiveImagesMask.set(imageUnit);
1383 }
1384 }
1385}
1386
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001387// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001388void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001389{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001390 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001391 mState.mAttributesTypeMask.reset();
1392 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001393 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001394 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001395 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001396 mState.mUniforms.clear();
1397 mState.mUniformLocations.clear();
1398 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001399 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001400 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001401 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001402 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001403 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001404 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001405 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001406 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001407 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001408 mState.mImageBindings.clear();
Qin Jiajia47f6dd02018-08-10 13:36:32 +08001409 mState.mActiveImagesMask.reset();
Jamie Madill493f9572018-05-24 19:52:15 -04001410 mState.mNumViews = -1;
1411 mState.mGeometryShaderInputPrimitiveType = PrimitiveMode::Triangles;
1412 mState.mGeometryShaderOutputPrimitiveType = PrimitiveMode::TriangleStrip;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001413 mState.mGeometryShaderInvocations = 1;
1414 mState.mGeometryShaderMaxVertices = 0;
Austin Eng1bf18ce2018-10-19 15:34:02 -07001415 mState.mDrawIDLocation = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417 mValidated = false;
1418
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001419 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001420 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001421}
1422
Jamie Madillf4a789f2018-10-18 16:56:20 -04001423angle::Result Program::loadBinary(const Context *context,
1424 GLenum binaryFormat,
1425 const void *binary,
1426 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001427{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001428 ASSERT(mLinkResolved);
Jamie Madill6c1f6712017-02-14 19:08:04 -05001429 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001430
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
Jamie Madill7c985f52018-11-29 18:16:17 -05001432 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001434 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1435 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001436 {
Jamie Madillf6113162015-05-07 11:49:21 -04001437 mInfoLog << "Invalid program binary format.";
Jamie Madill7c985f52018-11-29 18:16:17 -05001438 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439 }
1440
Jamie Madill4f86d052017-06-05 12:59:26 -04001441 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
Jamie Madill785e8a02018-10-04 17:42:00 -04001442 angle::Result result =
1443 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog);
Jamie Madill7c985f52018-11-29 18:16:17 -05001444 mLinked = result == angle::Result::Continue;
Jamie Madill785e8a02018-10-04 17:42:00 -04001445 ANGLE_TRY(result);
Jamie Madill32447362017-06-28 14:53:52 -04001446
1447 // Currently we require the full shader text to compute the program hash.
Jamie Madill785e8a02018-10-04 17:42:00 -04001448 // We could also store the binary in the internal program cache.
Jamie Madill32447362017-06-28 14:53:52 -04001449
Jamie Madill70aeda42018-08-20 12:17:40 -04001450 for (size_t uniformBlockIndex = 0; uniformBlockIndex < mState.mUniformBlocks.size();
1451 ++uniformBlockIndex)
1452 {
1453 mDirtyBits.set(uniformBlockIndex);
1454 }
1455
Jamie Madill7c985f52018-11-29 18:16:17 -05001456 return angle::Result::Continue;
Jamie Madilla2c74982016-12-12 11:20:42 -05001457#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458}
1459
Jamie Madillf4a789f2018-10-18 16:56:20 -04001460angle::Result Program::saveBinary(Context *context,
1461 GLenum *binaryFormat,
1462 void *binary,
1463 GLsizei bufSize,
1464 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001466 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 if (binaryFormat)
1468 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001469 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470 }
1471
Jamie Madill4f86d052017-06-05 12:59:26 -04001472 angle::MemoryBuffer memoryBuf;
1473 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474
Jamie Madill4f86d052017-06-05 12:59:26 -04001475 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1476 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477
1478 if (streamLength > bufSize)
1479 {
1480 if (length)
1481 {
1482 *length = 0;
1483 }
1484
Jamie Madill493f9572018-05-24 19:52:15 -04001485 // TODO: This should be moved to the validation layer but computing the size of the binary
1486 // before saving it causes the save to happen twice. It may be possible to write the binary
1487 // to a separate buffer, validate sizes and then copy it.
Jamie Madillf4a789f2018-10-18 16:56:20 -04001488 ANGLE_CHECK(context, false, "Insufficient buffer size", GL_INVALID_OPERATION);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489 }
1490
1491 if (binary)
1492 {
Jamie Madill493f9572018-05-24 19:52:15 -04001493 char *ptr = reinterpret_cast<char *>(binary);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494
Jamie Madill48ef11b2016-04-27 15:21:52 -04001495 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496 ptr += streamLength;
1497
1498 ASSERT(ptr - streamLength == binary);
1499 }
1500
1501 if (length)
1502 {
1503 *length = streamLength;
1504 }
1505
Jamie Madill7c985f52018-11-29 18:16:17 -05001506 return angle::Result::Continue;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001507}
1508
Jamie Madillf4a789f2018-10-18 16:56:20 -04001509GLint Program::getBinaryLength(Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001511 ASSERT(mLinkResolved);
Geoff Langb26ab822018-05-29 11:19:00 -04001512 if (!mLinked)
1513 {
1514 return 0;
1515 }
1516
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517 GLint length;
Jamie Madillf4a789f2018-10-18 16:56:20 -04001518 angle::Result result =
1519 saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Jamie Madill7c985f52018-11-29 18:16:17 -05001520 if (result != angle::Result::Continue)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521 {
1522 return 0;
1523 }
1524
1525 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001526}
1527
Geoff Langc5629752015-12-07 16:29:04 -05001528void Program::setBinaryRetrievableHint(bool retrievable)
1529{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001530 ASSERT(mLinkResolved);
Geoff Langc5629752015-12-07 16:29:04 -05001531 // TODO(jmadill) : replace with dirty bits
1532 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001533 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001534}
1535
1536bool Program::getBinaryRetrievableHint() const
1537{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001538 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001539 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001540}
1541
Yunchao He61afff12017-03-14 15:34:03 +08001542void Program::setSeparable(bool separable)
1543{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001544 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001545 // TODO(yunchao) : replace with dirty bits
1546 if (mState.mSeparable != separable)
1547 {
1548 mProgram->setSeparable(separable);
1549 mState.mSeparable = separable;
1550 }
1551}
1552
1553bool Program::isSeparable() const
1554{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001555 ASSERT(mLinkResolved);
Yunchao He61afff12017-03-14 15:34:03 +08001556 return mState.mSeparable;
1557}
1558
Jamie Madill956ab4d2018-10-10 16:13:03 -04001559void Program::deleteSelf(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001560{
Jamie Madill956ab4d2018-10-10 16:13:03 -04001561 ASSERT(mRefCount == 0 && mDeleteStatus);
1562 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001563}
1564
1565unsigned int Program::getRefCount() const
1566{
1567 return mRefCount;
1568}
1569
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001570int Program::getInfoLogLength() const
1571{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001572 ASSERT(mLinkResolved);
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001573 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001574}
1575
Geoff Lange1a27752015-10-05 13:16:04 -04001576void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001577{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001578 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001579 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001580}
1581
Geoff Lange1a27752015-10-05 13:16:04 -04001582void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001583{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001584 ASSERT(mLinkResolved);
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001585 int total = 0;
1586
Jiawei Shao016105b2018-04-12 16:38:31 +08001587 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001588 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001589 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001590 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001591 shaders[total] = shader->getHandle();
1592 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001593 }
1594 }
1595
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001596 if (count)
1597 {
1598 *count = total;
1599 }
1600}
1601
Geoff Lange1a27752015-10-05 13:16:04 -04001602GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001604 ASSERT(mLinkResolved);
Jamie Madill34ca4f52017-06-13 11:49:39 -04001605 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606}
1607
Jamie Madill63805b42015-08-25 13:17:39 -04001608bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001609{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001610 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001611 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1612 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001613}
1614
jchen10fd7c3b52017-03-21 15:36:03 +08001615void Program::getActiveAttribute(GLuint index,
1616 GLsizei bufsize,
1617 GLsizei *length,
1618 GLint *size,
1619 GLenum *type,
1620 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001621{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001622 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001623 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001624 {
1625 if (bufsize > 0)
1626 {
1627 name[0] = '\0';
1628 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001630 if (length)
1631 {
1632 *length = 0;
1633 }
1634
1635 *type = GL_NONE;
1636 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001637 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001638 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001639
jchen1036e120e2017-03-14 14:53:58 +08001640 ASSERT(index < mState.mAttributes.size());
1641 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001642
1643 if (bufsize > 0)
1644 {
jchen10fd7c3b52017-03-21 15:36:03 +08001645 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001646 }
1647
1648 // Always a single 'type' instance
1649 *size = 1;
1650 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001651}
1652
Geoff Lange1a27752015-10-05 13:16:04 -04001653GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001654{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001655 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001656 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001657 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001658 return 0;
1659 }
1660
jchen1036e120e2017-03-14 14:53:58 +08001661 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001662}
1663
Geoff Lange1a27752015-10-05 13:16:04 -04001664GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001665{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001666 ASSERT(mLinkResolved);
Jamie Madillc349ec02015-08-21 16:53:12 -04001667 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001668 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001669 return 0;
1670 }
1671
1672 size_t maxLength = 0;
1673
Jamie Madill48ef11b2016-04-27 15:21:52 -04001674 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001675 {
jchen1036e120e2017-03-14 14:53:58 +08001676 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001677 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678
Jamie Madillc349ec02015-08-21 16:53:12 -04001679 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680}
1681
jchen107ae70d82018-07-06 13:47:01 +08001682const std::vector<sh::Attribute> &Program::getAttributes() const
1683{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001684 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001685 return mState.mAttributes;
1686}
1687
jchen107ae70d82018-07-06 13:47:01 +08001688const std::vector<SamplerBinding> &Program::getSamplerBindings() const
1689{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001690 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001691 return mState.mSamplerBindings;
1692}
1693
jchen107ae70d82018-07-06 13:47:01 +08001694const sh::WorkGroupSize &Program::getComputeShaderLocalSize() const
1695{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001696 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001697 return mState.mComputeShaderLocalSize;
1698}
1699
1700PrimitiveMode Program::getGeometryShaderInputPrimitiveType() const
1701{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001702 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001703 return mState.mGeometryShaderInputPrimitiveType;
1704}
1705PrimitiveMode Program::getGeometryShaderOutputPrimitiveType() const
1706{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001707 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001708 return mState.mGeometryShaderOutputPrimitiveType;
1709}
1710GLint Program::getGeometryShaderInvocations() const
1711{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001712 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001713 return mState.mGeometryShaderInvocations;
1714}
1715GLint Program::getGeometryShaderMaxVertices() const
1716{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001717 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001718 return mState.mGeometryShaderMaxVertices;
1719}
1720
jchen1015015f72017-03-16 13:54:21 +08001721GLuint Program::getInputResourceIndex(const GLchar *name) const
1722{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001723 ASSERT(mLinkResolved);
Olli Etuahod2551232017-10-26 20:03:33 +03001724 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001725}
1726
1727GLuint Program::getOutputResourceIndex(const GLchar *name) const
1728{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001729 ASSERT(mLinkResolved);
jchen1015015f72017-03-16 13:54:21 +08001730 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1731}
1732
jchen10fd7c3b52017-03-21 15:36:03 +08001733size_t Program::getOutputResourceCount() const
1734{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001735 ASSERT(mLinkResolved);
jchen10fd7c3b52017-03-21 15:36:03 +08001736 return (mLinked ? mState.mOutputVariables.size() : 0);
1737}
1738
jchen107ae70d82018-07-06 13:47:01 +08001739const std::vector<GLenum> &Program::getOutputVariableTypes() const
1740{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04001741 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08001742 return mState.mOutputVariableTypes;
1743}
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 Madille3e680c2018-12-03 17:49:08 -05002055void Program::setUniform1iv(Context *context, 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 {
Jamie Madille3e680c2018-12-03 17:49:08 -05002065 updateSamplerUniform(context, locationInfo, clampedCount, v);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002066 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067}
2068
2069void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
2070{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002071 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002072 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2073 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002074 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075}
2076
2077void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
2078{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002079 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002080 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2081 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002082 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083}
2084
2085void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
2086{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002087 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002088 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2089 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002090 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091}
2092
2093void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
2094{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002095 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002096 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2097 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002098 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099}
2100
2101void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
2102{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002103 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002104 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2105 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002106 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107}
2108
2109void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
2110{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002111 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002112 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2113 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002114 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115}
2116
2117void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
2118{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002119 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002120 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2121 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002122 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123}
2124
Jamie Madill493f9572018-05-24 19:52:15 -04002125void Program::setUniformMatrix2fv(GLint location,
2126 GLsizei count,
2127 GLboolean transpose,
2128 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002130 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002131 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002132 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133}
2134
Jamie Madill493f9572018-05-24 19:52:15 -04002135void Program::setUniformMatrix3fv(GLint location,
2136 GLsizei count,
2137 GLboolean transpose,
2138 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002140 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002141 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002142 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143}
2144
Jamie Madill493f9572018-05-24 19:52:15 -04002145void Program::setUniformMatrix4fv(GLint location,
2146 GLsizei count,
2147 GLboolean transpose,
2148 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002150 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002151 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002152 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153}
2154
Jamie Madill493f9572018-05-24 19:52:15 -04002155void Program::setUniformMatrix2x3fv(GLint location,
2156 GLsizei count,
2157 GLboolean transpose,
2158 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002159{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002160 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002161 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002162 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163}
2164
Jamie Madill493f9572018-05-24 19:52:15 -04002165void Program::setUniformMatrix2x4fv(GLint location,
2166 GLsizei count,
2167 GLboolean transpose,
2168 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002170 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002171 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002172 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173}
2174
Jamie Madill493f9572018-05-24 19:52:15 -04002175void Program::setUniformMatrix3x2fv(GLint location,
2176 GLsizei count,
2177 GLboolean transpose,
2178 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002180 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002181 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002182 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183}
2184
Jamie Madill493f9572018-05-24 19:52:15 -04002185void Program::setUniformMatrix3x4fv(GLint location,
2186 GLsizei count,
2187 GLboolean transpose,
2188 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002190 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002191 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002192 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193}
2194
Jamie Madill493f9572018-05-24 19:52:15 -04002195void Program::setUniformMatrix4x2fv(GLint location,
2196 GLsizei count,
2197 GLboolean transpose,
2198 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002199{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002200 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002201 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002202 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203}
2204
Jamie Madill493f9572018-05-24 19:52:15 -04002205void Program::setUniformMatrix4x3fv(GLint location,
2206 GLsizei count,
2207 GLboolean transpose,
2208 const GLfloat *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002210 ASSERT(mLinkResolved);
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002211 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002212 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002213}
2214
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002215GLuint Program::getSamplerUniformBinding(const VariableLocation &uniformLocation) const
2216{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002217 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002218 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(uniformLocation.index);
2219 const std::vector<GLuint> &boundTextureUnits =
2220 mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2221 return boundTextureUnits[uniformLocation.arrayIndex];
2222}
2223
Jamie Madill54164b02017-08-28 15:17:37 -04002224void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002225{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002226 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002227 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2228 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002229
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002230 if (uniform.isSampler())
2231 {
2232 *v = static_cast<GLfloat>(getSamplerUniformBinding(uniformLocation));
2233 return;
2234 }
2235
2236 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002237 if (nativeType == GL_FLOAT)
2238 {
2239 mProgram->getUniformfv(context, location, v);
2240 }
2241 else
2242 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002243 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002244 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245}
2246
Jamie Madill54164b02017-08-28 15:17:37 -04002247void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002249 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002250 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2251 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002252
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002253 if (uniform.isSampler())
2254 {
2255 *v = static_cast<GLint>(getSamplerUniformBinding(uniformLocation));
2256 return;
2257 }
2258
2259 const GLenum nativeType = gl::VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002260 if (nativeType == GL_INT || nativeType == GL_BOOL)
2261 {
2262 mProgram->getUniformiv(context, location, v);
2263 }
2264 else
2265 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002266 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002267 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268}
2269
Jamie Madill54164b02017-08-28 15:17:37 -04002270void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002272 ASSERT(mLinkResolved);
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002273 const VariableLocation &uniformLocation = mState.getUniformLocations()[location];
2274 const LinkedUniform &uniform = mState.getUniforms()[uniformLocation.index];
Jamie Madill54164b02017-08-28 15:17:37 -04002275
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002276 if (uniform.isSampler())
2277 {
2278 *v = getSamplerUniformBinding(uniformLocation);
2279 return;
2280 }
2281
2282 const GLenum nativeType = VariableComponentType(uniform.type);
Jamie Madill54164b02017-08-28 15:17:37 -04002283 if (nativeType == GL_UNSIGNED_INT)
2284 {
2285 mProgram->getUniformuiv(context, location, v);
2286 }
2287 else
2288 {
Jamie Madill3bb2bbe2018-06-15 09:47:03 -04002289 getUniformInternal(context, v, location, nativeType, VariableComponentCount(uniform.type));
Jamie Madill54164b02017-08-28 15:17:37 -04002290 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002291}
2292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293void Program::flagForDeletion()
2294{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002295 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 mDeleteStatus = true;
2297}
2298
2299bool Program::isFlaggedForDeletion() const
2300{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002301 ASSERT(mLinkResolved);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 return mDeleteStatus;
2303}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00002304
Brandon Jones43a53e22014-08-28 16:23:22 -07002305void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002306{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002307 ASSERT(mLinkResolved);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002308 mInfoLog.reset();
2309
Geoff Lang7dd2e102014-11-10 15:19:26 -05002310 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002311 {
Geoff Lang92019432017-11-20 13:09:34 -05002312 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002313 }
2314 else
2315 {
Jamie Madillf6113162015-05-07 11:49:21 -04002316 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002317 }
2318}
2319
Jamie Madillcc73f242018-08-01 11:34:48 -04002320bool Program::validateSamplersImpl(InfoLog *infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002321{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002322 ASSERT(mLinkResolved);
jchen107ae70d82018-07-06 13:47:01 +08002323
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002324 // if any two active samplers in a program are of different types, but refer to the same
2325 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2326 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madill7e4eff12018-08-08 15:49:26 -04002327 for (size_t textureUnit : mState.mActiveSamplersMask)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002328 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002329 if (mState.mActiveSamplerTypes[textureUnit] == TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002330 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002331 if (infoLog)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002332 {
Jamie Madill7e4eff12018-08-08 15:49:26 -04002333 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2334 "image unit ("
2335 << textureUnit << ").";
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002336 }
2337
Jamie Madill7e4eff12018-08-08 15:49:26 -04002338 mCachedValidateSamplersResult = false;
2339 return false;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002340 }
2341 }
2342
2343 mCachedValidateSamplersResult = true;
2344 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345}
2346
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002347bool Program::isValidated() const
2348{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002349 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 return mValidated;
2351}
2352
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002353void Program::getActiveUniformBlockName(const GLuint blockIndex,
2354 GLsizei bufSize,
2355 GLsizei *length,
2356 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002357{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002358 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002359 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2360}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002362void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2363 GLsizei bufSize,
2364 GLsizei *length,
2365 GLchar *blockName) const
2366{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002367 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002368 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002369}
2370
Qin Jiajia9bf55522018-01-29 13:56:23 +08002371template <typename T>
2372GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002373{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374 int maxLength = 0;
2375
2376 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002377 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002378 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002380 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002381 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002382 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002383 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002384 }
2385 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002386 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387
2388 return maxLength;
2389}
2390
Qin Jiajia9bf55522018-01-29 13:56:23 +08002391GLint Program::getActiveUniformBlockMaxNameLength() const
2392{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002393 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002394 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2395}
2396
2397GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2398{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002399 ASSERT(mLinkResolved);
Qin Jiajia9bf55522018-01-29 13:56:23 +08002400 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2401}
2402
Geoff Lange1a27752015-10-05 13:16:04 -04002403GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002404{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002405 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002406 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2407}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002408
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002409GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2410{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002411 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002412 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002413}
2414
Jiajia Qin729b2c62017-08-14 09:36:11 +08002415const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002416{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002417 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002418 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2419 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002420}
2421
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002422const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2423{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002424 ASSERT(mLinkResolved);
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002425 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2426 return mState.mShaderStorageBlocks[index];
2427}
2428
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002429void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2430{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002431 ASSERT(mLinkResolved);
jchen107a20b972017-06-13 14:25:26 +08002432 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002433 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Jamie Madill70aeda42018-08-20 12:17:40 -04002434 mDirtyBits.set(DIRTY_BIT_UNIFORM_BLOCK_BINDING_0 + uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002435}
2436
2437GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2438{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002439 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002440 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002441}
2442
Jiajia Qin729b2c62017-08-14 09:36:11 +08002443GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2444{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002445 ASSERT(mLinkResolved);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002446 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2447}
2448
Jamie Madill493f9572018-05-24 19:52:15 -04002449void Program::setTransformFeedbackVaryings(GLsizei count,
2450 const GLchar *const *varyings,
2451 GLenum bufferMode)
Geoff Lang48dcae72014-02-05 16:28:24 -05002452{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002453 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002454 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002455 for (GLsizei i = 0; i < count; i++)
2456 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002457 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002458 }
2459
Jamie Madill48ef11b2016-04-27 15:21:52 -04002460 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002461}
2462
Jamie Madill493f9572018-05-24 19:52:15 -04002463void Program::getTransformFeedbackVarying(GLuint index,
2464 GLsizei bufSize,
2465 GLsizei *length,
2466 GLsizei *size,
2467 GLenum *type,
2468 GLchar *name) const
Geoff Lang48dcae72014-02-05 16:28:24 -05002469{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002470 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002471 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002472 {
jchen10a9042d32017-03-17 08:50:45 +08002473 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2474 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2475 std::string varName = var.nameWithArrayIndex();
2476 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002477 if (length)
2478 {
2479 *length = lastNameIdx;
2480 }
2481 if (size)
2482 {
jchen10a9042d32017-03-17 08:50:45 +08002483 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002484 }
2485 if (type)
2486 {
jchen10a9042d32017-03-17 08:50:45 +08002487 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002488 }
2489 if (name)
2490 {
jchen10a9042d32017-03-17 08:50:45 +08002491 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002492 name[lastNameIdx] = '\0';
2493 }
2494 }
2495}
2496
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002497GLsizei Program::getTransformFeedbackVaryingCount() const
2498{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002499 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002500 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002501 {
jchen10a9042d32017-03-17 08:50:45 +08002502 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002503 }
2504 else
2505 {
2506 return 0;
2507 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002508}
2509
2510GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2511{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002512 ASSERT(mLinkResolved);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002513 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002514 {
2515 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002516 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002517 {
jchen10a9042d32017-03-17 08:50:45 +08002518 maxSize =
2519 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002520 }
2521
2522 return maxSize;
2523 }
2524 else
2525 {
2526 return 0;
2527 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002528}
2529
2530GLenum Program::getTransformFeedbackBufferMode() const
2531{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002532 ASSERT(mLinkResolved);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002533 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002534}
2535
jchen103fd614d2018-08-13 12:21:58 +08002536bool Program::linkValidateShaders(InfoLog &infoLog)
Jiawei Shao73618602017-12-20 15:47:15 +08002537{
Jiawei Shao016105b2018-04-12 16:38:31 +08002538 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2539 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2540 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2541 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002542
Jamie Madill493f9572018-05-24 19:52:15 -04002543 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002544 bool isGraphicsShaderAttached =
2545 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002546 // Check whether we both have a compute and non-compute shaders attached.
2547 // If there are of both types attached, then linking should fail.
2548 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2549 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2550 {
2551 infoLog << "Both compute and graphics shaders are attached to the same program.";
2552 return false;
2553 }
2554
2555 if (computeShader)
2556 {
jchen103fd614d2018-08-13 12:21:58 +08002557 if (!computeShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002558 {
2559 infoLog << "Attached compute shader is not compiled.";
2560 return false;
2561 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002562 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002563
jchen103fd614d2018-08-13 12:21:58 +08002564 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Jiawei Shao73618602017-12-20 15:47:15 +08002565
2566 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2567 // If the work group size is not specified, a link time error should occur.
2568 if (!mState.mComputeShaderLocalSize.isDeclared())
2569 {
2570 infoLog << "Work group size is not specified.";
2571 return false;
2572 }
2573 }
2574 else
2575 {
jchen103fd614d2018-08-13 12:21:58 +08002576 if (!fragmentShader || !fragmentShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002577 {
2578 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2579 return false;
2580 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002581 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002582
jchen103fd614d2018-08-13 12:21:58 +08002583 if (!vertexShader || !vertexShader->isCompiled())
Jiawei Shao73618602017-12-20 15:47:15 +08002584 {
2585 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2586 return false;
2587 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002588 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002589
jchen103fd614d2018-08-13 12:21:58 +08002590 int vertexShaderVersion = vertexShader->getShaderVersion();
2591 if (fragmentShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002592 {
2593 infoLog << "Fragment shader version does not match vertex shader version.";
2594 return false;
2595 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002596
2597 if (geometryShader)
2598 {
2599 // [GL_EXT_geometry_shader] Chapter 7
2600 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2601 // Language Specification, as well as any of the following reasons:
2602 // * One or more of the shader objects attached to <program> are not compiled
2603 // successfully.
2604 // * The shaders do not use the same shader language version.
2605 // * <program> contains objects to form a geometry shader, and
2606 // - <program> is not separable and contains no objects to form a vertex shader; or
2607 // - the input primitive type, output primitive type, or maximum output vertex count
2608 // is not specified in the compiled geometry shader object.
jchen103fd614d2018-08-13 12:21:58 +08002609 if (!geometryShader->isCompiled())
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002610 {
2611 infoLog << "The attached geometry shader isn't compiled.";
2612 return false;
2613 }
2614
jchen103fd614d2018-08-13 12:21:58 +08002615 if (geometryShader->getShaderVersion() != vertexShaderVersion)
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002616 {
2617 mInfoLog << "Geometry shader version does not match vertex shader version.";
2618 return false;
2619 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002620 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002621
Jamie Madill493f9572018-05-24 19:52:15 -04002622 Optional<PrimitiveMode> inputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002623 geometryShader->getGeometryShaderInputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002624 if (!inputPrimitive.valid())
2625 {
2626 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2627 return false;
2628 }
2629
Jamie Madill493f9572018-05-24 19:52:15 -04002630 Optional<PrimitiveMode> outputPrimitive =
jchen103fd614d2018-08-13 12:21:58 +08002631 geometryShader->getGeometryShaderOutputPrimitiveType();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002632 if (!outputPrimitive.valid())
2633 {
2634 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2635 return false;
2636 }
2637
jchen103fd614d2018-08-13 12:21:58 +08002638 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002639 if (!maxVertices.valid())
2640 {
2641 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2642 return false;
2643 }
2644
2645 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2646 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2647 mState.mGeometryShaderMaxVertices = maxVertices.value();
jchen103fd614d2018-08-13 12:21:58 +08002648 mState.mGeometryShaderInvocations = geometryShader->getGeometryShaderInvocations();
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002649 }
Jiawei Shao73618602017-12-20 15:47:15 +08002650 }
2651
2652 return true;
2653}
2654
jchen10910a3da2017-11-15 09:40:11 +08002655GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2656{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002657 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002658 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2659 {
2660 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2661 if (tf.nameWithArrayIndex() == name)
2662 {
2663 return tfIndex;
2664 }
2665 }
2666 return GL_INVALID_INDEX;
2667}
2668
2669const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2670{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04002671 ASSERT(mLinkResolved);
jchen10910a3da2017-11-15 09:40:11 +08002672 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2673 return mState.mLinkedTransformFeedbackVaryings[index];
2674}
2675
Austin Eng1bf18ce2018-10-19 15:34:02 -07002676bool Program::hasDrawIDUniform() const
2677{
2678 ASSERT(mLinkResolved);
2679 return mState.mDrawIDLocation >= 0;
2680}
2681
2682void Program::setDrawIDUniform(GLint drawid)
2683{
2684 ASSERT(mLinkResolved);
2685 ASSERT(mState.mDrawIDLocation >= 0);
2686 mProgram->setUniform1iv(mState.mDrawIDLocation, 1, &drawid);
2687}
2688
jchen103fd614d2018-08-13 12:21:58 +08002689bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002690{
Jiawei Shao016105b2018-04-12 16:38:31 +08002691 Shader *previousShader = nullptr;
2692 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002693 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002694 Shader *currentShader = mState.mAttachedShaders[shaderType];
2695 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002696 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002697 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002698 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002699
2700 if (previousShader)
2701 {
jchen103fd614d2018-08-13 12:21:58 +08002702 if (!linkValidateShaderInterfaceMatching(previousShader, currentShader, infoLog))
Jiawei Shao016105b2018-04-12 16:38:31 +08002703 {
2704 return false;
2705 }
2706 }
2707 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002708 }
2709
jchen103fd614d2018-08-13 12:21:58 +08002710 if (!linkValidateBuiltInVaryings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002711 {
2712 return false;
2713 }
2714
jchen103fd614d2018-08-13 12:21:58 +08002715 if (!linkValidateFragmentInputBindings(infoLog))
Jiawei Shaod063aff2018-02-22 10:19:09 +08002716 {
2717 return false;
2718 }
2719
2720 return true;
2721}
2722
2723// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2724// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
jchen103fd614d2018-08-13 12:21:58 +08002725bool Program::linkValidateShaderInterfaceMatching(gl::Shader *generatingShader,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002726 gl::Shader *consumingShader,
2727 gl::InfoLog &infoLog) const
2728{
jchen103fd614d2018-08-13 12:21:58 +08002729 ASSERT(generatingShader->getShaderVersion() == consumingShader->getShaderVersion());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002730
jchen103fd614d2018-08-13 12:21:58 +08002731 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings();
2732 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002733
Jiawei Shao385b3e02018-03-21 09:43:28 +08002734 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002735
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002736 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002737 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002738 bool matched = false;
2739
2740 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002741 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002742 {
2743 continue;
2744 }
2745
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002746 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002747 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002748 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002749 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002750 ASSERT(!output.isBuiltIn());
2751
2752 std::string mismatchedStructFieldName;
2753 LinkMismatchError linkError =
jchen103fd614d2018-08-13 12:21:58 +08002754 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002755 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002756 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002757 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002758 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2759 mismatchedStructFieldName, generatingShader->getType(),
2760 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002761 return false;
2762 }
2763
Geoff Lang7dd2e102014-11-10 15:19:26 -05002764 matched = true;
2765 break;
2766 }
2767 }
2768
Olli Etuaho107c7242018-03-20 15:45:35 +02002769 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2770 // whether the input is statically used - a statically used input should fail this test even
2771 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002772 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002773 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002774 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2775 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2776 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002777 return false;
2778 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002779 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002780
Jiawei Shaod063aff2018-02-22 10:19:09 +08002781 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002782
Jiawei Shaod063aff2018-02-22 10:19:09 +08002783 return true;
2784}
2785
jchen103fd614d2018-08-13 12:21:58 +08002786bool Program::linkValidateFragmentInputBindings(gl::InfoLog &infoLog) const
Jiawei Shaod063aff2018-02-22 10:19:09 +08002787{
Jiawei Shao016105b2018-04-12 16:38:31 +08002788 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002789
2790 std::map<GLuint, std::string> staticFragmentInputLocations;
2791
2792 const std::vector<sh::Varying> &fragmentInputVaryings =
jchen103fd614d2018-08-13 12:21:58 +08002793 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings();
Jiawei Shaod063aff2018-02-22 10:19:09 +08002794 for (const sh::Varying &input : fragmentInputVaryings)
2795 {
2796 if (input.isBuiltIn() || !input.staticUse)
2797 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002798 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002799 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002800
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002801 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002802 if (inputBinding == -1)
2803 continue;
2804
2805 const auto it = staticFragmentInputLocations.find(inputBinding);
2806 if (it == std::end(staticFragmentInputLocations))
2807 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002808 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002809 }
2810 else
2811 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002812 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002813 << it->second;
2814 return false;
2815 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002816 }
2817
2818 return true;
2819}
2820
jchen103fd614d2018-08-13 12:21:58 +08002821bool Program::linkUniforms(const Caps &caps,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002822 InfoLog &infoLog,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002823 const ProgramBindings &uniformLocationBindings,
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002824 GLuint *combinedImageUniformsCount,
2825 std::vector<UnusedUniform> *unusedUniforms)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002826{
Olli Etuahob78707c2017-03-09 15:03:11 +00002827 UniformLinker linker(mState);
jchen103fd614d2018-08-13 12:21:58 +08002828 if (!linker.link(caps, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829 {
2830 return false;
2831 }
2832
Luc Ferrone17b5ba2018-06-04 14:28:58 -04002833 linker.getResults(&mState.mUniforms, unusedUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002834
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002835 linkSamplerAndImageBindings(combinedImageUniformsCount);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002836
jchen10eaef1e52017-06-13 10:44:11 +08002837 if (!linkAtomicCounterBuffers())
2838 {
2839 return false;
2840 }
2841
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002842 return true;
2843}
2844
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002845void Program::linkSamplerAndImageBindings(GLuint *combinedImageUniforms)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002846{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002847 ASSERT(combinedImageUniforms);
2848
Jamie Madill982f6e02017-06-07 14:33:04 -04002849 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2850 unsigned int low = high;
2851
jchen10eaef1e52017-06-13 10:44:11 +08002852 for (auto counterIter = mState.mUniforms.rbegin();
2853 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2854 {
2855 --low;
2856 }
2857
2858 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2859
2860 high = low;
2861
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002862 for (auto imageIter = mState.mUniforms.rbegin();
2863 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2864 {
2865 --low;
2866 }
2867
2868 mState.mImageUniformRange = RangeUI(low, high);
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002869 *combinedImageUniforms = 0u;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002870 // If uniform is a image type, insert it into the mImageBindings array.
2871 for (unsigned int imageIndex : mState.mImageUniformRange)
2872 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002873 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2874 // cannot load values into a uniform defined as an image. if declare without a
2875 // binding qualifier, any uniform image variable (include all elements of
2876 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002877 auto &imageUniform = mState.mUniforms[imageIndex];
2878 if (imageUniform.binding == -1)
2879 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002880 mState.mImageBindings.emplace_back(
2881 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002882 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002883 else
2884 {
2885 mState.mImageBindings.emplace_back(
Qin Jiajia47f6dd02018-08-10 13:36:32 +08002886 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount(), false));
Xinghua Cao0328b572017-06-26 15:51:36 +08002887 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08002888
2889 GLuint arraySize = imageUniform.isArray() ? imageUniform.arraySizes[0] : 1u;
2890 *combinedImageUniforms += imageUniform.activeShaderCount() * arraySize;
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002891 }
2892
2893 high = low;
2894
2895 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002896 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002897 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002898 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002899 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002900
2901 mState.mSamplerUniformRange = RangeUI(low, high);
2902
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002903 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002904 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002905 {
2906 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002907 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Jamie Madill7e4eff12018-08-08 15:49:26 -04002908 unsigned int elementCount = samplerUniform.getBasicTypeElementCount();
2909 mState.mSamplerBindings.emplace_back(textureType, elementCount, false);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002910 }
2911}
2912
jchen10eaef1e52017-06-13 10:44:11 +08002913bool Program::linkAtomicCounterBuffers()
2914{
2915 for (unsigned int index : mState.mAtomicCounterUniformRange)
2916 {
Jamie Madill493f9572018-05-24 19:52:15 -04002917 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002918 uniform.blockInfo.offset = uniform.offset;
2919 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2920 uniform.blockInfo.matrixStride = 0;
2921 uniform.blockInfo.isRowMajorMatrix = false;
2922
Jamie Madill493f9572018-05-24 19:52:15 -04002923 bool found = false;
jchen10eaef1e52017-06-13 10:44:11 +08002924 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2925 ++bufferIndex)
2926 {
2927 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2928 if (buffer.binding == uniform.binding)
2929 {
2930 buffer.memberIndexes.push_back(index);
2931 uniform.bufferIndex = bufferIndex;
2932 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002933 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002934 break;
2935 }
2936 }
2937 if (!found)
2938 {
2939 AtomicCounterBuffer atomicCounterBuffer;
2940 atomicCounterBuffer.binding = uniform.binding;
2941 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002942 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002943 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2944 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2945 }
2946 }
2947 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002948 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002949
2950 return true;
2951}
2952
Jamie Madilleb979bf2016-11-15 12:28:46 -05002953// Assigns locations to all attributes from the bindings and program locations.
jchen103fd614d2018-08-13 12:21:58 +08002954bool Program::linkAttributes(const Caps &caps, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002955{
Jamie Madillb980c562018-11-27 11:34:27 -05002956 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002957
jchen103fd614d2018-08-13 12:21:58 +08002958 int shaderVersion = vertexShader->getShaderVersion();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002959
Geoff Lang7dd2e102014-11-10 15:19:26 -05002960 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002961 if (shaderVersion >= 300)
2962 {
2963 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2964 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
jchen103fd614d2018-08-13 12:21:58 +08002965 mState.mAttributes = vertexShader->getAllAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002966 }
2967 else
2968 {
2969 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
jchen103fd614d2018-08-13 12:21:58 +08002970 mState.mAttributes = vertexShader->getActiveAttributes();
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002971 }
jchen103fd614d2018-08-13 12:21:58 +08002972 GLuint maxAttribs = caps.maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002973
2974 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002975 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002976 {
Jamie Madillf6113162015-05-07 11:49:21 -04002977 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002978 return false;
2979 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002980
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002981 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002982
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002983 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002984 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002985 {
Olli Etuahod2551232017-10-26 20:03:33 +03002986 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2987 // structures, so we don't need to worry about adjusting their names or generating entries
2988 // for each member/element (unlike uniforms for example).
2989 ASSERT(!attribute.isArray() && !attribute.isStruct());
2990
Jamie Madilleb979bf2016-11-15 12:28:46 -05002991 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002992 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002993 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002994 attribute.location = bindingLocation;
2995 }
2996
2997 if (attribute.location != -1)
2998 {
2999 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04003000 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003001
Jamie Madill63805b42015-08-25 13:17:39 -04003002 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003003 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003004 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
3005 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05003006
3007 return false;
3008 }
3009
Jamie Madill63805b42015-08-25 13:17:39 -04003010 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003011 {
Jamie Madill63805b42015-08-25 13:17:39 -04003012 const int regLocation = attribute.location + reg;
3013 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003014
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003015 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
3016 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
3017 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04003018 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003019 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003020 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003021 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003022 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04003023 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04003024 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003025 return false;
3026 }
3027 }
Jamie Madillc349ec02015-08-21 16:53:12 -04003028 else
3029 {
Jamie Madill63805b42015-08-25 13:17:39 -04003030 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04003031 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003032
Jamie Madill63805b42015-08-25 13:17:39 -04003033 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003034 }
3035 }
3036 }
3037
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003038 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003039 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003040 {
Jamie Madillc349ec02015-08-21 16:53:12 -04003041 // Not set by glBindAttribLocation or by location layout qualifier
3042 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003043 {
Jamie Madill63805b42015-08-25 13:17:39 -04003044 int regs = VariableRegisterCount(attribute.type);
3045 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05003046
Jamie Madill63805b42015-08-25 13:17:39 -04003047 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003048 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003049 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04003050 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003051 }
3052
Jamie Madillc349ec02015-08-21 16:53:12 -04003053 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003054 }
3055 }
3056
Brandon Jonesc405ae72017-12-06 14:15:03 -08003057 ASSERT(mState.mAttributesTypeMask.none());
3058 ASSERT(mState.mAttributesMask.none());
3059
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003060 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
3061 // shader versions we're only processing active attributes to begin with.
3062 if (shaderVersion >= 300)
3063 {
3064 for (auto attributeIter = mState.mAttributes.begin();
3065 attributeIter != mState.mAttributes.end();)
3066 {
3067 if (attributeIter->active)
3068 {
3069 ++attributeIter;
3070 }
3071 else
3072 {
3073 attributeIter = mState.mAttributes.erase(attributeIter);
3074 }
3075 }
3076 }
3077
Jamie Madill48ef11b2016-04-27 15:21:52 -04003078 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003079 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02003080 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04003081 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04003082 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04003083
Jamie Madillbd159f02017-10-09 19:39:06 -04003084 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003085 {
Jamie Madillbd159f02017-10-09 19:39:06 -04003086 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
3087 mState.mActiveAttribLocationsMask.set(location);
3088 mState.mMaxActiveAttribLocation =
3089 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08003090
3091 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
3092 if (!attribute.isBuiltIn())
3093 {
3094 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
3095 location);
3096 mState.mAttributesMask.set(location);
3097 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003098 }
3099 }
3100
Geoff Lang7dd2e102014-11-10 15:19:26 -05003101 return true;
3102}
3103
jchen103fd614d2018-08-13 12:21:58 +08003104bool Program::linkInterfaceBlocks(const Caps &caps,
3105 const Version &version,
3106 bool webglCompatibility,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003107 InfoLog &infoLog,
3108 GLuint *combinedShaderStorageBlocksCount)
Martin Radev4c4c8e72016-08-04 12:25:34 +03003109{
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003110 ASSERT(combinedShaderStorageBlocksCount);
3111
Jiawei Shao54aafe52018-04-27 14:54:57 +08003112 GLuint combinedUniformBlocksCount = 0u;
3113 GLuint numShadersHasUniformBlocks = 0u;
3114 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderUniformBlocks = {};
3115 for (ShaderType shaderType : AllShaderTypes())
Martin Radev4c4c8e72016-08-04 12:25:34 +03003116 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003117 Shader *shader = mState.mAttachedShaders[shaderType];
3118 if (!shader)
3119 {
3120 continue;
3121 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03003122
jchen103fd614d2018-08-13 12:21:58 +08003123 const auto &uniformBlocks = shader->getUniformBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003124 if (!uniformBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003125 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003126 if (!ValidateInterfaceBlocksCount(
3127 caps.maxShaderUniformBlocks[shaderType], uniformBlocks, shaderType,
3128 sh::BlockType::BLOCK_UNIFORM, &combinedUniformBlocksCount, infoLog))
3129 {
3130 return false;
3131 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003132
Jiawei Shao54aafe52018-04-27 14:54:57 +08003133 allShaderUniformBlocks[shaderType] = &uniformBlocks;
3134 ++numShadersHasUniformBlocks;
3135 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003136 }
3137
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003138 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
3139 {
3140 infoLog << "The sum of the number of active uniform blocks exceeds "
3141 "MAX_COMBINED_UNIFORM_BLOCKS ("
3142 << caps.maxCombinedUniformBlocks << ").";
3143 return false;
3144 }
3145
Jiawei Shao54aafe52018-04-27 14:54:57 +08003146 if (!ValidateInterfaceBlocksMatch(numShadersHasUniformBlocks, allShaderUniformBlocks, infoLog,
3147 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03003148 {
3149 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003150 }
Jamie Madille473dee2015-08-18 14:49:01 -04003151
jchen103fd614d2018-08-13 12:21:58 +08003152 if (version >= Version(3, 1))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003153 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003154 *combinedShaderStorageBlocksCount = 0u;
3155 GLuint numShadersHasShaderStorageBlocks = 0u;
3156 ShaderMap<const std::vector<sh::InterfaceBlock> *> allShaderStorageBlocks = {};
3157 for (ShaderType shaderType : AllShaderTypes())
Jiajia Qin729b2c62017-08-14 09:36:11 +08003158 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08003159 Shader *shader = mState.mAttachedShaders[shaderType];
3160 if (!shader)
3161 {
3162 continue;
3163 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003164
jchen103fd614d2018-08-13 12:21:58 +08003165 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks();
Jiawei Shao54aafe52018-04-27 14:54:57 +08003166 if (!shaderStorageBlocks.empty())
Jiawei Shao427071e2018-03-19 09:21:37 +08003167 {
Jiawei Shao54aafe52018-04-27 14:54:57 +08003168 if (!ValidateInterfaceBlocksCount(
3169 caps.maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
3170 sh::BlockType::BLOCK_BUFFER, combinedShaderStorageBlocksCount, infoLog))
3171 {
3172 return false;
3173 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08003174
Jiawei Shao54aafe52018-04-27 14:54:57 +08003175 allShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
3176 ++numShadersHasShaderStorageBlocks;
3177 }
Jiawei Shao427071e2018-03-19 09:21:37 +08003178 }
3179
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003180 if (*combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
Jiawei Shaobb3255b2018-04-27 09:45:18 +08003181 {
3182 infoLog << "The sum of the number of active shader storage blocks exceeds "
3183 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
3184 << caps.maxCombinedShaderStorageBlocks << ").";
3185 return false;
3186 }
3187
Jiawei Shao54aafe52018-04-27 14:54:57 +08003188 if (!ValidateInterfaceBlocksMatch(numShadersHasShaderStorageBlocks, allShaderStorageBlocks,
3189 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08003190 {
3191 return false;
3192 }
3193 }
Jiawei Shao54aafe52018-04-27 14:54:57 +08003194
Geoff Lang7dd2e102014-11-10 15:19:26 -05003195 return true;
3196}
3197
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003198LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
3199 const sh::ShaderVariable &variable2,
3200 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003201 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003202 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003203{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003204 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003205 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003206 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003207 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08003208 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003209 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003210 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003211 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003212 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003213 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003214 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003215 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003216 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04003217 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003218 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04003219 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003220
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003221 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05003222 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003223 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003224 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003225 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05003226 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
3227 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003228 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
3229 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05003230
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003231 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003232 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003233 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003234 }
3235
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003236 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08003237 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003238 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003239 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003240 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
3241 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003242 }
3243 }
3244
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003245 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003246}
3247
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003248LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
3249 const sh::Varying &inputVarying,
3250 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08003251 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003252 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003253{
Jiawei Shaod063aff2018-02-22 10:19:09 +08003254 if (validateGeometryShaderInputVarying)
3255 {
3256 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
3257 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
3258 // or outputs.
3259 ASSERT(inputVarying.arraySizes.size() == 1u);
3260
3261 // Geometry shader input varyings are not treated as arrays, so a vertex array output
3262 // varying cannot match a geometry shader input varying.
3263 // [GL_EXT_geometry_shader] Section 7.4.1:
3264 // Geometry shader per-vertex input variables and blocks are required to be declared as
3265 // arrays, with each element representing input or output values for a single vertex of a
3266 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
3267 // are treated as though they were not declared as arrays.
3268 if (outputVarying.isArray())
3269 {
3270 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
3271 }
3272 }
3273
3274 // Skip the validation on the array sizes between a vertex output varying and a geometry input
3275 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003276 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08003277 LinkValidateVariablesBase(outputVarying, inputVarying, false,
3278 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003279 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003280 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003281 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003282 }
3283
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003284 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05003285 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003286 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003287 }
3288
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003289 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04003290 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003291 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003292 }
3293
Jiawei Shao881b7bf2017-12-25 11:18:37 +08003294 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05003295}
3296
jchen103fd614d2018-08-13 12:21:58 +08003297bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05003298{
Jiawei Shao016105b2018-04-12 16:38:31 +08003299 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
3300 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
jchen103fd614d2018-08-13 12:21:58 +08003301 const auto &vertexVaryings = vertexShader->getOutputVaryings();
3302 const auto &fragmentVaryings = fragmentShader->getInputVaryings();
3303 int shaderVersion = vertexShader->getShaderVersion();
Yuly Novikov817232e2017-02-22 18:36:10 -05003304
3305 if (shaderVersion != 100)
3306 {
3307 // Only ESSL 1.0 has restrictions on matching input and output invariance
3308 return true;
3309 }
3310
3311 bool glPositionIsInvariant = false;
3312 bool glPointSizeIsInvariant = false;
3313 bool glFragCoordIsInvariant = false;
3314 bool glPointCoordIsInvariant = false;
3315
3316 for (const sh::Varying &varying : vertexVaryings)
3317 {
3318 if (!varying.isBuiltIn())
3319 {
3320 continue;
3321 }
3322 if (varying.name.compare("gl_Position") == 0)
3323 {
3324 glPositionIsInvariant = varying.isInvariant;
3325 }
3326 else if (varying.name.compare("gl_PointSize") == 0)
3327 {
3328 glPointSizeIsInvariant = varying.isInvariant;
3329 }
3330 }
3331
3332 for (const sh::Varying &varying : fragmentVaryings)
3333 {
3334 if (!varying.isBuiltIn())
3335 {
3336 continue;
3337 }
3338 if (varying.name.compare("gl_FragCoord") == 0)
3339 {
3340 glFragCoordIsInvariant = varying.isInvariant;
3341 }
3342 else if (varying.name.compare("gl_PointCoord") == 0)
3343 {
3344 glPointCoordIsInvariant = varying.isInvariant;
3345 }
3346 }
3347
3348 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3349 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3350 // Not requiring invariance to match is supported by:
3351 // dEQP, WebGL CTS, Nexus 5X GLES
3352 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3353 {
3354 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3355 "declared invariant.";
3356 return false;
3357 }
3358 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3359 {
3360 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3361 "declared invariant.";
3362 return false;
3363 }
3364
3365 return true;
3366}
3367
jchen103fd614d2018-08-13 12:21:58 +08003368bool Program::linkValidateTransformFeedback(const Version &version,
jchen10a9042d32017-03-17 08:50:45 +08003369 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003370 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003371 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003372{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003373
jchen108225e732017-11-14 16:29:03 +08003374 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003375 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003376 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003377 {
jchen103fd614d2018-08-13 12:21:58 +08003378 if (version < Version(3, 1) && tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003379 {
Geoff Lang1a683462015-09-29 15:09:59 -04003380 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003381 return false;
3382 }
jchen103fd614d2018-08-13 12:21:58 +08003383 if (version >= Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003384 {
3385 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3386 {
3387 infoLog << "Two transform feedback varyings include the same array element ("
3388 << tfVaryingName << ").";
3389 return false;
3390 }
3391 }
3392 else
3393 {
3394 if (uniqueNames.count(tfVaryingName) > 0)
3395 {
3396 infoLog << "Two transform feedback varyings specify the same output variable ("
3397 << tfVaryingName << ").";
3398 return false;
3399 }
3400 }
3401 uniqueNames.insert(tfVaryingName);
3402 }
3403
3404 // Validate against program varyings.
3405 size_t totalComponents = 0;
3406 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3407 {
3408 std::vector<unsigned int> subscripts;
3409 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3410
3411 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3412 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003413 {
3414 infoLog << "Transform feedback varying " << tfVaryingName
3415 << " does not exist in the vertex shader.";
3416 return false;
3417 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003418
jchen108225e732017-11-14 16:29:03 +08003419 // Validate the matching variable.
3420 if (var->isStruct())
3421 {
3422 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3423 return false;
3424 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003425
jchen108225e732017-11-14 16:29:03 +08003426 size_t elementCount = 0;
3427 size_t componentCount = 0;
3428
3429 if (var->isArray())
3430 {
jchen103fd614d2018-08-13 12:21:58 +08003431 if (version < Version(3, 1))
jchen108225e732017-11-14 16:29:03 +08003432 {
3433 infoLog << "Capture of arrays is undefined and not supported.";
3434 return false;
3435 }
3436
3437 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3438 ASSERT(!var->isArrayOfArrays());
3439
3440 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3441 {
3442 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3443 return false;
3444 }
3445 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3446 }
3447 else
3448 {
3449 if (!subscripts.empty())
3450 {
3451 infoLog << "Varying '" << baseName
3452 << "' is not an array to be captured by element.";
3453 return false;
3454 }
3455 elementCount = 1;
3456 }
3457
3458 // TODO(jmadill): Investigate implementation limits on D3D11
3459 componentCount = VariableComponentCount(var->type) * elementCount;
3460 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3461 componentCount > caps.maxTransformFeedbackSeparateComponents)
3462 {
3463 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3464 << componentCount << ") exceed the maximum separate components ("
3465 << caps.maxTransformFeedbackSeparateComponents << ").";
3466 return false;
3467 }
3468
3469 totalComponents += componentCount;
3470 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3471 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3472 {
3473 infoLog << "Transform feedback varying total components (" << totalComponents
3474 << ") exceed the maximum interleaved components ("
3475 << caps.maxTransformFeedbackInterleavedComponents << ").";
3476 return false;
3477 }
3478 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003479 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003480}
3481
jchen103fd614d2018-08-13 12:21:58 +08003482bool Program::linkValidateGlobalNames(InfoLog &infoLog) const
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003483{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003484 const std::vector<sh::Attribute> &attributes =
jchen103fd614d2018-08-13 12:21:58 +08003485 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes();
Jiawei Shao016105b2018-04-12 16:38:31 +08003486
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003487 for (const auto &attrib : attributes)
3488 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003489 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003490 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003491 Shader *shader = mState.mAttachedShaders[shaderType];
3492 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003493 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003494 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003495 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003496
jchen103fd614d2018-08-13 12:21:58 +08003497 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jiawei Shao016105b2018-04-12 16:38:31 +08003498 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003499 {
3500 if (uniform.name == attrib.name)
3501 {
3502 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3503 return false;
3504 }
3505 }
3506 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003507 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003508
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003509 return true;
3510}
3511
Jamie Madill3c1da042017-11-27 18:33:40 -05003512void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003513{
3514 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003515 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003516 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003517 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003518 std::vector<unsigned int> subscripts;
3519 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003520 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003521 if (!subscripts.empty())
3522 {
3523 subscript = subscripts.back();
3524 }
Jamie Madill192745a2016-12-22 15:58:21 -05003525 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003526 {
Jamie Madill192745a2016-12-22 15:58:21 -05003527 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003528 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003529 {
jchen10a9042d32017-03-17 08:50:45 +08003530 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3531 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003532 break;
3533 }
jchen108225e732017-11-14 16:29:03 +08003534 else if (varying->isStruct())
3535 {
3536 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3537 if (field != nullptr)
3538 {
3539 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3540 break;
3541 }
3542 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003543 }
3544 }
James Darpinian30b604d2018-03-12 17:26:57 -07003545 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003546}
3547
jchen103fd614d2018-08-13 12:21:58 +08003548ProgramMergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003549{
Jamie Madill3c1da042017-11-27 18:33:40 -05003550 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003551
Jiawei Shao016105b2018-04-12 16:38:31 +08003552 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003553 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003554 {
Jamie Madill192745a2016-12-22 15:58:21 -05003555 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003556 }
3557
Jiawei Shao016105b2018-04-12 16:38:31 +08003558 for (const sh::Varying &varying :
jchen103fd614d2018-08-13 12:21:58 +08003559 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04003560 {
Jamie Madill192745a2016-12-22 15:58:21 -05003561 merged[varying.name].fragment = &varying;
3562 }
3563
3564 return merged;
3565}
3566
Olli Etuaho0ca09752018-09-24 11:00:50 +03003567bool CompareOutputVariable(const sh::OutputVariable &a, const sh::OutputVariable &b)
3568{
3569 return a.getArraySizeProduct() > b.getArraySizeProduct();
3570}
3571
3572int Program::getOutputLocationForLink(const sh::OutputVariable &outputVariable) const
3573{
3574 if (outputVariable.location != -1)
3575 {
3576 return outputVariable.location;
3577 }
3578 int apiLocation = mFragmentOutputLocations.getBinding(outputVariable.name);
3579 if (apiLocation != -1)
3580 {
3581 return apiLocation;
3582 }
3583 return -1;
3584}
3585
3586bool Program::isOutputSecondaryForLink(const sh::OutputVariable &outputVariable) const
3587{
3588 if (outputVariable.index != -1)
3589 {
3590 ASSERT(outputVariable.index == 0 || outputVariable.index == 1);
3591 return (outputVariable.index == 1);
3592 }
3593 int apiIndex = mFragmentOutputIndexes.getBinding(outputVariable.name);
3594 if (apiIndex != -1)
3595 {
3596 // Index layout qualifier from the shader takes precedence, so the index from the API is
3597 // checked only if the index was not set in the shader. This is not specified in the EXT
3598 // spec, but is specified in desktop OpenGL specs.
3599 return (apiIndex == 1);
3600 }
3601 // EXT_blend_func_extended: Outputs get index 0 by default.
3602 return false;
3603}
3604
jchen103fd614d2018-08-13 12:21:58 +08003605bool Program::linkOutputVariables(const Caps &caps,
Olli Etuaho0ca09752018-09-24 11:00:50 +03003606 const Extensions &extensions,
jchen103fd614d2018-08-13 12:21:58 +08003607 const Version &version,
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003608 GLuint combinedImageUniformsCount,
3609 GLuint combinedShaderStorageBlocksCount)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003610{
Jiawei Shao016105b2018-04-12 16:38:31 +08003611 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003612 ASSERT(fragmentShader != nullptr);
3613
Geoff Lange0cff192017-05-30 13:04:56 -04003614 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003615 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003616 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003617
jchen103fd614d2018-08-13 12:21:58 +08003618 const auto &outputVariables = fragmentShader->getActiveOutputVariables();
Geoff Lange0cff192017-05-30 13:04:56 -04003619 // Gather output variable types
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003620 for (const auto &outputVariable : outputVariables)
Geoff Lange0cff192017-05-30 13:04:56 -04003621 {
3622 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3623 outputVariable.name != "gl_FragData")
3624 {
3625 continue;
3626 }
3627
3628 unsigned int baseLocation =
3629 (outputVariable.location == -1 ? 0u
3630 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003631
3632 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3633 // structures, so we may use getBasicTypeElementCount().
3634 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3635 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003636 {
3637 const unsigned int location = baseLocation + elementIndex;
3638 if (location >= mState.mOutputVariableTypes.size())
3639 {
3640 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3641 }
Corentin Walleze7557742017-06-01 13:09:57 -04003642 ASSERT(location < mState.mActiveOutputVariables.size());
3643 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003644 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003645 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003646 }
3647 }
3648
jchen103fd614d2018-08-13 12:21:58 +08003649 if (version >= ES_3_1)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003650 {
3651 // [OpenGL ES 3.1] Chapter 8.22 Page 203:
3652 // A link error will be generated if the sum of the number of active image uniforms used in
3653 // all shaders, the number of active shader storage blocks, and the number of active
3654 // fragment shader outputs exceeds the implementation-dependent value of
3655 // MAX_COMBINED_SHADER_OUTPUT_RESOURCES.
3656 if (combinedImageUniformsCount + combinedShaderStorageBlocksCount +
3657 mState.mActiveOutputVariables.count() >
jchen103fd614d2018-08-13 12:21:58 +08003658 caps.maxCombinedShaderOutputResources)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003659 {
3660 mInfoLog
3661 << "The sum of the number of active image uniforms, active shader storage blocks "
3662 "and active fragment shader outputs exceeds "
3663 "MAX_COMBINED_SHADER_OUTPUT_RESOURCES ("
jchen103fd614d2018-08-13 12:21:58 +08003664 << caps.maxCombinedShaderOutputResources << ")";
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003665 return false;
3666 }
3667 }
3668
Jamie Madill80a6fc02015-08-21 16:53:16 -04003669 // Skip this step for GLES2 shaders.
jchen103fd614d2018-08-13 12:21:58 +08003670 if (fragmentShader->getShaderVersion() == 100)
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003671 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003672
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003673 mState.mOutputVariables = outputVariables;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003674 // TODO(jmadill): any caps validation here?
3675
Olli Etuaho0ca09752018-09-24 11:00:50 +03003676 for (sh::OutputVariable &outputVariable : mState.mOutputVariables)
3677 {
3678 if (outputVariable.isArray())
3679 {
3680 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3681 // Resources and including [0] at the end of array variable names.
3682 outputVariable.name += "[0]";
3683 outputVariable.mappedName += "[0]";
3684 }
3685 }
3686
3687 bool hasSecondaryOutputs = false;
3688
3689 // Reserve locations for output variables whose location is fixed in the shader or through the
3690 // API.
jchen1015015f72017-03-16 13:54:21 +08003691 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003692 outputVariableIndex++)
3693 {
jchen1015015f72017-03-16 13:54:21 +08003694 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003695
3696 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3697 if (outputVariable.isBuiltIn())
3698 continue;
3699
Olli Etuaho0ca09752018-09-24 11:00:50 +03003700 int baseLocation = getOutputLocationForLink(outputVariable);
3701 if (baseLocation == -1)
3702 {
3703 // Here we're only reserving locations for variables whose location is fixed.
3704 continue;
3705 }
3706
3707 auto *outputLocations = &mState.mOutputLocations;
3708 if (isOutputSecondaryForLink(outputVariable))
3709 {
3710 outputLocations = &mState.mSecondaryOutputLocations;
3711 // Note that this check doesn't need to be before checking baseLocation == -1 above. If
3712 // an output has an index specified it will always also have the location specified.
3713 hasSecondaryOutputs = true;
3714 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003715
Olli Etuaho465835d2017-09-26 13:34:10 +03003716 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3717 // structures, so we may use getBasicTypeElementCount().
Jamie Madillb980c562018-11-27 11:34:27 -05003718 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
Olli Etuaho0ca09752018-09-24 11:00:50 +03003719 unsigned int outputLocationsNeeded = static_cast<unsigned int>(baseLocation) + elementCount;
3720 if (outputLocationsNeeded > outputLocations->size())
3721 {
3722 outputLocations->resize(outputLocationsNeeded);
3723 }
Olli Etuaho465835d2017-09-26 13:34:10 +03003724 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003725 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003726 const unsigned int location = static_cast<unsigned int>(baseLocation) + elementIndex;
3727 ASSERT(location < outputLocations->size());
3728 if (outputLocations->at(location).used())
Olli Etuahod2551232017-10-26 20:03:33 +03003729 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003730 mInfoLog << "Location of variable " << outputVariable.name
3731 << " conflicts with another variable.";
3732 return false;
Olli Etuahod2551232017-10-26 20:03:33 +03003733 }
Olli Etuahoc8538042017-09-27 11:20:15 +03003734 if (outputVariable.isArray())
3735 {
Olli Etuaho0ca09752018-09-24 11:00:50 +03003736 (*outputLocations)[location] = VariableLocation(elementIndex, outputVariableIndex);
Olli Etuahoc8538042017-09-27 11:20:15 +03003737 }
3738 else
3739 {
3740 VariableLocation locationInfo;
Jamie Madillb980c562018-11-27 11:34:27 -05003741 locationInfo.index = outputVariableIndex;
3742 (*outputLocations)[location] = locationInfo;
Olli Etuaho0ca09752018-09-24 11:00:50 +03003743 }
3744 }
3745 }
3746
3747 // Here we assign locations for the output variables that don't yet have them. Note that we're
3748 // not necessarily able to fit the variables optimally, since then we might have to try
3749 // different arrangements of output arrays. Now we just assign the locations in the order that
3750 // we got the output variables. The spec isn't clear on what kind of algorithm is required for
3751 // finding locations for the output variables, so this should be acceptable at least for now.
3752 GLuint maxLocation = caps.maxDrawBuffers;
3753 if (hasSecondaryOutputs)
3754 {
3755 // EXT_blend_func_extended: Program outputs will be validated against
3756 // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT if there's even one output with index one.
3757 maxLocation = extensions.maxDualSourceDrawBuffers;
3758 }
3759
3760 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
3761 outputVariableIndex++)
3762 {
3763 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
3764
3765 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3766 if (outputVariable.isBuiltIn())
3767 continue;
3768
3769 if (getOutputLocationForLink(outputVariable) != -1)
3770 {
3771 continue;
3772 }
3773
3774 auto *outputLocations = &mState.mOutputLocations;
3775 if (isOutputSecondaryForLink(outputVariable))
3776 {
3777 outputLocations = &mState.mSecondaryOutputLocations;
3778 }
3779
3780 int baseLocation = 0;
3781 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3782 bool elementsFit = false;
3783 while (!elementsFit)
3784 {
3785 // Try baseLocations starting from 0 one at a time and see if the variable fits.
3786 elementsFit = true;
3787 if (baseLocation + elementCount > maxLocation)
3788 {
3789 // EXT_blend_func_extended: Linking can fail:
3790 // "if the explicit binding assignments do not leave enough space for the linker to
3791 // automatically assign a location for a varying out array, which requires multiple
3792 // contiguous locations."
3793 mInfoLog << "Could not fit output variable into available locations: "
3794 << outputVariable.name;
3795 return false;
3796 }
3797 unsigned int outputLocationsNeeded =
3798 static_cast<unsigned int>(baseLocation) + elementCount;
3799 if (outputLocationsNeeded > outputLocations->size())
3800 {
3801 outputLocations->resize(outputLocationsNeeded);
3802 }
3803 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3804 {
3805 const unsigned int location =
3806 static_cast<unsigned int>(baseLocation) + elementIndex;
3807 ASSERT(location < outputLocations->size());
3808 if (outputLocations->at(location).used())
3809 {
3810 elementsFit = false;
3811 break;
3812 }
3813 }
3814 if (elementsFit)
3815 {
3816 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
3817 {
3818 const unsigned int location =
3819 static_cast<unsigned int>(baseLocation) + elementIndex;
3820 if (outputVariable.isArray())
3821 {
3822 (*outputLocations)[location] =
3823 VariableLocation(elementIndex, outputVariableIndex);
3824 }
3825 else
3826 {
3827 VariableLocation locationInfo;
3828 locationInfo.index = outputVariableIndex;
3829 (*outputLocations)[location] = locationInfo;
3830 }
3831 }
3832 }
3833 else
3834 {
3835 ++baseLocation;
Olli Etuahoc8538042017-09-27 11:20:15 +03003836 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003837 }
3838 }
Jiawei Shao7a8fe152018-04-28 12:59:58 +08003839
3840 return true;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003841}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003842
Olli Etuaho48fed632017-03-16 12:05:30 +00003843void Program::setUniformValuesFromBindingQualifiers()
3844{
Jamie Madill982f6e02017-06-07 14:33:04 -04003845 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003846 {
3847 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3848 if (samplerUniform.binding != -1)
3849 {
Olli Etuahod2551232017-10-26 20:03:33 +03003850 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003851 ASSERT(location != -1);
3852 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003853 for (unsigned int elementIndex = 0;
3854 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003855 {
3856 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3857 }
Jamie Madille3e680c2018-12-03 17:49:08 -05003858
3859 // Here we pass nullptr to avoid a large chain of calls that need a non-const Context.
3860 // We know it's safe not to notify the Context because this is only called after link.
3861 setUniform1iv(nullptr, location, static_cast<GLsizei>(boundTextureUnits.size()),
Olli Etuaho48fed632017-03-16 12:05:30 +00003862 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 Madille3e680c2018-12-03 17:49:08 -05003877void Program::updateSamplerUniform(Context *context,
3878 const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003879 GLsizei clampedCount,
3880 const GLint *v)
3881{
Jamie Madill81c2e252017-09-09 23:32:46 -04003882 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
Jamie Madillb980c562018-11-27 11:34:27 -05003883 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003884 SamplerBinding &samplerBinding = mState.mSamplerBindings[samplerIndex];
3885 std::vector<GLuint> &boundTextureUnits = samplerBinding.boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003886
Jamie Madill7e4eff12018-08-08 15:49:26 -04003887 if (samplerBinding.unreferenced)
3888 return;
3889
3890 // Update the sampler uniforms.
3891 for (GLsizei arrayIndex = 0; arrayIndex < clampedCount; ++arrayIndex)
3892 {
Jamie Madille3e680c2018-12-03 17:49:08 -05003893 GLint oldTextureUnit = boundTextureUnits[arrayIndex + locationInfo.arrayIndex];
3894 GLint newTextureUnit = v[arrayIndex];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003895
Jamie Madille3e680c2018-12-03 17:49:08 -05003896 if (oldTextureUnit == newTextureUnit)
Jamie Madill7e4eff12018-08-08 15:49:26 -04003897 continue;
3898
Jamie Madille3e680c2018-12-03 17:49:08 -05003899 boundTextureUnits[arrayIndex + locationInfo.arrayIndex] = newTextureUnit;
Jamie Madill7e4eff12018-08-08 15:49:26 -04003900
3901 // Update the reference counts.
Jamie Madille3e680c2018-12-03 17:49:08 -05003902 uint32_t &oldRefCount = mState.mActiveSamplerRefCounts[oldTextureUnit];
3903 uint32_t &newRefCount = mState.mActiveSamplerRefCounts[newTextureUnit];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003904 ASSERT(oldRefCount > 0);
3905 ASSERT(newRefCount < std::numeric_limits<uint32_t>::max());
3906 oldRefCount--;
3907 newRefCount++;
3908
3909 // Check for binding type change.
Jamie Madille3e680c2018-12-03 17:49:08 -05003910 TextureType &newSamplerType = mState.mActiveSamplerTypes[newTextureUnit];
3911 TextureType &oldSamplerType = mState.mActiveSamplerTypes[oldTextureUnit];
Jamie Madill7e4eff12018-08-08 15:49:26 -04003912
3913 if (newRefCount == 1)
3914 {
3915 newSamplerType = samplerBinding.textureType;
Jamie Madille3e680c2018-12-03 17:49:08 -05003916 mState.mActiveSamplersMask.set(newTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003917 }
3918 else if (newSamplerType != samplerBinding.textureType)
3919 {
3920 // Conflict detected. Ensure we reset it properly.
3921 newSamplerType = TextureType::InvalidEnum;
3922 }
3923
3924 // Unset previously active sampler.
3925 if (oldRefCount == 0)
3926 {
3927 oldSamplerType = TextureType::InvalidEnum;
Jamie Madille3e680c2018-12-03 17:49:08 -05003928 mState.mActiveSamplersMask.reset(oldTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003929 }
3930 else if (oldSamplerType == TextureType::InvalidEnum)
3931 {
3932 // Previous conflict. Check if this new change fixed the conflict.
Jamie Madille3e680c2018-12-03 17:49:08 -05003933 oldSamplerType = mState.getSamplerUniformTextureType(oldTextureUnit);
3934 }
3935
3936 // Notify context.
3937 if (context)
3938 {
3939 context->onSamplerUniformChange(newTextureUnit);
3940 context->onSamplerUniformChange(oldTextureUnit);
Jamie Madill7e4eff12018-08-08 15:49:26 -04003941 }
3942 }
Jamie Madilld68248b2017-09-11 14:34:14 -04003943
3944 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003945 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003946}
3947
Jamie Madill7e4eff12018-08-08 15:49:26 -04003948TextureType ProgramState::getSamplerUniformTextureType(size_t textureUnitIndex) const
3949{
3950 TextureType foundType = TextureType::InvalidEnum;
3951
3952 for (const SamplerBinding &binding : mSamplerBindings)
3953 {
3954 if (binding.unreferenced)
3955 continue;
3956
3957 // A conflict exists if samplers of different types are sourced by the same texture unit.
3958 // We need to check all bound textures to detect this error case.
3959 for (GLuint textureUnit : binding.boundTextureUnits)
3960 {
3961 if (textureUnit == textureUnitIndex)
3962 {
3963 if (foundType == TextureType::InvalidEnum)
3964 {
3965 foundType = binding.textureType;
3966 }
3967 else if (foundType != binding.textureType)
3968 {
3969 return TextureType::InvalidEnum;
3970 }
3971 }
3972 }
3973 }
3974
3975 return foundType;
3976}
3977
Jamie Madille7d84322017-01-10 18:21:59 -05003978template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003979GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3980 GLsizei count,
3981 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003982 const T *v)
3983{
Jamie Madill134f93d2017-08-31 17:11:00 -04003984 if (count == 1)
3985 return 1;
3986
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003987 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003988
Corentin Wallez15ac5342016-11-03 17:06:39 -04003989 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3990 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003991 unsigned int remainingElements =
3992 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003993 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003994 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003995
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003996 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003997 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003998 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003999 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05004000
4001 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004002}
4003
4004template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004005GLsizei Program::clampMatrixUniformCount(GLint location,
4006 GLsizei count,
4007 GLboolean transpose,
4008 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004009{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004010 const VariableLocation &locationInfo = mState.mUniformLocations[location];
4011
Jamie Madill62d31cb2015-09-11 13:25:51 -04004012 if (!transpose)
4013 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004014 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004015 }
4016
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004017 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04004018
4019 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
4020 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03004021 unsigned int remainingElements =
4022 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04004023 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04004024}
4025
Jamie Madill54164b02017-08-28 15:17:37 -04004026// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
4027// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04004028template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04004029void Program::getUniformInternal(const Context *context,
4030 DestT *dataOut,
4031 GLint location,
4032 GLenum nativeType,
4033 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04004034{
Jamie Madill54164b02017-08-28 15:17:37 -04004035 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04004036 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04004037 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04004038 {
4039 GLint tempValue[16] = {0};
4040 mProgram->getUniformiv(context, location, tempValue);
4041 UniformStateQueryCastLoop<GLboolean>(
4042 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004043 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004044 }
4045 case GL_INT:
4046 {
4047 GLint tempValue[16] = {0};
4048 mProgram->getUniformiv(context, location, tempValue);
4049 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4050 components);
4051 break;
4052 }
4053 case GL_UNSIGNED_INT:
4054 {
4055 GLuint tempValue[16] = {0};
4056 mProgram->getUniformuiv(context, location, tempValue);
4057 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
4058 components);
4059 break;
4060 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004061 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04004062 {
4063 GLfloat tempValue[16] = {0};
4064 mProgram->getUniformfv(context, location, tempValue);
4065 UniformStateQueryCastLoop<GLfloat>(
4066 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04004067 break;
Jamie Madill54164b02017-08-28 15:17:37 -04004068 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04004069 default:
4070 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04004071 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04004072 }
4073}
Jamie Madilla4595b82017-01-11 17:36:34 -05004074
4075bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
4076{
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004077 ASSERT(mLinkResolved);
Jamie Madilla4595b82017-01-11 17:36:34 -05004078 // Must be called after samplers are validated.
4079 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
4080
4081 for (const auto &binding : mState.mSamplerBindings)
4082 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08004083 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05004084 for (const auto &unit : binding.boundTextureUnits)
4085 {
4086 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
4087 if (programTextureID == textureID)
4088 {
4089 // TODO(jmadill): Check for appropriate overlap.
4090 return true;
4091 }
4092 }
4093 }
4094
4095 return false;
4096}
4097
Jamie Madill6f755b22018-10-09 12:48:54 -04004098angle::Result Program::syncState(const Context *context)
Jamie Madill70aeda42018-08-20 12:17:40 -04004099{
4100 if (mDirtyBits.any())
4101 {
Jamie Madill44a6fbf2018-10-02 13:38:56 -04004102 ASSERT(mLinkResolved);
Jamie Madill70aeda42018-08-20 12:17:40 -04004103 ANGLE_TRY(mProgram->syncState(context, mDirtyBits));
4104 mDirtyBits.reset();
4105 }
4106
Jamie Madill7c985f52018-11-29 18:16:17 -05004107 return angle::Result::Continue;
Jamie Madill70aeda42018-08-20 12:17:40 -04004108}
Jamie Madilla2c74982016-12-12 11:20:42 -05004109} // namespace gl