blob: ce401758cace3439865e917e86f587ede1942c9f [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
Jamie Madill192745a2016-12-22 15:58:21 -0500109
jchen1015015f72017-03-16 13:54:21 +0800110template <typename VarT>
111GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
112{
Olli Etuahod2551232017-10-26 20:03:33 +0300113 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800114 for (size_t index = 0; index < list.size(); index++)
115 {
116 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300117 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800118 {
Olli Etuahod2551232017-10-26 20:03:33 +0300119 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800120 }
121 }
122
123 return GL_INVALID_INDEX;
124}
125
Olli Etuahod2551232017-10-26 20:03:33 +0300126template <typename VarT>
127GLint GetVariableLocation(const std::vector<VarT> &list,
128 const std::vector<VariableLocation> &locationList,
129 const std::string &name)
130{
131 size_t nameLengthWithoutArrayIndex;
132 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
133
134 for (size_t location = 0u; location < locationList.size(); ++location)
135 {
136 const VariableLocation &variableLocation = locationList[location];
137 if (!variableLocation.used())
138 {
139 continue;
140 }
141
142 const VarT &variable = list[variableLocation.index];
143
144 if (angle::BeginsWith(variable.name, name))
145 {
146 if (name.length() == variable.name.length())
147 {
148 ASSERT(name == variable.name);
149 // GLES 3.1 November 2016 page 87.
150 // The string exactly matches the name of the active variable.
151 return static_cast<GLint>(location);
152 }
153 if (name.length() + 3u == variable.name.length() && variable.isArray())
154 {
155 ASSERT(name + "[0]" == variable.name);
156 // The string identifies the base name of an active array, where the string would
157 // exactly match the name of the variable if the suffix "[0]" were appended to the
158 // string.
159 return static_cast<GLint>(location);
160 }
161 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300162 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300163 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
164 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
165 {
166 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
167 // The string identifies an active element of the array, where the string ends with the
168 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
169 // zeroes, or whitespace) identifying an array element, and the "]" character, the
170 // integer is less than the number of active elements of the array variable, and where
171 // the string would exactly match the enumerated name of the array if the decimal
172 // integer were replaced with zero.
173 return static_cast<GLint>(location);
174 }
175 }
176
177 return -1;
178}
179
jchen10fd7c3b52017-03-21 15:36:03 +0800180void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
181{
182 ASSERT(bufSize > 0);
183 strncpy(buffer, string.c_str(), bufSize);
184 buffer[bufSize - 1] = '\0';
185
186 if (length)
187 {
188 *length = static_cast<GLsizei>(strlen(buffer));
189 }
190}
191
jchen10a9042d32017-03-17 08:50:45 +0800192bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
193{
Olli Etuahoc8538042017-09-27 11:20:15 +0300194 std::vector<unsigned int> subscripts;
195 std::string baseName = ParseResourceName(name, &subscripts);
196 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800197 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300198 std::vector<unsigned int> arrayIndices;
199 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
200 if (baseName == arrayName &&
201 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800202 {
203 return true;
204 }
205 }
206 return false;
207}
208
Jiajia Qin729b2c62017-08-14 09:36:11 +0800209bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
210 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
211 const std::string &errorMessage,
212 InfoLog &infoLog)
213{
214 GLuint blockCount = 0;
215 for (const sh::InterfaceBlock &block : interfaceBlocks)
216 {
217 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
218 {
219 blockCount += (block.arraySize ? block.arraySize : 1);
220 if (blockCount > maxInterfaceBlocks)
221 {
222 infoLog << errorMessage << maxInterfaceBlocks << ")";
223 return false;
224 }
225 }
226 }
227 return true;
228}
229
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800230GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
231{
232 std::vector<unsigned int> subscripts;
233 std::string baseName = ParseResourceName(name, &subscripts);
234
235 unsigned int numBlocks = static_cast<unsigned int>(list.size());
236 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
237 {
238 const auto &block = list[blockIndex];
239 if (block.name == baseName)
240 {
241 const bool arrayElementZero =
242 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
243 const bool arrayElementMatches =
244 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
245 if (arrayElementMatches || arrayElementZero)
246 {
247 return blockIndex;
248 }
249 }
250 }
251
252 return GL_INVALID_INDEX;
253}
254
255void GetInterfaceBlockName(const GLuint index,
256 const std::vector<InterfaceBlock> &list,
257 GLsizei bufSize,
258 GLsizei *length,
259 GLchar *name)
260{
261 ASSERT(index < list.size());
262
263 const auto &block = list[index];
264
265 if (bufSize > 0)
266 {
267 std::string blockName = block.name;
268
269 if (block.isArray)
270 {
271 blockName += ArrayString(block.arrayElement);
272 }
273 CopyStringToBuffer(name, blockName, bufSize, length);
274 }
275}
276
Jamie Madillc9727f32017-11-07 12:37:07 -0500277void InitUniformBlockLinker(const gl::Context *context,
278 const ProgramState &state,
279 UniformBlockLinker *blockLinker)
280{
281 if (state.getAttachedVertexShader())
282 {
283 blockLinker->addShaderBlocks(GL_VERTEX_SHADER,
284 &state.getAttachedVertexShader()->getUniformBlocks(context));
285 }
286
287 if (state.getAttachedFragmentShader())
288 {
289 blockLinker->addShaderBlocks(GL_FRAGMENT_SHADER,
290 &state.getAttachedFragmentShader()->getUniformBlocks(context));
291 }
292
293 if (state.getAttachedComputeShader())
294 {
295 blockLinker->addShaderBlocks(GL_COMPUTE_SHADER,
296 &state.getAttachedComputeShader()->getUniformBlocks(context));
297 }
298}
299
300void InitShaderStorageBlockLinker(const gl::Context *context,
301 const ProgramState &state,
302 ShaderStorageBlockLinker *blockLinker)
303{
304 if (state.getAttachedVertexShader())
305 {
306 blockLinker->addShaderBlocks(
307 GL_VERTEX_SHADER, &state.getAttachedVertexShader()->getShaderStorageBlocks(context));
308 }
309
310 if (state.getAttachedFragmentShader())
311 {
312 blockLinker->addShaderBlocks(
313 GL_FRAGMENT_SHADER,
314 &state.getAttachedFragmentShader()->getShaderStorageBlocks(context));
315 }
316
317 if (state.getAttachedComputeShader())
318 {
319 blockLinker->addShaderBlocks(
320 GL_COMPUTE_SHADER, &state.getAttachedComputeShader()->getShaderStorageBlocks(context));
321 }
322}
323
Jamie Madill62d31cb2015-09-11 13:25:51 -0400324} // anonymous namespace
325
Jamie Madill4a3c2342015-10-08 12:58:45 -0400326const char *const g_fakepath = "C:\\fakepath";
327
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400328InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000329{
330}
331
332InfoLog::~InfoLog()
333{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000334}
335
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400336size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000337{
Jamie Madill23176ce2017-07-31 14:14:33 -0400338 if (!mLazyStream)
339 {
340 return 0;
341 }
342
343 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400344 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000345}
346
Geoff Lange1a27752015-10-05 13:16:04 -0400347void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000348{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400349 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000350
351 if (bufSize > 0)
352 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400353 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400354
Jamie Madill23176ce2017-07-31 14:14:33 -0400355 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000356 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400357 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
358 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000359 }
360
361 infoLog[index] = '\0';
362 }
363
364 if (length)
365 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400366 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000367 }
368}
369
370// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300371// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000372// messages, so lets remove all occurrences of this fake file path from the log.
373void InfoLog::appendSanitized(const char *message)
374{
Jamie Madill23176ce2017-07-31 14:14:33 -0400375 ensureInitialized();
376
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000377 std::string msg(message);
378
379 size_t found;
380 do
381 {
382 found = msg.find(g_fakepath);
383 if (found != std::string::npos)
384 {
385 msg.erase(found, strlen(g_fakepath));
386 }
387 }
388 while (found != std::string::npos);
389
Jamie Madill23176ce2017-07-31 14:14:33 -0400390 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000391}
392
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000393void InfoLog::reset()
394{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000395}
396
Olli Etuaho1734e172017-10-27 15:30:27 +0300397VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000398{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500399}
400
Olli Etuahoc8538042017-09-27 11:20:15 +0300401VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300402 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500403{
Olli Etuahoc8538042017-09-27 11:20:15 +0300404 ASSERT(arrayIndex != GL_INVALID_INDEX);
405}
406
Geoff Langd8605522016-04-13 10:19:12 -0400407void Program::Bindings::bindLocation(GLuint index, const std::string &name)
408{
409 mBindings[name] = index;
410}
411
412int Program::Bindings::getBinding(const std::string &name) const
413{
414 auto iter = mBindings.find(name);
415 return (iter != mBindings.end()) ? iter->second : -1;
416}
417
418Program::Bindings::const_iterator Program::Bindings::begin() const
419{
420 return mBindings.begin();
421}
422
423Program::Bindings::const_iterator Program::Bindings::end() const
424{
425 return mBindings.end();
426}
427
Jamie Madill48ef11b2016-04-27 15:21:52 -0400428ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500429 : mLabel(),
430 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400431 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500433 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400434 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500435 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800436 mImageUniformRange(0, 0),
437 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300438 mBinaryRetrieveableHint(false),
439 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400440{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300441 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400442}
443
Jamie Madill48ef11b2016-04-27 15:21:52 -0400444ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400445{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500446 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400447}
448
Jamie Madill48ef11b2016-04-27 15:21:52 -0400449const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500450{
451 return mLabel;
452}
453
Jamie Madille7d84322017-01-10 18:21:59 -0500454GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400455{
jchen1015015f72017-03-16 13:54:21 +0800456 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400457}
458
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800459GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
460{
461 return GetResourceIndexFromName(mBufferVariables, name);
462}
463
Jamie Madille7d84322017-01-10 18:21:59 -0500464GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
465{
466 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
467 return mUniformLocations[location].index;
468}
469
470Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
471{
472 GLuint index = getUniformIndexFromLocation(location);
473 if (!isSamplerUniformIndex(index))
474 {
475 return Optional<GLuint>::Invalid();
476 }
477
478 return getSamplerIndexFromUniformIndex(index);
479}
480
481bool ProgramState::isSamplerUniformIndex(GLuint index) const
482{
Jamie Madill982f6e02017-06-07 14:33:04 -0400483 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500484}
485
486GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
487{
488 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400489 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500490}
491
Jamie Madill34ca4f52017-06-13 11:49:39 -0400492GLuint ProgramState::getAttributeLocation(const std::string &name) const
493{
494 for (const sh::Attribute &attribute : mAttributes)
495 {
496 if (attribute.name == name)
497 {
498 return attribute.location;
499 }
500 }
501
502 return static_cast<GLuint>(-1);
503}
504
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500505Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400506 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400507 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500508 mLinked(false),
509 mDeleteStatus(false),
510 mRefCount(0),
511 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500512 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500513{
514 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000515
Geoff Lang7dd2e102014-11-10 15:19:26 -0500516 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517}
518
519Program::~Program()
520{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400521 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522}
523
Jamie Madill4928b7c2017-06-20 12:57:39 -0400524void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500525{
526 if (mState.mAttachedVertexShader != nullptr)
527 {
528 mState.mAttachedVertexShader->release(context);
529 mState.mAttachedVertexShader = nullptr;
530 }
531
532 if (mState.mAttachedFragmentShader != nullptr)
533 {
534 mState.mAttachedFragmentShader->release(context);
535 mState.mAttachedFragmentShader = nullptr;
536 }
537
538 if (mState.mAttachedComputeShader != nullptr)
539 {
540 mState.mAttachedComputeShader->release(context);
541 mState.mAttachedComputeShader = nullptr;
542 }
543
Jamie Madillc564c072017-06-01 12:45:42 -0400544 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400545
546 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
547 !mState.mAttachedComputeShader);
548 SafeDelete(mProgram);
549
550 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500551}
552
Geoff Lang70d0f492015-12-10 17:45:46 -0500553void Program::setLabel(const std::string &label)
554{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400555 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500556}
557
558const std::string &Program::getLabel() const
559{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400560 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500561}
562
Jamie Madillef300b12016-10-07 15:12:09 -0400563void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300565 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300567 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568 {
Jamie Madillef300b12016-10-07 15:12:09 -0400569 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300570 mState.mAttachedVertexShader = shader;
571 mState.mAttachedVertexShader->addRef();
572 break;
573 }
574 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575 {
Jamie Madillef300b12016-10-07 15:12:09 -0400576 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300577 mState.mAttachedFragmentShader = shader;
578 mState.mAttachedFragmentShader->addRef();
579 break;
580 }
581 case GL_COMPUTE_SHADER:
582 {
Jamie Madillef300b12016-10-07 15:12:09 -0400583 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300584 mState.mAttachedComputeShader = shader;
585 mState.mAttachedComputeShader->addRef();
586 break;
587 }
588 default:
589 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591}
592
Jamie Madillc1d770e2017-04-13 17:31:24 -0400593void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300595 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300597 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400599 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500600 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300601 mState.mAttachedVertexShader = nullptr;
602 break;
603 }
604 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400606 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500607 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300608 mState.mAttachedFragmentShader = nullptr;
609 break;
610 }
611 case GL_COMPUTE_SHADER:
612 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400613 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500614 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300615 mState.mAttachedComputeShader = nullptr;
616 break;
617 }
618 default:
619 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621}
622
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000623int Program::getAttachedShadersCount() const
624{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300625 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
626 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000627}
628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629void Program::bindAttributeLocation(GLuint index, const char *name)
630{
Geoff Langd8605522016-04-13 10:19:12 -0400631 mAttributeBindings.bindLocation(index, name);
632}
633
634void Program::bindUniformLocation(GLuint index, const char *name)
635{
Olli Etuahod2551232017-10-26 20:03:33 +0300636 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
Sami Väisänen46eaa942016-06-29 10:26:37 +0300639void Program::bindFragmentInputLocation(GLint index, const char *name)
640{
641 mFragmentInputBindings.bindLocation(index, name);
642}
643
Jamie Madillbd044ed2017-06-05 12:59:21 -0400644BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300645{
646 BindingInfo ret;
647 ret.type = GL_NONE;
648 ret.valid = false;
649
Jamie Madillbd044ed2017-06-05 12:59:21 -0400650 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300651 ASSERT(fragmentShader);
652
653 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800654 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300655
656 for (const auto &binding : mFragmentInputBindings)
657 {
658 if (binding.second != static_cast<GLuint>(index))
659 continue;
660
661 ret.valid = true;
662
Olli Etuahod2551232017-10-26 20:03:33 +0300663 size_t nameLengthWithoutArrayIndex;
664 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300665
666 for (const auto &in : inputs)
667 {
Olli Etuahod2551232017-10-26 20:03:33 +0300668 if (in.name.length() == nameLengthWithoutArrayIndex &&
669 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300670 {
671 if (in.isArray())
672 {
673 // The client wants to bind either "name" or "name[0]".
674 // GL ES 3.1 spec refers to active array names with language such as:
675 // "if the string identifies the base name of an active array, where the
676 // string would exactly match the name of the variable if the suffix "[0]"
677 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400678 if (arrayIndex == GL_INVALID_INDEX)
679 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300680
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400681 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300682 }
683 else
684 {
685 ret.name = in.mappedName;
686 }
687 ret.type = in.type;
688 return ret;
689 }
690 }
691 }
692
693 return ret;
694}
695
Jamie Madillbd044ed2017-06-05 12:59:21 -0400696void Program::pathFragmentInputGen(const Context *context,
697 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300698 GLenum genMode,
699 GLint components,
700 const GLfloat *coeffs)
701{
702 // If the location is -1 then the command is silently ignored
703 if (index == -1)
704 return;
705
Jamie Madillbd044ed2017-06-05 12:59:21 -0400706 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300707
708 // If the input doesn't exist then then the command is silently ignored
709 // This could happen through optimization for example, the shader translator
710 // decides that a variable is not actually being used and optimizes it away.
711 if (binding.name.empty())
712 return;
713
714 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
715}
716
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717// The attached shaders are checked for linking errors by matching up their variables.
718// Uniform, input and output variables get collected.
719// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500720Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000721{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500722 const auto &data = context->getContextState();
723
Jamie Madill6c58b062017-08-01 13:44:25 -0400724 auto *platform = ANGLEPlatformCurrent();
725 double startTime = platform->currentTime(platform);
726
Jamie Madill6c1f6712017-02-14 19:08:04 -0500727 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000728
Jamie Madill32447362017-06-28 14:53:52 -0400729 ProgramHash programHash;
730 auto *cache = context->getMemoryProgramCache();
731 if (cache)
732 {
733 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400734 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400735 }
736
737 if (mLinked)
738 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400739 double delta = platform->currentTime(platform) - startTime;
740 int us = static_cast<int>(delta * 1000000.0);
741 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400742 return NoError();
743 }
744
745 // Cache load failed, fall through to normal linking.
746 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000747 mInfoLog.reset();
748
Martin Radev4c4c8e72016-08-04 12:25:34 +0300749 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500750
Jamie Madill192745a2016-12-22 15:58:21 -0500751 auto vertexShader = mState.mAttachedVertexShader;
752 auto fragmentShader = mState.mAttachedFragmentShader;
753 auto computeShader = mState.mAttachedComputeShader;
754
755 bool isComputeShaderAttached = (computeShader != nullptr);
756 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300757 // Check whether we both have a compute and non-compute shaders attached.
758 // If there are of both types attached, then linking should fail.
759 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
760 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500761 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300762 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
763 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400764 }
765
Jamie Madill192745a2016-12-22 15:58:21 -0500766 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500767 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400768 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300769 {
770 mInfoLog << "Attached compute shader is not compiled.";
771 return NoError();
772 }
Jamie Madill192745a2016-12-22 15:58:21 -0500773 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300774
Jamie Madillbd044ed2017-06-05 12:59:21 -0400775 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300776
777 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
778 // If the work group size is not specified, a link time error should occur.
779 if (!mState.mComputeShaderLocalSize.isDeclared())
780 {
781 mInfoLog << "Work group size is not specified.";
782 return NoError();
783 }
784
Jamie Madillbd044ed2017-06-05 12:59:21 -0400785 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300786 {
787 return NoError();
788 }
789
Jiajia Qin729b2c62017-08-14 09:36:11 +0800790 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300791 {
792 return NoError();
793 }
794
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800795 ProgramLinkedResources resources = {
796 {0, PackMode::ANGLE_RELAXED},
797 {&mState.mUniformBlocks, &mState.mUniforms},
798 {&mState.mShaderStorageBlocks, &mState.mBufferVariables}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500799
800 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
801 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
802
803 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500804 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300805 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500806 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300807 }
808 }
809 else
810 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400811 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300812 {
813 return NoError();
814 }
Jamie Madill192745a2016-12-22 15:58:21 -0500815 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300816
Jamie Madillbd044ed2017-06-05 12:59:21 -0400817 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300818 {
819 return NoError();
820 }
Jamie Madill192745a2016-12-22 15:58:21 -0500821 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300822
Jamie Madillbd044ed2017-06-05 12:59:21 -0400823 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300824 {
825 mInfoLog << "Fragment shader version does not match vertex shader version.";
826 return NoError();
827 }
828
Jamie Madillbd044ed2017-06-05 12:59:21 -0400829 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300830 {
831 return NoError();
832 }
833
Jamie Madillbd044ed2017-06-05 12:59:21 -0400834 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300835 {
836 return NoError();
837 }
838
Jamie Madillbd044ed2017-06-05 12:59:21 -0400839 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300840 {
841 return NoError();
842 }
843
Jiajia Qin729b2c62017-08-14 09:36:11 +0800844 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300845 {
846 return NoError();
847 }
848
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400849 if (!linkValidateGlobalNames(context, mInfoLog))
850 {
851 return NoError();
852 }
853
Jamie Madillbd044ed2017-06-05 12:59:21 -0400854 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300855
Martin Radev7cf61662017-07-26 17:10:53 +0300856 mState.mNumViews = vertexShader->getNumViews(context);
857
Jamie Madillbd044ed2017-06-05 12:59:21 -0400858 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300859
Jamie Madill192745a2016-12-22 15:58:21 -0500860 // Map the varyings to the register file
861 // In WebGL, we use a slightly different handling for packing variables.
862 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
863 : PackMode::ANGLE_RELAXED;
Jamie Madillc9727f32017-11-07 12:37:07 -0500864
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800865 ProgramLinkedResources resources = {
866 {data.getCaps().maxVaryingVectors, packMode},
867 {&mState.mUniformBlocks, &mState.mUniforms},
868 {&mState.mShaderStorageBlocks, &mState.mBufferVariables}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500869
870 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
871 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
872
jchen1085c93c42017-11-12 15:36:47 +0800873 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Jamie Madill192745a2016-12-22 15:58:21 -0500874 {
875 return NoError();
876 }
877
jchen1085c93c42017-11-12 15:36:47 +0800878 if (!resources.varyingPacking.collectAndPackUserVaryings(
879 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +0300880 {
881 return NoError();
882 }
883
Jamie Madillc9727f32017-11-07 12:37:07 -0500884 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500885 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300886 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500887 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300888 }
889
890 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500891 }
892
jchen10eaef1e52017-06-13 10:44:11 +0800893 gatherAtomicCounterBuffers();
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500894 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400895
jchen10eaef1e52017-06-13 10:44:11 +0800896 setUniformValuesFromBindingQualifiers();
897
Jamie Madill54164b02017-08-28 15:17:37 -0400898 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400899 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400900
Jamie Madill32447362017-06-28 14:53:52 -0400901 // Save to the program cache.
902 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
903 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
904 {
905 cache->putProgram(programHash, context, this);
906 }
907
Jamie Madill6c58b062017-08-01 13:44:25 -0400908 double delta = platform->currentTime(platform) - startTime;
909 int us = static_cast<int>(delta * 1000000.0);
910 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
911
Martin Radev4c4c8e72016-08-04 12:25:34 +0300912 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000913}
914
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000915// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500916void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400918 mState.mAttributes.clear();
919 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400920 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800921 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400922 mState.mUniforms.clear();
923 mState.mUniformLocations.clear();
924 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800925 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800926 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800928 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400929 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400930 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300931 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500932 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800933 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300934 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500935
Geoff Lang7dd2e102014-11-10 15:19:26 -0500936 mValidated = false;
937
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000938 mLinked = false;
939}
940
Geoff Lange1a27752015-10-05 13:16:04 -0400941bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000942{
943 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
Jamie Madilla2c74982016-12-12 11:20:42 -0500946Error Program::loadBinary(const Context *context,
947 GLenum binaryFormat,
948 const void *binary,
949 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000950{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500951 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000952
Geoff Lang7dd2e102014-11-10 15:19:26 -0500953#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800954 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500955#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400956 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
957 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000958 {
Jamie Madillf6113162015-05-07 11:49:21 -0400959 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800960 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500961 }
962
Jamie Madill4f86d052017-06-05 12:59:26 -0400963 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
964 ANGLE_TRY_RESULT(
965 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400966
967 // Currently we require the full shader text to compute the program hash.
968 // TODO(jmadill): Store the binary in the internal program cache.
969
Jamie Madillb0a838b2016-11-13 20:02:12 -0500970 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500971#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500972}
973
Jamie Madilla2c74982016-12-12 11:20:42 -0500974Error Program::saveBinary(const Context *context,
975 GLenum *binaryFormat,
976 void *binary,
977 GLsizei bufSize,
978 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500979{
980 if (binaryFormat)
981 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400982 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500983 }
984
Jamie Madill4f86d052017-06-05 12:59:26 -0400985 angle::MemoryBuffer memoryBuf;
986 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500987
Jamie Madill4f86d052017-06-05 12:59:26 -0400988 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
989 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500990
991 if (streamLength > bufSize)
992 {
993 if (length)
994 {
995 *length = 0;
996 }
997
998 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
999 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1000 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001001 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001002 }
1003
1004 if (binary)
1005 {
1006 char *ptr = reinterpret_cast<char*>(binary);
1007
Jamie Madill48ef11b2016-04-27 15:21:52 -04001008 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001009 ptr += streamLength;
1010
1011 ASSERT(ptr - streamLength == binary);
1012 }
1013
1014 if (length)
1015 {
1016 *length = streamLength;
1017 }
1018
He Yunchaoacd18982017-01-04 10:46:42 +08001019 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020}
1021
Jamie Madillffe00c02017-06-27 16:26:55 -04001022GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023{
1024 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001025 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001026 if (error.isError())
1027 {
1028 return 0;
1029 }
1030
1031 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001032}
1033
Geoff Langc5629752015-12-07 16:29:04 -05001034void Program::setBinaryRetrievableHint(bool retrievable)
1035{
1036 // TODO(jmadill) : replace with dirty bits
1037 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001038 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001039}
1040
1041bool Program::getBinaryRetrievableHint() const
1042{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001043 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001044}
1045
Yunchao He61afff12017-03-14 15:34:03 +08001046void Program::setSeparable(bool separable)
1047{
1048 // TODO(yunchao) : replace with dirty bits
1049 if (mState.mSeparable != separable)
1050 {
1051 mProgram->setSeparable(separable);
1052 mState.mSeparable = separable;
1053 }
1054}
1055
1056bool Program::isSeparable() const
1057{
1058 return mState.mSeparable;
1059}
1060
Jamie Madill6c1f6712017-02-14 19:08:04 -05001061void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001062{
1063 mRefCount--;
1064
1065 if (mRefCount == 0 && mDeleteStatus)
1066 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001067 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001068 }
1069}
1070
1071void Program::addRef()
1072{
1073 mRefCount++;
1074}
1075
1076unsigned int Program::getRefCount() const
1077{
1078 return mRefCount;
1079}
1080
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001081int Program::getInfoLogLength() const
1082{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001083 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001084}
1085
Geoff Lange1a27752015-10-05 13:16:04 -04001086void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001087{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001088 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001089}
1090
Geoff Lange1a27752015-10-05 13:16:04 -04001091void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001092{
1093 int total = 0;
1094
Martin Radev4c4c8e72016-08-04 12:25:34 +03001095 if (mState.mAttachedComputeShader)
1096 {
1097 if (total < maxCount)
1098 {
1099 shaders[total] = mState.mAttachedComputeShader->getHandle();
1100 total++;
1101 }
1102 }
1103
Jamie Madill48ef11b2016-04-27 15:21:52 -04001104 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001105 {
1106 if (total < maxCount)
1107 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001108 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001109 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001110 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001111 }
1112
Jamie Madill48ef11b2016-04-27 15:21:52 -04001113 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001114 {
1115 if (total < maxCount)
1116 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001117 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001118 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001119 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001120 }
1121
1122 if (count)
1123 {
1124 *count = total;
1125 }
1126}
1127
Geoff Lange1a27752015-10-05 13:16:04 -04001128GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001129{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001130 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001131}
1132
Jamie Madill63805b42015-08-25 13:17:39 -04001133bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001134{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001135 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1136 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001137}
1138
jchen10fd7c3b52017-03-21 15:36:03 +08001139void Program::getActiveAttribute(GLuint index,
1140 GLsizei bufsize,
1141 GLsizei *length,
1142 GLint *size,
1143 GLenum *type,
1144 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001145{
Jamie Madillc349ec02015-08-21 16:53:12 -04001146 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001147 {
1148 if (bufsize > 0)
1149 {
1150 name[0] = '\0';
1151 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001153 if (length)
1154 {
1155 *length = 0;
1156 }
1157
1158 *type = GL_NONE;
1159 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001160 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001161 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001162
jchen1036e120e2017-03-14 14:53:58 +08001163 ASSERT(index < mState.mAttributes.size());
1164 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001165
1166 if (bufsize > 0)
1167 {
jchen10fd7c3b52017-03-21 15:36:03 +08001168 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001169 }
1170
1171 // Always a single 'type' instance
1172 *size = 1;
1173 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001174}
1175
Geoff Lange1a27752015-10-05 13:16:04 -04001176GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001177{
Jamie Madillc349ec02015-08-21 16:53:12 -04001178 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001179 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001180 return 0;
1181 }
1182
jchen1036e120e2017-03-14 14:53:58 +08001183 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184}
1185
Geoff Lange1a27752015-10-05 13:16:04 -04001186GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001187{
Jamie Madillc349ec02015-08-21 16:53:12 -04001188 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001189 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001190 return 0;
1191 }
1192
1193 size_t maxLength = 0;
1194
Jamie Madill48ef11b2016-04-27 15:21:52 -04001195 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001196 {
jchen1036e120e2017-03-14 14:53:58 +08001197 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001198 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199
Jamie Madillc349ec02015-08-21 16:53:12 -04001200 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201}
1202
jchen1015015f72017-03-16 13:54:21 +08001203GLuint Program::getInputResourceIndex(const GLchar *name) const
1204{
Olli Etuahod2551232017-10-26 20:03:33 +03001205 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001206}
1207
1208GLuint Program::getOutputResourceIndex(const GLchar *name) const
1209{
1210 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1211}
1212
jchen10fd7c3b52017-03-21 15:36:03 +08001213size_t Program::getOutputResourceCount() const
1214{
1215 return (mLinked ? mState.mOutputVariables.size() : 0);
1216}
1217
jchen10baf5d942017-08-28 20:45:48 +08001218template <typename T>
1219void Program::getResourceName(GLuint index,
1220 const std::vector<T> &resources,
1221 GLsizei bufSize,
1222 GLsizei *length,
1223 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001224{
1225 if (length)
1226 {
1227 *length = 0;
1228 }
1229
1230 if (!mLinked)
1231 {
1232 if (bufSize > 0)
1233 {
1234 name[0] = '\0';
1235 }
1236 return;
1237 }
jchen10baf5d942017-08-28 20:45:48 +08001238 ASSERT(index < resources.size());
1239 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001240
1241 if (bufSize > 0)
1242 {
Olli Etuahod2551232017-10-26 20:03:33 +03001243 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001244 }
1245}
1246
jchen10baf5d942017-08-28 20:45:48 +08001247void Program::getInputResourceName(GLuint index,
1248 GLsizei bufSize,
1249 GLsizei *length,
1250 GLchar *name) const
1251{
1252 getResourceName(index, mState.mAttributes, bufSize, length, name);
1253}
1254
1255void Program::getOutputResourceName(GLuint index,
1256 GLsizei bufSize,
1257 GLsizei *length,
1258 GLchar *name) const
1259{
1260 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1261}
1262
1263void Program::getUniformResourceName(GLuint index,
1264 GLsizei bufSize,
1265 GLsizei *length,
1266 GLchar *name) const
1267{
1268 getResourceName(index, mState.mUniforms, bufSize, length, name);
1269}
1270
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001271void Program::getBufferVariableResourceName(GLuint index,
1272 GLsizei bufSize,
1273 GLsizei *length,
1274 GLchar *name) const
1275{
1276 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1277}
1278
jchen10880683b2017-04-12 16:21:55 +08001279const sh::Attribute &Program::getInputResource(GLuint index) const
1280{
1281 ASSERT(index < mState.mAttributes.size());
1282 return mState.mAttributes[index];
1283}
1284
1285const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1286{
1287 ASSERT(index < mState.mOutputVariables.size());
1288 return mState.mOutputVariables[index];
1289}
1290
Geoff Lang7dd2e102014-11-10 15:19:26 -05001291GLint Program::getFragDataLocation(const std::string &name) const
1292{
Olli Etuahod2551232017-10-26 20:03:33 +03001293 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001294}
1295
Geoff Lange1a27752015-10-05 13:16:04 -04001296void Program::getActiveUniform(GLuint index,
1297 GLsizei bufsize,
1298 GLsizei *length,
1299 GLint *size,
1300 GLenum *type,
1301 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001302{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001303 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001304 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001305 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001306 ASSERT(index < mState.mUniforms.size());
1307 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308
1309 if (bufsize > 0)
1310 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001311 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001312 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313 }
1314
Jamie Madill62d31cb2015-09-11 13:25:51 -04001315 *size = uniform.elementCount();
1316 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001317 }
1318 else
1319 {
1320 if (bufsize > 0)
1321 {
1322 name[0] = '\0';
1323 }
1324
1325 if (length)
1326 {
1327 *length = 0;
1328 }
1329
1330 *size = 0;
1331 *type = GL_NONE;
1332 }
1333}
1334
Geoff Lange1a27752015-10-05 13:16:04 -04001335GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001336{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001338 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001339 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001340 }
1341 else
1342 {
1343 return 0;
1344 }
1345}
1346
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001347size_t Program::getActiveBufferVariableCount() const
1348{
1349 return mLinked ? mState.mBufferVariables.size() : 0;
1350}
1351
Geoff Lange1a27752015-10-05 13:16:04 -04001352GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001353{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001354 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355
1356 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001357 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001358 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001360 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001362 size_t length = uniform.name.length() + 1u;
1363 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364 {
1365 length += 3; // Counting in "[0]".
1366 }
1367 maxLength = std::max(length, maxLength);
1368 }
1369 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001370 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371
Jamie Madill62d31cb2015-09-11 13:25:51 -04001372 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375bool Program::isValidUniformLocation(GLint location) const
1376{
Jamie Madille2e406c2016-06-02 13:04:10 -04001377 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001378 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001379 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001380}
1381
Jamie Madill62d31cb2015-09-11 13:25:51 -04001382const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001384 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001385 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386}
1387
Jamie Madillac4e9c32017-01-13 14:07:12 -05001388const VariableLocation &Program::getUniformLocation(GLint location) const
1389{
1390 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1391 return mState.mUniformLocations[location];
1392}
1393
1394const std::vector<VariableLocation> &Program::getUniformLocations() const
1395{
1396 return mState.mUniformLocations;
1397}
1398
1399const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1400{
1401 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1402 return mState.mUniforms[index];
1403}
1404
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001405const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1406{
1407 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1408 return mState.mBufferVariables[index];
1409}
1410
Jamie Madill62d31cb2015-09-11 13:25:51 -04001411GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412{
Olli Etuahod2551232017-10-26 20:03:33 +03001413 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414}
1415
Jamie Madill62d31cb2015-09-11 13:25:51 -04001416GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417{
Jamie Madille7d84322017-01-10 18:21:59 -05001418 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419}
1420
1421void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1422{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001423 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1424 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001425 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426}
1427
1428void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1429{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001430 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1431 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001432 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433}
1434
1435void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1436{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001437 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1438 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001439 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440}
1441
1442void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1443{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001444 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1445 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001446 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447}
1448
Jamie Madill81c2e252017-09-09 23:32:46 -04001449Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001451 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1452 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1453
Jamie Madill81c2e252017-09-09 23:32:46 -04001454 mProgram->setUniform1iv(location, clampedCount, v);
1455
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001456 if (mState.isSamplerUniformIndex(locationInfo.index))
1457 {
1458 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001459 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001460 }
1461
Jamie Madill81c2e252017-09-09 23:32:46 -04001462 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463}
1464
1465void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1466{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001467 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1468 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001469 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470}
1471
1472void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1473{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001474 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1475 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001476 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477}
1478
1479void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1480{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001481 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1482 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001483 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484}
1485
1486void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1487{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001488 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1489 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001490 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001491}
1492
1493void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1494{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001495 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1496 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001497 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498}
1499
1500void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1501{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001502 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1503 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001504 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001505}
1506
1507void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1508{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001509 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1510 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001511 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001512}
1513
1514void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1515{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001516 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001517 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518}
1519
1520void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1521{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001522 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001523 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524}
1525
1526void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1527{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001528 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001529 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530}
1531
1532void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1533{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001534 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001535 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001536}
1537
1538void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1539{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001540 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001541 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001542}
1543
1544void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1545{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001546 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001547 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548}
1549
1550void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1551{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001552 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001553 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001554}
1555
1556void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1557{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001558 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001559 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560}
1561
1562void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1563{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001564 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001565 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001566}
1567
Jamie Madill54164b02017-08-28 15:17:37 -04001568void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001569{
Jamie Madill54164b02017-08-28 15:17:37 -04001570 const auto &uniformLocation = mState.getUniformLocations()[location];
1571 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1572
1573 GLenum nativeType = gl::VariableComponentType(uniform.type);
1574 if (nativeType == GL_FLOAT)
1575 {
1576 mProgram->getUniformfv(context, location, v);
1577 }
1578 else
1579 {
1580 getUniformInternal(context, v, location, nativeType,
1581 gl::VariableComponentCount(uniform.type));
1582 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
Jamie Madill54164b02017-08-28 15:17:37 -04001585void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586{
Jamie Madill54164b02017-08-28 15:17:37 -04001587 const auto &uniformLocation = mState.getUniformLocations()[location];
1588 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1589
1590 GLenum nativeType = gl::VariableComponentType(uniform.type);
1591 if (nativeType == GL_INT || nativeType == GL_BOOL)
1592 {
1593 mProgram->getUniformiv(context, location, v);
1594 }
1595 else
1596 {
1597 getUniformInternal(context, v, location, nativeType,
1598 gl::VariableComponentCount(uniform.type));
1599 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001600}
1601
Jamie Madill54164b02017-08-28 15:17:37 -04001602void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603{
Jamie Madill54164b02017-08-28 15:17:37 -04001604 const auto &uniformLocation = mState.getUniformLocations()[location];
1605 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1606
1607 GLenum nativeType = gl::VariableComponentType(uniform.type);
1608 if (nativeType == GL_UNSIGNED_INT)
1609 {
1610 mProgram->getUniformuiv(context, location, v);
1611 }
1612 else
1613 {
1614 getUniformInternal(context, v, location, nativeType,
1615 gl::VariableComponentCount(uniform.type));
1616 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617}
1618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619void Program::flagForDeletion()
1620{
1621 mDeleteStatus = true;
1622}
1623
1624bool Program::isFlaggedForDeletion() const
1625{
1626 return mDeleteStatus;
1627}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001628
Brandon Jones43a53e22014-08-28 16:23:22 -07001629void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001630{
1631 mInfoLog.reset();
1632
Geoff Lang7dd2e102014-11-10 15:19:26 -05001633 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001634 {
Geoff Lang92019432017-11-20 13:09:34 -05001635 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001636 }
1637 else
1638 {
Jamie Madillf6113162015-05-07 11:49:21 -04001639 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001640 }
1641}
1642
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1644{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001645 // Skip cache if we're using an infolog, so we get the full error.
1646 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1647 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1648 {
1649 return mCachedValidateSamplersResult.value();
1650 }
1651
1652 if (mTextureUnitTypesCache.empty())
1653 {
1654 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1655 }
1656 else
1657 {
1658 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1659 }
1660
1661 // if any two active samplers in a program are of different types, but refer to the same
1662 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1663 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001664 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001665 {
Jamie Madill54164b02017-08-28 15:17:37 -04001666 if (samplerBinding.unreferenced)
1667 continue;
1668
Jamie Madille7d84322017-01-10 18:21:59 -05001669 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001670
Jamie Madille7d84322017-01-10 18:21:59 -05001671 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001672 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001673 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1674 {
1675 if (infoLog)
1676 {
1677 (*infoLog) << "Sampler uniform (" << textureUnit
1678 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1679 << caps.maxCombinedTextureImageUnits << ")";
1680 }
1681
1682 mCachedValidateSamplersResult = false;
1683 return false;
1684 }
1685
1686 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1687 {
1688 if (textureType != mTextureUnitTypesCache[textureUnit])
1689 {
1690 if (infoLog)
1691 {
1692 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1693 "image unit ("
1694 << textureUnit << ").";
1695 }
1696
1697 mCachedValidateSamplersResult = false;
1698 return false;
1699 }
1700 }
1701 else
1702 {
1703 mTextureUnitTypesCache[textureUnit] = textureType;
1704 }
1705 }
1706 }
1707
1708 mCachedValidateSamplersResult = true;
1709 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001710}
1711
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001712bool Program::isValidated() const
1713{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001714 return mValidated;
1715}
1716
Geoff Lange1a27752015-10-05 13:16:04 -04001717GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001719 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001720}
1721
jchen1058f67be2017-10-27 08:59:27 +08001722GLuint Program::getActiveAtomicCounterBufferCount() const
1723{
1724 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1725}
1726
Jiajia Qin729b2c62017-08-14 09:36:11 +08001727GLuint Program::getActiveShaderStorageBlockCount() const
1728{
1729 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1730}
1731
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001732void Program::getActiveUniformBlockName(const GLuint blockIndex,
1733 GLsizei bufSize,
1734 GLsizei *length,
1735 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001736{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001737 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
1738}
Geoff Lang7dd2e102014-11-10 15:19:26 -05001739
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001740void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
1741 GLsizei bufSize,
1742 GLsizei *length,
1743 GLchar *blockName) const
1744{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001745
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001746 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001747}
1748
Geoff Lange1a27752015-10-05 13:16:04 -04001749GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001750{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751 int maxLength = 0;
1752
1753 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001754 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001755 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001756 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1757 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001758 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001759 if (!uniformBlock.name.empty())
1760 {
jchen10af713a22017-04-19 09:10:56 +08001761 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1762 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763 }
1764 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001765 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001766
1767 return maxLength;
1768}
1769
Geoff Lange1a27752015-10-05 13:16:04 -04001770GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001772 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
1773}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001774
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001775GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
1776{
1777 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001778}
1779
Jiajia Qin729b2c62017-08-14 09:36:11 +08001780const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001781{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001782 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1783 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001784}
1785
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001786const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
1787{
1788 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
1789 return mState.mShaderStorageBlocks[index];
1790}
1791
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001792void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1793{
jchen107a20b972017-06-13 14:25:26 +08001794 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001795 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001796 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001797}
1798
1799GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1800{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001801 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001802}
1803
Jiajia Qin729b2c62017-08-14 09:36:11 +08001804GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1805{
1806 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1807}
1808
Geoff Lang48dcae72014-02-05 16:28:24 -05001809void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1810{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001811 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001812 for (GLsizei i = 0; i < count; i++)
1813 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001814 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001815 }
1816
Jamie Madill48ef11b2016-04-27 15:21:52 -04001817 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001818}
1819
1820void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1821{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001823 {
jchen10a9042d32017-03-17 08:50:45 +08001824 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1825 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1826 std::string varName = var.nameWithArrayIndex();
1827 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001828 if (length)
1829 {
1830 *length = lastNameIdx;
1831 }
1832 if (size)
1833 {
jchen10a9042d32017-03-17 08:50:45 +08001834 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001835 }
1836 if (type)
1837 {
jchen10a9042d32017-03-17 08:50:45 +08001838 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001839 }
1840 if (name)
1841 {
jchen10a9042d32017-03-17 08:50:45 +08001842 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001843 name[lastNameIdx] = '\0';
1844 }
1845 }
1846}
1847
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001848GLsizei Program::getTransformFeedbackVaryingCount() const
1849{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001851 {
jchen10a9042d32017-03-17 08:50:45 +08001852 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001853 }
1854 else
1855 {
1856 return 0;
1857 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001858}
1859
1860GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1861{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001862 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001863 {
1864 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001865 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001866 {
jchen10a9042d32017-03-17 08:50:45 +08001867 maxSize =
1868 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001869 }
1870
1871 return maxSize;
1872 }
1873 else
1874 {
1875 return 0;
1876 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001877}
1878
1879GLenum Program::getTransformFeedbackBufferMode() const
1880{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001881 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001882}
1883
Jamie Madillbd044ed2017-06-05 12:59:21 -04001884bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001886 Shader *vertexShader = mState.mAttachedVertexShader;
1887 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001888
Jamie Madillbd044ed2017-06-05 12:59:21 -04001889 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001890
Jiawei Shao3d404882017-10-16 13:30:48 +08001891 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
1892 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893
Sami Väisänen46eaa942016-06-29 10:26:37 +03001894 std::map<GLuint, std::string> staticFragmentInputLocations;
1895
Jamie Madill4cff2472015-08-21 16:53:18 -04001896 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898 bool matched = false;
1899
1900 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001901 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902 {
1903 continue;
1904 }
1905
Jamie Madill4cff2472015-08-21 16:53:18 -04001906 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001908 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001909 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001910 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001911 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001912 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 {
1914 return false;
1915 }
1916
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917 matched = true;
1918 break;
1919 }
1920 }
1921
1922 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001923 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001925 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001926 return false;
1927 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001928
1929 // Check for aliased path rendering input bindings (if any).
1930 // If more than one binding refer statically to the same
1931 // location the link must fail.
1932
1933 if (!output.staticUse)
1934 continue;
1935
1936 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1937 if (inputBinding == -1)
1938 continue;
1939
1940 const auto it = staticFragmentInputLocations.find(inputBinding);
1941 if (it == std::end(staticFragmentInputLocations))
1942 {
1943 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1944 }
1945 else
1946 {
1947 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1948 << it->second;
1949 return false;
1950 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951 }
1952
Jamie Madillbd044ed2017-06-05 12:59:21 -04001953 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001954 {
1955 return false;
1956 }
1957
Jamie Madillada9ecc2015-08-17 12:53:37 -04001958 // TODO(jmadill): verify no unmatched vertex varyings?
1959
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 return true;
1961}
1962
Jamie Madillbd044ed2017-06-05 12:59:21 -04001963bool Program::linkUniforms(const Context *context,
1964 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001965 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001966{
Olli Etuahob78707c2017-03-09 15:03:11 +00001967 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001968 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001969 {
1970 return false;
1971 }
1972
Olli Etuahob78707c2017-03-09 15:03:11 +00001973 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001974
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001975 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001976
jchen10eaef1e52017-06-13 10:44:11 +08001977 if (!linkAtomicCounterBuffers())
1978 {
1979 return false;
1980 }
1981
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001982 return true;
1983}
1984
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001985void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001986{
Jamie Madill982f6e02017-06-07 14:33:04 -04001987 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1988 unsigned int low = high;
1989
jchen10eaef1e52017-06-13 10:44:11 +08001990 for (auto counterIter = mState.mUniforms.rbegin();
1991 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1992 {
1993 --low;
1994 }
1995
1996 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1997
1998 high = low;
1999
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002000 for (auto imageIter = mState.mUniforms.rbegin();
2001 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2002 {
2003 --low;
2004 }
2005
2006 mState.mImageUniformRange = RangeUI(low, high);
2007
2008 // If uniform is a image type, insert it into the mImageBindings array.
2009 for (unsigned int imageIndex : mState.mImageUniformRange)
2010 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002011 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2012 // cannot load values into a uniform defined as an image. if declare without a
2013 // binding qualifier, any uniform image variable (include all elements of
2014 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002015 auto &imageUniform = mState.mUniforms[imageIndex];
2016 if (imageUniform.binding == -1)
2017 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002018 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002019 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002020 else
2021 {
2022 mState.mImageBindings.emplace_back(
2023 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
2024 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002025 }
2026
2027 high = low;
2028
2029 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002030 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002031 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002032 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002033 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002034
2035 mState.mSamplerUniformRange = RangeUI(low, high);
2036
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002037 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002038 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002039 {
2040 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2041 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2042 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04002043 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002044 }
2045}
2046
jchen10eaef1e52017-06-13 10:44:11 +08002047bool Program::linkAtomicCounterBuffers()
2048{
2049 for (unsigned int index : mState.mAtomicCounterUniformRange)
2050 {
2051 auto &uniform = mState.mUniforms[index];
2052 bool found = false;
2053 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2054 ++bufferIndex)
2055 {
2056 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2057 if (buffer.binding == uniform.binding)
2058 {
2059 buffer.memberIndexes.push_back(index);
2060 uniform.bufferIndex = bufferIndex;
2061 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002062 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002063 break;
2064 }
2065 }
2066 if (!found)
2067 {
2068 AtomicCounterBuffer atomicCounterBuffer;
2069 atomicCounterBuffer.binding = uniform.binding;
2070 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002071 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002072 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2073 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2074 }
2075 }
2076 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2077 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2078
2079 return true;
2080}
2081
Martin Radev4c4c8e72016-08-04 12:25:34 +03002082bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2083 const std::string &uniformName,
2084 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002085 const sh::InterfaceBlockField &fragmentUniform,
2086 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002088 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2089 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2090 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 {
2092 return false;
2093 }
2094
2095 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2096 {
Jamie Madillf6113162015-05-07 11:49:21 -04002097 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 return false;
2099 }
2100
2101 return true;
2102}
2103
Jamie Madilleb979bf2016-11-15 12:28:46 -05002104// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002105bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002107 const ContextState &data = context->getContextState();
2108 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002109
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002111 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002112 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002113
2114 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002115 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002116 {
Jamie Madillf6113162015-05-07 11:49:21 -04002117 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002118 return false;
2119 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002121 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002122
Jamie Madillc349ec02015-08-21 16:53:12 -04002123 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002124 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002125 {
Olli Etuahod2551232017-10-26 20:03:33 +03002126 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2127 // structures, so we don't need to worry about adjusting their names or generating entries
2128 // for each member/element (unlike uniforms for example).
2129 ASSERT(!attribute.isArray() && !attribute.isStruct());
2130
Jamie Madilleb979bf2016-11-15 12:28:46 -05002131 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002132 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002133 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002134 attribute.location = bindingLocation;
2135 }
2136
2137 if (attribute.location != -1)
2138 {
2139 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002140 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002141
Jamie Madill63805b42015-08-25 13:17:39 -04002142 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 {
Jamie Madillf6113162015-05-07 11:49:21 -04002144 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002145 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146
2147 return false;
2148 }
2149
Jamie Madill63805b42015-08-25 13:17:39 -04002150 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 {
Jamie Madill63805b42015-08-25 13:17:39 -04002152 const int regLocation = attribute.location + reg;
2153 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154
2155 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002156 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002157 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002159 // TODO(jmadill): fix aliasing on ES2
2160 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002162 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002163 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164 return false;
2165 }
2166 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002167 else
2168 {
Jamie Madill63805b42015-08-25 13:17:39 -04002169 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002170 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171
Jamie Madill63805b42015-08-25 13:17:39 -04002172 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 }
2174 }
2175 }
2176
2177 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002178 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002180 // Not set by glBindAttribLocation or by location layout qualifier
2181 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182 {
Jamie Madill63805b42015-08-25 13:17:39 -04002183 int regs = VariableRegisterCount(attribute.type);
2184 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002185
Jamie Madill63805b42015-08-25 13:17:39 -04002186 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187 {
Jamie Madillf6113162015-05-07 11:49:21 -04002188 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002189 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190 }
2191
Jamie Madillc349ec02015-08-21 16:53:12 -04002192 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193 }
2194 }
2195
Jamie Madill48ef11b2016-04-27 15:21:52 -04002196 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197 {
Jamie Madill63805b42015-08-25 13:17:39 -04002198 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002199 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002200
Jamie Madillbd159f02017-10-09 19:39:06 -04002201 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002203 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2204 mState.mActiveAttribLocationsMask.set(location);
2205 mState.mMaxActiveAttribLocation =
2206 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207 }
2208 }
2209
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210 return true;
2211}
2212
Martin Radev4c4c8e72016-08-04 12:25:34 +03002213bool Program::validateVertexAndFragmentInterfaceBlocks(
2214 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2215 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002216 InfoLog &infoLog,
2217 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002218{
2219 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002220 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2221 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002222
2223 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2224 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002225 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002226 }
2227
Jamie Madille473dee2015-08-18 14:49:01 -04002228 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002229 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002230 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2231 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002232 {
2233 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002234 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2235 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 {
2237 return false;
2238 }
2239 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002240 // TODO(jiajia.qin@intel.com): Add
2241 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002242 }
2243 return true;
2244}
Jamie Madille473dee2015-08-18 14:49:01 -04002245
Jiajia Qin729b2c62017-08-14 09:36:11 +08002246bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002247{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002248 const auto &caps = context->getCaps();
2249
Martin Radev4c4c8e72016-08-04 12:25:34 +03002250 if (mState.mAttachedComputeShader)
2251 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002252 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002253 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002254
Jiajia Qin729b2c62017-08-14 09:36:11 +08002255 if (!validateInterfaceBlocksCount(
2256 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002257 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2258 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002260 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002261 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002262
2263 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2264 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2265 computeShaderStorageBlocks,
2266 "Compute shader shader storage block count exceeds "
2267 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2268 infoLog))
2269 {
2270 return false;
2271 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002272 return true;
2273 }
2274
Jamie Madillbd044ed2017-06-05 12:59:21 -04002275 Shader &vertexShader = *mState.mAttachedVertexShader;
2276 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002277
Jiajia Qin729b2c62017-08-14 09:36:11 +08002278 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2279 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002280
Jiajia Qin729b2c62017-08-14 09:36:11 +08002281 if (!validateInterfaceBlocksCount(
2282 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002283 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2284 {
2285 return false;
2286 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002287 if (!validateInterfaceBlocksCount(
2288 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002289 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2290 infoLog))
2291 {
2292
2293 return false;
2294 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002295
2296 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002297 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002298 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002299 {
2300 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002301 }
Jamie Madille473dee2015-08-18 14:49:01 -04002302
Jiajia Qin729b2c62017-08-14 09:36:11 +08002303 if (context->getClientVersion() >= Version(3, 1))
2304 {
2305 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2306 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2307
2308 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2309 vertexShaderStorageBlocks,
2310 "Vertex shader shader storage block count exceeds "
2311 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2312 infoLog))
2313 {
2314 return false;
2315 }
2316 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2317 fragmentShaderStorageBlocks,
2318 "Fragment shader shader storage block count exceeds "
2319 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2320 infoLog))
2321 {
2322
2323 return false;
2324 }
2325
2326 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2327 fragmentShaderStorageBlocks, infoLog,
2328 webglCompatibility))
2329 {
2330 return false;
2331 }
2332 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002333 return true;
2334}
2335
Jamie Madilla2c74982016-12-12 11:20:42 -05002336bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002337 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002338 const sh::InterfaceBlock &fragmentInterfaceBlock,
2339 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002340{
2341 const char* blockName = vertexInterfaceBlock.name.c_str();
2342 // validate blocks for the same member types
2343 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2344 {
Jamie Madillf6113162015-05-07 11:49:21 -04002345 infoLog << "Types for interface block '" << blockName
2346 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002347 return false;
2348 }
2349 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2350 {
Jamie Madillf6113162015-05-07 11:49:21 -04002351 infoLog << "Array sizes differ for interface block '" << blockName
2352 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002353 return false;
2354 }
jchen10af713a22017-04-19 09:10:56 +08002355 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2356 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2357 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358 {
Jamie Madillf6113162015-05-07 11:49:21 -04002359 infoLog << "Layout qualifiers differ for interface block '" << blockName
2360 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361 return false;
2362 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002363 const unsigned int numBlockMembers =
2364 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2366 {
2367 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2368 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2369 if (vertexMember.name != fragmentMember.name)
2370 {
Jamie Madillf6113162015-05-07 11:49:21 -04002371 infoLog << "Name mismatch for field " << blockMemberIndex
2372 << " of interface block '" << blockName
2373 << "': (in vertex: '" << vertexMember.name
2374 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 return false;
2376 }
2377 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002378 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2379 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002380 {
2381 return false;
2382 }
2383 }
2384 return true;
2385}
2386
2387bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2388 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2389{
2390 if (vertexVariable.type != fragmentVariable.type)
2391 {
Jamie Madillf6113162015-05-07 11:49:21 -04002392 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393 return false;
2394 }
2395 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2396 {
Jamie Madillf6113162015-05-07 11:49:21 -04002397 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398 return false;
2399 }
2400 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2401 {
Jamie Madillf6113162015-05-07 11:49:21 -04002402 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403 return false;
2404 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002405 if (vertexVariable.structName != fragmentVariable.structName)
2406 {
2407 infoLog << "Structure names for " << variableName
2408 << " differ between vertex and fragment shaders";
2409 return false;
2410 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411
2412 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2413 {
Jamie Madillf6113162015-05-07 11:49:21 -04002414 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415 return false;
2416 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002417 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002418 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2419 {
2420 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2421 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2422
2423 if (vertexMember.name != fragmentMember.name)
2424 {
Jamie Madillf6113162015-05-07 11:49:21 -04002425 infoLog << "Name mismatch for field '" << memberIndex
2426 << "' of " << variableName
2427 << ": (in vertex: '" << vertexMember.name
2428 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002429 return false;
2430 }
2431
2432 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2433 vertexMember.name + "'";
2434
2435 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2436 {
2437 return false;
2438 }
2439 }
2440
2441 return true;
2442}
2443
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002444bool Program::linkValidateVaryings(InfoLog &infoLog,
2445 const std::string &varyingName,
2446 const sh::Varying &vertexVarying,
2447 const sh::Varying &fragmentVarying,
2448 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002449{
2450 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2451 {
2452 return false;
2453 }
2454
Jamie Madille9cc4692015-02-19 16:00:13 -05002455 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002456 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002457 infoLog << "Interpolation types for " << varyingName
2458 << " differ between vertex and fragment shaders.";
2459 return false;
2460 }
2461
2462 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2463 {
2464 infoLog << "Invariance for " << varyingName
2465 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002466 return false;
2467 }
2468
2469 return true;
2470}
2471
Jamie Madillbd044ed2017-06-05 12:59:21 -04002472bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002473{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002474 Shader *vertexShader = mState.mAttachedVertexShader;
2475 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002476 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2477 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002478 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002479
2480 if (shaderVersion != 100)
2481 {
2482 // Only ESSL 1.0 has restrictions on matching input and output invariance
2483 return true;
2484 }
2485
2486 bool glPositionIsInvariant = false;
2487 bool glPointSizeIsInvariant = false;
2488 bool glFragCoordIsInvariant = false;
2489 bool glPointCoordIsInvariant = false;
2490
2491 for (const sh::Varying &varying : vertexVaryings)
2492 {
2493 if (!varying.isBuiltIn())
2494 {
2495 continue;
2496 }
2497 if (varying.name.compare("gl_Position") == 0)
2498 {
2499 glPositionIsInvariant = varying.isInvariant;
2500 }
2501 else if (varying.name.compare("gl_PointSize") == 0)
2502 {
2503 glPointSizeIsInvariant = varying.isInvariant;
2504 }
2505 }
2506
2507 for (const sh::Varying &varying : fragmentVaryings)
2508 {
2509 if (!varying.isBuiltIn())
2510 {
2511 continue;
2512 }
2513 if (varying.name.compare("gl_FragCoord") == 0)
2514 {
2515 glFragCoordIsInvariant = varying.isInvariant;
2516 }
2517 else if (varying.name.compare("gl_PointCoord") == 0)
2518 {
2519 glPointCoordIsInvariant = varying.isInvariant;
2520 }
2521 }
2522
2523 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2524 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2525 // Not requiring invariance to match is supported by:
2526 // dEQP, WebGL CTS, Nexus 5X GLES
2527 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2528 {
2529 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2530 "declared invariant.";
2531 return false;
2532 }
2533 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2534 {
2535 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2536 "declared invariant.";
2537 return false;
2538 }
2539
2540 return true;
2541}
2542
jchen10a9042d32017-03-17 08:50:45 +08002543bool Program::linkValidateTransformFeedback(const gl::Context *context,
2544 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002545 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002546 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002547{
2548 size_t totalComponents = 0;
2549
Jamie Madillccdf74b2015-08-18 10:46:12 -04002550 std::set<std::string> uniqueNames;
2551
Jamie Madill48ef11b2016-04-27 15:21:52 -04002552 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002553 {
2554 bool found = false;
Olli Etuahoc8538042017-09-27 11:20:15 +03002555 std::vector<unsigned int> subscripts;
2556 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002557
Jamie Madill192745a2016-12-22 15:58:21 -05002558 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002559 {
Jamie Madill192745a2016-12-22 15:58:21 -05002560 const sh::Varying *varying = ref.second.get();
2561
jchen10a9042d32017-03-17 08:50:45 +08002562 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002563 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002564 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002565 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002566 infoLog << "Two transform feedback varyings specify the same output variable ("
2567 << tfVaryingName << ").";
2568 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002569 }
jchen10a9042d32017-03-17 08:50:45 +08002570 if (context->getClientVersion() >= Version(3, 1))
2571 {
2572 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2573 {
2574 infoLog
2575 << "Two transform feedback varyings include the same array element ("
2576 << tfVaryingName << ").";
2577 return false;
2578 }
2579 }
2580 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002581 {
2582 infoLog << "Capture of arrays is undefined and not supported.";
2583 return false;
2584 }
2585
jchen10a9042d32017-03-17 08:50:45 +08002586 uniqueNames.insert(tfVaryingName);
2587
Jamie Madillccdf74b2015-08-18 10:46:12 -04002588 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002589 size_t elementCount =
Olli Etuahoc8538042017-09-27 11:20:15 +03002590 ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1);
jchen10a9042d32017-03-17 08:50:45 +08002591 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002592 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002593 componentCount > caps.maxTransformFeedbackSeparateComponents)
2594 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002595 infoLog << "Transform feedback varying's " << varying->name << " components ("
2596 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002597 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002598 return false;
2599 }
2600
2601 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002602 found = true;
2603 break;
2604 }
2605 }
jchen10a9042d32017-03-17 08:50:45 +08002606 if (context->getClientVersion() < Version(3, 1) &&
2607 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002608 {
Geoff Lang1a683462015-09-29 15:09:59 -04002609 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002610 return false;
2611 }
jchen1085c93c42017-11-12 15:36:47 +08002612 if (!found)
2613 {
2614 infoLog << "Transform feedback varying " << tfVaryingName
2615 << " does not exist in the vertex shader.";
2616 return false;
2617 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002618 }
2619
Jamie Madill48ef11b2016-04-27 15:21:52 -04002620 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002621 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002622 {
Jamie Madillf6113162015-05-07 11:49:21 -04002623 infoLog << "Transform feedback varying total components (" << totalComponents
2624 << ") exceed the maximum interleaved components ("
2625 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002626 return false;
2627 }
2628
2629 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002630}
2631
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002632bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2633{
2634 const std::vector<sh::Uniform> &vertexUniforms =
2635 mState.mAttachedVertexShader->getUniforms(context);
2636 const std::vector<sh::Uniform> &fragmentUniforms =
2637 mState.mAttachedFragmentShader->getUniforms(context);
2638 const std::vector<sh::Attribute> &attributes =
2639 mState.mAttachedVertexShader->getActiveAttributes(context);
2640 for (const auto &attrib : attributes)
2641 {
2642 for (const auto &uniform : vertexUniforms)
2643 {
2644 if (uniform.name == attrib.name)
2645 {
2646 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2647 return false;
2648 }
2649 }
2650 for (const auto &uniform : fragmentUniforms)
2651 {
2652 if (uniform.name == attrib.name)
2653 {
2654 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2655 return false;
2656 }
2657 }
2658 }
2659 return true;
2660}
2661
Jamie Madill192745a2016-12-22 15:58:21 -05002662void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002663{
2664 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002665 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002666 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002667 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002668 std::vector<unsigned int> subscripts;
2669 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002670 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002671 if (!subscripts.empty())
2672 {
2673 subscript = subscripts.back();
2674 }
Jamie Madill192745a2016-12-22 15:58:21 -05002675 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002676 {
Jamie Madill192745a2016-12-22 15:58:21 -05002677 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002678 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002679 {
jchen10a9042d32017-03-17 08:50:45 +08002680 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2681 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002682 break;
2683 }
2684 }
2685 }
2686}
2687
Jamie Madillbd044ed2017-06-05 12:59:21 -04002688Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002689{
Jamie Madill192745a2016-12-22 15:58:21 -05002690 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002691
Jiawei Shao3d404882017-10-16 13:30:48 +08002692 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002693 {
Jamie Madill192745a2016-12-22 15:58:21 -05002694 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002695 }
2696
Jiawei Shao3d404882017-10-16 13:30:48 +08002697 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002698 {
Jamie Madill192745a2016-12-22 15:58:21 -05002699 merged[varying.name].fragment = &varying;
2700 }
2701
2702 return merged;
2703}
2704
Jamie Madill80a6fc02015-08-21 16:53:16 -04002705
Jamie Madillbd044ed2017-06-05 12:59:21 -04002706void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002707{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002708 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002709 ASSERT(fragmentShader != nullptr);
2710
Geoff Lange0cff192017-05-30 13:04:56 -04002711 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002712 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002713
2714 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002715 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002716 {
2717 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2718 outputVariable.name != "gl_FragData")
2719 {
2720 continue;
2721 }
2722
2723 unsigned int baseLocation =
2724 (outputVariable.location == -1 ? 0u
2725 : static_cast<unsigned int>(outputVariable.location));
2726 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2727 elementIndex++)
2728 {
2729 const unsigned int location = baseLocation + elementIndex;
2730 if (location >= mState.mOutputVariableTypes.size())
2731 {
2732 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2733 }
Corentin Walleze7557742017-06-01 13:09:57 -04002734 ASSERT(location < mState.mActiveOutputVariables.size());
2735 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002736 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2737 }
2738 }
2739
Jamie Madill80a6fc02015-08-21 16:53:16 -04002740 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002741 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002742 return;
2743
Jamie Madillbd044ed2017-06-05 12:59:21 -04002744 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002745 // TODO(jmadill): any caps validation here?
2746
jchen1015015f72017-03-16 13:54:21 +08002747 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002748 outputVariableIndex++)
2749 {
jchen1015015f72017-03-16 13:54:21 +08002750 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002751
Olli Etuahod2551232017-10-26 20:03:33 +03002752 if (outputVariable.isArray())
2753 {
2754 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
2755 // Resources and including [0] at the end of array variable names.
2756 mState.mOutputVariables[outputVariableIndex].name += "[0]";
2757 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
2758 }
2759
Jamie Madill80a6fc02015-08-21 16:53:16 -04002760 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2761 if (outputVariable.isBuiltIn())
2762 continue;
2763
2764 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03002765 unsigned int baseLocation =
2766 (outputVariable.location == -1 ? 0u
2767 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04002768
Jamie Madill80a6fc02015-08-21 16:53:16 -04002769 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2770 elementIndex++)
2771 {
Olli Etuahod2551232017-10-26 20:03:33 +03002772 const unsigned int location = baseLocation + elementIndex;
2773 if (location >= mState.mOutputLocations.size())
2774 {
2775 mState.mOutputLocations.resize(location + 1);
2776 }
2777 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03002778 if (outputVariable.isArray())
2779 {
2780 mState.mOutputLocations[location] =
2781 VariableLocation(elementIndex, outputVariableIndex);
2782 }
2783 else
2784 {
2785 VariableLocation locationInfo;
2786 locationInfo.index = outputVariableIndex;
2787 mState.mOutputLocations[location] = locationInfo;
2788 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002789 }
2790 }
2791}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002792
Olli Etuaho48fed632017-03-16 12:05:30 +00002793void Program::setUniformValuesFromBindingQualifiers()
2794{
Jamie Madill982f6e02017-06-07 14:33:04 -04002795 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002796 {
2797 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2798 if (samplerUniform.binding != -1)
2799 {
Olli Etuahod2551232017-10-26 20:03:33 +03002800 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00002801 ASSERT(location != -1);
2802 std::vector<GLint> boundTextureUnits;
2803 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2804 ++elementIndex)
2805 {
2806 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2807 }
2808 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2809 boundTextureUnits.data());
2810 }
2811 }
2812}
2813
jchen10eaef1e52017-06-13 10:44:11 +08002814void Program::gatherAtomicCounterBuffers()
2815{
jchen10baf5d942017-08-28 20:45:48 +08002816 for (unsigned int index : mState.mAtomicCounterUniformRange)
2817 {
2818 auto &uniform = mState.mUniforms[index];
2819 uniform.blockInfo.offset = uniform.offset;
2820 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2821 uniform.blockInfo.matrixStride = 0;
2822 uniform.blockInfo.isRowMajorMatrix = false;
2823 }
2824
jchen10eaef1e52017-06-13 10:44:11 +08002825 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2826}
2827
Jamie Madill6db1c2e2017-11-08 09:17:40 -05002828void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829{
jchen10af713a22017-04-19 09:10:56 +08002830 // Set initial bindings from shader.
2831 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2832 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002833 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002834 bindUniformBlock(blockIndex, uniformBlock.binding);
2835 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002836}
2837
Jamie Madille7d84322017-01-10 18:21:59 -05002838void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002839 GLsizei clampedCount,
2840 const GLint *v)
2841{
Jamie Madill81c2e252017-09-09 23:32:46 -04002842 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
2843 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2844 std::vector<GLuint> *boundTextureUnits =
2845 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05002846
Olli Etuaho1734e172017-10-27 15:30:27 +03002847 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04002848
2849 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04002850 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05002851}
2852
2853template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002854GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2855 GLsizei count,
2856 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002857 const T *v)
2858{
Jamie Madill134f93d2017-08-31 17:11:00 -04002859 if (count == 1)
2860 return 1;
2861
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002862 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002863
Corentin Wallez15ac5342016-11-03 17:06:39 -04002864 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2865 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho1734e172017-10-27 15:30:27 +03002866 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002867 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002868 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002869
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002870 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002871 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002872 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002873 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002874
2875 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002876}
2877
2878template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002879GLsizei Program::clampMatrixUniformCount(GLint location,
2880 GLsizei count,
2881 GLboolean transpose,
2882 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002883{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002884 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2885
Jamie Madill62d31cb2015-09-11 13:25:51 -04002886 if (!transpose)
2887 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002888 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002889 }
2890
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002891 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04002892
2893 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2894 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho1734e172017-10-27 15:30:27 +03002895 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002896 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002897}
2898
Jamie Madill54164b02017-08-28 15:17:37 -04002899// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
2900// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002901template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04002902void Program::getUniformInternal(const Context *context,
2903 DestT *dataOut,
2904 GLint location,
2905 GLenum nativeType,
2906 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04002907{
Jamie Madill54164b02017-08-28 15:17:37 -04002908 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002909 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002910 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04002911 {
2912 GLint tempValue[16] = {0};
2913 mProgram->getUniformiv(context, location, tempValue);
2914 UniformStateQueryCastLoop<GLboolean>(
2915 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002916 break;
Jamie Madill54164b02017-08-28 15:17:37 -04002917 }
2918 case GL_INT:
2919 {
2920 GLint tempValue[16] = {0};
2921 mProgram->getUniformiv(context, location, tempValue);
2922 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
2923 components);
2924 break;
2925 }
2926 case GL_UNSIGNED_INT:
2927 {
2928 GLuint tempValue[16] = {0};
2929 mProgram->getUniformuiv(context, location, tempValue);
2930 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
2931 components);
2932 break;
2933 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002934 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04002935 {
2936 GLfloat tempValue[16] = {0};
2937 mProgram->getUniformfv(context, location, tempValue);
2938 UniformStateQueryCastLoop<GLfloat>(
2939 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002940 break;
Jamie Madill54164b02017-08-28 15:17:37 -04002941 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002942 default:
2943 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04002944 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002945 }
2946}
Jamie Madilla4595b82017-01-11 17:36:34 -05002947
2948bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2949{
2950 // Must be called after samplers are validated.
2951 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2952
2953 for (const auto &binding : mState.mSamplerBindings)
2954 {
2955 GLenum textureType = binding.textureType;
2956 for (const auto &unit : binding.boundTextureUnits)
2957 {
2958 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2959 if (programTextureID == textureID)
2960 {
2961 // TODO(jmadill): Check for appropriate overlap.
2962 return true;
2963 }
2964 }
2965 }
2966
2967 return false;
2968}
2969
Jamie Madilla2c74982016-12-12 11:20:42 -05002970} // namespace gl