blob: bf1d3e1ca28049d4df765c2f44f3b8b270a037bd [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"
26#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040027#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040028#include "libANGLE/queryconversions.h"
29#include "libANGLE/renderer/GLImplFactory.h"
30#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040031#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050032
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033namespace gl
34{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000035
Geoff Lang7dd2e102014-11-10 15:19:26 -050036namespace
37{
38
Jamie Madill62d31cb2015-09-11 13:25:51 -040039// This simplified cast function doesn't need to worry about advanced concepts like
40// depth range values, or casting to bool.
41template <typename DestT, typename SrcT>
42DestT UniformStateQueryCast(SrcT value);
43
44// From-Float-To-Integer Casts
45template <>
46GLint UniformStateQueryCast(GLfloat value)
47{
48 return clampCast<GLint>(roundf(value));
49}
50
51template <>
52GLuint UniformStateQueryCast(GLfloat value)
53{
54 return clampCast<GLuint>(roundf(value));
55}
56
57// From-Integer-to-Integer Casts
58template <>
59GLint UniformStateQueryCast(GLuint value)
60{
61 return clampCast<GLint>(value);
62}
63
64template <>
65GLuint UniformStateQueryCast(GLint value)
66{
67 return clampCast<GLuint>(value);
68}
69
70// From-Boolean-to-Anything Casts
71template <>
72GLfloat UniformStateQueryCast(GLboolean value)
73{
Geoff Lang92019432017-11-20 13:09:34 -050074 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040075}
76
77template <>
78GLint UniformStateQueryCast(GLboolean value)
79{
Geoff Lang92019432017-11-20 13:09:34 -050080 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040081}
82
83template <>
84GLuint UniformStateQueryCast(GLboolean value)
85{
Geoff Lang92019432017-11-20 13:09:34 -050086 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040087}
88
89// Default to static_cast
90template <typename DestT, typename SrcT>
91DestT UniformStateQueryCast(SrcT value)
92{
93 return static_cast<DestT>(value);
94}
95
96template <typename SrcT, typename DestT>
97void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
98{
99 for (int comp = 0; comp < components; ++comp)
100 {
101 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
102 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
103 size_t offset = comp * 4;
104 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
105 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
106 }
107}
108
jchen1015015f72017-03-16 13:54:21 +0800109template <typename VarT>
110GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
111{
Olli Etuahod2551232017-10-26 20:03:33 +0300112 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800113 for (size_t index = 0; index < list.size(); index++)
114 {
115 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300116 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800117 {
Olli Etuahod2551232017-10-26 20:03:33 +0300118 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800119 }
120 }
121
122 return GL_INVALID_INDEX;
123}
124
Olli Etuahod2551232017-10-26 20:03:33 +0300125template <typename VarT>
126GLint GetVariableLocation(const std::vector<VarT> &list,
127 const std::vector<VariableLocation> &locationList,
128 const std::string &name)
129{
130 size_t nameLengthWithoutArrayIndex;
131 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
132
133 for (size_t location = 0u; location < locationList.size(); ++location)
134 {
135 const VariableLocation &variableLocation = locationList[location];
136 if (!variableLocation.used())
137 {
138 continue;
139 }
140
141 const VarT &variable = list[variableLocation.index];
142
143 if (angle::BeginsWith(variable.name, name))
144 {
145 if (name.length() == variable.name.length())
146 {
147 ASSERT(name == variable.name);
148 // GLES 3.1 November 2016 page 87.
149 // The string exactly matches the name of the active variable.
150 return static_cast<GLint>(location);
151 }
152 if (name.length() + 3u == variable.name.length() && variable.isArray())
153 {
154 ASSERT(name + "[0]" == variable.name);
155 // The string identifies the base name of an active array, where the string would
156 // exactly match the name of the variable if the suffix "[0]" were appended to the
157 // string.
158 return static_cast<GLint>(location);
159 }
160 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300161 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300162 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
163 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
164 {
165 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
166 // The string identifies an active element of the array, where the string ends with the
167 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
168 // zeroes, or whitespace) identifying an array element, and the "]" character, the
169 // integer is less than the number of active elements of the array variable, and where
170 // the string would exactly match the enumerated name of the array if the decimal
171 // integer were replaced with zero.
172 return static_cast<GLint>(location);
173 }
174 }
175
176 return -1;
177}
178
jchen10fd7c3b52017-03-21 15:36:03 +0800179void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
180{
181 ASSERT(bufSize > 0);
182 strncpy(buffer, string.c_str(), bufSize);
183 buffer[bufSize - 1] = '\0';
184
185 if (length)
186 {
187 *length = static_cast<GLsizei>(strlen(buffer));
188 }
189}
190
jchen10a9042d32017-03-17 08:50:45 +0800191bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
192{
Olli Etuahoc8538042017-09-27 11:20:15 +0300193 std::vector<unsigned int> subscripts;
194 std::string baseName = ParseResourceName(name, &subscripts);
195 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800196 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300197 std::vector<unsigned int> arrayIndices;
198 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
199 if (baseName == arrayName &&
200 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800201 {
202 return true;
203 }
204 }
205 return false;
206}
207
Jiawei Shao40786bd2018-04-18 13:58:57 +0800208std::string GetInterfaceBlockLimitName(ShaderType shaderType, sh::BlockType blockType)
209{
210 std::ostringstream stream;
211 stream << "GL_MAX_" << GetShaderTypeString(shaderType) << "_";
212
213 switch (blockType)
214 {
215 case sh::BlockType::BLOCK_UNIFORM:
216 stream << "UNIFORM_BUFFERS";
217 break;
218 case sh::BlockType::BLOCK_BUFFER:
219 stream << "SHADER_STORAGE_BLOCKS";
220 break;
221 default:
222 UNREACHABLE();
223 return "";
224 }
225
226 if (shaderType == ShaderType::Geometry)
227 {
228 stream << "_EXT";
229 }
230
231 return stream.str();
232}
233
234const char *GetInterfaceBlockTypeString(sh::BlockType blockType)
235{
236 switch (blockType)
237 {
238 case sh::BlockType::BLOCK_UNIFORM:
239 return "uniform block";
240 case sh::BlockType::BLOCK_BUFFER:
241 return "shader storage block";
242 default:
243 UNREACHABLE();
244 return "";
245 }
246}
247
248void LogInterfaceBlocksExceedLimit(InfoLog &infoLog,
249 ShaderType shaderType,
250 sh::BlockType blockType,
251 GLuint limit)
252{
253 infoLog << GetShaderTypeString(shaderType) << " shader "
254 << GetInterfaceBlockTypeString(blockType) << " count exceeds "
255 << GetInterfaceBlockLimitName(shaderType, blockType) << " (" << limit << ")";
256}
257
Jiawei Shao427071e2018-03-19 09:21:37 +0800258bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800259 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800260 ShaderType shaderType,
261 sh::BlockType blockType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800262 GLuint *combinedInterfaceBlocksCount,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800263 InfoLog &infoLog)
264{
265 GLuint blockCount = 0;
266 for (const sh::InterfaceBlock &block : interfaceBlocks)
267 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800268 if (IsActiveInterfaceBlock(block))
Jiajia Qin729b2c62017-08-14 09:36:11 +0800269 {
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800270 blockCount += std::max(block.arraySize, 1u);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800271 if (blockCount > maxInterfaceBlocks)
272 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800273 LogInterfaceBlocksExceedLimit(infoLog, shaderType, blockType, maxInterfaceBlocks);
Jiajia Qin729b2c62017-08-14 09:36:11 +0800274 return false;
275 }
276 }
277 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800278
279 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
280 // If a uniform block is used by multiple shader stages, each such use counts separately
281 // against this combined limit.
282 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
283 // If a shader storage block in a program is referenced by multiple shaders, each such
284 // reference counts separately against this combined limit.
285 if (combinedInterfaceBlocksCount)
286 {
287 *combinedInterfaceBlocksCount += blockCount;
288 }
289
Jiajia Qin729b2c62017-08-14 09:36:11 +0800290 return true;
291}
292
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800293GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
294{
295 std::vector<unsigned int> subscripts;
296 std::string baseName = ParseResourceName(name, &subscripts);
297
298 unsigned int numBlocks = static_cast<unsigned int>(list.size());
299 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
300 {
301 const auto &block = list[blockIndex];
302 if (block.name == baseName)
303 {
304 const bool arrayElementZero =
305 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
306 const bool arrayElementMatches =
307 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
308 if (arrayElementMatches || arrayElementZero)
309 {
310 return blockIndex;
311 }
312 }
313 }
314
315 return GL_INVALID_INDEX;
316}
317
318void GetInterfaceBlockName(const GLuint index,
319 const std::vector<InterfaceBlock> &list,
320 GLsizei bufSize,
321 GLsizei *length,
322 GLchar *name)
323{
324 ASSERT(index < list.size());
325
326 const auto &block = list[index];
327
328 if (bufSize > 0)
329 {
330 std::string blockName = block.name;
331
332 if (block.isArray)
333 {
334 blockName += ArrayString(block.arrayElement);
335 }
336 CopyStringToBuffer(name, blockName, bufSize, length);
337 }
338}
339
Jamie Madillc9727f32017-11-07 12:37:07 -0500340void InitUniformBlockLinker(const gl::Context *context,
341 const ProgramState &state,
342 UniformBlockLinker *blockLinker)
343{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800344 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500345 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800346 Shader *shader = state.getAttachedShader(shaderType);
347 if (shader)
348 {
349 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks(context));
350 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500351 }
352}
353
354void InitShaderStorageBlockLinker(const gl::Context *context,
355 const ProgramState &state,
356 ShaderStorageBlockLinker *blockLinker)
357{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800358 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500359 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800360 Shader *shader = state.getAttachedShader(shaderType);
361 if (shader != nullptr)
362 {
363 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks(context));
364 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500365 }
366}
367
jchen108225e732017-11-14 16:29:03 +0800368// Find the matching varying or field by name.
369const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
370 const std::string &name)
371{
372 const sh::ShaderVariable *var = nullptr;
373 for (const auto &ref : varyings)
374 {
375 const sh::Varying *varying = ref.second.get();
376 if (varying->name == name)
377 {
378 var = varying;
379 break;
380 }
381 var = FindShaderVarField(*varying, name);
382 if (var != nullptr)
383 {
384 break;
385 }
386 }
387 return var;
388}
389
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800390void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
391{
392 ASSERT(mismatchedFieldName);
393 if (mismatchedFieldName->empty())
394 {
395 *mismatchedFieldName = parentName;
396 }
397 else
398 {
399 std::ostringstream stream;
400 stream << parentName << "." << *mismatchedFieldName;
401 *mismatchedFieldName = stream.str();
402 }
403}
404
405const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
406{
407 switch (linkError)
408 {
409 case LinkMismatchError::TYPE_MISMATCH:
410 return "Type";
411 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
412 return "Array size";
413 case LinkMismatchError::PRECISION_MISMATCH:
414 return "Precision";
415 case LinkMismatchError::STRUCT_NAME_MISMATCH:
416 return "Structure name";
417 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
418 return "Field number";
419 case LinkMismatchError::FIELD_NAME_MISMATCH:
420 return "Field name";
421
422 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
423 return "Interpolation type";
424 case LinkMismatchError::INVARIANCE_MISMATCH:
425 return "Invariance";
426
427 case LinkMismatchError::BINDING_MISMATCH:
428 return "Binding layout qualifier";
429 case LinkMismatchError::LOCATION_MISMATCH:
430 return "Location layout qualifier";
431 case LinkMismatchError::OFFSET_MISMATCH:
432 return "Offset layout qualilfier";
433
434 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
435 return "Layout qualifier";
436 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
437 return "Matrix Packing";
438 default:
439 UNREACHABLE();
440 return "";
441 }
442}
443
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800444LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
445 const sh::InterfaceBlockField &blockField2,
446 bool webglCompatibility,
447 std::string *mismatchedBlockFieldName)
448{
449 if (blockField1.name != blockField2.name)
450 {
451 return LinkMismatchError::FIELD_NAME_MISMATCH;
452 }
453
454 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
455 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
456 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
457 if (linkError != LinkMismatchError::NO_MISMATCH)
458 {
459 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
460 return linkError;
461 }
462
463 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
464 {
465 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
466 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
467 }
468
469 return LinkMismatchError::NO_MISMATCH;
470}
471
472LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
473 const sh::InterfaceBlock &interfaceBlock2,
474 bool webglCompatibility,
475 std::string *mismatchedBlockFieldName)
476{
477 // validate blocks for the same member types
478 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
479 {
480 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
481 }
482 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
483 {
484 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
485 }
486 if (interfaceBlock1.layout != interfaceBlock2.layout ||
487 interfaceBlock1.binding != interfaceBlock2.binding)
488 {
489 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
490 }
491 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
492 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
493 {
494 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
495 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
496
497 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
498 member1, member2, webglCompatibility, mismatchedBlockFieldName);
499 if (linkError != LinkMismatchError::NO_MISMATCH)
500 {
501 return linkError;
502 }
503 }
504 return LinkMismatchError::NO_MISMATCH;
505}
506
Jiawei Shao40786bd2018-04-18 13:58:57 +0800507using ShaderInterfaceBlock = std::pair<ShaderType, const sh::InterfaceBlock *>;
508using InterfaceBlockMap = std::map<std::string, ShaderInterfaceBlock>;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800509
Jiawei Shao40786bd2018-04-18 13:58:57 +0800510void InitializeInterfaceBlockMap(const std::vector<sh::InterfaceBlock> &interfaceBlocks,
511 ShaderType shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800512 InterfaceBlockMap *linkedInterfaceBlocks)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800513{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800514 ASSERT(linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800515
516 for (const sh::InterfaceBlock &interfaceBlock : interfaceBlocks)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800517 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800518 (*linkedInterfaceBlocks)[interfaceBlock.name] = std::make_pair(shaderType, &interfaceBlock);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800519 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800520}
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800521
Jiawei Shao40786bd2018-04-18 13:58:57 +0800522bool ValidateGraphicsInterfaceBlocksPerShader(
523 const std::vector<sh::InterfaceBlock> &interfaceBlocksToLink,
524 ShaderType shaderType,
525 bool webglCompatibility,
526 InterfaceBlockMap *linkedBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +0800527 InfoLog &infoLog)
528{
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800529 ASSERT(linkedBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800530
531 for (const sh::InterfaceBlock &block : interfaceBlocksToLink)
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800532 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800533 const auto &entry = linkedBlocks->find(block.name);
534 if (entry != linkedBlocks->end())
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800535 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800536 const sh::InterfaceBlock &linkedBlock = *(entry->second.second);
537 std::string mismatchedStructFieldName;
538 LinkMismatchError linkError = AreMatchingInterfaceBlocks(
539 block, linkedBlock, webglCompatibility, &mismatchedStructFieldName);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800540 if (linkError != LinkMismatchError::NO_MISMATCH)
541 {
Jiawei Shao40786bd2018-04-18 13:58:57 +0800542 LogLinkMismatch(infoLog, block.name, GetInterfaceBlockTypeString(block.blockType),
543 linkError, mismatchedStructFieldName, entry->second.first,
544 shaderType);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800545 return false;
546 }
547 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800548 else
549 {
550 (*linkedBlocks)[block.name] = std::make_pair(shaderType, &block);
551 }
Jiawei Shao40786bd2018-04-18 13:58:57 +0800552 }
553
554 return true;
555}
556
557bool ValidateGraphicsInterfaceBlocks(
558 const ShaderMap<const std::vector<sh::InterfaceBlock> *> &shaderInterfaceBlocks,
559 InfoLog &infoLog,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800560 bool webglCompatibility)
Jiawei Shao40786bd2018-04-18 13:58:57 +0800561{
562 // Check that interface blocks defined in the graphics shaders are identical
563
564 InterfaceBlockMap linkedInterfaceBlocks;
Jiawei Shao40786bd2018-04-18 13:58:57 +0800565
566 bool interfaceBlockMapInitialized = false;
567 for (ShaderType shaderType : kAllGraphicsShaderTypes)
568 {
569 if (!shaderInterfaceBlocks[shaderType])
570 {
571 continue;
572 }
573
574 if (!interfaceBlockMapInitialized)
575 {
576 InitializeInterfaceBlockMap(*shaderInterfaceBlocks[shaderType], shaderType,
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800577 &linkedInterfaceBlocks);
Jiawei Shao40786bd2018-04-18 13:58:57 +0800578 interfaceBlockMapInitialized = true;
579 }
Jiawei Shaobb3255b2018-04-27 09:45:18 +0800580 else if (!ValidateGraphicsInterfaceBlocksPerShader(*shaderInterfaceBlocks[shaderType],
581 shaderType, webglCompatibility,
582 &linkedInterfaceBlocks, infoLog))
Jiawei Shao40786bd2018-04-18 13:58:57 +0800583 {
584 return false;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800585 }
586 }
587
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800588 return true;
589}
590
Jamie Madill62d31cb2015-09-11 13:25:51 -0400591} // anonymous namespace
592
Jamie Madill4a3c2342015-10-08 12:58:45 -0400593const char *const g_fakepath = "C:\\fakepath";
594
Jamie Madill3c1da042017-11-27 18:33:40 -0500595// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400596InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000597{
598}
599
600InfoLog::~InfoLog()
601{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000602}
603
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400604size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000605{
Jamie Madill23176ce2017-07-31 14:14:33 -0400606 if (!mLazyStream)
607 {
608 return 0;
609 }
610
611 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400612 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000613}
614
Geoff Lange1a27752015-10-05 13:16:04 -0400615void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000616{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400617 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000618
619 if (bufSize > 0)
620 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400621 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400622
Jamie Madill23176ce2017-07-31 14:14:33 -0400623 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000624 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400625 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
626 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000627 }
628
629 infoLog[index] = '\0';
630 }
631
632 if (length)
633 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400634 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000635 }
636}
637
638// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300639// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000640// messages, so lets remove all occurrences of this fake file path from the log.
641void InfoLog::appendSanitized(const char *message)
642{
Jamie Madill23176ce2017-07-31 14:14:33 -0400643 ensureInitialized();
644
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000645 std::string msg(message);
646
647 size_t found;
648 do
649 {
650 found = msg.find(g_fakepath);
651 if (found != std::string::npos)
652 {
653 msg.erase(found, strlen(g_fakepath));
654 }
655 }
656 while (found != std::string::npos);
657
Jamie Madill23176ce2017-07-31 14:14:33 -0400658 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000659}
660
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000661void InfoLog::reset()
662{
Jiawei Shao02f15232017-12-27 10:10:28 +0800663 if (mLazyStream)
664 {
665 mLazyStream.reset(nullptr);
666 }
667}
668
669bool InfoLog::empty() const
670{
671 if (!mLazyStream)
672 {
673 return true;
674 }
675
676 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000677}
678
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800679void LogLinkMismatch(InfoLog &infoLog,
680 const std::string &variableName,
681 const char *variableType,
682 LinkMismatchError linkError,
683 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800684 ShaderType shaderType1,
685 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800686{
687 std::ostringstream stream;
688 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
689 << variableName;
690
691 if (!mismatchedStructOrBlockFieldName.empty())
692 {
693 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
694 }
695
696 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
697 << GetShaderTypeString(shaderType2) << " shaders.";
698
699 infoLog << stream.str();
700}
701
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800702bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
703{
704 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200705 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800706}
707
Jamie Madill3c1da042017-11-27 18:33:40 -0500708// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300709VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000710{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500711}
712
Olli Etuahoc8538042017-09-27 11:20:15 +0300713VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300714 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500715{
Olli Etuahoc8538042017-09-27 11:20:15 +0300716 ASSERT(arrayIndex != GL_INVALID_INDEX);
717}
718
Jamie Madill3c1da042017-11-27 18:33:40 -0500719// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800720SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500721 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
722{
723}
724
725SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
726
727SamplerBinding::~SamplerBinding() = default;
728
Jamie Madill3c1da042017-11-27 18:33:40 -0500729// ProgramBindings implementation.
730ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500731{
732}
733
Jamie Madill3c1da042017-11-27 18:33:40 -0500734ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500735{
736}
737
Jamie Madill3c1da042017-11-27 18:33:40 -0500738void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400739{
740 mBindings[name] = index;
741}
742
Jamie Madill3c1da042017-11-27 18:33:40 -0500743int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400744{
745 auto iter = mBindings.find(name);
746 return (iter != mBindings.end()) ? iter->second : -1;
747}
748
Jamie Madill3c1da042017-11-27 18:33:40 -0500749ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400750{
751 return mBindings.begin();
752}
753
Jamie Madill3c1da042017-11-27 18:33:40 -0500754ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400755{
756 return mBindings.end();
757}
758
Jamie Madill3c1da042017-11-27 18:33:40 -0500759// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500760ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
761{
762}
763ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
764{
765 for (size_t index = 0; index < count; ++index)
766 {
767 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
768 }
769}
770
771ImageBinding::ImageBinding(const ImageBinding &other) = default;
772
773ImageBinding::~ImageBinding() = default;
774
Jamie Madill3c1da042017-11-27 18:33:40 -0500775// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400776ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500777 : mLabel(),
Jiawei Shao016105b2018-04-12 16:38:31 +0800778 mAttachedShaders({}),
Geoff Langc5629752015-12-07 16:29:04 -0500779 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400780 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500781 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800782 mImageUniformRange(0, 0),
783 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300784 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800785 mNumViews(-1),
786 // [GL_EXT_geometry_shader] Table 20.22
787 mGeometryShaderInputPrimitiveType(GL_TRIANGLES),
788 mGeometryShaderOutputPrimitiveType(GL_TRIANGLE_STRIP),
789 mGeometryShaderInvocations(1),
790 mGeometryShaderMaxVertices(0)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400791{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300792 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400793}
794
Jamie Madill48ef11b2016-04-27 15:21:52 -0400795ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400796{
Jiawei Shao016105b2018-04-12 16:38:31 +0800797 ASSERT(!hasAttachedShader());
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400798}
799
Jamie Madill48ef11b2016-04-27 15:21:52 -0400800const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500801{
802 return mLabel;
803}
804
Jiawei Shao385b3e02018-03-21 09:43:28 +0800805Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
806{
Jiawei Shao016105b2018-04-12 16:38:31 +0800807 ASSERT(shaderType != ShaderType::InvalidEnum);
808 return mAttachedShaders[shaderType];
Jiawei Shao385b3e02018-03-21 09:43:28 +0800809}
810
Jamie Madille7d84322017-01-10 18:21:59 -0500811GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400812{
jchen1015015f72017-03-16 13:54:21 +0800813 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814}
815
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800816GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
817{
818 return GetResourceIndexFromName(mBufferVariables, name);
819}
820
Jamie Madille7d84322017-01-10 18:21:59 -0500821GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
822{
823 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
824 return mUniformLocations[location].index;
825}
826
827Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
828{
829 GLuint index = getUniformIndexFromLocation(location);
830 if (!isSamplerUniformIndex(index))
831 {
832 return Optional<GLuint>::Invalid();
833 }
834
835 return getSamplerIndexFromUniformIndex(index);
836}
837
838bool ProgramState::isSamplerUniformIndex(GLuint index) const
839{
Jamie Madill982f6e02017-06-07 14:33:04 -0400840 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500841}
842
843GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
844{
845 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400846 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500847}
848
Jamie Madill34ca4f52017-06-13 11:49:39 -0400849GLuint ProgramState::getAttributeLocation(const std::string &name) const
850{
851 for (const sh::Attribute &attribute : mAttributes)
852 {
853 if (attribute.name == name)
854 {
855 return attribute.location;
856 }
857 }
858
859 return static_cast<GLuint>(-1);
860}
861
Jiawei Shao016105b2018-04-12 16:38:31 +0800862bool ProgramState::hasAttachedShader() const
863{
864 for (const Shader *shader : mAttachedShaders)
865 {
866 if (shader)
867 {
868 return true;
869 }
870 }
871 return false;
872}
873
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500874Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400875 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400876 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500877 mLinked(false),
878 mDeleteStatus(false),
879 mRefCount(0),
880 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500881 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500882{
883 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000884
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
888Program::~Program()
889{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400890 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
Jamie Madill4928b7c2017-06-20 12:57:39 -0400893void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500894{
Jiawei Shao016105b2018-04-12 16:38:31 +0800895 for (ShaderType shaderType : AllShaderTypes())
Jamie Madill6c1f6712017-02-14 19:08:04 -0500896 {
Jiawei Shao016105b2018-04-12 16:38:31 +0800897 if (mState.mAttachedShaders[shaderType])
898 {
899 mState.mAttachedShaders[shaderType]->release(context);
900 mState.mAttachedShaders[shaderType] = nullptr;
901 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800902 }
903
Jamie Madillb7d924a2018-03-10 11:16:54 -0500904 // TODO(jmadill): Handle error in the Context.
905 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400906
Jiawei Shao016105b2018-04-12 16:38:31 +0800907 ASSERT(!mState.hasAttachedShader());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400908 SafeDelete(mProgram);
909
910 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500911}
912
Geoff Lang70d0f492015-12-10 17:45:46 -0500913void Program::setLabel(const std::string &label)
914{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400915 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500916}
917
918const std::string &Program::getLabel() const
919{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400920 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500921}
922
Jamie Madillef300b12016-10-07 15:12:09 -0400923void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924{
Jiawei Shao016105b2018-04-12 16:38:31 +0800925 ShaderType shaderType = shader->getType();
926 ASSERT(shaderType != ShaderType::InvalidEnum);
927
928 mState.mAttachedShaders[shaderType] = shader;
929 mState.mAttachedShaders[shaderType]->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930}
931
Jamie Madillc1d770e2017-04-13 17:31:24 -0400932void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933{
Jiawei Shao016105b2018-04-12 16:38:31 +0800934 ShaderType shaderType = shader->getType();
935 ASSERT(shaderType != ShaderType::InvalidEnum);
936
937 ASSERT(mState.mAttachedShaders[shaderType] == shader);
938 shader->release(context);
939 mState.mAttachedShaders[shaderType] = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000942int Program::getAttachedShadersCount() const
943{
Jiawei Shao016105b2018-04-12 16:38:31 +0800944 int numAttachedShaders = 0;
945 for (const Shader *shader : mState.mAttachedShaders)
946 {
947 if (shader)
948 {
949 ++numAttachedShaders;
950 }
951 }
952
953 return numAttachedShaders;
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000954}
955
Jiawei Shao385b3e02018-03-21 09:43:28 +0800956const Shader *Program::getAttachedShader(ShaderType shaderType) const
957{
958 return mState.getAttachedShader(shaderType);
959}
960
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961void Program::bindAttributeLocation(GLuint index, const char *name)
962{
Geoff Langd8605522016-04-13 10:19:12 -0400963 mAttributeBindings.bindLocation(index, name);
964}
965
966void Program::bindUniformLocation(GLuint index, const char *name)
967{
Olli Etuahod2551232017-10-26 20:03:33 +0300968 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969}
970
Sami Väisänen46eaa942016-06-29 10:26:37 +0300971void Program::bindFragmentInputLocation(GLint index, const char *name)
972{
973 mFragmentInputBindings.bindLocation(index, name);
974}
975
Jamie Madillbd044ed2017-06-05 12:59:21 -0400976BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300977{
978 BindingInfo ret;
979 ret.type = GL_NONE;
980 ret.valid = false;
981
Jiawei Shao385b3e02018-03-21 09:43:28 +0800982 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300983 ASSERT(fragmentShader);
984
985 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800986 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300987
988 for (const auto &binding : mFragmentInputBindings)
989 {
990 if (binding.second != static_cast<GLuint>(index))
991 continue;
992
993 ret.valid = true;
994
Olli Etuahod2551232017-10-26 20:03:33 +0300995 size_t nameLengthWithoutArrayIndex;
996 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300997
998 for (const auto &in : inputs)
999 {
Olli Etuahod2551232017-10-26 20:03:33 +03001000 if (in.name.length() == nameLengthWithoutArrayIndex &&
1001 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +03001002 {
1003 if (in.isArray())
1004 {
1005 // The client wants to bind either "name" or "name[0]".
1006 // GL ES 3.1 spec refers to active array names with language such as:
1007 // "if the string identifies the base name of an active array, where the
1008 // string would exactly match the name of the variable if the suffix "[0]"
1009 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001010 if (arrayIndex == GL_INVALID_INDEX)
1011 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001012
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001013 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001014 }
1015 else
1016 {
1017 ret.name = in.mappedName;
1018 }
1019 ret.type = in.type;
1020 return ret;
1021 }
1022 }
1023 }
1024
1025 return ret;
1026}
1027
Jamie Madillbd044ed2017-06-05 12:59:21 -04001028void Program::pathFragmentInputGen(const Context *context,
1029 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001030 GLenum genMode,
1031 GLint components,
1032 const GLfloat *coeffs)
1033{
1034 // If the location is -1 then the command is silently ignored
1035 if (index == -1)
1036 return;
1037
Jamie Madillbd044ed2017-06-05 12:59:21 -04001038 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001039
1040 // If the input doesn't exist then then the command is silently ignored
1041 // This could happen through optimization for example, the shader translator
1042 // decides that a variable is not actually being used and optimizes it away.
1043 if (binding.name.empty())
1044 return;
1045
1046 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1047}
1048
Martin Radev4c4c8e72016-08-04 12:25:34 +03001049// The attached shaders are checked for linking errors by matching up their variables.
1050// Uniform, input and output variables get collected.
1051// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001052Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001053{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001054 const auto &data = context->getContextState();
1055
Jamie Madill6c58b062017-08-01 13:44:25 -04001056 auto *platform = ANGLEPlatformCurrent();
1057 double startTime = platform->currentTime(platform);
1058
Jamie Madill6c1f6712017-02-14 19:08:04 -05001059 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001060 mInfoLog.reset();
1061
1062 // Validate we have properly attached shaders before checking the cache.
1063 if (!linkValidateShaders(context, mInfoLog))
1064 {
1065 return NoError();
1066 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001067
Jamie Madill32447362017-06-28 14:53:52 -04001068 ProgramHash programHash;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001069 MemoryProgramCache *cache = context->getMemoryProgramCache();
Jamie Madill32447362017-06-28 14:53:52 -04001070 if (cache)
1071 {
1072 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001073 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001074 }
1075
1076 if (mLinked)
1077 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001078 double delta = platform->currentTime(platform) - startTime;
1079 int us = static_cast<int>(delta * 1000000.0);
1080 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001081 return NoError();
1082 }
1083
1084 // Cache load failed, fall through to normal linking.
1085 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001086
Jamie Madill6bc264a2018-03-31 15:36:05 -04001087 // Re-link shaders after the unlink call.
1088 ASSERT(linkValidateShaders(context, mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001089
Jiawei Shao016105b2018-04-12 16:38:31 +08001090 if (mState.mAttachedShaders[ShaderType::Compute])
Jamie Madill437d2662014-12-05 14:23:35 -05001091 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001092 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001093 {
1094 return NoError();
1095 }
1096
Jiajia Qin729b2c62017-08-14 09:36:11 +08001097 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001098 {
1099 return NoError();
1100 }
1101
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001102 ProgramLinkedResources resources = {
1103 {0, PackMode::ANGLE_RELAXED},
1104 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001105 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1106 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001107
1108 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1109 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1110
1111 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001112 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001113 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001114 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001115 }
1116 }
1117 else
1118 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001119 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001120 {
1121 return NoError();
1122 }
1123
Jamie Madillbd044ed2017-06-05 12:59:21 -04001124 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001125 {
1126 return NoError();
1127 }
1128
Jamie Madillbd044ed2017-06-05 12:59:21 -04001129 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001130 {
1131 return NoError();
1132 }
1133
Jiajia Qin729b2c62017-08-14 09:36:11 +08001134 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001135 {
1136 return NoError();
1137 }
1138
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001139 if (!linkValidateGlobalNames(context, mInfoLog))
1140 {
1141 return NoError();
1142 }
1143
Jamie Madillbd044ed2017-06-05 12:59:21 -04001144 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001145
Jiawei Shao016105b2018-04-12 16:38:31 +08001146 ASSERT(mState.mAttachedShaders[ShaderType::Vertex]);
1147 mState.mNumViews = mState.mAttachedShaders[ShaderType::Vertex]->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +03001148
Jamie Madillbd044ed2017-06-05 12:59:21 -04001149 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001150
Jamie Madill192745a2016-12-22 15:58:21 -05001151 // Map the varyings to the register file
1152 // In WebGL, we use a slightly different handling for packing variables.
Jamie Madill61d53252018-01-31 14:49:24 -05001153 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1154 if (data.getLimitations().noFlexibleVaryingPacking)
1155 {
1156 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1157 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1158 }
1159 else if (data.getExtensions().webglCompatibility)
1160 {
1161 packMode = PackMode::WEBGL_STRICT;
1162 }
Jamie Madillc9727f32017-11-07 12:37:07 -05001163
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001164 ProgramLinkedResources resources = {
1165 {data.getCaps().maxVaryingVectors, packMode},
1166 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001167 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1168 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001169
1170 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1171 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1172
Jiawei Shao73618602017-12-20 15:47:15 +08001173 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001174 {
1175 return NoError();
1176 }
1177
jchen1085c93c42017-11-12 15:36:47 +08001178 if (!resources.varyingPacking.collectAndPackUserVaryings(
1179 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001180 {
1181 return NoError();
1182 }
1183
Jamie Madillc9727f32017-11-07 12:37:07 -05001184 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001185 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001186 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001187 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001188 }
1189
1190 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001191 }
1192
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001193 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001194
jchen10eaef1e52017-06-13 10:44:11 +08001195 setUniformValuesFromBindingQualifiers();
1196
Yunchao Heece12532017-11-21 15:50:21 +08001197 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1198 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001199 ASSERT(mLinked);
1200 updateLinkedShaderStages();
1201
Jamie Madill54164b02017-08-28 15:17:37 -04001202 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001203 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001204
Jamie Madill32447362017-06-28 14:53:52 -04001205 // Save to the program cache.
1206 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
1207 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
1208 {
1209 cache->putProgram(programHash, context, this);
1210 }
1211
Jamie Madill6c58b062017-08-01 13:44:25 -04001212 double delta = platform->currentTime(platform) - startTime;
1213 int us = static_cast<int>(delta * 1000000.0);
1214 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
1215
Martin Radev4c4c8e72016-08-04 12:25:34 +03001216 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001217}
1218
Yunchao He85072e82017-11-14 15:43:28 +08001219void Program::updateLinkedShaderStages()
1220{
Yunchao Heece12532017-11-21 15:50:21 +08001221 mState.mLinkedShaderStages.reset();
1222
Jiawei Shao016105b2018-04-12 16:38:31 +08001223 for (const Shader *shader : mState.mAttachedShaders)
Yunchao He85072e82017-11-14 15:43:28 +08001224 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001225 if (shader)
1226 {
1227 mState.mLinkedShaderStages.set(shader->getType());
1228 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001229 }
Yunchao He85072e82017-11-14 15:43:28 +08001230}
1231
James Darpinian30b604d2018-03-12 17:26:57 -07001232void ProgramState::updateTransformFeedbackStrides()
1233{
1234 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1235 {
1236 mTransformFeedbackStrides.resize(1);
1237 size_t totalSize = 0;
1238 for (auto &varying : mLinkedTransformFeedbackVaryings)
1239 {
1240 totalSize += varying.size() * VariableExternalSize(varying.type);
1241 }
1242 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1243 }
1244 else
1245 {
1246 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1247 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1248 {
1249 auto &varying = mLinkedTransformFeedbackVaryings[i];
1250 mTransformFeedbackStrides[i] =
1251 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1252 }
1253 }
1254}
1255
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001256// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001257void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001259 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001260 mState.mAttributesTypeMask.reset();
1261 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001262 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001263 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001264 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001265 mState.mUniforms.clear();
1266 mState.mUniformLocations.clear();
1267 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001268 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001269 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001270 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001271 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001272 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001273 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001274 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001275 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001276 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001277 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001278 mState.mNumViews = -1;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001279 mState.mGeometryShaderInputPrimitiveType = GL_TRIANGLES;
1280 mState.mGeometryShaderOutputPrimitiveType = GL_TRIANGLE_STRIP;
1281 mState.mGeometryShaderInvocations = 1;
1282 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284 mValidated = false;
1285
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001286 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001287 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001288}
1289
Geoff Lange1a27752015-10-05 13:16:04 -04001290bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001291{
1292 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293}
1294
Jiawei Shao385b3e02018-03-21 09:43:28 +08001295bool Program::hasLinkedShaderStage(ShaderType shaderType) const
1296{
1297 ASSERT(shaderType != ShaderType::InvalidEnum);
1298 return mState.mLinkedShaderStages[shaderType];
1299}
1300
Jamie Madilla2c74982016-12-12 11:20:42 -05001301Error Program::loadBinary(const Context *context,
1302 GLenum binaryFormat,
1303 const void *binary,
1304 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001305{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001306 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001307
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001309 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001311 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1312 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001313 {
Jamie Madillf6113162015-05-07 11:49:21 -04001314 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001315 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001316 }
1317
Jamie Madill4f86d052017-06-05 12:59:26 -04001318 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1319 ANGLE_TRY_RESULT(
1320 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001321
1322 // Currently we require the full shader text to compute the program hash.
1323 // TODO(jmadill): Store the binary in the internal program cache.
1324
Jamie Madillb0a838b2016-11-13 20:02:12 -05001325 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001326#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001327}
1328
Jamie Madilla2c74982016-12-12 11:20:42 -05001329Error Program::saveBinary(const Context *context,
1330 GLenum *binaryFormat,
1331 void *binary,
1332 GLsizei bufSize,
1333 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001334{
1335 if (binaryFormat)
1336 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001337 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338 }
1339
Jamie Madill4f86d052017-06-05 12:59:26 -04001340 angle::MemoryBuffer memoryBuf;
1341 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342
Jamie Madill4f86d052017-06-05 12:59:26 -04001343 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1344 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001345
1346 if (streamLength > bufSize)
1347 {
1348 if (length)
1349 {
1350 *length = 0;
1351 }
1352
1353 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1354 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1355 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001356 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357 }
1358
1359 if (binary)
1360 {
1361 char *ptr = reinterpret_cast<char*>(binary);
1362
Jamie Madill48ef11b2016-04-27 15:21:52 -04001363 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364 ptr += streamLength;
1365
1366 ASSERT(ptr - streamLength == binary);
1367 }
1368
1369 if (length)
1370 {
1371 *length = streamLength;
1372 }
1373
He Yunchaoacd18982017-01-04 10:46:42 +08001374 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375}
1376
Jamie Madillffe00c02017-06-27 16:26:55 -04001377GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378{
1379 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001380 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381 if (error.isError())
1382 {
1383 return 0;
1384 }
1385
1386 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001387}
1388
Geoff Langc5629752015-12-07 16:29:04 -05001389void Program::setBinaryRetrievableHint(bool retrievable)
1390{
1391 // TODO(jmadill) : replace with dirty bits
1392 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001393 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001394}
1395
1396bool Program::getBinaryRetrievableHint() const
1397{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001398 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001399}
1400
Yunchao He61afff12017-03-14 15:34:03 +08001401void Program::setSeparable(bool separable)
1402{
1403 // TODO(yunchao) : replace with dirty bits
1404 if (mState.mSeparable != separable)
1405 {
1406 mProgram->setSeparable(separable);
1407 mState.mSeparable = separable;
1408 }
1409}
1410
1411bool Program::isSeparable() const
1412{
1413 return mState.mSeparable;
1414}
1415
Jamie Madill6c1f6712017-02-14 19:08:04 -05001416void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001417{
1418 mRefCount--;
1419
1420 if (mRefCount == 0 && mDeleteStatus)
1421 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001422 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001423 }
1424}
1425
1426void Program::addRef()
1427{
1428 mRefCount++;
1429}
1430
1431unsigned int Program::getRefCount() const
1432{
1433 return mRefCount;
1434}
1435
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001436int Program::getInfoLogLength() const
1437{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001438 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001439}
1440
Geoff Lange1a27752015-10-05 13:16:04 -04001441void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001442{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001443 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001444}
1445
Geoff Lange1a27752015-10-05 13:16:04 -04001446void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001447{
1448 int total = 0;
1449
Jiawei Shao016105b2018-04-12 16:38:31 +08001450 for (const Shader *shader : mState.mAttachedShaders)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001451 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001452 if (shader && (total < maxCount))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001453 {
Jiawei Shao016105b2018-04-12 16:38:31 +08001454 shaders[total] = shader->getHandle();
1455 ++total;
Jiawei Shao89be29a2017-11-06 14:36:45 +08001456 }
1457 }
1458
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001459 if (count)
1460 {
1461 *count = total;
1462 }
1463}
1464
Geoff Lange1a27752015-10-05 13:16:04 -04001465GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001467 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468}
1469
Jamie Madill63805b42015-08-25 13:17:39 -04001470bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001471{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001472 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1473 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
jchen10fd7c3b52017-03-21 15:36:03 +08001476void Program::getActiveAttribute(GLuint index,
1477 GLsizei bufsize,
1478 GLsizei *length,
1479 GLint *size,
1480 GLenum *type,
1481 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001482{
Jamie Madillc349ec02015-08-21 16:53:12 -04001483 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001484 {
1485 if (bufsize > 0)
1486 {
1487 name[0] = '\0';
1488 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001490 if (length)
1491 {
1492 *length = 0;
1493 }
1494
1495 *type = GL_NONE;
1496 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001497 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001498 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001499
jchen1036e120e2017-03-14 14:53:58 +08001500 ASSERT(index < mState.mAttributes.size());
1501 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001502
1503 if (bufsize > 0)
1504 {
jchen10fd7c3b52017-03-21 15:36:03 +08001505 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001506 }
1507
1508 // Always a single 'type' instance
1509 *size = 1;
1510 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001511}
1512
Geoff Lange1a27752015-10-05 13:16:04 -04001513GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001514{
Jamie Madillc349ec02015-08-21 16:53:12 -04001515 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001516 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001517 return 0;
1518 }
1519
jchen1036e120e2017-03-14 14:53:58 +08001520 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001521}
1522
Geoff Lange1a27752015-10-05 13:16:04 -04001523GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001524{
Jamie Madillc349ec02015-08-21 16:53:12 -04001525 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001526 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001527 return 0;
1528 }
1529
1530 size_t maxLength = 0;
1531
Jamie Madill48ef11b2016-04-27 15:21:52 -04001532 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001533 {
jchen1036e120e2017-03-14 14:53:58 +08001534 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001535 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001536
Jamie Madillc349ec02015-08-21 16:53:12 -04001537 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001538}
1539
jchen1015015f72017-03-16 13:54:21 +08001540GLuint Program::getInputResourceIndex(const GLchar *name) const
1541{
Olli Etuahod2551232017-10-26 20:03:33 +03001542 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001543}
1544
1545GLuint Program::getOutputResourceIndex(const GLchar *name) const
1546{
1547 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1548}
1549
jchen10fd7c3b52017-03-21 15:36:03 +08001550size_t Program::getOutputResourceCount() const
1551{
1552 return (mLinked ? mState.mOutputVariables.size() : 0);
1553}
1554
jchen10baf5d942017-08-28 20:45:48 +08001555template <typename T>
1556void Program::getResourceName(GLuint index,
1557 const std::vector<T> &resources,
1558 GLsizei bufSize,
1559 GLsizei *length,
1560 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001561{
1562 if (length)
1563 {
1564 *length = 0;
1565 }
1566
1567 if (!mLinked)
1568 {
1569 if (bufSize > 0)
1570 {
1571 name[0] = '\0';
1572 }
1573 return;
1574 }
jchen10baf5d942017-08-28 20:45:48 +08001575 ASSERT(index < resources.size());
1576 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001577
1578 if (bufSize > 0)
1579 {
Olli Etuahod2551232017-10-26 20:03:33 +03001580 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001581 }
1582}
1583
jchen10baf5d942017-08-28 20:45:48 +08001584void Program::getInputResourceName(GLuint index,
1585 GLsizei bufSize,
1586 GLsizei *length,
1587 GLchar *name) const
1588{
1589 getResourceName(index, mState.mAttributes, bufSize, length, name);
1590}
1591
1592void Program::getOutputResourceName(GLuint index,
1593 GLsizei bufSize,
1594 GLsizei *length,
1595 GLchar *name) const
1596{
1597 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1598}
1599
1600void Program::getUniformResourceName(GLuint index,
1601 GLsizei bufSize,
1602 GLsizei *length,
1603 GLchar *name) const
1604{
1605 getResourceName(index, mState.mUniforms, bufSize, length, name);
1606}
1607
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001608void Program::getBufferVariableResourceName(GLuint index,
1609 GLsizei bufSize,
1610 GLsizei *length,
1611 GLchar *name) const
1612{
1613 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1614}
1615
jchen10880683b2017-04-12 16:21:55 +08001616const sh::Attribute &Program::getInputResource(GLuint index) const
1617{
1618 ASSERT(index < mState.mAttributes.size());
1619 return mState.mAttributes[index];
1620}
1621
1622const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1623{
1624 ASSERT(index < mState.mOutputVariables.size());
1625 return mState.mOutputVariables[index];
1626}
1627
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628GLint Program::getFragDataLocation(const std::string &name) const
1629{
Olli Etuahod2551232017-10-26 20:03:33 +03001630 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001631}
1632
Geoff Lange1a27752015-10-05 13:16:04 -04001633void Program::getActiveUniform(GLuint index,
1634 GLsizei bufsize,
1635 GLsizei *length,
1636 GLint *size,
1637 GLenum *type,
1638 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001639{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001641 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001642 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001643 ASSERT(index < mState.mUniforms.size());
1644 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645
1646 if (bufsize > 0)
1647 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001648 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001649 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650 }
1651
Olli Etuaho465835d2017-09-26 13:34:10 +03001652 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001654 }
1655 else
1656 {
1657 if (bufsize > 0)
1658 {
1659 name[0] = '\0';
1660 }
1661
1662 if (length)
1663 {
1664 *length = 0;
1665 }
1666
1667 *size = 0;
1668 *type = GL_NONE;
1669 }
1670}
1671
Geoff Lange1a27752015-10-05 13:16:04 -04001672GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001673{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001675 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001676 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001677 }
1678 else
1679 {
1680 return 0;
1681 }
1682}
1683
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001684size_t Program::getActiveBufferVariableCount() const
1685{
1686 return mLinked ? mState.mBufferVariables.size() : 0;
1687}
1688
Geoff Lange1a27752015-10-05 13:16:04 -04001689GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001690{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001691 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001692
1693 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001694 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001695 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001697 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001699 size_t length = uniform.name.length() + 1u;
1700 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001701 {
1702 length += 3; // Counting in "[0]".
1703 }
1704 maxLength = std::max(length, maxLength);
1705 }
1706 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001707 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001708
Jamie Madill62d31cb2015-09-11 13:25:51 -04001709 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001710}
1711
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712bool Program::isValidUniformLocation(GLint location) const
1713{
Jamie Madille2e406c2016-06-02 13:04:10 -04001714 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001715 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001716 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001717}
1718
Jamie Madill62d31cb2015-09-11 13:25:51 -04001719const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001720{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001722 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001723}
1724
Jamie Madillac4e9c32017-01-13 14:07:12 -05001725const VariableLocation &Program::getUniformLocation(GLint location) const
1726{
1727 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1728 return mState.mUniformLocations[location];
1729}
1730
1731const std::vector<VariableLocation> &Program::getUniformLocations() const
1732{
1733 return mState.mUniformLocations;
1734}
1735
1736const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1737{
1738 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1739 return mState.mUniforms[index];
1740}
1741
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001742const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1743{
1744 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1745 return mState.mBufferVariables[index];
1746}
1747
Jamie Madill62d31cb2015-09-11 13:25:51 -04001748GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001749{
Olli Etuahod2551232017-10-26 20:03:33 +03001750 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751}
1752
Jamie Madill62d31cb2015-09-11 13:25:51 -04001753GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001754{
Jamie Madille7d84322017-01-10 18:21:59 -05001755 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001756}
1757
1758void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1759{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001760 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1761 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001762 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763}
1764
1765void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1766{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001767 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1768 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001769 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770}
1771
1772void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1773{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001774 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1775 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001776 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777}
1778
1779void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1780{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001781 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1782 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001783 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784}
1785
Jamie Madill81c2e252017-09-09 23:32:46 -04001786Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001788 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1789 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1790
Jamie Madill81c2e252017-09-09 23:32:46 -04001791 mProgram->setUniform1iv(location, clampedCount, v);
1792
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001793 if (mState.isSamplerUniformIndex(locationInfo.index))
1794 {
1795 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001796 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001797 }
1798
Jamie Madill81c2e252017-09-09 23:32:46 -04001799 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001800}
1801
1802void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1803{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001804 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1805 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001806 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807}
1808
1809void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1810{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001811 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1812 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001813 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814}
1815
1816void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1817{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001818 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1819 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001820 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821}
1822
1823void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1824{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001825 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1826 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001827 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001828}
1829
1830void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1831{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001832 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1833 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001834 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001835}
1836
1837void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1838{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001839 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1840 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001841 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842}
1843
1844void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1845{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001846 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1847 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001848 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849}
1850
1851void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1852{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001853 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001854 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001855}
1856
1857void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1858{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001859 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001860 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861}
1862
1863void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1864{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001865 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001866 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867}
1868
1869void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1870{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001871 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001872 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873}
1874
1875void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1876{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001877 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001878 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879}
1880
1881void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1882{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001883 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001884 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885}
1886
1887void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1888{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001889 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001890 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891}
1892
1893void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1894{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001895 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001896 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897}
1898
1899void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1900{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001901 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001902 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001903}
1904
Jamie Madill54164b02017-08-28 15:17:37 -04001905void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001906{
Jamie Madill54164b02017-08-28 15:17:37 -04001907 const auto &uniformLocation = mState.getUniformLocations()[location];
1908 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1909
1910 GLenum nativeType = gl::VariableComponentType(uniform.type);
1911 if (nativeType == GL_FLOAT)
1912 {
1913 mProgram->getUniformfv(context, location, v);
1914 }
1915 else
1916 {
1917 getUniformInternal(context, v, location, nativeType,
1918 gl::VariableComponentCount(uniform.type));
1919 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001920}
1921
Jamie Madill54164b02017-08-28 15:17:37 -04001922void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923{
Jamie Madill54164b02017-08-28 15:17:37 -04001924 const auto &uniformLocation = mState.getUniformLocations()[location];
1925 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1926
1927 GLenum nativeType = gl::VariableComponentType(uniform.type);
1928 if (nativeType == GL_INT || nativeType == GL_BOOL)
1929 {
1930 mProgram->getUniformiv(context, location, v);
1931 }
1932 else
1933 {
1934 getUniformInternal(context, v, location, nativeType,
1935 gl::VariableComponentCount(uniform.type));
1936 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001937}
1938
Jamie Madill54164b02017-08-28 15:17:37 -04001939void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001940{
Jamie Madill54164b02017-08-28 15:17:37 -04001941 const auto &uniformLocation = mState.getUniformLocations()[location];
1942 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1943
1944 GLenum nativeType = gl::VariableComponentType(uniform.type);
1945 if (nativeType == GL_UNSIGNED_INT)
1946 {
1947 mProgram->getUniformuiv(context, location, v);
1948 }
1949 else
1950 {
1951 getUniformInternal(context, v, location, nativeType,
1952 gl::VariableComponentCount(uniform.type));
1953 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954}
1955
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956void Program::flagForDeletion()
1957{
1958 mDeleteStatus = true;
1959}
1960
1961bool Program::isFlaggedForDeletion() const
1962{
1963 return mDeleteStatus;
1964}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001965
Brandon Jones43a53e22014-08-28 16:23:22 -07001966void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001967{
1968 mInfoLog.reset();
1969
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001971 {
Geoff Lang92019432017-11-20 13:09:34 -05001972 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001973 }
1974 else
1975 {
Jamie Madillf6113162015-05-07 11:49:21 -04001976 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001977 }
1978}
1979
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1981{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001982 // Skip cache if we're using an infolog, so we get the full error.
1983 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1984 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1985 {
1986 return mCachedValidateSamplersResult.value();
1987 }
1988
1989 if (mTextureUnitTypesCache.empty())
1990 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001991 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001992 }
1993 else
1994 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001995 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(),
1996 TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001997 }
1998
1999 // if any two active samplers in a program are of different types, but refer to the same
2000 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2001 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05002002 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002003 {
Jamie Madill54164b02017-08-28 15:17:37 -04002004 if (samplerBinding.unreferenced)
2005 continue;
2006
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002007 TextureType textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002008
Jamie Madille7d84322017-01-10 18:21:59 -05002009 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002010 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002011 if (textureUnit >= caps.maxCombinedTextureImageUnits)
2012 {
2013 if (infoLog)
2014 {
2015 (*infoLog) << "Sampler uniform (" << textureUnit
2016 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
2017 << caps.maxCombinedTextureImageUnits << ")";
2018 }
2019
2020 mCachedValidateSamplersResult = false;
2021 return false;
2022 }
2023
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002024 if (mTextureUnitTypesCache[textureUnit] != TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002025 {
2026 if (textureType != mTextureUnitTypesCache[textureUnit])
2027 {
2028 if (infoLog)
2029 {
2030 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2031 "image unit ("
2032 << textureUnit << ").";
2033 }
2034
2035 mCachedValidateSamplersResult = false;
2036 return false;
2037 }
2038 }
2039 else
2040 {
2041 mTextureUnitTypesCache[textureUnit] = textureType;
2042 }
2043 }
2044 }
2045
2046 mCachedValidateSamplersResult = true;
2047 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048}
2049
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002050bool Program::isValidated() const
2051{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002052 return mValidated;
2053}
2054
Geoff Lange1a27752015-10-05 13:16:04 -04002055GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002057 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058}
2059
jchen1058f67be2017-10-27 08:59:27 +08002060GLuint Program::getActiveAtomicCounterBufferCount() const
2061{
2062 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2063}
2064
Jiajia Qin729b2c62017-08-14 09:36:11 +08002065GLuint Program::getActiveShaderStorageBlockCount() const
2066{
2067 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2068}
2069
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002070void Program::getActiveUniformBlockName(const GLuint blockIndex,
2071 GLsizei bufSize,
2072 GLsizei *length,
2073 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002075 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2076}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002078void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2079 GLsizei bufSize,
2080 GLsizei *length,
2081 GLchar *blockName) const
2082{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002084 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002085}
2086
Qin Jiajia9bf55522018-01-29 13:56:23 +08002087template <typename T>
2088GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002089{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 int maxLength = 0;
2091
2092 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002093 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002094 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002096 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002097 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002098 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002099 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100 }
2101 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002102 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103
2104 return maxLength;
2105}
2106
Qin Jiajia9bf55522018-01-29 13:56:23 +08002107GLint Program::getActiveUniformBlockMaxNameLength() const
2108{
2109 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2110}
2111
2112GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2113{
2114 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2115}
2116
Geoff Lange1a27752015-10-05 13:16:04 -04002117GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002119 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2120}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002121
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002122GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2123{
2124 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002125}
2126
Jiajia Qin729b2c62017-08-14 09:36:11 +08002127const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002128{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002129 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2130 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002131}
2132
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002133const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2134{
2135 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2136 return mState.mShaderStorageBlocks[index];
2137}
2138
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002139void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2140{
jchen107a20b972017-06-13 14:25:26 +08002141 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002142 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04002143 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002144}
2145
2146GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2147{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002148 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002149}
2150
Jiajia Qin729b2c62017-08-14 09:36:11 +08002151GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2152{
2153 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2154}
2155
Geoff Lang48dcae72014-02-05 16:28:24 -05002156void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
2157{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002158 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002159 for (GLsizei i = 0; i < count; i++)
2160 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002161 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002162 }
2163
Jamie Madill48ef11b2016-04-27 15:21:52 -04002164 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002165}
2166
2167void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
2168{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002170 {
jchen10a9042d32017-03-17 08:50:45 +08002171 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2172 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2173 std::string varName = var.nameWithArrayIndex();
2174 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002175 if (length)
2176 {
2177 *length = lastNameIdx;
2178 }
2179 if (size)
2180 {
jchen10a9042d32017-03-17 08:50:45 +08002181 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002182 }
2183 if (type)
2184 {
jchen10a9042d32017-03-17 08:50:45 +08002185 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002186 }
2187 if (name)
2188 {
jchen10a9042d32017-03-17 08:50:45 +08002189 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002190 name[lastNameIdx] = '\0';
2191 }
2192 }
2193}
2194
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002195GLsizei Program::getTransformFeedbackVaryingCount() const
2196{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002198 {
jchen10a9042d32017-03-17 08:50:45 +08002199 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002200 }
2201 else
2202 {
2203 return 0;
2204 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002205}
2206
2207GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2208{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002210 {
2211 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002212 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002213 {
jchen10a9042d32017-03-17 08:50:45 +08002214 maxSize =
2215 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002216 }
2217
2218 return maxSize;
2219 }
2220 else
2221 {
2222 return 0;
2223 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002224}
2225
2226GLenum Program::getTransformFeedbackBufferMode() const
2227{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002228 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002229}
2230
Jiawei Shao73618602017-12-20 15:47:15 +08002231bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
2232{
Jiawei Shao016105b2018-04-12 16:38:31 +08002233 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2234 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
2235 Shader *computeShader = mState.mAttachedShaders[ShaderType::Compute];
2236 Shader *geometryShader = mState.mAttachedShaders[ShaderType::Geometry];
Jiawei Shao73618602017-12-20 15:47:15 +08002237
2238 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002239 bool isGraphicsShaderAttached =
2240 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002241 // Check whether we both have a compute and non-compute shaders attached.
2242 // If there are of both types attached, then linking should fail.
2243 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2244 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2245 {
2246 infoLog << "Both compute and graphics shaders are attached to the same program.";
2247 return false;
2248 }
2249
2250 if (computeShader)
2251 {
2252 if (!computeShader->isCompiled(context))
2253 {
2254 infoLog << "Attached compute shader is not compiled.";
2255 return false;
2256 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002257 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002258
2259 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
2260
2261 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2262 // If the work group size is not specified, a link time error should occur.
2263 if (!mState.mComputeShaderLocalSize.isDeclared())
2264 {
2265 infoLog << "Work group size is not specified.";
2266 return false;
2267 }
2268 }
2269 else
2270 {
2271 if (!fragmentShader || !fragmentShader->isCompiled(context))
2272 {
2273 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2274 return false;
2275 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002276 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002277
2278 if (!vertexShader || !vertexShader->isCompiled(context))
2279 {
2280 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2281 return false;
2282 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002283 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002284
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002285 int vertexShaderVersion = vertexShader->getShaderVersion(context);
2286 if (fragmentShader->getShaderVersion(context) != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002287 {
2288 infoLog << "Fragment shader version does not match vertex shader version.";
2289 return false;
2290 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002291
2292 if (geometryShader)
2293 {
2294 // [GL_EXT_geometry_shader] Chapter 7
2295 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2296 // Language Specification, as well as any of the following reasons:
2297 // * One or more of the shader objects attached to <program> are not compiled
2298 // successfully.
2299 // * The shaders do not use the same shader language version.
2300 // * <program> contains objects to form a geometry shader, and
2301 // - <program> is not separable and contains no objects to form a vertex shader; or
2302 // - the input primitive type, output primitive type, or maximum output vertex count
2303 // is not specified in the compiled geometry shader object.
2304 if (!geometryShader->isCompiled(context))
2305 {
2306 infoLog << "The attached geometry shader isn't compiled.";
2307 return false;
2308 }
2309
2310 if (geometryShader->getShaderVersion(context) != vertexShaderVersion)
2311 {
2312 mInfoLog << "Geometry shader version does not match vertex shader version.";
2313 return false;
2314 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002315 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002316
2317 Optional<GLenum> inputPrimitive =
2318 geometryShader->getGeometryShaderInputPrimitiveType(context);
2319 if (!inputPrimitive.valid())
2320 {
2321 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2322 return false;
2323 }
2324
2325 Optional<GLenum> outputPrimitive =
2326 geometryShader->getGeometryShaderOutputPrimitiveType(context);
2327 if (!outputPrimitive.valid())
2328 {
2329 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2330 return false;
2331 }
2332
2333 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices(context);
2334 if (!maxVertices.valid())
2335 {
2336 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2337 return false;
2338 }
2339
2340 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2341 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2342 mState.mGeometryShaderMaxVertices = maxVertices.value();
2343 mState.mGeometryShaderInvocations =
2344 geometryShader->getGeometryShaderInvocations(context);
2345 }
Jiawei Shao73618602017-12-20 15:47:15 +08002346 }
2347
2348 return true;
2349}
2350
jchen10910a3da2017-11-15 09:40:11 +08002351GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2352{
2353 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2354 {
2355 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2356 if (tf.nameWithArrayIndex() == name)
2357 {
2358 return tfIndex;
2359 }
2360 }
2361 return GL_INVALID_INDEX;
2362}
2363
2364const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2365{
2366 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2367 return mState.mLinkedTransformFeedbackVaryings[index];
2368}
2369
Jamie Madillbd044ed2017-06-05 12:59:21 -04002370bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371{
Jiawei Shao016105b2018-04-12 16:38:31 +08002372 Shader *previousShader = nullptr;
2373 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002374 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002375 Shader *currentShader = mState.mAttachedShaders[shaderType];
2376 if (!currentShader)
Jiawei Shaod063aff2018-02-22 10:19:09 +08002377 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002378 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002379 }
Jiawei Shao016105b2018-04-12 16:38:31 +08002380
2381 if (previousShader)
2382 {
2383 if (!linkValidateShaderInterfaceMatching(context, previousShader, currentShader,
2384 infoLog))
2385 {
2386 return false;
2387 }
2388 }
2389 previousShader = currentShader;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002390 }
2391
2392 if (!linkValidateBuiltInVaryings(context, infoLog))
2393 {
2394 return false;
2395 }
2396
2397 if (!linkValidateFragmentInputBindings(context, infoLog))
2398 {
2399 return false;
2400 }
2401
2402 return true;
2403}
2404
2405// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2406// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
2407bool Program::linkValidateShaderInterfaceMatching(const Context *context,
2408 gl::Shader *generatingShader,
2409 gl::Shader *consumingShader,
2410 gl::InfoLog &infoLog) const
2411{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002412 ASSERT(generatingShader->getShaderVersion(context) ==
2413 consumingShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002414
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002415 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context);
2416 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002417
Jiawei Shao385b3e02018-03-21 09:43:28 +08002418 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002419
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002420 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002422 bool matched = false;
2423
2424 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002425 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002426 {
2427 continue;
2428 }
2429
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002430 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002431 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002432 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002433 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002434 ASSERT(!output.isBuiltIn());
2435
2436 std::string mismatchedStructFieldName;
2437 LinkMismatchError linkError =
2438 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002439 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002440 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002442 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2443 mismatchedStructFieldName, generatingShader->getType(),
2444 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002445 return false;
2446 }
2447
Geoff Lang7dd2e102014-11-10 15:19:26 -05002448 matched = true;
2449 break;
2450 }
2451 }
2452
Olli Etuaho107c7242018-03-20 15:45:35 +02002453 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2454 // whether the input is statically used - a statically used input should fail this test even
2455 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002456 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002457 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002458 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2459 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2460 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002461 return false;
2462 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002463 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002464
Jiawei Shaod063aff2018-02-22 10:19:09 +08002465 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002466
Jiawei Shaod063aff2018-02-22 10:19:09 +08002467 return true;
2468}
2469
2470bool Program::linkValidateFragmentInputBindings(const Context *context, gl::InfoLog &infoLog) const
2471{
Jiawei Shao016105b2018-04-12 16:38:31 +08002472 ASSERT(mState.mAttachedShaders[ShaderType::Fragment]);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002473
2474 std::map<GLuint, std::string> staticFragmentInputLocations;
2475
2476 const std::vector<sh::Varying> &fragmentInputVaryings =
Jiawei Shao016105b2018-04-12 16:38:31 +08002477 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings(context);
Jiawei Shaod063aff2018-02-22 10:19:09 +08002478 for (const sh::Varying &input : fragmentInputVaryings)
2479 {
2480 if (input.isBuiltIn() || !input.staticUse)
2481 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002482 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002483 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002484
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002485 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002486 if (inputBinding == -1)
2487 continue;
2488
2489 const auto it = staticFragmentInputLocations.find(inputBinding);
2490 if (it == std::end(staticFragmentInputLocations))
2491 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002492 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002493 }
2494 else
2495 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002496 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002497 << it->second;
2498 return false;
2499 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002500 }
2501
2502 return true;
2503}
2504
Jamie Madillbd044ed2017-06-05 12:59:21 -04002505bool Program::linkUniforms(const Context *context,
2506 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002507 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002508{
Olli Etuahob78707c2017-03-09 15:03:11 +00002509 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002510 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002511 {
2512 return false;
2513 }
2514
Olli Etuahob78707c2017-03-09 15:03:11 +00002515 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002516
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002517 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002518
jchen10eaef1e52017-06-13 10:44:11 +08002519 if (!linkAtomicCounterBuffers())
2520 {
2521 return false;
2522 }
2523
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002524 return true;
2525}
2526
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002527void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002528{
Jamie Madill982f6e02017-06-07 14:33:04 -04002529 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2530 unsigned int low = high;
2531
jchen10eaef1e52017-06-13 10:44:11 +08002532 for (auto counterIter = mState.mUniforms.rbegin();
2533 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2534 {
2535 --low;
2536 }
2537
2538 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2539
2540 high = low;
2541
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002542 for (auto imageIter = mState.mUniforms.rbegin();
2543 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2544 {
2545 --low;
2546 }
2547
2548 mState.mImageUniformRange = RangeUI(low, high);
2549
2550 // If uniform is a image type, insert it into the mImageBindings array.
2551 for (unsigned int imageIndex : mState.mImageUniformRange)
2552 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002553 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2554 // cannot load values into a uniform defined as an image. if declare without a
2555 // binding qualifier, any uniform image variable (include all elements of
2556 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002557 auto &imageUniform = mState.mUniforms[imageIndex];
2558 if (imageUniform.binding == -1)
2559 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002560 mState.mImageBindings.emplace_back(
2561 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002562 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002563 else
2564 {
2565 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002566 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002567 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002568 }
2569
2570 high = low;
2571
2572 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002573 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002574 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002575 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002576 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002577
2578 mState.mSamplerUniformRange = RangeUI(low, high);
2579
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002580 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002581 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002582 {
2583 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002584 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002585 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002586 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002587 }
2588}
2589
jchen10eaef1e52017-06-13 10:44:11 +08002590bool Program::linkAtomicCounterBuffers()
2591{
2592 for (unsigned int index : mState.mAtomicCounterUniformRange)
2593 {
2594 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002595 uniform.blockInfo.offset = uniform.offset;
2596 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2597 uniform.blockInfo.matrixStride = 0;
2598 uniform.blockInfo.isRowMajorMatrix = false;
2599
jchen10eaef1e52017-06-13 10:44:11 +08002600 bool found = false;
2601 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2602 ++bufferIndex)
2603 {
2604 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2605 if (buffer.binding == uniform.binding)
2606 {
2607 buffer.memberIndexes.push_back(index);
2608 uniform.bufferIndex = bufferIndex;
2609 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002610 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002611 break;
2612 }
2613 }
2614 if (!found)
2615 {
2616 AtomicCounterBuffer atomicCounterBuffer;
2617 atomicCounterBuffer.binding = uniform.binding;
2618 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002619 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002620 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2621 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2622 }
2623 }
2624 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002625 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002626
2627 return true;
2628}
2629
Jamie Madilleb979bf2016-11-15 12:28:46 -05002630// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002631bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002632{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002633 const ContextState &data = context->getContextState();
Jiawei Shao385b3e02018-03-21 09:43:28 +08002634 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002635
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002636 int shaderVersion = vertexShader->getShaderVersion(context);
2637
Geoff Lang7dd2e102014-11-10 15:19:26 -05002638 unsigned int usedLocations = 0;
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002639 if (shaderVersion >= 300)
2640 {
2641 // In GLSL ES 3.00.6, aliasing checks should be done with all declared attributes - see GLSL
2642 // ES 3.00.6 section 12.46. Inactive attributes will be pruned after aliasing checks.
2643 mState.mAttributes = vertexShader->getAllAttributes(context);
2644 }
2645 else
2646 {
2647 // In GLSL ES 1.00.17 we only do aliasing checks for active attributes.
2648 mState.mAttributes = vertexShader->getActiveAttributes(context);
2649 }
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002650 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002651
2652 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002653 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002654 {
Jamie Madillf6113162015-05-07 11:49:21 -04002655 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002656 return false;
2657 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002658
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002659 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002660
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002661 // Assign locations to attributes that have a binding location and check for attribute aliasing.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002662 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002663 {
Olli Etuahod2551232017-10-26 20:03:33 +03002664 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2665 // structures, so we don't need to worry about adjusting their names or generating entries
2666 // for each member/element (unlike uniforms for example).
2667 ASSERT(!attribute.isArray() && !attribute.isStruct());
2668
Jamie Madilleb979bf2016-11-15 12:28:46 -05002669 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002670 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002671 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002672 attribute.location = bindingLocation;
2673 }
2674
2675 if (attribute.location != -1)
2676 {
2677 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002678 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002679
Jamie Madill63805b42015-08-25 13:17:39 -04002680 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002681 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002682 infoLog << "Attribute (" << attribute.name << ") at location " << attribute.location
2683 << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002684
2685 return false;
2686 }
2687
Jamie Madill63805b42015-08-25 13:17:39 -04002688 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002689 {
Jamie Madill63805b42015-08-25 13:17:39 -04002690 const int regLocation = attribute.location + reg;
2691 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002692
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002693 // In GLSL ES 3.00.6 and in WebGL, attribute aliasing produces a link error.
2694 // In non-WebGL GLSL ES 1.00.17, attribute aliasing is allowed with some
2695 // restrictions - see GLSL ES 1.00.17 section 2.10.4, but ANGLE currently has a bug.
Jamie Madillc349ec02015-08-21 16:53:12 -04002696 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002697 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002698 // TODO(jmadill): fix aliasing on ES2
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002699 // if (shaderVersion >= 300 && !webgl)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002700 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002701 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002702 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002703 return false;
2704 }
2705 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002706 else
2707 {
Jamie Madill63805b42015-08-25 13:17:39 -04002708 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002709 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002710
Jamie Madill63805b42015-08-25 13:17:39 -04002711 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002712 }
2713 }
2714 }
2715
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002716 // Assign locations to attributes that don't have a binding location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002717 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002718 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002719 // Not set by glBindAttribLocation or by location layout qualifier
2720 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002721 {
Jamie Madill63805b42015-08-25 13:17:39 -04002722 int regs = VariableRegisterCount(attribute.type);
2723 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002724
Jamie Madill63805b42015-08-25 13:17:39 -04002725 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002727 infoLog << "Too many attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002728 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002729 }
2730
Jamie Madillc349ec02015-08-21 16:53:12 -04002731 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002732 }
2733 }
2734
Brandon Jonesc405ae72017-12-06 14:15:03 -08002735 ASSERT(mState.mAttributesTypeMask.none());
2736 ASSERT(mState.mAttributesMask.none());
2737
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002738 // Prune inactive attributes. This step is only needed on shaderVersion >= 300 since on earlier
2739 // shader versions we're only processing active attributes to begin with.
2740 if (shaderVersion >= 300)
2741 {
2742 for (auto attributeIter = mState.mAttributes.begin();
2743 attributeIter != mState.mAttributes.end();)
2744 {
2745 if (attributeIter->active)
2746 {
2747 ++attributeIter;
2748 }
2749 else
2750 {
2751 attributeIter = mState.mAttributes.erase(attributeIter);
2752 }
2753 }
2754 }
2755
Jamie Madill48ef11b2016-04-27 15:21:52 -04002756 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002757 {
Olli Etuahoebd6e2d2018-03-23 17:07:55 +02002758 ASSERT(attribute.active);
Jamie Madill63805b42015-08-25 13:17:39 -04002759 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002760 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002761
Jamie Madillbd159f02017-10-09 19:39:06 -04002762 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002763 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002764 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2765 mState.mActiveAttribLocationsMask.set(location);
2766 mState.mMaxActiveAttribLocation =
2767 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08002768
2769 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
2770 if (!attribute.isBuiltIn())
2771 {
2772 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
2773 location);
2774 mState.mAttributesMask.set(location);
2775 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002776 }
2777 }
2778
Geoff Lang7dd2e102014-11-10 15:19:26 -05002779 return true;
2780}
2781
Jiajia Qin729b2c62017-08-14 09:36:11 +08002782bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002783{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002784 const auto &caps = context->getCaps();
2785
Jiawei Shao016105b2018-04-12 16:38:31 +08002786 if (mState.mAttachedShaders[ShaderType::Compute])
Martin Radev4c4c8e72016-08-04 12:25:34 +03002787 {
Jiawei Shao016105b2018-04-12 16:38:31 +08002788 Shader &computeShader = *mState.mAttachedShaders[ShaderType::Compute];
Jiajia Qin729b2c62017-08-14 09:36:11 +08002789 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002790
Jiawei Shao40786bd2018-04-18 13:58:57 +08002791 if (!ValidateInterfaceBlocksCount(caps.maxComputeUniformBlocks, computeUniformBlocks,
2792 ShaderType::Compute, sh::BlockType::BLOCK_UNIFORM,
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002793 nullptr, infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002794 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002795 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002796 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002797
2798 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
Jiawei Shao427071e2018-03-19 09:21:37 +08002799 if (!ValidateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
Jiawei Shao40786bd2018-04-18 13:58:57 +08002800 computeShaderStorageBlocks, ShaderType::Compute,
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002801 sh::BlockType::BLOCK_BUFFER, nullptr, infoLog))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002802 {
2803 return false;
2804 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002805 return true;
2806 }
2807
Jiawei Shao40786bd2018-04-18 13:58:57 +08002808 ShaderMap<GLuint> maxShaderUniformBlocks = {};
2809 maxShaderUniformBlocks[gl::ShaderType::Vertex] = caps.maxVertexUniformBlocks;
2810 maxShaderUniformBlocks[gl::ShaderType::Fragment] = caps.maxFragmentUniformBlocks;
2811 maxShaderUniformBlocks[gl::ShaderType::Geometry] = caps.maxGeometryUniformBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002812
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002813 GLuint combinedUniformBlocksCount = 0u;
Jiawei Shao40786bd2018-04-18 13:58:57 +08002814 ShaderMap<const std::vector<sh::InterfaceBlock> *> graphicsShaderUniformBlocks = {};
2815 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002816 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08002817 Shader *shader = mState.mAttachedShaders[shaderType];
2818 if (!shader)
2819 {
2820 continue;
2821 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002822
Jiawei Shao40786bd2018-04-18 13:58:57 +08002823 const auto &uniformBlocks = mState.mAttachedShaders[shaderType]->getUniformBlocks(context);
2824 if (!ValidateInterfaceBlocksCount(maxShaderUniformBlocks[shaderType], uniformBlocks,
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002825 shaderType, sh::BlockType::BLOCK_UNIFORM,
2826 &combinedUniformBlocksCount, infoLog))
Jiawei Shao427071e2018-03-19 09:21:37 +08002827 {
2828 return false;
2829 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08002830
2831 graphicsShaderUniformBlocks[shaderType] = &uniformBlocks;
Jiawei Shao427071e2018-03-19 09:21:37 +08002832 }
2833
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002834 if (combinedUniformBlocksCount > caps.maxCombinedUniformBlocks)
2835 {
2836 infoLog << "The sum of the number of active uniform blocks exceeds "
2837 "MAX_COMBINED_UNIFORM_BLOCKS ("
2838 << caps.maxCombinedUniformBlocks << ").";
2839 return false;
2840 }
2841
Jamie Madillbd044ed2017-06-05 12:59:21 -04002842 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002843 if (!ValidateGraphicsInterfaceBlocks(graphicsShaderUniformBlocks, infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002844 {
2845 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002846 }
Jamie Madille473dee2015-08-18 14:49:01 -04002847
Jiajia Qin729b2c62017-08-14 09:36:11 +08002848 if (context->getClientVersion() >= Version(3, 1))
2849 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08002850 ShaderMap<GLuint> maxShaderStorageBlocks = {};
2851 maxShaderStorageBlocks[ShaderType::Vertex] = caps.maxVertexShaderStorageBlocks;
2852 maxShaderStorageBlocks[ShaderType::Fragment] = caps.maxFragmentShaderStorageBlocks;
2853 maxShaderStorageBlocks[ShaderType::Geometry] = caps.maxGeometryShaderStorageBlocks;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002854
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002855 GLuint combinedShaderStorageBlocksCount = 0u;
Jiawei Shao40786bd2018-04-18 13:58:57 +08002856 ShaderMap<const std::vector<sh::InterfaceBlock> *> graphicsShaderStorageBlocks = {};
2857 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Jiajia Qin729b2c62017-08-14 09:36:11 +08002858 {
Jiawei Shao40786bd2018-04-18 13:58:57 +08002859 Shader *shader = mState.mAttachedShaders[shaderType];
2860 if (!shader)
2861 {
2862 continue;
2863 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002864
Jiawei Shao40786bd2018-04-18 13:58:57 +08002865 const auto &shaderStorageBlocks = shader->getShaderStorageBlocks(context);
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002866 if (!ValidateInterfaceBlocksCount(
2867 maxShaderStorageBlocks[shaderType], shaderStorageBlocks, shaderType,
2868 sh::BlockType::BLOCK_BUFFER, &combinedShaderStorageBlocksCount, infoLog))
Jiawei Shao427071e2018-03-19 09:21:37 +08002869 {
2870 return false;
2871 }
Jiawei Shao40786bd2018-04-18 13:58:57 +08002872
2873 graphicsShaderStorageBlocks[shaderType] = &shaderStorageBlocks;
Jiawei Shao427071e2018-03-19 09:21:37 +08002874 }
2875
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002876 if (combinedShaderStorageBlocksCount > caps.maxCombinedShaderStorageBlocks)
2877 {
2878 infoLog << "The sum of the number of active shader storage blocks exceeds "
2879 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
2880 << caps.maxCombinedShaderStorageBlocks << ").";
2881 return false;
2882 }
2883
Jiawei Shao40786bd2018-04-18 13:58:57 +08002884 if (!ValidateGraphicsInterfaceBlocks(graphicsShaderStorageBlocks, infoLog,
Jiawei Shaobb3255b2018-04-27 09:45:18 +08002885 webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002886 {
2887 return false;
2888 }
2889 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002890 return true;
2891}
2892
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002893LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
2894 const sh::ShaderVariable &variable2,
2895 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002896 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002897 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002898{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002899 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002900 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002901 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002902 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002903 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002904 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002905 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002906 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002907 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002908 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002909 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002910 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002911 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04002912 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002913 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04002914 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002915
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002916 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002917 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002918 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002919 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002920 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002921 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2922 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002923 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
2924 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002925
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002926 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002927 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002928 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002929 }
2930
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002931 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08002932 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002933 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002934 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002935 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
2936 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002937 }
2938 }
2939
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002940 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002941}
2942
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002943LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
2944 const sh::Varying &inputVarying,
2945 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002946 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002947 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002948{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002949 if (validateGeometryShaderInputVarying)
2950 {
2951 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
2952 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
2953 // or outputs.
2954 ASSERT(inputVarying.arraySizes.size() == 1u);
2955
2956 // Geometry shader input varyings are not treated as arrays, so a vertex array output
2957 // varying cannot match a geometry shader input varying.
2958 // [GL_EXT_geometry_shader] Section 7.4.1:
2959 // Geometry shader per-vertex input variables and blocks are required to be declared as
2960 // arrays, with each element representing input or output values for a single vertex of a
2961 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
2962 // are treated as though they were not declared as arrays.
2963 if (outputVarying.isArray())
2964 {
2965 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
2966 }
2967 }
2968
2969 // Skip the validation on the array sizes between a vertex output varying and a geometry input
2970 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002971 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08002972 LinkValidateVariablesBase(outputVarying, inputVarying, false,
2973 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002974 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002975 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002976 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002977 }
2978
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002979 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002980 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002981 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002982 }
2983
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002984 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002985 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002986 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002987 }
2988
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002989 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002990}
2991
Jamie Madillbd044ed2017-06-05 12:59:21 -04002992bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002993{
Jiawei Shao016105b2018-04-12 16:38:31 +08002994 Shader *vertexShader = mState.mAttachedShaders[ShaderType::Vertex];
2995 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jiawei Shao3d404882017-10-16 13:30:48 +08002996 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2997 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002998 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002999
3000 if (shaderVersion != 100)
3001 {
3002 // Only ESSL 1.0 has restrictions on matching input and output invariance
3003 return true;
3004 }
3005
3006 bool glPositionIsInvariant = false;
3007 bool glPointSizeIsInvariant = false;
3008 bool glFragCoordIsInvariant = false;
3009 bool glPointCoordIsInvariant = false;
3010
3011 for (const sh::Varying &varying : vertexVaryings)
3012 {
3013 if (!varying.isBuiltIn())
3014 {
3015 continue;
3016 }
3017 if (varying.name.compare("gl_Position") == 0)
3018 {
3019 glPositionIsInvariant = varying.isInvariant;
3020 }
3021 else if (varying.name.compare("gl_PointSize") == 0)
3022 {
3023 glPointSizeIsInvariant = varying.isInvariant;
3024 }
3025 }
3026
3027 for (const sh::Varying &varying : fragmentVaryings)
3028 {
3029 if (!varying.isBuiltIn())
3030 {
3031 continue;
3032 }
3033 if (varying.name.compare("gl_FragCoord") == 0)
3034 {
3035 glFragCoordIsInvariant = varying.isInvariant;
3036 }
3037 else if (varying.name.compare("gl_PointCoord") == 0)
3038 {
3039 glPointCoordIsInvariant = varying.isInvariant;
3040 }
3041 }
3042
3043 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3044 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3045 // Not requiring invariance to match is supported by:
3046 // dEQP, WebGL CTS, Nexus 5X GLES
3047 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3048 {
3049 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3050 "declared invariant.";
3051 return false;
3052 }
3053 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3054 {
3055 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3056 "declared invariant.";
3057 return false;
3058 }
3059
3060 return true;
3061}
3062
jchen10a9042d32017-03-17 08:50:45 +08003063bool Program::linkValidateTransformFeedback(const gl::Context *context,
3064 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003065 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003066 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003067{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003068
jchen108225e732017-11-14 16:29:03 +08003069 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003070 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003071 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003072 {
jchen10a9042d32017-03-17 08:50:45 +08003073 if (context->getClientVersion() < Version(3, 1) &&
3074 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003075 {
Geoff Lang1a683462015-09-29 15:09:59 -04003076 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003077 return false;
3078 }
jchen108225e732017-11-14 16:29:03 +08003079 if (context->getClientVersion() >= Version(3, 1))
3080 {
3081 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3082 {
3083 infoLog << "Two transform feedback varyings include the same array element ("
3084 << tfVaryingName << ").";
3085 return false;
3086 }
3087 }
3088 else
3089 {
3090 if (uniqueNames.count(tfVaryingName) > 0)
3091 {
3092 infoLog << "Two transform feedback varyings specify the same output variable ("
3093 << tfVaryingName << ").";
3094 return false;
3095 }
3096 }
3097 uniqueNames.insert(tfVaryingName);
3098 }
3099
3100 // Validate against program varyings.
3101 size_t totalComponents = 0;
3102 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3103 {
3104 std::vector<unsigned int> subscripts;
3105 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3106
3107 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3108 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003109 {
3110 infoLog << "Transform feedback varying " << tfVaryingName
3111 << " does not exist in the vertex shader.";
3112 return false;
3113 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003114
jchen108225e732017-11-14 16:29:03 +08003115 // Validate the matching variable.
3116 if (var->isStruct())
3117 {
3118 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3119 return false;
3120 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003121
jchen108225e732017-11-14 16:29:03 +08003122 size_t elementCount = 0;
3123 size_t componentCount = 0;
3124
3125 if (var->isArray())
3126 {
3127 if (context->getClientVersion() < Version(3, 1))
3128 {
3129 infoLog << "Capture of arrays is undefined and not supported.";
3130 return false;
3131 }
3132
3133 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3134 ASSERT(!var->isArrayOfArrays());
3135
3136 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3137 {
3138 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3139 return false;
3140 }
3141 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3142 }
3143 else
3144 {
3145 if (!subscripts.empty())
3146 {
3147 infoLog << "Varying '" << baseName
3148 << "' is not an array to be captured by element.";
3149 return false;
3150 }
3151 elementCount = 1;
3152 }
3153
3154 // TODO(jmadill): Investigate implementation limits on D3D11
3155 componentCount = VariableComponentCount(var->type) * elementCount;
3156 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3157 componentCount > caps.maxTransformFeedbackSeparateComponents)
3158 {
3159 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3160 << componentCount << ") exceed the maximum separate components ("
3161 << caps.maxTransformFeedbackSeparateComponents << ").";
3162 return false;
3163 }
3164
3165 totalComponents += componentCount;
3166 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3167 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3168 {
3169 infoLog << "Transform feedback varying total components (" << totalComponents
3170 << ") exceed the maximum interleaved components ("
3171 << caps.maxTransformFeedbackInterleavedComponents << ").";
3172 return false;
3173 }
3174 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003175 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003176}
3177
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003178bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
3179{
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003180 const std::vector<sh::Attribute> &attributes =
Jiawei Shao016105b2018-04-12 16:38:31 +08003181 mState.mAttachedShaders[ShaderType::Vertex]->getActiveAttributes(context);
3182
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003183 for (const auto &attrib : attributes)
3184 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003185 for (ShaderType shaderType : kAllGraphicsShaderTypes)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003186 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003187 Shader *shader = mState.mAttachedShaders[shaderType];
3188 if (!shader)
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003189 {
Jiawei Shao016105b2018-04-12 16:38:31 +08003190 continue;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003191 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003192
3193 const std::vector<sh::Uniform> &uniforms = shader->getUniforms(context);
3194 for (const auto &uniform : uniforms)
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003195 {
3196 if (uniform.name == attrib.name)
3197 {
3198 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3199 return false;
3200 }
3201 }
3202 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003203 }
Jiawei Shao016105b2018-04-12 16:38:31 +08003204
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003205 return true;
3206}
3207
Jamie Madill3c1da042017-11-27 18:33:40 -05003208void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003209{
3210 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003211 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003212 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003213 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003214 std::vector<unsigned int> subscripts;
3215 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003216 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003217 if (!subscripts.empty())
3218 {
3219 subscript = subscripts.back();
3220 }
Jamie Madill192745a2016-12-22 15:58:21 -05003221 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003222 {
Jamie Madill192745a2016-12-22 15:58:21 -05003223 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003224 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003225 {
jchen10a9042d32017-03-17 08:50:45 +08003226 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3227 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003228 break;
3229 }
jchen108225e732017-11-14 16:29:03 +08003230 else if (varying->isStruct())
3231 {
3232 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3233 if (field != nullptr)
3234 {
3235 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3236 break;
3237 }
3238 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003239 }
3240 }
James Darpinian30b604d2018-03-12 17:26:57 -07003241 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003242}
3243
Jamie Madill3c1da042017-11-27 18:33:40 -05003244ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003245{
Jamie Madill3c1da042017-11-27 18:33:40 -05003246 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003247
Jiawei Shao016105b2018-04-12 16:38:31 +08003248 for (const sh::Varying &varying :
3249 mState.mAttachedShaders[ShaderType::Vertex]->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003250 {
Jamie Madill192745a2016-12-22 15:58:21 -05003251 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003252 }
3253
Jiawei Shao016105b2018-04-12 16:38:31 +08003254 for (const sh::Varying &varying :
3255 mState.mAttachedShaders[ShaderType::Fragment]->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003256 {
Jamie Madill192745a2016-12-22 15:58:21 -05003257 merged[varying.name].fragment = &varying;
3258 }
3259
3260 return merged;
3261}
3262
Jamie Madillbd044ed2017-06-05 12:59:21 -04003263void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003264{
Jiawei Shao016105b2018-04-12 16:38:31 +08003265 Shader *fragmentShader = mState.mAttachedShaders[ShaderType::Fragment];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003266 ASSERT(fragmentShader != nullptr);
3267
Geoff Lange0cff192017-05-30 13:04:56 -04003268 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003269 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003270 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003271
3272 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04003273 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04003274 {
3275 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3276 outputVariable.name != "gl_FragData")
3277 {
3278 continue;
3279 }
3280
3281 unsigned int baseLocation =
3282 (outputVariable.location == -1 ? 0u
3283 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003284
3285 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3286 // structures, so we may use getBasicTypeElementCount().
3287 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3288 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003289 {
3290 const unsigned int location = baseLocation + elementIndex;
3291 if (location >= mState.mOutputVariableTypes.size())
3292 {
3293 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3294 }
Corentin Walleze7557742017-06-01 13:09:57 -04003295 ASSERT(location < mState.mActiveOutputVariables.size());
3296 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003297 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003298 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003299 }
3300 }
3301
Jamie Madill80a6fc02015-08-21 16:53:16 -04003302 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04003303 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003304 return;
3305
Jamie Madillbd044ed2017-06-05 12:59:21 -04003306 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04003307 // TODO(jmadill): any caps validation here?
3308
jchen1015015f72017-03-16 13:54:21 +08003309 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003310 outputVariableIndex++)
3311 {
jchen1015015f72017-03-16 13:54:21 +08003312 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003313
Olli Etuahod2551232017-10-26 20:03:33 +03003314 if (outputVariable.isArray())
3315 {
3316 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3317 // Resources and including [0] at the end of array variable names.
3318 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3319 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3320 }
3321
Jamie Madill80a6fc02015-08-21 16:53:16 -04003322 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3323 if (outputVariable.isBuiltIn())
3324 continue;
3325
3326 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003327 unsigned int baseLocation =
3328 (outputVariable.location == -1 ? 0u
3329 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003330
Olli Etuaho465835d2017-09-26 13:34:10 +03003331 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3332 // structures, so we may use getBasicTypeElementCount().
3333 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3334 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003335 {
Olli Etuahod2551232017-10-26 20:03:33 +03003336 const unsigned int location = baseLocation + elementIndex;
3337 if (location >= mState.mOutputLocations.size())
3338 {
3339 mState.mOutputLocations.resize(location + 1);
3340 }
3341 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003342 if (outputVariable.isArray())
3343 {
3344 mState.mOutputLocations[location] =
3345 VariableLocation(elementIndex, outputVariableIndex);
3346 }
3347 else
3348 {
3349 VariableLocation locationInfo;
3350 locationInfo.index = outputVariableIndex;
3351 mState.mOutputLocations[location] = locationInfo;
3352 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003353 }
3354 }
3355}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003356
Olli Etuaho48fed632017-03-16 12:05:30 +00003357void Program::setUniformValuesFromBindingQualifiers()
3358{
Jamie Madill982f6e02017-06-07 14:33:04 -04003359 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003360 {
3361 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3362 if (samplerUniform.binding != -1)
3363 {
Olli Etuahod2551232017-10-26 20:03:33 +03003364 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003365 ASSERT(location != -1);
3366 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003367 for (unsigned int elementIndex = 0;
3368 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003369 {
3370 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3371 }
3372 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3373 boundTextureUnits.data());
3374 }
3375 }
3376}
3377
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003378void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003379{
jchen10af713a22017-04-19 09:10:56 +08003380 // Set initial bindings from shader.
3381 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3382 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003383 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003384 bindUniformBlock(blockIndex, uniformBlock.binding);
3385 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003386}
3387
Jamie Madille7d84322017-01-10 18:21:59 -05003388void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003389 GLsizei clampedCount,
3390 const GLint *v)
3391{
Jamie Madill81c2e252017-09-09 23:32:46 -04003392 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3393 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3394 std::vector<GLuint> *boundTextureUnits =
3395 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003396
Olli Etuaho1734e172017-10-27 15:30:27 +03003397 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003398
3399 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003400 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003401}
3402
3403template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003404GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3405 GLsizei count,
3406 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003407 const T *v)
3408{
Jamie Madill134f93d2017-08-31 17:11:00 -04003409 if (count == 1)
3410 return 1;
3411
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003412 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003413
Corentin Wallez15ac5342016-11-03 17:06:39 -04003414 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3415 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003416 unsigned int remainingElements =
3417 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003418 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003419 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003420
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003421 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003422 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003423 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003424 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003425
3426 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003427}
3428
3429template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003430GLsizei Program::clampMatrixUniformCount(GLint location,
3431 GLsizei count,
3432 GLboolean transpose,
3433 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003434{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003435 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3436
Jamie Madill62d31cb2015-09-11 13:25:51 -04003437 if (!transpose)
3438 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003439 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003440 }
3441
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003442 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003443
3444 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3445 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003446 unsigned int remainingElements =
3447 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003448 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003449}
3450
Jamie Madill54164b02017-08-28 15:17:37 -04003451// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3452// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003453template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003454void Program::getUniformInternal(const Context *context,
3455 DestT *dataOut,
3456 GLint location,
3457 GLenum nativeType,
3458 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003459{
Jamie Madill54164b02017-08-28 15:17:37 -04003460 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003461 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003462 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003463 {
3464 GLint tempValue[16] = {0};
3465 mProgram->getUniformiv(context, location, tempValue);
3466 UniformStateQueryCastLoop<GLboolean>(
3467 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003468 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003469 }
3470 case GL_INT:
3471 {
3472 GLint tempValue[16] = {0};
3473 mProgram->getUniformiv(context, location, tempValue);
3474 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3475 components);
3476 break;
3477 }
3478 case GL_UNSIGNED_INT:
3479 {
3480 GLuint tempValue[16] = {0};
3481 mProgram->getUniformuiv(context, location, tempValue);
3482 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3483 components);
3484 break;
3485 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003486 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003487 {
3488 GLfloat tempValue[16] = {0};
3489 mProgram->getUniformfv(context, location, tempValue);
3490 UniformStateQueryCastLoop<GLfloat>(
3491 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003492 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003493 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003494 default:
3495 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003496 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003497 }
3498}
Jamie Madilla4595b82017-01-11 17:36:34 -05003499
3500bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3501{
3502 // Must be called after samplers are validated.
3503 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3504
3505 for (const auto &binding : mState.mSamplerBindings)
3506 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003507 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003508 for (const auto &unit : binding.boundTextureUnits)
3509 {
3510 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3511 if (programTextureID == textureID)
3512 {
3513 // TODO(jmadill): Check for appropriate overlap.
3514 return true;
3515 }
3516 }
3517 }
3518
3519 return false;
3520}
3521
Jamie Madilla2c74982016-12-12 11:20:42 -05003522} // namespace gl