blob: ee934e0f16730098db90c067f1fc443be30df3b1 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
Olli Etuahod2551232017-10-26 20:03:33 +030017#include "common/string_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/MemoryProgramCache.h"
Jamie Madill7af0de52017-11-06 17:09:33 -050022#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill437d2662014-12-05 14:23:35 -050023#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040024#include "libANGLE/Uniform.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040025#include "libANGLE/VaryingPacking.h"
26#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040027#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040028#include "libANGLE/queryconversions.h"
29#include "libANGLE/renderer/GLImplFactory.h"
30#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040031#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050032
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033namespace gl
34{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000035
Geoff Lang7dd2e102014-11-10 15:19:26 -050036namespace
37{
38
Jamie Madill62d31cb2015-09-11 13:25:51 -040039// This simplified cast function doesn't need to worry about advanced concepts like
40// depth range values, or casting to bool.
41template <typename DestT, typename SrcT>
42DestT UniformStateQueryCast(SrcT value);
43
44// From-Float-To-Integer Casts
45template <>
46GLint UniformStateQueryCast(GLfloat value)
47{
48 return clampCast<GLint>(roundf(value));
49}
50
51template <>
52GLuint UniformStateQueryCast(GLfloat value)
53{
54 return clampCast<GLuint>(roundf(value));
55}
56
57// From-Integer-to-Integer Casts
58template <>
59GLint UniformStateQueryCast(GLuint value)
60{
61 return clampCast<GLint>(value);
62}
63
64template <>
65GLuint UniformStateQueryCast(GLint value)
66{
67 return clampCast<GLuint>(value);
68}
69
70// From-Boolean-to-Anything Casts
71template <>
72GLfloat UniformStateQueryCast(GLboolean value)
73{
Geoff Lang92019432017-11-20 13:09:34 -050074 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040075}
76
77template <>
78GLint UniformStateQueryCast(GLboolean value)
79{
Geoff Lang92019432017-11-20 13:09:34 -050080 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040081}
82
83template <>
84GLuint UniformStateQueryCast(GLboolean value)
85{
Geoff Lang92019432017-11-20 13:09:34 -050086 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040087}
88
89// Default to static_cast
90template <typename DestT, typename SrcT>
91DestT UniformStateQueryCast(SrcT value)
92{
93 return static_cast<DestT>(value);
94}
95
96template <typename SrcT, typename DestT>
97void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
98{
99 for (int comp = 0; comp < components; ++comp)
100 {
101 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
102 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
103 size_t offset = comp * 4;
104 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
105 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
106 }
107}
108
jchen1015015f72017-03-16 13:54:21 +0800109template <typename VarT>
110GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
111{
Olli Etuahod2551232017-10-26 20:03:33 +0300112 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800113 for (size_t index = 0; index < list.size(); index++)
114 {
115 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300116 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800117 {
Olli Etuahod2551232017-10-26 20:03:33 +0300118 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800119 }
120 }
121
122 return GL_INVALID_INDEX;
123}
124
Olli Etuahod2551232017-10-26 20:03:33 +0300125template <typename VarT>
126GLint GetVariableLocation(const std::vector<VarT> &list,
127 const std::vector<VariableLocation> &locationList,
128 const std::string &name)
129{
130 size_t nameLengthWithoutArrayIndex;
131 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
132
133 for (size_t location = 0u; location < locationList.size(); ++location)
134 {
135 const VariableLocation &variableLocation = locationList[location];
136 if (!variableLocation.used())
137 {
138 continue;
139 }
140
141 const VarT &variable = list[variableLocation.index];
142
143 if (angle::BeginsWith(variable.name, name))
144 {
145 if (name.length() == variable.name.length())
146 {
147 ASSERT(name == variable.name);
148 // GLES 3.1 November 2016 page 87.
149 // The string exactly matches the name of the active variable.
150 return static_cast<GLint>(location);
151 }
152 if (name.length() + 3u == variable.name.length() && variable.isArray())
153 {
154 ASSERT(name + "[0]" == variable.name);
155 // The string identifies the base name of an active array, where the string would
156 // exactly match the name of the variable if the suffix "[0]" were appended to the
157 // string.
158 return static_cast<GLint>(location);
159 }
160 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300161 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300162 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
163 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
164 {
165 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
166 // The string identifies an active element of the array, where the string ends with the
167 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
168 // zeroes, or whitespace) identifying an array element, and the "]" character, the
169 // integer is less than the number of active elements of the array variable, and where
170 // the string would exactly match the enumerated name of the array if the decimal
171 // integer were replaced with zero.
172 return static_cast<GLint>(location);
173 }
174 }
175
176 return -1;
177}
178
jchen10fd7c3b52017-03-21 15:36:03 +0800179void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
180{
181 ASSERT(bufSize > 0);
182 strncpy(buffer, string.c_str(), bufSize);
183 buffer[bufSize - 1] = '\0';
184
185 if (length)
186 {
187 *length = static_cast<GLsizei>(strlen(buffer));
188 }
189}
190
jchen10a9042d32017-03-17 08:50:45 +0800191bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
192{
Olli Etuahoc8538042017-09-27 11:20:15 +0300193 std::vector<unsigned int> subscripts;
194 std::string baseName = ParseResourceName(name, &subscripts);
195 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800196 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300197 std::vector<unsigned int> arrayIndices;
198 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
199 if (baseName == arrayName &&
200 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800201 {
202 return true;
203 }
204 }
205 return false;
206}
207
Jiawei Shao427071e2018-03-19 09:21:37 +0800208bool ValidateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +0800209 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
210 const std::string &errorMessage,
211 InfoLog &infoLog)
212{
213 GLuint blockCount = 0;
214 for (const sh::InterfaceBlock &block : interfaceBlocks)
215 {
Olli Etuaho107c7242018-03-20 15:45:35 +0200216 if (block.active || block.layout != sh::BLOCKLAYOUT_PACKED)
Jiajia Qin729b2c62017-08-14 09:36:11 +0800217 {
218 blockCount += (block.arraySize ? block.arraySize : 1);
219 if (blockCount > maxInterfaceBlocks)
220 {
221 infoLog << errorMessage << maxInterfaceBlocks << ")";
222 return false;
223 }
224 }
225 }
226 return true;
227}
228
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800229GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
230{
231 std::vector<unsigned int> subscripts;
232 std::string baseName = ParseResourceName(name, &subscripts);
233
234 unsigned int numBlocks = static_cast<unsigned int>(list.size());
235 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
236 {
237 const auto &block = list[blockIndex];
238 if (block.name == baseName)
239 {
240 const bool arrayElementZero =
241 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
242 const bool arrayElementMatches =
243 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
244 if (arrayElementMatches || arrayElementZero)
245 {
246 return blockIndex;
247 }
248 }
249 }
250
251 return GL_INVALID_INDEX;
252}
253
254void GetInterfaceBlockName(const GLuint index,
255 const std::vector<InterfaceBlock> &list,
256 GLsizei bufSize,
257 GLsizei *length,
258 GLchar *name)
259{
260 ASSERT(index < list.size());
261
262 const auto &block = list[index];
263
264 if (bufSize > 0)
265 {
266 std::string blockName = block.name;
267
268 if (block.isArray)
269 {
270 blockName += ArrayString(block.arrayElement);
271 }
272 CopyStringToBuffer(name, blockName, bufSize, length);
273 }
274}
275
Jamie Madillc9727f32017-11-07 12:37:07 -0500276void InitUniformBlockLinker(const gl::Context *context,
277 const ProgramState &state,
278 UniformBlockLinker *blockLinker)
279{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800280 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500281 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800282 Shader *shader = state.getAttachedShader(shaderType);
283 if (shader)
284 {
285 blockLinker->addShaderBlocks(shaderType, &shader->getUniformBlocks(context));
286 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500287 }
288}
289
290void InitShaderStorageBlockLinker(const gl::Context *context,
291 const ProgramState &state,
292 ShaderStorageBlockLinker *blockLinker)
293{
Jiawei Shao385b3e02018-03-21 09:43:28 +0800294 for (ShaderType shaderType : AllShaderTypes())
Jamie Madillc9727f32017-11-07 12:37:07 -0500295 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800296 Shader *shader = state.getAttachedShader(shaderType);
297 if (shader != nullptr)
298 {
299 blockLinker->addShaderBlocks(shaderType, &shader->getShaderStorageBlocks(context));
300 }
Jamie Madillc9727f32017-11-07 12:37:07 -0500301 }
302}
303
jchen108225e732017-11-14 16:29:03 +0800304// Find the matching varying or field by name.
305const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
306 const std::string &name)
307{
308 const sh::ShaderVariable *var = nullptr;
309 for (const auto &ref : varyings)
310 {
311 const sh::Varying *varying = ref.second.get();
312 if (varying->name == name)
313 {
314 var = varying;
315 break;
316 }
317 var = FindShaderVarField(*varying, name);
318 if (var != nullptr)
319 {
320 break;
321 }
322 }
323 return var;
324}
325
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800326void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
327{
328 ASSERT(mismatchedFieldName);
329 if (mismatchedFieldName->empty())
330 {
331 *mismatchedFieldName = parentName;
332 }
333 else
334 {
335 std::ostringstream stream;
336 stream << parentName << "." << *mismatchedFieldName;
337 *mismatchedFieldName = stream.str();
338 }
339}
340
341const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
342{
343 switch (linkError)
344 {
345 case LinkMismatchError::TYPE_MISMATCH:
346 return "Type";
347 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
348 return "Array size";
349 case LinkMismatchError::PRECISION_MISMATCH:
350 return "Precision";
351 case LinkMismatchError::STRUCT_NAME_MISMATCH:
352 return "Structure name";
353 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
354 return "Field number";
355 case LinkMismatchError::FIELD_NAME_MISMATCH:
356 return "Field name";
357
358 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
359 return "Interpolation type";
360 case LinkMismatchError::INVARIANCE_MISMATCH:
361 return "Invariance";
362
363 case LinkMismatchError::BINDING_MISMATCH:
364 return "Binding layout qualifier";
365 case LinkMismatchError::LOCATION_MISMATCH:
366 return "Location layout qualifier";
367 case LinkMismatchError::OFFSET_MISMATCH:
368 return "Offset layout qualilfier";
369
370 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
371 return "Layout qualifier";
372 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
373 return "Matrix Packing";
374 default:
375 UNREACHABLE();
376 return "";
377 }
378}
379
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800380LinkMismatchError LinkValidateInterfaceBlockFields(const sh::InterfaceBlockField &blockField1,
381 const sh::InterfaceBlockField &blockField2,
382 bool webglCompatibility,
383 std::string *mismatchedBlockFieldName)
384{
385 if (blockField1.name != blockField2.name)
386 {
387 return LinkMismatchError::FIELD_NAME_MISMATCH;
388 }
389
390 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
391 LinkMismatchError linkError = Program::LinkValidateVariablesBase(
392 blockField1, blockField2, webglCompatibility, true, mismatchedBlockFieldName);
393 if (linkError != LinkMismatchError::NO_MISMATCH)
394 {
395 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
396 return linkError;
397 }
398
399 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
400 {
401 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
402 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
403 }
404
405 return LinkMismatchError::NO_MISMATCH;
406}
407
408LinkMismatchError AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
409 const sh::InterfaceBlock &interfaceBlock2,
410 bool webglCompatibility,
411 std::string *mismatchedBlockFieldName)
412{
413 // validate blocks for the same member types
414 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
415 {
416 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
417 }
418 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
419 {
420 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
421 }
422 if (interfaceBlock1.layout != interfaceBlock2.layout ||
423 interfaceBlock1.binding != interfaceBlock2.binding)
424 {
425 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
426 }
427 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
428 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
429 {
430 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
431 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
432
433 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
434 member1, member2, webglCompatibility, mismatchedBlockFieldName);
435 if (linkError != LinkMismatchError::NO_MISMATCH)
436 {
437 return linkError;
438 }
439 }
440 return LinkMismatchError::NO_MISMATCH;
441}
442
443bool ValidateGraphicsInterfaceBlocks(const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
444 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
445 InfoLog &infoLog,
446 bool webglCompatibility,
447 sh::BlockType blockType,
448 GLuint maxCombinedInterfaceBlocks)
449{
450 // Check that interface blocks defined in the vertex and fragment shaders are identical
451 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
452 InterfaceBlockMap linkedInterfaceBlocks;
453 GLuint blockCount = 0;
454
455 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
456 {
457 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
458 if (IsActiveInterfaceBlock(vertexInterfaceBlock))
459 {
460 blockCount += std::max(vertexInterfaceBlock.arraySize, 1u);
461 }
462 }
463
464 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
465 {
466 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
467 if (entry != linkedInterfaceBlocks.end())
468 {
469 const sh::InterfaceBlock &vertexInterfaceBlock = *(entry->second);
470 std::string mismatchedBlockFieldName;
471 LinkMismatchError linkError =
472 AreMatchingInterfaceBlocks(vertexInterfaceBlock, fragmentInterfaceBlock,
473 webglCompatibility, &mismatchedBlockFieldName);
474 if (linkError != LinkMismatchError::NO_MISMATCH)
475 {
476 LogLinkMismatch(infoLog, fragmentInterfaceBlock.name, "interface block", linkError,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800477 mismatchedBlockFieldName, ShaderType::Vertex, ShaderType::Fragment);
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800478 return false;
479 }
480 }
481
482 // [OpenGL ES 3.1] Chapter 7.6.2 Page 105:
483 // If a uniform block is used by multiple shader stages, each such use counts separately
484 // against this combined limit.
485 // [OpenGL ES 3.1] Chapter 7.8 Page 111:
486 // If a shader storage block in a program is referenced by multiple shaders, each such
487 // reference counts separately against this combined limit.
488 if (IsActiveInterfaceBlock(fragmentInterfaceBlock))
489 {
490 blockCount += std::max(fragmentInterfaceBlock.arraySize, 1u);
491 }
492 }
493
494 if (blockCount > maxCombinedInterfaceBlocks)
495 {
496 switch (blockType)
497 {
498 case sh::BlockType::BLOCK_UNIFORM:
499 infoLog << "The sum of the number of active uniform blocks exceeds "
500 "MAX_COMBINED_UNIFORM_BLOCKS ("
501 << maxCombinedInterfaceBlocks << ").";
502 break;
503 case sh::BlockType::BLOCK_BUFFER:
504 infoLog << "The sum of the number of active shader storage blocks exceeds "
505 "MAX_COMBINED_SHADER_STORAGE_BLOCKS ("
506 << maxCombinedInterfaceBlocks << ").";
507 break;
508 default:
509 UNREACHABLE();
510 }
511 return false;
512 }
513 return true;
514}
515
Jamie Madill62d31cb2015-09-11 13:25:51 -0400516} // anonymous namespace
517
Jamie Madill4a3c2342015-10-08 12:58:45 -0400518const char *const g_fakepath = "C:\\fakepath";
519
Jamie Madill3c1da042017-11-27 18:33:40 -0500520// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400521InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000522{
523}
524
525InfoLog::~InfoLog()
526{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000527}
528
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400529size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000530{
Jamie Madill23176ce2017-07-31 14:14:33 -0400531 if (!mLazyStream)
532 {
533 return 0;
534 }
535
536 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400537 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000538}
539
Geoff Lange1a27752015-10-05 13:16:04 -0400540void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000541{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400542 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000543
544 if (bufSize > 0)
545 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400546 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400547
Jamie Madill23176ce2017-07-31 14:14:33 -0400548 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000549 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400550 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
551 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000552 }
553
554 infoLog[index] = '\0';
555 }
556
557 if (length)
558 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400559 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000560 }
561}
562
563// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300564// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000565// messages, so lets remove all occurrences of this fake file path from the log.
566void InfoLog::appendSanitized(const char *message)
567{
Jamie Madill23176ce2017-07-31 14:14:33 -0400568 ensureInitialized();
569
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000570 std::string msg(message);
571
572 size_t found;
573 do
574 {
575 found = msg.find(g_fakepath);
576 if (found != std::string::npos)
577 {
578 msg.erase(found, strlen(g_fakepath));
579 }
580 }
581 while (found != std::string::npos);
582
Jamie Madill23176ce2017-07-31 14:14:33 -0400583 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000584}
585
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000586void InfoLog::reset()
587{
Jiawei Shao02f15232017-12-27 10:10:28 +0800588 if (mLazyStream)
589 {
590 mLazyStream.reset(nullptr);
591 }
592}
593
594bool InfoLog::empty() const
595{
596 if (!mLazyStream)
597 {
598 return true;
599 }
600
601 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000602}
603
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800604void LogLinkMismatch(InfoLog &infoLog,
605 const std::string &variableName,
606 const char *variableType,
607 LinkMismatchError linkError,
608 const std::string &mismatchedStructOrBlockFieldName,
Jiawei Shao385b3e02018-03-21 09:43:28 +0800609 ShaderType shaderType1,
610 ShaderType shaderType2)
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800611{
612 std::ostringstream stream;
613 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
614 << variableName;
615
616 if (!mismatchedStructOrBlockFieldName.empty())
617 {
618 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
619 }
620
621 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
622 << GetShaderTypeString(shaderType2) << " shaders.";
623
624 infoLog << stream.str();
625}
626
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800627bool IsActiveInterfaceBlock(const sh::InterfaceBlock &interfaceBlock)
628{
629 // Only 'packed' blocks are allowed to be considered inactive.
Olli Etuaho107c7242018-03-20 15:45:35 +0200630 return interfaceBlock.active || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED;
Jiawei Shao1c08cbb2018-03-15 15:11:56 +0800631}
632
Jamie Madill3c1da042017-11-27 18:33:40 -0500633// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300634VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000635{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500636}
637
Olli Etuahoc8538042017-09-27 11:20:15 +0300638VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300639 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500640{
Olli Etuahoc8538042017-09-27 11:20:15 +0300641 ASSERT(arrayIndex != GL_INVALID_INDEX);
642}
643
Jamie Madill3c1da042017-11-27 18:33:40 -0500644// SamplerBindings implementation.
Corentin Wallezf0e89be2017-11-08 14:00:32 -0800645SamplerBinding::SamplerBinding(TextureType textureTypeIn, size_t elementCount, bool unreferenced)
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500646 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
647{
648}
649
650SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
651
652SamplerBinding::~SamplerBinding() = default;
653
Jamie Madill3c1da042017-11-27 18:33:40 -0500654// ProgramBindings implementation.
655ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500656{
657}
658
Jamie Madill3c1da042017-11-27 18:33:40 -0500659ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500660{
661}
662
Jamie Madill3c1da042017-11-27 18:33:40 -0500663void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400664{
665 mBindings[name] = index;
666}
667
Jamie Madill3c1da042017-11-27 18:33:40 -0500668int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400669{
670 auto iter = mBindings.find(name);
671 return (iter != mBindings.end()) ? iter->second : -1;
672}
673
Jamie Madill3c1da042017-11-27 18:33:40 -0500674ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400675{
676 return mBindings.begin();
677}
678
Jamie Madill3c1da042017-11-27 18:33:40 -0500679ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400680{
681 return mBindings.end();
682}
683
Jamie Madill3c1da042017-11-27 18:33:40 -0500684// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500685ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
686{
687}
688ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
689{
690 for (size_t index = 0; index < count; ++index)
691 {
692 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
693 }
694}
695
696ImageBinding::ImageBinding(const ImageBinding &other) = default;
697
698ImageBinding::~ImageBinding() = default;
699
Jamie Madill3c1da042017-11-27 18:33:40 -0500700// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400701ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500702 : mLabel(),
703 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400704 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800706 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500707 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400708 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500709 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800710 mImageUniformRange(0, 0),
711 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300712 mBinaryRetrieveableHint(false),
Jiawei Shao4ed05da2018-02-02 14:26:15 +0800713 mNumViews(-1),
714 // [GL_EXT_geometry_shader] Table 20.22
715 mGeometryShaderInputPrimitiveType(GL_TRIANGLES),
716 mGeometryShaderOutputPrimitiveType(GL_TRIANGLE_STRIP),
717 mGeometryShaderInvocations(1),
718 mGeometryShaderMaxVertices(0)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400719{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400721}
722
Jamie Madill48ef11b2016-04-27 15:21:52 -0400723ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400724{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800725 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
726 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400727}
728
Jamie Madill48ef11b2016-04-27 15:21:52 -0400729const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500730{
731 return mLabel;
732}
733
Jiawei Shao385b3e02018-03-21 09:43:28 +0800734Shader *ProgramState::getAttachedShader(ShaderType shaderType) const
735{
736 switch (shaderType)
737 {
738 case ShaderType::Vertex:
739 return mAttachedVertexShader;
740 case ShaderType::Fragment:
741 return mAttachedFragmentShader;
742 case ShaderType::Compute:
743 return mAttachedComputeShader;
744 case ShaderType::Geometry:
745 return mAttachedGeometryShader;
746 default:
747 UNREACHABLE();
748 return nullptr;
749 }
750}
751
Jamie Madille7d84322017-01-10 18:21:59 -0500752GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400753{
jchen1015015f72017-03-16 13:54:21 +0800754 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400755}
756
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800757GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
758{
759 return GetResourceIndexFromName(mBufferVariables, name);
760}
761
Jamie Madille7d84322017-01-10 18:21:59 -0500762GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
763{
764 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
765 return mUniformLocations[location].index;
766}
767
768Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
769{
770 GLuint index = getUniformIndexFromLocation(location);
771 if (!isSamplerUniformIndex(index))
772 {
773 return Optional<GLuint>::Invalid();
774 }
775
776 return getSamplerIndexFromUniformIndex(index);
777}
778
779bool ProgramState::isSamplerUniformIndex(GLuint index) const
780{
Jamie Madill982f6e02017-06-07 14:33:04 -0400781 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500782}
783
784GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
785{
786 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400787 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500788}
789
Jamie Madill34ca4f52017-06-13 11:49:39 -0400790GLuint ProgramState::getAttributeLocation(const std::string &name) const
791{
792 for (const sh::Attribute &attribute : mAttributes)
793 {
794 if (attribute.name == name)
795 {
796 return attribute.location;
797 }
798 }
799
800 return static_cast<GLuint>(-1);
801}
802
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500803Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400804 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400805 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500806 mLinked(false),
807 mDeleteStatus(false),
808 mRefCount(0),
809 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500810 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500811{
812 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000813
Geoff Lang7dd2e102014-11-10 15:19:26 -0500814 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815}
816
817Program::~Program()
818{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400819 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820}
821
Jamie Madill4928b7c2017-06-20 12:57:39 -0400822void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500823{
824 if (mState.mAttachedVertexShader != nullptr)
825 {
826 mState.mAttachedVertexShader->release(context);
827 mState.mAttachedVertexShader = nullptr;
828 }
829
830 if (mState.mAttachedFragmentShader != nullptr)
831 {
832 mState.mAttachedFragmentShader->release(context);
833 mState.mAttachedFragmentShader = nullptr;
834 }
835
836 if (mState.mAttachedComputeShader != nullptr)
837 {
838 mState.mAttachedComputeShader->release(context);
839 mState.mAttachedComputeShader = nullptr;
840 }
841
Jiawei Shao89be29a2017-11-06 14:36:45 +0800842 if (mState.mAttachedGeometryShader != nullptr)
843 {
844 mState.mAttachedGeometryShader->release(context);
845 mState.mAttachedGeometryShader = nullptr;
846 }
847
Jamie Madillb7d924a2018-03-10 11:16:54 -0500848 // TODO(jmadill): Handle error in the Context.
849 ANGLE_SWALLOW_ERR(mProgram->destroy(context));
Jamie Madill4928b7c2017-06-20 12:57:39 -0400850
851 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800852 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400853 SafeDelete(mProgram);
854
855 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500856}
857
Geoff Lang70d0f492015-12-10 17:45:46 -0500858void Program::setLabel(const std::string &label)
859{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400860 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500861}
862
863const std::string &Program::getLabel() const
864{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400865 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500866}
867
Jamie Madillef300b12016-10-07 15:12:09 -0400868void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300870 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800872 case ShaderType::Vertex:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 {
Jamie Madillef300b12016-10-07 15:12:09 -0400874 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300875 mState.mAttachedVertexShader = shader;
876 mState.mAttachedVertexShader->addRef();
877 break;
878 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800879 case ShaderType::Fragment:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880 {
Jamie Madillef300b12016-10-07 15:12:09 -0400881 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300882 mState.mAttachedFragmentShader = shader;
883 mState.mAttachedFragmentShader->addRef();
884 break;
885 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800886 case ShaderType::Compute:
Martin Radev4c4c8e72016-08-04 12:25:34 +0300887 {
Jamie Madillef300b12016-10-07 15:12:09 -0400888 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300889 mState.mAttachedComputeShader = shader;
890 mState.mAttachedComputeShader->addRef();
891 break;
892 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800893 case ShaderType::Geometry:
Jiawei Shao89be29a2017-11-06 14:36:45 +0800894 {
895 ASSERT(!mState.mAttachedGeometryShader);
896 mState.mAttachedGeometryShader = shader;
897 mState.mAttachedGeometryShader->addRef();
898 break;
899 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300900 default:
901 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
904
Jamie Madillc1d770e2017-04-13 17:31:24 -0400905void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300907 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908 {
Jiawei Shao385b3e02018-03-21 09:43:28 +0800909 case ShaderType::Vertex:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400911 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500912 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300913 mState.mAttachedVertexShader = nullptr;
914 break;
915 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800916 case ShaderType::Fragment:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400918 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500919 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300920 mState.mAttachedFragmentShader = nullptr;
921 break;
922 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800923 case ShaderType::Compute:
Martin Radev4c4c8e72016-08-04 12:25:34 +0300924 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400925 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500926 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300927 mState.mAttachedComputeShader = nullptr;
928 break;
929 }
Jiawei Shao385b3e02018-03-21 09:43:28 +0800930 case ShaderType::Geometry:
Jiawei Shao89be29a2017-11-06 14:36:45 +0800931 {
932 ASSERT(mState.mAttachedGeometryShader == shader);
933 shader->release(context);
934 mState.mAttachedGeometryShader = nullptr;
935 break;
936 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300937 default:
938 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000942int Program::getAttachedShadersCount() const
943{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300944 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800945 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000946}
947
Jiawei Shao385b3e02018-03-21 09:43:28 +0800948const Shader *Program::getAttachedShader(ShaderType shaderType) const
949{
950 return mState.getAttachedShader(shaderType);
951}
952
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953void Program::bindAttributeLocation(GLuint index, const char *name)
954{
Geoff Langd8605522016-04-13 10:19:12 -0400955 mAttributeBindings.bindLocation(index, name);
956}
957
958void Program::bindUniformLocation(GLuint index, const char *name)
959{
Olli Etuahod2551232017-10-26 20:03:33 +0300960 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
Sami Väisänen46eaa942016-06-29 10:26:37 +0300963void Program::bindFragmentInputLocation(GLint index, const char *name)
964{
965 mFragmentInputBindings.bindLocation(index, name);
966}
967
Jamie Madillbd044ed2017-06-05 12:59:21 -0400968BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300969{
970 BindingInfo ret;
971 ret.type = GL_NONE;
972 ret.valid = false;
973
Jiawei Shao385b3e02018-03-21 09:43:28 +0800974 Shader *fragmentShader = mState.getAttachedShader(ShaderType::Fragment);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300975 ASSERT(fragmentShader);
976
977 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800978 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300979
980 for (const auto &binding : mFragmentInputBindings)
981 {
982 if (binding.second != static_cast<GLuint>(index))
983 continue;
984
985 ret.valid = true;
986
Olli Etuahod2551232017-10-26 20:03:33 +0300987 size_t nameLengthWithoutArrayIndex;
988 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300989
990 for (const auto &in : inputs)
991 {
Olli Etuahod2551232017-10-26 20:03:33 +0300992 if (in.name.length() == nameLengthWithoutArrayIndex &&
993 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300994 {
995 if (in.isArray())
996 {
997 // The client wants to bind either "name" or "name[0]".
998 // GL ES 3.1 spec refers to active array names with language such as:
999 // "if the string identifies the base name of an active array, where the
1000 // string would exactly match the name of the variable if the suffix "[0]"
1001 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -04001002 if (arrayIndex == GL_INVALID_INDEX)
1003 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +03001004
Corentin Wallez054f7ed2016-09-20 17:15:59 -04001005 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +03001006 }
1007 else
1008 {
1009 ret.name = in.mappedName;
1010 }
1011 ret.type = in.type;
1012 return ret;
1013 }
1014 }
1015 }
1016
1017 return ret;
1018}
1019
Jamie Madillbd044ed2017-06-05 12:59:21 -04001020void Program::pathFragmentInputGen(const Context *context,
1021 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001022 GLenum genMode,
1023 GLint components,
1024 const GLfloat *coeffs)
1025{
1026 // If the location is -1 then the command is silently ignored
1027 if (index == -1)
1028 return;
1029
Jamie Madillbd044ed2017-06-05 12:59:21 -04001030 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +03001031
1032 // If the input doesn't exist then then the command is silently ignored
1033 // This could happen through optimization for example, the shader translator
1034 // decides that a variable is not actually being used and optimizes it away.
1035 if (binding.name.empty())
1036 return;
1037
1038 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
1039}
1040
Martin Radev4c4c8e72016-08-04 12:25:34 +03001041// The attached shaders are checked for linking errors by matching up their variables.
1042// Uniform, input and output variables get collected.
1043// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001044Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001045{
Jamie Madill8ecf7f92017-01-13 17:29:52 -05001046 const auto &data = context->getContextState();
1047
Jamie Madill6c58b062017-08-01 13:44:25 -04001048 auto *platform = ANGLEPlatformCurrent();
1049 double startTime = platform->currentTime(platform);
1050
Jamie Madill6c1f6712017-02-14 19:08:04 -05001051 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001052
Jamie Madill32447362017-06-28 14:53:52 -04001053 ProgramHash programHash;
1054 auto *cache = context->getMemoryProgramCache();
1055 if (cache)
1056 {
1057 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -04001058 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001059 }
1060
1061 if (mLinked)
1062 {
Jamie Madill6c58b062017-08-01 13:44:25 -04001063 double delta = platform->currentTime(platform) - startTime;
1064 int us = static_cast<int>(delta * 1000000.0);
1065 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -04001066 return NoError();
1067 }
1068
1069 // Cache load failed, fall through to normal linking.
1070 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001071 mInfoLog.reset();
1072
Jiawei Shao73618602017-12-20 15:47:15 +08001073 if (!linkValidateShaders(context, mInfoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001074 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001075 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -04001076 }
1077
Jiawei Shao73618602017-12-20 15:47:15 +08001078 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -05001079 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001080 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001081 {
1082 return NoError();
1083 }
1084
Jiajia Qin729b2c62017-08-14 09:36:11 +08001085 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001086 {
1087 return NoError();
1088 }
1089
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001090 ProgramLinkedResources resources = {
1091 {0, PackMode::ANGLE_RELAXED},
1092 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001093 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1094 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001095
1096 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1097 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1098
1099 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001100 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001101 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001102 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001103 }
1104 }
1105 else
1106 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001107 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001108 {
1109 return NoError();
1110 }
1111
Jamie Madillbd044ed2017-06-05 12:59:21 -04001112 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001113 {
1114 return NoError();
1115 }
1116
Jamie Madillbd044ed2017-06-05 12:59:21 -04001117 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001118 {
1119 return NoError();
1120 }
1121
Jiajia Qin729b2c62017-08-14 09:36:11 +08001122 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001123 {
1124 return NoError();
1125 }
1126
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04001127 if (!linkValidateGlobalNames(context, mInfoLog))
1128 {
1129 return NoError();
1130 }
1131
Jamie Madillbd044ed2017-06-05 12:59:21 -04001132 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001133
Jiawei Shao73618602017-12-20 15:47:15 +08001134 ASSERT(mState.mAttachedVertexShader);
1135 mState.mNumViews = mState.mAttachedVertexShader->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +03001136
Jamie Madillbd044ed2017-06-05 12:59:21 -04001137 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001138
Jamie Madill192745a2016-12-22 15:58:21 -05001139 // Map the varyings to the register file
1140 // In WebGL, we use a slightly different handling for packing variables.
Jamie Madill61d53252018-01-31 14:49:24 -05001141 gl::PackMode packMode = PackMode::ANGLE_RELAXED;
1142 if (data.getLimitations().noFlexibleVaryingPacking)
1143 {
1144 // D3D9 pack mode is strictly more strict than WebGL, so takes priority.
1145 packMode = PackMode::ANGLE_NON_CONFORMANT_D3D9;
1146 }
1147 else if (data.getExtensions().webglCompatibility)
1148 {
1149 packMode = PackMode::WEBGL_STRICT;
1150 }
Jamie Madillc9727f32017-11-07 12:37:07 -05001151
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001152 ProgramLinkedResources resources = {
1153 {data.getCaps().maxVaryingVectors, packMode},
1154 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +08001155 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
1156 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -05001157
1158 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
1159 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1160
Jiawei Shao73618602017-12-20 15:47:15 +08001161 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001162 {
1163 return NoError();
1164 }
1165
jchen1085c93c42017-11-12 15:36:47 +08001166 if (!resources.varyingPacking.collectAndPackUserVaryings(
1167 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001168 {
1169 return NoError();
1170 }
1171
Jamie Madillc9727f32017-11-07 12:37:07 -05001172 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001173 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001174 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001175 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001176 }
1177
1178 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001179 }
1180
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001181 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001182
jchen10eaef1e52017-06-13 10:44:11 +08001183 setUniformValuesFromBindingQualifiers();
1184
Yunchao Heece12532017-11-21 15:50:21 +08001185 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1186 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001187 ASSERT(mLinked);
1188 updateLinkedShaderStages();
1189
Jamie Madill54164b02017-08-28 15:17:37 -04001190 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001191 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001192
Jamie Madill32447362017-06-28 14:53:52 -04001193 // Save to the program cache.
1194 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
1195 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
1196 {
1197 cache->putProgram(programHash, context, this);
1198 }
1199
Jamie Madill6c58b062017-08-01 13:44:25 -04001200 double delta = platform->currentTime(platform) - startTime;
1201 int us = static_cast<int>(delta * 1000000.0);
1202 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
1203
Martin Radev4c4c8e72016-08-04 12:25:34 +03001204 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001205}
1206
Yunchao He85072e82017-11-14 15:43:28 +08001207void Program::updateLinkedShaderStages()
1208{
Yunchao Heece12532017-11-21 15:50:21 +08001209 mState.mLinkedShaderStages.reset();
1210
Yunchao He85072e82017-11-14 15:43:28 +08001211 if (mState.mAttachedVertexShader)
1212 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001213 mState.mLinkedShaderStages.set(ShaderType::Vertex);
Yunchao He85072e82017-11-14 15:43:28 +08001214 }
1215
1216 if (mState.mAttachedFragmentShader)
1217 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001218 mState.mLinkedShaderStages.set(ShaderType::Fragment);
Yunchao He85072e82017-11-14 15:43:28 +08001219 }
1220
1221 if (mState.mAttachedComputeShader)
1222 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001223 mState.mLinkedShaderStages.set(ShaderType::Compute);
Yunchao He85072e82017-11-14 15:43:28 +08001224 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001225
1226 if (mState.mAttachedGeometryShader)
1227 {
Jiawei Shao385b3e02018-03-21 09:43:28 +08001228 mState.mLinkedShaderStages.set(ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001229 }
Yunchao He85072e82017-11-14 15:43:28 +08001230}
1231
James Darpinian30b604d2018-03-12 17:26:57 -07001232void ProgramState::updateTransformFeedbackStrides()
1233{
1234 if (mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS)
1235 {
1236 mTransformFeedbackStrides.resize(1);
1237 size_t totalSize = 0;
1238 for (auto &varying : mLinkedTransformFeedbackVaryings)
1239 {
1240 totalSize += varying.size() * VariableExternalSize(varying.type);
1241 }
1242 mTransformFeedbackStrides[0] = static_cast<GLsizei>(totalSize);
1243 }
1244 else
1245 {
1246 mTransformFeedbackStrides.resize(mLinkedTransformFeedbackVaryings.size());
1247 for (size_t i = 0; i < mLinkedTransformFeedbackVaryings.size(); i++)
1248 {
1249 auto &varying = mLinkedTransformFeedbackVaryings[i];
1250 mTransformFeedbackStrides[i] =
1251 static_cast<GLsizei>(varying.size() * VariableExternalSize(varying.type));
1252 }
1253 }
1254}
1255
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001256// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001257void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001259 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001260 mState.mAttributesTypeMask.reset();
1261 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001262 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001263 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001264 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001265 mState.mUniforms.clear();
1266 mState.mUniformLocations.clear();
1267 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001268 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001269 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001270 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001271 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001272 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001273 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001274 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001275 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001276 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001277 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001278 mState.mNumViews = -1;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08001279 mState.mGeometryShaderInputPrimitiveType = GL_TRIANGLES;
1280 mState.mGeometryShaderOutputPrimitiveType = GL_TRIANGLE_STRIP;
1281 mState.mGeometryShaderInvocations = 1;
1282 mState.mGeometryShaderMaxVertices = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284 mValidated = false;
1285
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001286 mLinked = false;
1287}
1288
Geoff Lange1a27752015-10-05 13:16:04 -04001289bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001290{
1291 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292}
1293
Jiawei Shao385b3e02018-03-21 09:43:28 +08001294bool Program::hasLinkedShaderStage(ShaderType shaderType) const
1295{
1296 ASSERT(shaderType != ShaderType::InvalidEnum);
1297 return mState.mLinkedShaderStages[shaderType];
1298}
1299
Jamie Madilla2c74982016-12-12 11:20:42 -05001300Error Program::loadBinary(const Context *context,
1301 GLenum binaryFormat,
1302 const void *binary,
1303 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001304{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001305 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001306
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001308 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001310 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1311 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001312 {
Jamie Madillf6113162015-05-07 11:49:21 -04001313 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001314 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001315 }
1316
Jamie Madill4f86d052017-06-05 12:59:26 -04001317 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1318 ANGLE_TRY_RESULT(
1319 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001320
1321 // Currently we require the full shader text to compute the program hash.
1322 // TODO(jmadill): Store the binary in the internal program cache.
1323
Jamie Madillb0a838b2016-11-13 20:02:12 -05001324 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001325#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326}
1327
Jamie Madilla2c74982016-12-12 11:20:42 -05001328Error Program::saveBinary(const Context *context,
1329 GLenum *binaryFormat,
1330 void *binary,
1331 GLsizei bufSize,
1332 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001333{
1334 if (binaryFormat)
1335 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001336 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337 }
1338
Jamie Madill4f86d052017-06-05 12:59:26 -04001339 angle::MemoryBuffer memoryBuf;
1340 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001341
Jamie Madill4f86d052017-06-05 12:59:26 -04001342 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1343 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344
1345 if (streamLength > bufSize)
1346 {
1347 if (length)
1348 {
1349 *length = 0;
1350 }
1351
1352 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1353 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1354 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001355 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356 }
1357
1358 if (binary)
1359 {
1360 char *ptr = reinterpret_cast<char*>(binary);
1361
Jamie Madill48ef11b2016-04-27 15:21:52 -04001362 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001363 ptr += streamLength;
1364
1365 ASSERT(ptr - streamLength == binary);
1366 }
1367
1368 if (length)
1369 {
1370 *length = streamLength;
1371 }
1372
He Yunchaoacd18982017-01-04 10:46:42 +08001373 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
Jamie Madillffe00c02017-06-27 16:26:55 -04001376GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377{
1378 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001379 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380 if (error.isError())
1381 {
1382 return 0;
1383 }
1384
1385 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001386}
1387
Geoff Langc5629752015-12-07 16:29:04 -05001388void Program::setBinaryRetrievableHint(bool retrievable)
1389{
1390 // TODO(jmadill) : replace with dirty bits
1391 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001392 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001393}
1394
1395bool Program::getBinaryRetrievableHint() const
1396{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001397 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001398}
1399
Yunchao He61afff12017-03-14 15:34:03 +08001400void Program::setSeparable(bool separable)
1401{
1402 // TODO(yunchao) : replace with dirty bits
1403 if (mState.mSeparable != separable)
1404 {
1405 mProgram->setSeparable(separable);
1406 mState.mSeparable = separable;
1407 }
1408}
1409
1410bool Program::isSeparable() const
1411{
1412 return mState.mSeparable;
1413}
1414
Jamie Madill6c1f6712017-02-14 19:08:04 -05001415void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001416{
1417 mRefCount--;
1418
1419 if (mRefCount == 0 && mDeleteStatus)
1420 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001421 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001422 }
1423}
1424
1425void Program::addRef()
1426{
1427 mRefCount++;
1428}
1429
1430unsigned int Program::getRefCount() const
1431{
1432 return mRefCount;
1433}
1434
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001435int Program::getInfoLogLength() const
1436{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001437 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001438}
1439
Geoff Lange1a27752015-10-05 13:16:04 -04001440void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001441{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001442 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001443}
1444
Geoff Lange1a27752015-10-05 13:16:04 -04001445void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001446{
1447 int total = 0;
1448
Martin Radev4c4c8e72016-08-04 12:25:34 +03001449 if (mState.mAttachedComputeShader)
1450 {
1451 if (total < maxCount)
1452 {
1453 shaders[total] = mState.mAttachedComputeShader->getHandle();
1454 total++;
1455 }
1456 }
1457
Jamie Madill48ef11b2016-04-27 15:21:52 -04001458 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001459 {
1460 if (total < maxCount)
1461 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001462 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001463 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001464 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001465 }
1466
Jamie Madill48ef11b2016-04-27 15:21:52 -04001467 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001468 {
1469 if (total < maxCount)
1470 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001471 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001472 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001473 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001474 }
1475
Jiawei Shao89be29a2017-11-06 14:36:45 +08001476 if (mState.mAttachedGeometryShader)
1477 {
1478 if (total < maxCount)
1479 {
1480 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1481 total++;
1482 }
1483 }
1484
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001485 if (count)
1486 {
1487 *count = total;
1488 }
1489}
1490
Geoff Lange1a27752015-10-05 13:16:04 -04001491GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001493 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494}
1495
Jamie Madill63805b42015-08-25 13:17:39 -04001496bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001497{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001498 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1499 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001500}
1501
jchen10fd7c3b52017-03-21 15:36:03 +08001502void Program::getActiveAttribute(GLuint index,
1503 GLsizei bufsize,
1504 GLsizei *length,
1505 GLint *size,
1506 GLenum *type,
1507 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001508{
Jamie Madillc349ec02015-08-21 16:53:12 -04001509 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001510 {
1511 if (bufsize > 0)
1512 {
1513 name[0] = '\0';
1514 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001515
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001516 if (length)
1517 {
1518 *length = 0;
1519 }
1520
1521 *type = GL_NONE;
1522 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001523 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001524 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001525
jchen1036e120e2017-03-14 14:53:58 +08001526 ASSERT(index < mState.mAttributes.size());
1527 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001528
1529 if (bufsize > 0)
1530 {
jchen10fd7c3b52017-03-21 15:36:03 +08001531 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001532 }
1533
1534 // Always a single 'type' instance
1535 *size = 1;
1536 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001537}
1538
Geoff Lange1a27752015-10-05 13:16:04 -04001539GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001540{
Jamie Madillc349ec02015-08-21 16:53:12 -04001541 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001542 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001543 return 0;
1544 }
1545
jchen1036e120e2017-03-14 14:53:58 +08001546 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001547}
1548
Geoff Lange1a27752015-10-05 13:16:04 -04001549GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001550{
Jamie Madillc349ec02015-08-21 16:53:12 -04001551 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001552 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001553 return 0;
1554 }
1555
1556 size_t maxLength = 0;
1557
Jamie Madill48ef11b2016-04-27 15:21:52 -04001558 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001559 {
jchen1036e120e2017-03-14 14:53:58 +08001560 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001561 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562
Jamie Madillc349ec02015-08-21 16:53:12 -04001563 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564}
1565
jchen1015015f72017-03-16 13:54:21 +08001566GLuint Program::getInputResourceIndex(const GLchar *name) const
1567{
Olli Etuahod2551232017-10-26 20:03:33 +03001568 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001569}
1570
1571GLuint Program::getOutputResourceIndex(const GLchar *name) const
1572{
1573 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1574}
1575
jchen10fd7c3b52017-03-21 15:36:03 +08001576size_t Program::getOutputResourceCount() const
1577{
1578 return (mLinked ? mState.mOutputVariables.size() : 0);
1579}
1580
jchen10baf5d942017-08-28 20:45:48 +08001581template <typename T>
1582void Program::getResourceName(GLuint index,
1583 const std::vector<T> &resources,
1584 GLsizei bufSize,
1585 GLsizei *length,
1586 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001587{
1588 if (length)
1589 {
1590 *length = 0;
1591 }
1592
1593 if (!mLinked)
1594 {
1595 if (bufSize > 0)
1596 {
1597 name[0] = '\0';
1598 }
1599 return;
1600 }
jchen10baf5d942017-08-28 20:45:48 +08001601 ASSERT(index < resources.size());
1602 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001603
1604 if (bufSize > 0)
1605 {
Olli Etuahod2551232017-10-26 20:03:33 +03001606 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001607 }
1608}
1609
jchen10baf5d942017-08-28 20:45:48 +08001610void Program::getInputResourceName(GLuint index,
1611 GLsizei bufSize,
1612 GLsizei *length,
1613 GLchar *name) const
1614{
1615 getResourceName(index, mState.mAttributes, bufSize, length, name);
1616}
1617
1618void Program::getOutputResourceName(GLuint index,
1619 GLsizei bufSize,
1620 GLsizei *length,
1621 GLchar *name) const
1622{
1623 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1624}
1625
1626void Program::getUniformResourceName(GLuint index,
1627 GLsizei bufSize,
1628 GLsizei *length,
1629 GLchar *name) const
1630{
1631 getResourceName(index, mState.mUniforms, bufSize, length, name);
1632}
1633
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001634void Program::getBufferVariableResourceName(GLuint index,
1635 GLsizei bufSize,
1636 GLsizei *length,
1637 GLchar *name) const
1638{
1639 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1640}
1641
jchen10880683b2017-04-12 16:21:55 +08001642const sh::Attribute &Program::getInputResource(GLuint index) const
1643{
1644 ASSERT(index < mState.mAttributes.size());
1645 return mState.mAttributes[index];
1646}
1647
1648const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1649{
1650 ASSERT(index < mState.mOutputVariables.size());
1651 return mState.mOutputVariables[index];
1652}
1653
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654GLint Program::getFragDataLocation(const std::string &name) const
1655{
Olli Etuahod2551232017-10-26 20:03:33 +03001656 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001657}
1658
Geoff Lange1a27752015-10-05 13:16:04 -04001659void Program::getActiveUniform(GLuint index,
1660 GLsizei bufsize,
1661 GLsizei *length,
1662 GLint *size,
1663 GLenum *type,
1664 GLchar *name) 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 Madill62d31cb2015-09-11 13:25:51 -04001668 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001669 ASSERT(index < mState.mUniforms.size());
1670 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671
1672 if (bufsize > 0)
1673 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001675 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676 }
1677
Olli Etuaho465835d2017-09-26 13:34:10 +03001678 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001680 }
1681 else
1682 {
1683 if (bufsize > 0)
1684 {
1685 name[0] = '\0';
1686 }
1687
1688 if (length)
1689 {
1690 *length = 0;
1691 }
1692
1693 *size = 0;
1694 *type = GL_NONE;
1695 }
1696}
1697
Geoff Lange1a27752015-10-05 13:16:04 -04001698GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001701 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001702 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001703 }
1704 else
1705 {
1706 return 0;
1707 }
1708}
1709
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001710size_t Program::getActiveBufferVariableCount() const
1711{
1712 return mLinked ? mState.mBufferVariables.size() : 0;
1713}
1714
Geoff Lange1a27752015-10-05 13:16:04 -04001715GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001716{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718
1719 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001720 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001723 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001725 size_t length = uniform.name.length() + 1u;
1726 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001727 {
1728 length += 3; // Counting in "[0]".
1729 }
1730 maxLength = std::max(length, maxLength);
1731 }
1732 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001733 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001734
Jamie Madill62d31cb2015-09-11 13:25:51 -04001735 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001736}
1737
Geoff Lang7dd2e102014-11-10 15:19:26 -05001738bool Program::isValidUniformLocation(GLint location) const
1739{
Jamie Madille2e406c2016-06-02 13:04:10 -04001740 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001741 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001742 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001743}
1744
Jamie Madill62d31cb2015-09-11 13:25:51 -04001745const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001746{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001747 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001748 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001749}
1750
Jamie Madillac4e9c32017-01-13 14:07:12 -05001751const VariableLocation &Program::getUniformLocation(GLint location) const
1752{
1753 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1754 return mState.mUniformLocations[location];
1755}
1756
1757const std::vector<VariableLocation> &Program::getUniformLocations() const
1758{
1759 return mState.mUniformLocations;
1760}
1761
1762const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1763{
1764 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1765 return mState.mUniforms[index];
1766}
1767
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001768const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1769{
1770 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1771 return mState.mBufferVariables[index];
1772}
1773
Jamie Madill62d31cb2015-09-11 13:25:51 -04001774GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775{
Olli Etuahod2551232017-10-26 20:03:33 +03001776 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777}
1778
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780{
Jamie Madille7d84322017-01-10 18:21:59 -05001781 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782}
1783
1784void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1785{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001786 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1787 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001788 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789}
1790
1791void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1792{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001793 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1794 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001795 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001796}
1797
1798void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1799{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001800 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1801 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001802 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803}
1804
1805void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1806{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001807 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1808 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001809 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810}
1811
Jamie Madill81c2e252017-09-09 23:32:46 -04001812Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001814 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1815 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1816
Jamie Madill81c2e252017-09-09 23:32:46 -04001817 mProgram->setUniform1iv(location, clampedCount, v);
1818
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001819 if (mState.isSamplerUniformIndex(locationInfo.index))
1820 {
1821 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001822 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001823 }
1824
Jamie Madill81c2e252017-09-09 23:32:46 -04001825 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826}
1827
1828void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1829{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001830 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1831 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001832 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833}
1834
1835void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1836{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001837 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1838 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001839 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840}
1841
1842void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1843{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001844 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1845 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001846 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847}
1848
1849void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1850{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001851 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1852 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001853 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854}
1855
1856void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1857{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001858 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1859 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001860 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861}
1862
1863void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1864{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001865 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1866 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001867 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868}
1869
1870void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1871{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001872 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1873 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001874 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875}
1876
1877void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1878{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001879 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001880 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881}
1882
1883void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1884{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001885 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001886 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001887}
1888
1889void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1890{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001891 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001892 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893}
1894
1895void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1896{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001897 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001898 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001899}
1900
1901void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1902{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001903 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001904 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905}
1906
1907void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1908{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001909 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001910 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001911}
1912
1913void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1914{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001915 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001916 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917}
1918
1919void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1920{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001921 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001922 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923}
1924
1925void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1926{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001927 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001928 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929}
1930
Jamie Madill54164b02017-08-28 15:17:37 -04001931void Program::getUniformfv(const Context *context, GLint location, GLfloat *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_FLOAT)
1938 {
1939 mProgram->getUniformfv(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
Jamie Madill54164b02017-08-28 15:17:37 -04001948void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949{
Jamie Madill54164b02017-08-28 15:17:37 -04001950 const auto &uniformLocation = mState.getUniformLocations()[location];
1951 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1952
1953 GLenum nativeType = gl::VariableComponentType(uniform.type);
1954 if (nativeType == GL_INT || nativeType == GL_BOOL)
1955 {
1956 mProgram->getUniformiv(context, location, v);
1957 }
1958 else
1959 {
1960 getUniformInternal(context, v, location, nativeType,
1961 gl::VariableComponentCount(uniform.type));
1962 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001963}
1964
Jamie Madill54164b02017-08-28 15:17:37 -04001965void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966{
Jamie Madill54164b02017-08-28 15:17:37 -04001967 const auto &uniformLocation = mState.getUniformLocations()[location];
1968 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1969
1970 GLenum nativeType = gl::VariableComponentType(uniform.type);
1971 if (nativeType == GL_UNSIGNED_INT)
1972 {
1973 mProgram->getUniformuiv(context, location, v);
1974 }
1975 else
1976 {
1977 getUniformInternal(context, v, location, nativeType,
1978 gl::VariableComponentCount(uniform.type));
1979 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980}
1981
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982void Program::flagForDeletion()
1983{
1984 mDeleteStatus = true;
1985}
1986
1987bool Program::isFlaggedForDeletion() const
1988{
1989 return mDeleteStatus;
1990}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001991
Brandon Jones43a53e22014-08-28 16:23:22 -07001992void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001993{
1994 mInfoLog.reset();
1995
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001997 {
Geoff Lang92019432017-11-20 13:09:34 -05001998 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001999 }
2000 else
2001 {
Jamie Madillf6113162015-05-07 11:49:21 -04002002 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002003 }
2004}
2005
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
2007{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002008 // Skip cache if we're using an infolog, so we get the full error.
2009 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
2010 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
2011 {
2012 return mCachedValidateSamplersResult.value();
2013 }
2014
2015 if (mTextureUnitTypesCache.empty())
2016 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002017 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002018 }
2019 else
2020 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002021 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(),
2022 TextureType::InvalidEnum);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002023 }
2024
2025 // if any two active samplers in a program are of different types, but refer to the same
2026 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2027 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05002028 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002029 {
Jamie Madill54164b02017-08-28 15:17:37 -04002030 if (samplerBinding.unreferenced)
2031 continue;
2032
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002033 TextureType textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002034
Jamie Madille7d84322017-01-10 18:21:59 -05002035 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002036 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002037 if (textureUnit >= caps.maxCombinedTextureImageUnits)
2038 {
2039 if (infoLog)
2040 {
2041 (*infoLog) << "Sampler uniform (" << textureUnit
2042 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
2043 << caps.maxCombinedTextureImageUnits << ")";
2044 }
2045
2046 mCachedValidateSamplersResult = false;
2047 return false;
2048 }
2049
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002050 if (mTextureUnitTypesCache[textureUnit] != TextureType::InvalidEnum)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002051 {
2052 if (textureType != mTextureUnitTypesCache[textureUnit])
2053 {
2054 if (infoLog)
2055 {
2056 (*infoLog) << "Samplers of conflicting types refer to the same texture "
2057 "image unit ("
2058 << textureUnit << ").";
2059 }
2060
2061 mCachedValidateSamplersResult = false;
2062 return false;
2063 }
2064 }
2065 else
2066 {
2067 mTextureUnitTypesCache[textureUnit] = textureType;
2068 }
2069 }
2070 }
2071
2072 mCachedValidateSamplersResult = true;
2073 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074}
2075
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002076bool Program::isValidated() const
2077{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 return mValidated;
2079}
2080
Geoff Lange1a27752015-10-05 13:16:04 -04002081GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002083 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084}
2085
jchen1058f67be2017-10-27 08:59:27 +08002086GLuint Program::getActiveAtomicCounterBufferCount() const
2087{
2088 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
2089}
2090
Jiajia Qin729b2c62017-08-14 09:36:11 +08002091GLuint Program::getActiveShaderStorageBlockCount() const
2092{
2093 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
2094}
2095
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002096void Program::getActiveUniformBlockName(const GLuint blockIndex,
2097 GLsizei bufSize,
2098 GLsizei *length,
2099 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002101 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
2102}
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002104void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
2105 GLsizei bufSize,
2106 GLsizei *length,
2107 GLchar *blockName) const
2108{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002110 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002111}
2112
Qin Jiajia9bf55522018-01-29 13:56:23 +08002113template <typename T>
2114GLint Program::getActiveInterfaceBlockMaxNameLength(const std::vector<T> &resources) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002115{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116 int maxLength = 0;
2117
2118 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002119 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002120 for (const T &resource : resources)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002122 if (!resource.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 {
Qin Jiajia9bf55522018-01-29 13:56:23 +08002124 int length = static_cast<int>(resource.nameWithArrayIndex().length());
jchen10af713a22017-04-19 09:10:56 +08002125 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 }
2127 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002128 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129
2130 return maxLength;
2131}
2132
Qin Jiajia9bf55522018-01-29 13:56:23 +08002133GLint Program::getActiveUniformBlockMaxNameLength() const
2134{
2135 return getActiveInterfaceBlockMaxNameLength(mState.mUniformBlocks);
2136}
2137
2138GLint Program::getActiveShaderStorageBlockMaxNameLength() const
2139{
2140 return getActiveInterfaceBlockMaxNameLength(mState.mShaderStorageBlocks);
2141}
2142
Geoff Lange1a27752015-10-05 13:16:04 -04002143GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002145 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
2146}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002147
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002148GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
2149{
2150 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00002151}
2152
Jiajia Qin729b2c62017-08-14 09:36:11 +08002153const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002154{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002155 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
2156 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00002157}
2158
Jiajia Qin3a9090f2017-09-27 14:37:04 +08002159const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
2160{
2161 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
2162 return mState.mShaderStorageBlocks[index];
2163}
2164
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002165void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
2166{
jchen107a20b972017-06-13 14:25:26 +08002167 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05002168 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04002169 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002170}
2171
2172GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
2173{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002174 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00002175}
2176
Jiajia Qin729b2c62017-08-14 09:36:11 +08002177GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
2178{
2179 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
2180}
2181
Geoff Lang48dcae72014-02-05 16:28:24 -05002182void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
2183{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002184 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05002185 for (GLsizei i = 0; i < count; i++)
2186 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002187 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05002188 }
2189
Jamie Madill48ef11b2016-04-27 15:21:52 -04002190 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05002191}
2192
2193void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
2194{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002195 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002196 {
jchen10a9042d32017-03-17 08:50:45 +08002197 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2198 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
2199 std::string varName = var.nameWithArrayIndex();
2200 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05002201 if (length)
2202 {
2203 *length = lastNameIdx;
2204 }
2205 if (size)
2206 {
jchen10a9042d32017-03-17 08:50:45 +08002207 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05002208 }
2209 if (type)
2210 {
jchen10a9042d32017-03-17 08:50:45 +08002211 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002212 }
2213 if (name)
2214 {
jchen10a9042d32017-03-17 08:50:45 +08002215 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002216 name[lastNameIdx] = '\0';
2217 }
2218 }
2219}
2220
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002221GLsizei Program::getTransformFeedbackVaryingCount() const
2222{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002223 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002224 {
jchen10a9042d32017-03-17 08:50:45 +08002225 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002226 }
2227 else
2228 {
2229 return 0;
2230 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002231}
2232
2233GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2234{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002236 {
2237 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002238 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002239 {
jchen10a9042d32017-03-17 08:50:45 +08002240 maxSize =
2241 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002242 }
2243
2244 return maxSize;
2245 }
2246 else
2247 {
2248 return 0;
2249 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002250}
2251
2252GLenum Program::getTransformFeedbackBufferMode() const
2253{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002254 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255}
2256
Jiawei Shao73618602017-12-20 15:47:15 +08002257bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
2258{
2259 Shader *vertexShader = mState.mAttachedVertexShader;
2260 Shader *fragmentShader = mState.mAttachedFragmentShader;
2261 Shader *computeShader = mState.mAttachedComputeShader;
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002262 Shader *geometryShader = mState.mAttachedGeometryShader;
Jiawei Shao73618602017-12-20 15:47:15 +08002263
2264 bool isComputeShaderAttached = (computeShader != nullptr);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002265 bool isGraphicsShaderAttached =
2266 (vertexShader != nullptr || fragmentShader != nullptr || geometryShader != nullptr);
Jiawei Shao73618602017-12-20 15:47:15 +08002267 // Check whether we both have a compute and non-compute shaders attached.
2268 // If there are of both types attached, then linking should fail.
2269 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2270 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2271 {
2272 infoLog << "Both compute and graphics shaders are attached to the same program.";
2273 return false;
2274 }
2275
2276 if (computeShader)
2277 {
2278 if (!computeShader->isCompiled(context))
2279 {
2280 infoLog << "Attached compute shader is not compiled.";
2281 return false;
2282 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002283 ASSERT(computeShader->getType() == ShaderType::Compute);
Jiawei Shao73618602017-12-20 15:47:15 +08002284
2285 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
2286
2287 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2288 // If the work group size is not specified, a link time error should occur.
2289 if (!mState.mComputeShaderLocalSize.isDeclared())
2290 {
2291 infoLog << "Work group size is not specified.";
2292 return false;
2293 }
2294 }
2295 else
2296 {
2297 if (!fragmentShader || !fragmentShader->isCompiled(context))
2298 {
2299 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2300 return false;
2301 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002302 ASSERT(fragmentShader->getType() == ShaderType::Fragment);
Jiawei Shao73618602017-12-20 15:47:15 +08002303
2304 if (!vertexShader || !vertexShader->isCompiled(context))
2305 {
2306 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2307 return false;
2308 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002309 ASSERT(vertexShader->getType() == ShaderType::Vertex);
Jiawei Shao73618602017-12-20 15:47:15 +08002310
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002311 int vertexShaderVersion = vertexShader->getShaderVersion(context);
2312 if (fragmentShader->getShaderVersion(context) != vertexShaderVersion)
Jiawei Shao73618602017-12-20 15:47:15 +08002313 {
2314 infoLog << "Fragment shader version does not match vertex shader version.";
2315 return false;
2316 }
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002317
2318 if (geometryShader)
2319 {
2320 // [GL_EXT_geometry_shader] Chapter 7
2321 // Linking can fail for a variety of reasons as specified in the OpenGL ES Shading
2322 // Language Specification, as well as any of the following reasons:
2323 // * One or more of the shader objects attached to <program> are not compiled
2324 // successfully.
2325 // * The shaders do not use the same shader language version.
2326 // * <program> contains objects to form a geometry shader, and
2327 // - <program> is not separable and contains no objects to form a vertex shader; or
2328 // - the input primitive type, output primitive type, or maximum output vertex count
2329 // is not specified in the compiled geometry shader object.
2330 if (!geometryShader->isCompiled(context))
2331 {
2332 infoLog << "The attached geometry shader isn't compiled.";
2333 return false;
2334 }
2335
2336 if (geometryShader->getShaderVersion(context) != vertexShaderVersion)
2337 {
2338 mInfoLog << "Geometry shader version does not match vertex shader version.";
2339 return false;
2340 }
Jiawei Shao385b3e02018-03-21 09:43:28 +08002341 ASSERT(geometryShader->getType() == ShaderType::Geometry);
Jiawei Shao4ed05da2018-02-02 14:26:15 +08002342
2343 Optional<GLenum> inputPrimitive =
2344 geometryShader->getGeometryShaderInputPrimitiveType(context);
2345 if (!inputPrimitive.valid())
2346 {
2347 mInfoLog << "Input primitive type is not specified in the geometry shader.";
2348 return false;
2349 }
2350
2351 Optional<GLenum> outputPrimitive =
2352 geometryShader->getGeometryShaderOutputPrimitiveType(context);
2353 if (!outputPrimitive.valid())
2354 {
2355 mInfoLog << "Output primitive type is not specified in the geometry shader.";
2356 return false;
2357 }
2358
2359 Optional<GLint> maxVertices = geometryShader->getGeometryShaderMaxVertices(context);
2360 if (!maxVertices.valid())
2361 {
2362 mInfoLog << "'max_vertices' is not specified in the geometry shader.";
2363 return false;
2364 }
2365
2366 mState.mGeometryShaderInputPrimitiveType = inputPrimitive.value();
2367 mState.mGeometryShaderOutputPrimitiveType = outputPrimitive.value();
2368 mState.mGeometryShaderMaxVertices = maxVertices.value();
2369 mState.mGeometryShaderInvocations =
2370 geometryShader->getGeometryShaderInvocations(context);
2371 }
Jiawei Shao73618602017-12-20 15:47:15 +08002372 }
2373
2374 return true;
2375}
2376
jchen10910a3da2017-11-15 09:40:11 +08002377GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2378{
2379 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2380 {
2381 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2382 if (tf.nameWithArrayIndex() == name)
2383 {
2384 return tfIndex;
2385 }
2386 }
2387 return GL_INVALID_INDEX;
2388}
2389
2390const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2391{
2392 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2393 return mState.mLinkedTransformFeedbackVaryings[index];
2394}
2395
Jamie Madillbd044ed2017-06-05 12:59:21 -04002396bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002397{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002398 std::vector<Shader *> activeShaders;
2399 activeShaders.push_back(mState.mAttachedVertexShader);
2400 if (mState.mAttachedGeometryShader)
2401 {
2402 activeShaders.push_back(mState.mAttachedGeometryShader);
2403 }
2404 activeShaders.push_back(mState.mAttachedFragmentShader);
Jamie Madill192745a2016-12-22 15:58:21 -05002405
Jiawei Shaod063aff2018-02-22 10:19:09 +08002406 const size_t activeShaderCount = activeShaders.size();
2407 for (size_t shaderIndex = 0; shaderIndex < activeShaderCount - 1; ++shaderIndex)
2408 {
2409 if (!linkValidateShaderInterfaceMatching(context, activeShaders[shaderIndex],
2410 activeShaders[shaderIndex + 1], infoLog))
2411 {
2412 return false;
2413 }
2414 }
2415
2416 if (!linkValidateBuiltInVaryings(context, infoLog))
2417 {
2418 return false;
2419 }
2420
2421 if (!linkValidateFragmentInputBindings(context, infoLog))
2422 {
2423 return false;
2424 }
2425
2426 return true;
2427}
2428
2429// [OpenGL ES 3.1] Chapter 7.4.1 "Shader Interface Matchining" Page 91
2430// TODO(jiawei.shao@intel.com): add validation on input/output blocks matching
2431bool Program::linkValidateShaderInterfaceMatching(const Context *context,
2432 gl::Shader *generatingShader,
2433 gl::Shader *consumingShader,
2434 gl::InfoLog &infoLog) const
2435{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002436 ASSERT(generatingShader->getShaderVersion(context) ==
2437 consumingShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002438
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002439 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context);
2440 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441
Jiawei Shao385b3e02018-03-21 09:43:28 +08002442 bool validateGeometryShaderInputs = consumingShader->getType() == ShaderType::Geometry;
Sami Väisänen46eaa942016-06-29 10:26:37 +03002443
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002444 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002445 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002446 bool matched = false;
2447
2448 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002449 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 {
2451 continue;
2452 }
2453
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002454 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002455 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002456 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002457 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002458 ASSERT(!output.isBuiltIn());
2459
2460 std::string mismatchedStructFieldName;
2461 LinkMismatchError linkError =
2462 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context),
Jiawei Shaod063aff2018-02-22 10:19:09 +08002463 validateGeometryShaderInputs, &mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002464 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002465 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002466 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2467 mismatchedStructFieldName, generatingShader->getType(),
2468 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002469 return false;
2470 }
2471
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472 matched = true;
2473 break;
2474 }
2475 }
2476
Olli Etuaho107c7242018-03-20 15:45:35 +02002477 // We permit unmatched, unreferenced varyings. Note that this specifically depends on
2478 // whether the input is statically used - a statically used input should fail this test even
2479 // if it is not active. GLSL ES 3.00.6 section 4.3.10.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002480 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002481 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002482 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2483 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2484 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002485 return false;
2486 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002487 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002488
Jiawei Shaod063aff2018-02-22 10:19:09 +08002489 // TODO(jmadill): verify no unmatched output varyings?
Sami Väisänen46eaa942016-06-29 10:26:37 +03002490
Jiawei Shaod063aff2018-02-22 10:19:09 +08002491 return true;
2492}
2493
2494bool Program::linkValidateFragmentInputBindings(const Context *context, gl::InfoLog &infoLog) const
2495{
2496 ASSERT(mState.mAttachedFragmentShader);
2497
2498 std::map<GLuint, std::string> staticFragmentInputLocations;
2499
2500 const std::vector<sh::Varying> &fragmentInputVaryings =
2501 mState.mAttachedFragmentShader->getInputVaryings(context);
2502 for (const sh::Varying &input : fragmentInputVaryings)
2503 {
2504 if (input.isBuiltIn() || !input.staticUse)
2505 {
Sami Väisänen46eaa942016-06-29 10:26:37 +03002506 continue;
Jiawei Shaod063aff2018-02-22 10:19:09 +08002507 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002508
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002509 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002510 if (inputBinding == -1)
2511 continue;
2512
2513 const auto it = staticFragmentInputLocations.find(inputBinding);
2514 if (it == std::end(staticFragmentInputLocations))
2515 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002516 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002517 }
2518 else
2519 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002520 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002521 << it->second;
2522 return false;
2523 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002524 }
2525
2526 return true;
2527}
2528
Jamie Madillbd044ed2017-06-05 12:59:21 -04002529bool Program::linkUniforms(const Context *context,
2530 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002531 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002532{
Olli Etuahob78707c2017-03-09 15:03:11 +00002533 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002534 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002535 {
2536 return false;
2537 }
2538
Olli Etuahob78707c2017-03-09 15:03:11 +00002539 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002540
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002541 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002542
jchen10eaef1e52017-06-13 10:44:11 +08002543 if (!linkAtomicCounterBuffers())
2544 {
2545 return false;
2546 }
2547
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002548 return true;
2549}
2550
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002551void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002552{
Jamie Madill982f6e02017-06-07 14:33:04 -04002553 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2554 unsigned int low = high;
2555
jchen10eaef1e52017-06-13 10:44:11 +08002556 for (auto counterIter = mState.mUniforms.rbegin();
2557 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2558 {
2559 --low;
2560 }
2561
2562 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2563
2564 high = low;
2565
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002566 for (auto imageIter = mState.mUniforms.rbegin();
2567 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2568 {
2569 --low;
2570 }
2571
2572 mState.mImageUniformRange = RangeUI(low, high);
2573
2574 // If uniform is a image type, insert it into the mImageBindings array.
2575 for (unsigned int imageIndex : mState.mImageUniformRange)
2576 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002577 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2578 // cannot load values into a uniform defined as an image. if declare without a
2579 // binding qualifier, any uniform image variable (include all elements of
2580 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002581 auto &imageUniform = mState.mUniforms[imageIndex];
2582 if (imageUniform.binding == -1)
2583 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002584 mState.mImageBindings.emplace_back(
2585 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002586 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002587 else
2588 {
2589 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002590 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002591 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002592 }
2593
2594 high = low;
2595
2596 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002597 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002598 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002599 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002600 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002601
2602 mState.mSamplerUniformRange = RangeUI(low, high);
2603
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002604 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002605 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002606 {
2607 const auto &samplerUniform = mState.mUniforms[samplerIndex];
Corentin Wallezf0e89be2017-11-08 14:00:32 -08002608 TextureType textureType = SamplerTypeToTextureType(samplerUniform.type);
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002609 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002610 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002611 }
2612}
2613
jchen10eaef1e52017-06-13 10:44:11 +08002614bool Program::linkAtomicCounterBuffers()
2615{
2616 for (unsigned int index : mState.mAtomicCounterUniformRange)
2617 {
2618 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002619 uniform.blockInfo.offset = uniform.offset;
2620 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2621 uniform.blockInfo.matrixStride = 0;
2622 uniform.blockInfo.isRowMajorMatrix = false;
2623
jchen10eaef1e52017-06-13 10:44:11 +08002624 bool found = false;
2625 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2626 ++bufferIndex)
2627 {
2628 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2629 if (buffer.binding == uniform.binding)
2630 {
2631 buffer.memberIndexes.push_back(index);
2632 uniform.bufferIndex = bufferIndex;
2633 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002634 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002635 break;
2636 }
2637 }
2638 if (!found)
2639 {
2640 AtomicCounterBuffer atomicCounterBuffer;
2641 atomicCounterBuffer.binding = uniform.binding;
2642 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002643 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002644 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2645 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2646 }
2647 }
2648 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
Jiawei Shao0d88ec92018-02-27 16:25:31 +08002649 // gl_Max[Vertex|Fragment|Compute|Geometry|Combined]AtomicCounterBuffers.
jchen10eaef1e52017-06-13 10:44:11 +08002650
2651 return true;
2652}
2653
Jamie Madilleb979bf2016-11-15 12:28:46 -05002654// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002655bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002656{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002657 const ContextState &data = context->getContextState();
Jiawei Shao385b3e02018-03-21 09:43:28 +08002658 Shader *vertexShader = mState.getAttachedShader(ShaderType::Vertex);
Jamie Madilleb979bf2016-11-15 12:28:46 -05002659
Geoff Lang7dd2e102014-11-10 15:19:26 -05002660 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002661 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002662 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002663
2664 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002665 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002666 {
Jamie Madillf6113162015-05-07 11:49:21 -04002667 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002668 return false;
2669 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002670
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002671 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002672
Jamie Madillc349ec02015-08-21 16:53:12 -04002673 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002674 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002675 {
Olli Etuahod2551232017-10-26 20:03:33 +03002676 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2677 // structures, so we don't need to worry about adjusting their names or generating entries
2678 // for each member/element (unlike uniforms for example).
2679 ASSERT(!attribute.isArray() && !attribute.isStruct());
2680
Jamie Madilleb979bf2016-11-15 12:28:46 -05002681 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002682 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002683 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002684 attribute.location = bindingLocation;
2685 }
2686
2687 if (attribute.location != -1)
2688 {
2689 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002690 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002691
Jamie Madill63805b42015-08-25 13:17:39 -04002692 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002693 {
Jamie Madillf6113162015-05-07 11:49:21 -04002694 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002695 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002696
2697 return false;
2698 }
2699
Jamie Madill63805b42015-08-25 13:17:39 -04002700 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002701 {
Jamie Madill63805b42015-08-25 13:17:39 -04002702 const int regLocation = attribute.location + reg;
2703 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002704
2705 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002706 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002707 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002708 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002709 // TODO(jmadill): fix aliasing on ES2
2710 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002711 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002712 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002713 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002714 return false;
2715 }
2716 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002717 else
2718 {
Jamie Madill63805b42015-08-25 13:17:39 -04002719 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002720 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002721
Jamie Madill63805b42015-08-25 13:17:39 -04002722 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002723 }
2724 }
2725 }
2726
2727 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002728 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002729 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002730 // Not set by glBindAttribLocation or by location layout qualifier
2731 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002732 {
Jamie Madill63805b42015-08-25 13:17:39 -04002733 int regs = VariableRegisterCount(attribute.type);
2734 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002735
Jamie Madill63805b42015-08-25 13:17:39 -04002736 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002737 {
Jamie Madillf6113162015-05-07 11:49:21 -04002738 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002739 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002740 }
2741
Jamie Madillc349ec02015-08-21 16:53:12 -04002742 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002743 }
2744 }
2745
Brandon Jonesc405ae72017-12-06 14:15:03 -08002746 ASSERT(mState.mAttributesTypeMask.none());
2747 ASSERT(mState.mAttributesMask.none());
2748
Jamie Madill48ef11b2016-04-27 15:21:52 -04002749 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002750 {
Jamie Madill63805b42015-08-25 13:17:39 -04002751 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002752 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002753
Jamie Madillbd159f02017-10-09 19:39:06 -04002754 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002755 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002756 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2757 mState.mActiveAttribLocationsMask.set(location);
2758 mState.mMaxActiveAttribLocation =
2759 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08002760
2761 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
2762 if (!attribute.isBuiltIn())
2763 {
2764 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
2765 location);
2766 mState.mAttributesMask.set(location);
2767 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002768 }
2769 }
2770
Geoff Lang7dd2e102014-11-10 15:19:26 -05002771 return true;
2772}
2773
Jiajia Qin729b2c62017-08-14 09:36:11 +08002774bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002775{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002776 const auto &caps = context->getCaps();
2777
Martin Radev4c4c8e72016-08-04 12:25:34 +03002778 if (mState.mAttachedComputeShader)
2779 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002780 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002781 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002782
Jiawei Shao427071e2018-03-19 09:21:37 +08002783 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002784 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002785 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2786 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002787 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002788 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002789 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002790
2791 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
Jiawei Shao427071e2018-03-19 09:21:37 +08002792 if (!ValidateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002793 computeShaderStorageBlocks,
2794 "Compute shader shader storage block count exceeds "
2795 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2796 infoLog))
2797 {
2798 return false;
2799 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002800 return true;
2801 }
2802
Jamie Madillbd044ed2017-06-05 12:59:21 -04002803 Shader &vertexShader = *mState.mAttachedVertexShader;
2804 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002805
Jiajia Qin729b2c62017-08-14 09:36:11 +08002806 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2807 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002808
Jiawei Shao427071e2018-03-19 09:21:37 +08002809 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002810 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002811 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2812 {
2813 return false;
2814 }
Jiawei Shao427071e2018-03-19 09:21:37 +08002815 if (!ValidateInterfaceBlocksCount(
Jiajia Qin729b2c62017-08-14 09:36:11 +08002816 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002817 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2818 infoLog))
2819 {
2820
2821 return false;
2822 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002823
Jiawei Shao427071e2018-03-19 09:21:37 +08002824 Shader *geometryShader = mState.mAttachedGeometryShader;
2825 if (geometryShader)
2826 {
2827 const auto &geometryUniformBlocks = geometryShader->getUniformBlocks(context);
2828 if (!ValidateInterfaceBlocksCount(
2829 caps.maxGeometryUniformBlocks, geometryUniformBlocks,
2830 "Geometry shader uniform block count exceeds GL_MAX_GEOMETRY_UNIFORM_BLOCKS_EXT (",
2831 infoLog))
2832 {
2833 return false;
2834 }
2835 }
2836
2837 // TODO(jiawei.shao@intel.com): validate geometry shader uniform blocks.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002838 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao73618602017-12-20 15:47:15 +08002839 if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog,
Jiajia Qin8efd1262017-12-19 09:32:55 +08002840 webglCompatibility, sh::BlockType::BLOCK_UNIFORM,
2841 caps.maxCombinedUniformBlocks))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002842 {
2843 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002844 }
Jamie Madille473dee2015-08-18 14:49:01 -04002845
Jiajia Qin729b2c62017-08-14 09:36:11 +08002846 if (context->getClientVersion() >= Version(3, 1))
2847 {
2848 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2849 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2850
Jiawei Shao427071e2018-03-19 09:21:37 +08002851 if (!ValidateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002852 vertexShaderStorageBlocks,
2853 "Vertex shader shader storage block count exceeds "
2854 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2855 infoLog))
2856 {
2857 return false;
2858 }
Jiawei Shao427071e2018-03-19 09:21:37 +08002859 if (!ValidateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
Jiajia Qin729b2c62017-08-14 09:36:11 +08002860 fragmentShaderStorageBlocks,
2861 "Fragment shader shader storage block count exceeds "
2862 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2863 infoLog))
2864 {
2865
2866 return false;
2867 }
2868
Jiawei Shao427071e2018-03-19 09:21:37 +08002869 if (geometryShader)
2870 {
2871 const auto &geometryShaderStorageBlocks =
2872 geometryShader->getShaderStorageBlocks(context);
2873 if (!ValidateInterfaceBlocksCount(caps.maxGeometryShaderStorageBlocks,
2874 geometryShaderStorageBlocks,
2875 "Geometry shader shader storage block count exceeds "
2876 "GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS_EXT (",
2877 infoLog))
2878 {
2879 return false;
2880 }
2881 }
2882
2883 // TODO(jiawei.shao@intel.com): validate geometry shader shader storage blocks.
Jiajia Qin8efd1262017-12-19 09:32:55 +08002884 if (!ValidateGraphicsInterfaceBlocks(
2885 vertexShaderStorageBlocks, fragmentShaderStorageBlocks, infoLog, webglCompatibility,
2886 sh::BlockType::BLOCK_BUFFER, caps.maxCombinedShaderStorageBlocks))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002887 {
2888 return false;
2889 }
2890 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002891 return true;
2892}
2893
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002894LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
2895 const sh::ShaderVariable &variable2,
2896 bool validatePrecision,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002897 bool validateArraySize,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002898 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002899{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002900 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002901 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002902 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002903 }
Jiawei Shaod063aff2018-02-22 10:19:09 +08002904 if (validateArraySize && variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002905 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002906 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002907 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002908 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002909 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002910 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002911 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002912 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04002913 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002914 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04002915 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002916
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002917 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002918 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002919 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002920 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002921 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002922 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2923 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002924 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
2925 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002926
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002927 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002928 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002929 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002930 }
2931
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002932 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
Jiawei Shaod063aff2018-02-22 10:19:09 +08002933 member1, member2, validatePrecision, true, mismatchedStructOrBlockMemberName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002934 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002935 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002936 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
2937 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002938 }
2939 }
2940
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002941 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002942}
2943
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002944LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
2945 const sh::Varying &inputVarying,
2946 int shaderVersion,
Jiawei Shaod063aff2018-02-22 10:19:09 +08002947 bool validateGeometryShaderInputVarying,
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002948 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002949{
Jiawei Shaod063aff2018-02-22 10:19:09 +08002950 if (validateGeometryShaderInputVarying)
2951 {
2952 // [GL_EXT_geometry_shader] Section 11.1gs.4.3:
2953 // The OpenGL ES Shading Language doesn't support multi-dimensional arrays as shader inputs
2954 // or outputs.
2955 ASSERT(inputVarying.arraySizes.size() == 1u);
2956
2957 // Geometry shader input varyings are not treated as arrays, so a vertex array output
2958 // varying cannot match a geometry shader input varying.
2959 // [GL_EXT_geometry_shader] Section 7.4.1:
2960 // Geometry shader per-vertex input variables and blocks are required to be declared as
2961 // arrays, with each element representing input or output values for a single vertex of a
2962 // multi-vertex primitive. For the purposes of interface matching, such variables and blocks
2963 // are treated as though they were not declared as arrays.
2964 if (outputVarying.isArray())
2965 {
2966 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
2967 }
2968 }
2969
2970 // Skip the validation on the array sizes between a vertex output varying and a geometry input
2971 // varying as it has been done before.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002972 LinkMismatchError linkError =
Jiawei Shaod063aff2018-02-22 10:19:09 +08002973 LinkValidateVariablesBase(outputVarying, inputVarying, false,
2974 !validateGeometryShaderInputVarying, mismatchedStructFieldName);
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002975 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002976 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002977 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002978 }
2979
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002980 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002981 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002982 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002983 }
2984
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002985 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002986 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002987 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002988 }
2989
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002990 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002991}
2992
Jamie Madillbd044ed2017-06-05 12:59:21 -04002993bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002994{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002995 Shader *vertexShader = mState.mAttachedVertexShader;
2996 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002997 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2998 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002999 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05003000
3001 if (shaderVersion != 100)
3002 {
3003 // Only ESSL 1.0 has restrictions on matching input and output invariance
3004 return true;
3005 }
3006
3007 bool glPositionIsInvariant = false;
3008 bool glPointSizeIsInvariant = false;
3009 bool glFragCoordIsInvariant = false;
3010 bool glPointCoordIsInvariant = false;
3011
3012 for (const sh::Varying &varying : vertexVaryings)
3013 {
3014 if (!varying.isBuiltIn())
3015 {
3016 continue;
3017 }
3018 if (varying.name.compare("gl_Position") == 0)
3019 {
3020 glPositionIsInvariant = varying.isInvariant;
3021 }
3022 else if (varying.name.compare("gl_PointSize") == 0)
3023 {
3024 glPointSizeIsInvariant = varying.isInvariant;
3025 }
3026 }
3027
3028 for (const sh::Varying &varying : fragmentVaryings)
3029 {
3030 if (!varying.isBuiltIn())
3031 {
3032 continue;
3033 }
3034 if (varying.name.compare("gl_FragCoord") == 0)
3035 {
3036 glFragCoordIsInvariant = varying.isInvariant;
3037 }
3038 else if (varying.name.compare("gl_PointCoord") == 0)
3039 {
3040 glPointCoordIsInvariant = varying.isInvariant;
3041 }
3042 }
3043
3044 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
3045 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
3046 // Not requiring invariance to match is supported by:
3047 // dEQP, WebGL CTS, Nexus 5X GLES
3048 if (glFragCoordIsInvariant && !glPositionIsInvariant)
3049 {
3050 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
3051 "declared invariant.";
3052 return false;
3053 }
3054 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
3055 {
3056 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
3057 "declared invariant.";
3058 return false;
3059 }
3060
3061 return true;
3062}
3063
jchen10a9042d32017-03-17 08:50:45 +08003064bool Program::linkValidateTransformFeedback(const gl::Context *context,
3065 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05003066 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04003067 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05003068{
Geoff Lang7dd2e102014-11-10 15:19:26 -05003069
jchen108225e732017-11-14 16:29:03 +08003070 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04003071 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04003072 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05003073 {
jchen10a9042d32017-03-17 08:50:45 +08003074 if (context->getClientVersion() < Version(3, 1) &&
3075 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04003076 {
Geoff Lang1a683462015-09-29 15:09:59 -04003077 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04003078 return false;
3079 }
jchen108225e732017-11-14 16:29:03 +08003080 if (context->getClientVersion() >= Version(3, 1))
3081 {
3082 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
3083 {
3084 infoLog << "Two transform feedback varyings include the same array element ("
3085 << tfVaryingName << ").";
3086 return false;
3087 }
3088 }
3089 else
3090 {
3091 if (uniqueNames.count(tfVaryingName) > 0)
3092 {
3093 infoLog << "Two transform feedback varyings specify the same output variable ("
3094 << tfVaryingName << ").";
3095 return false;
3096 }
3097 }
3098 uniqueNames.insert(tfVaryingName);
3099 }
3100
3101 // Validate against program varyings.
3102 size_t totalComponents = 0;
3103 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
3104 {
3105 std::vector<unsigned int> subscripts;
3106 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
3107
3108 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
3109 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08003110 {
3111 infoLog << "Transform feedback varying " << tfVaryingName
3112 << " does not exist in the vertex shader.";
3113 return false;
3114 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003115
jchen108225e732017-11-14 16:29:03 +08003116 // Validate the matching variable.
3117 if (var->isStruct())
3118 {
3119 infoLog << "Struct cannot be captured directly (" << baseName << ").";
3120 return false;
3121 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003122
jchen108225e732017-11-14 16:29:03 +08003123 size_t elementCount = 0;
3124 size_t componentCount = 0;
3125
3126 if (var->isArray())
3127 {
3128 if (context->getClientVersion() < Version(3, 1))
3129 {
3130 infoLog << "Capture of arrays is undefined and not supported.";
3131 return false;
3132 }
3133
3134 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
3135 ASSERT(!var->isArrayOfArrays());
3136
3137 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
3138 {
3139 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
3140 return false;
3141 }
3142 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
3143 }
3144 else
3145 {
3146 if (!subscripts.empty())
3147 {
3148 infoLog << "Varying '" << baseName
3149 << "' is not an array to be captured by element.";
3150 return false;
3151 }
3152 elementCount = 1;
3153 }
3154
3155 // TODO(jmadill): Investigate implementation limits on D3D11
3156 componentCount = VariableComponentCount(var->type) * elementCount;
3157 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
3158 componentCount > caps.maxTransformFeedbackSeparateComponents)
3159 {
3160 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
3161 << componentCount << ") exceed the maximum separate components ("
3162 << caps.maxTransformFeedbackSeparateComponents << ").";
3163 return false;
3164 }
3165
3166 totalComponents += componentCount;
3167 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
3168 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
3169 {
3170 infoLog << "Transform feedback varying total components (" << totalComponents
3171 << ") exceed the maximum interleaved components ("
3172 << caps.maxTransformFeedbackInterleavedComponents << ").";
3173 return false;
3174 }
3175 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05003176 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05003177}
3178
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003179bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
3180{
3181 const std::vector<sh::Uniform> &vertexUniforms =
3182 mState.mAttachedVertexShader->getUniforms(context);
3183 const std::vector<sh::Uniform> &fragmentUniforms =
3184 mState.mAttachedFragmentShader->getUniforms(context);
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003185 const std::vector<sh::Uniform> *geometryUniforms =
3186 (mState.mAttachedGeometryShader) ? &mState.mAttachedGeometryShader->getUniforms(context)
3187 : nullptr;
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003188 const std::vector<sh::Attribute> &attributes =
3189 mState.mAttachedVertexShader->getActiveAttributes(context);
3190 for (const auto &attrib : attributes)
3191 {
3192 for (const auto &uniform : vertexUniforms)
3193 {
3194 if (uniform.name == attrib.name)
3195 {
3196 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3197 return false;
3198 }
3199 }
3200 for (const auto &uniform : fragmentUniforms)
3201 {
3202 if (uniform.name == attrib.name)
3203 {
3204 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3205 return false;
3206 }
3207 }
Jiawei Shao0d88ec92018-02-27 16:25:31 +08003208 if (geometryUniforms)
3209 {
3210 for (const auto &uniform : *geometryUniforms)
3211 {
3212 if (uniform.name == attrib.name)
3213 {
3214 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
3215 return false;
3216 }
3217 }
3218 }
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04003219 }
3220 return true;
3221}
3222
Jamie Madill3c1da042017-11-27 18:33:40 -05003223void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003224{
3225 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08003226 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04003227 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003228 {
Olli Etuahoc8538042017-09-27 11:20:15 +03003229 std::vector<unsigned int> subscripts;
3230 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08003231 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03003232 if (!subscripts.empty())
3233 {
3234 subscript = subscripts.back();
3235 }
Jamie Madill192745a2016-12-22 15:58:21 -05003236 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003237 {
Jamie Madill192745a2016-12-22 15:58:21 -05003238 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08003239 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04003240 {
jchen10a9042d32017-03-17 08:50:45 +08003241 mState.mLinkedTransformFeedbackVaryings.emplace_back(
3242 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04003243 break;
3244 }
jchen108225e732017-11-14 16:29:03 +08003245 else if (varying->isStruct())
3246 {
3247 const auto *field = FindShaderVarField(*varying, tfVaryingName);
3248 if (field != nullptr)
3249 {
3250 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
3251 break;
3252 }
3253 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04003254 }
3255 }
James Darpinian30b604d2018-03-12 17:26:57 -07003256 mState.updateTransformFeedbackStrides();
Jamie Madillccdf74b2015-08-18 10:46:12 -04003257}
3258
Jamie Madill3c1da042017-11-27 18:33:40 -05003259ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04003260{
Jamie Madill3c1da042017-11-27 18:33:40 -05003261 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003262
Jiawei Shao3d404882017-10-16 13:30:48 +08003263 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003264 {
Jamie Madill192745a2016-12-22 15:58:21 -05003265 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04003266 }
3267
Jiawei Shao3d404882017-10-16 13:30:48 +08003268 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04003269 {
Jamie Madill192745a2016-12-22 15:58:21 -05003270 merged[varying.name].fragment = &varying;
3271 }
3272
3273 return merged;
3274}
3275
Jamie Madillbd044ed2017-06-05 12:59:21 -04003276void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003277{
Jamie Madillbd044ed2017-06-05 12:59:21 -04003278 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04003279 ASSERT(fragmentShader != nullptr);
3280
Geoff Lange0cff192017-05-30 13:04:56 -04003281 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003282 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003283 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003284
3285 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04003286 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04003287 {
3288 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3289 outputVariable.name != "gl_FragData")
3290 {
3291 continue;
3292 }
3293
3294 unsigned int baseLocation =
3295 (outputVariable.location == -1 ? 0u
3296 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003297
3298 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3299 // structures, so we may use getBasicTypeElementCount().
3300 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3301 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003302 {
3303 const unsigned int location = baseLocation + elementIndex;
3304 if (location >= mState.mOutputVariableTypes.size())
3305 {
3306 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3307 }
Corentin Walleze7557742017-06-01 13:09:57 -04003308 ASSERT(location < mState.mActiveOutputVariables.size());
3309 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003310 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003311 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003312 }
3313 }
3314
Jamie Madill80a6fc02015-08-21 16:53:16 -04003315 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04003316 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003317 return;
3318
Jamie Madillbd044ed2017-06-05 12:59:21 -04003319 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04003320 // TODO(jmadill): any caps validation here?
3321
jchen1015015f72017-03-16 13:54:21 +08003322 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003323 outputVariableIndex++)
3324 {
jchen1015015f72017-03-16 13:54:21 +08003325 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003326
Olli Etuahod2551232017-10-26 20:03:33 +03003327 if (outputVariable.isArray())
3328 {
3329 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3330 // Resources and including [0] at the end of array variable names.
3331 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3332 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3333 }
3334
Jamie Madill80a6fc02015-08-21 16:53:16 -04003335 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3336 if (outputVariable.isBuiltIn())
3337 continue;
3338
3339 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003340 unsigned int baseLocation =
3341 (outputVariable.location == -1 ? 0u
3342 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003343
Olli Etuaho465835d2017-09-26 13:34:10 +03003344 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3345 // structures, so we may use getBasicTypeElementCount().
3346 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3347 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003348 {
Olli Etuahod2551232017-10-26 20:03:33 +03003349 const unsigned int location = baseLocation + elementIndex;
3350 if (location >= mState.mOutputLocations.size())
3351 {
3352 mState.mOutputLocations.resize(location + 1);
3353 }
3354 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003355 if (outputVariable.isArray())
3356 {
3357 mState.mOutputLocations[location] =
3358 VariableLocation(elementIndex, outputVariableIndex);
3359 }
3360 else
3361 {
3362 VariableLocation locationInfo;
3363 locationInfo.index = outputVariableIndex;
3364 mState.mOutputLocations[location] = locationInfo;
3365 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003366 }
3367 }
3368}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003369
Olli Etuaho48fed632017-03-16 12:05:30 +00003370void Program::setUniformValuesFromBindingQualifiers()
3371{
Jamie Madill982f6e02017-06-07 14:33:04 -04003372 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003373 {
3374 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3375 if (samplerUniform.binding != -1)
3376 {
Olli Etuahod2551232017-10-26 20:03:33 +03003377 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003378 ASSERT(location != -1);
3379 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003380 for (unsigned int elementIndex = 0;
3381 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003382 {
3383 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3384 }
3385 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3386 boundTextureUnits.data());
3387 }
3388 }
3389}
3390
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003391void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003392{
jchen10af713a22017-04-19 09:10:56 +08003393 // Set initial bindings from shader.
3394 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3395 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003396 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003397 bindUniformBlock(blockIndex, uniformBlock.binding);
3398 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003399}
3400
Jamie Madille7d84322017-01-10 18:21:59 -05003401void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003402 GLsizei clampedCount,
3403 const GLint *v)
3404{
Jamie Madill81c2e252017-09-09 23:32:46 -04003405 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3406 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3407 std::vector<GLuint> *boundTextureUnits =
3408 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003409
Olli Etuaho1734e172017-10-27 15:30:27 +03003410 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003411
3412 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003413 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003414}
3415
3416template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003417GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3418 GLsizei count,
3419 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003420 const T *v)
3421{
Jamie Madill134f93d2017-08-31 17:11:00 -04003422 if (count == 1)
3423 return 1;
3424
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003425 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003426
Corentin Wallez15ac5342016-11-03 17:06:39 -04003427 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3428 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003429 unsigned int remainingElements =
3430 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003431 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003432 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003433
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003434 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003435 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003436 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003437 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003438
3439 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003440}
3441
3442template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003443GLsizei Program::clampMatrixUniformCount(GLint location,
3444 GLsizei count,
3445 GLboolean transpose,
3446 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003447{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003448 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3449
Jamie Madill62d31cb2015-09-11 13:25:51 -04003450 if (!transpose)
3451 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003452 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003453 }
3454
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003455 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003456
3457 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3458 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003459 unsigned int remainingElements =
3460 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003461 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003462}
3463
Jamie Madill54164b02017-08-28 15:17:37 -04003464// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3465// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003466template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003467void Program::getUniformInternal(const Context *context,
3468 DestT *dataOut,
3469 GLint location,
3470 GLenum nativeType,
3471 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003472{
Jamie Madill54164b02017-08-28 15:17:37 -04003473 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003474 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003475 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003476 {
3477 GLint tempValue[16] = {0};
3478 mProgram->getUniformiv(context, location, tempValue);
3479 UniformStateQueryCastLoop<GLboolean>(
3480 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003481 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003482 }
3483 case GL_INT:
3484 {
3485 GLint tempValue[16] = {0};
3486 mProgram->getUniformiv(context, location, tempValue);
3487 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3488 components);
3489 break;
3490 }
3491 case GL_UNSIGNED_INT:
3492 {
3493 GLuint tempValue[16] = {0};
3494 mProgram->getUniformuiv(context, location, tempValue);
3495 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3496 components);
3497 break;
3498 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003499 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003500 {
3501 GLfloat tempValue[16] = {0};
3502 mProgram->getUniformfv(context, location, tempValue);
3503 UniformStateQueryCastLoop<GLfloat>(
3504 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003505 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003506 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003507 default:
3508 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003509 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003510 }
3511}
Jamie Madilla4595b82017-01-11 17:36:34 -05003512
3513bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3514{
3515 // Must be called after samplers are validated.
3516 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3517
3518 for (const auto &binding : mState.mSamplerBindings)
3519 {
Corentin Wallezf0e89be2017-11-08 14:00:32 -08003520 TextureType textureType = binding.textureType;
Jamie Madilla4595b82017-01-11 17:36:34 -05003521 for (const auto &unit : binding.boundTextureUnits)
3522 {
3523 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3524 if (programTextureID == textureID)
3525 {
3526 // TODO(jmadill): Check for appropriate overlap.
3527 return true;
3528 }
3529 }
3530 }
3531
3532 return false;
3533}
3534
Jamie Madilla2c74982016-12-12 11:20:42 -05003535} // namespace gl