blob: c756a920f4fc408c26f161480bd18d079cf7eb10 [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
Jiajia Qin729b2c62017-08-14 09:36:11 +0800208bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
209 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
210 const std::string &errorMessage,
211 InfoLog &infoLog)
212{
213 GLuint blockCount = 0;
214 for (const sh::InterfaceBlock &block : interfaceBlocks)
215 {
216 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
217 {
218 blockCount += (block.arraySize ? block.arraySize : 1);
219 if (blockCount > maxInterfaceBlocks)
220 {
221 infoLog << errorMessage << maxInterfaceBlocks << ")";
222 return false;
223 }
224 }
225 }
226 return true;
227}
228
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800229GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
230{
231 std::vector<unsigned int> subscripts;
232 std::string baseName = ParseResourceName(name, &subscripts);
233
234 unsigned int numBlocks = static_cast<unsigned int>(list.size());
235 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
236 {
237 const auto &block = list[blockIndex];
238 if (block.name == baseName)
239 {
240 const bool arrayElementZero =
241 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
242 const bool arrayElementMatches =
243 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
244 if (arrayElementMatches || arrayElementZero)
245 {
246 return blockIndex;
247 }
248 }
249 }
250
251 return GL_INVALID_INDEX;
252}
253
254void GetInterfaceBlockName(const GLuint index,
255 const std::vector<InterfaceBlock> &list,
256 GLsizei bufSize,
257 GLsizei *length,
258 GLchar *name)
259{
260 ASSERT(index < list.size());
261
262 const auto &block = list[index];
263
264 if (bufSize > 0)
265 {
266 std::string blockName = block.name;
267
268 if (block.isArray)
269 {
270 blockName += ArrayString(block.arrayElement);
271 }
272 CopyStringToBuffer(name, blockName, bufSize, length);
273 }
274}
275
Jamie Madillc9727f32017-11-07 12:37:07 -0500276void InitUniformBlockLinker(const gl::Context *context,
277 const ProgramState &state,
278 UniformBlockLinker *blockLinker)
279{
280 if (state.getAttachedVertexShader())
281 {
282 blockLinker->addShaderBlocks(GL_VERTEX_SHADER,
283 &state.getAttachedVertexShader()->getUniformBlocks(context));
284 }
285
286 if (state.getAttachedFragmentShader())
287 {
288 blockLinker->addShaderBlocks(GL_FRAGMENT_SHADER,
289 &state.getAttachedFragmentShader()->getUniformBlocks(context));
290 }
291
292 if (state.getAttachedComputeShader())
293 {
294 blockLinker->addShaderBlocks(GL_COMPUTE_SHADER,
295 &state.getAttachedComputeShader()->getUniformBlocks(context));
296 }
297}
298
299void InitShaderStorageBlockLinker(const gl::Context *context,
300 const ProgramState &state,
301 ShaderStorageBlockLinker *blockLinker)
302{
303 if (state.getAttachedVertexShader())
304 {
305 blockLinker->addShaderBlocks(
306 GL_VERTEX_SHADER, &state.getAttachedVertexShader()->getShaderStorageBlocks(context));
307 }
308
309 if (state.getAttachedFragmentShader())
310 {
311 blockLinker->addShaderBlocks(
312 GL_FRAGMENT_SHADER,
313 &state.getAttachedFragmentShader()->getShaderStorageBlocks(context));
314 }
315
316 if (state.getAttachedComputeShader())
317 {
318 blockLinker->addShaderBlocks(
319 GL_COMPUTE_SHADER, &state.getAttachedComputeShader()->getShaderStorageBlocks(context));
320 }
321}
322
jchen108225e732017-11-14 16:29:03 +0800323// Find the matching varying or field by name.
324const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
325 const std::string &name)
326{
327 const sh::ShaderVariable *var = nullptr;
328 for (const auto &ref : varyings)
329 {
330 const sh::Varying *varying = ref.second.get();
331 if (varying->name == name)
332 {
333 var = varying;
334 break;
335 }
336 var = FindShaderVarField(*varying, name);
337 if (var != nullptr)
338 {
339 break;
340 }
341 }
342 return var;
343}
344
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800345void AddParentPrefix(const std::string &parentName, std::string *mismatchedFieldName)
346{
347 ASSERT(mismatchedFieldName);
348 if (mismatchedFieldName->empty())
349 {
350 *mismatchedFieldName = parentName;
351 }
352 else
353 {
354 std::ostringstream stream;
355 stream << parentName << "." << *mismatchedFieldName;
356 *mismatchedFieldName = stream.str();
357 }
358}
359
360const char *GetLinkMismatchErrorString(LinkMismatchError linkError)
361{
362 switch (linkError)
363 {
364 case LinkMismatchError::TYPE_MISMATCH:
365 return "Type";
366 case LinkMismatchError::ARRAY_SIZE_MISMATCH:
367 return "Array size";
368 case LinkMismatchError::PRECISION_MISMATCH:
369 return "Precision";
370 case LinkMismatchError::STRUCT_NAME_MISMATCH:
371 return "Structure name";
372 case LinkMismatchError::FIELD_NUMBER_MISMATCH:
373 return "Field number";
374 case LinkMismatchError::FIELD_NAME_MISMATCH:
375 return "Field name";
376
377 case LinkMismatchError::INTERPOLATION_TYPE_MISMATCH:
378 return "Interpolation type";
379 case LinkMismatchError::INVARIANCE_MISMATCH:
380 return "Invariance";
381
382 case LinkMismatchError::BINDING_MISMATCH:
383 return "Binding layout qualifier";
384 case LinkMismatchError::LOCATION_MISMATCH:
385 return "Location layout qualifier";
386 case LinkMismatchError::OFFSET_MISMATCH:
387 return "Offset layout qualilfier";
388
389 case LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH:
390 return "Layout qualifier";
391 case LinkMismatchError::MATRIX_PACKING_MISMATCH:
392 return "Matrix Packing";
393 default:
394 UNREACHABLE();
395 return "";
396 }
397}
398
Jamie Madill62d31cb2015-09-11 13:25:51 -0400399} // anonymous namespace
400
Jamie Madill4a3c2342015-10-08 12:58:45 -0400401const char *const g_fakepath = "C:\\fakepath";
402
Jamie Madill3c1da042017-11-27 18:33:40 -0500403// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400404InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000405{
406}
407
408InfoLog::~InfoLog()
409{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000410}
411
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400412size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000413{
Jamie Madill23176ce2017-07-31 14:14:33 -0400414 if (!mLazyStream)
415 {
416 return 0;
417 }
418
419 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400420 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000421}
422
Geoff Lange1a27752015-10-05 13:16:04 -0400423void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000424{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400425 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000426
427 if (bufSize > 0)
428 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400429 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400430
Jamie Madill23176ce2017-07-31 14:14:33 -0400431 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000432 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400433 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
434 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000435 }
436
437 infoLog[index] = '\0';
438 }
439
440 if (length)
441 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400442 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000443 }
444}
445
446// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300447// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000448// messages, so lets remove all occurrences of this fake file path from the log.
449void InfoLog::appendSanitized(const char *message)
450{
Jamie Madill23176ce2017-07-31 14:14:33 -0400451 ensureInitialized();
452
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000453 std::string msg(message);
454
455 size_t found;
456 do
457 {
458 found = msg.find(g_fakepath);
459 if (found != std::string::npos)
460 {
461 msg.erase(found, strlen(g_fakepath));
462 }
463 }
464 while (found != std::string::npos);
465
Jamie Madill23176ce2017-07-31 14:14:33 -0400466 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000467}
468
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000469void InfoLog::reset()
470{
Jiawei Shao02f15232017-12-27 10:10:28 +0800471 if (mLazyStream)
472 {
473 mLazyStream.reset(nullptr);
474 }
475}
476
477bool InfoLog::empty() const
478{
479 if (!mLazyStream)
480 {
481 return true;
482 }
483
484 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000485}
486
Jiawei Shao881b7bf2017-12-25 11:18:37 +0800487void LogLinkMismatch(InfoLog &infoLog,
488 const std::string &variableName,
489 const char *variableType,
490 LinkMismatchError linkError,
491 const std::string &mismatchedStructOrBlockFieldName,
492 GLenum shaderType1,
493 GLenum shaderType2)
494{
495 std::ostringstream stream;
496 stream << GetLinkMismatchErrorString(linkError) << "s of " << variableType << " '"
497 << variableName;
498
499 if (!mismatchedStructOrBlockFieldName.empty())
500 {
501 stream << "' member '" << variableName << "." << mismatchedStructOrBlockFieldName;
502 }
503
504 stream << "' differ between " << GetShaderTypeString(shaderType1) << " and "
505 << GetShaderTypeString(shaderType2) << " shaders.";
506
507 infoLog << stream.str();
508}
509
Jamie Madill3c1da042017-11-27 18:33:40 -0500510// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300511VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000512{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500513}
514
Olli Etuahoc8538042017-09-27 11:20:15 +0300515VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300516 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500517{
Olli Etuahoc8538042017-09-27 11:20:15 +0300518 ASSERT(arrayIndex != GL_INVALID_INDEX);
519}
520
Jamie Madill3c1da042017-11-27 18:33:40 -0500521// SamplerBindings implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500522SamplerBinding::SamplerBinding(GLenum textureTypeIn, size_t elementCount, bool unreferenced)
523 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
524{
525}
526
527SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
528
529SamplerBinding::~SamplerBinding() = default;
530
Jamie Madill3c1da042017-11-27 18:33:40 -0500531// ProgramBindings implementation.
532ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500533{
534}
535
Jamie Madill3c1da042017-11-27 18:33:40 -0500536ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500537{
538}
539
Jamie Madill3c1da042017-11-27 18:33:40 -0500540void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400541{
542 mBindings[name] = index;
543}
544
Jamie Madill3c1da042017-11-27 18:33:40 -0500545int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400546{
547 auto iter = mBindings.find(name);
548 return (iter != mBindings.end()) ? iter->second : -1;
549}
550
Jamie Madill3c1da042017-11-27 18:33:40 -0500551ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400552{
553 return mBindings.begin();
554}
555
Jamie Madill3c1da042017-11-27 18:33:40 -0500556ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400557{
558 return mBindings.end();
559}
560
Jamie Madill3c1da042017-11-27 18:33:40 -0500561// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500562ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
563{
564}
565ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
566{
567 for (size_t index = 0; index < count; ++index)
568 {
569 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
570 }
571}
572
573ImageBinding::ImageBinding(const ImageBinding &other) = default;
574
575ImageBinding::~ImageBinding() = default;
576
Jamie Madill3c1da042017-11-27 18:33:40 -0500577// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400578ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500579 : mLabel(),
580 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400581 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300582 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800583 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500584 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400585 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500586 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800587 mImageUniformRange(0, 0),
588 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300589 mBinaryRetrieveableHint(false),
590 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400591{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300592 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400593}
594
Jamie Madill48ef11b2016-04-27 15:21:52 -0400595ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400596{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800597 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
598 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400599}
600
Jamie Madill48ef11b2016-04-27 15:21:52 -0400601const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500602{
603 return mLabel;
604}
605
Jamie Madille7d84322017-01-10 18:21:59 -0500606GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400607{
jchen1015015f72017-03-16 13:54:21 +0800608 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400609}
610
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800611GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
612{
613 return GetResourceIndexFromName(mBufferVariables, name);
614}
615
Jamie Madille7d84322017-01-10 18:21:59 -0500616GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
617{
618 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
619 return mUniformLocations[location].index;
620}
621
622Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
623{
624 GLuint index = getUniformIndexFromLocation(location);
625 if (!isSamplerUniformIndex(index))
626 {
627 return Optional<GLuint>::Invalid();
628 }
629
630 return getSamplerIndexFromUniformIndex(index);
631}
632
633bool ProgramState::isSamplerUniformIndex(GLuint index) const
634{
Jamie Madill982f6e02017-06-07 14:33:04 -0400635 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500636}
637
638GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
639{
640 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400641 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500642}
643
Jamie Madill34ca4f52017-06-13 11:49:39 -0400644GLuint ProgramState::getAttributeLocation(const std::string &name) const
645{
646 for (const sh::Attribute &attribute : mAttributes)
647 {
648 if (attribute.name == name)
649 {
650 return attribute.location;
651 }
652 }
653
654 return static_cast<GLuint>(-1);
655}
656
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500657Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400658 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400659 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500660 mLinked(false),
661 mDeleteStatus(false),
662 mRefCount(0),
663 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500664 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500665{
666 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000667
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
671Program::~Program()
672{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400673 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674}
675
Jamie Madill4928b7c2017-06-20 12:57:39 -0400676void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500677{
678 if (mState.mAttachedVertexShader != nullptr)
679 {
680 mState.mAttachedVertexShader->release(context);
681 mState.mAttachedVertexShader = nullptr;
682 }
683
684 if (mState.mAttachedFragmentShader != nullptr)
685 {
686 mState.mAttachedFragmentShader->release(context);
687 mState.mAttachedFragmentShader = nullptr;
688 }
689
690 if (mState.mAttachedComputeShader != nullptr)
691 {
692 mState.mAttachedComputeShader->release(context);
693 mState.mAttachedComputeShader = nullptr;
694 }
695
Jiawei Shao89be29a2017-11-06 14:36:45 +0800696 if (mState.mAttachedGeometryShader != nullptr)
697 {
698 mState.mAttachedGeometryShader->release(context);
699 mState.mAttachedGeometryShader = nullptr;
700 }
701
Jamie Madillc564c072017-06-01 12:45:42 -0400702 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400703
704 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800705 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400706 SafeDelete(mProgram);
707
708 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500709}
710
Geoff Lang70d0f492015-12-10 17:45:46 -0500711void Program::setLabel(const std::string &label)
712{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400713 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500714}
715
716const std::string &Program::getLabel() const
717{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400718 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500719}
720
Jamie Madillef300b12016-10-07 15:12:09 -0400721void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300723 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300725 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726 {
Jamie Madillef300b12016-10-07 15:12:09 -0400727 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300728 mState.mAttachedVertexShader = shader;
729 mState.mAttachedVertexShader->addRef();
730 break;
731 }
732 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733 {
Jamie Madillef300b12016-10-07 15:12:09 -0400734 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300735 mState.mAttachedFragmentShader = shader;
736 mState.mAttachedFragmentShader->addRef();
737 break;
738 }
739 case GL_COMPUTE_SHADER:
740 {
Jamie Madillef300b12016-10-07 15:12:09 -0400741 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300742 mState.mAttachedComputeShader = shader;
743 mState.mAttachedComputeShader->addRef();
744 break;
745 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800746 case GL_GEOMETRY_SHADER_EXT:
747 {
748 ASSERT(!mState.mAttachedGeometryShader);
749 mState.mAttachedGeometryShader = shader;
750 mState.mAttachedGeometryShader->addRef();
751 break;
752 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753 default:
754 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756}
757
Jamie Madillc1d770e2017-04-13 17:31:24 -0400758void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300760 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300762 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400764 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500765 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300766 mState.mAttachedVertexShader = nullptr;
767 break;
768 }
769 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400771 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500772 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300773 mState.mAttachedFragmentShader = nullptr;
774 break;
775 }
776 case GL_COMPUTE_SHADER:
777 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400778 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500779 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300780 mState.mAttachedComputeShader = nullptr;
781 break;
782 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800783 case GL_GEOMETRY_SHADER_EXT:
784 {
785 ASSERT(mState.mAttachedGeometryShader == shader);
786 shader->release(context);
787 mState.mAttachedGeometryShader = nullptr;
788 break;
789 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300790 default:
791 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000795int Program::getAttachedShadersCount() const
796{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300797 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800798 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000799}
800
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801void Program::bindAttributeLocation(GLuint index, const char *name)
802{
Geoff Langd8605522016-04-13 10:19:12 -0400803 mAttributeBindings.bindLocation(index, name);
804}
805
806void Program::bindUniformLocation(GLuint index, const char *name)
807{
Olli Etuahod2551232017-10-26 20:03:33 +0300808 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
Sami Väisänen46eaa942016-06-29 10:26:37 +0300811void Program::bindFragmentInputLocation(GLint index, const char *name)
812{
813 mFragmentInputBindings.bindLocation(index, name);
814}
815
Jamie Madillbd044ed2017-06-05 12:59:21 -0400816BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300817{
818 BindingInfo ret;
819 ret.type = GL_NONE;
820 ret.valid = false;
821
Jamie Madillbd044ed2017-06-05 12:59:21 -0400822 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300823 ASSERT(fragmentShader);
824
825 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800826 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300827
828 for (const auto &binding : mFragmentInputBindings)
829 {
830 if (binding.second != static_cast<GLuint>(index))
831 continue;
832
833 ret.valid = true;
834
Olli Etuahod2551232017-10-26 20:03:33 +0300835 size_t nameLengthWithoutArrayIndex;
836 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300837
838 for (const auto &in : inputs)
839 {
Olli Etuahod2551232017-10-26 20:03:33 +0300840 if (in.name.length() == nameLengthWithoutArrayIndex &&
841 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300842 {
843 if (in.isArray())
844 {
845 // The client wants to bind either "name" or "name[0]".
846 // GL ES 3.1 spec refers to active array names with language such as:
847 // "if the string identifies the base name of an active array, where the
848 // string would exactly match the name of the variable if the suffix "[0]"
849 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400850 if (arrayIndex == GL_INVALID_INDEX)
851 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300852
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400853 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300854 }
855 else
856 {
857 ret.name = in.mappedName;
858 }
859 ret.type = in.type;
860 return ret;
861 }
862 }
863 }
864
865 return ret;
866}
867
Jamie Madillbd044ed2017-06-05 12:59:21 -0400868void Program::pathFragmentInputGen(const Context *context,
869 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300870 GLenum genMode,
871 GLint components,
872 const GLfloat *coeffs)
873{
874 // If the location is -1 then the command is silently ignored
875 if (index == -1)
876 return;
877
Jamie Madillbd044ed2017-06-05 12:59:21 -0400878 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300879
880 // If the input doesn't exist then then the command is silently ignored
881 // This could happen through optimization for example, the shader translator
882 // decides that a variable is not actually being used and optimizes it away.
883 if (binding.name.empty())
884 return;
885
886 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
887}
888
Martin Radev4c4c8e72016-08-04 12:25:34 +0300889// The attached shaders are checked for linking errors by matching up their variables.
890// Uniform, input and output variables get collected.
891// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500892Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000893{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500894 const auto &data = context->getContextState();
895
Jamie Madill6c58b062017-08-01 13:44:25 -0400896 auto *platform = ANGLEPlatformCurrent();
897 double startTime = platform->currentTime(platform);
898
Jamie Madill6c1f6712017-02-14 19:08:04 -0500899 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000900
Jamie Madill32447362017-06-28 14:53:52 -0400901 ProgramHash programHash;
902 auto *cache = context->getMemoryProgramCache();
903 if (cache)
904 {
905 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400906 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400907 }
908
909 if (mLinked)
910 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400911 double delta = platform->currentTime(platform) - startTime;
912 int us = static_cast<int>(delta * 1000000.0);
913 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400914 return NoError();
915 }
916
917 // Cache load failed, fall through to normal linking.
918 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000919 mInfoLog.reset();
920
Jiawei Shao73618602017-12-20 15:47:15 +0800921 if (!linkValidateShaders(context, mInfoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -0500922 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300923 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400924 }
925
Jiawei Shao73618602017-12-20 15:47:15 +0800926 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500927 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400928 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300929 {
930 return NoError();
931 }
932
Jiajia Qin729b2c62017-08-14 09:36:11 +0800933 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300934 {
935 return NoError();
936 }
937
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800938 ProgramLinkedResources resources = {
939 {0, PackMode::ANGLE_RELAXED},
940 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +0800941 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
942 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500943
944 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
945 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
946
947 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500948 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300949 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500950 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300951 }
952 }
953 else
954 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400955 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300956 {
957 return NoError();
958 }
959
Jamie Madillbd044ed2017-06-05 12:59:21 -0400960 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300961 {
962 return NoError();
963 }
964
Jamie Madillbd044ed2017-06-05 12:59:21 -0400965 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300966 {
967 return NoError();
968 }
969
Jiajia Qin729b2c62017-08-14 09:36:11 +0800970 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300971 {
972 return NoError();
973 }
974
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400975 if (!linkValidateGlobalNames(context, mInfoLog))
976 {
977 return NoError();
978 }
979
Jamie Madillbd044ed2017-06-05 12:59:21 -0400980 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300981
Jiawei Shao73618602017-12-20 15:47:15 +0800982 ASSERT(mState.mAttachedVertexShader);
983 mState.mNumViews = mState.mAttachedVertexShader->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +0300984
Jamie Madillbd044ed2017-06-05 12:59:21 -0400985 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300986
Jamie Madill192745a2016-12-22 15:58:21 -0500987 // Map the varyings to the register file
988 // In WebGL, we use a slightly different handling for packing variables.
989 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
990 : PackMode::ANGLE_RELAXED;
Jamie Madillc9727f32017-11-07 12:37:07 -0500991
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800992 ProgramLinkedResources resources = {
993 {data.getCaps().maxVaryingVectors, packMode},
994 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +0800995 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
996 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500997
998 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
999 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
1000
Jiawei Shao73618602017-12-20 15:47:15 +08001001 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -05001002 {
1003 return NoError();
1004 }
1005
jchen1085c93c42017-11-12 15:36:47 +08001006 if (!resources.varyingPacking.collectAndPackUserVaryings(
1007 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +03001008 {
1009 return NoError();
1010 }
1011
Jamie Madillc9727f32017-11-07 12:37:07 -05001012 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -05001013 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001014 {
Jamie Madillb0a838b2016-11-13 20:02:12 -05001015 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001016 }
1017
1018 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -05001019 }
1020
Jamie Madill6db1c2e2017-11-08 09:17:40 -05001021 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -04001022
jchen10eaef1e52017-06-13 10:44:11 +08001023 setUniformValuesFromBindingQualifiers();
1024
Yunchao Heece12532017-11-21 15:50:21 +08001025 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
1026 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +08001027 ASSERT(mLinked);
1028 updateLinkedShaderStages();
1029
Jamie Madill54164b02017-08-28 15:17:37 -04001030 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -04001031 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -04001032
Jamie Madill32447362017-06-28 14:53:52 -04001033 // Save to the program cache.
1034 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
1035 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
1036 {
1037 cache->putProgram(programHash, context, this);
1038 }
1039
Jamie Madill6c58b062017-08-01 13:44:25 -04001040 double delta = platform->currentTime(platform) - startTime;
1041 int us = static_cast<int>(delta * 1000000.0);
1042 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
1043
Martin Radev4c4c8e72016-08-04 12:25:34 +03001044 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +00001045}
1046
Yunchao He85072e82017-11-14 15:43:28 +08001047void Program::updateLinkedShaderStages()
1048{
Yunchao Heece12532017-11-21 15:50:21 +08001049 mState.mLinkedShaderStages.reset();
1050
Yunchao He85072e82017-11-14 15:43:28 +08001051 if (mState.mAttachedVertexShader)
1052 {
1053 mState.mLinkedShaderStages.set(SHADER_VERTEX);
1054 }
1055
1056 if (mState.mAttachedFragmentShader)
1057 {
1058 mState.mLinkedShaderStages.set(SHADER_FRAGMENT);
1059 }
1060
1061 if (mState.mAttachedComputeShader)
1062 {
1063 mState.mLinkedShaderStages.set(SHADER_COMPUTE);
1064 }
1065}
1066
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +00001067// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -05001068void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001070 mState.mAttributes.clear();
Brandon Jonesc405ae72017-12-06 14:15:03 -08001071 mState.mAttributesTypeMask.reset();
1072 mState.mAttributesMask.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001073 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -04001074 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +08001075 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001076 mState.mUniforms.clear();
1077 mState.mUniformLocations.clear();
1078 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001079 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001080 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001081 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001082 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001083 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001084 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001085 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001086 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001087 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001088 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001089 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090
Geoff Lang7dd2e102014-11-10 15:19:26 -05001091 mValidated = false;
1092
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001093 mLinked = false;
1094}
1095
Geoff Lange1a27752015-10-05 13:16:04 -04001096bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001097{
1098 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
Jamie Madilla2c74982016-12-12 11:20:42 -05001101Error Program::loadBinary(const Context *context,
1102 GLenum binaryFormat,
1103 const void *binary,
1104 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001105{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001106 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001107
Geoff Lang7dd2e102014-11-10 15:19:26 -05001108#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001109 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001110#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001111 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1112 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001113 {
Jamie Madillf6113162015-05-07 11:49:21 -04001114 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001115 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001116 }
1117
Jamie Madill4f86d052017-06-05 12:59:26 -04001118 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1119 ANGLE_TRY_RESULT(
1120 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001121
1122 // Currently we require the full shader text to compute the program hash.
1123 // TODO(jmadill): Store the binary in the internal program cache.
1124
Jamie Madillb0a838b2016-11-13 20:02:12 -05001125 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001126#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001127}
1128
Jamie Madilla2c74982016-12-12 11:20:42 -05001129Error Program::saveBinary(const Context *context,
1130 GLenum *binaryFormat,
1131 void *binary,
1132 GLsizei bufSize,
1133 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001134{
1135 if (binaryFormat)
1136 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001137 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001138 }
1139
Jamie Madill4f86d052017-06-05 12:59:26 -04001140 angle::MemoryBuffer memoryBuf;
1141 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142
Jamie Madill4f86d052017-06-05 12:59:26 -04001143 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1144 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001145
1146 if (streamLength > bufSize)
1147 {
1148 if (length)
1149 {
1150 *length = 0;
1151 }
1152
1153 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1154 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1155 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001156 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157 }
1158
1159 if (binary)
1160 {
1161 char *ptr = reinterpret_cast<char*>(binary);
1162
Jamie Madill48ef11b2016-04-27 15:21:52 -04001163 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001164 ptr += streamLength;
1165
1166 ASSERT(ptr - streamLength == binary);
1167 }
1168
1169 if (length)
1170 {
1171 *length = streamLength;
1172 }
1173
He Yunchaoacd18982017-01-04 10:46:42 +08001174 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001175}
1176
Jamie Madillffe00c02017-06-27 16:26:55 -04001177GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178{
1179 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001180 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181 if (error.isError())
1182 {
1183 return 0;
1184 }
1185
1186 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001187}
1188
Geoff Langc5629752015-12-07 16:29:04 -05001189void Program::setBinaryRetrievableHint(bool retrievable)
1190{
1191 // TODO(jmadill) : replace with dirty bits
1192 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001193 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001194}
1195
1196bool Program::getBinaryRetrievableHint() const
1197{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001198 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001199}
1200
Yunchao He61afff12017-03-14 15:34:03 +08001201void Program::setSeparable(bool separable)
1202{
1203 // TODO(yunchao) : replace with dirty bits
1204 if (mState.mSeparable != separable)
1205 {
1206 mProgram->setSeparable(separable);
1207 mState.mSeparable = separable;
1208 }
1209}
1210
1211bool Program::isSeparable() const
1212{
1213 return mState.mSeparable;
1214}
1215
Jamie Madill6c1f6712017-02-14 19:08:04 -05001216void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001217{
1218 mRefCount--;
1219
1220 if (mRefCount == 0 && mDeleteStatus)
1221 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001222 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001223 }
1224}
1225
1226void Program::addRef()
1227{
1228 mRefCount++;
1229}
1230
1231unsigned int Program::getRefCount() const
1232{
1233 return mRefCount;
1234}
1235
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001236int Program::getInfoLogLength() const
1237{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001238 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001239}
1240
Geoff Lange1a27752015-10-05 13:16:04 -04001241void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001242{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001243 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001244}
1245
Geoff Lange1a27752015-10-05 13:16:04 -04001246void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001247{
1248 int total = 0;
1249
Martin Radev4c4c8e72016-08-04 12:25:34 +03001250 if (mState.mAttachedComputeShader)
1251 {
1252 if (total < maxCount)
1253 {
1254 shaders[total] = mState.mAttachedComputeShader->getHandle();
1255 total++;
1256 }
1257 }
1258
Jamie Madill48ef11b2016-04-27 15:21:52 -04001259 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001260 {
1261 if (total < maxCount)
1262 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001263 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001264 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001265 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001266 }
1267
Jamie Madill48ef11b2016-04-27 15:21:52 -04001268 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001269 {
1270 if (total < maxCount)
1271 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001272 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001273 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001274 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001275 }
1276
Jiawei Shao89be29a2017-11-06 14:36:45 +08001277 if (mState.mAttachedGeometryShader)
1278 {
1279 if (total < maxCount)
1280 {
1281 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1282 total++;
1283 }
1284 }
1285
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001286 if (count)
1287 {
1288 *count = total;
1289 }
1290}
1291
Geoff Lange1a27752015-10-05 13:16:04 -04001292GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001293{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001294 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295}
1296
Jamie Madill63805b42015-08-25 13:17:39 -04001297bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001298{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001299 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1300 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001301}
1302
jchen10fd7c3b52017-03-21 15:36:03 +08001303void Program::getActiveAttribute(GLuint index,
1304 GLsizei bufsize,
1305 GLsizei *length,
1306 GLint *size,
1307 GLenum *type,
1308 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001309{
Jamie Madillc349ec02015-08-21 16:53:12 -04001310 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001311 {
1312 if (bufsize > 0)
1313 {
1314 name[0] = '\0';
1315 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001316
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001317 if (length)
1318 {
1319 *length = 0;
1320 }
1321
1322 *type = GL_NONE;
1323 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001324 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001325 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001326
jchen1036e120e2017-03-14 14:53:58 +08001327 ASSERT(index < mState.mAttributes.size());
1328 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001329
1330 if (bufsize > 0)
1331 {
jchen10fd7c3b52017-03-21 15:36:03 +08001332 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001333 }
1334
1335 // Always a single 'type' instance
1336 *size = 1;
1337 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001338}
1339
Geoff Lange1a27752015-10-05 13:16:04 -04001340GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001341{
Jamie Madillc349ec02015-08-21 16:53:12 -04001342 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001343 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001344 return 0;
1345 }
1346
jchen1036e120e2017-03-14 14:53:58 +08001347 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001348}
1349
Geoff Lange1a27752015-10-05 13:16:04 -04001350GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001351{
Jamie Madillc349ec02015-08-21 16:53:12 -04001352 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001353 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001354 return 0;
1355 }
1356
1357 size_t maxLength = 0;
1358
Jamie Madill48ef11b2016-04-27 15:21:52 -04001359 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001360 {
jchen1036e120e2017-03-14 14:53:58 +08001361 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001362 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001363
Jamie Madillc349ec02015-08-21 16:53:12 -04001364 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365}
1366
jchen1015015f72017-03-16 13:54:21 +08001367GLuint Program::getInputResourceIndex(const GLchar *name) const
1368{
Olli Etuahod2551232017-10-26 20:03:33 +03001369 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001370}
1371
1372GLuint Program::getOutputResourceIndex(const GLchar *name) const
1373{
1374 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1375}
1376
jchen10fd7c3b52017-03-21 15:36:03 +08001377size_t Program::getOutputResourceCount() const
1378{
1379 return (mLinked ? mState.mOutputVariables.size() : 0);
1380}
1381
jchen10baf5d942017-08-28 20:45:48 +08001382template <typename T>
1383void Program::getResourceName(GLuint index,
1384 const std::vector<T> &resources,
1385 GLsizei bufSize,
1386 GLsizei *length,
1387 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001388{
1389 if (length)
1390 {
1391 *length = 0;
1392 }
1393
1394 if (!mLinked)
1395 {
1396 if (bufSize > 0)
1397 {
1398 name[0] = '\0';
1399 }
1400 return;
1401 }
jchen10baf5d942017-08-28 20:45:48 +08001402 ASSERT(index < resources.size());
1403 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001404
1405 if (bufSize > 0)
1406 {
Olli Etuahod2551232017-10-26 20:03:33 +03001407 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001408 }
1409}
1410
jchen10baf5d942017-08-28 20:45:48 +08001411void Program::getInputResourceName(GLuint index,
1412 GLsizei bufSize,
1413 GLsizei *length,
1414 GLchar *name) const
1415{
1416 getResourceName(index, mState.mAttributes, bufSize, length, name);
1417}
1418
1419void Program::getOutputResourceName(GLuint index,
1420 GLsizei bufSize,
1421 GLsizei *length,
1422 GLchar *name) const
1423{
1424 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1425}
1426
1427void Program::getUniformResourceName(GLuint index,
1428 GLsizei bufSize,
1429 GLsizei *length,
1430 GLchar *name) const
1431{
1432 getResourceName(index, mState.mUniforms, bufSize, length, name);
1433}
1434
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001435void Program::getBufferVariableResourceName(GLuint index,
1436 GLsizei bufSize,
1437 GLsizei *length,
1438 GLchar *name) const
1439{
1440 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1441}
1442
jchen10880683b2017-04-12 16:21:55 +08001443const sh::Attribute &Program::getInputResource(GLuint index) const
1444{
1445 ASSERT(index < mState.mAttributes.size());
1446 return mState.mAttributes[index];
1447}
1448
1449const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1450{
1451 ASSERT(index < mState.mOutputVariables.size());
1452 return mState.mOutputVariables[index];
1453}
1454
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455GLint Program::getFragDataLocation(const std::string &name) const
1456{
Olli Etuahod2551232017-10-26 20:03:33 +03001457 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001458}
1459
Geoff Lange1a27752015-10-05 13:16:04 -04001460void Program::getActiveUniform(GLuint index,
1461 GLsizei bufsize,
1462 GLsizei *length,
1463 GLint *size,
1464 GLenum *type,
1465 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001466{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001468 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001469 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001470 ASSERT(index < mState.mUniforms.size());
1471 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472
1473 if (bufsize > 0)
1474 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001476 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477 }
1478
Olli Etuaho465835d2017-09-26 13:34:10 +03001479 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001480 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001481 }
1482 else
1483 {
1484 if (bufsize > 0)
1485 {
1486 name[0] = '\0';
1487 }
1488
1489 if (length)
1490 {
1491 *length = 0;
1492 }
1493
1494 *size = 0;
1495 *type = GL_NONE;
1496 }
1497}
1498
Geoff Lange1a27752015-10-05 13:16:04 -04001499GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001500{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001502 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001503 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001504 }
1505 else
1506 {
1507 return 0;
1508 }
1509}
1510
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001511size_t Program::getActiveBufferVariableCount() const
1512{
1513 return mLinked ? mState.mBufferVariables.size() : 0;
1514}
1515
Geoff Lange1a27752015-10-05 13:16:04 -04001516GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001517{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001518 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001519
1520 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001521 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001522 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001524 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001526 size_t length = uniform.name.length() + 1u;
1527 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528 {
1529 length += 3; // Counting in "[0]".
1530 }
1531 maxLength = std::max(length, maxLength);
1532 }
1533 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001534 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535
Jamie Madill62d31cb2015-09-11 13:25:51 -04001536 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001537}
1538
Geoff Lang7dd2e102014-11-10 15:19:26 -05001539bool Program::isValidUniformLocation(GLint location) const
1540{
Jamie Madille2e406c2016-06-02 13:04:10 -04001541 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001542 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001543 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001544}
1545
Jamie Madill62d31cb2015-09-11 13:25:51 -04001546const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001547{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001548 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001549 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001550}
1551
Jamie Madillac4e9c32017-01-13 14:07:12 -05001552const VariableLocation &Program::getUniformLocation(GLint location) const
1553{
1554 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1555 return mState.mUniformLocations[location];
1556}
1557
1558const std::vector<VariableLocation> &Program::getUniformLocations() const
1559{
1560 return mState.mUniformLocations;
1561}
1562
1563const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1564{
1565 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1566 return mState.mUniforms[index];
1567}
1568
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001569const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1570{
1571 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1572 return mState.mBufferVariables[index];
1573}
1574
Jamie Madill62d31cb2015-09-11 13:25:51 -04001575GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576{
Olli Etuahod2551232017-10-26 20:03:33 +03001577 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001578}
1579
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001581{
Jamie Madille7d84322017-01-10 18:21:59 -05001582 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
1585void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1586{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001587 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1588 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001589 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590}
1591
1592void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1593{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001594 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1595 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001596 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001597}
1598
1599void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1600{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001601 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1602 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001603 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604}
1605
1606void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1607{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001608 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1609 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001610 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611}
1612
Jamie Madill81c2e252017-09-09 23:32:46 -04001613Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001615 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1616 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1617
Jamie Madill81c2e252017-09-09 23:32:46 -04001618 mProgram->setUniform1iv(location, clampedCount, v);
1619
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001620 if (mState.isSamplerUniformIndex(locationInfo.index))
1621 {
1622 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001623 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001624 }
1625
Jamie Madill81c2e252017-09-09 23:32:46 -04001626 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627}
1628
1629void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1630{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001631 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1632 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001633 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634}
1635
1636void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1637{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001638 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1639 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001640 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001641}
1642
1643void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1644{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001645 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1646 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001647 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648}
1649
1650void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1651{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001652 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1653 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001654 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655}
1656
1657void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1658{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001659 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1660 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001661 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662}
1663
1664void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1665{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001666 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1667 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001668 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001669}
1670
1671void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1672{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001673 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1674 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001675 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676}
1677
1678void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1679{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001680 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001681 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682}
1683
1684void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1685{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001686 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001687 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688}
1689
1690void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1691{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001692 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001693 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001694}
1695
1696void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1697{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001698 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001699 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700}
1701
1702void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1703{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001704 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001705 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706}
1707
1708void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1709{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001710 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001711 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712}
1713
1714void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1715{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001716 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001717 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718}
1719
1720void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1721{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001722 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001723 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724}
1725
1726void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1727{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001728 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001729 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730}
1731
Jamie Madill54164b02017-08-28 15:17:37 -04001732void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001733{
Jamie Madill54164b02017-08-28 15:17:37 -04001734 const auto &uniformLocation = mState.getUniformLocations()[location];
1735 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1736
1737 GLenum nativeType = gl::VariableComponentType(uniform.type);
1738 if (nativeType == GL_FLOAT)
1739 {
1740 mProgram->getUniformfv(context, location, v);
1741 }
1742 else
1743 {
1744 getUniformInternal(context, v, location, nativeType,
1745 gl::VariableComponentCount(uniform.type));
1746 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001747}
1748
Jamie Madill54164b02017-08-28 15:17:37 -04001749void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001750{
Jamie Madill54164b02017-08-28 15:17:37 -04001751 const auto &uniformLocation = mState.getUniformLocations()[location];
1752 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1753
1754 GLenum nativeType = gl::VariableComponentType(uniform.type);
1755 if (nativeType == GL_INT || nativeType == GL_BOOL)
1756 {
1757 mProgram->getUniformiv(context, location, v);
1758 }
1759 else
1760 {
1761 getUniformInternal(context, v, location, nativeType,
1762 gl::VariableComponentCount(uniform.type));
1763 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001764}
1765
Jamie Madill54164b02017-08-28 15:17:37 -04001766void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001767{
Jamie Madill54164b02017-08-28 15:17:37 -04001768 const auto &uniformLocation = mState.getUniformLocations()[location];
1769 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1770
1771 GLenum nativeType = gl::VariableComponentType(uniform.type);
1772 if (nativeType == GL_UNSIGNED_INT)
1773 {
1774 mProgram->getUniformuiv(context, location, v);
1775 }
1776 else
1777 {
1778 getUniformInternal(context, v, location, nativeType,
1779 gl::VariableComponentCount(uniform.type));
1780 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781}
1782
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783void Program::flagForDeletion()
1784{
1785 mDeleteStatus = true;
1786}
1787
1788bool Program::isFlaggedForDeletion() const
1789{
1790 return mDeleteStatus;
1791}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001792
Brandon Jones43a53e22014-08-28 16:23:22 -07001793void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001794{
1795 mInfoLog.reset();
1796
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001798 {
Geoff Lang92019432017-11-20 13:09:34 -05001799 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001800 }
1801 else
1802 {
Jamie Madillf6113162015-05-07 11:49:21 -04001803 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001804 }
1805}
1806
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1808{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001809 // Skip cache if we're using an infolog, so we get the full error.
1810 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1811 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1812 {
1813 return mCachedValidateSamplersResult.value();
1814 }
1815
1816 if (mTextureUnitTypesCache.empty())
1817 {
1818 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1819 }
1820 else
1821 {
1822 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1823 }
1824
1825 // if any two active samplers in a program are of different types, but refer to the same
1826 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1827 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001828 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001829 {
Jamie Madill54164b02017-08-28 15:17:37 -04001830 if (samplerBinding.unreferenced)
1831 continue;
1832
Jamie Madille7d84322017-01-10 18:21:59 -05001833 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001834
Jamie Madille7d84322017-01-10 18:21:59 -05001835 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001836 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001837 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1838 {
1839 if (infoLog)
1840 {
1841 (*infoLog) << "Sampler uniform (" << textureUnit
1842 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1843 << caps.maxCombinedTextureImageUnits << ")";
1844 }
1845
1846 mCachedValidateSamplersResult = false;
1847 return false;
1848 }
1849
1850 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1851 {
1852 if (textureType != mTextureUnitTypesCache[textureUnit])
1853 {
1854 if (infoLog)
1855 {
1856 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1857 "image unit ("
1858 << textureUnit << ").";
1859 }
1860
1861 mCachedValidateSamplersResult = false;
1862 return false;
1863 }
1864 }
1865 else
1866 {
1867 mTextureUnitTypesCache[textureUnit] = textureType;
1868 }
1869 }
1870 }
1871
1872 mCachedValidateSamplersResult = true;
1873 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874}
1875
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001876bool Program::isValidated() const
1877{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 return mValidated;
1879}
1880
Geoff Lange1a27752015-10-05 13:16:04 -04001881GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001882{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001883 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884}
1885
jchen1058f67be2017-10-27 08:59:27 +08001886GLuint Program::getActiveAtomicCounterBufferCount() const
1887{
1888 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1889}
1890
Jiajia Qin729b2c62017-08-14 09:36:11 +08001891GLuint Program::getActiveShaderStorageBlockCount() const
1892{
1893 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1894}
1895
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001896void Program::getActiveUniformBlockName(const GLuint blockIndex,
1897 GLsizei bufSize,
1898 GLsizei *length,
1899 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001901 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
1902}
Geoff Lang7dd2e102014-11-10 15:19:26 -05001903
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001904void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
1905 GLsizei bufSize,
1906 GLsizei *length,
1907 GLchar *blockName) const
1908{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001909
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001910 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001911}
1912
Geoff Lange1a27752015-10-05 13:16:04 -04001913GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001914{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001915 int maxLength = 0;
1916
1917 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001918 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001919 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001920 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1921 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001922 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923 if (!uniformBlock.name.empty())
1924 {
jchen10af713a22017-04-19 09:10:56 +08001925 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1926 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 }
1928 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001929 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001930
1931 return maxLength;
1932}
1933
Geoff Lange1a27752015-10-05 13:16:04 -04001934GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001935{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001936 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
1937}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001938
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001939GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
1940{
1941 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001942}
1943
Jiajia Qin729b2c62017-08-14 09:36:11 +08001944const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001945{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001946 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1947 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001948}
1949
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001950const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
1951{
1952 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
1953 return mState.mShaderStorageBlocks[index];
1954}
1955
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001956void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1957{
jchen107a20b972017-06-13 14:25:26 +08001958 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001959 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001960 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001961}
1962
1963GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1964{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001965 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001966}
1967
Jiajia Qin729b2c62017-08-14 09:36:11 +08001968GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1969{
1970 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1971}
1972
Geoff Lang48dcae72014-02-05 16:28:24 -05001973void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1974{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001975 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001976 for (GLsizei i = 0; i < count; i++)
1977 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001978 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001979 }
1980
Jamie Madill48ef11b2016-04-27 15:21:52 -04001981 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001982}
1983
1984void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1985{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001987 {
jchen10a9042d32017-03-17 08:50:45 +08001988 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1989 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1990 std::string varName = var.nameWithArrayIndex();
1991 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001992 if (length)
1993 {
1994 *length = lastNameIdx;
1995 }
1996 if (size)
1997 {
jchen10a9042d32017-03-17 08:50:45 +08001998 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001999 }
2000 if (type)
2001 {
jchen10a9042d32017-03-17 08:50:45 +08002002 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05002003 }
2004 if (name)
2005 {
jchen10a9042d32017-03-17 08:50:45 +08002006 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05002007 name[lastNameIdx] = '\0';
2008 }
2009 }
2010}
2011
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002012GLsizei Program::getTransformFeedbackVaryingCount() const
2013{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002015 {
jchen10a9042d32017-03-17 08:50:45 +08002016 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05002017 }
2018 else
2019 {
2020 return 0;
2021 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002022}
2023
2024GLsizei Program::getTransformFeedbackVaryingMaxLength() const
2025{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05002027 {
2028 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08002029 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05002030 {
jchen10a9042d32017-03-17 08:50:45 +08002031 maxSize =
2032 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05002033 }
2034
2035 return maxSize;
2036 }
2037 else
2038 {
2039 return 0;
2040 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002041}
2042
2043GLenum Program::getTransformFeedbackBufferMode() const
2044{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002045 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046}
2047
Jiawei Shao73618602017-12-20 15:47:15 +08002048bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
2049{
2050 Shader *vertexShader = mState.mAttachedVertexShader;
2051 Shader *fragmentShader = mState.mAttachedFragmentShader;
2052 Shader *computeShader = mState.mAttachedComputeShader;
2053
2054 bool isComputeShaderAttached = (computeShader != nullptr);
2055 bool isGraphicsShaderAttached = (vertexShader != nullptr || fragmentShader != nullptr);
2056 // Check whether we both have a compute and non-compute shaders attached.
2057 // If there are of both types attached, then linking should fail.
2058 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
2059 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
2060 {
2061 infoLog << "Both compute and graphics shaders are attached to the same program.";
2062 return false;
2063 }
2064
2065 if (computeShader)
2066 {
2067 if (!computeShader->isCompiled(context))
2068 {
2069 infoLog << "Attached compute shader is not compiled.";
2070 return false;
2071 }
2072 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
2073
2074 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
2075
2076 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
2077 // If the work group size is not specified, a link time error should occur.
2078 if (!mState.mComputeShaderLocalSize.isDeclared())
2079 {
2080 infoLog << "Work group size is not specified.";
2081 return false;
2082 }
2083 }
2084 else
2085 {
2086 if (!fragmentShader || !fragmentShader->isCompiled(context))
2087 {
2088 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2089 return false;
2090 }
2091 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
2092
2093 if (!vertexShader || !vertexShader->isCompiled(context))
2094 {
2095 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2096 return false;
2097 }
2098 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
2099
2100 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
2101 {
2102 infoLog << "Fragment shader version does not match vertex shader version.";
2103 return false;
2104 }
2105 }
2106
2107 return true;
2108}
2109
jchen10910a3da2017-11-15 09:40:11 +08002110GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2111{
2112 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2113 {
2114 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2115 if (tf.nameWithArrayIndex() == name)
2116 {
2117 return tfIndex;
2118 }
2119 }
2120 return GL_INVALID_INDEX;
2121}
2122
2123const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2124{
2125 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2126 return mState.mLinkedTransformFeedbackVaryings[index];
2127}
2128
Jamie Madillbd044ed2017-06-05 12:59:21 -04002129bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002131 Shader *generatingShader = mState.mAttachedVertexShader;
2132 Shader *consumingShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05002133
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002134 ASSERT(generatingShader->getShaderVersion(context) ==
2135 consumingShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002136
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002137 const std::vector<sh::Varying> &outputVaryings = generatingShader->getOutputVaryings(context);
2138 const std::vector<sh::Varying> &inputVaryings = consumingShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139
Sami Väisänen46eaa942016-06-29 10:26:37 +03002140 std::map<GLuint, std::string> staticFragmentInputLocations;
2141
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002142 for (const sh::Varying &input : inputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 bool matched = false;
2145
2146 // Built-in varyings obey special rules
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002147 if (input.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 {
2149 continue;
2150 }
2151
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002152 for (const sh::Varying &output : outputVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002154 if (input.name == output.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002156 ASSERT(!output.isBuiltIn());
2157
2158 std::string mismatchedStructFieldName;
2159 LinkMismatchError linkError =
2160 LinkValidateVaryings(output, input, generatingShader->getShaderVersion(context),
2161 &mismatchedStructFieldName);
2162 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002164 LogLinkMismatch(infoLog, input.name, "varying", linkError,
2165 mismatchedStructFieldName, generatingShader->getType(),
2166 consumingShader->getType());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 return false;
2168 }
2169
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170 matched = true;
2171 break;
2172 }
2173 }
2174
2175 // We permit unmatched, unreferenced varyings
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002176 if (!matched && input.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002178 infoLog << GetShaderTypeString(consumingShader->getType()) << " varying " << input.name
2179 << " does not match any " << GetShaderTypeString(generatingShader->getType())
2180 << " varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002181 return false;
2182 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002183
2184 // Check for aliased path rendering input bindings (if any).
2185 // If more than one binding refer statically to the same
2186 // location the link must fail.
2187
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002188 if (!input.staticUse)
Sami Väisänen46eaa942016-06-29 10:26:37 +03002189 continue;
2190
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002191 const auto inputBinding = mFragmentInputBindings.getBinding(input.name);
Sami Väisänen46eaa942016-06-29 10:26:37 +03002192 if (inputBinding == -1)
2193 continue;
2194
2195 const auto it = staticFragmentInputLocations.find(inputBinding);
2196 if (it == std::end(staticFragmentInputLocations))
2197 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002198 staticFragmentInputLocations.insert(std::make_pair(inputBinding, input.name));
Sami Väisänen46eaa942016-06-29 10:26:37 +03002199 }
2200 else
2201 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002202 infoLog << "Binding for fragment input " << input.name << " conflicts with "
Sami Väisänen46eaa942016-06-29 10:26:37 +03002203 << it->second;
2204 return false;
2205 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002206 }
2207
Jamie Madillbd044ed2017-06-05 12:59:21 -04002208 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05002209 {
2210 return false;
2211 }
2212
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002213 // TODO(jmadill): verify no unmatched output varyings?
Jamie Madillada9ecc2015-08-17 12:53:37 -04002214
Geoff Lang7dd2e102014-11-10 15:19:26 -05002215 return true;
2216}
2217
Jamie Madillbd044ed2017-06-05 12:59:21 -04002218bool Program::linkUniforms(const Context *context,
2219 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002220 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002221{
Olli Etuahob78707c2017-03-09 15:03:11 +00002222 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002223 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002224 {
2225 return false;
2226 }
2227
Olli Etuahob78707c2017-03-09 15:03:11 +00002228 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002229
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002230 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002231
jchen10eaef1e52017-06-13 10:44:11 +08002232 if (!linkAtomicCounterBuffers())
2233 {
2234 return false;
2235 }
2236
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002237 return true;
2238}
2239
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002240void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002241{
Jamie Madill982f6e02017-06-07 14:33:04 -04002242 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2243 unsigned int low = high;
2244
jchen10eaef1e52017-06-13 10:44:11 +08002245 for (auto counterIter = mState.mUniforms.rbegin();
2246 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2247 {
2248 --low;
2249 }
2250
2251 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2252
2253 high = low;
2254
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002255 for (auto imageIter = mState.mUniforms.rbegin();
2256 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2257 {
2258 --low;
2259 }
2260
2261 mState.mImageUniformRange = RangeUI(low, high);
2262
2263 // If uniform is a image type, insert it into the mImageBindings array.
2264 for (unsigned int imageIndex : mState.mImageUniformRange)
2265 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002266 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2267 // cannot load values into a uniform defined as an image. if declare without a
2268 // binding qualifier, any uniform image variable (include all elements of
2269 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002270 auto &imageUniform = mState.mUniforms[imageIndex];
2271 if (imageUniform.binding == -1)
2272 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002273 mState.mImageBindings.emplace_back(
2274 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002275 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002276 else
2277 {
2278 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002279 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002280 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002281 }
2282
2283 high = low;
2284
2285 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002286 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002287 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002288 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002289 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002290
2291 mState.mSamplerUniformRange = RangeUI(low, high);
2292
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002293 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002294 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002295 {
2296 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2297 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2298 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002299 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002300 }
2301}
2302
jchen10eaef1e52017-06-13 10:44:11 +08002303bool Program::linkAtomicCounterBuffers()
2304{
2305 for (unsigned int index : mState.mAtomicCounterUniformRange)
2306 {
2307 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002308 uniform.blockInfo.offset = uniform.offset;
2309 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2310 uniform.blockInfo.matrixStride = 0;
2311 uniform.blockInfo.isRowMajorMatrix = false;
2312
jchen10eaef1e52017-06-13 10:44:11 +08002313 bool found = false;
2314 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2315 ++bufferIndex)
2316 {
2317 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2318 if (buffer.binding == uniform.binding)
2319 {
2320 buffer.memberIndexes.push_back(index);
2321 uniform.bufferIndex = bufferIndex;
2322 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002323 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002324 break;
2325 }
2326 }
2327 if (!found)
2328 {
2329 AtomicCounterBuffer atomicCounterBuffer;
2330 atomicCounterBuffer.binding = uniform.binding;
2331 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002332 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002333 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2334 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2335 }
2336 }
2337 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2338 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2339
2340 return true;
2341}
2342
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002343LinkMismatchError Program::LinkValidateInterfaceBlockFields(
2344 const sh::InterfaceBlockField &blockField1,
2345 const sh::InterfaceBlockField &blockField2,
2346 bool webglCompatibility,
2347 std::string *mismatchedBlockFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002348{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002349 if (blockField1.name != blockField2.name)
2350 {
2351 return LinkMismatchError::FIELD_NAME_MISMATCH;
2352 }
2353
Frank Henigmanfccbac22017-05-28 17:29:26 -04002354 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002355 LinkMismatchError linkError = LinkValidateVariablesBase(
2356 blockField1, blockField2, webglCompatibility, mismatchedBlockFieldName);
2357 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002359 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
2360 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361 }
2362
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002363 if (blockField1.isRowMajorLayout != blockField2.isRowMajorLayout)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002365 AddParentPrefix(blockField1.name, mismatchedBlockFieldName);
2366 return LinkMismatchError::MATRIX_PACKING_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002367 }
2368
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002369 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370}
2371
Jamie Madilleb979bf2016-11-15 12:28:46 -05002372// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002373bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002375 const ContextState &data = context->getContextState();
2376 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002377
Geoff Lang7dd2e102014-11-10 15:19:26 -05002378 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002379 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002380 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002381
2382 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002383 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002384 {
Jamie Madillf6113162015-05-07 11:49:21 -04002385 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002386 return false;
2387 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002388
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002389 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002390
Jamie Madillc349ec02015-08-21 16:53:12 -04002391 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002392 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002393 {
Olli Etuahod2551232017-10-26 20:03:33 +03002394 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2395 // structures, so we don't need to worry about adjusting their names or generating entries
2396 // for each member/element (unlike uniforms for example).
2397 ASSERT(!attribute.isArray() && !attribute.isStruct());
2398
Jamie Madilleb979bf2016-11-15 12:28:46 -05002399 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002400 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002401 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002402 attribute.location = bindingLocation;
2403 }
2404
2405 if (attribute.location != -1)
2406 {
2407 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002408 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002409
Jamie Madill63805b42015-08-25 13:17:39 -04002410 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 {
Jamie Madillf6113162015-05-07 11:49:21 -04002412 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002413 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002414
2415 return false;
2416 }
2417
Jamie Madill63805b42015-08-25 13:17:39 -04002418 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002419 {
Jamie Madill63805b42015-08-25 13:17:39 -04002420 const int regLocation = attribute.location + reg;
2421 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002422
2423 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002424 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002425 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002426 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002427 // TODO(jmadill): fix aliasing on ES2
2428 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002429 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002430 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002431 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002432 return false;
2433 }
2434 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002435 else
2436 {
Jamie Madill63805b42015-08-25 13:17:39 -04002437 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002438 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002439
Jamie Madill63805b42015-08-25 13:17:39 -04002440 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441 }
2442 }
2443 }
2444
2445 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002446 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002448 // Not set by glBindAttribLocation or by location layout qualifier
2449 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 {
Jamie Madill63805b42015-08-25 13:17:39 -04002451 int regs = VariableRegisterCount(attribute.type);
2452 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002453
Jamie Madill63805b42015-08-25 13:17:39 -04002454 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002455 {
Jamie Madillf6113162015-05-07 11:49:21 -04002456 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002457 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002458 }
2459
Jamie Madillc349ec02015-08-21 16:53:12 -04002460 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002461 }
2462 }
2463
Brandon Jonesc405ae72017-12-06 14:15:03 -08002464 ASSERT(mState.mAttributesTypeMask.none());
2465 ASSERT(mState.mAttributesMask.none());
2466
Jamie Madill48ef11b2016-04-27 15:21:52 -04002467 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002468 {
Jamie Madill63805b42015-08-25 13:17:39 -04002469 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002470 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002471
Jamie Madillbd159f02017-10-09 19:39:06 -04002472 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002474 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2475 mState.mActiveAttribLocationsMask.set(location);
2476 mState.mMaxActiveAttribLocation =
2477 std::max(mState.mMaxActiveAttribLocation, location + 1);
Brandon Jonesc405ae72017-12-06 14:15:03 -08002478
2479 // gl_VertexID and gl_InstanceID are active attributes but don't have a bound attribute.
2480 if (!attribute.isBuiltIn())
2481 {
2482 mState.mAttributesTypeMask.setIndex(VariableComponentType(attribute.type),
2483 location);
2484 mState.mAttributesMask.set(location);
2485 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002486 }
2487 }
2488
Geoff Lang7dd2e102014-11-10 15:19:26 -05002489 return true;
2490}
2491
Jiawei Shao73618602017-12-20 15:47:15 +08002492bool Program::ValidateGraphicsInterfaceBlocks(
Martin Radev4c4c8e72016-08-04 12:25:34 +03002493 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2494 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002495 InfoLog &infoLog,
Jiawei Shao73618602017-12-20 15:47:15 +08002496 bool webglCompatibility)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002497{
2498 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002499 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2500 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002501
2502 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2503 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002504 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002505 }
2506
Jamie Madille473dee2015-08-18 14:49:01 -04002507 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002508 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002509 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2510 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002511 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002512 const sh::InterfaceBlock &vertexInterfaceBlock = *(entry->second);
2513 std::string mismatchedBlockFieldName;
2514 LinkMismatchError linkError =
2515 AreMatchingInterfaceBlocks(vertexInterfaceBlock, fragmentInterfaceBlock,
2516 webglCompatibility, &mismatchedBlockFieldName);
2517 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002518 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002519 LogLinkMismatch(infoLog, fragmentInterfaceBlock.name, "interface block", linkError,
2520 mismatchedBlockFieldName, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002521 return false;
2522 }
2523 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002524 // TODO(jiajia.qin@intel.com): Add
2525 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002526 }
2527 return true;
2528}
Jamie Madille473dee2015-08-18 14:49:01 -04002529
Jiajia Qin729b2c62017-08-14 09:36:11 +08002530bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002531{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002532 const auto &caps = context->getCaps();
2533
Martin Radev4c4c8e72016-08-04 12:25:34 +03002534 if (mState.mAttachedComputeShader)
2535 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002536 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002537 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002538
Jiajia Qin729b2c62017-08-14 09:36:11 +08002539 if (!validateInterfaceBlocksCount(
2540 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002541 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2542 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002543 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002544 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002545 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002546
2547 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2548 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2549 computeShaderStorageBlocks,
2550 "Compute shader shader storage block count exceeds "
2551 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2552 infoLog))
2553 {
2554 return false;
2555 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002556 return true;
2557 }
2558
Jamie Madillbd044ed2017-06-05 12:59:21 -04002559 Shader &vertexShader = *mState.mAttachedVertexShader;
2560 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002561
Jiajia Qin729b2c62017-08-14 09:36:11 +08002562 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2563 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002564
Jiajia Qin729b2c62017-08-14 09:36:11 +08002565 if (!validateInterfaceBlocksCount(
2566 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002567 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2568 {
2569 return false;
2570 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002571 if (!validateInterfaceBlocksCount(
2572 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002573 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2574 infoLog))
2575 {
2576
2577 return false;
2578 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002579
2580 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao73618602017-12-20 15:47:15 +08002581 if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog,
2582 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002583 {
2584 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002585 }
Jamie Madille473dee2015-08-18 14:49:01 -04002586
Jiajia Qin729b2c62017-08-14 09:36:11 +08002587 if (context->getClientVersion() >= Version(3, 1))
2588 {
2589 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2590 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2591
2592 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2593 vertexShaderStorageBlocks,
2594 "Vertex shader shader storage block count exceeds "
2595 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2596 infoLog))
2597 {
2598 return false;
2599 }
2600 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2601 fragmentShaderStorageBlocks,
2602 "Fragment shader shader storage block count exceeds "
2603 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2604 infoLog))
2605 {
2606
2607 return false;
2608 }
2609
Jiawei Shao73618602017-12-20 15:47:15 +08002610 if (!ValidateGraphicsInterfaceBlocks(vertexShaderStorageBlocks, fragmentShaderStorageBlocks,
2611 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002612 {
2613 return false;
2614 }
2615 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002616 return true;
2617}
2618
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002619LinkMismatchError Program::AreMatchingInterfaceBlocks(const sh::InterfaceBlock &interfaceBlock1,
2620 const sh::InterfaceBlock &interfaceBlock2,
2621 bool webglCompatibility,
2622 std::string *mismatchedBlockFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002623{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002624 // validate blocks for the same member types
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002625 if (interfaceBlock1.fields.size() != interfaceBlock2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002626 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002627 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002628 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002629 if (interfaceBlock1.arraySize != interfaceBlock2.arraySize)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002630 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002631 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002632 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002633 if (interfaceBlock1.layout != interfaceBlock2.layout ||
2634 interfaceBlock1.binding != interfaceBlock2.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002635 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002636 return LinkMismatchError::LAYOUT_QUALIFIER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002637 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002638 const unsigned int numBlockMembers = static_cast<unsigned int>(interfaceBlock1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002639 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2640 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002641 const sh::InterfaceBlockField &member1 = interfaceBlock1.fields[blockMemberIndex];
2642 const sh::InterfaceBlockField &member2 = interfaceBlock2.fields[blockMemberIndex];
2643
2644 LinkMismatchError linkError = LinkValidateInterfaceBlockFields(
2645 member1, member2, webglCompatibility, mismatchedBlockFieldName);
2646 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002647 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002648 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002649 }
2650 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002651 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002652}
2653
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002654LinkMismatchError Program::LinkValidateVariablesBase(const sh::ShaderVariable &variable1,
2655 const sh::ShaderVariable &variable2,
2656 bool validatePrecision,
2657 std::string *mismatchedStructOrBlockMemberName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002658{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002659 if (variable1.type != variable2.type)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002660 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002661 return LinkMismatchError::TYPE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002662 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002663 if (variable1.arraySizes != variable2.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002664 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002665 return LinkMismatchError::ARRAY_SIZE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002666 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002667 if (validatePrecision && variable1.precision != variable2.precision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002668 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002669 return LinkMismatchError::PRECISION_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002670 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002671 if (variable1.structName != variable2.structName)
Geoff Langbb1e7502017-06-05 16:40:09 -04002672 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002673 return LinkMismatchError::STRUCT_NAME_MISMATCH;
Geoff Langbb1e7502017-06-05 16:40:09 -04002674 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002675
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002676 if (variable1.fields.size() != variable2.fields.size())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002677 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002678 return LinkMismatchError::FIELD_NUMBER_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002679 }
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002680 const unsigned int numMembers = static_cast<unsigned int>(variable1.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002681 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2682 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002683 const sh::ShaderVariable &member1 = variable1.fields[memberIndex];
2684 const sh::ShaderVariable &member2 = variable2.fields[memberIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002685
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002686 if (member1.name != member2.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002687 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002688 return LinkMismatchError::FIELD_NAME_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002689 }
2690
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002691 LinkMismatchError linkErrorOnField = LinkValidateVariablesBase(
2692 member1, member2, validatePrecision, mismatchedStructOrBlockMemberName);
2693 if (linkErrorOnField != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002694 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002695 AddParentPrefix(member1.name, mismatchedStructOrBlockMemberName);
2696 return linkErrorOnField;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002697 }
2698 }
2699
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002700 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002701}
2702
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002703LinkMismatchError Program::LinkValidateVaryings(const sh::Varying &outputVarying,
2704 const sh::Varying &inputVarying,
2705 int shaderVersion,
2706 std::string *mismatchedStructFieldName)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002707{
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002708 LinkMismatchError linkError =
2709 LinkValidateVariablesBase(outputVarying, inputVarying, false, mismatchedStructFieldName);
2710 if (linkError != LinkMismatchError::NO_MISMATCH)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002711 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002712 return linkError;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002713 }
2714
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002715 if (!sh::InterpolationTypesMatch(outputVarying.interpolation, inputVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002716 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002717 return LinkMismatchError::INTERPOLATION_TYPE_MISMATCH;
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002718 }
2719
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002720 if (shaderVersion == 100 && outputVarying.isInvariant != inputVarying.isInvariant)
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002721 {
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002722 return LinkMismatchError::INVARIANCE_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002723 }
2724
Jiawei Shao881b7bf2017-12-25 11:18:37 +08002725 return LinkMismatchError::NO_MISMATCH;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726}
2727
Jamie Madillbd044ed2017-06-05 12:59:21 -04002728bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002729{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002730 Shader *vertexShader = mState.mAttachedVertexShader;
2731 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002732 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2733 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002734 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002735
2736 if (shaderVersion != 100)
2737 {
2738 // Only ESSL 1.0 has restrictions on matching input and output invariance
2739 return true;
2740 }
2741
2742 bool glPositionIsInvariant = false;
2743 bool glPointSizeIsInvariant = false;
2744 bool glFragCoordIsInvariant = false;
2745 bool glPointCoordIsInvariant = false;
2746
2747 for (const sh::Varying &varying : vertexVaryings)
2748 {
2749 if (!varying.isBuiltIn())
2750 {
2751 continue;
2752 }
2753 if (varying.name.compare("gl_Position") == 0)
2754 {
2755 glPositionIsInvariant = varying.isInvariant;
2756 }
2757 else if (varying.name.compare("gl_PointSize") == 0)
2758 {
2759 glPointSizeIsInvariant = varying.isInvariant;
2760 }
2761 }
2762
2763 for (const sh::Varying &varying : fragmentVaryings)
2764 {
2765 if (!varying.isBuiltIn())
2766 {
2767 continue;
2768 }
2769 if (varying.name.compare("gl_FragCoord") == 0)
2770 {
2771 glFragCoordIsInvariant = varying.isInvariant;
2772 }
2773 else if (varying.name.compare("gl_PointCoord") == 0)
2774 {
2775 glPointCoordIsInvariant = varying.isInvariant;
2776 }
2777 }
2778
2779 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2780 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2781 // Not requiring invariance to match is supported by:
2782 // dEQP, WebGL CTS, Nexus 5X GLES
2783 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2784 {
2785 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2786 "declared invariant.";
2787 return false;
2788 }
2789 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2790 {
2791 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2792 "declared invariant.";
2793 return false;
2794 }
2795
2796 return true;
2797}
2798
jchen10a9042d32017-03-17 08:50:45 +08002799bool Program::linkValidateTransformFeedback(const gl::Context *context,
2800 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002801 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002802 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002803{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002804
jchen108225e732017-11-14 16:29:03 +08002805 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04002806 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002807 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002808 {
jchen10a9042d32017-03-17 08:50:45 +08002809 if (context->getClientVersion() < Version(3, 1) &&
2810 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002811 {
Geoff Lang1a683462015-09-29 15:09:59 -04002812 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002813 return false;
2814 }
jchen108225e732017-11-14 16:29:03 +08002815 if (context->getClientVersion() >= Version(3, 1))
2816 {
2817 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2818 {
2819 infoLog << "Two transform feedback varyings include the same array element ("
2820 << tfVaryingName << ").";
2821 return false;
2822 }
2823 }
2824 else
2825 {
2826 if (uniqueNames.count(tfVaryingName) > 0)
2827 {
2828 infoLog << "Two transform feedback varyings specify the same output variable ("
2829 << tfVaryingName << ").";
2830 return false;
2831 }
2832 }
2833 uniqueNames.insert(tfVaryingName);
2834 }
2835
2836 // Validate against program varyings.
2837 size_t totalComponents = 0;
2838 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
2839 {
2840 std::vector<unsigned int> subscripts;
2841 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
2842
2843 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
2844 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08002845 {
2846 infoLog << "Transform feedback varying " << tfVaryingName
2847 << " does not exist in the vertex shader.";
2848 return false;
2849 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002850
jchen108225e732017-11-14 16:29:03 +08002851 // Validate the matching variable.
2852 if (var->isStruct())
2853 {
2854 infoLog << "Struct cannot be captured directly (" << baseName << ").";
2855 return false;
2856 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002857
jchen108225e732017-11-14 16:29:03 +08002858 size_t elementCount = 0;
2859 size_t componentCount = 0;
2860
2861 if (var->isArray())
2862 {
2863 if (context->getClientVersion() < Version(3, 1))
2864 {
2865 infoLog << "Capture of arrays is undefined and not supported.";
2866 return false;
2867 }
2868
2869 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
2870 ASSERT(!var->isArrayOfArrays());
2871
2872 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
2873 {
2874 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
2875 return false;
2876 }
2877 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
2878 }
2879 else
2880 {
2881 if (!subscripts.empty())
2882 {
2883 infoLog << "Varying '" << baseName
2884 << "' is not an array to be captured by element.";
2885 return false;
2886 }
2887 elementCount = 1;
2888 }
2889
2890 // TODO(jmadill): Investigate implementation limits on D3D11
2891 componentCount = VariableComponentCount(var->type) * elementCount;
2892 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
2893 componentCount > caps.maxTransformFeedbackSeparateComponents)
2894 {
2895 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
2896 << componentCount << ") exceed the maximum separate components ("
2897 << caps.maxTransformFeedbackSeparateComponents << ").";
2898 return false;
2899 }
2900
2901 totalComponents += componentCount;
2902 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
2903 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
2904 {
2905 infoLog << "Transform feedback varying total components (" << totalComponents
2906 << ") exceed the maximum interleaved components ("
2907 << caps.maxTransformFeedbackInterleavedComponents << ").";
2908 return false;
2909 }
2910 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002911 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002912}
2913
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002914bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2915{
2916 const std::vector<sh::Uniform> &vertexUniforms =
2917 mState.mAttachedVertexShader->getUniforms(context);
2918 const std::vector<sh::Uniform> &fragmentUniforms =
2919 mState.mAttachedFragmentShader->getUniforms(context);
2920 const std::vector<sh::Attribute> &attributes =
2921 mState.mAttachedVertexShader->getActiveAttributes(context);
2922 for (const auto &attrib : attributes)
2923 {
2924 for (const auto &uniform : vertexUniforms)
2925 {
2926 if (uniform.name == attrib.name)
2927 {
2928 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2929 return false;
2930 }
2931 }
2932 for (const auto &uniform : fragmentUniforms)
2933 {
2934 if (uniform.name == attrib.name)
2935 {
2936 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2937 return false;
2938 }
2939 }
2940 }
2941 return true;
2942}
2943
Jamie Madill3c1da042017-11-27 18:33:40 -05002944void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002945{
2946 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002947 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002948 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002949 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002950 std::vector<unsigned int> subscripts;
2951 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002952 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002953 if (!subscripts.empty())
2954 {
2955 subscript = subscripts.back();
2956 }
Jamie Madill192745a2016-12-22 15:58:21 -05002957 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002958 {
Jamie Madill192745a2016-12-22 15:58:21 -05002959 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002960 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002961 {
jchen10a9042d32017-03-17 08:50:45 +08002962 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2963 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002964 break;
2965 }
jchen108225e732017-11-14 16:29:03 +08002966 else if (varying->isStruct())
2967 {
2968 const auto *field = FindShaderVarField(*varying, tfVaryingName);
2969 if (field != nullptr)
2970 {
2971 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
2972 break;
2973 }
2974 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002975 }
2976 }
2977}
2978
Jamie Madill3c1da042017-11-27 18:33:40 -05002979ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002980{
Jamie Madill3c1da042017-11-27 18:33:40 -05002981 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002982
Jiawei Shao3d404882017-10-16 13:30:48 +08002983 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002984 {
Jamie Madill192745a2016-12-22 15:58:21 -05002985 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002986 }
2987
Jiawei Shao3d404882017-10-16 13:30:48 +08002988 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002989 {
Jamie Madill192745a2016-12-22 15:58:21 -05002990 merged[varying.name].fragment = &varying;
2991 }
2992
2993 return merged;
2994}
2995
Jamie Madillbd044ed2017-06-05 12:59:21 -04002996void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002997{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002998 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002999 ASSERT(fragmentShader != nullptr);
3000
Geoff Lange0cff192017-05-30 13:04:56 -04003001 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04003002 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08003003 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04003004
3005 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04003006 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04003007 {
3008 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
3009 outputVariable.name != "gl_FragData")
3010 {
3011 continue;
3012 }
3013
3014 unsigned int baseLocation =
3015 (outputVariable.location == -1 ? 0u
3016 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03003017
3018 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3019 // structures, so we may use getBasicTypeElementCount().
3020 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3021 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04003022 {
3023 const unsigned int location = baseLocation + elementIndex;
3024 if (location >= mState.mOutputVariableTypes.size())
3025 {
3026 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
3027 }
Corentin Walleze7557742017-06-01 13:09:57 -04003028 ASSERT(location < mState.mActiveOutputVariables.size());
3029 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04003030 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08003031 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04003032 }
3033 }
3034
Jamie Madill80a6fc02015-08-21 16:53:16 -04003035 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04003036 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003037 return;
3038
Jamie Madillbd044ed2017-06-05 12:59:21 -04003039 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04003040 // TODO(jmadill): any caps validation here?
3041
jchen1015015f72017-03-16 13:54:21 +08003042 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04003043 outputVariableIndex++)
3044 {
jchen1015015f72017-03-16 13:54:21 +08003045 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04003046
Olli Etuahod2551232017-10-26 20:03:33 +03003047 if (outputVariable.isArray())
3048 {
3049 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
3050 // Resources and including [0] at the end of array variable names.
3051 mState.mOutputVariables[outputVariableIndex].name += "[0]";
3052 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
3053 }
3054
Jamie Madill80a6fc02015-08-21 16:53:16 -04003055 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
3056 if (outputVariable.isBuiltIn())
3057 continue;
3058
3059 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03003060 unsigned int baseLocation =
3061 (outputVariable.location == -1 ? 0u
3062 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04003063
Olli Etuaho465835d2017-09-26 13:34:10 +03003064 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
3065 // structures, so we may use getBasicTypeElementCount().
3066 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
3067 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04003068 {
Olli Etuahod2551232017-10-26 20:03:33 +03003069 const unsigned int location = baseLocation + elementIndex;
3070 if (location >= mState.mOutputLocations.size())
3071 {
3072 mState.mOutputLocations.resize(location + 1);
3073 }
3074 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03003075 if (outputVariable.isArray())
3076 {
3077 mState.mOutputLocations[location] =
3078 VariableLocation(elementIndex, outputVariableIndex);
3079 }
3080 else
3081 {
3082 VariableLocation locationInfo;
3083 locationInfo.index = outputVariableIndex;
3084 mState.mOutputLocations[location] = locationInfo;
3085 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003086 }
3087 }
3088}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003089
Olli Etuaho48fed632017-03-16 12:05:30 +00003090void Program::setUniformValuesFromBindingQualifiers()
3091{
Jamie Madill982f6e02017-06-07 14:33:04 -04003092 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003093 {
3094 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3095 if (samplerUniform.binding != -1)
3096 {
Olli Etuahod2551232017-10-26 20:03:33 +03003097 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003098 ASSERT(location != -1);
3099 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003100 for (unsigned int elementIndex = 0;
3101 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003102 {
3103 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3104 }
3105 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3106 boundTextureUnits.data());
3107 }
3108 }
3109}
3110
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003111void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003112{
jchen10af713a22017-04-19 09:10:56 +08003113 // Set initial bindings from shader.
3114 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3115 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003116 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003117 bindUniformBlock(blockIndex, uniformBlock.binding);
3118 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003119}
3120
Jamie Madille7d84322017-01-10 18:21:59 -05003121void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003122 GLsizei clampedCount,
3123 const GLint *v)
3124{
Jamie Madill81c2e252017-09-09 23:32:46 -04003125 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3126 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3127 std::vector<GLuint> *boundTextureUnits =
3128 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003129
Olli Etuaho1734e172017-10-27 15:30:27 +03003130 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003131
3132 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003133 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003134}
3135
3136template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003137GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3138 GLsizei count,
3139 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003140 const T *v)
3141{
Jamie Madill134f93d2017-08-31 17:11:00 -04003142 if (count == 1)
3143 return 1;
3144
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003145 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003146
Corentin Wallez15ac5342016-11-03 17:06:39 -04003147 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3148 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003149 unsigned int remainingElements =
3150 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003151 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003152 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003153
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003154 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003155 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003156 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003157 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003158
3159 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003160}
3161
3162template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003163GLsizei Program::clampMatrixUniformCount(GLint location,
3164 GLsizei count,
3165 GLboolean transpose,
3166 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003167{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003168 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3169
Jamie Madill62d31cb2015-09-11 13:25:51 -04003170 if (!transpose)
3171 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003172 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003173 }
3174
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003175 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003176
3177 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3178 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003179 unsigned int remainingElements =
3180 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003181 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003182}
3183
Jamie Madill54164b02017-08-28 15:17:37 -04003184// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3185// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003186template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003187void Program::getUniformInternal(const Context *context,
3188 DestT *dataOut,
3189 GLint location,
3190 GLenum nativeType,
3191 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003192{
Jamie Madill54164b02017-08-28 15:17:37 -04003193 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003194 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003195 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003196 {
3197 GLint tempValue[16] = {0};
3198 mProgram->getUniformiv(context, location, tempValue);
3199 UniformStateQueryCastLoop<GLboolean>(
3200 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003201 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003202 }
3203 case GL_INT:
3204 {
3205 GLint tempValue[16] = {0};
3206 mProgram->getUniformiv(context, location, tempValue);
3207 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3208 components);
3209 break;
3210 }
3211 case GL_UNSIGNED_INT:
3212 {
3213 GLuint tempValue[16] = {0};
3214 mProgram->getUniformuiv(context, location, tempValue);
3215 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3216 components);
3217 break;
3218 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003219 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003220 {
3221 GLfloat tempValue[16] = {0};
3222 mProgram->getUniformfv(context, location, tempValue);
3223 UniformStateQueryCastLoop<GLfloat>(
3224 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003225 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003226 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003227 default:
3228 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003229 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003230 }
3231}
Jamie Madilla4595b82017-01-11 17:36:34 -05003232
3233bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3234{
3235 // Must be called after samplers are validated.
3236 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3237
3238 for (const auto &binding : mState.mSamplerBindings)
3239 {
3240 GLenum textureType = binding.textureType;
3241 for (const auto &unit : binding.boundTextureUnits)
3242 {
3243 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3244 if (programTextureID == textureID)
3245 {
3246 // TODO(jmadill): Check for appropriate overlap.
3247 return true;
3248 }
3249 }
3250 }
3251
3252 return false;
3253}
3254
Jamie Madilla2c74982016-12-12 11:20:42 -05003255} // namespace gl