blob: fc39b40bfc23887157358abd2613980c98ef6e84 [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
Martin Radev7cf61662017-07-26 17:10:53 +0300754 mState.mNumViews = vertexShader->getNumViews(context);
755
Jamie Madillbd044ed2017-06-05 12:59:21 -0400756 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300757
Jamie Madill192745a2016-12-22 15:58:21 -0500758 // Validate we can pack the varyings.
759 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
760
761 // Map the varyings to the register file
762 // In WebGL, we use a slightly different handling for packing variables.
763 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
764 : PackMode::ANGLE_RELAXED;
765 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
766 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
767 mState.getTransformFeedbackVaryingNames()))
768 {
769 return NoError();
770 }
771
Olli Etuaho39e78122017-08-29 14:34:22 +0300772 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
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 Madill54164b02017-08-28 15:17:37 -0400791 // Mark implementation-specific unreferenced uniforms as ignored.
792 mProgram->markUnusedUniformLocations(&mState.mUniformLocations);
793
794 // Update sampler bindings with unreferenced uniforms.
795 for (const auto &location : mState.mUniformLocations)
796 {
797 if (!location.used && mState.isSamplerUniformIndex(location.index))
798 {
799 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(location.index);
800 mState.mSamplerBindings[samplerIndex].unreferenced = true;
801 }
802 }
803
Jamie Madill32447362017-06-28 14:53:52 -0400804 // Save to the program cache.
805 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
806 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
807 {
808 cache->putProgram(programHash, context, this);
809 }
810
Jamie Madill6c58b062017-08-01 13:44:25 -0400811 double delta = platform->currentTime(platform) - startTime;
812 int us = static_cast<int>(delta * 1000000.0);
813 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
814
Martin Radev4c4c8e72016-08-04 12:25:34 +0300815 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000816}
817
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000818// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500819void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400821 mState.mAttributes.clear();
822 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800823 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400824 mState.mUniforms.clear();
825 mState.mUniformLocations.clear();
826 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800827 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800828 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800830 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400831 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400832 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300833 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500834 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800835 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300836 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500837
Geoff Lang7dd2e102014-11-10 15:19:26 -0500838 mValidated = false;
839
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000840 mLinked = false;
841}
842
Geoff Lange1a27752015-10-05 13:16:04 -0400843bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000844{
845 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
Jamie Madilla2c74982016-12-12 11:20:42 -0500848Error Program::loadBinary(const Context *context,
849 GLenum binaryFormat,
850 const void *binary,
851 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000852{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500853 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000854
Geoff Lang7dd2e102014-11-10 15:19:26 -0500855#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800856 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500857#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400858 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
859 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000860 {
Jamie Madillf6113162015-05-07 11:49:21 -0400861 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800862 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500863 }
864
Jamie Madill4f86d052017-06-05 12:59:26 -0400865 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
866 ANGLE_TRY_RESULT(
867 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400868
869 // Currently we require the full shader text to compute the program hash.
870 // TODO(jmadill): Store the binary in the internal program cache.
871
Jamie Madillb0a838b2016-11-13 20:02:12 -0500872 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500873#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500874}
875
Jamie Madilla2c74982016-12-12 11:20:42 -0500876Error Program::saveBinary(const Context *context,
877 GLenum *binaryFormat,
878 void *binary,
879 GLsizei bufSize,
880 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881{
882 if (binaryFormat)
883 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400884 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885 }
886
Jamie Madill4f86d052017-06-05 12:59:26 -0400887 angle::MemoryBuffer memoryBuf;
888 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500889
Jamie Madill4f86d052017-06-05 12:59:26 -0400890 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
891 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892
893 if (streamLength > bufSize)
894 {
895 if (length)
896 {
897 *length = 0;
898 }
899
900 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
901 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
902 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500903 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904 }
905
906 if (binary)
907 {
908 char *ptr = reinterpret_cast<char*>(binary);
909
Jamie Madill48ef11b2016-04-27 15:21:52 -0400910 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500911 ptr += streamLength;
912
913 ASSERT(ptr - streamLength == binary);
914 }
915
916 if (length)
917 {
918 *length = streamLength;
919 }
920
He Yunchaoacd18982017-01-04 10:46:42 +0800921 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500922}
923
Jamie Madillffe00c02017-06-27 16:26:55 -0400924GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500925{
926 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400927 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500928 if (error.isError())
929 {
930 return 0;
931 }
932
933 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000934}
935
Geoff Langc5629752015-12-07 16:29:04 -0500936void Program::setBinaryRetrievableHint(bool retrievable)
937{
938 // TODO(jmadill) : replace with dirty bits
939 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400940 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500941}
942
943bool Program::getBinaryRetrievableHint() const
944{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400945 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500946}
947
Yunchao He61afff12017-03-14 15:34:03 +0800948void Program::setSeparable(bool separable)
949{
950 // TODO(yunchao) : replace with dirty bits
951 if (mState.mSeparable != separable)
952 {
953 mProgram->setSeparable(separable);
954 mState.mSeparable = separable;
955 }
956}
957
958bool Program::isSeparable() const
959{
960 return mState.mSeparable;
961}
962
Jamie Madill6c1f6712017-02-14 19:08:04 -0500963void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964{
965 mRefCount--;
966
967 if (mRefCount == 0 && mDeleteStatus)
968 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500969 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970 }
971}
972
973void Program::addRef()
974{
975 mRefCount++;
976}
977
978unsigned int Program::getRefCount() const
979{
980 return mRefCount;
981}
982
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000983int Program::getInfoLogLength() const
984{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400985 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000986}
987
Geoff Lange1a27752015-10-05 13:16:04 -0400988void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000989{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000990 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000991}
992
Geoff Lange1a27752015-10-05 13:16:04 -0400993void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000994{
995 int total = 0;
996
Martin Radev4c4c8e72016-08-04 12:25:34 +0300997 if (mState.mAttachedComputeShader)
998 {
999 if (total < maxCount)
1000 {
1001 shaders[total] = mState.mAttachedComputeShader->getHandle();
1002 total++;
1003 }
1004 }
1005
Jamie Madill48ef11b2016-04-27 15:21:52 -04001006 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001007 {
1008 if (total < maxCount)
1009 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001010 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001011 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001012 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001013 }
1014
Jamie Madill48ef11b2016-04-27 15:21:52 -04001015 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001016 {
1017 if (total < maxCount)
1018 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001019 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001020 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001021 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001022 }
1023
1024 if (count)
1025 {
1026 *count = total;
1027 }
1028}
1029
Geoff Lange1a27752015-10-05 13:16:04 -04001030GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001031{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001032 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001033}
1034
Jamie Madill63805b42015-08-25 13:17:39 -04001035bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001036{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001037 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1038 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039}
1040
jchen10fd7c3b52017-03-21 15:36:03 +08001041void Program::getActiveAttribute(GLuint index,
1042 GLsizei bufsize,
1043 GLsizei *length,
1044 GLint *size,
1045 GLenum *type,
1046 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001047{
Jamie Madillc349ec02015-08-21 16:53:12 -04001048 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001049 {
1050 if (bufsize > 0)
1051 {
1052 name[0] = '\0';
1053 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001054
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001055 if (length)
1056 {
1057 *length = 0;
1058 }
1059
1060 *type = GL_NONE;
1061 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001062 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001063 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001064
jchen1036e120e2017-03-14 14:53:58 +08001065 ASSERT(index < mState.mAttributes.size());
1066 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001067
1068 if (bufsize > 0)
1069 {
jchen10fd7c3b52017-03-21 15:36:03 +08001070 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001071 }
1072
1073 // Always a single 'type' instance
1074 *size = 1;
1075 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001076}
1077
Geoff Lange1a27752015-10-05 13:16:04 -04001078GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001079{
Jamie Madillc349ec02015-08-21 16:53:12 -04001080 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001081 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001082 return 0;
1083 }
1084
jchen1036e120e2017-03-14 14:53:58 +08001085 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001086}
1087
Geoff Lange1a27752015-10-05 13:16:04 -04001088GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001089{
Jamie Madillc349ec02015-08-21 16:53:12 -04001090 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001091 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001092 return 0;
1093 }
1094
1095 size_t maxLength = 0;
1096
Jamie Madill48ef11b2016-04-27 15:21:52 -04001097 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001098 {
jchen1036e120e2017-03-14 14:53:58 +08001099 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001100 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001101
Jamie Madillc349ec02015-08-21 16:53:12 -04001102 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001103}
1104
jchen1015015f72017-03-16 13:54:21 +08001105GLuint Program::getInputResourceIndex(const GLchar *name) const
1106{
1107 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1108 {
1109 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1110 if (attribute.name == name)
1111 {
1112 return attributeIndex;
1113 }
1114 }
1115 return GL_INVALID_INDEX;
1116}
1117
1118GLuint Program::getOutputResourceIndex(const GLchar *name) const
1119{
1120 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1121}
1122
jchen10fd7c3b52017-03-21 15:36:03 +08001123size_t Program::getOutputResourceCount() const
1124{
1125 return (mLinked ? mState.mOutputVariables.size() : 0);
1126}
1127
1128void Program::getInputResourceName(GLuint index,
1129 GLsizei bufSize,
1130 GLsizei *length,
1131 GLchar *name) const
1132{
1133 GLint size;
1134 GLenum type;
1135 getActiveAttribute(index, bufSize, length, &size, &type, name);
1136}
1137
1138void Program::getOutputResourceName(GLuint index,
1139 GLsizei bufSize,
1140 GLsizei *length,
1141 GLchar *name) const
1142{
1143 if (length)
1144 {
1145 *length = 0;
1146 }
1147
1148 if (!mLinked)
1149 {
1150 if (bufSize > 0)
1151 {
1152 name[0] = '\0';
1153 }
1154 return;
1155 }
1156 ASSERT(index < mState.mOutputVariables.size());
1157 const auto &output = mState.mOutputVariables[index];
1158
1159 if (bufSize > 0)
1160 {
1161 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1162
1163 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1164 }
1165}
1166
jchen10880683b2017-04-12 16:21:55 +08001167const sh::Attribute &Program::getInputResource(GLuint index) const
1168{
1169 ASSERT(index < mState.mAttributes.size());
1170 return mState.mAttributes[index];
1171}
1172
1173const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1174{
1175 ASSERT(index < mState.mOutputVariables.size());
1176 return mState.mOutputVariables[index];
1177}
1178
Geoff Lang7dd2e102014-11-10 15:19:26 -05001179GLint Program::getFragDataLocation(const std::string &name) const
1180{
1181 std::string baseName(name);
1182 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001183 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001185 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1187 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001188 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001190 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001192}
1193
Geoff Lange1a27752015-10-05 13:16:04 -04001194void Program::getActiveUniform(GLuint index,
1195 GLsizei bufsize,
1196 GLsizei *length,
1197 GLint *size,
1198 GLenum *type,
1199 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001200{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001202 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001203 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001204 ASSERT(index < mState.mUniforms.size());
1205 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001206
1207 if (bufsize > 0)
1208 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001209 std::string string = uniform.name;
1210 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 {
1212 string += "[0]";
1213 }
jchen10fd7c3b52017-03-21 15:36:03 +08001214 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001215 }
1216
Jamie Madill62d31cb2015-09-11 13:25:51 -04001217 *size = uniform.elementCount();
1218 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001219 }
1220 else
1221 {
1222 if (bufsize > 0)
1223 {
1224 name[0] = '\0';
1225 }
1226
1227 if (length)
1228 {
1229 *length = 0;
1230 }
1231
1232 *size = 0;
1233 *type = GL_NONE;
1234 }
1235}
1236
Geoff Lange1a27752015-10-05 13:16:04 -04001237GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001238{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001239 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001240 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001241 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001242 }
1243 else
1244 {
1245 return 0;
1246 }
1247}
1248
Geoff Lange1a27752015-10-05 13:16:04 -04001249GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001250{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001251 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001252
1253 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001255 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001256 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001257 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001259 size_t length = uniform.name.length() + 1u;
1260 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001261 {
1262 length += 3; // Counting in "[0]".
1263 }
1264 maxLength = std::max(length, maxLength);
1265 }
1266 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001267 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001268
Jamie Madill62d31cb2015-09-11 13:25:51 -04001269 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270}
1271
1272GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1273{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001274 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001275 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001276 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001277 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1279 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1280 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001281 case GL_UNIFORM_BLOCK_INDEX:
1282 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1284 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1285 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1286 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1287 default:
1288 UNREACHABLE();
1289 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001290 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001291 return 0;
1292}
1293
1294bool Program::isValidUniformLocation(GLint location) const
1295{
Jamie Madille2e406c2016-06-02 13:04:10 -04001296 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001297 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1298 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001299}
1300
Jamie Madill62d31cb2015-09-11 13:25:51 -04001301const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001303 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001304 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305}
1306
Jamie Madillac4e9c32017-01-13 14:07:12 -05001307const VariableLocation &Program::getUniformLocation(GLint location) const
1308{
1309 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1310 return mState.mUniformLocations[location];
1311}
1312
1313const std::vector<VariableLocation> &Program::getUniformLocations() const
1314{
1315 return mState.mUniformLocations;
1316}
1317
1318const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1319{
1320 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1321 return mState.mUniforms[index];
1322}
1323
Jamie Madill62d31cb2015-09-11 13:25:51 -04001324GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001326 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001327}
1328
Jamie Madill62d31cb2015-09-11 13:25:51 -04001329GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330{
Jamie Madille7d84322017-01-10 18:21:59 -05001331 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
1334void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1335{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001336 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1337 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001338 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001339}
1340
1341void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1342{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001343 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1344 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001345 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346}
1347
1348void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1349{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001350 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1351 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001352 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
1355void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1356{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001357 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1358 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001359 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001360}
1361
1362void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1363{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001364 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1365 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1366
1367 if (mState.isSamplerUniformIndex(locationInfo.index))
1368 {
1369 updateSamplerUniform(locationInfo, clampedCount, v);
1370 }
1371
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001372 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
1375void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1376{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001377 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1378 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001379 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380}
1381
1382void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1383{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001384 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1385 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001386 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387}
1388
1389void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1390{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001391 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1392 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001393 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394}
1395
1396void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1397{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001398 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1399 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001400 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401}
1402
1403void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1404{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001405 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1406 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001407 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408}
1409
1410void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1411{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001412 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1413 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001414 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
1417void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1418{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001419 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1420 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001421 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422}
1423
1424void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1425{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001426 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001427 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428}
1429
1430void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1431{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001432 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001433 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434}
1435
1436void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1437{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001438 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001439 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440}
1441
1442void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1443{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001444 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001445 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446}
1447
1448void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1449{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001450 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001451 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001452}
1453
1454void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1455{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001456 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001457 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458}
1459
1460void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1461{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001462 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001463 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464}
1465
1466void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1467{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001468 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001469 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470}
1471
1472void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1473{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001474 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001475 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476}
1477
Jamie Madill54164b02017-08-28 15:17:37 -04001478void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479{
Jamie Madill54164b02017-08-28 15:17:37 -04001480 const auto &uniformLocation = mState.getUniformLocations()[location];
1481 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1482
1483 GLenum nativeType = gl::VariableComponentType(uniform.type);
1484 if (nativeType == GL_FLOAT)
1485 {
1486 mProgram->getUniformfv(context, location, v);
1487 }
1488 else
1489 {
1490 getUniformInternal(context, v, location, nativeType,
1491 gl::VariableComponentCount(uniform.type));
1492 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001493}
1494
Jamie Madill54164b02017-08-28 15:17:37 -04001495void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496{
Jamie Madill54164b02017-08-28 15:17:37 -04001497 const auto &uniformLocation = mState.getUniformLocations()[location];
1498 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1499
1500 GLenum nativeType = gl::VariableComponentType(uniform.type);
1501 if (nativeType == GL_INT || nativeType == GL_BOOL)
1502 {
1503 mProgram->getUniformiv(context, location, v);
1504 }
1505 else
1506 {
1507 getUniformInternal(context, v, location, nativeType,
1508 gl::VariableComponentCount(uniform.type));
1509 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510}
1511
Jamie Madill54164b02017-08-28 15:17:37 -04001512void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513{
Jamie Madill54164b02017-08-28 15:17:37 -04001514 const auto &uniformLocation = mState.getUniformLocations()[location];
1515 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1516
1517 GLenum nativeType = gl::VariableComponentType(uniform.type);
1518 if (nativeType == GL_UNSIGNED_INT)
1519 {
1520 mProgram->getUniformuiv(context, location, v);
1521 }
1522 else
1523 {
1524 getUniformInternal(context, v, location, nativeType,
1525 gl::VariableComponentCount(uniform.type));
1526 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001527}
1528
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529void Program::flagForDeletion()
1530{
1531 mDeleteStatus = true;
1532}
1533
1534bool Program::isFlaggedForDeletion() const
1535{
1536 return mDeleteStatus;
1537}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001538
Brandon Jones43a53e22014-08-28 16:23:22 -07001539void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001540{
1541 mInfoLog.reset();
1542
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001544 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001545 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001546 }
1547 else
1548 {
Jamie Madillf6113162015-05-07 11:49:21 -04001549 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001550 }
1551}
1552
Geoff Lang7dd2e102014-11-10 15:19:26 -05001553bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1554{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001555 // Skip cache if we're using an infolog, so we get the full error.
1556 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1557 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1558 {
1559 return mCachedValidateSamplersResult.value();
1560 }
1561
1562 if (mTextureUnitTypesCache.empty())
1563 {
1564 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1565 }
1566 else
1567 {
1568 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1569 }
1570
1571 // if any two active samplers in a program are of different types, but refer to the same
1572 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1573 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001574 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001575 {
Jamie Madill54164b02017-08-28 15:17:37 -04001576 if (samplerBinding.unreferenced)
1577 continue;
1578
Jamie Madille7d84322017-01-10 18:21:59 -05001579 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001580
Jamie Madille7d84322017-01-10 18:21:59 -05001581 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001582 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001583 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1584 {
1585 if (infoLog)
1586 {
1587 (*infoLog) << "Sampler uniform (" << textureUnit
1588 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1589 << caps.maxCombinedTextureImageUnits << ")";
1590 }
1591
1592 mCachedValidateSamplersResult = false;
1593 return false;
1594 }
1595
1596 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1597 {
1598 if (textureType != mTextureUnitTypesCache[textureUnit])
1599 {
1600 if (infoLog)
1601 {
1602 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1603 "image unit ("
1604 << textureUnit << ").";
1605 }
1606
1607 mCachedValidateSamplersResult = false;
1608 return false;
1609 }
1610 }
1611 else
1612 {
1613 mTextureUnitTypesCache[textureUnit] = textureType;
1614 }
1615 }
1616 }
1617
1618 mCachedValidateSamplersResult = true;
1619 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620}
1621
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001622bool Program::isValidated() const
1623{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 return mValidated;
1625}
1626
Geoff Lange1a27752015-10-05 13:16:04 -04001627GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001629 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001630}
1631
1632void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1633{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001634 ASSERT(
1635 uniformBlockIndex <
1636 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637
Jamie Madill48ef11b2016-04-27 15:21:52 -04001638 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639
1640 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001641 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001642 std::string string = uniformBlock.name;
1643
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001647 }
jchen10fd7c3b52017-03-21 15:36:03 +08001648 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001649 }
1650}
1651
Geoff Lange1a27752015-10-05 13:16:04 -04001652GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001653{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654 int maxLength = 0;
1655
1656 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001657 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001658 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001659 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1660 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001661 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662 if (!uniformBlock.name.empty())
1663 {
jchen10af713a22017-04-19 09:10:56 +08001664 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1665 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 }
1667 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001668 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001669
1670 return maxLength;
1671}
1672
Geoff Lange1a27752015-10-05 13:16:04 -04001673GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001675 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001676 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001677
Jamie Madill48ef11b2016-04-27 15:21:52 -04001678 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1680 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001681 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001682 if (uniformBlock.name == baseName)
1683 {
1684 const bool arrayElementZero =
1685 (subscript == GL_INVALID_INDEX &&
1686 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1687 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1688 {
1689 return blockIndex;
1690 }
1691 }
1692 }
1693
1694 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001695}
1696
Jamie Madill62d31cb2015-09-11 13:25:51 -04001697const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001698{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001699 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1700 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001701}
1702
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001703void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1704{
jchen107a20b972017-06-13 14:25:26 +08001705 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001706 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001707 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001708}
1709
1710GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1711{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001712 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001713}
1714
Geoff Lang48dcae72014-02-05 16:28:24 -05001715void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1716{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001717 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001718 for (GLsizei i = 0; i < count; i++)
1719 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001720 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001721 }
1722
Jamie Madill48ef11b2016-04-27 15:21:52 -04001723 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001724}
1725
1726void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1727{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001729 {
jchen10a9042d32017-03-17 08:50:45 +08001730 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1731 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1732 std::string varName = var.nameWithArrayIndex();
1733 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001734 if (length)
1735 {
1736 *length = lastNameIdx;
1737 }
1738 if (size)
1739 {
jchen10a9042d32017-03-17 08:50:45 +08001740 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001741 }
1742 if (type)
1743 {
jchen10a9042d32017-03-17 08:50:45 +08001744 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001745 }
1746 if (name)
1747 {
jchen10a9042d32017-03-17 08:50:45 +08001748 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001749 name[lastNameIdx] = '\0';
1750 }
1751 }
1752}
1753
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001754GLsizei Program::getTransformFeedbackVaryingCount() const
1755{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001756 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001757 {
jchen10a9042d32017-03-17 08:50:45 +08001758 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001759 }
1760 else
1761 {
1762 return 0;
1763 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001764}
1765
1766GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1767{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001768 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001769 {
1770 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001771 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001772 {
jchen10a9042d32017-03-17 08:50:45 +08001773 maxSize =
1774 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001775 }
1776
1777 return maxSize;
1778 }
1779 else
1780 {
1781 return 0;
1782 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001783}
1784
1785GLenum Program::getTransformFeedbackBufferMode() const
1786{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001787 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001788}
1789
Jamie Madillbd044ed2017-06-05 12:59:21 -04001790bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001792 Shader *vertexShader = mState.mAttachedVertexShader;
1793 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001794
Jamie Madillbd044ed2017-06-05 12:59:21 -04001795 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001796
Jamie Madillbd044ed2017-06-05 12:59:21 -04001797 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1798 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799
Sami Väisänen46eaa942016-06-29 10:26:37 +03001800 std::map<GLuint, std::string> staticFragmentInputLocations;
1801
Jamie Madill4cff2472015-08-21 16:53:18 -04001802 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 bool matched = false;
1805
1806 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001807 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808 {
1809 continue;
1810 }
1811
Jamie Madill4cff2472015-08-21 16:53:18 -04001812 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001814 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001816 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001817 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001818 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001819 {
1820 return false;
1821 }
1822
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 matched = true;
1824 break;
1825 }
1826 }
1827
1828 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001829 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001831 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 return false;
1833 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001834
1835 // Check for aliased path rendering input bindings (if any).
1836 // If more than one binding refer statically to the same
1837 // location the link must fail.
1838
1839 if (!output.staticUse)
1840 continue;
1841
1842 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1843 if (inputBinding == -1)
1844 continue;
1845
1846 const auto it = staticFragmentInputLocations.find(inputBinding);
1847 if (it == std::end(staticFragmentInputLocations))
1848 {
1849 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1850 }
1851 else
1852 {
1853 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1854 << it->second;
1855 return false;
1856 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 }
1858
Jamie Madillbd044ed2017-06-05 12:59:21 -04001859 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001860 {
1861 return false;
1862 }
1863
Jamie Madillada9ecc2015-08-17 12:53:37 -04001864 // TODO(jmadill): verify no unmatched vertex varyings?
1865
Geoff Lang7dd2e102014-11-10 15:19:26 -05001866 return true;
1867}
1868
Jamie Madillbd044ed2017-06-05 12:59:21 -04001869bool Program::linkUniforms(const Context *context,
1870 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001871 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001872{
Olli Etuahob78707c2017-03-09 15:03:11 +00001873 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001874 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001875 {
1876 return false;
1877 }
1878
Olli Etuahob78707c2017-03-09 15:03:11 +00001879 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001880
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001881 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001882
jchen10eaef1e52017-06-13 10:44:11 +08001883 if (!linkAtomicCounterBuffers())
1884 {
1885 return false;
1886 }
1887
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001888 return true;
1889}
1890
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001891void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001892{
Jamie Madill982f6e02017-06-07 14:33:04 -04001893 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1894 unsigned int low = high;
1895
jchen10eaef1e52017-06-13 10:44:11 +08001896 for (auto counterIter = mState.mUniforms.rbegin();
1897 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1898 {
1899 --low;
1900 }
1901
1902 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1903
1904 high = low;
1905
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001906 for (auto imageIter = mState.mUniforms.rbegin();
1907 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1908 {
1909 --low;
1910 }
1911
1912 mState.mImageUniformRange = RangeUI(low, high);
1913
1914 // If uniform is a image type, insert it into the mImageBindings array.
1915 for (unsigned int imageIndex : mState.mImageUniformRange)
1916 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001917 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1918 // cannot load values into a uniform defined as an image. if declare without a
1919 // binding qualifier, any uniform image variable (include all elements of
1920 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001921 auto &imageUniform = mState.mUniforms[imageIndex];
1922 if (imageUniform.binding == -1)
1923 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001924 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001925 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001926 else
1927 {
1928 mState.mImageBindings.emplace_back(
1929 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1930 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001931 }
1932
1933 high = low;
1934
1935 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001936 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001937 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001938 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001939 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001940
1941 mState.mSamplerUniformRange = RangeUI(low, high);
1942
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001943 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001944 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001945 {
1946 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1947 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1948 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001949 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001950 }
1951}
1952
jchen10eaef1e52017-06-13 10:44:11 +08001953bool Program::linkAtomicCounterBuffers()
1954{
1955 for (unsigned int index : mState.mAtomicCounterUniformRange)
1956 {
1957 auto &uniform = mState.mUniforms[index];
1958 bool found = false;
1959 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1960 ++bufferIndex)
1961 {
1962 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1963 if (buffer.binding == uniform.binding)
1964 {
1965 buffer.memberIndexes.push_back(index);
1966 uniform.bufferIndex = bufferIndex;
1967 found = true;
1968 break;
1969 }
1970 }
1971 if (!found)
1972 {
1973 AtomicCounterBuffer atomicCounterBuffer;
1974 atomicCounterBuffer.binding = uniform.binding;
1975 atomicCounterBuffer.memberIndexes.push_back(index);
1976 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1977 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1978 }
1979 }
1980 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1981 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1982
1983 return true;
1984}
1985
Martin Radev4c4c8e72016-08-04 12:25:34 +03001986bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1987 const std::string &uniformName,
1988 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001989 const sh::InterfaceBlockField &fragmentUniform,
1990 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001991{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001992 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1993 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1994 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001995 {
1996 return false;
1997 }
1998
1999 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2000 {
Jamie Madillf6113162015-05-07 11:49:21 -04002001 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002 return false;
2003 }
2004
2005 return true;
2006}
2007
Jamie Madilleb979bf2016-11-15 12:28:46 -05002008// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002009bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002011 const ContextState &data = context->getContextState();
2012 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002013
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002015 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002016 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002017
2018 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002019 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002020 {
Jamie Madillf6113162015-05-07 11:49:21 -04002021 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002022 return false;
2023 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002024
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002025 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002026
Jamie Madillc349ec02015-08-21 16:53:12 -04002027 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002028 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002029 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002030 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002031 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002032 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002033 attribute.location = bindingLocation;
2034 }
2035
2036 if (attribute.location != -1)
2037 {
2038 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002039 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040
Jamie Madill63805b42015-08-25 13:17:39 -04002041 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 {
Jamie Madillf6113162015-05-07 11:49:21 -04002043 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002044 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045
2046 return false;
2047 }
2048
Jamie Madill63805b42015-08-25 13:17:39 -04002049 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 {
Jamie Madill63805b42015-08-25 13:17:39 -04002051 const int regLocation = attribute.location + reg;
2052 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053
2054 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002055 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002056 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002058 // TODO(jmadill): fix aliasing on ES2
2059 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002060 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002061 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002062 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 return false;
2064 }
2065 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002066 else
2067 {
Jamie Madill63805b42015-08-25 13:17:39 -04002068 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002069 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070
Jamie Madill63805b42015-08-25 13:17:39 -04002071 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002072 }
2073 }
2074 }
2075
2076 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002077 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002079 // Not set by glBindAttribLocation or by location layout qualifier
2080 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 {
Jamie Madill63805b42015-08-25 13:17:39 -04002082 int regs = VariableRegisterCount(attribute.type);
2083 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084
Jamie Madill63805b42015-08-25 13:17:39 -04002085 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086 {
Jamie Madillf6113162015-05-07 11:49:21 -04002087 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002088 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 }
2090
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 }
2093 }
2094
Jamie Madill48ef11b2016-04-27 15:21:52 -04002095 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 {
Jamie Madill63805b42015-08-25 13:17:39 -04002097 ASSERT(attribute.location != -1);
2098 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002099
Jamie Madill63805b42015-08-25 13:17:39 -04002100 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002102 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 }
2104 }
2105
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 return true;
2107}
2108
Martin Radev4c4c8e72016-08-04 12:25:34 +03002109bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2110 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2111 const std::string &errorMessage,
2112 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002114 GLuint blockCount = 0;
2115 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002117 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002118 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002119 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002120 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002121 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002122 return false;
2123 }
2124 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002126 return true;
2127}
Jamie Madille473dee2015-08-18 14:49:01 -04002128
Martin Radev4c4c8e72016-08-04 12:25:34 +03002129bool Program::validateVertexAndFragmentInterfaceBlocks(
2130 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2131 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002132 InfoLog &infoLog,
2133 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002134{
2135 // Check that interface blocks defined in the vertex and fragment shaders are identical
2136 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2137 UniformBlockMap linkedUniformBlocks;
2138
2139 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2140 {
2141 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2142 }
2143
Jamie Madille473dee2015-08-18 14:49:01 -04002144 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 {
Jamie Madille473dee2015-08-18 14:49:01 -04002146 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147 if (entry != linkedUniformBlocks.end())
2148 {
2149 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002150 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2151 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 {
2153 return false;
2154 }
2155 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002156 }
2157 return true;
2158}
Jamie Madille473dee2015-08-18 14:49:01 -04002159
Jamie Madillbd044ed2017-06-05 12:59:21 -04002160bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002161{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002162 const auto &caps = context->getCaps();
2163
Martin Radev4c4c8e72016-08-04 12:25:34 +03002164 if (mState.mAttachedComputeShader)
2165 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002166 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002167 const auto &computeInterfaceBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002168
2169 if (!validateUniformBlocksCount(
2170 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2171 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2172 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002174 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002176 return true;
2177 }
2178
Jamie Madillbd044ed2017-06-05 12:59:21 -04002179 Shader &vertexShader = *mState.mAttachedVertexShader;
2180 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002181
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002182 const auto &vertexInterfaceBlocks = vertexShader.getUniformBlocks(context);
2183 const auto &fragmentInterfaceBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002184
2185 if (!validateUniformBlocksCount(
2186 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2187 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2188 {
2189 return false;
2190 }
2191 if (!validateUniformBlocksCount(
2192 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2193 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2194 infoLog))
2195 {
2196
2197 return false;
2198 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002199
2200 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002201 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002202 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002203 {
2204 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002205 }
Jamie Madille473dee2015-08-18 14:49:01 -04002206
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207 return true;
2208}
2209
Jamie Madilla2c74982016-12-12 11:20:42 -05002210bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002211 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002212 const sh::InterfaceBlock &fragmentInterfaceBlock,
2213 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214{
2215 const char* blockName = vertexInterfaceBlock.name.c_str();
2216 // validate blocks for the same member types
2217 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2218 {
Jamie Madillf6113162015-05-07 11:49:21 -04002219 infoLog << "Types for interface block '" << blockName
2220 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 return false;
2222 }
2223 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2224 {
Jamie Madillf6113162015-05-07 11:49:21 -04002225 infoLog << "Array sizes differ for interface block '" << blockName
2226 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002227 return false;
2228 }
jchen10af713a22017-04-19 09:10:56 +08002229 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2230 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2231 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002232 {
Jamie Madillf6113162015-05-07 11:49:21 -04002233 infoLog << "Layout qualifiers differ for interface block '" << blockName
2234 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235 return false;
2236 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002237 const unsigned int numBlockMembers =
2238 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002239 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2240 {
2241 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2242 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2243 if (vertexMember.name != fragmentMember.name)
2244 {
Jamie Madillf6113162015-05-07 11:49:21 -04002245 infoLog << "Name mismatch for field " << blockMemberIndex
2246 << " of interface block '" << blockName
2247 << "': (in vertex: '" << vertexMember.name
2248 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249 return false;
2250 }
2251 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002252 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2253 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002254 {
2255 return false;
2256 }
2257 }
2258 return true;
2259}
2260
2261bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2262 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2263{
2264 if (vertexVariable.type != fragmentVariable.type)
2265 {
Jamie Madillf6113162015-05-07 11:49:21 -04002266 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002267 return false;
2268 }
2269 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2270 {
Jamie Madillf6113162015-05-07 11:49:21 -04002271 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002272 return false;
2273 }
2274 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2275 {
Jamie Madillf6113162015-05-07 11:49:21 -04002276 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002277 return false;
2278 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002279 if (vertexVariable.structName != fragmentVariable.structName)
2280 {
2281 infoLog << "Structure names for " << variableName
2282 << " differ between vertex and fragment shaders";
2283 return false;
2284 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002285
2286 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2287 {
Jamie Madillf6113162015-05-07 11:49:21 -04002288 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289 return false;
2290 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002291 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002292 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2293 {
2294 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2295 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2296
2297 if (vertexMember.name != fragmentMember.name)
2298 {
Jamie Madillf6113162015-05-07 11:49:21 -04002299 infoLog << "Name mismatch for field '" << memberIndex
2300 << "' of " << variableName
2301 << ": (in vertex: '" << vertexMember.name
2302 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002303 return false;
2304 }
2305
2306 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2307 vertexMember.name + "'";
2308
2309 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2310 {
2311 return false;
2312 }
2313 }
2314
2315 return true;
2316}
2317
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002318bool Program::linkValidateVaryings(InfoLog &infoLog,
2319 const std::string &varyingName,
2320 const sh::Varying &vertexVarying,
2321 const sh::Varying &fragmentVarying,
2322 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323{
2324 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2325 {
2326 return false;
2327 }
2328
Jamie Madille9cc4692015-02-19 16:00:13 -05002329 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002330 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002331 infoLog << "Interpolation types for " << varyingName
2332 << " differ between vertex and fragment shaders.";
2333 return false;
2334 }
2335
2336 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2337 {
2338 infoLog << "Invariance for " << varyingName
2339 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002340 return false;
2341 }
2342
2343 return true;
2344}
2345
Jamie Madillbd044ed2017-06-05 12:59:21 -04002346bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002347{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002348 Shader *vertexShader = mState.mAttachedVertexShader;
2349 Shader *fragmentShader = mState.mAttachedFragmentShader;
2350 const auto &vertexVaryings = vertexShader->getVaryings(context);
2351 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2352 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002353
2354 if (shaderVersion != 100)
2355 {
2356 // Only ESSL 1.0 has restrictions on matching input and output invariance
2357 return true;
2358 }
2359
2360 bool glPositionIsInvariant = false;
2361 bool glPointSizeIsInvariant = false;
2362 bool glFragCoordIsInvariant = false;
2363 bool glPointCoordIsInvariant = false;
2364
2365 for (const sh::Varying &varying : vertexVaryings)
2366 {
2367 if (!varying.isBuiltIn())
2368 {
2369 continue;
2370 }
2371 if (varying.name.compare("gl_Position") == 0)
2372 {
2373 glPositionIsInvariant = varying.isInvariant;
2374 }
2375 else if (varying.name.compare("gl_PointSize") == 0)
2376 {
2377 glPointSizeIsInvariant = varying.isInvariant;
2378 }
2379 }
2380
2381 for (const sh::Varying &varying : fragmentVaryings)
2382 {
2383 if (!varying.isBuiltIn())
2384 {
2385 continue;
2386 }
2387 if (varying.name.compare("gl_FragCoord") == 0)
2388 {
2389 glFragCoordIsInvariant = varying.isInvariant;
2390 }
2391 else if (varying.name.compare("gl_PointCoord") == 0)
2392 {
2393 glPointCoordIsInvariant = varying.isInvariant;
2394 }
2395 }
2396
2397 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2398 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2399 // Not requiring invariance to match is supported by:
2400 // dEQP, WebGL CTS, Nexus 5X GLES
2401 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2402 {
2403 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2404 "declared invariant.";
2405 return false;
2406 }
2407 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2408 {
2409 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2410 "declared invariant.";
2411 return false;
2412 }
2413
2414 return true;
2415}
2416
jchen10a9042d32017-03-17 08:50:45 +08002417bool Program::linkValidateTransformFeedback(const gl::Context *context,
2418 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002419 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002420 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421{
2422 size_t totalComponents = 0;
2423
Jamie Madillccdf74b2015-08-18 10:46:12 -04002424 std::set<std::string> uniqueNames;
2425
Jamie Madill48ef11b2016-04-27 15:21:52 -04002426 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002427 {
2428 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002429 size_t subscript = GL_INVALID_INDEX;
2430 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2431
Jamie Madill192745a2016-12-22 15:58:21 -05002432 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002433 {
Jamie Madill192745a2016-12-22 15:58:21 -05002434 const sh::Varying *varying = ref.second.get();
2435
jchen10a9042d32017-03-17 08:50:45 +08002436 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002437 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002438 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002439 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002440 infoLog << "Two transform feedback varyings specify the same output variable ("
2441 << tfVaryingName << ").";
2442 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002443 }
jchen10a9042d32017-03-17 08:50:45 +08002444 if (context->getClientVersion() >= Version(3, 1))
2445 {
2446 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2447 {
2448 infoLog
2449 << "Two transform feedback varyings include the same array element ("
2450 << tfVaryingName << ").";
2451 return false;
2452 }
2453 }
2454 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002455 {
2456 infoLog << "Capture of arrays is undefined and not supported.";
2457 return false;
2458 }
2459
jchen10a9042d32017-03-17 08:50:45 +08002460 uniqueNames.insert(tfVaryingName);
2461
Jamie Madillccdf74b2015-08-18 10:46:12 -04002462 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002463 size_t elementCount =
2464 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2465 : 1);
2466 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002467 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002468 componentCount > caps.maxTransformFeedbackSeparateComponents)
2469 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002470 infoLog << "Transform feedback varying's " << varying->name << " components ("
2471 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002472 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 return false;
2474 }
2475
2476 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 found = true;
2478 break;
2479 }
2480 }
jchen10a9042d32017-03-17 08:50:45 +08002481 if (context->getClientVersion() < Version(3, 1) &&
2482 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002483 {
Geoff Lang1a683462015-09-29 15:09:59 -04002484 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002485 return false;
2486 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002487 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2488 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002489 ASSERT(found);
2490 }
2491
Jamie Madill48ef11b2016-04-27 15:21:52 -04002492 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002493 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494 {
Jamie Madillf6113162015-05-07 11:49:21 -04002495 infoLog << "Transform feedback varying total components (" << totalComponents
2496 << ") exceed the maximum interleaved components ("
2497 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002498 return false;
2499 }
2500
2501 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002502}
2503
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002504bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2505{
2506 const std::vector<sh::Uniform> &vertexUniforms =
2507 mState.mAttachedVertexShader->getUniforms(context);
2508 const std::vector<sh::Uniform> &fragmentUniforms =
2509 mState.mAttachedFragmentShader->getUniforms(context);
2510 const std::vector<sh::Attribute> &attributes =
2511 mState.mAttachedVertexShader->getActiveAttributes(context);
2512 for (const auto &attrib : attributes)
2513 {
2514 for (const auto &uniform : vertexUniforms)
2515 {
2516 if (uniform.name == attrib.name)
2517 {
2518 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2519 return false;
2520 }
2521 }
2522 for (const auto &uniform : fragmentUniforms)
2523 {
2524 if (uniform.name == attrib.name)
2525 {
2526 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2527 return false;
2528 }
2529 }
2530 }
2531 return true;
2532}
2533
Jamie Madill192745a2016-12-22 15:58:21 -05002534void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002535{
2536 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002537 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002538 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002539 {
jchen10a9042d32017-03-17 08:50:45 +08002540 size_t subscript = GL_INVALID_INDEX;
2541 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002542 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002543 {
Jamie Madill192745a2016-12-22 15:58:21 -05002544 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002545 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002546 {
jchen10a9042d32017-03-17 08:50:45 +08002547 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2548 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002549 break;
2550 }
2551 }
2552 }
2553}
2554
Jamie Madillbd044ed2017-06-05 12:59:21 -04002555Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002556{
Jamie Madill192745a2016-12-22 15:58:21 -05002557 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002558
Jamie Madillbd044ed2017-06-05 12:59:21 -04002559 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002560 {
Jamie Madill192745a2016-12-22 15:58:21 -05002561 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002562 }
2563
Jamie Madillbd044ed2017-06-05 12:59:21 -04002564 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002565 {
Jamie Madill192745a2016-12-22 15:58:21 -05002566 merged[varying.name].fragment = &varying;
2567 }
2568
2569 return merged;
2570}
2571
2572std::vector<PackedVarying> Program::getPackedVaryings(
2573 const Program::MergedVaryings &mergedVaryings) const
2574{
2575 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2576 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002577 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002578
2579 for (const auto &ref : mergedVaryings)
2580 {
2581 const sh::Varying *input = ref.second.vertex;
2582 const sh::Varying *output = ref.second.fragment;
2583
2584 // Only pack varyings that have a matched input or output, plus special builtins.
2585 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002586 {
Jamie Madill192745a2016-12-22 15:58:21 -05002587 // Will get the vertex shader interpolation by default.
2588 auto interpolation = ref.second.get()->interpolation;
2589
Olli Etuaho06a06f52017-07-12 12:22:15 +03002590 // Note that we lose the vertex shader static use information here. The data for the
2591 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002592 if (output->isStruct())
2593 {
2594 ASSERT(!output->isArray());
2595 for (const auto &field : output->fields)
2596 {
2597 ASSERT(!field.isStruct() && !field.isArray());
2598 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2599 }
2600 }
2601 else
2602 {
2603 packedVaryings.push_back(PackedVarying(*output, interpolation));
2604 }
2605 continue;
2606 }
2607
2608 // Keep Transform FB varyings in the merged list always.
2609 if (!input)
2610 {
2611 continue;
2612 }
2613
2614 for (const std::string &tfVarying : tfVaryings)
2615 {
jchen10a9042d32017-03-17 08:50:45 +08002616 size_t subscript = GL_INVALID_INDEX;
2617 std::string baseName = ParseResourceName(tfVarying, &subscript);
2618 if (uniqueFullNames.count(tfVarying) > 0)
2619 {
2620 continue;
2621 }
2622 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002623 {
2624 // Transform feedback for varying structs is underspecified.
2625 // See Khronos bug 9856.
2626 // TODO(jmadill): Figure out how to be spec-compliant here.
2627 if (!input->isStruct())
2628 {
2629 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2630 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002631 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2632 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002633 }
jchen10a9042d32017-03-17 08:50:45 +08002634 if (subscript == GL_INVALID_INDEX)
2635 {
2636 break;
2637 }
Jamie Madill192745a2016-12-22 15:58:21 -05002638 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002639 }
2640 }
2641
Jamie Madill192745a2016-12-22 15:58:21 -05002642 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2643
2644 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002645}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002646
Jamie Madillbd044ed2017-06-05 12:59:21 -04002647void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002648{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002649 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002650 ASSERT(fragmentShader != nullptr);
2651
Geoff Lange0cff192017-05-30 13:04:56 -04002652 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002653 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002654
2655 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002656 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002657 {
2658 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2659 outputVariable.name != "gl_FragData")
2660 {
2661 continue;
2662 }
2663
2664 unsigned int baseLocation =
2665 (outputVariable.location == -1 ? 0u
2666 : static_cast<unsigned int>(outputVariable.location));
2667 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2668 elementIndex++)
2669 {
2670 const unsigned int location = baseLocation + elementIndex;
2671 if (location >= mState.mOutputVariableTypes.size())
2672 {
2673 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2674 }
Corentin Walleze7557742017-06-01 13:09:57 -04002675 ASSERT(location < mState.mActiveOutputVariables.size());
2676 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002677 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2678 }
2679 }
2680
Jamie Madill80a6fc02015-08-21 16:53:16 -04002681 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002682 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002683 return;
2684
Jamie Madillbd044ed2017-06-05 12:59:21 -04002685 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002686 // TODO(jmadill): any caps validation here?
2687
jchen1015015f72017-03-16 13:54:21 +08002688 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002689 outputVariableIndex++)
2690 {
jchen1015015f72017-03-16 13:54:21 +08002691 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002692
2693 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2694 if (outputVariable.isBuiltIn())
2695 continue;
2696
2697 // Since multiple output locations must be specified, use 0 for non-specified locations.
2698 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2699
Jamie Madill80a6fc02015-08-21 16:53:16 -04002700 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2701 elementIndex++)
2702 {
2703 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002704 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002705 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002706 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002707 VariableLocation(outputVariable.name, element, outputVariableIndex);
2708 }
2709 }
2710}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002711
Olli Etuaho48fed632017-03-16 12:05:30 +00002712void Program::setUniformValuesFromBindingQualifiers()
2713{
Jamie Madill982f6e02017-06-07 14:33:04 -04002714 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002715 {
2716 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2717 if (samplerUniform.binding != -1)
2718 {
2719 GLint location = mState.getUniformLocation(samplerUniform.name);
2720 ASSERT(location != -1);
2721 std::vector<GLint> boundTextureUnits;
2722 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2723 ++elementIndex)
2724 {
2725 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2726 }
2727 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2728 boundTextureUnits.data());
2729 }
2730 }
2731}
2732
jchen10eaef1e52017-06-13 10:44:11 +08002733void Program::gatherAtomicCounterBuffers()
2734{
2735 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2736 // counter.
2737 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2738}
2739
Jamie Madillbd044ed2017-06-05 12:59:21 -04002740void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002741{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002742 ASSERT(mState.mUniformBlocks.empty());
2743
2744 if (mState.mAttachedComputeShader)
2745 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002746 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002747
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002748 for (const sh::InterfaceBlock &computeBlock : computeShader->getUniformBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002749 {
2750
2751 // Only 'packed' blocks are allowed to be considered inactive.
2752 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2753 continue;
2754
Jamie Madilla2c74982016-12-12 11:20:42 -05002755 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002756 {
2757 if (block.name == computeBlock.name)
2758 {
2759 block.computeStaticUse = computeBlock.staticUse;
2760 }
2761 }
2762
2763 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2764 }
2765 return;
2766 }
2767
Jamie Madill62d31cb2015-09-11 13:25:51 -04002768 std::set<std::string> visitedList;
2769
Jamie Madillbd044ed2017-06-05 12:59:21 -04002770 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002771
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002772 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002773 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002774 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002775 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2776 continue;
2777
2778 if (visitedList.count(vertexBlock.name) > 0)
2779 continue;
2780
2781 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2782 visitedList.insert(vertexBlock.name);
2783 }
2784
Jamie Madillbd044ed2017-06-05 12:59:21 -04002785 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002786
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002787 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002788 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002789 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002790 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2791 continue;
2792
2793 if (visitedList.count(fragmentBlock.name) > 0)
2794 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002795 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002796 {
2797 if (block.name == fragmentBlock.name)
2798 {
2799 block.fragmentStaticUse = fragmentBlock.staticUse;
2800 }
2801 }
2802
2803 continue;
2804 }
2805
2806 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2807 visitedList.insert(fragmentBlock.name);
2808 }
jchen10af713a22017-04-19 09:10:56 +08002809 // Set initial bindings from shader.
2810 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2811 {
2812 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2813 bindUniformBlock(blockIndex, uniformBlock.binding);
2814 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002815}
2816
Jamie Madill4a3c2342015-10-08 12:58:45 -04002817template <typename VarT>
2818void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2819 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002820 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002821 int blockIndex)
2822{
2823 for (const VarT &field : fields)
2824 {
2825 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2826
Olli Etuaho855d9642017-05-17 14:05:06 +03002827 const std::string &fullMappedName =
2828 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2829
Jamie Madill4a3c2342015-10-08 12:58:45 -04002830 if (field.isStruct())
2831 {
2832 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2833 {
2834 const std::string uniformElementName =
2835 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002836 const std::string uniformElementMappedName =
2837 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2838 defineUniformBlockMembers(field.fields, uniformElementName,
2839 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002840 }
2841 }
2842 else
2843 {
2844 // If getBlockMemberInfo returns false, the uniform is optimized out.
2845 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002846 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002847 {
2848 continue;
2849 }
2850
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002851 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002852 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002853 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002854
2855 // Since block uniforms have no location, we don't need to store them in the uniform
2856 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002857 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002858 }
2859 }
2860}
2861
Jamie Madill62d31cb2015-09-11 13:25:51 -04002862void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2863{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002864 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002865 size_t blockSize = 0;
2866
Jamie Madill4a3c2342015-10-08 12:58:45 -04002867 // Track the first and last uniform index to determine the range of active uniforms in the
2868 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002869 size_t firstBlockUniformIndex = mState.mUniforms.size();
Olli Etuaho855d9642017-05-17 14:05:06 +03002870 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2871 interfaceBlock.fieldMappedPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002872 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002873
2874 std::vector<unsigned int> blockUniformIndexes;
2875 for (size_t blockUniformIndex = firstBlockUniformIndex;
2876 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2877 {
2878 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2879 }
jchen10af713a22017-04-19 09:10:56 +08002880 // ESSL 3.10 section 4.4.4 page 58:
2881 // Any uniform or shader storage block declared without a binding qualifier is initially
2882 // assigned to block binding point zero.
2883 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002884 if (interfaceBlock.arraySize > 0)
2885 {
2886 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2887 {
jchen10af713a22017-04-19 09:10:56 +08002888 // Don't define this block at all if it's not active in the implementation.
Olli Etuaho855d9642017-05-17 14:05:06 +03002889 if (!mProgram->getUniformBlockSize(
2890 interfaceBlock.name + ArrayString(arrayElement),
2891 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
jchen10af713a22017-04-19 09:10:56 +08002892 {
2893 continue;
2894 }
Olli Etuaho855d9642017-05-17 14:05:06 +03002895 UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
jchen10af713a22017-04-19 09:10:56 +08002896 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002897 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002898
Martin Radev4c4c8e72016-08-04 12:25:34 +03002899 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002900 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002901 case GL_VERTEX_SHADER:
2902 {
2903 block.vertexStaticUse = interfaceBlock.staticUse;
2904 break;
2905 }
2906 case GL_FRAGMENT_SHADER:
2907 {
2908 block.fragmentStaticUse = interfaceBlock.staticUse;
2909 break;
2910 }
2911 case GL_COMPUTE_SHADER:
2912 {
2913 block.computeStaticUse = interfaceBlock.staticUse;
2914 break;
2915 }
2916 default:
2917 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002918 }
2919
Qin Jiajia0350a642016-11-01 17:01:51 +08002920 // Since all block elements in an array share the same active uniforms, they will all be
2921 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2922 // here we will add every block element in the array.
2923 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002924 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002925 }
2926 }
2927 else
2928 {
Olli Etuaho855d9642017-05-17 14:05:06 +03002929 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
2930 &blockSize))
jchen10af713a22017-04-19 09:10:56 +08002931 {
2932 return;
2933 }
Olli Etuaho855d9642017-05-17 14:05:06 +03002934 UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002935 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002936
Martin Radev4c4c8e72016-08-04 12:25:34 +03002937 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002938 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002939 case GL_VERTEX_SHADER:
2940 {
2941 block.vertexStaticUse = interfaceBlock.staticUse;
2942 break;
2943 }
2944 case GL_FRAGMENT_SHADER:
2945 {
2946 block.fragmentStaticUse = interfaceBlock.staticUse;
2947 break;
2948 }
2949 case GL_COMPUTE_SHADER:
2950 {
2951 block.computeStaticUse = interfaceBlock.staticUse;
2952 break;
2953 }
2954 default:
2955 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002956 }
2957
Jamie Madill4a3c2342015-10-08 12:58:45 -04002958 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002959 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960 }
2961}
2962
Jamie Madille7d84322017-01-10 18:21:59 -05002963void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002964 GLsizei clampedCount,
2965 const GLint *v)
2966{
2967 // Invalidate the validation cache only if we modify the sampler data.
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002968 if (mState.isSamplerUniformIndex(locationInfo.index))
Jamie Madille7d84322017-01-10 18:21:59 -05002969 {
2970 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2971 std::vector<GLuint> *boundTextureUnits =
2972 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2973
2974 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2975 mCachedValidateSamplersResult.reset();
2976 }
2977}
2978
2979template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002980GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2981 GLsizei count,
2982 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002983 const T *v)
2984{
Jamie Madill134f93d2017-08-31 17:11:00 -04002985 if (count == 1)
2986 return 1;
2987
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002988 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002989
Corentin Wallez15ac5342016-11-03 17:06:39 -04002990 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2991 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002992 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002993 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002994 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002995
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002996 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002997 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002998 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002999 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003000
3001 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003002}
3003
3004template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003005GLsizei Program::clampMatrixUniformCount(GLint location,
3006 GLsizei count,
3007 GLboolean transpose,
3008 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003009{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003010 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3011
Jamie Madill62d31cb2015-09-11 13:25:51 -04003012 if (!transpose)
3013 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003014 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003015 }
3016
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003017 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003018
3019 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3020 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003021 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
3022 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003023}
3024
Jamie Madill54164b02017-08-28 15:17:37 -04003025// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3026// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003027template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003028void Program::getUniformInternal(const Context *context,
3029 DestT *dataOut,
3030 GLint location,
3031 GLenum nativeType,
3032 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003033{
Jamie Madill54164b02017-08-28 15:17:37 -04003034 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003035 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003036 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003037 {
3038 GLint tempValue[16] = {0};
3039 mProgram->getUniformiv(context, location, tempValue);
3040 UniformStateQueryCastLoop<GLboolean>(
3041 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003042 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003043 }
3044 case GL_INT:
3045 {
3046 GLint tempValue[16] = {0};
3047 mProgram->getUniformiv(context, location, tempValue);
3048 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3049 components);
3050 break;
3051 }
3052 case GL_UNSIGNED_INT:
3053 {
3054 GLuint tempValue[16] = {0};
3055 mProgram->getUniformuiv(context, location, tempValue);
3056 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3057 components);
3058 break;
3059 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003060 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003061 {
3062 GLfloat tempValue[16] = {0};
3063 mProgram->getUniformfv(context, location, tempValue);
3064 UniformStateQueryCastLoop<GLfloat>(
3065 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003066 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003067 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003068 default:
3069 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003070 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003071 }
3072}
Jamie Madilla4595b82017-01-11 17:36:34 -05003073
3074bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3075{
3076 // Must be called after samplers are validated.
3077 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3078
3079 for (const auto &binding : mState.mSamplerBindings)
3080 {
3081 GLenum textureType = binding.textureType;
3082 for (const auto &unit : binding.boundTextureUnits)
3083 {
3084 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3085 if (programTextureID == textureID)
3086 {
3087 // TODO(jmadill): Check for appropriate overlap.
3088 return true;
3089 }
3090 }
3091 }
3092
3093 return false;
3094}
3095
Jamie Madilla2c74982016-12-12 11:20:42 -05003096} // namespace gl