blob: 0973b42b3b11053dd9ca49a8db7fffe5d3acceaf [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"
17#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050019#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040020#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040022#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000023#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040024#include "libANGLE/VaryingPacking.h"
25#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040026#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/queryconversions.h"
28#include "libANGLE/renderer/GLImplFactory.h"
29#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040030#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace gl
33{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000034
Geoff Lang7dd2e102014-11-10 15:19:26 -050035namespace
36{
37
Jamie Madill62d31cb2015-09-11 13:25:51 -040038// This simplified cast function doesn't need to worry about advanced concepts like
39// depth range values, or casting to bool.
40template <typename DestT, typename SrcT>
41DestT UniformStateQueryCast(SrcT value);
42
43// From-Float-To-Integer Casts
44template <>
45GLint UniformStateQueryCast(GLfloat value)
46{
47 return clampCast<GLint>(roundf(value));
48}
49
50template <>
51GLuint UniformStateQueryCast(GLfloat value)
52{
53 return clampCast<GLuint>(roundf(value));
54}
55
56// From-Integer-to-Integer Casts
57template <>
58GLint UniformStateQueryCast(GLuint value)
59{
60 return clampCast<GLint>(value);
61}
62
63template <>
64GLuint UniformStateQueryCast(GLint value)
65{
66 return clampCast<GLuint>(value);
67}
68
69// From-Boolean-to-Anything Casts
70template <>
71GLfloat UniformStateQueryCast(GLboolean value)
72{
73 return (value == GL_TRUE ? 1.0f : 0.0f);
74}
75
76template <>
77GLint UniformStateQueryCast(GLboolean value)
78{
79 return (value == GL_TRUE ? 1 : 0);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLboolean value)
84{
85 return (value == GL_TRUE ? 1u : 0u);
86}
87
88// Default to static_cast
89template <typename DestT, typename SrcT>
90DestT UniformStateQueryCast(SrcT value)
91{
92 return static_cast<DestT>(value);
93}
94
95template <typename SrcT, typename DestT>
96void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
97{
98 for (int comp = 0; comp < components; ++comp)
99 {
100 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
101 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
102 size_t offset = comp * 4;
103 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
104 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
105 }
106}
107
Jamie Madill192745a2016-12-22 15:58:21 -0500108// true if varying x has a higher priority in packing than y
109bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
110{
jchen10a9042d32017-03-17 08:50:45 +0800111 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
112 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
113 sh::ShaderVariable vx, vy;
114 const sh::ShaderVariable *px, *py;
115 if (x.isArrayElement())
116 {
117 vx = *x.varying;
118 vx.arraySize = 0;
119 px = &vx;
120 }
121 else
122 {
123 px = x.varying;
124 }
125
126 if (y.isArrayElement())
127 {
128 vy = *y.varying;
129 vy.arraySize = 0;
130 py = &vy;
131 }
132 else
133 {
134 py = y.varying;
135 }
136
137 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500138}
139
jchen1015015f72017-03-16 13:54:21 +0800140template <typename VarT>
141GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
142{
143 size_t subscript = GL_INVALID_INDEX;
144 std::string baseName = ParseResourceName(name, &subscript);
145
146 // The app is not allowed to specify array indices other than 0 for arrays of basic types
147 if (subscript != 0 && subscript != GL_INVALID_INDEX)
148 {
149 return GL_INVALID_INDEX;
150 }
151
152 for (size_t index = 0; index < list.size(); index++)
153 {
154 const VarT &resource = list[index];
155 if (resource.name == baseName)
156 {
157 if (resource.isArray() || subscript == GL_INVALID_INDEX)
158 {
159 return static_cast<GLuint>(index);
160 }
161 }
162 }
163
164 return GL_INVALID_INDEX;
165}
166
jchen10fd7c3b52017-03-21 15:36:03 +0800167void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
168{
169 ASSERT(bufSize > 0);
170 strncpy(buffer, string.c_str(), bufSize);
171 buffer[bufSize - 1] = '\0';
172
173 if (length)
174 {
175 *length = static_cast<GLsizei>(strlen(buffer));
176 }
177}
178
jchen10a9042d32017-03-17 08:50:45 +0800179bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
180{
181 size_t subscript = GL_INVALID_INDEX;
182 std::string baseName = ParseResourceName(name, &subscript);
183 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
184 {
185 size_t arrayIndex = GL_INVALID_INDEX;
186 std::string arrayName = ParseResourceName(*it, &arrayIndex);
187 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
188 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
189 {
190 return true;
191 }
192 }
193 return false;
194}
195
Jamie Madill62d31cb2015-09-11 13:25:51 -0400196} // anonymous namespace
197
Jamie Madill4a3c2342015-10-08 12:58:45 -0400198const char *const g_fakepath = "C:\\fakepath";
199
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400200InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000201{
202}
203
204InfoLog::~InfoLog()
205{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000206}
207
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209{
Jamie Madill23176ce2017-07-31 14:14:33 -0400210 if (!mLazyStream)
211 {
212 return 0;
213 }
214
215 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400216 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000217}
218
Geoff Lange1a27752015-10-05 13:16:04 -0400219void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000220{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400221 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000222
223 if (bufSize > 0)
224 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400225 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400226
Jamie Madill23176ce2017-07-31 14:14:33 -0400227 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000228 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400229 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
230 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000231 }
232
233 infoLog[index] = '\0';
234 }
235
236 if (length)
237 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400238 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000239 }
240}
241
242// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300243// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000244// messages, so lets remove all occurrences of this fake file path from the log.
245void InfoLog::appendSanitized(const char *message)
246{
Jamie Madill23176ce2017-07-31 14:14:33 -0400247 ensureInitialized();
248
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000249 std::string msg(message);
250
251 size_t found;
252 do
253 {
254 found = msg.find(g_fakepath);
255 if (found != std::string::npos)
256 {
257 msg.erase(found, strlen(g_fakepath));
258 }
259 }
260 while (found != std::string::npos);
261
Jamie Madill23176ce2017-07-31 14:14:33 -0400262 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000263}
264
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265void InfoLog::reset()
266{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000267}
268
Geoff Langd8605522016-04-13 10:19:12 -0400269VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000270{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500271}
272
Geoff Langd8605522016-04-13 10:19:12 -0400273VariableLocation::VariableLocation(const std::string &name,
274 unsigned int element,
275 unsigned int index)
276 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500277{
278}
279
Geoff Langd8605522016-04-13 10:19:12 -0400280void Program::Bindings::bindLocation(GLuint index, const std::string &name)
281{
282 mBindings[name] = index;
283}
284
285int Program::Bindings::getBinding(const std::string &name) const
286{
287 auto iter = mBindings.find(name);
288 return (iter != mBindings.end()) ? iter->second : -1;
289}
290
291Program::Bindings::const_iterator Program::Bindings::begin() const
292{
293 return mBindings.begin();
294}
295
296Program::Bindings::const_iterator Program::Bindings::end() const
297{
298 return mBindings.end();
299}
300
Jamie Madill48ef11b2016-04-27 15:21:52 -0400301ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500302 : mLabel(),
303 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400304 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300305 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500306 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500307 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800308 mImageUniformRange(0, 0),
309 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300310 mBinaryRetrieveableHint(false),
311 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400312{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300313 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400314}
315
Jamie Madill48ef11b2016-04-27 15:21:52 -0400316ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400317{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500318 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400319}
320
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500322{
323 return mLabel;
324}
325
Jamie Madill48ef11b2016-04-27 15:21:52 -0400326GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400327{
328 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800329 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400330
331 for (size_t location = 0; location < mUniformLocations.size(); ++location)
332 {
333 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400334 if (!uniformLocation.used)
335 {
336 continue;
337 }
338
339 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400340
341 if (uniform.name == baseName)
342 {
Geoff Langd8605522016-04-13 10:19:12 -0400343 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400344 {
Geoff Langd8605522016-04-13 10:19:12 -0400345 if (uniformLocation.element == subscript ||
346 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
347 {
348 return static_cast<GLint>(location);
349 }
350 }
351 else
352 {
353 if (subscript == GL_INVALID_INDEX)
354 {
355 return static_cast<GLint>(location);
356 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400357 }
358 }
359 }
360
361 return -1;
362}
363
Jamie Madille7d84322017-01-10 18:21:59 -0500364GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400365{
jchen1015015f72017-03-16 13:54:21 +0800366 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400367}
368
Jamie Madille7d84322017-01-10 18:21:59 -0500369GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
370{
371 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
372 return mUniformLocations[location].index;
373}
374
375Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
376{
377 GLuint index = getUniformIndexFromLocation(location);
378 if (!isSamplerUniformIndex(index))
379 {
380 return Optional<GLuint>::Invalid();
381 }
382
383 return getSamplerIndexFromUniformIndex(index);
384}
385
386bool ProgramState::isSamplerUniformIndex(GLuint index) const
387{
Jamie Madill982f6e02017-06-07 14:33:04 -0400388 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500389}
390
391GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
392{
393 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400394 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500395}
396
Jamie Madill34ca4f52017-06-13 11:49:39 -0400397GLuint ProgramState::getAttributeLocation(const std::string &name) const
398{
399 for (const sh::Attribute &attribute : mAttributes)
400 {
401 if (attribute.name == name)
402 {
403 return attribute.location;
404 }
405 }
406
407 return static_cast<GLuint>(-1);
408}
409
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500410Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400411 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400412 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500413 mLinked(false),
414 mDeleteStatus(false),
415 mRefCount(0),
416 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500417 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500418{
419 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000420
Geoff Lang7dd2e102014-11-10 15:19:26 -0500421 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
424Program::~Program()
425{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400426 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427}
428
Jamie Madill4928b7c2017-06-20 12:57:39 -0400429void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500430{
431 if (mState.mAttachedVertexShader != nullptr)
432 {
433 mState.mAttachedVertexShader->release(context);
434 mState.mAttachedVertexShader = nullptr;
435 }
436
437 if (mState.mAttachedFragmentShader != nullptr)
438 {
439 mState.mAttachedFragmentShader->release(context);
440 mState.mAttachedFragmentShader = nullptr;
441 }
442
443 if (mState.mAttachedComputeShader != nullptr)
444 {
445 mState.mAttachedComputeShader->release(context);
446 mState.mAttachedComputeShader = nullptr;
447 }
448
Jamie Madillc564c072017-06-01 12:45:42 -0400449 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400450
451 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
452 !mState.mAttachedComputeShader);
453 SafeDelete(mProgram);
454
455 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500456}
457
Geoff Lang70d0f492015-12-10 17:45:46 -0500458void Program::setLabel(const std::string &label)
459{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400460 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500461}
462
463const std::string &Program::getLabel() const
464{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400465 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500466}
467
Jamie Madillef300b12016-10-07 15:12:09 -0400468void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300470 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300472 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473 {
Jamie Madillef300b12016-10-07 15:12:09 -0400474 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300475 mState.mAttachedVertexShader = shader;
476 mState.mAttachedVertexShader->addRef();
477 break;
478 }
479 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480 {
Jamie Madillef300b12016-10-07 15:12:09 -0400481 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300482 mState.mAttachedFragmentShader = shader;
483 mState.mAttachedFragmentShader->addRef();
484 break;
485 }
486 case GL_COMPUTE_SHADER:
487 {
Jamie Madillef300b12016-10-07 15:12:09 -0400488 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300489 mState.mAttachedComputeShader = shader;
490 mState.mAttachedComputeShader->addRef();
491 break;
492 }
493 default:
494 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496}
497
Jamie Madillc1d770e2017-04-13 17:31:24 -0400498void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300500 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300502 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400504 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500505 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300506 mState.mAttachedVertexShader = nullptr;
507 break;
508 }
509 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400511 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500512 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300513 mState.mAttachedFragmentShader = nullptr;
514 break;
515 }
516 case GL_COMPUTE_SHADER:
517 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400518 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500519 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300520 mState.mAttachedComputeShader = nullptr;
521 break;
522 }
523 default:
524 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526}
527
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000528int Program::getAttachedShadersCount() const
529{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300530 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
531 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000532}
533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534void Program::bindAttributeLocation(GLuint index, const char *name)
535{
Geoff Langd8605522016-04-13 10:19:12 -0400536 mAttributeBindings.bindLocation(index, name);
537}
538
539void Program::bindUniformLocation(GLuint index, const char *name)
540{
541 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800542 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543}
544
Sami Väisänen46eaa942016-06-29 10:26:37 +0300545void Program::bindFragmentInputLocation(GLint index, const char *name)
546{
547 mFragmentInputBindings.bindLocation(index, name);
548}
549
Jamie Madillbd044ed2017-06-05 12:59:21 -0400550BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300551{
552 BindingInfo ret;
553 ret.type = GL_NONE;
554 ret.valid = false;
555
Jamie Madillbd044ed2017-06-05 12:59:21 -0400556 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300557 ASSERT(fragmentShader);
558
559 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400560 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300561
562 for (const auto &binding : mFragmentInputBindings)
563 {
564 if (binding.second != static_cast<GLuint>(index))
565 continue;
566
567 ret.valid = true;
568
569 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400570 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300571
572 for (const auto &in : inputs)
573 {
574 if (in.name == originalName)
575 {
576 if (in.isArray())
577 {
578 // The client wants to bind either "name" or "name[0]".
579 // GL ES 3.1 spec refers to active array names with language such as:
580 // "if the string identifies the base name of an active array, where the
581 // string would exactly match the name of the variable if the suffix "[0]"
582 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400583 if (arrayIndex == GL_INVALID_INDEX)
584 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300585
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400586 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300587 }
588 else
589 {
590 ret.name = in.mappedName;
591 }
592 ret.type = in.type;
593 return ret;
594 }
595 }
596 }
597
598 return ret;
599}
600
Jamie Madillbd044ed2017-06-05 12:59:21 -0400601void Program::pathFragmentInputGen(const Context *context,
602 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300603 GLenum genMode,
604 GLint components,
605 const GLfloat *coeffs)
606{
607 // If the location is -1 then the command is silently ignored
608 if (index == -1)
609 return;
610
Jamie Madillbd044ed2017-06-05 12:59:21 -0400611 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300612
613 // If the input doesn't exist then then the command is silently ignored
614 // This could happen through optimization for example, the shader translator
615 // decides that a variable is not actually being used and optimizes it away.
616 if (binding.name.empty())
617 return;
618
619 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
620}
621
Martin Radev4c4c8e72016-08-04 12:25:34 +0300622// The attached shaders are checked for linking errors by matching up their variables.
623// Uniform, input and output variables get collected.
624// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500625Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000626{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500627 const auto &data = context->getContextState();
628
Jamie Madill6c58b062017-08-01 13:44:25 -0400629 auto *platform = ANGLEPlatformCurrent();
630 double startTime = platform->currentTime(platform);
631
Jamie Madill6c1f6712017-02-14 19:08:04 -0500632 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000633
Jamie Madill32447362017-06-28 14:53:52 -0400634 ProgramHash programHash;
635 auto *cache = context->getMemoryProgramCache();
636 if (cache)
637 {
638 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400639 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400640 }
641
642 if (mLinked)
643 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400644 double delta = platform->currentTime(platform) - startTime;
645 int us = static_cast<int>(delta * 1000000.0);
646 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400647 return NoError();
648 }
649
650 // Cache load failed, fall through to normal linking.
651 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000652 mInfoLog.reset();
653
Martin Radev4c4c8e72016-08-04 12:25:34 +0300654 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500655
Jamie Madill192745a2016-12-22 15:58:21 -0500656 auto vertexShader = mState.mAttachedVertexShader;
657 auto fragmentShader = mState.mAttachedFragmentShader;
658 auto computeShader = mState.mAttachedComputeShader;
659
660 bool isComputeShaderAttached = (computeShader != nullptr);
661 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662 // Check whether we both have a compute and non-compute shaders attached.
663 // If there are of both types attached, then linking should fail.
664 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
665 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500666 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300667 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
668 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400669 }
670
Jamie Madill192745a2016-12-22 15:58:21 -0500671 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500672 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400673 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300674 {
675 mInfoLog << "Attached compute shader is not compiled.";
676 return NoError();
677 }
Jamie Madill192745a2016-12-22 15:58:21 -0500678 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679
Jamie Madillbd044ed2017-06-05 12:59:21 -0400680 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300681
682 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
683 // If the work group size is not specified, a link time error should occur.
684 if (!mState.mComputeShaderLocalSize.isDeclared())
685 {
686 mInfoLog << "Work group size is not specified.";
687 return NoError();
688 }
689
Jamie Madillbd044ed2017-06-05 12:59:21 -0400690 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300691 {
692 return NoError();
693 }
694
Jamie Madillbd044ed2017-06-05 12:59:21 -0400695 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300696 {
697 return NoError();
698 }
699
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500700 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400701 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500702 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500704 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 }
706 }
707 else
708 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400709 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300710 {
711 return NoError();
712 }
Jamie Madill192745a2016-12-22 15:58:21 -0500713 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300714
Jamie Madillbd044ed2017-06-05 12:59:21 -0400715 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300716 {
717 return NoError();
718 }
Jamie Madill192745a2016-12-22 15:58:21 -0500719 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720
Jamie Madillbd044ed2017-06-05 12:59:21 -0400721 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300722 {
723 mInfoLog << "Fragment shader version does not match vertex shader version.";
724 return NoError();
725 }
726
Jamie Madillbd044ed2017-06-05 12:59:21 -0400727 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300728 {
729 return NoError();
730 }
731
Jamie Madillbd044ed2017-06-05 12:59:21 -0400732 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733 {
734 return NoError();
735 }
736
Jamie Madillbd044ed2017-06-05 12:59:21 -0400737 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300738 {
739 return NoError();
740 }
741
Jamie Madillbd044ed2017-06-05 12:59:21 -0400742 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300743 {
744 return NoError();
745 }
746
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400747 if (!linkValidateGlobalNames(context, mInfoLog))
748 {
749 return NoError();
750 }
751
Jamie Madillbd044ed2017-06-05 12:59:21 -0400752 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753
jchen10a9042d32017-03-17 08:50:45 +0800754 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300755 {
756 return NoError();
757 }
758
Martin Radev7cf61662017-07-26 17:10:53 +0300759 mState.mNumViews = vertexShader->getNumViews(context);
760
Jamie Madillbd044ed2017-06-05 12:59:21 -0400761 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300762
Jamie Madill192745a2016-12-22 15:58:21 -0500763 // Validate we can pack the varyings.
764 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
765
766 // Map the varyings to the register file
767 // In WebGL, we use a slightly different handling for packing variables.
768 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
769 : PackMode::ANGLE_RELAXED;
770 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
771 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
772 mState.getTransformFeedbackVaryingNames()))
773 {
774 return NoError();
775 }
776
Jamie Madillc564c072017-06-01 12:45:42 -0400777 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500778 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300779 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500780 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300781 }
782
783 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500784 }
785
jchen10eaef1e52017-06-13 10:44:11 +0800786 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400787 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400788
jchen10eaef1e52017-06-13 10:44:11 +0800789 setUniformValuesFromBindingQualifiers();
790
Jamie Madill32447362017-06-28 14:53:52 -0400791 // Save to the program cache.
792 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
793 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
794 {
795 cache->putProgram(programHash, context, this);
796 }
797
Jamie Madill6c58b062017-08-01 13:44:25 -0400798 double delta = platform->currentTime(platform) - startTime;
799 int us = static_cast<int>(delta * 1000000.0);
800 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
801
Martin Radev4c4c8e72016-08-04 12:25:34 +0300802 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000803}
804
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000805// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500806void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400808 mState.mAttributes.clear();
809 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800810 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 mState.mUniforms.clear();
812 mState.mUniformLocations.clear();
813 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800814 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800815 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400816 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800817 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400818 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400819 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300820 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500821 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800822 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300823 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500824
Geoff Lang7dd2e102014-11-10 15:19:26 -0500825 mValidated = false;
826
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000827 mLinked = false;
828}
829
Geoff Lange1a27752015-10-05 13:16:04 -0400830bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000831{
832 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
Jamie Madilla2c74982016-12-12 11:20:42 -0500835Error Program::loadBinary(const Context *context,
836 GLenum binaryFormat,
837 const void *binary,
838 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000839{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500840 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000841
Geoff Lang7dd2e102014-11-10 15:19:26 -0500842#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800843 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500844#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400845 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
846 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000847 {
Jamie Madillf6113162015-05-07 11:49:21 -0400848 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800849 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500850 }
851
Jamie Madill4f86d052017-06-05 12:59:26 -0400852 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
853 ANGLE_TRY_RESULT(
854 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400855
856 // Currently we require the full shader text to compute the program hash.
857 // TODO(jmadill): Store the binary in the internal program cache.
858
Jamie Madillb0a838b2016-11-13 20:02:12 -0500859 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500860#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500861}
862
Jamie Madilla2c74982016-12-12 11:20:42 -0500863Error Program::saveBinary(const Context *context,
864 GLenum *binaryFormat,
865 void *binary,
866 GLsizei bufSize,
867 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500868{
869 if (binaryFormat)
870 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400871 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500872 }
873
Jamie Madill4f86d052017-06-05 12:59:26 -0400874 angle::MemoryBuffer memoryBuf;
875 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500876
Jamie Madill4f86d052017-06-05 12:59:26 -0400877 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
878 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500879
880 if (streamLength > bufSize)
881 {
882 if (length)
883 {
884 *length = 0;
885 }
886
887 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
888 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
889 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500890 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891 }
892
893 if (binary)
894 {
895 char *ptr = reinterpret_cast<char*>(binary);
896
Jamie Madill48ef11b2016-04-27 15:21:52 -0400897 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898 ptr += streamLength;
899
900 ASSERT(ptr - streamLength == binary);
901 }
902
903 if (length)
904 {
905 *length = streamLength;
906 }
907
He Yunchaoacd18982017-01-04 10:46:42 +0800908 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500909}
910
Jamie Madillffe00c02017-06-27 16:26:55 -0400911GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500912{
913 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400914 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500915 if (error.isError())
916 {
917 return 0;
918 }
919
920 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000921}
922
Geoff Langc5629752015-12-07 16:29:04 -0500923void Program::setBinaryRetrievableHint(bool retrievable)
924{
925 // TODO(jmadill) : replace with dirty bits
926 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500928}
929
930bool Program::getBinaryRetrievableHint() const
931{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400932 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500933}
934
Yunchao He61afff12017-03-14 15:34:03 +0800935void Program::setSeparable(bool separable)
936{
937 // TODO(yunchao) : replace with dirty bits
938 if (mState.mSeparable != separable)
939 {
940 mProgram->setSeparable(separable);
941 mState.mSeparable = separable;
942 }
943}
944
945bool Program::isSeparable() const
946{
947 return mState.mSeparable;
948}
949
Jamie Madill6c1f6712017-02-14 19:08:04 -0500950void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951{
952 mRefCount--;
953
954 if (mRefCount == 0 && mDeleteStatus)
955 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500956 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000957 }
958}
959
960void Program::addRef()
961{
962 mRefCount++;
963}
964
965unsigned int Program::getRefCount() const
966{
967 return mRefCount;
968}
969
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000970int Program::getInfoLogLength() const
971{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400972 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000973}
974
Geoff Lange1a27752015-10-05 13:16:04 -0400975void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000977 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000978}
979
Geoff Lange1a27752015-10-05 13:16:04 -0400980void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000981{
982 int total = 0;
983
Martin Radev4c4c8e72016-08-04 12:25:34 +0300984 if (mState.mAttachedComputeShader)
985 {
986 if (total < maxCount)
987 {
988 shaders[total] = mState.mAttachedComputeShader->getHandle();
989 total++;
990 }
991 }
992
Jamie Madill48ef11b2016-04-27 15:21:52 -0400993 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000994 {
995 if (total < maxCount)
996 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400997 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200998 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000999 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001000 }
1001
Jamie Madill48ef11b2016-04-27 15:21:52 -04001002 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001003 {
1004 if (total < maxCount)
1005 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001006 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001007 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001008 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001009 }
1010
1011 if (count)
1012 {
1013 *count = total;
1014 }
1015}
1016
Geoff Lange1a27752015-10-05 13:16:04 -04001017GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001018{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001019 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020}
1021
Jamie Madill63805b42015-08-25 13:17:39 -04001022bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001023{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001024 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1025 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001026}
1027
jchen10fd7c3b52017-03-21 15:36:03 +08001028void Program::getActiveAttribute(GLuint index,
1029 GLsizei bufsize,
1030 GLsizei *length,
1031 GLint *size,
1032 GLenum *type,
1033 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001034{
Jamie Madillc349ec02015-08-21 16:53:12 -04001035 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001036 {
1037 if (bufsize > 0)
1038 {
1039 name[0] = '\0';
1040 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001041
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001042 if (length)
1043 {
1044 *length = 0;
1045 }
1046
1047 *type = GL_NONE;
1048 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001049 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001050 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001051
jchen1036e120e2017-03-14 14:53:58 +08001052 ASSERT(index < mState.mAttributes.size());
1053 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001054
1055 if (bufsize > 0)
1056 {
jchen10fd7c3b52017-03-21 15:36:03 +08001057 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001058 }
1059
1060 // Always a single 'type' instance
1061 *size = 1;
1062 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001063}
1064
Geoff Lange1a27752015-10-05 13:16:04 -04001065GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001066{
Jamie Madillc349ec02015-08-21 16:53:12 -04001067 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001068 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001069 return 0;
1070 }
1071
jchen1036e120e2017-03-14 14:53:58 +08001072 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001073}
1074
Geoff Lange1a27752015-10-05 13:16:04 -04001075GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001076{
Jamie Madillc349ec02015-08-21 16:53:12 -04001077 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001078 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001079 return 0;
1080 }
1081
1082 size_t maxLength = 0;
1083
Jamie Madill48ef11b2016-04-27 15:21:52 -04001084 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001085 {
jchen1036e120e2017-03-14 14:53:58 +08001086 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001087 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088
Jamie Madillc349ec02015-08-21 16:53:12 -04001089 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090}
1091
jchen1015015f72017-03-16 13:54:21 +08001092GLuint Program::getInputResourceIndex(const GLchar *name) const
1093{
1094 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1095 {
1096 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1097 if (attribute.name == name)
1098 {
1099 return attributeIndex;
1100 }
1101 }
1102 return GL_INVALID_INDEX;
1103}
1104
1105GLuint Program::getOutputResourceIndex(const GLchar *name) const
1106{
1107 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1108}
1109
jchen10fd7c3b52017-03-21 15:36:03 +08001110size_t Program::getOutputResourceCount() const
1111{
1112 return (mLinked ? mState.mOutputVariables.size() : 0);
1113}
1114
1115void Program::getInputResourceName(GLuint index,
1116 GLsizei bufSize,
1117 GLsizei *length,
1118 GLchar *name) const
1119{
1120 GLint size;
1121 GLenum type;
1122 getActiveAttribute(index, bufSize, length, &size, &type, name);
1123}
1124
1125void Program::getOutputResourceName(GLuint index,
1126 GLsizei bufSize,
1127 GLsizei *length,
1128 GLchar *name) const
1129{
1130 if (length)
1131 {
1132 *length = 0;
1133 }
1134
1135 if (!mLinked)
1136 {
1137 if (bufSize > 0)
1138 {
1139 name[0] = '\0';
1140 }
1141 return;
1142 }
1143 ASSERT(index < mState.mOutputVariables.size());
1144 const auto &output = mState.mOutputVariables[index];
1145
1146 if (bufSize > 0)
1147 {
1148 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1149
1150 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1151 }
1152}
1153
Geoff Lang7dd2e102014-11-10 15:19:26 -05001154GLint Program::getFragDataLocation(const std::string &name) const
1155{
1156 std::string baseName(name);
1157 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001158 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001159 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001160 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001161 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1162 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001163 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001164 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001165 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001167}
1168
Geoff Lange1a27752015-10-05 13:16:04 -04001169void Program::getActiveUniform(GLuint index,
1170 GLsizei bufsize,
1171 GLsizei *length,
1172 GLint *size,
1173 GLenum *type,
1174 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001175{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001176 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001177 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001178 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001179 ASSERT(index < mState.mUniforms.size());
1180 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181
1182 if (bufsize > 0)
1183 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001184 std::string string = uniform.name;
1185 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186 {
1187 string += "[0]";
1188 }
jchen10fd7c3b52017-03-21 15:36:03 +08001189 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001190 }
1191
Jamie Madill62d31cb2015-09-11 13:25:51 -04001192 *size = uniform.elementCount();
1193 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001194 }
1195 else
1196 {
1197 if (bufsize > 0)
1198 {
1199 name[0] = '\0';
1200 }
1201
1202 if (length)
1203 {
1204 *length = 0;
1205 }
1206
1207 *size = 0;
1208 *type = GL_NONE;
1209 }
1210}
1211
Geoff Lange1a27752015-10-05 13:16:04 -04001212GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001213{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001215 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001217 }
1218 else
1219 {
1220 return 0;
1221 }
1222}
1223
Geoff Lange1a27752015-10-05 13:16:04 -04001224GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001225{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001226 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001227
1228 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001229 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001230 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001231 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001232 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001234 size_t length = uniform.name.length() + 1u;
1235 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001236 {
1237 length += 3; // Counting in "[0]".
1238 }
1239 maxLength = std::max(length, maxLength);
1240 }
1241 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001242 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001243
Jamie Madill62d31cb2015-09-11 13:25:51 -04001244 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245}
1246
1247GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1248{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001249 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001250 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1254 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1255 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001256 case GL_UNIFORM_BLOCK_INDEX:
1257 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1259 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1260 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1261 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1262 default:
1263 UNREACHABLE();
1264 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001265 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001266 return 0;
1267}
1268
1269bool Program::isValidUniformLocation(GLint location) const
1270{
Jamie Madille2e406c2016-06-02 13:04:10 -04001271 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001272 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1273 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001274}
1275
Jamie Madill62d31cb2015-09-11 13:25:51 -04001276const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001278 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001279 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280}
1281
Jamie Madillac4e9c32017-01-13 14:07:12 -05001282const VariableLocation &Program::getUniformLocation(GLint location) const
1283{
1284 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1285 return mState.mUniformLocations[location];
1286}
1287
1288const std::vector<VariableLocation> &Program::getUniformLocations() const
1289{
1290 return mState.mUniformLocations;
1291}
1292
1293const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1294{
1295 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1296 return mState.mUniforms[index];
1297}
1298
Jamie Madill62d31cb2015-09-11 13:25:51 -04001299GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001300{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001301 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302}
1303
Jamie Madill62d31cb2015-09-11 13:25:51 -04001304GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305{
Jamie Madille7d84322017-01-10 18:21:59 -05001306 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307}
1308
1309void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1310{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001311 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1312 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313}
1314
1315void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1316{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001317 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1318 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
1321void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1322{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001323 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1324 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325}
1326
1327void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1328{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001329 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1330 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331}
1332
1333void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1334{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001335 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1336 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
1339void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1340{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001341 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1342 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343}
1344
1345void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1346{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001347 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1348 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349}
1350
1351void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1352{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001353 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1354 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355}
1356
1357void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1358{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001359 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1360 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361}
1362
1363void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1364{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001365 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1366 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001367}
1368
1369void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1370{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001371 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1372 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
1375void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1376{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001377 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1378 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
1381void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1382{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001383 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1384 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385}
1386
1387void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1388{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001389 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1390 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391}
1392
1393void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1394{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001395 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1396 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397}
1398
1399void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1400{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001401 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1402 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
1405void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1406{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001407 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1408 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
1411void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1412{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001413 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1414 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
1417void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1418{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001419 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1420 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421}
1422
1423void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1424{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001425 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1426 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427}
1428
1429void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1430{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001431 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1432 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433}
1434
Geoff Lange1a27752015-10-05 13:16:04 -04001435void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001437 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438}
1439
Geoff Lange1a27752015-10-05 13:16:04 -04001440void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001441{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001442 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443}
1444
Geoff Lange1a27752015-10-05 13:16:04 -04001445void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001447 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448}
1449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450void Program::flagForDeletion()
1451{
1452 mDeleteStatus = true;
1453}
1454
1455bool Program::isFlaggedForDeletion() const
1456{
1457 return mDeleteStatus;
1458}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001459
Brandon Jones43a53e22014-08-28 16:23:22 -07001460void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001461{
1462 mInfoLog.reset();
1463
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001465 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001466 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001467 }
1468 else
1469 {
Jamie Madillf6113162015-05-07 11:49:21 -04001470 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001471 }
1472}
1473
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1475{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001476 // Skip cache if we're using an infolog, so we get the full error.
1477 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1478 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1479 {
1480 return mCachedValidateSamplersResult.value();
1481 }
1482
1483 if (mTextureUnitTypesCache.empty())
1484 {
1485 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1486 }
1487 else
1488 {
1489 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1490 }
1491
1492 // if any two active samplers in a program are of different types, but refer to the same
1493 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1494 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001495 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001496 {
Jamie Madille7d84322017-01-10 18:21:59 -05001497 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001498
Jamie Madille7d84322017-01-10 18:21:59 -05001499 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001500 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001501 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1502 {
1503 if (infoLog)
1504 {
1505 (*infoLog) << "Sampler uniform (" << textureUnit
1506 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1507 << caps.maxCombinedTextureImageUnits << ")";
1508 }
1509
1510 mCachedValidateSamplersResult = false;
1511 return false;
1512 }
1513
1514 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1515 {
1516 if (textureType != mTextureUnitTypesCache[textureUnit])
1517 {
1518 if (infoLog)
1519 {
1520 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1521 "image unit ("
1522 << textureUnit << ").";
1523 }
1524
1525 mCachedValidateSamplersResult = false;
1526 return false;
1527 }
1528 }
1529 else
1530 {
1531 mTextureUnitTypesCache[textureUnit] = textureType;
1532 }
1533 }
1534 }
1535
1536 mCachedValidateSamplersResult = true;
1537 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001538}
1539
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001540bool Program::isValidated() const
1541{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001542 return mValidated;
1543}
1544
Geoff Lange1a27752015-10-05 13:16:04 -04001545GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001546{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001547 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548}
1549
1550void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1551{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001552 ASSERT(
1553 uniformBlockIndex <
1554 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555
Jamie Madill48ef11b2016-04-27 15:21:52 -04001556 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557
1558 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001559 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560 std::string string = uniformBlock.name;
1561
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001563 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001564 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001565 }
jchen10fd7c3b52017-03-21 15:36:03 +08001566 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001567 }
1568}
1569
Geoff Lange1a27752015-10-05 13:16:04 -04001570GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001571{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001572 int maxLength = 0;
1573
1574 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001575 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001576 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001577 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1578 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001579 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580 if (!uniformBlock.name.empty())
1581 {
jchen10af713a22017-04-19 09:10:56 +08001582 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1583 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584 }
1585 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001586 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587
1588 return maxLength;
1589}
1590
Geoff Lange1a27752015-10-05 13:16:04 -04001591GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001592{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001593 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001594 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001595
Jamie Madill48ef11b2016-04-27 15:21:52 -04001596 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001597 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1598 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001599 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600 if (uniformBlock.name == baseName)
1601 {
1602 const bool arrayElementZero =
1603 (subscript == GL_INVALID_INDEX &&
1604 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1605 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1606 {
1607 return blockIndex;
1608 }
1609 }
1610 }
1611
1612 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001613}
1614
Jamie Madill62d31cb2015-09-11 13:25:51 -04001615const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001616{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001617 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1618 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001619}
1620
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001621void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1622{
jchen107a20b972017-06-13 14:25:26 +08001623 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001624 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001625 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001626}
1627
1628GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1629{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001630 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001631}
1632
Geoff Lang48dcae72014-02-05 16:28:24 -05001633void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1634{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001635 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001636 for (GLsizei i = 0; i < count; i++)
1637 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001638 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001639 }
1640
Jamie Madill48ef11b2016-04-27 15:21:52 -04001641 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001642}
1643
1644void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1645{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001647 {
jchen10a9042d32017-03-17 08:50:45 +08001648 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1649 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1650 std::string varName = var.nameWithArrayIndex();
1651 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001652 if (length)
1653 {
1654 *length = lastNameIdx;
1655 }
1656 if (size)
1657 {
jchen10a9042d32017-03-17 08:50:45 +08001658 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001659 }
1660 if (type)
1661 {
jchen10a9042d32017-03-17 08:50:45 +08001662 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001663 }
1664 if (name)
1665 {
jchen10a9042d32017-03-17 08:50:45 +08001666 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001667 name[lastNameIdx] = '\0';
1668 }
1669 }
1670}
1671
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001672GLsizei Program::getTransformFeedbackVaryingCount() const
1673{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001675 {
jchen10a9042d32017-03-17 08:50:45 +08001676 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001677 }
1678 else
1679 {
1680 return 0;
1681 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001682}
1683
1684GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1685{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001687 {
1688 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001689 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001690 {
jchen10a9042d32017-03-17 08:50:45 +08001691 maxSize =
1692 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001693 }
1694
1695 return maxSize;
1696 }
1697 else
1698 {
1699 return 0;
1700 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001701}
1702
1703GLenum Program::getTransformFeedbackBufferMode() const
1704{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001705 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706}
1707
Jamie Madillbd044ed2017-06-05 12:59:21 -04001708bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001710 Shader *vertexShader = mState.mAttachedVertexShader;
1711 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001712
Jamie Madillbd044ed2017-06-05 12:59:21 -04001713 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001714
Jamie Madillbd044ed2017-06-05 12:59:21 -04001715 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1716 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001717
Sami Väisänen46eaa942016-06-29 10:26:37 +03001718 std::map<GLuint, std::string> staticFragmentInputLocations;
1719
Jamie Madill4cff2472015-08-21 16:53:18 -04001720 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001721 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722 bool matched = false;
1723
1724 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001725 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001726 {
1727 continue;
1728 }
1729
Jamie Madill4cff2472015-08-21 16:53:18 -04001730 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001731 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001732 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001733 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001734 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001735 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001736 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001737 {
1738 return false;
1739 }
1740
Geoff Lang7dd2e102014-11-10 15:19:26 -05001741 matched = true;
1742 break;
1743 }
1744 }
1745
1746 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001747 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001748 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001749 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001750 return false;
1751 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001752
1753 // Check for aliased path rendering input bindings (if any).
1754 // If more than one binding refer statically to the same
1755 // location the link must fail.
1756
1757 if (!output.staticUse)
1758 continue;
1759
1760 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1761 if (inputBinding == -1)
1762 continue;
1763
1764 const auto it = staticFragmentInputLocations.find(inputBinding);
1765 if (it == std::end(staticFragmentInputLocations))
1766 {
1767 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1768 }
1769 else
1770 {
1771 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1772 << it->second;
1773 return false;
1774 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 }
1776
Jamie Madillbd044ed2017-06-05 12:59:21 -04001777 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001778 {
1779 return false;
1780 }
1781
Jamie Madillada9ecc2015-08-17 12:53:37 -04001782 // TODO(jmadill): verify no unmatched vertex varyings?
1783
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784 return true;
1785}
1786
Jamie Madillbd044ed2017-06-05 12:59:21 -04001787bool Program::linkUniforms(const Context *context,
1788 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001789 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001790{
Olli Etuahob78707c2017-03-09 15:03:11 +00001791 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001792 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001793 {
1794 return false;
1795 }
1796
Olli Etuahob78707c2017-03-09 15:03:11 +00001797 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001798
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001799 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001800
jchen10eaef1e52017-06-13 10:44:11 +08001801 if (!linkAtomicCounterBuffers())
1802 {
1803 return false;
1804 }
1805
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001806 return true;
1807}
1808
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001809void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001810{
Jamie Madill982f6e02017-06-07 14:33:04 -04001811 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1812 unsigned int low = high;
1813
jchen10eaef1e52017-06-13 10:44:11 +08001814 for (auto counterIter = mState.mUniforms.rbegin();
1815 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1816 {
1817 --low;
1818 }
1819
1820 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1821
1822 high = low;
1823
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001824 for (auto imageIter = mState.mUniforms.rbegin();
1825 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1826 {
1827 --low;
1828 }
1829
1830 mState.mImageUniformRange = RangeUI(low, high);
1831
1832 // If uniform is a image type, insert it into the mImageBindings array.
1833 for (unsigned int imageIndex : mState.mImageUniformRange)
1834 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001835 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1836 // cannot load values into a uniform defined as an image. if declare without a
1837 // binding qualifier, any uniform image variable (include all elements of
1838 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001839 auto &imageUniform = mState.mUniforms[imageIndex];
1840 if (imageUniform.binding == -1)
1841 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001842 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001843 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001844 else
1845 {
1846 mState.mImageBindings.emplace_back(
1847 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1848 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001849 }
1850
1851 high = low;
1852
1853 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001854 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001855 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001856 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001857 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001858
1859 mState.mSamplerUniformRange = RangeUI(low, high);
1860
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001861 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001862 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001863 {
1864 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1865 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1866 mState.mSamplerBindings.emplace_back(
1867 SamplerBinding(textureType, samplerUniform.elementCount()));
1868 }
1869}
1870
jchen10eaef1e52017-06-13 10:44:11 +08001871bool Program::linkAtomicCounterBuffers()
1872{
1873 for (unsigned int index : mState.mAtomicCounterUniformRange)
1874 {
1875 auto &uniform = mState.mUniforms[index];
1876 bool found = false;
1877 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1878 ++bufferIndex)
1879 {
1880 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1881 if (buffer.binding == uniform.binding)
1882 {
1883 buffer.memberIndexes.push_back(index);
1884 uniform.bufferIndex = bufferIndex;
1885 found = true;
1886 break;
1887 }
1888 }
1889 if (!found)
1890 {
1891 AtomicCounterBuffer atomicCounterBuffer;
1892 atomicCounterBuffer.binding = uniform.binding;
1893 atomicCounterBuffer.memberIndexes.push_back(index);
1894 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1895 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1896 }
1897 }
1898 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1899 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1900
1901 return true;
1902}
1903
Martin Radev4c4c8e72016-08-04 12:25:34 +03001904bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1905 const std::string &uniformName,
1906 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001907 const sh::InterfaceBlockField &fragmentUniform,
1908 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001909{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001910 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1911 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1912 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 {
1914 return false;
1915 }
1916
1917 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1918 {
Jamie Madillf6113162015-05-07 11:49:21 -04001919 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001920 return false;
1921 }
1922
1923 return true;
1924}
1925
Jamie Madilleb979bf2016-11-15 12:28:46 -05001926// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001927bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001929 const ContextState &data = context->getContextState();
1930 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001931
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001933 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001934 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001935
1936 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001937 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001938 {
Jamie Madillf6113162015-05-07 11:49:21 -04001939 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001940 return false;
1941 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001942
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001943 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001944
Jamie Madillc349ec02015-08-21 16:53:12 -04001945 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001946 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001947 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001948 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001949 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001950 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001951 attribute.location = bindingLocation;
1952 }
1953
1954 if (attribute.location != -1)
1955 {
1956 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001957 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001958
Jamie Madill63805b42015-08-25 13:17:39 -04001959 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 {
Jamie Madillf6113162015-05-07 11:49:21 -04001961 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001962 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001963
1964 return false;
1965 }
1966
Jamie Madill63805b42015-08-25 13:17:39 -04001967 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 {
Jamie Madill63805b42015-08-25 13:17:39 -04001969 const int regLocation = attribute.location + reg;
1970 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971
1972 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001973 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001974 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001976 // TODO(jmadill): fix aliasing on ES2
1977 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001978 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001979 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001980 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001981 return false;
1982 }
1983 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001984 else
1985 {
Jamie Madill63805b42015-08-25 13:17:39 -04001986 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001987 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988
Jamie Madill63805b42015-08-25 13:17:39 -04001989 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990 }
1991 }
1992 }
1993
1994 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001995 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001997 // Not set by glBindAttribLocation or by location layout qualifier
1998 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999 {
Jamie Madill63805b42015-08-25 13:17:39 -04002000 int regs = VariableRegisterCount(attribute.type);
2001 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002
Jamie Madill63805b42015-08-25 13:17:39 -04002003 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004 {
Jamie Madillf6113162015-05-07 11:49:21 -04002005 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002006 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 }
2008
Jamie Madillc349ec02015-08-21 16:53:12 -04002009 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 }
2011 }
2012
Jamie Madill48ef11b2016-04-27 15:21:52 -04002013 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 {
Jamie Madill63805b42015-08-25 13:17:39 -04002015 ASSERT(attribute.location != -1);
2016 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002017
Jamie Madill63805b42015-08-25 13:17:39 -04002018 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002020 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021 }
2022 }
2023
Geoff Lang7dd2e102014-11-10 15:19:26 -05002024 return true;
2025}
2026
Martin Radev4c4c8e72016-08-04 12:25:34 +03002027bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2028 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2029 const std::string &errorMessage,
2030 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002032 GLuint blockCount = 0;
2033 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002035 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002036 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002037 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002038 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002039 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002040 return false;
2041 }
2042 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002044 return true;
2045}
Jamie Madille473dee2015-08-18 14:49:01 -04002046
Martin Radev4c4c8e72016-08-04 12:25:34 +03002047bool Program::validateVertexAndFragmentInterfaceBlocks(
2048 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2049 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002050 InfoLog &infoLog,
2051 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002052{
2053 // Check that interface blocks defined in the vertex and fragment shaders are identical
2054 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2055 UniformBlockMap linkedUniformBlocks;
2056
2057 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2058 {
2059 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2060 }
2061
Jamie Madille473dee2015-08-18 14:49:01 -04002062 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 {
Jamie Madille473dee2015-08-18 14:49:01 -04002064 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065 if (entry != linkedUniformBlocks.end())
2066 {
2067 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002068 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2069 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070 {
2071 return false;
2072 }
2073 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002074 }
2075 return true;
2076}
Jamie Madille473dee2015-08-18 14:49:01 -04002077
Jamie Madillbd044ed2017-06-05 12:59:21 -04002078bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002079{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002080 const auto &caps = context->getCaps();
2081
Martin Radev4c4c8e72016-08-04 12:25:34 +03002082 if (mState.mAttachedComputeShader)
2083 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002084 Shader &computeShader = *mState.mAttachedComputeShader;
2085 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002086
2087 if (!validateUniformBlocksCount(
2088 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2089 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2090 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002092 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002094 return true;
2095 }
2096
Jamie Madillbd044ed2017-06-05 12:59:21 -04002097 Shader &vertexShader = *mState.mAttachedVertexShader;
2098 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002099
Jamie Madillbd044ed2017-06-05 12:59:21 -04002100 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2101 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002102
2103 if (!validateUniformBlocksCount(
2104 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2105 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2106 {
2107 return false;
2108 }
2109 if (!validateUniformBlocksCount(
2110 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2111 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2112 infoLog))
2113 {
2114
2115 return false;
2116 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002117
2118 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002119 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002120 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002121 {
2122 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 }
Jamie Madille473dee2015-08-18 14:49:01 -04002124
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 return true;
2126}
2127
Jamie Madilla2c74982016-12-12 11:20:42 -05002128bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002129 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002130 const sh::InterfaceBlock &fragmentInterfaceBlock,
2131 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132{
2133 const char* blockName = vertexInterfaceBlock.name.c_str();
2134 // validate blocks for the same member types
2135 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2136 {
Jamie Madillf6113162015-05-07 11:49:21 -04002137 infoLog << "Types for interface block '" << blockName
2138 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139 return false;
2140 }
2141 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2142 {
Jamie Madillf6113162015-05-07 11:49:21 -04002143 infoLog << "Array sizes differ for interface block '" << blockName
2144 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 return false;
2146 }
jchen10af713a22017-04-19 09:10:56 +08002147 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2148 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2149 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150 {
Jamie Madillf6113162015-05-07 11:49:21 -04002151 infoLog << "Layout qualifiers differ for interface block '" << blockName
2152 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153 return false;
2154 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002155 const unsigned int numBlockMembers =
2156 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2158 {
2159 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2160 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2161 if (vertexMember.name != fragmentMember.name)
2162 {
Jamie Madillf6113162015-05-07 11:49:21 -04002163 infoLog << "Name mismatch for field " << blockMemberIndex
2164 << " of interface block '" << blockName
2165 << "': (in vertex: '" << vertexMember.name
2166 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 return false;
2168 }
2169 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002170 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2171 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
2173 return false;
2174 }
2175 }
2176 return true;
2177}
2178
2179bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2180 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2181{
2182 if (vertexVariable.type != fragmentVariable.type)
2183 {
Jamie Madillf6113162015-05-07 11:49:21 -04002184 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002185 return false;
2186 }
2187 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2188 {
Jamie Madillf6113162015-05-07 11:49:21 -04002189 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190 return false;
2191 }
2192 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2193 {
Jamie Madillf6113162015-05-07 11:49:21 -04002194 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002195 return false;
2196 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002197 if (vertexVariable.structName != fragmentVariable.structName)
2198 {
2199 infoLog << "Structure names for " << variableName
2200 << " differ between vertex and fragment shaders";
2201 return false;
2202 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203
2204 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2205 {
Jamie Madillf6113162015-05-07 11:49:21 -04002206 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207 return false;
2208 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002209 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2211 {
2212 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2213 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2214
2215 if (vertexMember.name != fragmentMember.name)
2216 {
Jamie Madillf6113162015-05-07 11:49:21 -04002217 infoLog << "Name mismatch for field '" << memberIndex
2218 << "' of " << variableName
2219 << ": (in vertex: '" << vertexMember.name
2220 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 return false;
2222 }
2223
2224 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2225 vertexMember.name + "'";
2226
2227 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2228 {
2229 return false;
2230 }
2231 }
2232
2233 return true;
2234}
2235
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002236bool Program::linkValidateVaryings(InfoLog &infoLog,
2237 const std::string &varyingName,
2238 const sh::Varying &vertexVarying,
2239 const sh::Varying &fragmentVarying,
2240 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002241{
2242 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2243 {
2244 return false;
2245 }
2246
Jamie Madille9cc4692015-02-19 16:00:13 -05002247 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002249 infoLog << "Interpolation types for " << varyingName
2250 << " differ between vertex and fragment shaders.";
2251 return false;
2252 }
2253
2254 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2255 {
2256 infoLog << "Invariance for " << varyingName
2257 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002258 return false;
2259 }
2260
2261 return true;
2262}
2263
Jamie Madillbd044ed2017-06-05 12:59:21 -04002264bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002265{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002266 Shader *vertexShader = mState.mAttachedVertexShader;
2267 Shader *fragmentShader = mState.mAttachedFragmentShader;
2268 const auto &vertexVaryings = vertexShader->getVaryings(context);
2269 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2270 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002271
2272 if (shaderVersion != 100)
2273 {
2274 // Only ESSL 1.0 has restrictions on matching input and output invariance
2275 return true;
2276 }
2277
2278 bool glPositionIsInvariant = false;
2279 bool glPointSizeIsInvariant = false;
2280 bool glFragCoordIsInvariant = false;
2281 bool glPointCoordIsInvariant = false;
2282
2283 for (const sh::Varying &varying : vertexVaryings)
2284 {
2285 if (!varying.isBuiltIn())
2286 {
2287 continue;
2288 }
2289 if (varying.name.compare("gl_Position") == 0)
2290 {
2291 glPositionIsInvariant = varying.isInvariant;
2292 }
2293 else if (varying.name.compare("gl_PointSize") == 0)
2294 {
2295 glPointSizeIsInvariant = varying.isInvariant;
2296 }
2297 }
2298
2299 for (const sh::Varying &varying : fragmentVaryings)
2300 {
2301 if (!varying.isBuiltIn())
2302 {
2303 continue;
2304 }
2305 if (varying.name.compare("gl_FragCoord") == 0)
2306 {
2307 glFragCoordIsInvariant = varying.isInvariant;
2308 }
2309 else if (varying.name.compare("gl_PointCoord") == 0)
2310 {
2311 glPointCoordIsInvariant = varying.isInvariant;
2312 }
2313 }
2314
2315 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2316 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2317 // Not requiring invariance to match is supported by:
2318 // dEQP, WebGL CTS, Nexus 5X GLES
2319 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2320 {
2321 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2322 "declared invariant.";
2323 return false;
2324 }
2325 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2326 {
2327 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2328 "declared invariant.";
2329 return false;
2330 }
2331
2332 return true;
2333}
2334
jchen10a9042d32017-03-17 08:50:45 +08002335bool Program::linkValidateTransformFeedback(const gl::Context *context,
2336 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002337 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002338 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002339{
2340 size_t totalComponents = 0;
2341
Jamie Madillccdf74b2015-08-18 10:46:12 -04002342 std::set<std::string> uniqueNames;
2343
Jamie Madill48ef11b2016-04-27 15:21:52 -04002344 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345 {
2346 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002347 size_t subscript = GL_INVALID_INDEX;
2348 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2349
Jamie Madill192745a2016-12-22 15:58:21 -05002350 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351 {
Jamie Madill192745a2016-12-22 15:58:21 -05002352 const sh::Varying *varying = ref.second.get();
2353
jchen10a9042d32017-03-17 08:50:45 +08002354 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002355 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002356 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002357 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002358 infoLog << "Two transform feedback varyings specify the same output variable ("
2359 << tfVaryingName << ").";
2360 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361 }
jchen10a9042d32017-03-17 08:50:45 +08002362 if (context->getClientVersion() >= Version(3, 1))
2363 {
2364 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2365 {
2366 infoLog
2367 << "Two transform feedback varyings include the same array element ("
2368 << tfVaryingName << ").";
2369 return false;
2370 }
2371 }
2372 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002373 {
2374 infoLog << "Capture of arrays is undefined and not supported.";
2375 return false;
2376 }
2377
jchen10a9042d32017-03-17 08:50:45 +08002378 uniqueNames.insert(tfVaryingName);
2379
Jamie Madillccdf74b2015-08-18 10:46:12 -04002380 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002381 size_t elementCount =
2382 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2383 : 1);
2384 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002385 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386 componentCount > caps.maxTransformFeedbackSeparateComponents)
2387 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002388 infoLog << "Transform feedback varying's " << varying->name << " components ("
2389 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002390 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 return false;
2392 }
2393
2394 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002395 found = true;
2396 break;
2397 }
2398 }
jchen10a9042d32017-03-17 08:50:45 +08002399 if (context->getClientVersion() < Version(3, 1) &&
2400 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002401 {
Geoff Lang1a683462015-09-29 15:09:59 -04002402 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002403 return false;
2404 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002405 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2406 ASSERT(found);
2407 }
2408
Jamie Madill48ef11b2016-04-27 15:21:52 -04002409 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002410 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 {
Jamie Madillf6113162015-05-07 11:49:21 -04002412 infoLog << "Transform feedback varying total components (" << totalComponents
2413 << ") exceed the maximum interleaved components ("
2414 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415 return false;
2416 }
2417
2418 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002419}
2420
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002421bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2422{
2423 const std::vector<sh::Uniform> &vertexUniforms =
2424 mState.mAttachedVertexShader->getUniforms(context);
2425 const std::vector<sh::Uniform> &fragmentUniforms =
2426 mState.mAttachedFragmentShader->getUniforms(context);
2427 const std::vector<sh::Attribute> &attributes =
2428 mState.mAttachedVertexShader->getActiveAttributes(context);
2429 for (const auto &attrib : attributes)
2430 {
2431 for (const auto &uniform : vertexUniforms)
2432 {
2433 if (uniform.name == attrib.name)
2434 {
2435 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2436 return false;
2437 }
2438 }
2439 for (const auto &uniform : fragmentUniforms)
2440 {
2441 if (uniform.name == attrib.name)
2442 {
2443 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2444 return false;
2445 }
2446 }
2447 }
2448 return true;
2449}
2450
Jamie Madill192745a2016-12-22 15:58:21 -05002451void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002452{
2453 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002454 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002455 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002456 {
jchen10a9042d32017-03-17 08:50:45 +08002457 size_t subscript = GL_INVALID_INDEX;
2458 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002459 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002460 {
Jamie Madill192745a2016-12-22 15:58:21 -05002461 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002462 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002463 {
jchen10a9042d32017-03-17 08:50:45 +08002464 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2465 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002466 break;
2467 }
2468 }
2469 }
2470}
2471
Jamie Madillbd044ed2017-06-05 12:59:21 -04002472Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002473{
Jamie Madill192745a2016-12-22 15:58:21 -05002474 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002475
Jamie Madillbd044ed2017-06-05 12:59:21 -04002476 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002477 {
Jamie Madill192745a2016-12-22 15:58:21 -05002478 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002479 }
2480
Jamie Madillbd044ed2017-06-05 12:59:21 -04002481 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002482 {
Jamie Madill192745a2016-12-22 15:58:21 -05002483 merged[varying.name].fragment = &varying;
2484 }
2485
2486 return merged;
2487}
2488
2489std::vector<PackedVarying> Program::getPackedVaryings(
2490 const Program::MergedVaryings &mergedVaryings) const
2491{
2492 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2493 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002494 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002495
2496 for (const auto &ref : mergedVaryings)
2497 {
2498 const sh::Varying *input = ref.second.vertex;
2499 const sh::Varying *output = ref.second.fragment;
2500
2501 // Only pack varyings that have a matched input or output, plus special builtins.
2502 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002503 {
Jamie Madill192745a2016-12-22 15:58:21 -05002504 // Will get the vertex shader interpolation by default.
2505 auto interpolation = ref.second.get()->interpolation;
2506
Olli Etuaho06a06f52017-07-12 12:22:15 +03002507 // Note that we lose the vertex shader static use information here. The data for the
2508 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002509 if (output->isStruct())
2510 {
2511 ASSERT(!output->isArray());
2512 for (const auto &field : output->fields)
2513 {
2514 ASSERT(!field.isStruct() && !field.isArray());
2515 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2516 }
2517 }
2518 else
2519 {
2520 packedVaryings.push_back(PackedVarying(*output, interpolation));
2521 }
2522 continue;
2523 }
2524
2525 // Keep Transform FB varyings in the merged list always.
2526 if (!input)
2527 {
2528 continue;
2529 }
2530
2531 for (const std::string &tfVarying : tfVaryings)
2532 {
jchen10a9042d32017-03-17 08:50:45 +08002533 size_t subscript = GL_INVALID_INDEX;
2534 std::string baseName = ParseResourceName(tfVarying, &subscript);
2535 if (uniqueFullNames.count(tfVarying) > 0)
2536 {
2537 continue;
2538 }
2539 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002540 {
2541 // Transform feedback for varying structs is underspecified.
2542 // See Khronos bug 9856.
2543 // TODO(jmadill): Figure out how to be spec-compliant here.
2544 if (!input->isStruct())
2545 {
2546 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2547 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002548 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2549 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002550 }
jchen10a9042d32017-03-17 08:50:45 +08002551 if (subscript == GL_INVALID_INDEX)
2552 {
2553 break;
2554 }
Jamie Madill192745a2016-12-22 15:58:21 -05002555 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002556 }
2557 }
2558
Jamie Madill192745a2016-12-22 15:58:21 -05002559 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2560
2561 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002562}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002563
Jamie Madillbd044ed2017-06-05 12:59:21 -04002564void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002565{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002566 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002567 ASSERT(fragmentShader != nullptr);
2568
Geoff Lange0cff192017-05-30 13:04:56 -04002569 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002570 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002571
2572 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002573 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002574 {
2575 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2576 outputVariable.name != "gl_FragData")
2577 {
2578 continue;
2579 }
2580
2581 unsigned int baseLocation =
2582 (outputVariable.location == -1 ? 0u
2583 : static_cast<unsigned int>(outputVariable.location));
2584 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2585 elementIndex++)
2586 {
2587 const unsigned int location = baseLocation + elementIndex;
2588 if (location >= mState.mOutputVariableTypes.size())
2589 {
2590 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2591 }
Corentin Walleze7557742017-06-01 13:09:57 -04002592 ASSERT(location < mState.mActiveOutputVariables.size());
2593 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002594 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2595 }
2596 }
2597
Jamie Madill80a6fc02015-08-21 16:53:16 -04002598 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002599 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002600 return;
2601
Jamie Madillbd044ed2017-06-05 12:59:21 -04002602 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002603 // TODO(jmadill): any caps validation here?
2604
jchen1015015f72017-03-16 13:54:21 +08002605 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002606 outputVariableIndex++)
2607 {
jchen1015015f72017-03-16 13:54:21 +08002608 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002609
2610 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2611 if (outputVariable.isBuiltIn())
2612 continue;
2613
2614 // Since multiple output locations must be specified, use 0 for non-specified locations.
2615 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2616
Jamie Madill80a6fc02015-08-21 16:53:16 -04002617 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2618 elementIndex++)
2619 {
2620 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002621 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002622 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002623 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002624 VariableLocation(outputVariable.name, element, outputVariableIndex);
2625 }
2626 }
2627}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002628
Olli Etuaho48fed632017-03-16 12:05:30 +00002629void Program::setUniformValuesFromBindingQualifiers()
2630{
Jamie Madill982f6e02017-06-07 14:33:04 -04002631 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002632 {
2633 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2634 if (samplerUniform.binding != -1)
2635 {
2636 GLint location = mState.getUniformLocation(samplerUniform.name);
2637 ASSERT(location != -1);
2638 std::vector<GLint> boundTextureUnits;
2639 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2640 ++elementIndex)
2641 {
2642 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2643 }
2644 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2645 boundTextureUnits.data());
2646 }
2647 }
2648}
2649
jchen10eaef1e52017-06-13 10:44:11 +08002650void Program::gatherAtomicCounterBuffers()
2651{
2652 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2653 // counter.
2654 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2655}
2656
Jamie Madillbd044ed2017-06-05 12:59:21 -04002657void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002658{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002659 ASSERT(mState.mUniformBlocks.empty());
2660
2661 if (mState.mAttachedComputeShader)
2662 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002663 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002664
Jamie Madillbd044ed2017-06-05 12:59:21 -04002665 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002666 {
2667
2668 // Only 'packed' blocks are allowed to be considered inactive.
2669 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2670 continue;
2671
Jamie Madilla2c74982016-12-12 11:20:42 -05002672 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002673 {
2674 if (block.name == computeBlock.name)
2675 {
2676 block.computeStaticUse = computeBlock.staticUse;
2677 }
2678 }
2679
2680 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2681 }
2682 return;
2683 }
2684
Jamie Madill62d31cb2015-09-11 13:25:51 -04002685 std::set<std::string> visitedList;
2686
Jamie Madillbd044ed2017-06-05 12:59:21 -04002687 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002688
Jamie Madillbd044ed2017-06-05 12:59:21 -04002689 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002690 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002691 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002692 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2693 continue;
2694
2695 if (visitedList.count(vertexBlock.name) > 0)
2696 continue;
2697
2698 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2699 visitedList.insert(vertexBlock.name);
2700 }
2701
Jamie Madillbd044ed2017-06-05 12:59:21 -04002702 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002703
Jamie Madillbd044ed2017-06-05 12:59:21 -04002704 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002705 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002706 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002707 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2708 continue;
2709
2710 if (visitedList.count(fragmentBlock.name) > 0)
2711 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002712 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002713 {
2714 if (block.name == fragmentBlock.name)
2715 {
2716 block.fragmentStaticUse = fragmentBlock.staticUse;
2717 }
2718 }
2719
2720 continue;
2721 }
2722
2723 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2724 visitedList.insert(fragmentBlock.name);
2725 }
jchen10af713a22017-04-19 09:10:56 +08002726 // Set initial bindings from shader.
2727 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2728 {
2729 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2730 bindUniformBlock(blockIndex, uniformBlock.binding);
2731 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002732}
2733
Jamie Madill4a3c2342015-10-08 12:58:45 -04002734template <typename VarT>
2735void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2736 const std::string &prefix,
2737 int blockIndex)
2738{
2739 for (const VarT &field : fields)
2740 {
2741 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2742
2743 if (field.isStruct())
2744 {
2745 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2746 {
2747 const std::string uniformElementName =
2748 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2749 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2750 }
2751 }
2752 else
2753 {
2754 // If getBlockMemberInfo returns false, the uniform is optimized out.
2755 sh::BlockMemberInfo memberInfo;
2756 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2757 {
2758 continue;
2759 }
2760
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002761 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002762 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002763
2764 // Since block uniforms have no location, we don't need to store them in the uniform
2765 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002766 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002767 }
2768 }
2769}
2770
Jamie Madill62d31cb2015-09-11 13:25:51 -04002771void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2772{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002773 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002774 size_t blockSize = 0;
2775
Jamie Madill4a3c2342015-10-08 12:58:45 -04002776 // Track the first and last uniform index to determine the range of active uniforms in the
2777 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002778 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002779 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002780 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002781
2782 std::vector<unsigned int> blockUniformIndexes;
2783 for (size_t blockUniformIndex = firstBlockUniformIndex;
2784 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2785 {
2786 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2787 }
jchen10af713a22017-04-19 09:10:56 +08002788 // ESSL 3.10 section 4.4.4 page 58:
2789 // Any uniform or shader storage block declared without a binding qualifier is initially
2790 // assigned to block binding point zero.
2791 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002792 if (interfaceBlock.arraySize > 0)
2793 {
2794 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2795 {
jchen10af713a22017-04-19 09:10:56 +08002796 // Don't define this block at all if it's not active in the implementation.
2797 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2798 &blockSize))
2799 {
2800 continue;
2801 }
2802 UniformBlock block(interfaceBlock.name, true, arrayElement,
2803 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002804 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002805
Martin Radev4c4c8e72016-08-04 12:25:34 +03002806 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002807 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002808 case GL_VERTEX_SHADER:
2809 {
2810 block.vertexStaticUse = interfaceBlock.staticUse;
2811 break;
2812 }
2813 case GL_FRAGMENT_SHADER:
2814 {
2815 block.fragmentStaticUse = interfaceBlock.staticUse;
2816 break;
2817 }
2818 case GL_COMPUTE_SHADER:
2819 {
2820 block.computeStaticUse = interfaceBlock.staticUse;
2821 break;
2822 }
2823 default:
2824 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002825 }
2826
Qin Jiajia0350a642016-11-01 17:01:51 +08002827 // Since all block elements in an array share the same active uniforms, they will all be
2828 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2829 // here we will add every block element in the array.
2830 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002831 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002832 }
2833 }
2834 else
2835 {
jchen10af713a22017-04-19 09:10:56 +08002836 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2837 {
2838 return;
2839 }
2840 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002841 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842
Martin Radev4c4c8e72016-08-04 12:25:34 +03002843 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002844 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002845 case GL_VERTEX_SHADER:
2846 {
2847 block.vertexStaticUse = interfaceBlock.staticUse;
2848 break;
2849 }
2850 case GL_FRAGMENT_SHADER:
2851 {
2852 block.fragmentStaticUse = interfaceBlock.staticUse;
2853 break;
2854 }
2855 case GL_COMPUTE_SHADER:
2856 {
2857 block.computeStaticUse = interfaceBlock.staticUse;
2858 break;
2859 }
2860 default:
2861 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002862 }
2863
Jamie Madill4a3c2342015-10-08 12:58:45 -04002864 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002865 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002866 }
2867}
2868
Jamie Madille7d84322017-01-10 18:21:59 -05002869template <>
2870void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2871 const uint8_t *destPointer,
2872 GLsizei clampedCount,
2873 const GLint *v)
2874{
2875 // Invalidate the validation cache only if we modify the sampler data.
2876 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2877 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2878 {
2879 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2880 std::vector<GLuint> *boundTextureUnits =
2881 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2882
2883 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2884 mCachedValidateSamplersResult.reset();
2885 }
2886}
2887
2888template <typename T>
2889void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2890 const uint8_t *destPointer,
2891 GLsizei clampedCount,
2892 const T *v)
2893{
2894}
2895
Jamie Madill62d31cb2015-09-11 13:25:51 -04002896template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002897GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002898{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002899 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2900 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002901 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2902
Corentin Wallez15ac5342016-11-03 17:06:39 -04002903 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2904 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2905 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002906 GLsizei maxElementCount =
2907 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2908
2909 GLsizei count = countIn;
2910 GLsizei clampedCount = count * vectorSize;
2911 if (clampedCount > maxElementCount)
2912 {
2913 clampedCount = maxElementCount;
2914 count = maxElementCount / vectorSize;
2915 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002916
Jamie Madill44183cc2017-08-01 12:48:34 -04002917 // VariableComponentType(linkedUniform->type) has a dozens of compares and thus is evil for
2918 // inlining with regards to code size. This version is one subtract and one compare only.
2919 if (IsVariableComponentTypeBool(linkedUniform->type))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002920 {
2921 // Do a cast conversion for boolean types. From the spec:
2922 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2923 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002924 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002925 {
2926 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2927 }
2928 }
2929 else
2930 {
Jamie Madille7d84322017-01-10 18:21:59 -05002931 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002932 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002933 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002934
2935 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002936}
2937
2938template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002939GLsizei Program::setMatrixUniformInternal(GLint location,
2940 GLsizei count,
2941 GLboolean transpose,
2942 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002943{
2944 if (!transpose)
2945 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002946 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002947 }
2948
2949 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002950 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2951 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002952 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002953
2954 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2955 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2956 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2957 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2958
2959 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960 {
2961 size_t elementOffset = element * rows * cols;
2962
2963 for (size_t row = 0; row < rows; ++row)
2964 {
2965 for (size_t col = 0; col < cols; ++col)
2966 {
2967 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2968 }
2969 }
2970 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002971
2972 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002973}
2974
2975template <typename DestT>
2976void Program::getUniformInternal(GLint location, DestT *dataOut) const
2977{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002978 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2979 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002980
2981 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2982
2983 GLenum componentType = VariableComponentType(uniform.type);
2984 if (componentType == GLTypeToGLenum<DestT>::value)
2985 {
2986 memcpy(dataOut, srcPointer, uniform.getElementSize());
2987 return;
2988 }
2989
Corentin Wallez6596c462016-03-17 17:26:58 -04002990 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002991
2992 switch (componentType)
2993 {
2994 case GL_INT:
2995 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2996 break;
2997 case GL_UNSIGNED_INT:
2998 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2999 break;
3000 case GL_BOOL:
3001 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3002 break;
3003 case GL_FLOAT:
3004 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3005 break;
3006 default:
3007 UNREACHABLE();
3008 }
3009}
Jamie Madilla4595b82017-01-11 17:36:34 -05003010
3011bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3012{
3013 // Must be called after samplers are validated.
3014 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3015
3016 for (const auto &binding : mState.mSamplerBindings)
3017 {
3018 GLenum textureType = binding.textureType;
3019 for (const auto &unit : binding.boundTextureUnits)
3020 {
3021 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3022 if (programTextureID == textureID)
3023 {
3024 // TODO(jmadill): Check for appropriate overlap.
3025 return true;
3026 }
3027 }
3028 }
3029
3030 return false;
3031}
3032
Jamie Madilla2c74982016-12-12 11:20:42 -05003033} // namespace gl