blob: 1f47b9d0b69ffdb293275824599f7f4ffdc4b244 [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
jchen10880683b2017-04-12 16:21:55 +08001154const sh::Attribute &Program::getInputResource(GLuint index) const
1155{
1156 ASSERT(index < mState.mAttributes.size());
1157 return mState.mAttributes[index];
1158}
1159
1160const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1161{
1162 ASSERT(index < mState.mOutputVariables.size());
1163 return mState.mOutputVariables[index];
1164}
1165
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166GLint Program::getFragDataLocation(const std::string &name) const
1167{
1168 std::string baseName(name);
1169 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001170 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001171 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001172 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001173 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1174 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001175 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001176 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001177 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001179}
1180
Geoff Lange1a27752015-10-05 13:16:04 -04001181void Program::getActiveUniform(GLuint index,
1182 GLsizei bufsize,
1183 GLsizei *length,
1184 GLint *size,
1185 GLenum *type,
1186 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001187{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001188 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001189 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001190 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001191 ASSERT(index < mState.mUniforms.size());
1192 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193
1194 if (bufsize > 0)
1195 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001196 std::string string = uniform.name;
1197 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 {
1199 string += "[0]";
1200 }
jchen10fd7c3b52017-03-21 15:36:03 +08001201 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 }
1203
Jamie Madill62d31cb2015-09-11 13:25:51 -04001204 *size = uniform.elementCount();
1205 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001206 }
1207 else
1208 {
1209 if (bufsize > 0)
1210 {
1211 name[0] = '\0';
1212 }
1213
1214 if (length)
1215 {
1216 *length = 0;
1217 }
1218
1219 *size = 0;
1220 *type = GL_NONE;
1221 }
1222}
1223
Geoff Lange1a27752015-10-05 13:16:04 -04001224GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001225{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001227 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001228 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001229 }
1230 else
1231 {
1232 return 0;
1233 }
1234}
1235
Geoff Lange1a27752015-10-05 13:16:04 -04001236GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001237{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001238 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001239
1240 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001242 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001243 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001244 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001246 size_t length = uniform.name.length() + 1u;
1247 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001248 {
1249 length += 3; // Counting in "[0]".
1250 }
1251 maxLength = std::max(length, maxLength);
1252 }
1253 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001255
Jamie Madill62d31cb2015-09-11 13:25:51 -04001256 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257}
1258
1259GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1260{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001261 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001262 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001263 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001264 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1266 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1267 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001268 case GL_UNIFORM_BLOCK_INDEX:
1269 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1271 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1272 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1273 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1274 default:
1275 UNREACHABLE();
1276 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001277 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278 return 0;
1279}
1280
1281bool Program::isValidUniformLocation(GLint location) const
1282{
Jamie Madille2e406c2016-06-02 13:04:10 -04001283 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001284 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1285 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001286}
1287
Jamie Madill62d31cb2015-09-11 13:25:51 -04001288const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001290 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001291 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292}
1293
Jamie Madillac4e9c32017-01-13 14:07:12 -05001294const VariableLocation &Program::getUniformLocation(GLint location) const
1295{
1296 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1297 return mState.mUniformLocations[location];
1298}
1299
1300const std::vector<VariableLocation> &Program::getUniformLocations() const
1301{
1302 return mState.mUniformLocations;
1303}
1304
1305const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1306{
1307 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1308 return mState.mUniforms[index];
1309}
1310
Jamie Madill62d31cb2015-09-11 13:25:51 -04001311GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001312{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001313 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001314}
1315
Jamie Madill62d31cb2015-09-11 13:25:51 -04001316GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001317{
Jamie Madille7d84322017-01-10 18:21:59 -05001318 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
1321void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1322{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001323 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1324 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325}
1326
1327void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1328{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001329 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1330 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331}
1332
1333void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1334{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001335 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1336 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
1339void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1340{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001341 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1342 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343}
1344
1345void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1346{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001347 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1348 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349}
1350
1351void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1352{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001353 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1354 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355}
1356
1357void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1358{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001359 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1360 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361}
1362
1363void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1364{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001365 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1366 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001367}
1368
1369void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1370{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001371 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1372 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
1375void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1376{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001377 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1378 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
1381void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1382{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001383 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1384 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385}
1386
1387void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1388{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001389 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1390 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391}
1392
1393void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1394{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001395 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1396 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397}
1398
1399void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1400{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001401 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1402 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
1405void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1406{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001407 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1408 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
1411void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1412{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001413 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1414 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
1417void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1418{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001419 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1420 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421}
1422
1423void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1424{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001425 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1426 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427}
1428
1429void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1430{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001431 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1432 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433}
1434
1435void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1436{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001437 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1438 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439}
1440
1441void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1442{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001443 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1444 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445}
1446
Geoff Lange1a27752015-10-05 13:16:04 -04001447void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001449 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450}
1451
Geoff Lange1a27752015-10-05 13:16:04 -04001452void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001453{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001454 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455}
1456
Geoff Lange1a27752015-10-05 13:16:04 -04001457void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460}
1461
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001462void Program::flagForDeletion()
1463{
1464 mDeleteStatus = true;
1465}
1466
1467bool Program::isFlaggedForDeletion() const
1468{
1469 return mDeleteStatus;
1470}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001471
Brandon Jones43a53e22014-08-28 16:23:22 -07001472void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001473{
1474 mInfoLog.reset();
1475
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001477 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001478 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001479 }
1480 else
1481 {
Jamie Madillf6113162015-05-07 11:49:21 -04001482 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001483 }
1484}
1485
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1487{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001488 // Skip cache if we're using an infolog, so we get the full error.
1489 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1490 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1491 {
1492 return mCachedValidateSamplersResult.value();
1493 }
1494
1495 if (mTextureUnitTypesCache.empty())
1496 {
1497 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1498 }
1499 else
1500 {
1501 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1502 }
1503
1504 // if any two active samplers in a program are of different types, but refer to the same
1505 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1506 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001507 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001508 {
Jamie Madille7d84322017-01-10 18:21:59 -05001509 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001510
Jamie Madille7d84322017-01-10 18:21:59 -05001511 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001512 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001513 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1514 {
1515 if (infoLog)
1516 {
1517 (*infoLog) << "Sampler uniform (" << textureUnit
1518 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1519 << caps.maxCombinedTextureImageUnits << ")";
1520 }
1521
1522 mCachedValidateSamplersResult = false;
1523 return false;
1524 }
1525
1526 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1527 {
1528 if (textureType != mTextureUnitTypesCache[textureUnit])
1529 {
1530 if (infoLog)
1531 {
1532 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1533 "image unit ("
1534 << textureUnit << ").";
1535 }
1536
1537 mCachedValidateSamplersResult = false;
1538 return false;
1539 }
1540 }
1541 else
1542 {
1543 mTextureUnitTypesCache[textureUnit] = textureType;
1544 }
1545 }
1546 }
1547
1548 mCachedValidateSamplersResult = true;
1549 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001550}
1551
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001552bool Program::isValidated() const
1553{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001554 return mValidated;
1555}
1556
Geoff Lange1a27752015-10-05 13:16:04 -04001557GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001558{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001559 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560}
1561
1562void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1563{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001564 ASSERT(
1565 uniformBlockIndex <
1566 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567
Jamie Madill48ef11b2016-04-27 15:21:52 -04001568 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001569
1570 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001571 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001572 std::string string = uniformBlock.name;
1573
Jamie Madill62d31cb2015-09-11 13:25:51 -04001574 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001575 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001576 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001577 }
jchen10fd7c3b52017-03-21 15:36:03 +08001578 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001579 }
1580}
1581
Geoff Lange1a27752015-10-05 13:16:04 -04001582GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001583{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584 int maxLength = 0;
1585
1586 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001587 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001588 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1590 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001591 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001592 if (!uniformBlock.name.empty())
1593 {
jchen10af713a22017-04-19 09:10:56 +08001594 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1595 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001596 }
1597 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001598 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001599
1600 return maxLength;
1601}
1602
Geoff Lange1a27752015-10-05 13:16:04 -04001603GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001605 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001606 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607
Jamie Madill48ef11b2016-04-27 15:21:52 -04001608 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001609 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1610 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001611 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001612 if (uniformBlock.name == baseName)
1613 {
1614 const bool arrayElementZero =
1615 (subscript == GL_INVALID_INDEX &&
1616 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1617 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1618 {
1619 return blockIndex;
1620 }
1621 }
1622 }
1623
1624 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001625}
1626
Jamie Madill62d31cb2015-09-11 13:25:51 -04001627const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001628{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001629 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1630 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001631}
1632
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001633void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1634{
jchen107a20b972017-06-13 14:25:26 +08001635 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001636 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001637 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001638}
1639
1640GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1641{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001642 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001643}
1644
Geoff Lang48dcae72014-02-05 16:28:24 -05001645void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1646{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001647 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001648 for (GLsizei i = 0; i < count; i++)
1649 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001650 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001651 }
1652
Jamie Madill48ef11b2016-04-27 15:21:52 -04001653 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001654}
1655
1656void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1657{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001659 {
jchen10a9042d32017-03-17 08:50:45 +08001660 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1661 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1662 std::string varName = var.nameWithArrayIndex();
1663 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001664 if (length)
1665 {
1666 *length = lastNameIdx;
1667 }
1668 if (size)
1669 {
jchen10a9042d32017-03-17 08:50:45 +08001670 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001671 }
1672 if (type)
1673 {
jchen10a9042d32017-03-17 08:50:45 +08001674 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001675 }
1676 if (name)
1677 {
jchen10a9042d32017-03-17 08:50:45 +08001678 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001679 name[lastNameIdx] = '\0';
1680 }
1681 }
1682}
1683
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001684GLsizei Program::getTransformFeedbackVaryingCount() const
1685{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001687 {
jchen10a9042d32017-03-17 08:50:45 +08001688 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001689 }
1690 else
1691 {
1692 return 0;
1693 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001694}
1695
1696GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1697{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001699 {
1700 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001701 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001702 {
jchen10a9042d32017-03-17 08:50:45 +08001703 maxSize =
1704 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001705 }
1706
1707 return maxSize;
1708 }
1709 else
1710 {
1711 return 0;
1712 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001713}
1714
1715GLenum Program::getTransformFeedbackBufferMode() const
1716{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001717 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718}
1719
Jamie Madillbd044ed2017-06-05 12:59:21 -04001720bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001721{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001722 Shader *vertexShader = mState.mAttachedVertexShader;
1723 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001724
Jamie Madillbd044ed2017-06-05 12:59:21 -04001725 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001726
Jamie Madillbd044ed2017-06-05 12:59:21 -04001727 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1728 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001729
Sami Väisänen46eaa942016-06-29 10:26:37 +03001730 std::map<GLuint, std::string> staticFragmentInputLocations;
1731
Jamie Madill4cff2472015-08-21 16:53:18 -04001732 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001733 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001734 bool matched = false;
1735
1736 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001737 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001738 {
1739 continue;
1740 }
1741
Jamie Madill4cff2472015-08-21 16:53:18 -04001742 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001743 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001744 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001745 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001746 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001747 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001748 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001749 {
1750 return false;
1751 }
1752
Geoff Lang7dd2e102014-11-10 15:19:26 -05001753 matched = true;
1754 break;
1755 }
1756 }
1757
1758 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001759 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001760 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001761 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001762 return false;
1763 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001764
1765 // Check for aliased path rendering input bindings (if any).
1766 // If more than one binding refer statically to the same
1767 // location the link must fail.
1768
1769 if (!output.staticUse)
1770 continue;
1771
1772 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1773 if (inputBinding == -1)
1774 continue;
1775
1776 const auto it = staticFragmentInputLocations.find(inputBinding);
1777 if (it == std::end(staticFragmentInputLocations))
1778 {
1779 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1780 }
1781 else
1782 {
1783 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1784 << it->second;
1785 return false;
1786 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 }
1788
Jamie Madillbd044ed2017-06-05 12:59:21 -04001789 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001790 {
1791 return false;
1792 }
1793
Jamie Madillada9ecc2015-08-17 12:53:37 -04001794 // TODO(jmadill): verify no unmatched vertex varyings?
1795
Geoff Lang7dd2e102014-11-10 15:19:26 -05001796 return true;
1797}
1798
Jamie Madillbd044ed2017-06-05 12:59:21 -04001799bool Program::linkUniforms(const Context *context,
1800 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001801 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001802{
Olli Etuahob78707c2017-03-09 15:03:11 +00001803 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001804 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001805 {
1806 return false;
1807 }
1808
Olli Etuahob78707c2017-03-09 15:03:11 +00001809 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001810
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001811 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001812
jchen10eaef1e52017-06-13 10:44:11 +08001813 if (!linkAtomicCounterBuffers())
1814 {
1815 return false;
1816 }
1817
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001818 return true;
1819}
1820
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001821void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001822{
Jamie Madill982f6e02017-06-07 14:33:04 -04001823 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1824 unsigned int low = high;
1825
jchen10eaef1e52017-06-13 10:44:11 +08001826 for (auto counterIter = mState.mUniforms.rbegin();
1827 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1828 {
1829 --low;
1830 }
1831
1832 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1833
1834 high = low;
1835
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001836 for (auto imageIter = mState.mUniforms.rbegin();
1837 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1838 {
1839 --low;
1840 }
1841
1842 mState.mImageUniformRange = RangeUI(low, high);
1843
1844 // If uniform is a image type, insert it into the mImageBindings array.
1845 for (unsigned int imageIndex : mState.mImageUniformRange)
1846 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001847 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1848 // cannot load values into a uniform defined as an image. if declare without a
1849 // binding qualifier, any uniform image variable (include all elements of
1850 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001851 auto &imageUniform = mState.mUniforms[imageIndex];
1852 if (imageUniform.binding == -1)
1853 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001854 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001855 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001856 else
1857 {
1858 mState.mImageBindings.emplace_back(
1859 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1860 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001861 }
1862
1863 high = low;
1864
1865 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001866 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001867 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001868 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001869 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001870
1871 mState.mSamplerUniformRange = RangeUI(low, high);
1872
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001873 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001874 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001875 {
1876 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1877 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1878 mState.mSamplerBindings.emplace_back(
1879 SamplerBinding(textureType, samplerUniform.elementCount()));
1880 }
1881}
1882
jchen10eaef1e52017-06-13 10:44:11 +08001883bool Program::linkAtomicCounterBuffers()
1884{
1885 for (unsigned int index : mState.mAtomicCounterUniformRange)
1886 {
1887 auto &uniform = mState.mUniforms[index];
1888 bool found = false;
1889 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1890 ++bufferIndex)
1891 {
1892 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1893 if (buffer.binding == uniform.binding)
1894 {
1895 buffer.memberIndexes.push_back(index);
1896 uniform.bufferIndex = bufferIndex;
1897 found = true;
1898 break;
1899 }
1900 }
1901 if (!found)
1902 {
1903 AtomicCounterBuffer atomicCounterBuffer;
1904 atomicCounterBuffer.binding = uniform.binding;
1905 atomicCounterBuffer.memberIndexes.push_back(index);
1906 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1907 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1908 }
1909 }
1910 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1911 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1912
1913 return true;
1914}
1915
Martin Radev4c4c8e72016-08-04 12:25:34 +03001916bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1917 const std::string &uniformName,
1918 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001919 const sh::InterfaceBlockField &fragmentUniform,
1920 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001921{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001922 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1923 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1924 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001925 {
1926 return false;
1927 }
1928
1929 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1930 {
Jamie Madillf6113162015-05-07 11:49:21 -04001931 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932 return false;
1933 }
1934
1935 return true;
1936}
1937
Jamie Madilleb979bf2016-11-15 12:28:46 -05001938// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001939bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001940{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001941 const ContextState &data = context->getContextState();
1942 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001943
Geoff Lang7dd2e102014-11-10 15:19:26 -05001944 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001945 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001946 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001947
1948 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001949 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001950 {
Jamie Madillf6113162015-05-07 11:49:21 -04001951 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001952 return false;
1953 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001955 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001956
Jamie Madillc349ec02015-08-21 16:53:12 -04001957 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001958 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001959 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001960 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001961 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001962 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001963 attribute.location = bindingLocation;
1964 }
1965
1966 if (attribute.location != -1)
1967 {
1968 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001969 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970
Jamie Madill63805b42015-08-25 13:17:39 -04001971 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972 {
Jamie Madillf6113162015-05-07 11:49:21 -04001973 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001974 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975
1976 return false;
1977 }
1978
Jamie Madill63805b42015-08-25 13:17:39 -04001979 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980 {
Jamie Madill63805b42015-08-25 13:17:39 -04001981 const int regLocation = attribute.location + reg;
1982 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001983
1984 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001985 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001986 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001988 // TODO(jmadill): fix aliasing on ES2
1989 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001991 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001992 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993 return false;
1994 }
1995 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001996 else
1997 {
Jamie Madill63805b42015-08-25 13:17:39 -04001998 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001999 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000
Jamie Madill63805b42015-08-25 13:17:39 -04002001 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002 }
2003 }
2004 }
2005
2006 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002007 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002009 // Not set by glBindAttribLocation or by location layout qualifier
2010 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011 {
Jamie Madill63805b42015-08-25 13:17:39 -04002012 int regs = VariableRegisterCount(attribute.type);
2013 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014
Jamie Madill63805b42015-08-25 13:17:39 -04002015 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002016 {
Jamie Madillf6113162015-05-07 11:49:21 -04002017 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002018 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 }
2020
Jamie Madillc349ec02015-08-21 16:53:12 -04002021 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022 }
2023 }
2024
Jamie Madill48ef11b2016-04-27 15:21:52 -04002025 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 {
Jamie Madill63805b42015-08-25 13:17:39 -04002027 ASSERT(attribute.location != -1);
2028 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002029
Jamie Madill63805b42015-08-25 13:17:39 -04002030 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002032 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002033 }
2034 }
2035
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 return true;
2037}
2038
Martin Radev4c4c8e72016-08-04 12:25:34 +03002039bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2040 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2041 const std::string &errorMessage,
2042 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002044 GLuint blockCount = 0;
2045 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002047 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002048 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002049 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002050 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002051 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002052 return false;
2053 }
2054 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002055 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002056 return true;
2057}
Jamie Madille473dee2015-08-18 14:49:01 -04002058
Martin Radev4c4c8e72016-08-04 12:25:34 +03002059bool Program::validateVertexAndFragmentInterfaceBlocks(
2060 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2061 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002062 InfoLog &infoLog,
2063 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002064{
2065 // Check that interface blocks defined in the vertex and fragment shaders are identical
2066 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2067 UniformBlockMap linkedUniformBlocks;
2068
2069 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2070 {
2071 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2072 }
2073
Jamie Madille473dee2015-08-18 14:49:01 -04002074 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075 {
Jamie Madille473dee2015-08-18 14:49:01 -04002076 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077 if (entry != linkedUniformBlocks.end())
2078 {
2079 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002080 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2081 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082 {
2083 return false;
2084 }
2085 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002086 }
2087 return true;
2088}
Jamie Madille473dee2015-08-18 14:49:01 -04002089
Jamie Madillbd044ed2017-06-05 12:59:21 -04002090bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002091{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002092 const auto &caps = context->getCaps();
2093
Martin Radev4c4c8e72016-08-04 12:25:34 +03002094 if (mState.mAttachedComputeShader)
2095 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002096 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002097 const auto &computeInterfaceBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002098
2099 if (!validateUniformBlocksCount(
2100 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2101 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2102 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002104 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002106 return true;
2107 }
2108
Jamie Madillbd044ed2017-06-05 12:59:21 -04002109 Shader &vertexShader = *mState.mAttachedVertexShader;
2110 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002111
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002112 const auto &vertexInterfaceBlocks = vertexShader.getUniformBlocks(context);
2113 const auto &fragmentInterfaceBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002114
2115 if (!validateUniformBlocksCount(
2116 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2117 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2118 {
2119 return false;
2120 }
2121 if (!validateUniformBlocksCount(
2122 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2123 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2124 infoLog))
2125 {
2126
2127 return false;
2128 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002129
2130 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002131 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002132 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002133 {
2134 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 }
Jamie Madille473dee2015-08-18 14:49:01 -04002136
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 return true;
2138}
2139
Jamie Madilla2c74982016-12-12 11:20:42 -05002140bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002141 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002142 const sh::InterfaceBlock &fragmentInterfaceBlock,
2143 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144{
2145 const char* blockName = vertexInterfaceBlock.name.c_str();
2146 // validate blocks for the same member types
2147 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2148 {
Jamie Madillf6113162015-05-07 11:49:21 -04002149 infoLog << "Types for interface block '" << blockName
2150 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 return false;
2152 }
2153 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2154 {
Jamie Madillf6113162015-05-07 11:49:21 -04002155 infoLog << "Array sizes differ for interface block '" << blockName
2156 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 return false;
2158 }
jchen10af713a22017-04-19 09:10:56 +08002159 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2160 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2161 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162 {
Jamie Madillf6113162015-05-07 11:49:21 -04002163 infoLog << "Layout qualifiers differ for interface block '" << blockName
2164 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002165 return false;
2166 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002167 const unsigned int numBlockMembers =
2168 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2170 {
2171 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2172 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2173 if (vertexMember.name != fragmentMember.name)
2174 {
Jamie Madillf6113162015-05-07 11:49:21 -04002175 infoLog << "Name mismatch for field " << blockMemberIndex
2176 << " of interface block '" << blockName
2177 << "': (in vertex: '" << vertexMember.name
2178 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 return false;
2180 }
2181 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002182 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2183 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002184 {
2185 return false;
2186 }
2187 }
2188 return true;
2189}
2190
2191bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2192 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2193{
2194 if (vertexVariable.type != fragmentVariable.type)
2195 {
Jamie Madillf6113162015-05-07 11:49:21 -04002196 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197 return false;
2198 }
2199 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2200 {
Jamie Madillf6113162015-05-07 11:49:21 -04002201 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202 return false;
2203 }
2204 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2205 {
Jamie Madillf6113162015-05-07 11:49:21 -04002206 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207 return false;
2208 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002209 if (vertexVariable.structName != fragmentVariable.structName)
2210 {
2211 infoLog << "Structure names for " << variableName
2212 << " differ between vertex and fragment shaders";
2213 return false;
2214 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002215
2216 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2217 {
Jamie Madillf6113162015-05-07 11:49:21 -04002218 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002219 return false;
2220 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002221 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002222 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2223 {
2224 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2225 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2226
2227 if (vertexMember.name != fragmentMember.name)
2228 {
Jamie Madillf6113162015-05-07 11:49:21 -04002229 infoLog << "Name mismatch for field '" << memberIndex
2230 << "' of " << variableName
2231 << ": (in vertex: '" << vertexMember.name
2232 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233 return false;
2234 }
2235
2236 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2237 vertexMember.name + "'";
2238
2239 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2240 {
2241 return false;
2242 }
2243 }
2244
2245 return true;
2246}
2247
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002248bool Program::linkValidateVaryings(InfoLog &infoLog,
2249 const std::string &varyingName,
2250 const sh::Varying &vertexVarying,
2251 const sh::Varying &fragmentVarying,
2252 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002253{
2254 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2255 {
2256 return false;
2257 }
2258
Jamie Madille9cc4692015-02-19 16:00:13 -05002259 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002261 infoLog << "Interpolation types for " << varyingName
2262 << " differ between vertex and fragment shaders.";
2263 return false;
2264 }
2265
2266 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2267 {
2268 infoLog << "Invariance for " << varyingName
2269 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002270 return false;
2271 }
2272
2273 return true;
2274}
2275
Jamie Madillbd044ed2017-06-05 12:59:21 -04002276bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002277{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002278 Shader *vertexShader = mState.mAttachedVertexShader;
2279 Shader *fragmentShader = mState.mAttachedFragmentShader;
2280 const auto &vertexVaryings = vertexShader->getVaryings(context);
2281 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2282 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002283
2284 if (shaderVersion != 100)
2285 {
2286 // Only ESSL 1.0 has restrictions on matching input and output invariance
2287 return true;
2288 }
2289
2290 bool glPositionIsInvariant = false;
2291 bool glPointSizeIsInvariant = false;
2292 bool glFragCoordIsInvariant = false;
2293 bool glPointCoordIsInvariant = false;
2294
2295 for (const sh::Varying &varying : vertexVaryings)
2296 {
2297 if (!varying.isBuiltIn())
2298 {
2299 continue;
2300 }
2301 if (varying.name.compare("gl_Position") == 0)
2302 {
2303 glPositionIsInvariant = varying.isInvariant;
2304 }
2305 else if (varying.name.compare("gl_PointSize") == 0)
2306 {
2307 glPointSizeIsInvariant = varying.isInvariant;
2308 }
2309 }
2310
2311 for (const sh::Varying &varying : fragmentVaryings)
2312 {
2313 if (!varying.isBuiltIn())
2314 {
2315 continue;
2316 }
2317 if (varying.name.compare("gl_FragCoord") == 0)
2318 {
2319 glFragCoordIsInvariant = varying.isInvariant;
2320 }
2321 else if (varying.name.compare("gl_PointCoord") == 0)
2322 {
2323 glPointCoordIsInvariant = varying.isInvariant;
2324 }
2325 }
2326
2327 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2328 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2329 // Not requiring invariance to match is supported by:
2330 // dEQP, WebGL CTS, Nexus 5X GLES
2331 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2332 {
2333 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2334 "declared invariant.";
2335 return false;
2336 }
2337 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2338 {
2339 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2340 "declared invariant.";
2341 return false;
2342 }
2343
2344 return true;
2345}
2346
jchen10a9042d32017-03-17 08:50:45 +08002347bool Program::linkValidateTransformFeedback(const gl::Context *context,
2348 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002349 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002350 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351{
2352 size_t totalComponents = 0;
2353
Jamie Madillccdf74b2015-08-18 10:46:12 -04002354 std::set<std::string> uniqueNames;
2355
Jamie Madill48ef11b2016-04-27 15:21:52 -04002356 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002357 {
2358 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002359 size_t subscript = GL_INVALID_INDEX;
2360 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2361
Jamie Madill192745a2016-12-22 15:58:21 -05002362 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363 {
Jamie Madill192745a2016-12-22 15:58:21 -05002364 const sh::Varying *varying = ref.second.get();
2365
jchen10a9042d32017-03-17 08:50:45 +08002366 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002367 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002368 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002370 infoLog << "Two transform feedback varyings specify the same output variable ("
2371 << tfVaryingName << ").";
2372 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373 }
jchen10a9042d32017-03-17 08:50:45 +08002374 if (context->getClientVersion() >= Version(3, 1))
2375 {
2376 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2377 {
2378 infoLog
2379 << "Two transform feedback varyings include the same array element ("
2380 << tfVaryingName << ").";
2381 return false;
2382 }
2383 }
2384 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002385 {
2386 infoLog << "Capture of arrays is undefined and not supported.";
2387 return false;
2388 }
2389
jchen10a9042d32017-03-17 08:50:45 +08002390 uniqueNames.insert(tfVaryingName);
2391
Jamie Madillccdf74b2015-08-18 10:46:12 -04002392 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002393 size_t elementCount =
2394 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2395 : 1);
2396 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002397 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398 componentCount > caps.maxTransformFeedbackSeparateComponents)
2399 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002400 infoLog << "Transform feedback varying's " << varying->name << " components ("
2401 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002402 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403 return false;
2404 }
2405
2406 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002407 found = true;
2408 break;
2409 }
2410 }
jchen10a9042d32017-03-17 08:50:45 +08002411 if (context->getClientVersion() < Version(3, 1) &&
2412 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002413 {
Geoff Lang1a683462015-09-29 15:09:59 -04002414 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002415 return false;
2416 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002417 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2418 ASSERT(found);
2419 }
2420
Jamie Madill48ef11b2016-04-27 15:21:52 -04002421 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002422 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002423 {
Jamie Madillf6113162015-05-07 11:49:21 -04002424 infoLog << "Transform feedback varying total components (" << totalComponents
2425 << ") exceed the maximum interleaved components ("
2426 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002427 return false;
2428 }
2429
2430 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002431}
2432
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002433bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2434{
2435 const std::vector<sh::Uniform> &vertexUniforms =
2436 mState.mAttachedVertexShader->getUniforms(context);
2437 const std::vector<sh::Uniform> &fragmentUniforms =
2438 mState.mAttachedFragmentShader->getUniforms(context);
2439 const std::vector<sh::Attribute> &attributes =
2440 mState.mAttachedVertexShader->getActiveAttributes(context);
2441 for (const auto &attrib : attributes)
2442 {
2443 for (const auto &uniform : vertexUniforms)
2444 {
2445 if (uniform.name == attrib.name)
2446 {
2447 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2448 return false;
2449 }
2450 }
2451 for (const auto &uniform : fragmentUniforms)
2452 {
2453 if (uniform.name == attrib.name)
2454 {
2455 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2456 return false;
2457 }
2458 }
2459 }
2460 return true;
2461}
2462
Jamie Madill192745a2016-12-22 15:58:21 -05002463void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464{
2465 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002466 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002467 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002468 {
jchen10a9042d32017-03-17 08:50:45 +08002469 size_t subscript = GL_INVALID_INDEX;
2470 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002471 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002472 {
Jamie Madill192745a2016-12-22 15:58:21 -05002473 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002474 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002475 {
jchen10a9042d32017-03-17 08:50:45 +08002476 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2477 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002478 break;
2479 }
2480 }
2481 }
2482}
2483
Jamie Madillbd044ed2017-06-05 12:59:21 -04002484Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002485{
Jamie Madill192745a2016-12-22 15:58:21 -05002486 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002487
Jamie Madillbd044ed2017-06-05 12:59:21 -04002488 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002489 {
Jamie Madill192745a2016-12-22 15:58:21 -05002490 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002491 }
2492
Jamie Madillbd044ed2017-06-05 12:59:21 -04002493 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002494 {
Jamie Madill192745a2016-12-22 15:58:21 -05002495 merged[varying.name].fragment = &varying;
2496 }
2497
2498 return merged;
2499}
2500
2501std::vector<PackedVarying> Program::getPackedVaryings(
2502 const Program::MergedVaryings &mergedVaryings) const
2503{
2504 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2505 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002506 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002507
2508 for (const auto &ref : mergedVaryings)
2509 {
2510 const sh::Varying *input = ref.second.vertex;
2511 const sh::Varying *output = ref.second.fragment;
2512
2513 // Only pack varyings that have a matched input or output, plus special builtins.
2514 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002515 {
Jamie Madill192745a2016-12-22 15:58:21 -05002516 // Will get the vertex shader interpolation by default.
2517 auto interpolation = ref.second.get()->interpolation;
2518
Olli Etuaho06a06f52017-07-12 12:22:15 +03002519 // Note that we lose the vertex shader static use information here. The data for the
2520 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002521 if (output->isStruct())
2522 {
2523 ASSERT(!output->isArray());
2524 for (const auto &field : output->fields)
2525 {
2526 ASSERT(!field.isStruct() && !field.isArray());
2527 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2528 }
2529 }
2530 else
2531 {
2532 packedVaryings.push_back(PackedVarying(*output, interpolation));
2533 }
2534 continue;
2535 }
2536
2537 // Keep Transform FB varyings in the merged list always.
2538 if (!input)
2539 {
2540 continue;
2541 }
2542
2543 for (const std::string &tfVarying : tfVaryings)
2544 {
jchen10a9042d32017-03-17 08:50:45 +08002545 size_t subscript = GL_INVALID_INDEX;
2546 std::string baseName = ParseResourceName(tfVarying, &subscript);
2547 if (uniqueFullNames.count(tfVarying) > 0)
2548 {
2549 continue;
2550 }
2551 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002552 {
2553 // Transform feedback for varying structs is underspecified.
2554 // See Khronos bug 9856.
2555 // TODO(jmadill): Figure out how to be spec-compliant here.
2556 if (!input->isStruct())
2557 {
2558 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2559 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002560 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2561 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002562 }
jchen10a9042d32017-03-17 08:50:45 +08002563 if (subscript == GL_INVALID_INDEX)
2564 {
2565 break;
2566 }
Jamie Madill192745a2016-12-22 15:58:21 -05002567 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002568 }
2569 }
2570
Jamie Madill192745a2016-12-22 15:58:21 -05002571 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2572
2573 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002574}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002575
Jamie Madillbd044ed2017-06-05 12:59:21 -04002576void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002577{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002578 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002579 ASSERT(fragmentShader != nullptr);
2580
Geoff Lange0cff192017-05-30 13:04:56 -04002581 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002582 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002583
2584 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002585 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002586 {
2587 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2588 outputVariable.name != "gl_FragData")
2589 {
2590 continue;
2591 }
2592
2593 unsigned int baseLocation =
2594 (outputVariable.location == -1 ? 0u
2595 : static_cast<unsigned int>(outputVariable.location));
2596 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2597 elementIndex++)
2598 {
2599 const unsigned int location = baseLocation + elementIndex;
2600 if (location >= mState.mOutputVariableTypes.size())
2601 {
2602 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2603 }
Corentin Walleze7557742017-06-01 13:09:57 -04002604 ASSERT(location < mState.mActiveOutputVariables.size());
2605 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002606 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2607 }
2608 }
2609
Jamie Madill80a6fc02015-08-21 16:53:16 -04002610 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002611 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002612 return;
2613
Jamie Madillbd044ed2017-06-05 12:59:21 -04002614 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002615 // TODO(jmadill): any caps validation here?
2616
jchen1015015f72017-03-16 13:54:21 +08002617 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002618 outputVariableIndex++)
2619 {
jchen1015015f72017-03-16 13:54:21 +08002620 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002621
2622 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2623 if (outputVariable.isBuiltIn())
2624 continue;
2625
2626 // Since multiple output locations must be specified, use 0 for non-specified locations.
2627 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2628
Jamie Madill80a6fc02015-08-21 16:53:16 -04002629 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2630 elementIndex++)
2631 {
2632 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002633 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002634 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002635 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002636 VariableLocation(outputVariable.name, element, outputVariableIndex);
2637 }
2638 }
2639}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002640
Olli Etuaho48fed632017-03-16 12:05:30 +00002641void Program::setUniformValuesFromBindingQualifiers()
2642{
Jamie Madill982f6e02017-06-07 14:33:04 -04002643 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002644 {
2645 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2646 if (samplerUniform.binding != -1)
2647 {
2648 GLint location = mState.getUniformLocation(samplerUniform.name);
2649 ASSERT(location != -1);
2650 std::vector<GLint> boundTextureUnits;
2651 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2652 ++elementIndex)
2653 {
2654 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2655 }
2656 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2657 boundTextureUnits.data());
2658 }
2659 }
2660}
2661
jchen10eaef1e52017-06-13 10:44:11 +08002662void Program::gatherAtomicCounterBuffers()
2663{
2664 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2665 // counter.
2666 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2667}
2668
Jamie Madillbd044ed2017-06-05 12:59:21 -04002669void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002670{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002671 ASSERT(mState.mUniformBlocks.empty());
2672
2673 if (mState.mAttachedComputeShader)
2674 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002675 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002676
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002677 for (const sh::InterfaceBlock &computeBlock : computeShader->getUniformBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002678 {
2679
2680 // Only 'packed' blocks are allowed to be considered inactive.
2681 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2682 continue;
2683
Jamie Madilla2c74982016-12-12 11:20:42 -05002684 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002685 {
2686 if (block.name == computeBlock.name)
2687 {
2688 block.computeStaticUse = computeBlock.staticUse;
2689 }
2690 }
2691
2692 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2693 }
2694 return;
2695 }
2696
Jamie Madill62d31cb2015-09-11 13:25:51 -04002697 std::set<std::string> visitedList;
2698
Jamie Madillbd044ed2017-06-05 12:59:21 -04002699 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002700
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002701 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002702 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002703 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002704 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2705 continue;
2706
2707 if (visitedList.count(vertexBlock.name) > 0)
2708 continue;
2709
2710 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2711 visitedList.insert(vertexBlock.name);
2712 }
2713
Jamie Madillbd044ed2017-06-05 12:59:21 -04002714 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002715
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002716 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002717 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002718 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002719 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2720 continue;
2721
2722 if (visitedList.count(fragmentBlock.name) > 0)
2723 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002724 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002725 {
2726 if (block.name == fragmentBlock.name)
2727 {
2728 block.fragmentStaticUse = fragmentBlock.staticUse;
2729 }
2730 }
2731
2732 continue;
2733 }
2734
2735 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2736 visitedList.insert(fragmentBlock.name);
2737 }
jchen10af713a22017-04-19 09:10:56 +08002738 // Set initial bindings from shader.
2739 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2740 {
2741 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2742 bindUniformBlock(blockIndex, uniformBlock.binding);
2743 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002744}
2745
Jamie Madill4a3c2342015-10-08 12:58:45 -04002746template <typename VarT>
2747void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2748 const std::string &prefix,
2749 int blockIndex)
2750{
2751 for (const VarT &field : fields)
2752 {
2753 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2754
2755 if (field.isStruct())
2756 {
2757 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2758 {
2759 const std::string uniformElementName =
2760 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2761 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2762 }
2763 }
2764 else
2765 {
2766 // If getBlockMemberInfo returns false, the uniform is optimized out.
2767 sh::BlockMemberInfo memberInfo;
2768 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2769 {
2770 continue;
2771 }
2772
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002773 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002774 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002775
2776 // Since block uniforms have no location, we don't need to store them in the uniform
2777 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002778 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002779 }
2780 }
2781}
2782
Jamie Madill62d31cb2015-09-11 13:25:51 -04002783void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2784{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002785 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002786 size_t blockSize = 0;
2787
Jamie Madill4a3c2342015-10-08 12:58:45 -04002788 // Track the first and last uniform index to determine the range of active uniforms in the
2789 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002790 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002791 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002792 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002793
2794 std::vector<unsigned int> blockUniformIndexes;
2795 for (size_t blockUniformIndex = firstBlockUniformIndex;
2796 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2797 {
2798 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2799 }
jchen10af713a22017-04-19 09:10:56 +08002800 // ESSL 3.10 section 4.4.4 page 58:
2801 // Any uniform or shader storage block declared without a binding qualifier is initially
2802 // assigned to block binding point zero.
2803 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002804 if (interfaceBlock.arraySize > 0)
2805 {
2806 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2807 {
jchen10af713a22017-04-19 09:10:56 +08002808 // Don't define this block at all if it's not active in the implementation.
2809 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2810 &blockSize))
2811 {
2812 continue;
2813 }
2814 UniformBlock block(interfaceBlock.name, true, arrayElement,
2815 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002816 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817
Martin Radev4c4c8e72016-08-04 12:25:34 +03002818 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002820 case GL_VERTEX_SHADER:
2821 {
2822 block.vertexStaticUse = interfaceBlock.staticUse;
2823 break;
2824 }
2825 case GL_FRAGMENT_SHADER:
2826 {
2827 block.fragmentStaticUse = interfaceBlock.staticUse;
2828 break;
2829 }
2830 case GL_COMPUTE_SHADER:
2831 {
2832 block.computeStaticUse = interfaceBlock.staticUse;
2833 break;
2834 }
2835 default:
2836 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002837 }
2838
Qin Jiajia0350a642016-11-01 17:01:51 +08002839 // Since all block elements in an array share the same active uniforms, they will all be
2840 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2841 // here we will add every block element in the array.
2842 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002843 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002844 }
2845 }
2846 else
2847 {
jchen10af713a22017-04-19 09:10:56 +08002848 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2849 {
2850 return;
2851 }
2852 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002853 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002854
Martin Radev4c4c8e72016-08-04 12:25:34 +03002855 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002857 case GL_VERTEX_SHADER:
2858 {
2859 block.vertexStaticUse = interfaceBlock.staticUse;
2860 break;
2861 }
2862 case GL_FRAGMENT_SHADER:
2863 {
2864 block.fragmentStaticUse = interfaceBlock.staticUse;
2865 break;
2866 }
2867 case GL_COMPUTE_SHADER:
2868 {
2869 block.computeStaticUse = interfaceBlock.staticUse;
2870 break;
2871 }
2872 default:
2873 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002874 }
2875
Jamie Madill4a3c2342015-10-08 12:58:45 -04002876 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002877 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002878 }
2879}
2880
Jamie Madille7d84322017-01-10 18:21:59 -05002881template <>
2882void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2883 const uint8_t *destPointer,
2884 GLsizei clampedCount,
2885 const GLint *v)
2886{
2887 // Invalidate the validation cache only if we modify the sampler data.
2888 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2889 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2890 {
2891 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2892 std::vector<GLuint> *boundTextureUnits =
2893 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2894
2895 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2896 mCachedValidateSamplersResult.reset();
2897 }
2898}
2899
2900template <typename T>
2901void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2902 const uint8_t *destPointer,
2903 GLsizei clampedCount,
2904 const T *v)
2905{
2906}
2907
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002909GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002910{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002911 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2912 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002913 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2914
Corentin Wallez15ac5342016-11-03 17:06:39 -04002915 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2916 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2917 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002918 GLsizei maxElementCount =
2919 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2920
2921 GLsizei count = countIn;
2922 GLsizei clampedCount = count * vectorSize;
2923 if (clampedCount > maxElementCount)
2924 {
2925 clampedCount = maxElementCount;
2926 count = maxElementCount / vectorSize;
2927 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002928
Jamie Madill44183cc2017-08-01 12:48:34 -04002929 // VariableComponentType(linkedUniform->type) has a dozens of compares and thus is evil for
2930 // inlining with regards to code size. This version is one subtract and one compare only.
2931 if (IsVariableComponentTypeBool(linkedUniform->type))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002932 {
2933 // Do a cast conversion for boolean types. From the spec:
2934 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2935 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002936 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002937 {
2938 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2939 }
2940 }
2941 else
2942 {
Jamie Madille7d84322017-01-10 18:21:59 -05002943 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002944 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002945 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002946
2947 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002948}
2949
2950template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002951GLsizei Program::setMatrixUniformInternal(GLint location,
2952 GLsizei count,
2953 GLboolean transpose,
2954 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002955{
2956 if (!transpose)
2957 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002958 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002959 }
2960
2961 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002962 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2963 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002964 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002965
2966 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2967 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2968 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2969 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2970
2971 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002972 {
2973 size_t elementOffset = element * rows * cols;
2974
2975 for (size_t row = 0; row < rows; ++row)
2976 {
2977 for (size_t col = 0; col < cols; ++col)
2978 {
2979 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2980 }
2981 }
2982 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002983
2984 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002985}
2986
2987template <typename DestT>
2988void Program::getUniformInternal(GLint location, DestT *dataOut) const
2989{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002990 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2991 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002992
2993 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2994
2995 GLenum componentType = VariableComponentType(uniform.type);
2996 if (componentType == GLTypeToGLenum<DestT>::value)
2997 {
2998 memcpy(dataOut, srcPointer, uniform.getElementSize());
2999 return;
3000 }
3001
Corentin Wallez6596c462016-03-17 17:26:58 -04003002 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003003
3004 switch (componentType)
3005 {
3006 case GL_INT:
3007 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3008 break;
3009 case GL_UNSIGNED_INT:
3010 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3011 break;
3012 case GL_BOOL:
3013 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3014 break;
3015 case GL_FLOAT:
3016 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3017 break;
3018 default:
3019 UNREACHABLE();
3020 }
3021}
Jamie Madilla4595b82017-01-11 17:36:34 -05003022
3023bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3024{
3025 // Must be called after samplers are validated.
3026 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3027
3028 for (const auto &binding : mState.mSamplerBindings)
3029 {
3030 GLenum textureType = binding.textureType;
3031 for (const auto &unit : binding.boundTextureUnits)
3032 {
3033 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3034 if (programTextureID == textureID)
3035 {
3036 // TODO(jmadill): Check for appropriate overlap.
3037 return true;
3038 }
3039 }
3040 }
3041
3042 return false;
3043}
3044
Jamie Madilla2c74982016-12-12 11:20:42 -05003045} // namespace gl