blob: b1080d89ada73b9ff23e68337ce48ce8af0a6ebf [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 Shao427071e2018-03-19 09:21:37 +0800208bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800209 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
210 const std::string &errorMessage,
211 InfoLog &infoLog)
212{
213 GLuint blockCount = 0;
214 for (const sh::InterfaceBlock &block : interfaceBlocks)
215 {
Olli Etuaho107c7242018-03-20 15:45:35 +0200216 if (block.active || block.layout != sh::BLOCKLAYOUT_PACKED)
Jiajia Qin729b2c62017-08-14 09:36:11 +0800217 {
218 blockCount += (block.arraySize ? block.arraySize : 1);
219 if (blockCount > maxInterfaceBlocks)
220 {
221 infoLog << errorMessage << maxInterfaceBlocks << ")";
222 return false;
223 }
224 }
225 }
226 return true;
227}
228
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800229GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
230{
231 std::vector<unsigned int> subscripts;
232 std::string baseName = ParseResourceName(name, &subscripts);
233
234 unsigned int numBlocks = static_cast<unsigned int>(list.size());
235 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
236 {
237 const auto &block = list[blockIndex];
238 if (block.name == baseName)
239 {
240 const bool arrayElementZero =
241 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
242 const bool arrayElementMatches =
243 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
244 if (arrayElementMatches || arrayElementZero)
245 {
246 return blockIndex;
247 }
248 }
249 }
250
251 return GL_INVALID_INDEX;
252}
253
254void GetInterfaceBlockName(const GLuint index,
255 const std::vector<InterfaceBlock> &list,
256 GLsizei bufSize,
257 GLsizei *length,
258 GLchar *name)
259{
260 ASSERT(index < list.size());
261
262 const auto &block = list[index];
263
264 if (bufSize > 0)
265 {
266 std::string blockName = block.name;
267
268 if (block.isArray)
269 {
270 blockName += ArrayString(block.arrayElement);
271 }
272 CopyStringToBuffer(name, blockName, bufSize, length);
273 }
274}
275
Jamie Madillc9727f32017-11-07 12:37:07 -0500276void InitUniformBlockLinker(const gl::Context *context,
277 const ProgramState &state,
278 UniformBlockLinker *blockLinker)
279{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800280 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500281 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800282 Shader *shader = state.getAttachedShader(shaderType);
283 if (shader)
284 {
285 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks(context));
286 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500287 }
288}
289
290void InitShaderStorageBlockLinker(const gl::Context *context,
291 const ProgramState &state,
292 ShaderStorageBlockLinker *blockLinker)
293{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800294 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500295 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800296 Shader *shader = state.getAttachedShader(shaderType);
297 if (shader != nullptr)
298 {
299 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks(context));
300 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500301 }
302}
303
jchen108225e732017-11-14 16:29:03 +0800304// Find the matching varying or field by name.
305const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
306 const std::string &name)
307{
308 const sh::ShaderVariable *var = nullptr;
309 for (const auto &ref : varyings)
310 {
311 const sh::Varying *varying = ref.second.get();
312 if (varying->name == name)
313 {
314 var = varying;
315 break;
316 }
317 var = FindShaderVarField(*varying, name);
318 if (var != nullptr)
319 {
320 break;
321 }
322 }
323 return var;
324}
325
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800326void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
327{
328 ASSERT(mismatchedFieldName);
329 if (mismatchedFieldName->empty())
330 {
331 *mismatchedFieldName = parentName;
332 }
333 else
334 {
335 std::ostringstream stream;
336 stream << parentName << "." << *mismatchedFieldName;
337 *mismatchedFieldName = stream.str();
338 }
339}
340
341const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
342{
343 switch (linkError)
344 {
345 case LinkMismatchError::TYPE_MISMATCH:
346 return "Type";
347 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
348 return "Array size";
349 case LinkMismatchError::PRECISION_MISMATCH:
350 return "Precision";
351 case LinkMismatchError::STRUCT_NAME_MISMATCH:
352 return "Structure name";
353 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
354 return "Field number";
355 case LinkMismatchError::FIELD_NAME_MISMATCH:
356 return "Field name";
357
358 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
359 return "Interpolation type";
360 case LinkMismatchError::INVARIANCE_MISMATCH:
361 return "Invariance";
362
363 case LinkMismatchError::BINDING_MISMATCH:
364 return "Binding layout qualifier";
365 case LinkMismatchError::LOCATION_MISMATCH:
366 return "Location layout qualifier";
367 case LinkMismatchError::OFFSET_MISMATCH:
368 return "Offset layout qualilfier";
369
370 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
371 return "Layout qualifier";
372 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
373 return "Matrix Packing";
374 default:
375 UNREACHABLE();
376 return "";
377 }
378}
379
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800380LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
381 const sh::InterfaceBlockField &blockField2,
382 bool webglCompatibility,
383 std::string *mismatchedBlockFieldName)
384{
385 if (blockField1.name != blockField2.name)
386 {
387 return LinkMismatchError::FIELD_NAME_MISMATCH;
388 }
389
390 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
391 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
392 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
393 if (linkError != LinkMismatchError::NO_MISMATCH)
394 {
395 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
396 return linkError;
397 }
398
399 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
400 {
401 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
402 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
403 }
404
405 return LinkMismatchError::NO_MISMATCH;
406}
407
408LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
409 const sh::InterfaceBlock &interfaceBlock2,
410 bool webglCompatibility,
411 std::string *mismatchedBlockFieldName)
412{
413 // validate blocks for the same member types
414 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
415 {
416 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
417 }
418 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
419 {
420 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
421 }
422 if (interfaceBlock1.layout != interfaceBlock2.layout ||
423 interfaceBlock1.binding != interfaceBlock2.binding)
424 {
425 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
426 }
427 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
428 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
429 {
430 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
431 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
432
433 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
434 member1, member2, webglCompatibility, mismatchedBlockFieldName);
435 if (linkError != LinkMismatchError::NO_MISMATCH)
436 {
437 return linkError;
438 }
439 }
440 return LinkMismatchError::NO_MISMATCH;
441}
442
443bool ValidateGraphicsInterfaceBlocks(const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
444 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
445 InfoLog &infoLog,
446 bool webglCompatibility,
447 sh::BlockType blockType,
448 GLuint maxCombinedInterfaceBlocks)
449{
450 // Check that interface blocks defined in the vertex and fragment shaders are identical
451 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
452 InterfaceBlockMap linkedInterfaceBlocks;
453 GLuint blockCount = 0;
454
455 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
456 {
457 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
458 if (IsActiveInterfaceBlock(vertexInterfaceBlock))
459 {
460 blockCount += std::max(vertexInterfaceBlock.arraySize, 1u);
461 }
462 }
463
464 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
465 {
466 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
467 if (entry != linkedInterfaceBlocks.end())
468 {
469 const sh::InterfaceBlock &vertexInterfaceBlock = *(entry->second);
470 std::string mismatchedBlockFieldName;
471 LinkMismatchError linkError =
472 AreMatchingInterfaceBlocks(vertexInterfaceBlock, fragmentInterfaceBlock,
473 webglCompatibility, &mismatchedBlockFieldName);
474 if (linkError != LinkMismatchError::NO_MISMATCH)
475 {
476 LogLinkMismatch(infoLog, fragmentInterfaceBlock.name, "interface block", linkError,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800477 mismatchedBlockFieldName, ShaderType::Vertex, ShaderType::Fragment);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800478 return false;
479 }
480 }
481
482 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
483 // If a uniform block is used by multiple shader stages, each such use counts separately
484 // against this combined limit.
485 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
486 // If a shader storage block in a program is referenced by multiple shaders, each such
487 // reference counts separately against this combined limit.
488 if (IsActiveInterfaceBlock(fragmentInterfaceBlock))
489 {
490 blockCount += std::max(fragmentInterfaceBlock.arraySize, 1u);
491 }
492 }
493
494 if (blockCount > maxCombinedInterfaceBlocks)
495 {
496 switch (blockType)
497 {
498 case sh::BlockType::BLOCK_UNIFORM:
499 infoLog << "The sum of the number of active uniform blocks exceeds "
500 "MAX_COMBINED_UNIFORM_BLOCKS ("
501 << maxCombinedInterfaceBlocks << ").";
502 break;
503 case sh::BlockType::BLOCK_BUFFER:
504 infoLog << "The sum of the number of active shader storage blocks exceeds "
505 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
506 << maxCombinedInterfaceBlocks << ").";
507 break;
508 default:
509 UNREACHABLE();
510 }
511 return false;
512 }
513 return true;
514}
515
Jamie Madill62d31cb2015-09-11 13:25:51 -0400516} // anonymous namespace
517
Jamie Madill4a3c2342015-10-08 12:58:45 -0400518const char *const g_fakepath = "C:\\fakepath";
519
Jamie Madill3c1da042017-11-27 18:33:40 -0500520// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400521InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000522{
523}
524
525InfoLog::~InfoLog()
526{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000527}
528
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400529size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000530{
Jamie Madill23176ce2017-07-31 14:14:33 -0400531 if (!mLazyStream)
532 {
533 return 0;
534 }
535
536 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400537 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000538}
539
Geoff Lange1a27752015-10-05 13:16:04 -0400540void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000541{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400542 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000543
544 if (bufSize > 0)
545 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400546 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400547
Jamie Madill23176ce2017-07-31 14:14:33 -0400548 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000549 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400550 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
551 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000552 }
553
554 infoLog[index] = '\0';
555 }
556
557 if (length)
558 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400559 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000560 }
561}
562
563// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300564// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000565// messages, so lets remove all occurrences of this fake file path from the log.
566void InfoLog::appendSanitized(const char *message)
567{
Jamie Madill23176ce2017-07-31 14:14:33 -0400568 ensureInitialized();
569
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000570 std::string msg(message);
571
572 size_t found;
573 do
574 {
575 found = msg.find(g_fakepath);
576 if (found != std::string::npos)
577 {
578 msg.erase(found, strlen(g_fakepath));
579 }
580 }
581 while (found != std::string::npos);
582
Jamie Madill23176ce2017-07-31 14:14:33 -0400583 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000584}
585
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000586void InfoLog::reset()
587{
Jiawei Shao02f15232017-12-27 10:10:28 +0800588 if (mLazyStream)
589 {
590 mLazyStream.reset(nullptr);
591 }
592}
593
594bool InfoLog::empty() const
595{
596 if (!mLazyStream)
597 {
598 return true;
599 }
600
601 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000602}
603
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800604void LogLinkMismatch(InfoLog &infoLog,
605 const std::string &variableName,
606 const char *variableType,
607 LinkMismatchError linkError,
608 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800609 ShaderType shaderType1,
610 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800611{
612 std::ostringstream stream;
613 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
614 << variableName;
615
616 if (!mismatchedStructOrBlockFieldName.empty())
617 {
618 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
619 }
620
621 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
622 << GetShaderTypeString(shaderType2) << " shaders.";
623
624 infoLog << stream.str();
625}
626
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800627bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
628{
629 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200630 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800631}
632
Jamie Madill3c1da042017-11-27 18:33:40 -0500633// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300634VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000635{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500636}
637
Olli Etuahoc8538042017-09-27 11:20:15 +0300638VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300639 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500640{
Olli Etuahoc8538042017-09-27 11:20:15 +0300641 ASSERT(arrayIndex != GL_INVALID_INDEX);
642}
643
Jamie Madill3c1da042017-11-27 18:33:40 -0500644// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800645SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500646 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
647{
648}
649
650SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
651
652SamplerBinding::~SamplerBinding() = default;
653
Jamie Madill3c1da042017-11-27 18:33:40 -0500654// ProgramBindings implementation.
655ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500656{
657}
658
Jamie Madill3c1da042017-11-27 18:33:40 -0500659ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500660{
661}
662
Jamie Madill3c1da042017-11-27 18:33:40 -0500663void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400664{
665 mBindings[name] = index;
666}
667
Jamie Madill3c1da042017-11-27 18:33:40 -0500668int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400669{
670 auto iter = mBindings.find(name);
671 return (iter != mBindings.end()) ? iter->second : -1;
672}
673
Jamie Madill3c1da042017-11-27 18:33:40 -0500674ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400675{
676 return mBindings.begin();
677}
678
Jamie Madill3c1da042017-11-27 18:33:40 -0500679ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400680{
681 return mBindings.end();
682}
683
Jamie Madill3c1da042017-11-27 18:33:40 -0500684// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500685ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
686{
687}
688ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
689{
690 for (size_t index = 0; index < count; ++index)
691 {
692 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
693 }
694}
695
696ImageBinding::ImageBinding(const ImageBinding &other) = default;
697
698ImageBinding::~ImageBinding() = default;
699
Jamie Madill3c1da042017-11-27 18:33:40 -0500700// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400701ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500702 : mLabel(),
703 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400704 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800706 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500707 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400708 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500709 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800710 mImageUniformRange(0, 0),
711 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300712 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800713 mNumViews(-1),
714 // [GL_EXT_geometry_shader] Table 20.22
715 mGeometryShaderInputPrimitiveType(GL_TRIANGLES),
716 mGeometryShaderOutputPrimitiveType(GL_TRIANGLE_STRIP),
717 mGeometryShaderInvocations(1),
718 mGeometryShaderMaxVertices(0)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400719{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400721}
722
Jamie Madill48ef11b2016-04-27 15:21:52 -0400723ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400724{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800725 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
726 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400727}
728
Jamie Madill48ef11b2016-04-27 15:21:52 -0400729const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500730{
731 return mLabel;
732}
733
Jiawei Shao385b3e02018-03-21 09:43:28 +0800734Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
735{
736 switch (shaderType)
737 {
738 case ShaderType::Vertex:
739 return mAttachedVertexShader;
740 case ShaderType::Fragment:
741 return mAttachedFragmentShader;
742 case ShaderType::Compute:
743 return mAttachedComputeShader;
744 case ShaderType::Geometry:
745 return mAttachedGeometryShader;
746 default:
747 UNREACHABLE();
748 return nullptr;
749 }
750}
751
Jamie Madille7d84322017-01-10 18:21:59 -0500752GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400753{
jchen1015015f72017-03-16 13:54:21 +0800754 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400755}
756
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800757GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
758{
759 return GetResourceIndexFromName(mBufferVariables, name);
760}
761
Jamie Madille7d84322017-01-10 18:21:59 -0500762GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
763{
764 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
765 return mUniformLocations[location].index;
766}
767
768Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
769{
770 GLuint index = getUniformIndexFromLocation(location);
771 if (!isSamplerUniformIndex(index))
772 {
773 return Optional<GLuint>::Invalid();
774 }
775
776 return getSamplerIndexFromUniformIndex(index);
777}
778
779bool ProgramState::isSamplerUniformIndex(GLuint index) const
780{
Jamie Madill982f6e02017-06-07 14:33:04 -0400781 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500782}
783
784GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
785{
786 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400787 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500788}
789
Jamie Madill34ca4f52017-06-13 11:49:39 -0400790GLuint ProgramState::getAttributeLocation(const std::string &name) const
791{
792 for (const sh::Attribute &attribute : mAttributes)
793 {
794 if (attribute.name == name)
795 {
796 return attribute.location;
797 }
798 }
799
800 return static_cast<GLuint>(-1);
801}
802
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500803Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400804 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400805 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500806 mLinked(false),
807 mDeleteStatus(false),
808 mRefCount(0),
809 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500810 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500811{
812 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000813
Geoff Lang7dd2e102014-11-10 15:19:26 -0500814 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815}
816
817Program::~Program()
818{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400819 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820}
821
Jamie Madill4928b7c2017-06-20 12:57:39 -0400822void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500823{
824 if (mState.mAttachedVertexShader != nullptr)
825 {
826 mState.mAttachedVertexShader->release(context);
827 mState.mAttachedVertexShader = nullptr;
828 }
829
830 if (mState.mAttachedFragmentShader != nullptr)
831 {
832 mState.mAttachedFragmentShader->release(context);
833 mState.mAttachedFragmentShader = nullptr;
834 }
835
836 if (mState.mAttachedComputeShader != nullptr)
837 {
838 mState.mAttachedComputeShader->release(context);
839 mState.mAttachedComputeShader = nullptr;
840 }
841
Jiawei Shao89be29a2017-11-06 14:36:45 +0800842 if (mState.mAttachedGeometryShader != nullptr)
843 {
844 mState.mAttachedGeometryShader->release(context);
845 mState.mAttachedGeometryShader = nullptr;
846 }
847
Jamie Madillb7d924a2018-03-10 11:16:54 -0500848 // TODO(jmadill): Handle error in the Context.
849 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400850
851 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800852 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400853 SafeDelete(mProgram);
854
855 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500856}
857
Geoff Lang70d0f492015-12-10 17:45:46 -0500858void Program::setLabel(const std::string &label)
859{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400860 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500861}
862
863const std::string &Program::getLabel() const
864{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400865 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500866}
867
Jamie Madillef300b12016-10-07 15:12:09 -0400868void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300870 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800872 case ShaderType::Vertex:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 {
Jamie Madillef300b12016-10-07 15:12:09 -0400874 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300875 mState.mAttachedVertexShader = shader;
876 mState.mAttachedVertexShader->addRef();
877 break;
878 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800879 case ShaderType::Fragment:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880 {
Jamie Madillef300b12016-10-07 15:12:09 -0400881 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300882 mState.mAttachedFragmentShader = shader;
883 mState.mAttachedFragmentShader->addRef();
884 break;
885 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800886 case ShaderType::Compute:
Martin Radev4c4c8e72016-08-04 12:25:34 +0300887 {
Jamie Madillef300b12016-10-07 15:12:09 -0400888 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300889 mState.mAttachedComputeShader = shader;
890 mState.mAttachedComputeShader->addRef();
891 break;
892 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800893 case ShaderType::Geometry:
Jiawei Shao89be29a2017-11-06 14:36:45 +0800894 {
895 ASSERT(!mState.mAttachedGeometryShader);
896 mState.mAttachedGeometryShader = shader;
897 mState.mAttachedGeometryShader->addRef();
898 break;
899 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300900 default:
901 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
904
Jamie Madillc1d770e2017-04-13 17:31:24 -0400905void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300907 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800909 case ShaderType::Vertex:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400911 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500912 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300913 mState.mAttachedVertexShader = nullptr;
914 break;
915 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800916 case ShaderType::Fragment:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400918 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500919 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300920 mState.mAttachedFragmentShader = nullptr;
921 break;
922 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800923 case ShaderType::Compute:
Martin Radev4c4c8e72016-08-04 12:25:34 +0300924 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400925 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500926 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300927 mState.mAttachedComputeShader = nullptr;
928 break;
929 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800930 case ShaderType::Geometry:
Jiawei Shao89be29a2017-11-06 14:36:45 +0800931 {
932 ASSERT(mState.mAttachedGeometryShader == shader);
933 shader->release(context);
934 mState.mAttachedGeometryShader = nullptr;
935 break;
936 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300937 default:
938 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000942int Program::getAttachedShadersCount() const
943{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300944 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800945 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000946}
947
Jiawei Shao385b3e02018-03-21 09:43:28 +0800948const Shader *Program::getAttachedShader(ShaderType shaderType) const
949{
950 return mState.getAttachedShader(shaderType);
951}
952
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953void Program::bindAttributeLocation(GLuint index, const char *name)
954{
Geoff Langd8605522016-04-13 10:19:12 -0400955 mAttributeBindings.bindLocation(index, name);
956}
957
958void Program::bindUniformLocation(GLuint index, const char *name)
959{
Olli Etuahod2551232017-10-26 20:03:33 +0300960 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
Sami Väisänen46eaa942016-06-29 10:26:37 +0300963void Program::bindFragmentInputLocation(GLint index, const char *name)
964{
965 mFragmentInputBindings.bindLocation(index, name);
966}
967
Jamie Madillbd044ed2017-06-05 12:59:21 -0400968BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300969{
970 BindingInfo ret;
971 ret.type = GL_NONE;
972 ret.valid = false;
973
Jiawei Shao385b3e02018-03-21 09:43:28 +0800974 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300975 ASSERT(fragmentShader);
976
977 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800978 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300979
980 for (const auto &binding : mFragmentInputBindings)
981 {
982 if (binding.second != static_cast<GLuint>(index))
983 continue;
984
985 ret.valid = true;
986
Olli Etuahod2551232017-10-26 20:03:33 +0300987 size_t nameLengthWithoutArrayIndex;
988 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300989
990 for (const auto &in : inputs)
991 {
Olli Etuahod2551232017-10-26 20:03:33 +0300992 if (in.name.length() == nameLengthWithoutArrayIndex &&
993 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300994 {
995 if (in.isArray())
996 {
997 // The client wants to bind either "name" or "name[0]".
998 // GL ES 3.1 spec refers to active array names with language such as:
999 // "if the string identifies the base name of an active array, where the
1000 // string would exactly match the name of the variable if the suffix "[0]"
1001 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001002 if (arrayIndex == GL_INVALID_INDEX)
1003 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001004
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001005 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001006 }
1007 else
1008 {
1009 ret.name = in.mappedName;
1010 }
1011 ret.type = in.type;
1012 return ret;
1013 }
1014 }
1015 }
1016
1017 return ret;
1018}
1019
Jamie Madillbd044ed2017-06-05 12:59:21 -04001020void Program::pathFragmentInputGen(const Context *context,
1021 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001022 GLenum genMode,
1023 GLint components,
1024 const GLfloat *coeffs)
1025{
1026 // If the location is -1 then the command is silently ignored
1027 if (index == -1)
1028 return;
1029
Jamie Madillbd044ed2017-06-05 12:59:21 -04001030 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001031
1032 // If the input doesn't exist then then the command is silently ignored
1033 // This could happen through optimization for example, the shader translator
1034 // decides that a variable is not actually being used and optimizes it away.
1035 if (binding.name.empty())
1036 return;
1037
1038 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1039}
1040
Martin Radev4c4c8e72016-08-04 12:25:34 +03001041// The attached shaders are checked for linking errors by matching up their variables.
1042// Uniform, input and output variables get collected.
1043// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001044Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001045{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001046 const auto &data = context->getContextState();
1047
Jamie Madill6c58b062017-08-01 13:44:25 -04001048 auto *platform = ANGLEPlatformCurrent();
1049 double startTime = platform->currentTime(platform);
1050
Jamie Madill6c1f6712017-02-14 19:08:04 -05001051 unlink();
Jamie Madill6bc264a2018-03-31 15:36:05 -04001052 mInfoLog.reset();
1053
1054 // Validate we have properly attached shaders before checking the cache.
1055 if (!linkValidateShaders(context, mInfoLog))
1056 {
1057 return NoError();
1058 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001059
Jamie Madill32447362017-06-28 14:53:52 -04001060 ProgramHash programHash;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001061 MemoryProgramCache *cache = context->getMemoryProgramCache();
Jamie Madill32447362017-06-28 14:53:52 -04001062 if (cache)
1063 {
1064 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001065 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001066 }
1067
1068 if (mLinked)
1069 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001070 double delta = platform->currentTime(platform) - startTime;
1071 int us = static_cast<int>(delta * 1000000.0);
1072 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001073 return NoError();
1074 }
1075
1076 // Cache load failed, fall through to normal linking.
1077 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001078
Jamie Madill6bc264a2018-03-31 15:36:05 -04001079 // Re-link shaders after the unlink call.
1080 ASSERT(linkValidateShaders(context, mInfoLog));
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001081
Jiawei Shao73618602017-12-20 15:47:15 +08001082 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -05001083 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001084 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001085 {
1086 return NoError();
1087 }
1088
Jiajia Qin729b2c62017-08-14 09:36:11 +08001089 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001090 {
1091 return NoError();
1092 }
1093
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001094 ProgramLinkedResources resources = {
1095 {0, PackMode::ANGLE_RELAXED},
1096 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001097 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1098 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001099
1100 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1101 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1102
1103 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001104 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001105 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001106 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001107 }
1108 }
1109 else
1110 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001111 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001112 {
1113 return NoError();
1114 }
1115
Jamie Madillbd044ed2017-06-05 12:59:21 -04001116 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001117 {
1118 return NoError();
1119 }
1120
Jamie Madillbd044ed2017-06-05 12:59:21 -04001121 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001122 {
1123 return NoError();
1124 }
1125
Jiajia Qin729b2c62017-08-14 09:36:11 +08001126 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001127 {
1128 return NoError();
1129 }
1130
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001131 if (!linkValidateGlobalNames(context, mInfoLog))
1132 {
1133 return NoError();
1134 }
1135
Jamie Madillbd044ed2017-06-05 12:59:21 -04001136 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001137
Jiawei Shao73618602017-12-20 15:47:15 +08001138 ASSERT(mState.mAttachedVertexShader);
1139 mState.mNumViews = mState.mAttachedVertexShader->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +03001140
Jamie Madillbd044ed2017-06-05 12:59:21 -04001141 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001142
Jamie Madill192745a2016-12-22 15:58:21 -05001143 // Map the varyings to the register file
1144 // In WebGL, we use a slightly different handling for packing variables.
Jamie Madill61d53252018-01-31 14:49:24 -05001145 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1146 if (data.getLimitations().noFlexibleVaryingPacking)
1147 {
1148 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1149 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1150 }
1151 else if (data.getExtensions().webglCompatibility)
1152 {
1153 packMode = PackMode::WEBGL_STRICT;
1154 }
Jamie Madillc9727f32017-11-07 12:37:07 -05001155
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001156 ProgramLinkedResources resources = {
1157 {data.getCaps().maxVaryingVectors, packMode},
1158 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001159 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1160 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001161
1162 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1163 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1164
Jiawei Shao73618602017-12-20 15:47:15 +08001165 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001166 {
1167 return NoError();
1168 }
1169
jchen1085c93c42017-11-12 15:36:47 +08001170 if (!resources.varyingPacking.collectAndPackUserVaryings(
1171 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001172 {
1173 return NoError();
1174 }
1175
Jamie Madillc9727f32017-11-07 12:37:07 -05001176 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001177 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001178 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001179 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001180 }
1181
1182 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001183 }
1184
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001185 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001186
jchen10eaef1e52017-06-13 10:44:11 +08001187 setUniformValuesFromBindingQualifiers();
1188
Yunchao Heece12532017-11-21 15:50:21 +08001189 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1190 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001191 ASSERT(mLinked);
1192 updateLinkedShaderStages();
1193
Jamie Madill54164b02017-08-28 15:17:37 -04001194 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001195 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001196
Jamie Madill32447362017-06-28 14:53:52 -04001197 // Save to the program cache.
1198 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
1199 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
1200 {
1201 cache->putProgram(programHash, context, this);
1202 }
1203
Jamie Madill6c58b062017-08-01 13:44:25 -04001204 double delta = platform->currentTime(platform) - startTime;
1205 int us = static_cast<int>(delta * 1000000.0);
1206 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
1207
Martin Radev4c4c8e72016-08-04 12:25:34 +03001208 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001209}
1210
Yunchao He85072e82017-11-14 15:43:28 +08001211void Program::updateLinkedShaderStages()
1212{
Yunchao Heece12532017-11-21 15:50:21 +08001213 mState.mLinkedShaderStages.reset();
1214
Yunchao He85072e82017-11-14 15:43:28 +08001215 if (mState.mAttachedVertexShader)
1216 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001217 mState.mLinkedShaderStages.set(ShaderType::Vertex);
Yunchao He85072e82017-11-14 15:43:28 +08001218 }
1219
1220 if (mState.mAttachedFragmentShader)
1221 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001222 mState.mLinkedShaderStages.set(ShaderType::Fragment);
Yunchao He85072e82017-11-14 15:43:28 +08001223 }
1224
1225 if (mState.mAttachedComputeShader)
1226 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001227 mState.mLinkedShaderStages.set(ShaderType::Compute);
Yunchao He85072e82017-11-14 15:43:28 +08001228 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001229
1230 if (mState.mAttachedGeometryShader)
1231 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001232 mState.mLinkedShaderStages.set(ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001233 }
Yunchao He85072e82017-11-14 15:43:28 +08001234}
1235
James Darpinian30b604d2018-03-12 17:26:57 -07001236void ProgramState::updateTransformFeedbackStrides()
1237{
1238 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1239 {
1240 mTransformFeedbackStrides.resize(1);
1241 size_t totalSize = 0;
1242 for (auto &varying : mLinkedTransformFeedbackVaryings)
1243 {
1244 totalSize += varying.size() * VariableExternalSize(varying.type);
1245 }
1246 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1247 }
1248 else
1249 {
1250 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1251 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1252 {
1253 auto &varying = mLinkedTransformFeedbackVaryings[i];
1254 mTransformFeedbackStrides[i] =
1255 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1256 }
1257 }
1258}
1259
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001260// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001261void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001263 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001264 mState.mAttributesTypeMask.reset();
1265 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001266 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001267 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001268 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001269 mState.mUniforms.clear();
1270 mState.mUniformLocations.clear();
1271 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001272 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001273 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001274 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001275 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001276 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001277 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001278 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001279 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001280 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001281 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001282 mState.mNumViews = -1;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001283 mState.mGeometryShaderInputPrimitiveType = GL_TRIANGLES;
1284 mState.mGeometryShaderOutputPrimitiveType = GL_TRIANGLE_STRIP;
1285 mState.mGeometryShaderInvocations = 1;
1286 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288 mValidated = false;
1289
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001290 mLinked = false;
Jamie Madill6bc264a2018-03-31 15:36:05 -04001291 mInfoLog.reset();
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001292}
1293
Geoff Lange1a27752015-10-05 13:16:04 -04001294bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001295{
1296 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297}
1298
Jiawei Shao385b3e02018-03-21 09:43:28 +08001299bool Program::hasLinkedShaderStage(ShaderType shaderType) const
1300{
1301 ASSERT(shaderType != ShaderType::InvalidEnum);
1302 return mState.mLinkedShaderStages[shaderType];
1303}
1304
Jamie Madilla2c74982016-12-12 11:20:42 -05001305Error Program::loadBinary(const Context *context,
1306 GLenum binaryFormat,
1307 const void *binary,
1308 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001309{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001310 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001311
Geoff Lang7dd2e102014-11-10 15:19:26 -05001312#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001313 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001314#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001315 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1316 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001317 {
Jamie Madillf6113162015-05-07 11:49:21 -04001318 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001319 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001320 }
1321
Jamie Madill4f86d052017-06-05 12:59:26 -04001322 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1323 ANGLE_TRY_RESULT(
1324 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001325
1326 // Currently we require the full shader text to compute the program hash.
1327 // TODO(jmadill): Store the binary in the internal program cache.
1328
Jamie Madillb0a838b2016-11-13 20:02:12 -05001329 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001330#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331}
1332
Jamie Madilla2c74982016-12-12 11:20:42 -05001333Error Program::saveBinary(const Context *context,
1334 GLenum *binaryFormat,
1335 void *binary,
1336 GLsizei bufSize,
1337 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338{
1339 if (binaryFormat)
1340 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001341 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342 }
1343
Jamie Madill4f86d052017-06-05 12:59:26 -04001344 angle::MemoryBuffer memoryBuf;
1345 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346
Jamie Madill4f86d052017-06-05 12:59:26 -04001347 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1348 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349
1350 if (streamLength > bufSize)
1351 {
1352 if (length)
1353 {
1354 *length = 0;
1355 }
1356
1357 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1358 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1359 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001360 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361 }
1362
1363 if (binary)
1364 {
1365 char *ptr = reinterpret_cast<char*>(binary);
1366
Jamie Madill48ef11b2016-04-27 15:21:52 -04001367 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368 ptr += streamLength;
1369
1370 ASSERT(ptr - streamLength == binary);
1371 }
1372
1373 if (length)
1374 {
1375 *length = streamLength;
1376 }
1377
He Yunchaoacd18982017-01-04 10:46:42 +08001378 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
Jamie Madillffe00c02017-06-27 16:26:55 -04001381GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382{
1383 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001384 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385 if (error.isError())
1386 {
1387 return 0;
1388 }
1389
1390 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001391}
1392
Geoff Langc5629752015-12-07 16:29:04 -05001393void Program::setBinaryRetrievableHint(bool retrievable)
1394{
1395 // TODO(jmadill) : replace with dirty bits
1396 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001397 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001398}
1399
1400bool Program::getBinaryRetrievableHint() const
1401{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001402 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001403}
1404
Yunchao He61afff12017-03-14 15:34:03 +08001405void Program::setSeparable(bool separable)
1406{
1407 // TODO(yunchao) : replace with dirty bits
1408 if (mState.mSeparable != separable)
1409 {
1410 mProgram->setSeparable(separable);
1411 mState.mSeparable = separable;
1412 }
1413}
1414
1415bool Program::isSeparable() const
1416{
1417 return mState.mSeparable;
1418}
1419
Jamie Madill6c1f6712017-02-14 19:08:04 -05001420void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001421{
1422 mRefCount--;
1423
1424 if (mRefCount == 0 && mDeleteStatus)
1425 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001426 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001427 }
1428}
1429
1430void Program::addRef()
1431{
1432 mRefCount++;
1433}
1434
1435unsigned int Program::getRefCount() const
1436{
1437 return mRefCount;
1438}
1439
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001440int Program::getInfoLogLength() const
1441{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001442 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001443}
1444
Geoff Lange1a27752015-10-05 13:16:04 -04001445void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001446{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001447 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001448}
1449
Geoff Lange1a27752015-10-05 13:16:04 -04001450void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001451{
1452 int total = 0;
1453
Martin Radev4c4c8e72016-08-04 12:25:34 +03001454 if (mState.mAttachedComputeShader)
1455 {
1456 if (total < maxCount)
1457 {
1458 shaders[total] = mState.mAttachedComputeShader->getHandle();
1459 total++;
1460 }
1461 }
1462
Jamie Madill48ef11b2016-04-27 15:21:52 -04001463 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001464 {
1465 if (total < maxCount)
1466 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001467 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001468 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001469 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001470 }
1471
Jamie Madill48ef11b2016-04-27 15:21:52 -04001472 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001473 {
1474 if (total < maxCount)
1475 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001476 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001477 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001478 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001479 }
1480
Jiawei Shao89be29a2017-11-06 14:36:45 +08001481 if (mState.mAttachedGeometryShader)
1482 {
1483 if (total < maxCount)
1484 {
1485 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1486 total++;
1487 }
1488 }
1489
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001490 if (count)
1491 {
1492 *count = total;
1493 }
1494}
1495
Geoff Lange1a27752015-10-05 13:16:04 -04001496GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001498 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499}
1500
Jamie Madill63805b42015-08-25 13:17:39 -04001501bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001502{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001503 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1504 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001505}
1506
jchen10fd7c3b52017-03-21 15:36:03 +08001507void Program::getActiveAttribute(GLuint index,
1508 GLsizei bufsize,
1509 GLsizei *length,
1510 GLint *size,
1511 GLenum *type,
1512 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001513{
Jamie Madillc349ec02015-08-21 16:53:12 -04001514 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001515 {
1516 if (bufsize > 0)
1517 {
1518 name[0] = '\0';
1519 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001521 if (length)
1522 {
1523 *length = 0;
1524 }
1525
1526 *type = GL_NONE;
1527 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001528 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001529 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001530
jchen1036e120e2017-03-14 14:53:58 +08001531 ASSERT(index < mState.mAttributes.size());
1532 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001533
1534 if (bufsize > 0)
1535 {
jchen10fd7c3b52017-03-21 15:36:03 +08001536 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001537 }
1538
1539 // Always a single 'type' instance
1540 *size = 1;
1541 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001542}
1543
Geoff Lange1a27752015-10-05 13:16:04 -04001544GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001545{
Jamie Madillc349ec02015-08-21 16:53:12 -04001546 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001547 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001548 return 0;
1549 }
1550
jchen1036e120e2017-03-14 14:53:58 +08001551 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001552}
1553
Geoff Lange1a27752015-10-05 13:16:04 -04001554GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001555{
Jamie Madillc349ec02015-08-21 16:53:12 -04001556 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001557 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001558 return 0;
1559 }
1560
1561 size_t maxLength = 0;
1562
Jamie Madill48ef11b2016-04-27 15:21:52 -04001563 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001564 {
jchen1036e120e2017-03-14 14:53:58 +08001565 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001566 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567
Jamie Madillc349ec02015-08-21 16:53:12 -04001568 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001569}
1570
jchen1015015f72017-03-16 13:54:21 +08001571GLuint Program::getInputResourceIndex(const GLchar *name) const
1572{
Olli Etuahod2551232017-10-26 20:03:33 +03001573 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001574}
1575
1576GLuint Program::getOutputResourceIndex(const GLchar *name) const
1577{
1578 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1579}
1580
jchen10fd7c3b52017-03-21 15:36:03 +08001581size_t Program::getOutputResourceCount() const
1582{
1583 return (mLinked ? mState.mOutputVariables.size() : 0);
1584}
1585
jchen10baf5d942017-08-28 20:45:48 +08001586template <typename T>
1587void Program::getResourceName(GLuint index,
1588 const std::vector<T> &resources,
1589 GLsizei bufSize,
1590 GLsizei *length,
1591 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001592{
1593 if (length)
1594 {
1595 *length = 0;
1596 }
1597
1598 if (!mLinked)
1599 {
1600 if (bufSize > 0)
1601 {
1602 name[0] = '\0';
1603 }
1604 return;
1605 }
jchen10baf5d942017-08-28 20:45:48 +08001606 ASSERT(index < resources.size());
1607 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001608
1609 if (bufSize > 0)
1610 {
Olli Etuahod2551232017-10-26 20:03:33 +03001611 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001612 }
1613}
1614
jchen10baf5d942017-08-28 20:45:48 +08001615void Program::getInputResourceName(GLuint index,
1616 GLsizei bufSize,
1617 GLsizei *length,
1618 GLchar *name) const
1619{
1620 getResourceName(index, mState.mAttributes, bufSize, length, name);
1621}
1622
1623void Program::getOutputResourceName(GLuint index,
1624 GLsizei bufSize,
1625 GLsizei *length,
1626 GLchar *name) const
1627{
1628 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1629}
1630
1631void Program::getUniformResourceName(GLuint index,
1632 GLsizei bufSize,
1633 GLsizei *length,
1634 GLchar *name) const
1635{
1636 getResourceName(index, mState.mUniforms, bufSize, length, name);
1637}
1638
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001639void Program::getBufferVariableResourceName(GLuint index,
1640 GLsizei bufSize,
1641 GLsizei *length,
1642 GLchar *name) const
1643{
1644 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1645}
1646
jchen10880683b2017-04-12 16:21:55 +08001647const sh::Attribute &Program::getInputResource(GLuint index) const
1648{
1649 ASSERT(index < mState.mAttributes.size());
1650 return mState.mAttributes[index];
1651}
1652
1653const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1654{
1655 ASSERT(index < mState.mOutputVariables.size());
1656 return mState.mOutputVariables[index];
1657}
1658
Geoff Lang7dd2e102014-11-10 15:19:26 -05001659GLint Program::getFragDataLocation(const std::string &name) const
1660{
Olli Etuahod2551232017-10-26 20:03:33 +03001661 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001662}
1663
Geoff Lange1a27752015-10-05 13:16:04 -04001664void Program::getActiveUniform(GLuint index,
1665 GLsizei bufsize,
1666 GLsizei *length,
1667 GLint *size,
1668 GLenum *type,
1669 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001670{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001672 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001674 ASSERT(index < mState.mUniforms.size());
1675 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676
1677 if (bufsize > 0)
1678 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001680 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 }
1682
Olli Etuaho465835d2017-09-26 13:34:10 +03001683 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001684 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001685 }
1686 else
1687 {
1688 if (bufsize > 0)
1689 {
1690 name[0] = '\0';
1691 }
1692
1693 if (length)
1694 {
1695 *length = 0;
1696 }
1697
1698 *size = 0;
1699 *type = GL_NONE;
1700 }
1701}
1702
Geoff Lange1a27752015-10-05 13:16:04 -04001703GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001704{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001705 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001706 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001707 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001708 }
1709 else
1710 {
1711 return 0;
1712 }
1713}
1714
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001715size_t Program::getActiveBufferVariableCount() const
1716{
1717 return mLinked ? mState.mBufferVariables.size() : 0;
1718}
1719
Geoff Lange1a27752015-10-05 13:16:04 -04001720GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001721{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001722 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001723
1724 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001725 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001726 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001727 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001728 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001729 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001730 size_t length = uniform.name.length() + 1u;
1731 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001732 {
1733 length += 3; // Counting in "[0]".
1734 }
1735 maxLength = std::max(length, maxLength);
1736 }
1737 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001738 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001739
Jamie Madill62d31cb2015-09-11 13:25:51 -04001740 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001741}
1742
Geoff Lang7dd2e102014-11-10 15:19:26 -05001743bool Program::isValidUniformLocation(GLint location) const
1744{
Jamie Madille2e406c2016-06-02 13:04:10 -04001745 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001746 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001747 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001748}
1749
Jamie Madill62d31cb2015-09-11 13:25:51 -04001750const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001752 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001753 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001754}
1755
Jamie Madillac4e9c32017-01-13 14:07:12 -05001756const VariableLocation &Program::getUniformLocation(GLint location) const
1757{
1758 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1759 return mState.mUniformLocations[location];
1760}
1761
1762const std::vector<VariableLocation> &Program::getUniformLocations() const
1763{
1764 return mState.mUniformLocations;
1765}
1766
1767const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1768{
1769 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1770 return mState.mUniforms[index];
1771}
1772
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001773const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1774{
1775 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1776 return mState.mBufferVariables[index];
1777}
1778
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780{
Olli Etuahod2551232017-10-26 20:03:33 +03001781 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782}
1783
Jamie Madill62d31cb2015-09-11 13:25:51 -04001784GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785{
Jamie Madille7d84322017-01-10 18:21:59 -05001786 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787}
1788
1789void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1790{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001791 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1792 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001793 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001794}
1795
1796void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1797{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001798 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1799 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001800 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801}
1802
1803void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1804{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001805 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1806 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001807 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808}
1809
1810void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1811{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001812 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1813 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001814 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815}
1816
Jamie Madill81c2e252017-09-09 23:32:46 -04001817Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001819 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1820 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1821
Jamie Madill81c2e252017-09-09 23:32:46 -04001822 mProgram->setUniform1iv(location, clampedCount, v);
1823
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001824 if (mState.isSamplerUniformIndex(locationInfo.index))
1825 {
1826 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001827 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001828 }
1829
Jamie Madill81c2e252017-09-09 23:32:46 -04001830 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831}
1832
1833void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1834{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001835 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1836 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001837 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838}
1839
1840void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1841{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001842 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1843 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001844 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001845}
1846
1847void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1848{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001849 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1850 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001851 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852}
1853
1854void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1855{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001856 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1857 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001858 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859}
1860
1861void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1862{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001863 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1864 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001865 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001866}
1867
1868void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1869{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001870 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1871 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001872 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873}
1874
1875void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1876{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001877 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1878 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001879 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880}
1881
1882void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1883{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001884 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001885 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001886}
1887
1888void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1889{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001890 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001891 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892}
1893
1894void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1895{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001896 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001897 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898}
1899
1900void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1901{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001902 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001903 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904}
1905
1906void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1907{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001908 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001909 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001910}
1911
1912void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1913{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001914 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001915 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001916}
1917
1918void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1919{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001920 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001921 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001922}
1923
1924void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1925{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001926 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001927 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928}
1929
1930void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1931{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001932 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001933 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934}
1935
Jamie Madill54164b02017-08-28 15:17:37 -04001936void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001937{
Jamie Madill54164b02017-08-28 15:17:37 -04001938 const auto &uniformLocation = mState.getUniformLocations()[location];
1939 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1940
1941 GLenum nativeType = gl::VariableComponentType(uniform.type);
1942 if (nativeType == GL_FLOAT)
1943 {
1944 mProgram->getUniformfv(context, location, v);
1945 }
1946 else
1947 {
1948 getUniformInternal(context, v, location, nativeType,
1949 gl::VariableComponentCount(uniform.type));
1950 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951}
1952
Jamie Madill54164b02017-08-28 15:17:37 -04001953void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954{
Jamie Madill54164b02017-08-28 15:17:37 -04001955 const auto &uniformLocation = mState.getUniformLocations()[location];
1956 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1957
1958 GLenum nativeType = gl::VariableComponentType(uniform.type);
1959 if (nativeType == GL_INT || nativeType == GL_BOOL)
1960 {
1961 mProgram->getUniformiv(context, location, v);
1962 }
1963 else
1964 {
1965 getUniformInternal(context, v, location, nativeType,
1966 gl::VariableComponentCount(uniform.type));
1967 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968}
1969
Jamie Madill54164b02017-08-28 15:17:37 -04001970void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971{
Jamie Madill54164b02017-08-28 15:17:37 -04001972 const auto &uniformLocation = mState.getUniformLocations()[location];
1973 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1974
1975 GLenum nativeType = gl::VariableComponentType(uniform.type);
1976 if (nativeType == GL_UNSIGNED_INT)
1977 {
1978 mProgram->getUniformuiv(context, location, v);
1979 }
1980 else
1981 {
1982 getUniformInternal(context, v, location, nativeType,
1983 gl::VariableComponentCount(uniform.type));
1984 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001985}
1986
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987void Program::flagForDeletion()
1988{
1989 mDeleteStatus = true;
1990}
1991
1992bool Program::isFlaggedForDeletion() const
1993{
1994 return mDeleteStatus;
1995}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001996
Brandon Jones43a53e22014-08-28 16:23:22 -07001997void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001998{
1999 mInfoLog.reset();
2000
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002002 {
Geoff Lang92019432017-11-20 13:09:34 -05002003 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002004 }
2005 else
2006 {
Jamie Madillf6113162015-05-07 11:49:21 -04002007 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002008 }
2009}
2010
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
2012{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002013 // Skip cache if we're using an infolog, so we get the full error.
2014 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
2015 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
2016 {
2017 return mCachedValidateSamplersResult.value();
2018 }
2019
2020 if (mTextureUnitTypesCache.empty())
2021 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002022 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002023 }
2024 else
2025 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002026 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(),
2027 TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002028 }
2029
2030 // if any two active samplers in a program are of different types, but refer to the same
2031 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2032 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05002033 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002034 {
Jamie Madill54164b02017-08-28 15:17:37 -04002035 if (samplerBinding.unreferenced)
2036 continue;
2037
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002038 TextureType textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002039
Jamie Madille7d84322017-01-10 18:21:59 -05002040 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002041 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002042 if (textureUnit >= caps.maxCombinedTextureImageUnits)
2043 {
2044 if (infoLog)
2045 {
2046 (*infoLog) << "Sampler uniform (" << textureUnit
2047 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
2048 << caps.maxCombinedTextureImageUnits << ")";
2049 }
2050
2051 mCachedValidateSamplersResult = false;
2052 return false;
2053 }
2054
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002055 if (mTextureUnitTypesCache[textureUnit] != TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002056 {
2057 if (textureType != mTextureUnitTypesCache[textureUnit])
2058 {
2059 if (infoLog)
2060 {
2061 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2062 "image unit ("
2063 << textureUnit << ").";
2064 }
2065
2066 mCachedValidateSamplersResult = false;
2067 return false;
2068 }
2069 }
2070 else
2071 {
2072 mTextureUnitTypesCache[textureUnit] = textureType;
2073 }
2074 }
2075 }
2076
2077 mCachedValidateSamplersResult = true;
2078 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079}
2080
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002081bool Program::isValidated() const
2082{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 return mValidated;
2084}
2085
Geoff Lange1a27752015-10-05 13:16:04 -04002086GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002088 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089}
2090
jchen1058f67be2017-10-27 08:59:27 +08002091GLuint Program::getActiveAtomicCounterBufferCount() const
2092{
2093 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2094}
2095
Jiajia Qin729b2c62017-08-14 09:36:11 +08002096GLuint Program::getActiveShaderStorageBlockCount() const
2097{
2098 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2099}
2100
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002101void Program::getActiveUniformBlockName(const GLuint blockIndex,
2102 GLsizei bufSize,
2103 GLsizei *length,
2104 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002106 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2107}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002109void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2110 GLsizei bufSize,
2111 GLsizei *length,
2112 GLchar *blockName) const
2113{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002115 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002116}
2117
Qin Jiajia9bf55522018-01-29 13:56:23 +08002118template <typename T>
2119GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002120{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 int maxLength = 0;
2122
2123 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002124 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002125 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002127 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002128 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002129 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002130 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 }
2132 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002133 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134
2135 return maxLength;
2136}
2137
Qin Jiajia9bf55522018-01-29 13:56:23 +08002138GLint Program::getActiveUniformBlockMaxNameLength() const
2139{
2140 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2141}
2142
2143GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2144{
2145 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2146}
2147
Geoff Lange1a27752015-10-05 13:16:04 -04002148GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002150 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2151}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002152
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002153GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2154{
2155 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002156}
2157
Jiajia Qin729b2c62017-08-14 09:36:11 +08002158const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002159{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002160 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2161 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002162}
2163
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002164const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2165{
2166 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2167 return mState.mShaderStorageBlocks[index];
2168}
2169
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002170void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2171{
jchen107a20b972017-06-13 14:25:26 +08002172 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002173 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04002174 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002175}
2176
2177GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2178{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002179 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002180}
2181
Jiajia Qin729b2c62017-08-14 09:36:11 +08002182GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2183{
2184 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2185}
2186
Geoff Lang48dcae72014-02-05 16:28:24 -05002187void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
2188{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002189 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002190 for (GLsizei i = 0; i < count; i++)
2191 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002192 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002193 }
2194
Jamie Madill48ef11b2016-04-27 15:21:52 -04002195 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002196}
2197
2198void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
2199{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002200 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002201 {
jchen10a9042d32017-03-17 08:50:45 +08002202 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2203 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2204 std::string varName = var.nameWithArrayIndex();
2205 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002206 if (length)
2207 {
2208 *length = lastNameIdx;
2209 }
2210 if (size)
2211 {
jchen10a9042d32017-03-17 08:50:45 +08002212 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002213 }
2214 if (type)
2215 {
jchen10a9042d32017-03-17 08:50:45 +08002216 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002217 }
2218 if (name)
2219 {
jchen10a9042d32017-03-17 08:50:45 +08002220 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002221 name[lastNameIdx] = '\0';
2222 }
2223 }
2224}
2225
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002226GLsizei Program::getTransformFeedbackVaryingCount() const
2227{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002229 {
jchen10a9042d32017-03-17 08:50:45 +08002230 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002231 }
2232 else
2233 {
2234 return 0;
2235 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002236}
2237
2238GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2239{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002240 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002241 {
2242 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002243 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002244 {
jchen10a9042d32017-03-17 08:50:45 +08002245 maxSize =
2246 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002247 }
2248
2249 return maxSize;
2250 }
2251 else
2252 {
2253 return 0;
2254 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002255}
2256
2257GLenum Program::getTransformFeedbackBufferMode() const
2258{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002259 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260}
2261
Jiawei Shao73618602017-12-20 15:47:15 +08002262bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
2263{
2264 Shader *vertexShader = mState.mAttachedVertexShader;
2265 Shader *fragmentShader = mState.mAttachedFragmentShader;
2266 Shader *computeShader = mState.mAttachedComputeShader;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002267 Shader *geometryShader = mState.mAttachedGeometryShader;
Jiawei Shao73618602017-12-20 15:47:15 +08002268
2269 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002270 bool isGraphicsShaderAttached =
2271 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002272 // Check whether we both have a compute and non-compute shaders attached.
2273 // If there are of both types attached, then linking should fail.
2274 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2275 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2276 {
2277 infoLog << "Both compute and graphics shaders are attached to the same program.";
2278 return false;
2279 }
2280
2281 if (computeShader)
2282 {
2283 if (!computeShader->isCompiled(context))
2284 {
2285 infoLog << "Attached compute shader is not compiled.";
2286 return false;
2287 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002288 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002289
2290 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
2291
2292 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2293 // If the work group size is not specified, a link time error should occur.
2294 if (!mState.mComputeShaderLocalSize.isDeclared())
2295 {
2296 infoLog << "Work group size is not specified.";
2297 return false;
2298 }
2299 }
2300 else
2301 {
2302 if (!fragmentShader || !fragmentShader->isCompiled(context))
2303 {
2304 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2305 return false;
2306 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002307 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002308
2309 if (!vertexShader || !vertexShader->isCompiled(context))
2310 {
2311 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2312 return false;
2313 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002314 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002315
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002316 int vertexShaderVersion = vertexShader->getShaderVersion(context);
2317 if (fragmentShader->getShaderVersion(context) != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002318 {
2319 infoLog << "Fragment shader version does not match vertex shader version.";
2320 return false;
2321 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002322
2323 if (geometryShader)
2324 {
2325 // [GL_EXT_geometry_shader] Chapter 7
2326 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2327 // Language Specification, as well as any of the following reasons:
2328 // * One or more of the shader objects attached to <program> are not compiled
2329 // successfully.
2330 // * The shaders do not use the same shader language version.
2331 // * <program> contains objects to form a geometry shader, and
2332 // - <program> is not separable and contains no objects to form a vertex shader; or
2333 // - the input primitive type, output primitive type, or maximum output vertex count
2334 // is not specified in the compiled geometry shader object.
2335 if (!geometryShader->isCompiled(context))
2336 {
2337 infoLog << "The attached geometry shader isn't compiled.";
2338 return false;
2339 }
2340
2341 if (geometryShader->getShaderVersion(context) != vertexShaderVersion)
2342 {
2343 mInfoLog << "Geometry shader version does not match vertex shader version.";
2344 return false;
2345 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002346 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002347
2348 Optional<GLenum> inputPrimitive =
2349 geometryShader->getGeometryShaderInputPrimitiveType(context);
2350 if (!inputPrimitive.valid())
2351 {
2352 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2353 return false;
2354 }
2355
2356 Optional<GLenum> outputPrimitive =
2357 geometryShader->getGeometryShaderOutputPrimitiveType(context);
2358 if (!outputPrimitive.valid())
2359 {
2360 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2361 return false;
2362 }
2363
2364 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices(context);
2365 if (!maxVertices.valid())
2366 {
2367 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2368 return false;
2369 }
2370
2371 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2372 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2373 mState.mGeometryShaderMaxVertices = maxVertices.value();
2374 mState.mGeometryShaderInvocations =
2375 geometryShader->getGeometryShaderInvocations(context);
2376 }
Jiawei Shao73618602017-12-20 15:47:15 +08002377 }
2378
2379 return true;
2380}
2381
jchen10910a3da2017-11-15 09:40:11 +08002382GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2383{
2384 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2385 {
2386 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2387 if (tf.nameWithArrayIndex() == name)
2388 {
2389 return tfIndex;
2390 }
2391 }
2392 return GL_INVALID_INDEX;
2393}
2394
2395const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2396{
2397 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2398 return mState.mLinkedTransformFeedbackVaryings[index];
2399}
2400
Jamie Madillbd044ed2017-06-05 12:59:21 -04002401bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002402{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002403 std::vector<Shader *> activeShaders;
2404 activeShaders.push_back(mState.mAttachedVertexShader);
2405 if (mState.mAttachedGeometryShader)
2406 {
2407 activeShaders.push_back(mState.mAttachedGeometryShader);
2408 }
2409 activeShaders.push_back(mState.mAttachedFragmentShader);
Jamie Madill192745a2016-12-22 15:58:21 -05002410
Jiawei Shaod063aff2018-02-22 10:19:09 +08002411 const size_t activeShaderCount = activeShaders.size();
2412 for (size_t shaderIndex = 0; shaderIndex < activeShaderCount - 1; ++shaderIndex)
2413 {
2414 if (!linkValidateShaderInterfaceMatching(context, activeShaders[shaderIndex],
2415 activeShaders[shaderIndex + 1], infoLog))
2416 {
2417 return false;
2418 }
2419 }
2420
2421 if (!linkValidateBuiltInVaryings(context, infoLog))
2422 {
2423 return false;
2424 }
2425
2426 if (!linkValidateFragmentInputBindings(context, infoLog))
2427 {
2428 return false;
2429 }
2430
2431 return true;
2432}
2433
2434// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2435// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
2436bool Program::linkValidateShaderInterfaceMatching(const Context *context,
2437 gl::Shader *generatingShader,
2438 gl::Shader *consumingShader,
2439 gl::InfoLog &infoLog) const
2440{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002441 ASSERT(generatingShader->getShaderVersion(context) ==
2442 consumingShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002443
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002444 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context);
2445 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002446
Jiawei Shao385b3e02018-03-21 09:43:28 +08002447 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002448
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002449 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002451 bool matched = false;
2452
2453 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002454 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002455 {
2456 continue;
2457 }
2458
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002459 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002460 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002461 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002462 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002463 ASSERT(!output.isBuiltIn());
2464
2465 std::string mismatchedStructFieldName;
2466 LinkMismatchError linkError =
2467 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002468 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002469 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002470 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002471 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2472 mismatchedStructFieldName, generatingShader->getType(),
2473 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002474 return false;
2475 }
2476
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 matched = true;
2478 break;
2479 }
2480 }
2481
Olli Etuaho107c7242018-03-20 15:45:35 +02002482 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2483 // whether the input is statically used - a statically used input should fail this test even
2484 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002485 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002486 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002487 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2488 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2489 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 return false;
2491 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002492 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002493
Jiawei Shaod063aff2018-02-22 10:19:09 +08002494 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002495
Jiawei Shaod063aff2018-02-22 10:19:09 +08002496 return true;
2497}
2498
2499bool Program::linkValidateFragmentInputBindings(const Context *context, gl::InfoLog &infoLog) const
2500{
2501 ASSERT(mState.mAttachedFragmentShader);
2502
2503 std::map<GLuint, std::string> staticFragmentInputLocations;
2504
2505 const std::vector<sh::Varying> &fragmentInputVaryings =
2506 mState.mAttachedFragmentShader->getInputVaryings(context);
2507 for (const sh::Varying &input : fragmentInputVaryings)
2508 {
2509 if (input.isBuiltIn() || !input.staticUse)
2510 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002511 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002512 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002513
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002514 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002515 if (inputBinding == -1)
2516 continue;
2517
2518 const auto it = staticFragmentInputLocations.find(inputBinding);
2519 if (it == std::end(staticFragmentInputLocations))
2520 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002521 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002522 }
2523 else
2524 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002525 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002526 << it->second;
2527 return false;
2528 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002529 }
2530
2531 return true;
2532}
2533
Jamie Madillbd044ed2017-06-05 12:59:21 -04002534bool Program::linkUniforms(const Context *context,
2535 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002536 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002537{
Olli Etuahob78707c2017-03-09 15:03:11 +00002538 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002539 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002540 {
2541 return false;
2542 }
2543
Olli Etuahob78707c2017-03-09 15:03:11 +00002544 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002545
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002546 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002547
jchen10eaef1e52017-06-13 10:44:11 +08002548 if (!linkAtomicCounterBuffers())
2549 {
2550 return false;
2551 }
2552
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002553 return true;
2554}
2555
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002556void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002557{
Jamie Madill982f6e02017-06-07 14:33:04 -04002558 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2559 unsigned int low = high;
2560
jchen10eaef1e52017-06-13 10:44:11 +08002561 for (auto counterIter = mState.mUniforms.rbegin();
2562 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2563 {
2564 --low;
2565 }
2566
2567 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2568
2569 high = low;
2570
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002571 for (auto imageIter = mState.mUniforms.rbegin();
2572 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2573 {
2574 --low;
2575 }
2576
2577 mState.mImageUniformRange = RangeUI(low, high);
2578
2579 // If uniform is a image type, insert it into the mImageBindings array.
2580 for (unsigned int imageIndex : mState.mImageUniformRange)
2581 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002582 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2583 // cannot load values into a uniform defined as an image. if declare without a
2584 // binding qualifier, any uniform image variable (include all elements of
2585 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002586 auto &imageUniform = mState.mUniforms[imageIndex];
2587 if (imageUniform.binding == -1)
2588 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002589 mState.mImageBindings.emplace_back(
2590 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002591 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002592 else
2593 {
2594 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002595 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002596 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002597 }
2598
2599 high = low;
2600
2601 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002602 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002603 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002604 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002605 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002606
2607 mState.mSamplerUniformRange = RangeUI(low, high);
2608
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002609 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002610 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002611 {
2612 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002613 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002614 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002615 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002616 }
2617}
2618
jchen10eaef1e52017-06-13 10:44:11 +08002619bool Program::linkAtomicCounterBuffers()
2620{
2621 for (unsigned int index : mState.mAtomicCounterUniformRange)
2622 {
2623 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002624 uniform.blockInfo.offset = uniform.offset;
2625 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2626 uniform.blockInfo.matrixStride = 0;
2627 uniform.blockInfo.isRowMajorMatrix = false;
2628
jchen10eaef1e52017-06-13 10:44:11 +08002629 bool found = false;
2630 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2631 ++bufferIndex)
2632 {
2633 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2634 if (buffer.binding == uniform.binding)
2635 {
2636 buffer.memberIndexes.push_back(index);
2637 uniform.bufferIndex = bufferIndex;
2638 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002639 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002640 break;
2641 }
2642 }
2643 if (!found)
2644 {
2645 AtomicCounterBuffer atomicCounterBuffer;
2646 atomicCounterBuffer.binding = uniform.binding;
2647 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002648 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002649 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2650 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2651 }
2652 }
2653 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002654 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002655
2656 return true;
2657}
2658
Jamie Madilleb979bf2016-11-15 12:28:46 -05002659// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002660bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002661{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002662 const ContextState &data = context->getContextState();
Jiawei Shao385b3e02018-03-21 09:43:28 +08002663 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002664
Geoff Lang7dd2e102014-11-10 15:19:26 -05002665 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002666 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002667 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002668
2669 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002670 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002671 {
Jamie Madillf6113162015-05-07 11:49:21 -04002672 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002673 return false;
2674 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002675
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002676 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002677
Jamie Madillc349ec02015-08-21 16:53:12 -04002678 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002679 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002680 {
Olli Etuahod2551232017-10-26 20:03:33 +03002681 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2682 // structures, so we don't need to worry about adjusting their names or generating entries
2683 // for each member/element (unlike uniforms for example).
2684 ASSERT(!attribute.isArray() && !attribute.isStruct());
2685
Jamie Madilleb979bf2016-11-15 12:28:46 -05002686 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002687 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002688 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002689 attribute.location = bindingLocation;
2690 }
2691
2692 if (attribute.location != -1)
2693 {
2694 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002695 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002696
Jamie Madill63805b42015-08-25 13:17:39 -04002697 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002698 {
Jamie Madillf6113162015-05-07 11:49:21 -04002699 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002700 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002701
2702 return false;
2703 }
2704
Jamie Madill63805b42015-08-25 13:17:39 -04002705 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002706 {
Jamie Madill63805b42015-08-25 13:17:39 -04002707 const int regLocation = attribute.location + reg;
2708 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002709
2710 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002711 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002712 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002713 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002714 // TODO(jmadill): fix aliasing on ES2
2715 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002716 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002717 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002718 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002719 return false;
2720 }
2721 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002722 else
2723 {
Jamie Madill63805b42015-08-25 13:17:39 -04002724 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002725 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726
Jamie Madill63805b42015-08-25 13:17:39 -04002727 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002728 }
2729 }
2730 }
2731
2732 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002733 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002734 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002735 // Not set by glBindAttribLocation or by location layout qualifier
2736 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002737 {
Jamie Madill63805b42015-08-25 13:17:39 -04002738 int regs = VariableRegisterCount(attribute.type);
2739 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002740
Jamie Madill63805b42015-08-25 13:17:39 -04002741 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002742 {
Jamie Madillf6113162015-05-07 11:49:21 -04002743 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002744 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002745 }
2746
Jamie Madillc349ec02015-08-21 16:53:12 -04002747 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002748 }
2749 }
2750
Brandon Jonesc405ae72017-12-06 14:15:03 -08002751 ASSERT(mState.mAttributesTypeMask.none());
2752 ASSERT(mState.mAttributesMask.none());
2753
Jamie Madill48ef11b2016-04-27 15:21:52 -04002754 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002755 {
Jamie Madill63805b42015-08-25 13:17:39 -04002756 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002757 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002758
Jamie Madillbd159f02017-10-09 19:39:06 -04002759 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002760 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002761 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2762 mState.mActiveAttribLocationsMask.set(location);
2763 mState.mMaxActiveAttribLocation =
2764 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08002765
2766 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
2767 if (!attribute.isBuiltIn())
2768 {
2769 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
2770 location);
2771 mState.mAttributesMask.set(location);
2772 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002773 }
2774 }
2775
Geoff Lang7dd2e102014-11-10 15:19:26 -05002776 return true;
2777}
2778
Jiajia Qin729b2c62017-08-14 09:36:11 +08002779bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002780{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002781 const auto &caps = context->getCaps();
2782
Martin Radev4c4c8e72016-08-04 12:25:34 +03002783 if (mState.mAttachedComputeShader)
2784 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002785 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002786 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002787
Jiawei Shao427071e2018-03-19 09:21:37 +08002788 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002789 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002790 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2791 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002792 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002793 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002794 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002795
2796 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
Jiawei Shao427071e2018-03-19 09:21:37 +08002797 if (!ValidateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002798 computeShaderStorageBlocks,
2799 "Compute shader shader storage block count exceeds "
2800 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2801 infoLog))
2802 {
2803 return false;
2804 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002805 return true;
2806 }
2807
Jamie Madillbd044ed2017-06-05 12:59:21 -04002808 Shader &vertexShader = *mState.mAttachedVertexShader;
2809 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002810
Jiajia Qin729b2c62017-08-14 09:36:11 +08002811 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2812 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002813
Jiawei Shao427071e2018-03-19 09:21:37 +08002814 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002815 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002816 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2817 {
2818 return false;
2819 }
Jiawei Shao427071e2018-03-19 09:21:37 +08002820 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002821 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002822 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2823 infoLog))
2824 {
2825
2826 return false;
2827 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002828
Jiawei Shao427071e2018-03-19 09:21:37 +08002829 Shader *geometryShader = mState.mAttachedGeometryShader;
2830 if (geometryShader)
2831 {
2832 const auto &geometryUniformBlocks = geometryShader->getUniformBlocks(context);
2833 if (!ValidateInterfaceBlocksCount(
2834 caps.maxGeometryUniformBlocks, geometryUniformBlocks,
2835 "Geometry shader uniform block count exceeds GL_MAX_GEOMETRY_UNIFORM_BLOCKS_EXT (",
2836 infoLog))
2837 {
2838 return false;
2839 }
2840 }
2841
2842 // TODO(jiawei.shao@intel.com): validate geometry shader uniform blocks.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002843 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao73618602017-12-20 15:47:15 +08002844 if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog,
Jiajia Qin8efd1262017-12-19 09:32:55 +08002845 webglCompatibility, sh::BlockType::BLOCK_UNIFORM,
2846 caps.maxCombinedUniformBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002847 {
2848 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002849 }
Jamie Madille473dee2015-08-18 14:49:01 -04002850
Jiajia Qin729b2c62017-08-14 09:36:11 +08002851 if (context->getClientVersion() >= Version(3, 1))
2852 {
2853 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2854 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2855
Jiawei Shao427071e2018-03-19 09:21:37 +08002856 if (!ValidateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002857 vertexShaderStorageBlocks,
2858 "Vertex shader shader storage block count exceeds "
2859 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2860 infoLog))
2861 {
2862 return false;
2863 }
Jiawei Shao427071e2018-03-19 09:21:37 +08002864 if (!ValidateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002865 fragmentShaderStorageBlocks,
2866 "Fragment shader shader storage block count exceeds "
2867 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2868 infoLog))
2869 {
2870
2871 return false;
2872 }
2873
Jiawei Shao427071e2018-03-19 09:21:37 +08002874 if (geometryShader)
2875 {
2876 const auto &geometryShaderStorageBlocks =
2877 geometryShader->getShaderStorageBlocks(context);
2878 if (!ValidateInterfaceBlocksCount(caps.maxGeometryShaderStorageBlocks,
2879 geometryShaderStorageBlocks,
2880 "Geometry shader shader storage block count exceeds "
2881 "GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS_EXT (",
2882 infoLog))
2883 {
2884 return false;
2885 }
2886 }
2887
2888 // TODO(jiawei.shao@intel.com): validate geometry shader shader storage blocks.
Jiajia Qin8efd1262017-12-19 09:32:55 +08002889 if (!ValidateGraphicsInterfaceBlocks(
2890 vertexShaderStorageBlocks, fragmentShaderStorageBlocks, infoLog, webglCompatibility,
2891 sh::BlockType::BLOCK_BUFFER, caps.maxCombinedShaderStorageBlocks))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002892 {
2893 return false;
2894 }
2895 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002896 return true;
2897}
2898
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002899LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
2900 const sh::ShaderVariable &variable2,
2901 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002902 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002903 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002904{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002905 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002906 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002907 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002908 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002909 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002910 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002911 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002912 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002913 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002914 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002915 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002916 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002917 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04002918 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002919 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04002920 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002921
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002922 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002923 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002924 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002925 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002926 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002927 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2928 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002929 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
2930 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002931
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002932 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002933 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002934 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002935 }
2936
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002937 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08002938 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002939 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002940 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002941 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
2942 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002943 }
2944 }
2945
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002946 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002947}
2948
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002949LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
2950 const sh::Varying &inputVarying,
2951 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002952 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002953 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002954{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002955 if (validateGeometryShaderInputVarying)
2956 {
2957 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
2958 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
2959 // or outputs.
2960 ASSERT(inputVarying.arraySizes.size() == 1u);
2961
2962 // Geometry shader input varyings are not treated as arrays, so a vertex array output
2963 // varying cannot match a geometry shader input varying.
2964 // [GL_EXT_geometry_shader] Section 7.4.1:
2965 // Geometry shader per-vertex input variables and blocks are required to be declared as
2966 // arrays, with each element representing input or output values for a single vertex of a
2967 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
2968 // are treated as though they were not declared as arrays.
2969 if (outputVarying.isArray())
2970 {
2971 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
2972 }
2973 }
2974
2975 // Skip the validation on the array sizes between a vertex output varying and a geometry input
2976 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002977 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08002978 LinkValidateVariablesBase(outputVarying, inputVarying, false,
2979 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002980 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002981 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002982 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002983 }
2984
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002985 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002986 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002987 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002988 }
2989
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002990 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002991 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002992 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002993 }
2994
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002995 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002996}
2997
Jamie Madillbd044ed2017-06-05 12:59:21 -04002998bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002999{
Jamie Madillbd044ed2017-06-05 12:59:21 -04003000 Shader *vertexShader = mState.mAttachedVertexShader;
3001 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08003002 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
3003 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04003004 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05003005
3006 if (shaderVersion != 100)
3007 {
3008 // Only ESSL 1.0 has restrictions on matching input and output invariance
3009 return true;
3010 }
3011
3012 bool glPositionIsInvariant = false;
3013 bool glPointSizeIsInvariant = false;
3014 bool glFragCoordIsInvariant = false;
3015 bool glPointCoordIsInvariant = false;
3016
3017 for (const sh::Varying &varying : vertexVaryings)
3018 {
3019 if (!varying.isBuiltIn())
3020 {
3021 continue;
3022 }
3023 if (varying.name.compare("gl_Position") == 0)
3024 {
3025 glPositionIsInvariant = varying.isInvariant;
3026 }
3027 else if (varying.name.compare("gl_PointSize") == 0)
3028 {
3029 glPointSizeIsInvariant = varying.isInvariant;
3030 }
3031 }
3032
3033 for (const sh::Varying &varying : fragmentVaryings)
3034 {
3035 if (!varying.isBuiltIn())
3036 {
3037 continue;
3038 }
3039 if (varying.name.compare("gl_FragCoord") == 0)
3040 {
3041 glFragCoordIsInvariant = varying.isInvariant;
3042 }
3043 else if (varying.name.compare("gl_PointCoord") == 0)
3044 {
3045 glPointCoordIsInvariant = varying.isInvariant;
3046 }
3047 }
3048
3049 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3050 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3051 // Not requiring invariance to match is supported by:
3052 // dEQP, WebGL CTS, Nexus 5X GLES
3053 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3054 {
3055 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3056 "declared invariant.";
3057 return false;
3058 }
3059 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3060 {
3061 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3062 "declared invariant.";
3063 return false;
3064 }
3065
3066 return true;
3067}
3068
jchen10a9042d32017-03-17 08:50:45 +08003069bool Program::linkValidateTransformFeedback(const gl::Context *context,
3070 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003071 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003072 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003073{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003074
jchen108225e732017-11-14 16:29:03 +08003075 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003076 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003077 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003078 {
jchen10a9042d32017-03-17 08:50:45 +08003079 if (context->getClientVersion() < Version(3, 1) &&
3080 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003081 {
Geoff Lang1a683462015-09-29 15:09:59 -04003082 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003083 return false;
3084 }
jchen108225e732017-11-14 16:29:03 +08003085 if (context->getClientVersion() >= Version(3, 1))
3086 {
3087 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3088 {
3089 infoLog << "Two transform feedback varyings include the same array element ("
3090 << tfVaryingName << ").";
3091 return false;
3092 }
3093 }
3094 else
3095 {
3096 if (uniqueNames.count(tfVaryingName) > 0)
3097 {
3098 infoLog << "Two transform feedback varyings specify the same output variable ("
3099 << tfVaryingName << ").";
3100 return false;
3101 }
3102 }
3103 uniqueNames.insert(tfVaryingName);
3104 }
3105
3106 // Validate against program varyings.
3107 size_t totalComponents = 0;
3108 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3109 {
3110 std::vector<unsigned int> subscripts;
3111 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3112
3113 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3114 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003115 {
3116 infoLog << "Transform feedback varying " << tfVaryingName
3117 << " does not exist in the vertex shader.";
3118 return false;
3119 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003120
jchen108225e732017-11-14 16:29:03 +08003121 // Validate the matching variable.
3122 if (var->isStruct())
3123 {
3124 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3125 return false;
3126 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003127
jchen108225e732017-11-14 16:29:03 +08003128 size_t elementCount = 0;
3129 size_t componentCount = 0;
3130
3131 if (var->isArray())
3132 {
3133 if (context->getClientVersion() < Version(3, 1))
3134 {
3135 infoLog << "Capture of arrays is undefined and not supported.";
3136 return false;
3137 }
3138
3139 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3140 ASSERT(!var->isArrayOfArrays());
3141
3142 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3143 {
3144 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3145 return false;
3146 }
3147 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3148 }
3149 else
3150 {
3151 if (!subscripts.empty())
3152 {
3153 infoLog << "Varying '" << baseName
3154 << "' is not an array to be captured by element.";
3155 return false;
3156 }
3157 elementCount = 1;
3158 }
3159
3160 // TODO(jmadill): Investigate implementation limits on D3D11
3161 componentCount = VariableComponentCount(var->type) * elementCount;
3162 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3163 componentCount > caps.maxTransformFeedbackSeparateComponents)
3164 {
3165 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3166 << componentCount << ") exceed the maximum separate components ("
3167 << caps.maxTransformFeedbackSeparateComponents << ").";
3168 return false;
3169 }
3170
3171 totalComponents += componentCount;
3172 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3173 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3174 {
3175 infoLog << "Transform feedback varying total components (" << totalComponents
3176 << ") exceed the maximum interleaved components ("
3177 << caps.maxTransformFeedbackInterleavedComponents << ").";
3178 return false;
3179 }
3180 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003181 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003182}
3183
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003184bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
3185{
3186 const std::vector<sh::Uniform> &vertexUniforms =
3187 mState.mAttachedVertexShader->getUniforms(context);
3188 const std::vector<sh::Uniform> &fragmentUniforms =
3189 mState.mAttachedFragmentShader->getUniforms(context);
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003190 const std::vector<sh::Uniform> *geometryUniforms =
3191 (mState.mAttachedGeometryShader) ? &mState.mAttachedGeometryShader->getUniforms(context)
3192 : nullptr;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003193 const std::vector<sh::Attribute> &attributes =
3194 mState.mAttachedVertexShader->getActiveAttributes(context);
3195 for (const auto &attrib : attributes)
3196 {
3197 for (const auto &uniform : vertexUniforms)
3198 {
3199 if (uniform.name == attrib.name)
3200 {
3201 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3202 return false;
3203 }
3204 }
3205 for (const auto &uniform : fragmentUniforms)
3206 {
3207 if (uniform.name == attrib.name)
3208 {
3209 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3210 return false;
3211 }
3212 }
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003213 if (geometryUniforms)
3214 {
3215 for (const auto &uniform : *geometryUniforms)
3216 {
3217 if (uniform.name == attrib.name)
3218 {
3219 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3220 return false;
3221 }
3222 }
3223 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003224 }
3225 return true;
3226}
3227
Jamie Madill3c1da042017-11-27 18:33:40 -05003228void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003229{
3230 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003231 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003232 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003233 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003234 std::vector<unsigned int> subscripts;
3235 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003236 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003237 if (!subscripts.empty())
3238 {
3239 subscript = subscripts.back();
3240 }
Jamie Madill192745a2016-12-22 15:58:21 -05003241 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003242 {
Jamie Madill192745a2016-12-22 15:58:21 -05003243 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003244 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003245 {
jchen10a9042d32017-03-17 08:50:45 +08003246 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3247 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003248 break;
3249 }
jchen108225e732017-11-14 16:29:03 +08003250 else if (varying->isStruct())
3251 {
3252 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3253 if (field != nullptr)
3254 {
3255 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3256 break;
3257 }
3258 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003259 }
3260 }
James Darpinian30b604d2018-03-12 17:26:57 -07003261 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003262}
3263
Jamie Madill3c1da042017-11-27 18:33:40 -05003264ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003265{
Jamie Madill3c1da042017-11-27 18:33:40 -05003266 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003267
Jiawei Shao3d404882017-10-16 13:30:48 +08003268 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003269 {
Jamie Madill192745a2016-12-22 15:58:21 -05003270 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003271 }
3272
Jiawei Shao3d404882017-10-16 13:30:48 +08003273 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003274 {
Jamie Madill192745a2016-12-22 15:58:21 -05003275 merged[varying.name].fragment = &varying;
3276 }
3277
3278 return merged;
3279}
3280
Jamie Madillbd044ed2017-06-05 12:59:21 -04003281void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003282{
Jamie Madillbd044ed2017-06-05 12:59:21 -04003283 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003284 ASSERT(fragmentShader != nullptr);
3285
Geoff Lange0cff192017-05-30 13:04:56 -04003286 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003287 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003288 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003289
3290 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04003291 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04003292 {
3293 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3294 outputVariable.name != "gl_FragData")
3295 {
3296 continue;
3297 }
3298
3299 unsigned int baseLocation =
3300 (outputVariable.location == -1 ? 0u
3301 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003302
3303 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3304 // structures, so we may use getBasicTypeElementCount().
3305 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3306 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003307 {
3308 const unsigned int location = baseLocation + elementIndex;
3309 if (location >= mState.mOutputVariableTypes.size())
3310 {
3311 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3312 }
Corentin Walleze7557742017-06-01 13:09:57 -04003313 ASSERT(location < mState.mActiveOutputVariables.size());
3314 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003315 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003316 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003317 }
3318 }
3319
Jamie Madill80a6fc02015-08-21 16:53:16 -04003320 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04003321 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003322 return;
3323
Jamie Madillbd044ed2017-06-05 12:59:21 -04003324 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04003325 // TODO(jmadill): any caps validation here?
3326
jchen1015015f72017-03-16 13:54:21 +08003327 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003328 outputVariableIndex++)
3329 {
jchen1015015f72017-03-16 13:54:21 +08003330 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003331
Olli Etuahod2551232017-10-26 20:03:33 +03003332 if (outputVariable.isArray())
3333 {
3334 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3335 // Resources and including [0] at the end of array variable names.
3336 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3337 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3338 }
3339
Jamie Madill80a6fc02015-08-21 16:53:16 -04003340 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3341 if (outputVariable.isBuiltIn())
3342 continue;
3343
3344 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003345 unsigned int baseLocation =
3346 (outputVariable.location == -1 ? 0u
3347 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003348
Olli Etuaho465835d2017-09-26 13:34:10 +03003349 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3350 // structures, so we may use getBasicTypeElementCount().
3351 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3352 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003353 {
Olli Etuahod2551232017-10-26 20:03:33 +03003354 const unsigned int location = baseLocation + elementIndex;
3355 if (location >= mState.mOutputLocations.size())
3356 {
3357 mState.mOutputLocations.resize(location + 1);
3358 }
3359 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003360 if (outputVariable.isArray())
3361 {
3362 mState.mOutputLocations[location] =
3363 VariableLocation(elementIndex, outputVariableIndex);
3364 }
3365 else
3366 {
3367 VariableLocation locationInfo;
3368 locationInfo.index = outputVariableIndex;
3369 mState.mOutputLocations[location] = locationInfo;
3370 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003371 }
3372 }
3373}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003374
Olli Etuaho48fed632017-03-16 12:05:30 +00003375void Program::setUniformValuesFromBindingQualifiers()
3376{
Jamie Madill982f6e02017-06-07 14:33:04 -04003377 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003378 {
3379 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3380 if (samplerUniform.binding != -1)
3381 {
Olli Etuahod2551232017-10-26 20:03:33 +03003382 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003383 ASSERT(location != -1);
3384 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003385 for (unsigned int elementIndex = 0;
3386 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003387 {
3388 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3389 }
3390 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3391 boundTextureUnits.data());
3392 }
3393 }
3394}
3395
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003396void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003397{
jchen10af713a22017-04-19 09:10:56 +08003398 // Set initial bindings from shader.
3399 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3400 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003401 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003402 bindUniformBlock(blockIndex, uniformBlock.binding);
3403 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003404}
3405
Jamie Madille7d84322017-01-10 18:21:59 -05003406void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003407 GLsizei clampedCount,
3408 const GLint *v)
3409{
Jamie Madill81c2e252017-09-09 23:32:46 -04003410 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3411 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3412 std::vector<GLuint> *boundTextureUnits =
3413 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003414
Olli Etuaho1734e172017-10-27 15:30:27 +03003415 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003416
3417 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003418 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003419}
3420
3421template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003422GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3423 GLsizei count,
3424 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003425 const T *v)
3426{
Jamie Madill134f93d2017-08-31 17:11:00 -04003427 if (count == 1)
3428 return 1;
3429
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003430 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003431
Corentin Wallez15ac5342016-11-03 17:06:39 -04003432 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3433 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003434 unsigned int remainingElements =
3435 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003436 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003437 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003438
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003439 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003440 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003441 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003442 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003443
3444 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003445}
3446
3447template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003448GLsizei Program::clampMatrixUniformCount(GLint location,
3449 GLsizei count,
3450 GLboolean transpose,
3451 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003452{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003453 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3454
Jamie Madill62d31cb2015-09-11 13:25:51 -04003455 if (!transpose)
3456 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003457 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003458 }
3459
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003460 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003461
3462 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3463 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003464 unsigned int remainingElements =
3465 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003466 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003467}
3468
Jamie Madill54164b02017-08-28 15:17:37 -04003469// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3470// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003471template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003472void Program::getUniformInternal(const Context *context,
3473 DestT *dataOut,
3474 GLint location,
3475 GLenum nativeType,
3476 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003477{
Jamie Madill54164b02017-08-28 15:17:37 -04003478 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003479 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003480 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003481 {
3482 GLint tempValue[16] = {0};
3483 mProgram->getUniformiv(context, location, tempValue);
3484 UniformStateQueryCastLoop<GLboolean>(
3485 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003486 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003487 }
3488 case GL_INT:
3489 {
3490 GLint tempValue[16] = {0};
3491 mProgram->getUniformiv(context, location, tempValue);
3492 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3493 components);
3494 break;
3495 }
3496 case GL_UNSIGNED_INT:
3497 {
3498 GLuint tempValue[16] = {0};
3499 mProgram->getUniformuiv(context, location, tempValue);
3500 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3501 components);
3502 break;
3503 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003504 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003505 {
3506 GLfloat tempValue[16] = {0};
3507 mProgram->getUniformfv(context, location, tempValue);
3508 UniformStateQueryCastLoop<GLfloat>(
3509 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003510 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003511 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003512 default:
3513 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003514 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003515 }
3516}
Jamie Madilla4595b82017-01-11 17:36:34 -05003517
3518bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3519{
3520 // Must be called after samplers are validated.
3521 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3522
3523 for (const auto &binding : mState.mSamplerBindings)
3524 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003525 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003526 for (const auto &unit : binding.boundTextureUnits)
3527 {
3528 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3529 if (programTextureID == textureID)
3530 {
3531 // TODO(jmadill): Check for appropriate overlap.
3532 return true;
3533 }
3534 }
3535 }
3536
3537 return false;
3538}
3539
Jamie Madilla2c74982016-12-12 11:20:42 -05003540} // namespace gl