blob: 8e1abb98cb57e2b6737a801b3a94e13084c980c6 [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 {
216 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
217 {
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{
280 if (state.getAttachedVertexShader())
281 {
282 blockLinker->addShaderBlocks(GL_VERTEX_SHADER,
283 &state.getAttachedVertexShader()->getUniformBlocks(context));
284 }
285
286 if (state.getAttachedFragmentShader())
287 {
288 blockLinker->addShaderBlocks(GL_FRAGMENT_SHADER,
289 &state.getAttachedFragmentShader()->getUniformBlocks(context));
290 }
291
292 if (state.getAttachedComputeShader())
293 {
294 blockLinker->addShaderBlocks(GL_COMPUTE_SHADER,
295 &state.getAttachedComputeShader()->getUniformBlocks(context));
296 }
297}
298
299void InitShaderStorageBlockLinker(const gl::Context *context,
300 const ProgramState &state,
301 ShaderStorageBlockLinker *blockLinker)
302{
303 if (state.getAttachedVertexShader())
304 {
305 blockLinker->addShaderBlocks(
306 GL_VERTEX_SHADER, &state.getAttachedVertexShader()->getShaderStorageBlocks(context));
307 }
308
309 if (state.getAttachedFragmentShader())
310 {
311 blockLinker->addShaderBlocks(
312 GL_FRAGMENT_SHADER,
313 &state.getAttachedFragmentShader()->getShaderStorageBlocks(context));
314 }
315
316 if (state.getAttachedComputeShader())
317 {
318 blockLinker->addShaderBlocks(
319 GL_COMPUTE_SHADER, &state.getAttachedComputeShader()->getShaderStorageBlocks(context));
320 }
321}
322
jchen108225e732017-11-14 16:29:03 +0800323// Find the matching varying or field by name.
324const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
325 const std::string &name)
326{
327 const sh::ShaderVariable *var = nullptr;
328 for (const auto &ref : varyings)
329 {
330 const sh::Varying *varying = ref.second.get();
331 if (varying->name == name)
332 {
333 var = varying;
334 break;
335 }
336 var = FindShaderVarField(*varying, name);
337 if (var != nullptr)
338 {
339 break;
340 }
341 }
342 return var;
343}
344
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800345void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
346{
347 ASSERT(mismatchedFieldName);
348 if (mismatchedFieldName->empty())
349 {
350 *mismatchedFieldName = parentName;
351 }
352 else
353 {
354 std::ostringstream stream;
355 stream << parentName << "." << *mismatchedFieldName;
356 *mismatchedFieldName = stream.str();
357 }
358}
359
360const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
361{
362 switch (linkError)
363 {
364 case LinkMismatchError::TYPE_MISMATCH:
365 return "Type";
366 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
367 return "Array size";
368 case LinkMismatchError::PRECISION_MISMATCH:
369 return "Precision";
370 case LinkMismatchError::STRUCT_NAME_MISMATCH:
371 return "Structure name";
372 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
373 return "Field number";
374 case LinkMismatchError::FIELD_NAME_MISMATCH:
375 return "Field name";
376
377 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
378 return "Interpolation type";
379 case LinkMismatchError::INVARIANCE_MISMATCH:
380 return "Invariance";
381
382 case LinkMismatchError::BINDING_MISMATCH:
383 return "Binding layout qualifier";
384 case LinkMismatchError::LOCATION_MISMATCH:
385 return "Location layout qualifier";
386 case LinkMismatchError::OFFSET_MISMATCH:
387 return "Offset layout qualilfier";
388
389 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
390 return "Layout qualifier";
391 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
392 return "Matrix Packing";
393 default:
394 UNREACHABLE();
395 return "";
396 }
397}
398
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800399LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
400 const sh::InterfaceBlockField &blockField2,
401 bool webglCompatibility,
402 std::string *mismatchedBlockFieldName)
403{
404 if (blockField1.name != blockField2.name)
405 {
406 return LinkMismatchError::FIELD_NAME_MISMATCH;
407 }
408
409 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
410 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
411 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
412 if (linkError != LinkMismatchError::NO_MISMATCH)
413 {
414 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
415 return linkError;
416 }
417
418 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
419 {
420 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
421 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
422 }
423
424 return LinkMismatchError::NO_MISMATCH;
425}
426
427LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
428 const sh::InterfaceBlock &interfaceBlock2,
429 bool webglCompatibility,
430 std::string *mismatchedBlockFieldName)
431{
432 // validate blocks for the same member types
433 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
434 {
435 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
436 }
437 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
438 {
439 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
440 }
441 if (interfaceBlock1.layout != interfaceBlock2.layout ||
442 interfaceBlock1.binding != interfaceBlock2.binding)
443 {
444 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
445 }
446 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
447 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
448 {
449 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
450 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
451
452 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
453 member1, member2, webglCompatibility, mismatchedBlockFieldName);
454 if (linkError != LinkMismatchError::NO_MISMATCH)
455 {
456 return linkError;
457 }
458 }
459 return LinkMismatchError::NO_MISMATCH;
460}
461
462bool ValidateGraphicsInterfaceBlocks(const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
463 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
464 InfoLog &infoLog,
465 bool webglCompatibility,
466 sh::BlockType blockType,
467 GLuint maxCombinedInterfaceBlocks)
468{
469 // Check that interface blocks defined in the vertex and fragment shaders are identical
470 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
471 InterfaceBlockMap linkedInterfaceBlocks;
472 GLuint blockCount = 0;
473
474 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
475 {
476 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
477 if (IsActiveInterfaceBlock(vertexInterfaceBlock))
478 {
479 blockCount += std::max(vertexInterfaceBlock.arraySize, 1u);
480 }
481 }
482
483 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
484 {
485 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
486 if (entry != linkedInterfaceBlocks.end())
487 {
488 const sh::InterfaceBlock &vertexInterfaceBlock = *(entry->second);
489 std::string mismatchedBlockFieldName;
490 LinkMismatchError linkError =
491 AreMatchingInterfaceBlocks(vertexInterfaceBlock, fragmentInterfaceBlock,
492 webglCompatibility, &mismatchedBlockFieldName);
493 if (linkError != LinkMismatchError::NO_MISMATCH)
494 {
495 LogLinkMismatch(infoLog, fragmentInterfaceBlock.name, "interface block", linkError,
496 mismatchedBlockFieldName, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER);
497 return false;
498 }
499 }
500
501 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
502 // If a uniform block is used by multiple shader stages, each such use counts separately
503 // against this combined limit.
504 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
505 // If a shader storage block in a program is referenced by multiple shaders, each such
506 // reference counts separately against this combined limit.
507 if (IsActiveInterfaceBlock(fragmentInterfaceBlock))
508 {
509 blockCount += std::max(fragmentInterfaceBlock.arraySize, 1u);
510 }
511 }
512
513 if (blockCount > maxCombinedInterfaceBlocks)
514 {
515 switch (blockType)
516 {
517 case sh::BlockType::BLOCK_UNIFORM:
518 infoLog << "The sum of the number of active uniform blocks exceeds "
519 "MAX_COMBINED_UNIFORM_BLOCKS ("
520 << maxCombinedInterfaceBlocks << ").";
521 break;
522 case sh::BlockType::BLOCK_BUFFER:
523 infoLog << "The sum of the number of active shader storage blocks exceeds "
524 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
525 << maxCombinedInterfaceBlocks << ").";
526 break;
527 default:
528 UNREACHABLE();
529 }
530 return false;
531 }
532 return true;
533}
534
Jamie Madill62d31cb2015-09-11 13:25:51 -0400535} // anonymous namespace
536
Jamie Madill4a3c2342015-10-08 12:58:45 -0400537const char *const g_fakepath = "C:\\fakepath";
538
Jamie Madill3c1da042017-11-27 18:33:40 -0500539// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400540InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000541{
542}
543
544InfoLog::~InfoLog()
545{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000546}
547
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400548size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000549{
Jamie Madill23176ce2017-07-31 14:14:33 -0400550 if (!mLazyStream)
551 {
552 return 0;
553 }
554
555 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400556 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000557}
558
Geoff Lange1a27752015-10-05 13:16:04 -0400559void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000560{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400561 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000562
563 if (bufSize > 0)
564 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400565 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400566
Jamie Madill23176ce2017-07-31 14:14:33 -0400567 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000568 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400569 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
570 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000571 }
572
573 infoLog[index] = '\0';
574 }
575
576 if (length)
577 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400578 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000579 }
580}
581
582// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300583// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000584// messages, so lets remove all occurrences of this fake file path from the log.
585void InfoLog::appendSanitized(const char *message)
586{
Jamie Madill23176ce2017-07-31 14:14:33 -0400587 ensureInitialized();
588
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000589 std::string msg(message);
590
591 size_t found;
592 do
593 {
594 found = msg.find(g_fakepath);
595 if (found != std::string::npos)
596 {
597 msg.erase(found, strlen(g_fakepath));
598 }
599 }
600 while (found != std::string::npos);
601
Jamie Madill23176ce2017-07-31 14:14:33 -0400602 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000603}
604
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000605void InfoLog::reset()
606{
Jiawei Shao02f15232017-12-27 10:10:28 +0800607 if (mLazyStream)
608 {
609 mLazyStream.reset(nullptr);
610 }
611}
612
613bool InfoLog::empty() const
614{
615 if (!mLazyStream)
616 {
617 return true;
618 }
619
620 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000621}
622
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800623void LogLinkMismatch(InfoLog &infoLog,
624 const std::string &variableName,
625 const char *variableType,
626 LinkMismatchError linkError,
627 const std::string &mismatchedStructOrBlockFieldName,
628 GLenum shaderType1,
629 GLenum shaderType2)
630{
631 std::ostringstream stream;
632 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
633 << variableName;
634
635 if (!mismatchedStructOrBlockFieldName.empty())
636 {
637 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
638 }
639
640 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
641 << GetShaderTypeString(shaderType2) << " shaders.";
642
643 infoLog << stream.str();
644}
645
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800646bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
647{
648 // Only 'packed' blocks are allowed to be considered inactive.
649 return interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
650}
651
Jamie Madill3c1da042017-11-27 18:33:40 -0500652// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300653VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000654{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500655}
656
Olli Etuahoc8538042017-09-27 11:20:15 +0300657VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300658 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500659{
Olli Etuahoc8538042017-09-27 11:20:15 +0300660 ASSERT(arrayIndex != GL_INVALID_INDEX);
661}
662
Jamie Madill3c1da042017-11-27 18:33:40 -0500663// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800664SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500665 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
666{
667}
668
669SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
670
671SamplerBinding::~SamplerBinding() = default;
672
Jamie Madill3c1da042017-11-27 18:33:40 -0500673// ProgramBindings implementation.
674ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500675{
676}
677
Jamie Madill3c1da042017-11-27 18:33:40 -0500678ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500679{
680}
681
Jamie Madill3c1da042017-11-27 18:33:40 -0500682void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400683{
684 mBindings[name] = index;
685}
686
Jamie Madill3c1da042017-11-27 18:33:40 -0500687int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400688{
689 auto iter = mBindings.find(name);
690 return (iter != mBindings.end()) ? iter->second : -1;
691}
692
Jamie Madill3c1da042017-11-27 18:33:40 -0500693ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400694{
695 return mBindings.begin();
696}
697
Jamie Madill3c1da042017-11-27 18:33:40 -0500698ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400699{
700 return mBindings.end();
701}
702
Jamie Madill3c1da042017-11-27 18:33:40 -0500703// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500704ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
705{
706}
707ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
708{
709 for (size_t index = 0; index < count; ++index)
710 {
711 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
712 }
713}
714
715ImageBinding::ImageBinding(const ImageBinding &other) = default;
716
717ImageBinding::~ImageBinding() = default;
718
Jamie Madill3c1da042017-11-27 18:33:40 -0500719// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400720ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500721 : mLabel(),
722 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400723 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800725 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500726 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400727 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500728 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800729 mImageUniformRange(0, 0),
730 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300731 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800732 mNumViews(-1),
733 // [GL_EXT_geometry_shader] Table 20.22
734 mGeometryShaderInputPrimitiveType(GL_TRIANGLES),
735 mGeometryShaderOutputPrimitiveType(GL_TRIANGLE_STRIP),
736 mGeometryShaderInvocations(1),
737 mGeometryShaderMaxVertices(0)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400738{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300739 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400740}
741
Jamie Madill48ef11b2016-04-27 15:21:52 -0400742ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400743{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800744 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
745 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400746}
747
Jamie Madill48ef11b2016-04-27 15:21:52 -0400748const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500749{
750 return mLabel;
751}
752
Jamie Madille7d84322017-01-10 18:21:59 -0500753GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400754{
jchen1015015f72017-03-16 13:54:21 +0800755 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400756}
757
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800758GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
759{
760 return GetResourceIndexFromName(mBufferVariables, name);
761}
762
Jamie Madille7d84322017-01-10 18:21:59 -0500763GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
764{
765 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
766 return mUniformLocations[location].index;
767}
768
769Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
770{
771 GLuint index = getUniformIndexFromLocation(location);
772 if (!isSamplerUniformIndex(index))
773 {
774 return Optional<GLuint>::Invalid();
775 }
776
777 return getSamplerIndexFromUniformIndex(index);
778}
779
780bool ProgramState::isSamplerUniformIndex(GLuint index) const
781{
Jamie Madill982f6e02017-06-07 14:33:04 -0400782 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500783}
784
785GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
786{
787 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400788 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500789}
790
Jamie Madill34ca4f52017-06-13 11:49:39 -0400791GLuint ProgramState::getAttributeLocation(const std::string &name) const
792{
793 for (const sh::Attribute &attribute : mAttributes)
794 {
795 if (attribute.name == name)
796 {
797 return attribute.location;
798 }
799 }
800
801 return static_cast<GLuint>(-1);
802}
803
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500804Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400805 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400806 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500807 mLinked(false),
808 mDeleteStatus(false),
809 mRefCount(0),
810 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500811 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500812{
813 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000814
Geoff Lang7dd2e102014-11-10 15:19:26 -0500815 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
818Program::~Program()
819{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400820 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
Jamie Madill4928b7c2017-06-20 12:57:39 -0400823void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500824{
825 if (mState.mAttachedVertexShader != nullptr)
826 {
827 mState.mAttachedVertexShader->release(context);
828 mState.mAttachedVertexShader = nullptr;
829 }
830
831 if (mState.mAttachedFragmentShader != nullptr)
832 {
833 mState.mAttachedFragmentShader->release(context);
834 mState.mAttachedFragmentShader = nullptr;
835 }
836
837 if (mState.mAttachedComputeShader != nullptr)
838 {
839 mState.mAttachedComputeShader->release(context);
840 mState.mAttachedComputeShader = nullptr;
841 }
842
Jiawei Shao89be29a2017-11-06 14:36:45 +0800843 if (mState.mAttachedGeometryShader != nullptr)
844 {
845 mState.mAttachedGeometryShader->release(context);
846 mState.mAttachedGeometryShader = nullptr;
847 }
848
Jamie Madillb7d924a2018-03-10 11:16:54 -0500849 // TODO(jmadill): Handle error in the Context.
850 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400851
852 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800853 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400854 SafeDelete(mProgram);
855
856 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500857}
858
Geoff Lang70d0f492015-12-10 17:45:46 -0500859void Program::setLabel(const std::string &label)
860{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400861 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500862}
863
864const std::string &Program::getLabel() const
865{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400866 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500867}
868
Jamie Madillef300b12016-10-07 15:12:09 -0400869void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300871 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300873 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874 {
Jamie Madillef300b12016-10-07 15:12:09 -0400875 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300876 mState.mAttachedVertexShader = shader;
877 mState.mAttachedVertexShader->addRef();
878 break;
879 }
880 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881 {
Jamie Madillef300b12016-10-07 15:12:09 -0400882 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300883 mState.mAttachedFragmentShader = shader;
884 mState.mAttachedFragmentShader->addRef();
885 break;
886 }
887 case GL_COMPUTE_SHADER:
888 {
Jamie Madillef300b12016-10-07 15:12:09 -0400889 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300890 mState.mAttachedComputeShader = shader;
891 mState.mAttachedComputeShader->addRef();
892 break;
893 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800894 case GL_GEOMETRY_SHADER_EXT:
895 {
896 ASSERT(!mState.mAttachedGeometryShader);
897 mState.mAttachedGeometryShader = shader;
898 mState.mAttachedGeometryShader->addRef();
899 break;
900 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300901 default:
902 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904}
905
Jamie Madillc1d770e2017-04-13 17:31:24 -0400906void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300908 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300910 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400912 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500913 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300914 mState.mAttachedVertexShader = nullptr;
915 break;
916 }
917 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400919 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500920 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300921 mState.mAttachedFragmentShader = nullptr;
922 break;
923 }
924 case GL_COMPUTE_SHADER:
925 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400926 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500927 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300928 mState.mAttachedComputeShader = nullptr;
929 break;
930 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800931 case GL_GEOMETRY_SHADER_EXT:
932 {
933 ASSERT(mState.mAttachedGeometryShader == shader);
934 shader->release(context);
935 mState.mAttachedGeometryShader = nullptr;
936 break;
937 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300938 default:
939 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000943int Program::getAttachedShadersCount() const
944{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300945 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800946 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000947}
948
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949void Program::bindAttributeLocation(GLuint index, const char *name)
950{
Geoff Langd8605522016-04-13 10:19:12 -0400951 mAttributeBindings.bindLocation(index, name);
952}
953
954void Program::bindUniformLocation(GLuint index, const char *name)
955{
Olli Etuahod2551232017-10-26 20:03:33 +0300956 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
Sami Väisänen46eaa942016-06-29 10:26:37 +0300959void Program::bindFragmentInputLocation(GLint index, const char *name)
960{
961 mFragmentInputBindings.bindLocation(index, name);
962}
963
Jamie Madillbd044ed2017-06-05 12:59:21 -0400964BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300965{
966 BindingInfo ret;
967 ret.type = GL_NONE;
968 ret.valid = false;
969
Jamie Madillbd044ed2017-06-05 12:59:21 -0400970 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300971 ASSERT(fragmentShader);
972
973 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800974 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300975
976 for (const auto &binding : mFragmentInputBindings)
977 {
978 if (binding.second != static_cast<GLuint>(index))
979 continue;
980
981 ret.valid = true;
982
Olli Etuahod2551232017-10-26 20:03:33 +0300983 size_t nameLengthWithoutArrayIndex;
984 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300985
986 for (const auto &in : inputs)
987 {
Olli Etuahod2551232017-10-26 20:03:33 +0300988 if (in.name.length() == nameLengthWithoutArrayIndex &&
989 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300990 {
991 if (in.isArray())
992 {
993 // The client wants to bind either "name" or "name[0]".
994 // GL ES 3.1 spec refers to active array names with language such as:
995 // "if the string identifies the base name of an active array, where the
996 // string would exactly match the name of the variable if the suffix "[0]"
997 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400998 if (arrayIndex == GL_INVALID_INDEX)
999 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001000
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001001 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001002 }
1003 else
1004 {
1005 ret.name = in.mappedName;
1006 }
1007 ret.type = in.type;
1008 return ret;
1009 }
1010 }
1011 }
1012
1013 return ret;
1014}
1015
Jamie Madillbd044ed2017-06-05 12:59:21 -04001016void Program::pathFragmentInputGen(const Context *context,
1017 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001018 GLenum genMode,
1019 GLint components,
1020 const GLfloat *coeffs)
1021{
1022 // If the location is -1 then the command is silently ignored
1023 if (index == -1)
1024 return;
1025
Jamie Madillbd044ed2017-06-05 12:59:21 -04001026 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001027
1028 // If the input doesn't exist then then the command is silently ignored
1029 // This could happen through optimization for example, the shader translator
1030 // decides that a variable is not actually being used and optimizes it away.
1031 if (binding.name.empty())
1032 return;
1033
1034 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1035}
1036
Martin Radev4c4c8e72016-08-04 12:25:34 +03001037// The attached shaders are checked for linking errors by matching up their variables.
1038// Uniform, input and output variables get collected.
1039// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001040Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001041{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001042 const auto &data = context->getContextState();
1043
Jamie Madill6c58b062017-08-01 13:44:25 -04001044 auto *platform = ANGLEPlatformCurrent();
1045 double startTime = platform->currentTime(platform);
1046
Jamie Madill6c1f6712017-02-14 19:08:04 -05001047 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001048
Jamie Madill32447362017-06-28 14:53:52 -04001049 ProgramHash programHash;
1050 auto *cache = context->getMemoryProgramCache();
1051 if (cache)
1052 {
1053 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001054 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001055 }
1056
1057 if (mLinked)
1058 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001059 double delta = platform->currentTime(platform) - startTime;
1060 int us = static_cast<int>(delta * 1000000.0);
1061 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001062 return NoError();
1063 }
1064
1065 // Cache load failed, fall through to normal linking.
1066 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001067 mInfoLog.reset();
1068
Jiawei Shao73618602017-12-20 15:47:15 +08001069 if (!linkValidateShaders(context, mInfoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001070 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001071 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001072 }
1073
Jiawei Shao73618602017-12-20 15:47:15 +08001074 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -05001075 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001076 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001077 {
1078 return NoError();
1079 }
1080
Jiajia Qin729b2c62017-08-14 09:36:11 +08001081 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001082 {
1083 return NoError();
1084 }
1085
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001086 ProgramLinkedResources resources = {
1087 {0, PackMode::ANGLE_RELAXED},
1088 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001089 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1090 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001091
1092 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1093 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1094
1095 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001096 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001097 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001098 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001099 }
1100 }
1101 else
1102 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001103 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001104 {
1105 return NoError();
1106 }
1107
Jamie Madillbd044ed2017-06-05 12:59:21 -04001108 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001109 {
1110 return NoError();
1111 }
1112
Jamie Madillbd044ed2017-06-05 12:59:21 -04001113 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001114 {
1115 return NoError();
1116 }
1117
Jiajia Qin729b2c62017-08-14 09:36:11 +08001118 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001119 {
1120 return NoError();
1121 }
1122
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001123 if (!linkValidateGlobalNames(context, mInfoLog))
1124 {
1125 return NoError();
1126 }
1127
Jamie Madillbd044ed2017-06-05 12:59:21 -04001128 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001129
Jiawei Shao73618602017-12-20 15:47:15 +08001130 ASSERT(mState.mAttachedVertexShader);
1131 mState.mNumViews = mState.mAttachedVertexShader->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +03001132
Jamie Madillbd044ed2017-06-05 12:59:21 -04001133 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001134
Jamie Madill192745a2016-12-22 15:58:21 -05001135 // Map the varyings to the register file
1136 // In WebGL, we use a slightly different handling for packing variables.
Jamie Madill61d53252018-01-31 14:49:24 -05001137 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1138 if (data.getLimitations().noFlexibleVaryingPacking)
1139 {
1140 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1141 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1142 }
1143 else if (data.getExtensions().webglCompatibility)
1144 {
1145 packMode = PackMode::WEBGL_STRICT;
1146 }
Jamie Madillc9727f32017-11-07 12:37:07 -05001147
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001148 ProgramLinkedResources resources = {
1149 {data.getCaps().maxVaryingVectors, packMode},
1150 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001151 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1152 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001153
1154 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1155 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1156
Jiawei Shao73618602017-12-20 15:47:15 +08001157 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001158 {
1159 return NoError();
1160 }
1161
jchen1085c93c42017-11-12 15:36:47 +08001162 if (!resources.varyingPacking.collectAndPackUserVaryings(
1163 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001164 {
1165 return NoError();
1166 }
1167
Jamie Madillc9727f32017-11-07 12:37:07 -05001168 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001169 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001170 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001171 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001172 }
1173
1174 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001175 }
1176
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001177 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001178
jchen10eaef1e52017-06-13 10:44:11 +08001179 setUniformValuesFromBindingQualifiers();
1180
Yunchao Heece12532017-11-21 15:50:21 +08001181 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1182 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001183 ASSERT(mLinked);
1184 updateLinkedShaderStages();
1185
Jamie Madill54164b02017-08-28 15:17:37 -04001186 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001187 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001188
Jamie Madill32447362017-06-28 14:53:52 -04001189 // Save to the program cache.
1190 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
1191 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
1192 {
1193 cache->putProgram(programHash, context, this);
1194 }
1195
Jamie Madill6c58b062017-08-01 13:44:25 -04001196 double delta = platform->currentTime(platform) - startTime;
1197 int us = static_cast<int>(delta * 1000000.0);
1198 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
1199
Martin Radev4c4c8e72016-08-04 12:25:34 +03001200 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001201}
1202
Yunchao He85072e82017-11-14 15:43:28 +08001203void Program::updateLinkedShaderStages()
1204{
Yunchao Heece12532017-11-21 15:50:21 +08001205 mState.mLinkedShaderStages.reset();
1206
Yunchao He85072e82017-11-14 15:43:28 +08001207 if (mState.mAttachedVertexShader)
1208 {
1209 mState.mLinkedShaderStages.set(SHADER_VERTEX);
1210 }
1211
1212 if (mState.mAttachedFragmentShader)
1213 {
1214 mState.mLinkedShaderStages.set(SHADER_FRAGMENT);
1215 }
1216
1217 if (mState.mAttachedComputeShader)
1218 {
1219 mState.mLinkedShaderStages.set(SHADER_COMPUTE);
1220 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001221
1222 if (mState.mAttachedGeometryShader)
1223 {
1224 mState.mLinkedShaderStages.set(SHADER_GEOMETRY);
1225 }
Yunchao He85072e82017-11-14 15:43:28 +08001226}
1227
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001228// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001229void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001230{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001231 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001232 mState.mAttributesTypeMask.reset();
1233 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001234 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001235 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001236 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001237 mState.mUniforms.clear();
1238 mState.mUniformLocations.clear();
1239 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001240 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001241 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001242 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001243 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001244 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001245 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001246 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001247 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001248 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001249 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001250 mState.mNumViews = -1;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001251 mState.mGeometryShaderInputPrimitiveType = GL_TRIANGLES;
1252 mState.mGeometryShaderOutputPrimitiveType = GL_TRIANGLE_STRIP;
1253 mState.mGeometryShaderInvocations = 1;
1254 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001255
Geoff Lang7dd2e102014-11-10 15:19:26 -05001256 mValidated = false;
1257
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001258 mLinked = false;
1259}
1260
Geoff Lange1a27752015-10-05 13:16:04 -04001261bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001262{
1263 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001264}
1265
Jamie Madilla2c74982016-12-12 11:20:42 -05001266Error Program::loadBinary(const Context *context,
1267 GLenum binaryFormat,
1268 const void *binary,
1269 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001270{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001271 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001272
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001274 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001275#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001276 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1277 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001278 {
Jamie Madillf6113162015-05-07 11:49:21 -04001279 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001280 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001281 }
1282
Jamie Madill4f86d052017-06-05 12:59:26 -04001283 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1284 ANGLE_TRY_RESULT(
1285 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001286
1287 // Currently we require the full shader text to compute the program hash.
1288 // TODO(jmadill): Store the binary in the internal program cache.
1289
Jamie Madillb0a838b2016-11-13 20:02:12 -05001290 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001291#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292}
1293
Jamie Madilla2c74982016-12-12 11:20:42 -05001294Error Program::saveBinary(const Context *context,
1295 GLenum *binaryFormat,
1296 void *binary,
1297 GLsizei bufSize,
1298 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001299{
1300 if (binaryFormat)
1301 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001302 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001303 }
1304
Jamie Madill4f86d052017-06-05 12:59:26 -04001305 angle::MemoryBuffer memoryBuf;
1306 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307
Jamie Madill4f86d052017-06-05 12:59:26 -04001308 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1309 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310
1311 if (streamLength > bufSize)
1312 {
1313 if (length)
1314 {
1315 *length = 0;
1316 }
1317
1318 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1319 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1320 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001321 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001322 }
1323
1324 if (binary)
1325 {
1326 char *ptr = reinterpret_cast<char*>(binary);
1327
Jamie Madill48ef11b2016-04-27 15:21:52 -04001328 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329 ptr += streamLength;
1330
1331 ASSERT(ptr - streamLength == binary);
1332 }
1333
1334 if (length)
1335 {
1336 *length = streamLength;
1337 }
1338
He Yunchaoacd18982017-01-04 10:46:42 +08001339 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340}
1341
Jamie Madillffe00c02017-06-27 16:26:55 -04001342GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343{
1344 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001345 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346 if (error.isError())
1347 {
1348 return 0;
1349 }
1350
1351 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001352}
1353
Geoff Langc5629752015-12-07 16:29:04 -05001354void Program::setBinaryRetrievableHint(bool retrievable)
1355{
1356 // TODO(jmadill) : replace with dirty bits
1357 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001358 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001359}
1360
1361bool Program::getBinaryRetrievableHint() const
1362{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001363 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001364}
1365
Yunchao He61afff12017-03-14 15:34:03 +08001366void Program::setSeparable(bool separable)
1367{
1368 // TODO(yunchao) : replace with dirty bits
1369 if (mState.mSeparable != separable)
1370 {
1371 mProgram->setSeparable(separable);
1372 mState.mSeparable = separable;
1373 }
1374}
1375
1376bool Program::isSeparable() const
1377{
1378 return mState.mSeparable;
1379}
1380
Jamie Madill6c1f6712017-02-14 19:08:04 -05001381void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001382{
1383 mRefCount--;
1384
1385 if (mRefCount == 0 && mDeleteStatus)
1386 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001387 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001388 }
1389}
1390
1391void Program::addRef()
1392{
1393 mRefCount++;
1394}
1395
1396unsigned int Program::getRefCount() const
1397{
1398 return mRefCount;
1399}
1400
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001401int Program::getInfoLogLength() const
1402{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001403 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001404}
1405
Geoff Lange1a27752015-10-05 13:16:04 -04001406void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001407{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001408 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001409}
1410
Geoff Lange1a27752015-10-05 13:16:04 -04001411void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001412{
1413 int total = 0;
1414
Martin Radev4c4c8e72016-08-04 12:25:34 +03001415 if (mState.mAttachedComputeShader)
1416 {
1417 if (total < maxCount)
1418 {
1419 shaders[total] = mState.mAttachedComputeShader->getHandle();
1420 total++;
1421 }
1422 }
1423
Jamie Madill48ef11b2016-04-27 15:21:52 -04001424 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001425 {
1426 if (total < maxCount)
1427 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001428 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001429 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001430 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001431 }
1432
Jamie Madill48ef11b2016-04-27 15:21:52 -04001433 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001434 {
1435 if (total < maxCount)
1436 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001437 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001438 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001439 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001440 }
1441
Jiawei Shao89be29a2017-11-06 14:36:45 +08001442 if (mState.mAttachedGeometryShader)
1443 {
1444 if (total < maxCount)
1445 {
1446 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1447 total++;
1448 }
1449 }
1450
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001451 if (count)
1452 {
1453 *count = total;
1454 }
1455}
1456
Geoff Lange1a27752015-10-05 13:16:04 -04001457GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001459 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460}
1461
Jamie Madill63805b42015-08-25 13:17:39 -04001462bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001463{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001464 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1465 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466}
1467
jchen10fd7c3b52017-03-21 15:36:03 +08001468void Program::getActiveAttribute(GLuint index,
1469 GLsizei bufsize,
1470 GLsizei *length,
1471 GLint *size,
1472 GLenum *type,
1473 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001474{
Jamie Madillc349ec02015-08-21 16:53:12 -04001475 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001476 {
1477 if (bufsize > 0)
1478 {
1479 name[0] = '\0';
1480 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001482 if (length)
1483 {
1484 *length = 0;
1485 }
1486
1487 *type = GL_NONE;
1488 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001489 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001490 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001491
jchen1036e120e2017-03-14 14:53:58 +08001492 ASSERT(index < mState.mAttributes.size());
1493 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001494
1495 if (bufsize > 0)
1496 {
jchen10fd7c3b52017-03-21 15:36:03 +08001497 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001498 }
1499
1500 // Always a single 'type' instance
1501 *size = 1;
1502 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001503}
1504
Geoff Lange1a27752015-10-05 13:16:04 -04001505GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001506{
Jamie Madillc349ec02015-08-21 16:53:12 -04001507 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001508 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001509 return 0;
1510 }
1511
jchen1036e120e2017-03-14 14:53:58 +08001512 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001513}
1514
Geoff Lange1a27752015-10-05 13:16:04 -04001515GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001516{
Jamie Madillc349ec02015-08-21 16:53:12 -04001517 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001518 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001519 return 0;
1520 }
1521
1522 size_t maxLength = 0;
1523
Jamie Madill48ef11b2016-04-27 15:21:52 -04001524 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001525 {
jchen1036e120e2017-03-14 14:53:58 +08001526 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001527 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528
Jamie Madillc349ec02015-08-21 16:53:12 -04001529 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530}
1531
jchen1015015f72017-03-16 13:54:21 +08001532GLuint Program::getInputResourceIndex(const GLchar *name) const
1533{
Olli Etuahod2551232017-10-26 20:03:33 +03001534 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001535}
1536
1537GLuint Program::getOutputResourceIndex(const GLchar *name) const
1538{
1539 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1540}
1541
jchen10fd7c3b52017-03-21 15:36:03 +08001542size_t Program::getOutputResourceCount() const
1543{
1544 return (mLinked ? mState.mOutputVariables.size() : 0);
1545}
1546
jchen10baf5d942017-08-28 20:45:48 +08001547template <typename T>
1548void Program::getResourceName(GLuint index,
1549 const std::vector<T> &resources,
1550 GLsizei bufSize,
1551 GLsizei *length,
1552 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001553{
1554 if (length)
1555 {
1556 *length = 0;
1557 }
1558
1559 if (!mLinked)
1560 {
1561 if (bufSize > 0)
1562 {
1563 name[0] = '\0';
1564 }
1565 return;
1566 }
jchen10baf5d942017-08-28 20:45:48 +08001567 ASSERT(index < resources.size());
1568 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001569
1570 if (bufSize > 0)
1571 {
Olli Etuahod2551232017-10-26 20:03:33 +03001572 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001573 }
1574}
1575
jchen10baf5d942017-08-28 20:45:48 +08001576void Program::getInputResourceName(GLuint index,
1577 GLsizei bufSize,
1578 GLsizei *length,
1579 GLchar *name) const
1580{
1581 getResourceName(index, mState.mAttributes, bufSize, length, name);
1582}
1583
1584void Program::getOutputResourceName(GLuint index,
1585 GLsizei bufSize,
1586 GLsizei *length,
1587 GLchar *name) const
1588{
1589 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1590}
1591
1592void Program::getUniformResourceName(GLuint index,
1593 GLsizei bufSize,
1594 GLsizei *length,
1595 GLchar *name) const
1596{
1597 getResourceName(index, mState.mUniforms, bufSize, length, name);
1598}
1599
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001600void Program::getBufferVariableResourceName(GLuint index,
1601 GLsizei bufSize,
1602 GLsizei *length,
1603 GLchar *name) const
1604{
1605 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1606}
1607
jchen10880683b2017-04-12 16:21:55 +08001608const sh::Attribute &Program::getInputResource(GLuint index) const
1609{
1610 ASSERT(index < mState.mAttributes.size());
1611 return mState.mAttributes[index];
1612}
1613
1614const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1615{
1616 ASSERT(index < mState.mOutputVariables.size());
1617 return mState.mOutputVariables[index];
1618}
1619
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620GLint Program::getFragDataLocation(const std::string &name) const
1621{
Olli Etuahod2551232017-10-26 20:03:33 +03001622 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001623}
1624
Geoff Lange1a27752015-10-05 13:16:04 -04001625void Program::getActiveUniform(GLuint index,
1626 GLsizei bufsize,
1627 GLsizei *length,
1628 GLint *size,
1629 GLenum *type,
1630 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001631{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001633 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001635 ASSERT(index < mState.mUniforms.size());
1636 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637
1638 if (bufsize > 0)
1639 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001640 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001641 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001642 }
1643
Olli Etuaho465835d2017-09-26 13:34:10 +03001644 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001645 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001646 }
1647 else
1648 {
1649 if (bufsize > 0)
1650 {
1651 name[0] = '\0';
1652 }
1653
1654 if (length)
1655 {
1656 *length = 0;
1657 }
1658
1659 *size = 0;
1660 *type = GL_NONE;
1661 }
1662}
1663
Geoff Lange1a27752015-10-05 13:16:04 -04001664GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001665{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001667 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001668 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001669 }
1670 else
1671 {
1672 return 0;
1673 }
1674}
1675
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001676size_t Program::getActiveBufferVariableCount() const
1677{
1678 return mLinked ? mState.mBufferVariables.size() : 0;
1679}
1680
Geoff Lange1a27752015-10-05 13:16:04 -04001681GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001682{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001684
1685 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001686 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001687 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001689 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001690 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001691 size_t length = uniform.name.length() + 1u;
1692 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693 {
1694 length += 3; // Counting in "[0]".
1695 }
1696 maxLength = std::max(length, maxLength);
1697 }
1698 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700
Jamie Madill62d31cb2015-09-11 13:25:51 -04001701 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702}
1703
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704bool Program::isValidUniformLocation(GLint location) const
1705{
Jamie Madille2e406c2016-06-02 13:04:10 -04001706 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001707 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001708 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001709}
1710
Jamie Madill62d31cb2015-09-11 13:25:51 -04001711const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001713 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001714 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001715}
1716
Jamie Madillac4e9c32017-01-13 14:07:12 -05001717const VariableLocation &Program::getUniformLocation(GLint location) const
1718{
1719 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1720 return mState.mUniformLocations[location];
1721}
1722
1723const std::vector<VariableLocation> &Program::getUniformLocations() const
1724{
1725 return mState.mUniformLocations;
1726}
1727
1728const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1729{
1730 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1731 return mState.mUniforms[index];
1732}
1733
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001734const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1735{
1736 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1737 return mState.mBufferVariables[index];
1738}
1739
Jamie Madill62d31cb2015-09-11 13:25:51 -04001740GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001741{
Olli Etuahod2551232017-10-26 20:03:33 +03001742 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001743}
1744
Jamie Madill62d31cb2015-09-11 13:25:51 -04001745GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001746{
Jamie Madille7d84322017-01-10 18:21:59 -05001747 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001748}
1749
1750void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1751{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001752 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1753 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001754 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755}
1756
1757void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1758{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001759 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1760 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001761 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001762}
1763
1764void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1765{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001766 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1767 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001768 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001769}
1770
1771void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1772{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001773 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1774 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001775 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776}
1777
Jamie Madill81c2e252017-09-09 23:32:46 -04001778Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001779{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001780 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1781 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1782
Jamie Madill81c2e252017-09-09 23:32:46 -04001783 mProgram->setUniform1iv(location, clampedCount, v);
1784
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001785 if (mState.isSamplerUniformIndex(locationInfo.index))
1786 {
1787 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001788 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001789 }
1790
Jamie Madill81c2e252017-09-09 23:32:46 -04001791 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792}
1793
1794void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1795{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001796 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1797 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001798 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799}
1800
1801void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1802{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001803 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1804 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001805 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806}
1807
1808void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1809{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001810 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1811 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001812 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813}
1814
1815void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1816{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001817 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1818 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001819 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820}
1821
1822void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1823{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001824 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1825 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001826 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827}
1828
1829void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1830{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001831 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1832 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001833 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834}
1835
1836void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1837{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001838 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1839 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001840 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841}
1842
1843void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1844{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001845 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001846 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847}
1848
1849void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1850{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001851 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001852 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853}
1854
1855void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1856{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001857 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001858 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859}
1860
1861void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1862{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001863 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001864 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865}
1866
1867void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1868{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001869 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001870 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871}
1872
1873void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1874{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001875 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001876 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877}
1878
1879void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1880{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001881 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001882 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883}
1884
1885void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1886{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001887 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001888 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001889}
1890
1891void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1892{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001893 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001894 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895}
1896
Jamie Madill54164b02017-08-28 15:17:37 -04001897void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898{
Jamie Madill54164b02017-08-28 15:17:37 -04001899 const auto &uniformLocation = mState.getUniformLocations()[location];
1900 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1901
1902 GLenum nativeType = gl::VariableComponentType(uniform.type);
1903 if (nativeType == GL_FLOAT)
1904 {
1905 mProgram->getUniformfv(context, location, v);
1906 }
1907 else
1908 {
1909 getUniformInternal(context, v, location, nativeType,
1910 gl::VariableComponentCount(uniform.type));
1911 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912}
1913
Jamie Madill54164b02017-08-28 15:17:37 -04001914void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001915{
Jamie Madill54164b02017-08-28 15:17:37 -04001916 const auto &uniformLocation = mState.getUniformLocations()[location];
1917 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1918
1919 GLenum nativeType = gl::VariableComponentType(uniform.type);
1920 if (nativeType == GL_INT || nativeType == GL_BOOL)
1921 {
1922 mProgram->getUniformiv(context, location, v);
1923 }
1924 else
1925 {
1926 getUniformInternal(context, v, location, nativeType,
1927 gl::VariableComponentCount(uniform.type));
1928 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929}
1930
Jamie Madill54164b02017-08-28 15:17:37 -04001931void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932{
Jamie Madill54164b02017-08-28 15:17:37 -04001933 const auto &uniformLocation = mState.getUniformLocations()[location];
1934 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1935
1936 GLenum nativeType = gl::VariableComponentType(uniform.type);
1937 if (nativeType == GL_UNSIGNED_INT)
1938 {
1939 mProgram->getUniformuiv(context, location, v);
1940 }
1941 else
1942 {
1943 getUniformInternal(context, v, location, nativeType,
1944 gl::VariableComponentCount(uniform.type));
1945 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001946}
1947
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948void Program::flagForDeletion()
1949{
1950 mDeleteStatus = true;
1951}
1952
1953bool Program::isFlaggedForDeletion() const
1954{
1955 return mDeleteStatus;
1956}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001957
Brandon Jones43a53e22014-08-28 16:23:22 -07001958void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001959{
1960 mInfoLog.reset();
1961
Geoff Lang7dd2e102014-11-10 15:19:26 -05001962 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001963 {
Geoff Lang92019432017-11-20 13:09:34 -05001964 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001965 }
1966 else
1967 {
Jamie Madillf6113162015-05-07 11:49:21 -04001968 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001969 }
1970}
1971
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1973{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001974 // Skip cache if we're using an infolog, so we get the full error.
1975 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1976 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1977 {
1978 return mCachedValidateSamplersResult.value();
1979 }
1980
1981 if (mTextureUnitTypesCache.empty())
1982 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001983 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001984 }
1985 else
1986 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001987 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(),
1988 TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001989 }
1990
1991 // if any two active samplers in a program are of different types, but refer to the same
1992 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1993 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001994 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001995 {
Jamie Madill54164b02017-08-28 15:17:37 -04001996 if (samplerBinding.unreferenced)
1997 continue;
1998
Corentin Wallezf0e89be2017-11-08 14:00:32 -08001999 TextureType textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002000
Jamie Madille7d84322017-01-10 18:21:59 -05002001 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002002 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002003 if (textureUnit >= caps.maxCombinedTextureImageUnits)
2004 {
2005 if (infoLog)
2006 {
2007 (*infoLog) << "Sampler uniform (" << textureUnit
2008 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
2009 << caps.maxCombinedTextureImageUnits << ")";
2010 }
2011
2012 mCachedValidateSamplersResult = false;
2013 return false;
2014 }
2015
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002016 if (mTextureUnitTypesCache[textureUnit] != TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002017 {
2018 if (textureType != mTextureUnitTypesCache[textureUnit])
2019 {
2020 if (infoLog)
2021 {
2022 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2023 "image unit ("
2024 << textureUnit << ").";
2025 }
2026
2027 mCachedValidateSamplersResult = false;
2028 return false;
2029 }
2030 }
2031 else
2032 {
2033 mTextureUnitTypesCache[textureUnit] = textureType;
2034 }
2035 }
2036 }
2037
2038 mCachedValidateSamplersResult = true;
2039 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040}
2041
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002042bool Program::isValidated() const
2043{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044 return mValidated;
2045}
2046
Geoff Lange1a27752015-10-05 13:16:04 -04002047GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002049 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050}
2051
jchen1058f67be2017-10-27 08:59:27 +08002052GLuint Program::getActiveAtomicCounterBufferCount() const
2053{
2054 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2055}
2056
Jiajia Qin729b2c62017-08-14 09:36:11 +08002057GLuint Program::getActiveShaderStorageBlockCount() const
2058{
2059 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2060}
2061
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002062void Program::getActiveUniformBlockName(const GLuint blockIndex,
2063 GLsizei bufSize,
2064 GLsizei *length,
2065 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002067 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2068}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002070void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2071 GLsizei bufSize,
2072 GLsizei *length,
2073 GLchar *blockName) const
2074{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002076 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002077}
2078
Qin Jiajia9bf55522018-01-29 13:56:23 +08002079template <typename T>
2080GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002081{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082 int maxLength = 0;
2083
2084 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002085 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002086 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002088 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002090 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002091 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 }
2093 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002094 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095
2096 return maxLength;
2097}
2098
Qin Jiajia9bf55522018-01-29 13:56:23 +08002099GLint Program::getActiveUniformBlockMaxNameLength() const
2100{
2101 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2102}
2103
2104GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2105{
2106 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2107}
2108
Geoff Lange1a27752015-10-05 13:16:04 -04002109GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002111 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2112}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002113
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002114GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2115{
2116 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002117}
2118
Jiajia Qin729b2c62017-08-14 09:36:11 +08002119const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002120{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002121 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2122 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002123}
2124
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002125const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2126{
2127 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2128 return mState.mShaderStorageBlocks[index];
2129}
2130
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002131void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2132{
jchen107a20b972017-06-13 14:25:26 +08002133 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002134 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04002135 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002136}
2137
2138GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2139{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002140 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002141}
2142
Jiajia Qin729b2c62017-08-14 09:36:11 +08002143GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2144{
2145 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2146}
2147
Geoff Lang48dcae72014-02-05 16:28:24 -05002148void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
2149{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002150 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002151 for (GLsizei i = 0; i < count; i++)
2152 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002153 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002154 }
2155
Jamie Madill48ef11b2016-04-27 15:21:52 -04002156 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002157}
2158
2159void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
2160{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002162 {
jchen10a9042d32017-03-17 08:50:45 +08002163 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2164 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2165 std::string varName = var.nameWithArrayIndex();
2166 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002167 if (length)
2168 {
2169 *length = lastNameIdx;
2170 }
2171 if (size)
2172 {
jchen10a9042d32017-03-17 08:50:45 +08002173 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002174 }
2175 if (type)
2176 {
jchen10a9042d32017-03-17 08:50:45 +08002177 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002178 }
2179 if (name)
2180 {
jchen10a9042d32017-03-17 08:50:45 +08002181 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002182 name[lastNameIdx] = '\0';
2183 }
2184 }
2185}
2186
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002187GLsizei Program::getTransformFeedbackVaryingCount() const
2188{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002190 {
jchen10a9042d32017-03-17 08:50:45 +08002191 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002192 }
2193 else
2194 {
2195 return 0;
2196 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002197}
2198
2199GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2200{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002201 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002202 {
2203 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002204 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002205 {
jchen10a9042d32017-03-17 08:50:45 +08002206 maxSize =
2207 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002208 }
2209
2210 return maxSize;
2211 }
2212 else
2213 {
2214 return 0;
2215 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002216}
2217
2218GLenum Program::getTransformFeedbackBufferMode() const
2219{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002220 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221}
2222
Jiawei Shao73618602017-12-20 15:47:15 +08002223bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
2224{
2225 Shader *vertexShader = mState.mAttachedVertexShader;
2226 Shader *fragmentShader = mState.mAttachedFragmentShader;
2227 Shader *computeShader = mState.mAttachedComputeShader;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002228 Shader *geometryShader = mState.mAttachedGeometryShader;
Jiawei Shao73618602017-12-20 15:47:15 +08002229
2230 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002231 bool isGraphicsShaderAttached =
2232 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002233 // Check whether we both have a compute and non-compute shaders attached.
2234 // If there are of both types attached, then linking should fail.
2235 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2236 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2237 {
2238 infoLog << "Both compute and graphics shaders are attached to the same program.";
2239 return false;
2240 }
2241
2242 if (computeShader)
2243 {
2244 if (!computeShader->isCompiled(context))
2245 {
2246 infoLog << "Attached compute shader is not compiled.";
2247 return false;
2248 }
2249 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
2250
2251 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
2252
2253 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2254 // If the work group size is not specified, a link time error should occur.
2255 if (!mState.mComputeShaderLocalSize.isDeclared())
2256 {
2257 infoLog << "Work group size is not specified.";
2258 return false;
2259 }
2260 }
2261 else
2262 {
2263 if (!fragmentShader || !fragmentShader->isCompiled(context))
2264 {
2265 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2266 return false;
2267 }
2268 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
2269
2270 if (!vertexShader || !vertexShader->isCompiled(context))
2271 {
2272 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2273 return false;
2274 }
2275 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
2276
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002277 int vertexShaderVersion = vertexShader->getShaderVersion(context);
2278 if (fragmentShader->getShaderVersion(context) != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002279 {
2280 infoLog << "Fragment shader version does not match vertex shader version.";
2281 return false;
2282 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002283
2284 if (geometryShader)
2285 {
2286 // [GL_EXT_geometry_shader] Chapter 7
2287 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2288 // Language Specification, as well as any of the following reasons:
2289 // * One or more of the shader objects attached to <program> are not compiled
2290 // successfully.
2291 // * The shaders do not use the same shader language version.
2292 // * <program> contains objects to form a geometry shader, and
2293 // - <program> is not separable and contains no objects to form a vertex shader; or
2294 // - the input primitive type, output primitive type, or maximum output vertex count
2295 // is not specified in the compiled geometry shader object.
2296 if (!geometryShader->isCompiled(context))
2297 {
2298 infoLog << "The attached geometry shader isn't compiled.";
2299 return false;
2300 }
2301
2302 if (geometryShader->getShaderVersion(context) != vertexShaderVersion)
2303 {
2304 mInfoLog << "Geometry shader version does not match vertex shader version.";
2305 return false;
2306 }
2307 ASSERT(geometryShader->getType() == GL_GEOMETRY_SHADER_EXT);
2308
2309 Optional<GLenum> inputPrimitive =
2310 geometryShader->getGeometryShaderInputPrimitiveType(context);
2311 if (!inputPrimitive.valid())
2312 {
2313 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2314 return false;
2315 }
2316
2317 Optional<GLenum> outputPrimitive =
2318 geometryShader->getGeometryShaderOutputPrimitiveType(context);
2319 if (!outputPrimitive.valid())
2320 {
2321 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2322 return false;
2323 }
2324
2325 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices(context);
2326 if (!maxVertices.valid())
2327 {
2328 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2329 return false;
2330 }
2331
2332 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2333 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2334 mState.mGeometryShaderMaxVertices = maxVertices.value();
2335 mState.mGeometryShaderInvocations =
2336 geometryShader->getGeometryShaderInvocations(context);
2337 }
Jiawei Shao73618602017-12-20 15:47:15 +08002338 }
2339
2340 return true;
2341}
2342
jchen10910a3da2017-11-15 09:40:11 +08002343GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2344{
2345 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2346 {
2347 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2348 if (tf.nameWithArrayIndex() == name)
2349 {
2350 return tfIndex;
2351 }
2352 }
2353 return GL_INVALID_INDEX;
2354}
2355
2356const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2357{
2358 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2359 return mState.mLinkedTransformFeedbackVaryings[index];
2360}
2361
Jamie Madillbd044ed2017-06-05 12:59:21 -04002362bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002364 std::vector<Shader *> activeShaders;
2365 activeShaders.push_back(mState.mAttachedVertexShader);
2366 if (mState.mAttachedGeometryShader)
2367 {
2368 activeShaders.push_back(mState.mAttachedGeometryShader);
2369 }
2370 activeShaders.push_back(mState.mAttachedFragmentShader);
Jamie Madill192745a2016-12-22 15:58:21 -05002371
Jiawei Shaod063aff2018-02-22 10:19:09 +08002372 const size_t activeShaderCount = activeShaders.size();
2373 for (size_t shaderIndex = 0; shaderIndex < activeShaderCount - 1; ++shaderIndex)
2374 {
2375 if (!linkValidateShaderInterfaceMatching(context, activeShaders[shaderIndex],
2376 activeShaders[shaderIndex + 1], infoLog))
2377 {
2378 return false;
2379 }
2380 }
2381
2382 if (!linkValidateBuiltInVaryings(context, infoLog))
2383 {
2384 return false;
2385 }
2386
2387 if (!linkValidateFragmentInputBindings(context, infoLog))
2388 {
2389 return false;
2390 }
2391
2392 return true;
2393}
2394
2395// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2396// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
2397bool Program::linkValidateShaderInterfaceMatching(const Context *context,
2398 gl::Shader *generatingShader,
2399 gl::Shader *consumingShader,
2400 gl::InfoLog &infoLog) const
2401{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002402 ASSERT(generatingShader->getShaderVersion(context) ==
2403 consumingShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002404
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002405 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context);
2406 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002407
Jiawei Shaod063aff2018-02-22 10:19:09 +08002408 bool validateGeometryShaderInputs = consumingShader->getType() == GL_GEOMETRY_SHADER_EXT;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002409
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002410 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002412 bool matched = false;
2413
2414 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002415 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002416 {
2417 continue;
2418 }
2419
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002420 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002422 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002423 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002424 ASSERT(!output.isBuiltIn());
2425
2426 std::string mismatchedStructFieldName;
2427 LinkMismatchError linkError =
2428 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002429 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002430 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002431 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002432 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2433 mismatchedStructFieldName, generatingShader->getType(),
2434 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002435 return false;
2436 }
2437
Geoff Lang7dd2e102014-11-10 15:19:26 -05002438 matched = true;
2439 break;
2440 }
2441 }
2442
2443 // We permit unmatched, unreferenced varyings
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002444 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002445 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002446 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2447 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2448 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002449 return false;
2450 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002451 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002452
Jiawei Shaod063aff2018-02-22 10:19:09 +08002453 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002454
Jiawei Shaod063aff2018-02-22 10:19:09 +08002455 return true;
2456}
2457
2458bool Program::linkValidateFragmentInputBindings(const Context *context, gl::InfoLog &infoLog) const
2459{
2460 ASSERT(mState.mAttachedFragmentShader);
2461
2462 std::map<GLuint, std::string> staticFragmentInputLocations;
2463
2464 const std::vector<sh::Varying> &fragmentInputVaryings =
2465 mState.mAttachedFragmentShader->getInputVaryings(context);
2466 for (const sh::Varying &input : fragmentInputVaryings)
2467 {
2468 if (input.isBuiltIn() || !input.staticUse)
2469 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002470 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002471 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002472
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002473 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002474 if (inputBinding == -1)
2475 continue;
2476
2477 const auto it = staticFragmentInputLocations.find(inputBinding);
2478 if (it == std::end(staticFragmentInputLocations))
2479 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002480 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002481 }
2482 else
2483 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002484 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002485 << it->second;
2486 return false;
2487 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002488 }
2489
2490 return true;
2491}
2492
Jamie Madillbd044ed2017-06-05 12:59:21 -04002493bool Program::linkUniforms(const Context *context,
2494 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002495 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002496{
Olli Etuahob78707c2017-03-09 15:03:11 +00002497 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002498 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002499 {
2500 return false;
2501 }
2502
Olli Etuahob78707c2017-03-09 15:03:11 +00002503 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002504
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002505 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002506
jchen10eaef1e52017-06-13 10:44:11 +08002507 if (!linkAtomicCounterBuffers())
2508 {
2509 return false;
2510 }
2511
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002512 return true;
2513}
2514
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002515void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002516{
Jamie Madill982f6e02017-06-07 14:33:04 -04002517 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2518 unsigned int low = high;
2519
jchen10eaef1e52017-06-13 10:44:11 +08002520 for (auto counterIter = mState.mUniforms.rbegin();
2521 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2522 {
2523 --low;
2524 }
2525
2526 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2527
2528 high = low;
2529
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002530 for (auto imageIter = mState.mUniforms.rbegin();
2531 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2532 {
2533 --low;
2534 }
2535
2536 mState.mImageUniformRange = RangeUI(low, high);
2537
2538 // If uniform is a image type, insert it into the mImageBindings array.
2539 for (unsigned int imageIndex : mState.mImageUniformRange)
2540 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002541 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2542 // cannot load values into a uniform defined as an image. if declare without a
2543 // binding qualifier, any uniform image variable (include all elements of
2544 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002545 auto &imageUniform = mState.mUniforms[imageIndex];
2546 if (imageUniform.binding == -1)
2547 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002548 mState.mImageBindings.emplace_back(
2549 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002550 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002551 else
2552 {
2553 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002554 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002555 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002556 }
2557
2558 high = low;
2559
2560 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002561 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002562 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002563 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002564 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002565
2566 mState.mSamplerUniformRange = RangeUI(low, high);
2567
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002568 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002569 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002570 {
2571 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002572 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002573 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002574 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002575 }
2576}
2577
jchen10eaef1e52017-06-13 10:44:11 +08002578bool Program::linkAtomicCounterBuffers()
2579{
2580 for (unsigned int index : mState.mAtomicCounterUniformRange)
2581 {
2582 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002583 uniform.blockInfo.offset = uniform.offset;
2584 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2585 uniform.blockInfo.matrixStride = 0;
2586 uniform.blockInfo.isRowMajorMatrix = false;
2587
jchen10eaef1e52017-06-13 10:44:11 +08002588 bool found = false;
2589 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2590 ++bufferIndex)
2591 {
2592 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2593 if (buffer.binding == uniform.binding)
2594 {
2595 buffer.memberIndexes.push_back(index);
2596 uniform.bufferIndex = bufferIndex;
2597 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002598 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002599 break;
2600 }
2601 }
2602 if (!found)
2603 {
2604 AtomicCounterBuffer atomicCounterBuffer;
2605 atomicCounterBuffer.binding = uniform.binding;
2606 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002607 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002608 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2609 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2610 }
2611 }
2612 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002613 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002614
2615 return true;
2616}
2617
Jamie Madilleb979bf2016-11-15 12:28:46 -05002618// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002619bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002620{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002621 const ContextState &data = context->getContextState();
2622 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002623
Geoff Lang7dd2e102014-11-10 15:19:26 -05002624 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002625 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002626 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002627
2628 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002629 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002630 {
Jamie Madillf6113162015-05-07 11:49:21 -04002631 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002632 return false;
2633 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002634
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002635 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002636
Jamie Madillc349ec02015-08-21 16:53:12 -04002637 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002638 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002639 {
Olli Etuahod2551232017-10-26 20:03:33 +03002640 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2641 // structures, so we don't need to worry about adjusting their names or generating entries
2642 // for each member/element (unlike uniforms for example).
2643 ASSERT(!attribute.isArray() && !attribute.isStruct());
2644
Jamie Madilleb979bf2016-11-15 12:28:46 -05002645 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002646 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002647 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002648 attribute.location = bindingLocation;
2649 }
2650
2651 if (attribute.location != -1)
2652 {
2653 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002654 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002655
Jamie Madill63805b42015-08-25 13:17:39 -04002656 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002657 {
Jamie Madillf6113162015-05-07 11:49:21 -04002658 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002659 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002660
2661 return false;
2662 }
2663
Jamie Madill63805b42015-08-25 13:17:39 -04002664 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002665 {
Jamie Madill63805b42015-08-25 13:17:39 -04002666 const int regLocation = attribute.location + reg;
2667 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002668
2669 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002670 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002671 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002672 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002673 // TODO(jmadill): fix aliasing on ES2
2674 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002675 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002676 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002677 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002678 return false;
2679 }
2680 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002681 else
2682 {
Jamie Madill63805b42015-08-25 13:17:39 -04002683 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002684 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002685
Jamie Madill63805b42015-08-25 13:17:39 -04002686 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002687 }
2688 }
2689 }
2690
2691 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002692 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002693 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002694 // Not set by glBindAttribLocation or by location layout qualifier
2695 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002696 {
Jamie Madill63805b42015-08-25 13:17:39 -04002697 int regs = VariableRegisterCount(attribute.type);
2698 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002699
Jamie Madill63805b42015-08-25 13:17:39 -04002700 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002701 {
Jamie Madillf6113162015-05-07 11:49:21 -04002702 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002703 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002704 }
2705
Jamie Madillc349ec02015-08-21 16:53:12 -04002706 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002707 }
2708 }
2709
Brandon Jonesc405ae72017-12-06 14:15:03 -08002710 ASSERT(mState.mAttributesTypeMask.none());
2711 ASSERT(mState.mAttributesMask.none());
2712
Jamie Madill48ef11b2016-04-27 15:21:52 -04002713 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002714 {
Jamie Madill63805b42015-08-25 13:17:39 -04002715 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002716 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002717
Jamie Madillbd159f02017-10-09 19:39:06 -04002718 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002719 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002720 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2721 mState.mActiveAttribLocationsMask.set(location);
2722 mState.mMaxActiveAttribLocation =
2723 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08002724
2725 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
2726 if (!attribute.isBuiltIn())
2727 {
2728 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
2729 location);
2730 mState.mAttributesMask.set(location);
2731 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002732 }
2733 }
2734
Geoff Lang7dd2e102014-11-10 15:19:26 -05002735 return true;
2736}
2737
Jiajia Qin729b2c62017-08-14 09:36:11 +08002738bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002739{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002740 const auto &caps = context->getCaps();
2741
Martin Radev4c4c8e72016-08-04 12:25:34 +03002742 if (mState.mAttachedComputeShader)
2743 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002744 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002745 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002746
Jiawei Shao427071e2018-03-19 09:21:37 +08002747 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002748 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002749 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2750 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002751 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002752 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002753 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002754
2755 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
Jiawei Shao427071e2018-03-19 09:21:37 +08002756 if (!ValidateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002757 computeShaderStorageBlocks,
2758 "Compute shader shader storage block count exceeds "
2759 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2760 infoLog))
2761 {
2762 return false;
2763 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002764 return true;
2765 }
2766
Jamie Madillbd044ed2017-06-05 12:59:21 -04002767 Shader &vertexShader = *mState.mAttachedVertexShader;
2768 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002769
Jiajia Qin729b2c62017-08-14 09:36:11 +08002770 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2771 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002772
Jiawei Shao427071e2018-03-19 09:21:37 +08002773 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002774 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002775 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2776 {
2777 return false;
2778 }
Jiawei Shao427071e2018-03-19 09:21:37 +08002779 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002780 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002781 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2782 infoLog))
2783 {
2784
2785 return false;
2786 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002787
Jiawei Shao427071e2018-03-19 09:21:37 +08002788 Shader *geometryShader = mState.mAttachedGeometryShader;
2789 if (geometryShader)
2790 {
2791 const auto &geometryUniformBlocks = geometryShader->getUniformBlocks(context);
2792 if (!ValidateInterfaceBlocksCount(
2793 caps.maxGeometryUniformBlocks, geometryUniformBlocks,
2794 "Geometry shader uniform block count exceeds GL_MAX_GEOMETRY_UNIFORM_BLOCKS_EXT (",
2795 infoLog))
2796 {
2797 return false;
2798 }
2799 }
2800
2801 // TODO(jiawei.shao@intel.com): validate geometry shader uniform blocks.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002802 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao73618602017-12-20 15:47:15 +08002803 if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog,
Jiajia Qin8efd1262017-12-19 09:32:55 +08002804 webglCompatibility, sh::BlockType::BLOCK_UNIFORM,
2805 caps.maxCombinedUniformBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002806 {
2807 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002808 }
Jamie Madille473dee2015-08-18 14:49:01 -04002809
Jiajia Qin729b2c62017-08-14 09:36:11 +08002810 if (context->getClientVersion() >= Version(3, 1))
2811 {
2812 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2813 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2814
Jiawei Shao427071e2018-03-19 09:21:37 +08002815 if (!ValidateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002816 vertexShaderStorageBlocks,
2817 "Vertex shader shader storage block count exceeds "
2818 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2819 infoLog))
2820 {
2821 return false;
2822 }
Jiawei Shao427071e2018-03-19 09:21:37 +08002823 if (!ValidateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002824 fragmentShaderStorageBlocks,
2825 "Fragment shader shader storage block count exceeds "
2826 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2827 infoLog))
2828 {
2829
2830 return false;
2831 }
2832
Jiawei Shao427071e2018-03-19 09:21:37 +08002833 if (geometryShader)
2834 {
2835 const auto &geometryShaderStorageBlocks =
2836 geometryShader->getShaderStorageBlocks(context);
2837 if (!ValidateInterfaceBlocksCount(caps.maxGeometryShaderStorageBlocks,
2838 geometryShaderStorageBlocks,
2839 "Geometry shader shader storage block count exceeds "
2840 "GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS_EXT (",
2841 infoLog))
2842 {
2843 return false;
2844 }
2845 }
2846
2847 // TODO(jiawei.shao@intel.com): validate geometry shader shader storage blocks.
Jiajia Qin8efd1262017-12-19 09:32:55 +08002848 if (!ValidateGraphicsInterfaceBlocks(
2849 vertexShaderStorageBlocks, fragmentShaderStorageBlocks, infoLog, webglCompatibility,
2850 sh::BlockType::BLOCK_BUFFER, caps.maxCombinedShaderStorageBlocks))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002851 {
2852 return false;
2853 }
2854 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002855 return true;
2856}
2857
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002858LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
2859 const sh::ShaderVariable &variable2,
2860 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002861 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002862 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002863{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002864 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002865 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002866 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002867 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002868 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002869 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002870 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002871 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002872 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002873 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002874 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002875 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002876 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04002877 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002878 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04002879 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002880
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002881 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002882 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002883 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002884 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002885 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002886 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2887 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002888 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
2889 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002890
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002891 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002892 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002893 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002894 }
2895
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002896 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08002897 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002898 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002899 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002900 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
2901 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002902 }
2903 }
2904
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002905 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002906}
2907
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002908LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
2909 const sh::Varying &inputVarying,
2910 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002911 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002912 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002913{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002914 if (validateGeometryShaderInputVarying)
2915 {
2916 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
2917 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
2918 // or outputs.
2919 ASSERT(inputVarying.arraySizes.size() == 1u);
2920
2921 // Geometry shader input varyings are not treated as arrays, so a vertex array output
2922 // varying cannot match a geometry shader input varying.
2923 // [GL_EXT_geometry_shader] Section 7.4.1:
2924 // Geometry shader per-vertex input variables and blocks are required to be declared as
2925 // arrays, with each element representing input or output values for a single vertex of a
2926 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
2927 // are treated as though they were not declared as arrays.
2928 if (outputVarying.isArray())
2929 {
2930 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
2931 }
2932 }
2933
2934 // Skip the validation on the array sizes between a vertex output varying and a geometry input
2935 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002936 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08002937 LinkValidateVariablesBase(outputVarying, inputVarying, false,
2938 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002939 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002940 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002941 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002942 }
2943
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002944 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002945 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002946 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002947 }
2948
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002949 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002950 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002951 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002952 }
2953
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002954 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002955}
2956
Jamie Madillbd044ed2017-06-05 12:59:21 -04002957bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002958{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002959 Shader *vertexShader = mState.mAttachedVertexShader;
2960 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002961 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2962 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002963 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002964
2965 if (shaderVersion != 100)
2966 {
2967 // Only ESSL 1.0 has restrictions on matching input and output invariance
2968 return true;
2969 }
2970
2971 bool glPositionIsInvariant = false;
2972 bool glPointSizeIsInvariant = false;
2973 bool glFragCoordIsInvariant = false;
2974 bool glPointCoordIsInvariant = false;
2975
2976 for (const sh::Varying &varying : vertexVaryings)
2977 {
2978 if (!varying.isBuiltIn())
2979 {
2980 continue;
2981 }
2982 if (varying.name.compare("gl_Position") == 0)
2983 {
2984 glPositionIsInvariant = varying.isInvariant;
2985 }
2986 else if (varying.name.compare("gl_PointSize") == 0)
2987 {
2988 glPointSizeIsInvariant = varying.isInvariant;
2989 }
2990 }
2991
2992 for (const sh::Varying &varying : fragmentVaryings)
2993 {
2994 if (!varying.isBuiltIn())
2995 {
2996 continue;
2997 }
2998 if (varying.name.compare("gl_FragCoord") == 0)
2999 {
3000 glFragCoordIsInvariant = varying.isInvariant;
3001 }
3002 else if (varying.name.compare("gl_PointCoord") == 0)
3003 {
3004 glPointCoordIsInvariant = varying.isInvariant;
3005 }
3006 }
3007
3008 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3009 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3010 // Not requiring invariance to match is supported by:
3011 // dEQP, WebGL CTS, Nexus 5X GLES
3012 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3013 {
3014 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3015 "declared invariant.";
3016 return false;
3017 }
3018 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3019 {
3020 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3021 "declared invariant.";
3022 return false;
3023 }
3024
3025 return true;
3026}
3027
jchen10a9042d32017-03-17 08:50:45 +08003028bool Program::linkValidateTransformFeedback(const gl::Context *context,
3029 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003030 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003031 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003032{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003033
jchen108225e732017-11-14 16:29:03 +08003034 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003035 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003036 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003037 {
jchen10a9042d32017-03-17 08:50:45 +08003038 if (context->getClientVersion() < Version(3, 1) &&
3039 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003040 {
Geoff Lang1a683462015-09-29 15:09:59 -04003041 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003042 return false;
3043 }
jchen108225e732017-11-14 16:29:03 +08003044 if (context->getClientVersion() >= Version(3, 1))
3045 {
3046 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3047 {
3048 infoLog << "Two transform feedback varyings include the same array element ("
3049 << tfVaryingName << ").";
3050 return false;
3051 }
3052 }
3053 else
3054 {
3055 if (uniqueNames.count(tfVaryingName) > 0)
3056 {
3057 infoLog << "Two transform feedback varyings specify the same output variable ("
3058 << tfVaryingName << ").";
3059 return false;
3060 }
3061 }
3062 uniqueNames.insert(tfVaryingName);
3063 }
3064
3065 // Validate against program varyings.
3066 size_t totalComponents = 0;
3067 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3068 {
3069 std::vector<unsigned int> subscripts;
3070 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3071
3072 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3073 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003074 {
3075 infoLog << "Transform feedback varying " << tfVaryingName
3076 << " does not exist in the vertex shader.";
3077 return false;
3078 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003079
jchen108225e732017-11-14 16:29:03 +08003080 // Validate the matching variable.
3081 if (var->isStruct())
3082 {
3083 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3084 return false;
3085 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003086
jchen108225e732017-11-14 16:29:03 +08003087 size_t elementCount = 0;
3088 size_t componentCount = 0;
3089
3090 if (var->isArray())
3091 {
3092 if (context->getClientVersion() < Version(3, 1))
3093 {
3094 infoLog << "Capture of arrays is undefined and not supported.";
3095 return false;
3096 }
3097
3098 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3099 ASSERT(!var->isArrayOfArrays());
3100
3101 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3102 {
3103 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3104 return false;
3105 }
3106 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3107 }
3108 else
3109 {
3110 if (!subscripts.empty())
3111 {
3112 infoLog << "Varying '" << baseName
3113 << "' is not an array to be captured by element.";
3114 return false;
3115 }
3116 elementCount = 1;
3117 }
3118
3119 // TODO(jmadill): Investigate implementation limits on D3D11
3120 componentCount = VariableComponentCount(var->type) * elementCount;
3121 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3122 componentCount > caps.maxTransformFeedbackSeparateComponents)
3123 {
3124 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3125 << componentCount << ") exceed the maximum separate components ("
3126 << caps.maxTransformFeedbackSeparateComponents << ").";
3127 return false;
3128 }
3129
3130 totalComponents += componentCount;
3131 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3132 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3133 {
3134 infoLog << "Transform feedback varying total components (" << totalComponents
3135 << ") exceed the maximum interleaved components ("
3136 << caps.maxTransformFeedbackInterleavedComponents << ").";
3137 return false;
3138 }
3139 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003140 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003141}
3142
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003143bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
3144{
3145 const std::vector<sh::Uniform> &vertexUniforms =
3146 mState.mAttachedVertexShader->getUniforms(context);
3147 const std::vector<sh::Uniform> &fragmentUniforms =
3148 mState.mAttachedFragmentShader->getUniforms(context);
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003149 const std::vector<sh::Uniform> *geometryUniforms =
3150 (mState.mAttachedGeometryShader) ? &mState.mAttachedGeometryShader->getUniforms(context)
3151 : nullptr;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003152 const std::vector<sh::Attribute> &attributes =
3153 mState.mAttachedVertexShader->getActiveAttributes(context);
3154 for (const auto &attrib : attributes)
3155 {
3156 for (const auto &uniform : vertexUniforms)
3157 {
3158 if (uniform.name == attrib.name)
3159 {
3160 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3161 return false;
3162 }
3163 }
3164 for (const auto &uniform : fragmentUniforms)
3165 {
3166 if (uniform.name == attrib.name)
3167 {
3168 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3169 return false;
3170 }
3171 }
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003172 if (geometryUniforms)
3173 {
3174 for (const auto &uniform : *geometryUniforms)
3175 {
3176 if (uniform.name == attrib.name)
3177 {
3178 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3179 return false;
3180 }
3181 }
3182 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003183 }
3184 return true;
3185}
3186
Jamie Madill3c1da042017-11-27 18:33:40 -05003187void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003188{
3189 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003190 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003191 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003192 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003193 std::vector<unsigned int> subscripts;
3194 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003195 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003196 if (!subscripts.empty())
3197 {
3198 subscript = subscripts.back();
3199 }
Jamie Madill192745a2016-12-22 15:58:21 -05003200 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003201 {
Jamie Madill192745a2016-12-22 15:58:21 -05003202 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003203 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003204 {
jchen10a9042d32017-03-17 08:50:45 +08003205 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3206 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003207 break;
3208 }
jchen108225e732017-11-14 16:29:03 +08003209 else if (varying->isStruct())
3210 {
3211 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3212 if (field != nullptr)
3213 {
3214 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3215 break;
3216 }
3217 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003218 }
3219 }
3220}
3221
Jamie Madill3c1da042017-11-27 18:33:40 -05003222ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003223{
Jamie Madill3c1da042017-11-27 18:33:40 -05003224 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003225
Jiawei Shao3d404882017-10-16 13:30:48 +08003226 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003227 {
Jamie Madill192745a2016-12-22 15:58:21 -05003228 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003229 }
3230
Jiawei Shao3d404882017-10-16 13:30:48 +08003231 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003232 {
Jamie Madill192745a2016-12-22 15:58:21 -05003233 merged[varying.name].fragment = &varying;
3234 }
3235
3236 return merged;
3237}
3238
Jamie Madillbd044ed2017-06-05 12:59:21 -04003239void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003240{
Jamie Madillbd044ed2017-06-05 12:59:21 -04003241 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003242 ASSERT(fragmentShader != nullptr);
3243
Geoff Lange0cff192017-05-30 13:04:56 -04003244 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003245 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003246 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003247
3248 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04003249 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04003250 {
3251 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3252 outputVariable.name != "gl_FragData")
3253 {
3254 continue;
3255 }
3256
3257 unsigned int baseLocation =
3258 (outputVariable.location == -1 ? 0u
3259 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003260
3261 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3262 // structures, so we may use getBasicTypeElementCount().
3263 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3264 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003265 {
3266 const unsigned int location = baseLocation + elementIndex;
3267 if (location >= mState.mOutputVariableTypes.size())
3268 {
3269 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3270 }
Corentin Walleze7557742017-06-01 13:09:57 -04003271 ASSERT(location < mState.mActiveOutputVariables.size());
3272 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003273 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003274 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003275 }
3276 }
3277
Jamie Madill80a6fc02015-08-21 16:53:16 -04003278 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04003279 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003280 return;
3281
Jamie Madillbd044ed2017-06-05 12:59:21 -04003282 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04003283 // TODO(jmadill): any caps validation here?
3284
jchen1015015f72017-03-16 13:54:21 +08003285 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003286 outputVariableIndex++)
3287 {
jchen1015015f72017-03-16 13:54:21 +08003288 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003289
Olli Etuahod2551232017-10-26 20:03:33 +03003290 if (outputVariable.isArray())
3291 {
3292 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3293 // Resources and including [0] at the end of array variable names.
3294 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3295 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3296 }
3297
Jamie Madill80a6fc02015-08-21 16:53:16 -04003298 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3299 if (outputVariable.isBuiltIn())
3300 continue;
3301
3302 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003303 unsigned int baseLocation =
3304 (outputVariable.location == -1 ? 0u
3305 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003306
Olli Etuaho465835d2017-09-26 13:34:10 +03003307 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3308 // structures, so we may use getBasicTypeElementCount().
3309 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3310 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003311 {
Olli Etuahod2551232017-10-26 20:03:33 +03003312 const unsigned int location = baseLocation + elementIndex;
3313 if (location >= mState.mOutputLocations.size())
3314 {
3315 mState.mOutputLocations.resize(location + 1);
3316 }
3317 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003318 if (outputVariable.isArray())
3319 {
3320 mState.mOutputLocations[location] =
3321 VariableLocation(elementIndex, outputVariableIndex);
3322 }
3323 else
3324 {
3325 VariableLocation locationInfo;
3326 locationInfo.index = outputVariableIndex;
3327 mState.mOutputLocations[location] = locationInfo;
3328 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003329 }
3330 }
3331}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003332
Olli Etuaho48fed632017-03-16 12:05:30 +00003333void Program::setUniformValuesFromBindingQualifiers()
3334{
Jamie Madill982f6e02017-06-07 14:33:04 -04003335 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003336 {
3337 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3338 if (samplerUniform.binding != -1)
3339 {
Olli Etuahod2551232017-10-26 20:03:33 +03003340 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003341 ASSERT(location != -1);
3342 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003343 for (unsigned int elementIndex = 0;
3344 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003345 {
3346 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3347 }
3348 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3349 boundTextureUnits.data());
3350 }
3351 }
3352}
3353
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003354void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003355{
jchen10af713a22017-04-19 09:10:56 +08003356 // Set initial bindings from shader.
3357 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3358 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003359 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003360 bindUniformBlock(blockIndex, uniformBlock.binding);
3361 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003362}
3363
Jamie Madille7d84322017-01-10 18:21:59 -05003364void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003365 GLsizei clampedCount,
3366 const GLint *v)
3367{
Jamie Madill81c2e252017-09-09 23:32:46 -04003368 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3369 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3370 std::vector<GLuint> *boundTextureUnits =
3371 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003372
Olli Etuaho1734e172017-10-27 15:30:27 +03003373 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003374
3375 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003376 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003377}
3378
3379template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003380GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3381 GLsizei count,
3382 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003383 const T *v)
3384{
Jamie Madill134f93d2017-08-31 17:11:00 -04003385 if (count == 1)
3386 return 1;
3387
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003388 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003389
Corentin Wallez15ac5342016-11-03 17:06:39 -04003390 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3391 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003392 unsigned int remainingElements =
3393 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003394 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003395 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003396
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003397 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003398 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003399 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003400 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003401
3402 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003403}
3404
3405template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003406GLsizei Program::clampMatrixUniformCount(GLint location,
3407 GLsizei count,
3408 GLboolean transpose,
3409 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003410{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003411 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3412
Jamie Madill62d31cb2015-09-11 13:25:51 -04003413 if (!transpose)
3414 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003415 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003416 }
3417
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003418 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003419
3420 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3421 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003422 unsigned int remainingElements =
3423 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003424 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003425}
3426
Jamie Madill54164b02017-08-28 15:17:37 -04003427// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3428// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003429template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003430void Program::getUniformInternal(const Context *context,
3431 DestT *dataOut,
3432 GLint location,
3433 GLenum nativeType,
3434 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003435{
Jamie Madill54164b02017-08-28 15:17:37 -04003436 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003437 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003438 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003439 {
3440 GLint tempValue[16] = {0};
3441 mProgram->getUniformiv(context, location, tempValue);
3442 UniformStateQueryCastLoop<GLboolean>(
3443 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003444 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003445 }
3446 case GL_INT:
3447 {
3448 GLint tempValue[16] = {0};
3449 mProgram->getUniformiv(context, location, tempValue);
3450 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3451 components);
3452 break;
3453 }
3454 case GL_UNSIGNED_INT:
3455 {
3456 GLuint tempValue[16] = {0};
3457 mProgram->getUniformuiv(context, location, tempValue);
3458 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3459 components);
3460 break;
3461 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003462 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003463 {
3464 GLfloat tempValue[16] = {0};
3465 mProgram->getUniformfv(context, location, tempValue);
3466 UniformStateQueryCastLoop<GLfloat>(
3467 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003468 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003469 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003470 default:
3471 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003472 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003473 }
3474}
Jamie Madilla4595b82017-01-11 17:36:34 -05003475
3476bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3477{
3478 // Must be called after samplers are validated.
3479 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3480
3481 for (const auto &binding : mState.mSamplerBindings)
3482 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003483 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003484 for (const auto &unit : binding.boundTextureUnits)
3485 {
3486 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3487 if (programTextureID == textureID)
3488 {
3489 // TODO(jmadill): Check for appropriate overlap.
3490 return true;
3491 }
3492 }
3493 }
3494
3495 return false;
3496}
3497
Jamie Madilla2c74982016-12-12 11:20:42 -05003498} // namespace gl