blob: cc37d2fd785b38953a14afa4f7b024a2d4540a08 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050019#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040020#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040022#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000023#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040024#include "libANGLE/VaryingPacking.h"
25#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040026#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/queryconversions.h"
28#include "libANGLE/renderer/GLImplFactory.h"
29#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040030#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace gl
33{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000034
Geoff Lang7dd2e102014-11-10 15:19:26 -050035namespace
36{
37
Jamie Madill62d31cb2015-09-11 13:25:51 -040038// This simplified cast function doesn't need to worry about advanced concepts like
39// depth range values, or casting to bool.
40template <typename DestT, typename SrcT>
41DestT UniformStateQueryCast(SrcT value);
42
43// From-Float-To-Integer Casts
44template <>
45GLint UniformStateQueryCast(GLfloat value)
46{
47 return clampCast<GLint>(roundf(value));
48}
49
50template <>
51GLuint UniformStateQueryCast(GLfloat value)
52{
53 return clampCast<GLuint>(roundf(value));
54}
55
56// From-Integer-to-Integer Casts
57template <>
58GLint UniformStateQueryCast(GLuint value)
59{
60 return clampCast<GLint>(value);
61}
62
63template <>
64GLuint UniformStateQueryCast(GLint value)
65{
66 return clampCast<GLuint>(value);
67}
68
69// From-Boolean-to-Anything Casts
70template <>
71GLfloat UniformStateQueryCast(GLboolean value)
72{
73 return (value == GL_TRUE ? 1.0f : 0.0f);
74}
75
76template <>
77GLint UniformStateQueryCast(GLboolean value)
78{
79 return (value == GL_TRUE ? 1 : 0);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLboolean value)
84{
85 return (value == GL_TRUE ? 1u : 0u);
86}
87
88// Default to static_cast
89template <typename DestT, typename SrcT>
90DestT UniformStateQueryCast(SrcT value)
91{
92 return static_cast<DestT>(value);
93}
94
95template <typename SrcT, typename DestT>
96void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
97{
98 for (int comp = 0; comp < components; ++comp)
99 {
100 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
101 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
102 size_t offset = comp * 4;
103 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
104 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
105 }
106}
107
Jamie Madill192745a2016-12-22 15:58:21 -0500108// true if varying x has a higher priority in packing than y
109bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
110{
jchen10a9042d32017-03-17 08:50:45 +0800111 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
112 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
113 sh::ShaderVariable vx, vy;
114 const sh::ShaderVariable *px, *py;
115 if (x.isArrayElement())
116 {
117 vx = *x.varying;
118 vx.arraySize = 0;
119 px = &vx;
120 }
121 else
122 {
123 px = x.varying;
124 }
125
126 if (y.isArrayElement())
127 {
128 vy = *y.varying;
129 vy.arraySize = 0;
130 py = &vy;
131 }
132 else
133 {
134 py = y.varying;
135 }
136
137 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500138}
139
jchen1015015f72017-03-16 13:54:21 +0800140template <typename VarT>
141GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
142{
143 size_t subscript = GL_INVALID_INDEX;
144 std::string baseName = ParseResourceName(name, &subscript);
145
146 // The app is not allowed to specify array indices other than 0 for arrays of basic types
147 if (subscript != 0 && subscript != GL_INVALID_INDEX)
148 {
149 return GL_INVALID_INDEX;
150 }
151
152 for (size_t index = 0; index < list.size(); index++)
153 {
154 const VarT &resource = list[index];
155 if (resource.name == baseName)
156 {
157 if (resource.isArray() || subscript == GL_INVALID_INDEX)
158 {
159 return static_cast<GLuint>(index);
160 }
161 }
162 }
163
164 return GL_INVALID_INDEX;
165}
166
jchen10fd7c3b52017-03-21 15:36:03 +0800167void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
168{
169 ASSERT(bufSize > 0);
170 strncpy(buffer, string.c_str(), bufSize);
171 buffer[bufSize - 1] = '\0';
172
173 if (length)
174 {
175 *length = static_cast<GLsizei>(strlen(buffer));
176 }
177}
178
jchen10a9042d32017-03-17 08:50:45 +0800179bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
180{
181 size_t subscript = GL_INVALID_INDEX;
182 std::string baseName = ParseResourceName(name, &subscript);
183 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
184 {
185 size_t arrayIndex = GL_INVALID_INDEX;
186 std::string arrayName = ParseResourceName(*it, &arrayIndex);
187 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
188 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
189 {
190 return true;
191 }
192 }
193 return false;
194}
195
Jamie Madill62d31cb2015-09-11 13:25:51 -0400196} // anonymous namespace
197
Jamie Madill4a3c2342015-10-08 12:58:45 -0400198const char *const g_fakepath = "C:\\fakepath";
199
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400200InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000201{
202}
203
204InfoLog::~InfoLog()
205{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000206}
207
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209{
Jamie Madill23176ce2017-07-31 14:14:33 -0400210 if (!mLazyStream)
211 {
212 return 0;
213 }
214
215 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400216 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000217}
218
Geoff Lange1a27752015-10-05 13:16:04 -0400219void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000220{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400221 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000222
223 if (bufSize > 0)
224 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400225 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400226
Jamie Madill23176ce2017-07-31 14:14:33 -0400227 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000228 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400229 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
230 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000231 }
232
233 infoLog[index] = '\0';
234 }
235
236 if (length)
237 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400238 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000239 }
240}
241
242// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300243// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000244// messages, so lets remove all occurrences of this fake file path from the log.
245void InfoLog::appendSanitized(const char *message)
246{
Jamie Madill23176ce2017-07-31 14:14:33 -0400247 ensureInitialized();
248
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000249 std::string msg(message);
250
251 size_t found;
252 do
253 {
254 found = msg.find(g_fakepath);
255 if (found != std::string::npos)
256 {
257 msg.erase(found, strlen(g_fakepath));
258 }
259 }
260 while (found != std::string::npos);
261
Jamie Madill23176ce2017-07-31 14:14:33 -0400262 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000263}
264
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265void InfoLog::reset()
266{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000267}
268
Geoff Langd8605522016-04-13 10:19:12 -0400269VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000270{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500271}
272
Geoff Langd8605522016-04-13 10:19:12 -0400273VariableLocation::VariableLocation(const std::string &name,
274 unsigned int element,
275 unsigned int index)
276 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500277{
278}
279
Geoff Langd8605522016-04-13 10:19:12 -0400280void Program::Bindings::bindLocation(GLuint index, const std::string &name)
281{
282 mBindings[name] = index;
283}
284
285int Program::Bindings::getBinding(const std::string &name) const
286{
287 auto iter = mBindings.find(name);
288 return (iter != mBindings.end()) ? iter->second : -1;
289}
290
291Program::Bindings::const_iterator Program::Bindings::begin() const
292{
293 return mBindings.begin();
294}
295
296Program::Bindings::const_iterator Program::Bindings::end() const
297{
298 return mBindings.end();
299}
300
Jamie Madill48ef11b2016-04-27 15:21:52 -0400301ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500302 : mLabel(),
303 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400304 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300305 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500306 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500307 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800308 mImageUniformRange(0, 0),
309 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300310 mBinaryRetrieveableHint(false),
311 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400312{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300313 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400314}
315
Jamie Madill48ef11b2016-04-27 15:21:52 -0400316ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400317{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500318 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400319}
320
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500322{
323 return mLabel;
324}
325
Jamie Madill48ef11b2016-04-27 15:21:52 -0400326GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400327{
328 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800329 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400330
331 for (size_t location = 0; location < mUniformLocations.size(); ++location)
332 {
333 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400334 if (!uniformLocation.used)
335 {
336 continue;
337 }
338
339 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400340
341 if (uniform.name == baseName)
342 {
Geoff Langd8605522016-04-13 10:19:12 -0400343 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400344 {
Geoff Langd8605522016-04-13 10:19:12 -0400345 if (uniformLocation.element == subscript ||
346 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
347 {
348 return static_cast<GLint>(location);
349 }
350 }
351 else
352 {
353 if (subscript == GL_INVALID_INDEX)
354 {
355 return static_cast<GLint>(location);
356 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400357 }
358 }
359 }
360
361 return -1;
362}
363
Jamie Madille7d84322017-01-10 18:21:59 -0500364GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400365{
jchen1015015f72017-03-16 13:54:21 +0800366 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400367}
368
Jamie Madille7d84322017-01-10 18:21:59 -0500369GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
370{
371 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
372 return mUniformLocations[location].index;
373}
374
375Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
376{
377 GLuint index = getUniformIndexFromLocation(location);
378 if (!isSamplerUniformIndex(index))
379 {
380 return Optional<GLuint>::Invalid();
381 }
382
383 return getSamplerIndexFromUniformIndex(index);
384}
385
386bool ProgramState::isSamplerUniformIndex(GLuint index) const
387{
Jamie Madill982f6e02017-06-07 14:33:04 -0400388 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500389}
390
391GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
392{
393 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400394 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500395}
396
Jamie Madill34ca4f52017-06-13 11:49:39 -0400397GLuint ProgramState::getAttributeLocation(const std::string &name) const
398{
399 for (const sh::Attribute &attribute : mAttributes)
400 {
401 if (attribute.name == name)
402 {
403 return attribute.location;
404 }
405 }
406
407 return static_cast<GLuint>(-1);
408}
409
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500410Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400411 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400412 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500413 mLinked(false),
414 mDeleteStatus(false),
415 mRefCount(0),
416 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500417 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500418{
419 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000420
Geoff Lang7dd2e102014-11-10 15:19:26 -0500421 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
424Program::~Program()
425{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400426 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427}
428
Jamie Madill4928b7c2017-06-20 12:57:39 -0400429void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500430{
431 if (mState.mAttachedVertexShader != nullptr)
432 {
433 mState.mAttachedVertexShader->release(context);
434 mState.mAttachedVertexShader = nullptr;
435 }
436
437 if (mState.mAttachedFragmentShader != nullptr)
438 {
439 mState.mAttachedFragmentShader->release(context);
440 mState.mAttachedFragmentShader = nullptr;
441 }
442
443 if (mState.mAttachedComputeShader != nullptr)
444 {
445 mState.mAttachedComputeShader->release(context);
446 mState.mAttachedComputeShader = nullptr;
447 }
448
Jamie Madillc564c072017-06-01 12:45:42 -0400449 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400450
451 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
452 !mState.mAttachedComputeShader);
453 SafeDelete(mProgram);
454
455 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500456}
457
Geoff Lang70d0f492015-12-10 17:45:46 -0500458void Program::setLabel(const std::string &label)
459{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400460 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500461}
462
463const std::string &Program::getLabel() const
464{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400465 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500466}
467
Jamie Madillef300b12016-10-07 15:12:09 -0400468void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300470 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300472 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473 {
Jamie Madillef300b12016-10-07 15:12:09 -0400474 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300475 mState.mAttachedVertexShader = shader;
476 mState.mAttachedVertexShader->addRef();
477 break;
478 }
479 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480 {
Jamie Madillef300b12016-10-07 15:12:09 -0400481 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300482 mState.mAttachedFragmentShader = shader;
483 mState.mAttachedFragmentShader->addRef();
484 break;
485 }
486 case GL_COMPUTE_SHADER:
487 {
Jamie Madillef300b12016-10-07 15:12:09 -0400488 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300489 mState.mAttachedComputeShader = shader;
490 mState.mAttachedComputeShader->addRef();
491 break;
492 }
493 default:
494 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496}
497
Jamie Madillc1d770e2017-04-13 17:31:24 -0400498void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300500 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300502 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400504 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500505 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300506 mState.mAttachedVertexShader = nullptr;
507 break;
508 }
509 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400511 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500512 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300513 mState.mAttachedFragmentShader = nullptr;
514 break;
515 }
516 case GL_COMPUTE_SHADER:
517 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400518 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500519 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300520 mState.mAttachedComputeShader = nullptr;
521 break;
522 }
523 default:
524 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526}
527
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000528int Program::getAttachedShadersCount() const
529{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300530 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
531 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000532}
533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534void Program::bindAttributeLocation(GLuint index, const char *name)
535{
Geoff Langd8605522016-04-13 10:19:12 -0400536 mAttributeBindings.bindLocation(index, name);
537}
538
539void Program::bindUniformLocation(GLuint index, const char *name)
540{
541 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800542 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543}
544
Sami Väisänen46eaa942016-06-29 10:26:37 +0300545void Program::bindFragmentInputLocation(GLint index, const char *name)
546{
547 mFragmentInputBindings.bindLocation(index, name);
548}
549
Jamie Madillbd044ed2017-06-05 12:59:21 -0400550BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300551{
552 BindingInfo ret;
553 ret.type = GL_NONE;
554 ret.valid = false;
555
Jamie Madillbd044ed2017-06-05 12:59:21 -0400556 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300557 ASSERT(fragmentShader);
558
559 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400560 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300561
562 for (const auto &binding : mFragmentInputBindings)
563 {
564 if (binding.second != static_cast<GLuint>(index))
565 continue;
566
567 ret.valid = true;
568
569 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400570 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300571
572 for (const auto &in : inputs)
573 {
574 if (in.name == originalName)
575 {
576 if (in.isArray())
577 {
578 // The client wants to bind either "name" or "name[0]".
579 // GL ES 3.1 spec refers to active array names with language such as:
580 // "if the string identifies the base name of an active array, where the
581 // string would exactly match the name of the variable if the suffix "[0]"
582 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400583 if (arrayIndex == GL_INVALID_INDEX)
584 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300585
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400586 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300587 }
588 else
589 {
590 ret.name = in.mappedName;
591 }
592 ret.type = in.type;
593 return ret;
594 }
595 }
596 }
597
598 return ret;
599}
600
Jamie Madillbd044ed2017-06-05 12:59:21 -0400601void Program::pathFragmentInputGen(const Context *context,
602 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300603 GLenum genMode,
604 GLint components,
605 const GLfloat *coeffs)
606{
607 // If the location is -1 then the command is silently ignored
608 if (index == -1)
609 return;
610
Jamie Madillbd044ed2017-06-05 12:59:21 -0400611 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300612
613 // If the input doesn't exist then then the command is silently ignored
614 // This could happen through optimization for example, the shader translator
615 // decides that a variable is not actually being used and optimizes it away.
616 if (binding.name.empty())
617 return;
618
619 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
620}
621
Martin Radev4c4c8e72016-08-04 12:25:34 +0300622// The attached shaders are checked for linking errors by matching up their variables.
623// Uniform, input and output variables get collected.
624// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500625Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000626{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500627 const auto &data = context->getContextState();
628
Jamie Madill6c58b062017-08-01 13:44:25 -0400629 auto *platform = ANGLEPlatformCurrent();
630 double startTime = platform->currentTime(platform);
631
Jamie Madill6c1f6712017-02-14 19:08:04 -0500632 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000633
Jamie Madill32447362017-06-28 14:53:52 -0400634 ProgramHash programHash;
635 auto *cache = context->getMemoryProgramCache();
636 if (cache)
637 {
638 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400639 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400640 }
641
642 if (mLinked)
643 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400644 double delta = platform->currentTime(platform) - startTime;
645 int us = static_cast<int>(delta * 1000000.0);
646 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400647 return NoError();
648 }
649
650 // Cache load failed, fall through to normal linking.
651 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000652 mInfoLog.reset();
653
Martin Radev4c4c8e72016-08-04 12:25:34 +0300654 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500655
Jamie Madill192745a2016-12-22 15:58:21 -0500656 auto vertexShader = mState.mAttachedVertexShader;
657 auto fragmentShader = mState.mAttachedFragmentShader;
658 auto computeShader = mState.mAttachedComputeShader;
659
660 bool isComputeShaderAttached = (computeShader != nullptr);
661 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662 // Check whether we both have a compute and non-compute shaders attached.
663 // If there are of both types attached, then linking should fail.
664 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
665 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500666 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300667 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
668 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400669 }
670
Jamie Madill192745a2016-12-22 15:58:21 -0500671 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500672 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400673 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300674 {
675 mInfoLog << "Attached compute shader is not compiled.";
676 return NoError();
677 }
Jamie Madill192745a2016-12-22 15:58:21 -0500678 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679
Jamie Madillbd044ed2017-06-05 12:59:21 -0400680 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300681
682 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
683 // If the work group size is not specified, a link time error should occur.
684 if (!mState.mComputeShaderLocalSize.isDeclared())
685 {
686 mInfoLog << "Work group size is not specified.";
687 return NoError();
688 }
689
Jamie Madillbd044ed2017-06-05 12:59:21 -0400690 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300691 {
692 return NoError();
693 }
694
Jamie Madillbd044ed2017-06-05 12:59:21 -0400695 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300696 {
697 return NoError();
698 }
699
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500700 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400701 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500702 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500704 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 }
706 }
707 else
708 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400709 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300710 {
711 return NoError();
712 }
Jamie Madill192745a2016-12-22 15:58:21 -0500713 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300714
Jamie Madillbd044ed2017-06-05 12:59:21 -0400715 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300716 {
717 return NoError();
718 }
Jamie Madill192745a2016-12-22 15:58:21 -0500719 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720
Jamie Madillbd044ed2017-06-05 12:59:21 -0400721 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300722 {
723 mInfoLog << "Fragment shader version does not match vertex shader version.";
724 return NoError();
725 }
726
Jamie Madillbd044ed2017-06-05 12:59:21 -0400727 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300728 {
729 return NoError();
730 }
731
Jamie Madillbd044ed2017-06-05 12:59:21 -0400732 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733 {
734 return NoError();
735 }
736
Jamie Madillbd044ed2017-06-05 12:59:21 -0400737 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300738 {
739 return NoError();
740 }
741
Jamie Madillbd044ed2017-06-05 12:59:21 -0400742 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300743 {
744 return NoError();
745 }
746
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400747 if (!linkValidateGlobalNames(context, mInfoLog))
748 {
749 return NoError();
750 }
751
Jamie Madillbd044ed2017-06-05 12:59:21 -0400752 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753
jchen10a9042d32017-03-17 08:50:45 +0800754 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300755 {
756 return NoError();
757 }
758
Martin Radev7cf61662017-07-26 17:10:53 +0300759 mState.mNumViews = vertexShader->getNumViews(context);
760
Jamie Madillbd044ed2017-06-05 12:59:21 -0400761 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300762
Jamie Madill192745a2016-12-22 15:58:21 -0500763 // Validate we can pack the varyings.
764 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
765
766 // Map the varyings to the register file
767 // In WebGL, we use a slightly different handling for packing variables.
768 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
769 : PackMode::ANGLE_RELAXED;
770 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
771 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
772 mState.getTransformFeedbackVaryingNames()))
773 {
774 return NoError();
775 }
776
Jamie Madillc564c072017-06-01 12:45:42 -0400777 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500778 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300779 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500780 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300781 }
782
783 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500784 }
785
jchen10eaef1e52017-06-13 10:44:11 +0800786 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400787 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400788
jchen10eaef1e52017-06-13 10:44:11 +0800789 setUniformValuesFromBindingQualifiers();
790
Jamie 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{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001336 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1337 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338}
1339
1340void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1341{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001342 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1343 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344}
1345
1346void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1347{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001348 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1349 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350}
1351
1352void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1353{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001354 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1355 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356}
1357
1358void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1359{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001360 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1361 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362}
1363
1364void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1365{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001366 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1367 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368}
1369
1370void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1371{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001372 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1373 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
1376void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1377{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001378 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1379 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380}
1381
1382void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1383{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001384 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1385 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386}
1387
1388void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1389{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001390 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1391 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392}
1393
1394void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1395{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001396 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1397 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398}
1399
1400void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1401{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001402 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1403 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404}
1405
1406void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1407{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001408 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1409 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410}
1411
1412void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1413{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001414 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1415 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416}
1417
1418void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1419{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001420 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1421 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422}
1423
1424void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1425{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001426 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1427 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428}
1429
1430void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1431{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001432 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1433 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434}
1435
1436void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1437{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001438 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1439 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440}
1441
1442void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1443{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001444 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1445 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446}
1447
1448void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1449{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001450 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1451 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001452}
1453
1454void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1455{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001456 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1457 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458}
1459
Jamie Madill54164b02017-08-28 15:17:37 -04001460void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461{
Jamie Madill54164b02017-08-28 15:17:37 -04001462 const auto &uniformLocation = mState.getUniformLocations()[location];
1463 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1464
1465 GLenum nativeType = gl::VariableComponentType(uniform.type);
1466 if (nativeType == GL_FLOAT)
1467 {
1468 mProgram->getUniformfv(context, location, v);
1469 }
1470 else
1471 {
1472 getUniformInternal(context, v, location, nativeType,
1473 gl::VariableComponentCount(uniform.type));
1474 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475}
1476
Jamie Madill54164b02017-08-28 15:17:37 -04001477void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478{
Jamie Madill54164b02017-08-28 15:17:37 -04001479 const auto &uniformLocation = mState.getUniformLocations()[location];
1480 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1481
1482 GLenum nativeType = gl::VariableComponentType(uniform.type);
1483 if (nativeType == GL_INT || nativeType == GL_BOOL)
1484 {
1485 mProgram->getUniformiv(context, location, v);
1486 }
1487 else
1488 {
1489 getUniformInternal(context, v, location, nativeType,
1490 gl::VariableComponentCount(uniform.type));
1491 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492}
1493
Jamie Madill54164b02017-08-28 15:17:37 -04001494void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495{
Jamie Madill54164b02017-08-28 15:17:37 -04001496 const auto &uniformLocation = mState.getUniformLocations()[location];
1497 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1498
1499 GLenum nativeType = gl::VariableComponentType(uniform.type);
1500 if (nativeType == GL_UNSIGNED_INT)
1501 {
1502 mProgram->getUniformuiv(context, location, v);
1503 }
1504 else
1505 {
1506 getUniformInternal(context, v, location, nativeType,
1507 gl::VariableComponentCount(uniform.type));
1508 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509}
1510
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511void Program::flagForDeletion()
1512{
1513 mDeleteStatus = true;
1514}
1515
1516bool Program::isFlaggedForDeletion() const
1517{
1518 return mDeleteStatus;
1519}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001520
Brandon Jones43a53e22014-08-28 16:23:22 -07001521void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001522{
1523 mInfoLog.reset();
1524
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001526 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001527 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001528 }
1529 else
1530 {
Jamie Madillf6113162015-05-07 11:49:21 -04001531 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001532 }
1533}
1534
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1536{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001537 // Skip cache if we're using an infolog, so we get the full error.
1538 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1539 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1540 {
1541 return mCachedValidateSamplersResult.value();
1542 }
1543
1544 if (mTextureUnitTypesCache.empty())
1545 {
1546 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1547 }
1548 else
1549 {
1550 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1551 }
1552
1553 // if any two active samplers in a program are of different types, but refer to the same
1554 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1555 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001556 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001557 {
Jamie Madill54164b02017-08-28 15:17:37 -04001558 if (samplerBinding.unreferenced)
1559 continue;
1560
Jamie Madille7d84322017-01-10 18:21:59 -05001561 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001562
Jamie Madille7d84322017-01-10 18:21:59 -05001563 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001564 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001565 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1566 {
1567 if (infoLog)
1568 {
1569 (*infoLog) << "Sampler uniform (" << textureUnit
1570 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1571 << caps.maxCombinedTextureImageUnits << ")";
1572 }
1573
1574 mCachedValidateSamplersResult = false;
1575 return false;
1576 }
1577
1578 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1579 {
1580 if (textureType != mTextureUnitTypesCache[textureUnit])
1581 {
1582 if (infoLog)
1583 {
1584 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1585 "image unit ("
1586 << textureUnit << ").";
1587 }
1588
1589 mCachedValidateSamplersResult = false;
1590 return false;
1591 }
1592 }
1593 else
1594 {
1595 mTextureUnitTypesCache[textureUnit] = textureType;
1596 }
1597 }
1598 }
1599
1600 mCachedValidateSamplersResult = true;
1601 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001602}
1603
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001604bool Program::isValidated() const
1605{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606 return mValidated;
1607}
1608
Geoff Lange1a27752015-10-05 13:16:04 -04001609GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001610{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001611 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001612}
1613
1614void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1615{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001616 ASSERT(
1617 uniformBlockIndex <
1618 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619
Jamie Madill48ef11b2016-04-27 15:21:52 -04001620 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621
1622 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001623 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 std::string string = uniformBlock.name;
1625
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001628 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629 }
jchen10fd7c3b52017-03-21 15:36:03 +08001630 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001631 }
1632}
1633
Geoff Lange1a27752015-10-05 13:16:04 -04001634GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001635{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636 int maxLength = 0;
1637
1638 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001639 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001640 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001641 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1642 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001643 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001644 if (!uniformBlock.name.empty())
1645 {
jchen10af713a22017-04-19 09:10:56 +08001646 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1647 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648 }
1649 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001650 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651
1652 return maxLength;
1653}
1654
Geoff Lange1a27752015-10-05 13:16:04 -04001655GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001658 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659
Jamie Madill48ef11b2016-04-27 15:21:52 -04001660 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001661 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1662 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001663 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664 if (uniformBlock.name == baseName)
1665 {
1666 const bool arrayElementZero =
1667 (subscript == GL_INVALID_INDEX &&
1668 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1669 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1670 {
1671 return blockIndex;
1672 }
1673 }
1674 }
1675
1676 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001677}
1678
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001680{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001681 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1682 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001683}
1684
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001685void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1686{
jchen107a20b972017-06-13 14:25:26 +08001687 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001688 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001689 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001690}
1691
1692GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1693{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001694 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001695}
1696
Geoff Lang48dcae72014-02-05 16:28:24 -05001697void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1698{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001699 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001700 for (GLsizei i = 0; i < count; i++)
1701 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001702 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001703 }
1704
Jamie Madill48ef11b2016-04-27 15:21:52 -04001705 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001706}
1707
1708void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1709{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001710 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001711 {
jchen10a9042d32017-03-17 08:50:45 +08001712 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1713 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1714 std::string varName = var.nameWithArrayIndex();
1715 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001716 if (length)
1717 {
1718 *length = lastNameIdx;
1719 }
1720 if (size)
1721 {
jchen10a9042d32017-03-17 08:50:45 +08001722 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001723 }
1724 if (type)
1725 {
jchen10a9042d32017-03-17 08:50:45 +08001726 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001727 }
1728 if (name)
1729 {
jchen10a9042d32017-03-17 08:50:45 +08001730 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001731 name[lastNameIdx] = '\0';
1732 }
1733 }
1734}
1735
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001736GLsizei Program::getTransformFeedbackVaryingCount() const
1737{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001738 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001739 {
jchen10a9042d32017-03-17 08:50:45 +08001740 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001741 }
1742 else
1743 {
1744 return 0;
1745 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001746}
1747
1748GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1749{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001750 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001751 {
1752 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001753 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001754 {
jchen10a9042d32017-03-17 08:50:45 +08001755 maxSize =
1756 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001757 }
1758
1759 return maxSize;
1760 }
1761 else
1762 {
1763 return 0;
1764 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001765}
1766
1767GLenum Program::getTransformFeedbackBufferMode() const
1768{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001769 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770}
1771
Jamie Madillbd044ed2017-06-05 12:59:21 -04001772bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001774 Shader *vertexShader = mState.mAttachedVertexShader;
1775 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001776
Jamie Madillbd044ed2017-06-05 12:59:21 -04001777 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001778
Jamie Madillbd044ed2017-06-05 12:59:21 -04001779 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1780 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781
Sami Väisänen46eaa942016-06-29 10:26:37 +03001782 std::map<GLuint, std::string> staticFragmentInputLocations;
1783
Jamie Madill4cff2472015-08-21 16:53:18 -04001784 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001786 bool matched = false;
1787
1788 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001789 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001790 {
1791 continue;
1792 }
1793
Jamie Madill4cff2472015-08-21 16:53:18 -04001794 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001796 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001798 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001799 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001800 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801 {
1802 return false;
1803 }
1804
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805 matched = true;
1806 break;
1807 }
1808 }
1809
1810 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001811 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001813 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814 return false;
1815 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001816
1817 // Check for aliased path rendering input bindings (if any).
1818 // If more than one binding refer statically to the same
1819 // location the link must fail.
1820
1821 if (!output.staticUse)
1822 continue;
1823
1824 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1825 if (inputBinding == -1)
1826 continue;
1827
1828 const auto it = staticFragmentInputLocations.find(inputBinding);
1829 if (it == std::end(staticFragmentInputLocations))
1830 {
1831 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1832 }
1833 else
1834 {
1835 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1836 << it->second;
1837 return false;
1838 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 }
1840
Jamie Madillbd044ed2017-06-05 12:59:21 -04001841 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001842 {
1843 return false;
1844 }
1845
Jamie Madillada9ecc2015-08-17 12:53:37 -04001846 // TODO(jmadill): verify no unmatched vertex varyings?
1847
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 return true;
1849}
1850
Jamie Madillbd044ed2017-06-05 12:59:21 -04001851bool Program::linkUniforms(const Context *context,
1852 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001853 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001854{
Olli Etuahob78707c2017-03-09 15:03:11 +00001855 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001856 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001857 {
1858 return false;
1859 }
1860
Olli Etuahob78707c2017-03-09 15:03:11 +00001861 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001862
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001863 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001864
jchen10eaef1e52017-06-13 10:44:11 +08001865 if (!linkAtomicCounterBuffers())
1866 {
1867 return false;
1868 }
1869
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001870 return true;
1871}
1872
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001873void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001874{
Jamie Madill982f6e02017-06-07 14:33:04 -04001875 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1876 unsigned int low = high;
1877
jchen10eaef1e52017-06-13 10:44:11 +08001878 for (auto counterIter = mState.mUniforms.rbegin();
1879 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1880 {
1881 --low;
1882 }
1883
1884 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1885
1886 high = low;
1887
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001888 for (auto imageIter = mState.mUniforms.rbegin();
1889 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1890 {
1891 --low;
1892 }
1893
1894 mState.mImageUniformRange = RangeUI(low, high);
1895
1896 // If uniform is a image type, insert it into the mImageBindings array.
1897 for (unsigned int imageIndex : mState.mImageUniformRange)
1898 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001899 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1900 // cannot load values into a uniform defined as an image. if declare without a
1901 // binding qualifier, any uniform image variable (include all elements of
1902 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001903 auto &imageUniform = mState.mUniforms[imageIndex];
1904 if (imageUniform.binding == -1)
1905 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001906 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001907 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001908 else
1909 {
1910 mState.mImageBindings.emplace_back(
1911 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1912 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001913 }
1914
1915 high = low;
1916
1917 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001918 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001919 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001920 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001921 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001922
1923 mState.mSamplerUniformRange = RangeUI(low, high);
1924
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001925 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001926 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001927 {
1928 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1929 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1930 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001931 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001932 }
1933}
1934
jchen10eaef1e52017-06-13 10:44:11 +08001935bool Program::linkAtomicCounterBuffers()
1936{
1937 for (unsigned int index : mState.mAtomicCounterUniformRange)
1938 {
1939 auto &uniform = mState.mUniforms[index];
1940 bool found = false;
1941 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1942 ++bufferIndex)
1943 {
1944 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1945 if (buffer.binding == uniform.binding)
1946 {
1947 buffer.memberIndexes.push_back(index);
1948 uniform.bufferIndex = bufferIndex;
1949 found = true;
1950 break;
1951 }
1952 }
1953 if (!found)
1954 {
1955 AtomicCounterBuffer atomicCounterBuffer;
1956 atomicCounterBuffer.binding = uniform.binding;
1957 atomicCounterBuffer.memberIndexes.push_back(index);
1958 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1959 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1960 }
1961 }
1962 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1963 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1964
1965 return true;
1966}
1967
Martin Radev4c4c8e72016-08-04 12:25:34 +03001968bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1969 const std::string &uniformName,
1970 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001971 const sh::InterfaceBlockField &fragmentUniform,
1972 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001973{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001974 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1975 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1976 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 {
1978 return false;
1979 }
1980
1981 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1982 {
Jamie Madillf6113162015-05-07 11:49:21 -04001983 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984 return false;
1985 }
1986
1987 return true;
1988}
1989
Jamie Madilleb979bf2016-11-15 12:28:46 -05001990// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001991bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001993 const ContextState &data = context->getContextState();
1994 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001995
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001997 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001998 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001999
2000 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002001 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002002 {
Jamie Madillf6113162015-05-07 11:49:21 -04002003 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002004 return false;
2005 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002007 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002008
Jamie Madillc349ec02015-08-21 16:53:12 -04002009 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002010 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002011 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002012 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002013 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002014 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002015 attribute.location = bindingLocation;
2016 }
2017
2018 if (attribute.location != -1)
2019 {
2020 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002021 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022
Jamie Madill63805b42015-08-25 13:17:39 -04002023 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002024 {
Jamie Madillf6113162015-05-07 11:49:21 -04002025 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002026 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002027
2028 return false;
2029 }
2030
Jamie Madill63805b42015-08-25 13:17:39 -04002031 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032 {
Jamie Madill63805b42015-08-25 13:17:39 -04002033 const int regLocation = attribute.location + reg;
2034 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035
2036 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002037 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002038 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002040 // TODO(jmadill): fix aliasing on ES2
2041 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002043 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002044 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045 return false;
2046 }
2047 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002048 else
2049 {
Jamie Madill63805b42015-08-25 13:17:39 -04002050 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002051 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002052
Jamie Madill63805b42015-08-25 13:17:39 -04002053 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054 }
2055 }
2056 }
2057
2058 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002059 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002060 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002061 // Not set by glBindAttribLocation or by location layout qualifier
2062 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 {
Jamie Madill63805b42015-08-25 13:17:39 -04002064 int regs = VariableRegisterCount(attribute.type);
2065 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066
Jamie Madill63805b42015-08-25 13:17:39 -04002067 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 {
Jamie Madillf6113162015-05-07 11:49:21 -04002069 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 }
2072
Jamie Madillc349ec02015-08-21 16:53:12 -04002073 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 }
2075 }
2076
Jamie Madill48ef11b2016-04-27 15:21:52 -04002077 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
Jamie Madill63805b42015-08-25 13:17:39 -04002079 ASSERT(attribute.location != -1);
2080 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002081
Jamie Madill63805b42015-08-25 13:17:39 -04002082 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002084 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 }
2086 }
2087
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088 return true;
2089}
2090
Martin Radev4c4c8e72016-08-04 12:25:34 +03002091bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2092 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2093 const std::string &errorMessage,
2094 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002096 GLuint blockCount = 0;
2097 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002099 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002100 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002101 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002102 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002103 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002104 return false;
2105 }
2106 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002108 return true;
2109}
Jamie Madille473dee2015-08-18 14:49:01 -04002110
Martin Radev4c4c8e72016-08-04 12:25:34 +03002111bool Program::validateVertexAndFragmentInterfaceBlocks(
2112 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2113 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002114 InfoLog &infoLog,
2115 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002116{
2117 // Check that interface blocks defined in the vertex and fragment shaders are identical
2118 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2119 UniformBlockMap linkedUniformBlocks;
2120
2121 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2122 {
2123 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2124 }
2125
Jamie Madille473dee2015-08-18 14:49:01 -04002126 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 {
Jamie Madille473dee2015-08-18 14:49:01 -04002128 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 if (entry != linkedUniformBlocks.end())
2130 {
2131 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002132 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2133 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 {
2135 return false;
2136 }
2137 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002138 }
2139 return true;
2140}
Jamie Madille473dee2015-08-18 14:49:01 -04002141
Jamie Madillbd044ed2017-06-05 12:59:21 -04002142bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002143{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002144 const auto &caps = context->getCaps();
2145
Martin Radev4c4c8e72016-08-04 12:25:34 +03002146 if (mState.mAttachedComputeShader)
2147 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002148 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002149 const auto &computeInterfaceBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002150
2151 if (!validateUniformBlocksCount(
2152 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2153 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2154 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002156 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002158 return true;
2159 }
2160
Jamie Madillbd044ed2017-06-05 12:59:21 -04002161 Shader &vertexShader = *mState.mAttachedVertexShader;
2162 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002163
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002164 const auto &vertexInterfaceBlocks = vertexShader.getUniformBlocks(context);
2165 const auto &fragmentInterfaceBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002166
2167 if (!validateUniformBlocksCount(
2168 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2169 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2170 {
2171 return false;
2172 }
2173 if (!validateUniformBlocksCount(
2174 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2175 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2176 infoLog))
2177 {
2178
2179 return false;
2180 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002181
2182 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002183 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002184 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185 {
2186 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187 }
Jamie Madille473dee2015-08-18 14:49:01 -04002188
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189 return true;
2190}
2191
Jamie Madilla2c74982016-12-12 11:20:42 -05002192bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002193 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002194 const sh::InterfaceBlock &fragmentInterfaceBlock,
2195 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002196{
2197 const char* blockName = vertexInterfaceBlock.name.c_str();
2198 // validate blocks for the same member types
2199 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2200 {
Jamie Madillf6113162015-05-07 11:49:21 -04002201 infoLog << "Types for interface block '" << blockName
2202 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203 return false;
2204 }
2205 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2206 {
Jamie Madillf6113162015-05-07 11:49:21 -04002207 infoLog << "Array sizes differ for interface block '" << blockName
2208 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209 return false;
2210 }
jchen10af713a22017-04-19 09:10:56 +08002211 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2212 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2213 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214 {
Jamie Madillf6113162015-05-07 11:49:21 -04002215 infoLog << "Layout qualifiers differ for interface block '" << blockName
2216 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002217 return false;
2218 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002219 const unsigned int numBlockMembers =
2220 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2222 {
2223 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2224 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2225 if (vertexMember.name != fragmentMember.name)
2226 {
Jamie Madillf6113162015-05-07 11:49:21 -04002227 infoLog << "Name mismatch for field " << blockMemberIndex
2228 << " of interface block '" << blockName
2229 << "': (in vertex: '" << vertexMember.name
2230 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002231 return false;
2232 }
2233 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002234 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2235 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 {
2237 return false;
2238 }
2239 }
2240 return true;
2241}
2242
2243bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2244 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2245{
2246 if (vertexVariable.type != fragmentVariable.type)
2247 {
Jamie Madillf6113162015-05-07 11:49:21 -04002248 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249 return false;
2250 }
2251 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2252 {
Jamie Madillf6113162015-05-07 11:49:21 -04002253 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002254 return false;
2255 }
2256 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2257 {
Jamie Madillf6113162015-05-07 11:49:21 -04002258 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 return false;
2260 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002261 if (vertexVariable.structName != fragmentVariable.structName)
2262 {
2263 infoLog << "Structure names for " << variableName
2264 << " differ between vertex and fragment shaders";
2265 return false;
2266 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002267
2268 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2269 {
Jamie Madillf6113162015-05-07 11:49:21 -04002270 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271 return false;
2272 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002273 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002274 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2275 {
2276 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2277 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2278
2279 if (vertexMember.name != fragmentMember.name)
2280 {
Jamie Madillf6113162015-05-07 11:49:21 -04002281 infoLog << "Name mismatch for field '" << memberIndex
2282 << "' of " << variableName
2283 << ": (in vertex: '" << vertexMember.name
2284 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002285 return false;
2286 }
2287
2288 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2289 vertexMember.name + "'";
2290
2291 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2292 {
2293 return false;
2294 }
2295 }
2296
2297 return true;
2298}
2299
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002300bool Program::linkValidateVaryings(InfoLog &infoLog,
2301 const std::string &varyingName,
2302 const sh::Varying &vertexVarying,
2303 const sh::Varying &fragmentVarying,
2304 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002305{
2306 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2307 {
2308 return false;
2309 }
2310
Jamie Madille9cc4692015-02-19 16:00:13 -05002311 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002312 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002313 infoLog << "Interpolation types for " << varyingName
2314 << " differ between vertex and fragment shaders.";
2315 return false;
2316 }
2317
2318 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2319 {
2320 infoLog << "Invariance for " << varyingName
2321 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 return false;
2323 }
2324
2325 return true;
2326}
2327
Jamie Madillbd044ed2017-06-05 12:59:21 -04002328bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002329{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002330 Shader *vertexShader = mState.mAttachedVertexShader;
2331 Shader *fragmentShader = mState.mAttachedFragmentShader;
2332 const auto &vertexVaryings = vertexShader->getVaryings(context);
2333 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2334 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002335
2336 if (shaderVersion != 100)
2337 {
2338 // Only ESSL 1.0 has restrictions on matching input and output invariance
2339 return true;
2340 }
2341
2342 bool glPositionIsInvariant = false;
2343 bool glPointSizeIsInvariant = false;
2344 bool glFragCoordIsInvariant = false;
2345 bool glPointCoordIsInvariant = false;
2346
2347 for (const sh::Varying &varying : vertexVaryings)
2348 {
2349 if (!varying.isBuiltIn())
2350 {
2351 continue;
2352 }
2353 if (varying.name.compare("gl_Position") == 0)
2354 {
2355 glPositionIsInvariant = varying.isInvariant;
2356 }
2357 else if (varying.name.compare("gl_PointSize") == 0)
2358 {
2359 glPointSizeIsInvariant = varying.isInvariant;
2360 }
2361 }
2362
2363 for (const sh::Varying &varying : fragmentVaryings)
2364 {
2365 if (!varying.isBuiltIn())
2366 {
2367 continue;
2368 }
2369 if (varying.name.compare("gl_FragCoord") == 0)
2370 {
2371 glFragCoordIsInvariant = varying.isInvariant;
2372 }
2373 else if (varying.name.compare("gl_PointCoord") == 0)
2374 {
2375 glPointCoordIsInvariant = varying.isInvariant;
2376 }
2377 }
2378
2379 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2380 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2381 // Not requiring invariance to match is supported by:
2382 // dEQP, WebGL CTS, Nexus 5X GLES
2383 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2384 {
2385 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2386 "declared invariant.";
2387 return false;
2388 }
2389 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2390 {
2391 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2392 "declared invariant.";
2393 return false;
2394 }
2395
2396 return true;
2397}
2398
jchen10a9042d32017-03-17 08:50:45 +08002399bool Program::linkValidateTransformFeedback(const gl::Context *context,
2400 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002401 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002402 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403{
2404 size_t totalComponents = 0;
2405
Jamie Madillccdf74b2015-08-18 10:46:12 -04002406 std::set<std::string> uniqueNames;
2407
Jamie Madill48ef11b2016-04-27 15:21:52 -04002408 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002409 {
2410 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002411 size_t subscript = GL_INVALID_INDEX;
2412 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2413
Jamie Madill192745a2016-12-22 15:58:21 -05002414 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415 {
Jamie Madill192745a2016-12-22 15:58:21 -05002416 const sh::Varying *varying = ref.second.get();
2417
jchen10a9042d32017-03-17 08:50:45 +08002418 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002419 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002420 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002422 infoLog << "Two transform feedback varyings specify the same output variable ("
2423 << tfVaryingName << ").";
2424 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002425 }
jchen10a9042d32017-03-17 08:50:45 +08002426 if (context->getClientVersion() >= Version(3, 1))
2427 {
2428 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2429 {
2430 infoLog
2431 << "Two transform feedback varyings include the same array element ("
2432 << tfVaryingName << ").";
2433 return false;
2434 }
2435 }
2436 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002437 {
2438 infoLog << "Capture of arrays is undefined and not supported.";
2439 return false;
2440 }
2441
jchen10a9042d32017-03-17 08:50:45 +08002442 uniqueNames.insert(tfVaryingName);
2443
Jamie Madillccdf74b2015-08-18 10:46:12 -04002444 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002445 size_t elementCount =
2446 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2447 : 1);
2448 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002449 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 componentCount > caps.maxTransformFeedbackSeparateComponents)
2451 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002452 infoLog << "Transform feedback varying's " << varying->name << " components ("
2453 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002454 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002455 return false;
2456 }
2457
2458 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002459 found = true;
2460 break;
2461 }
2462 }
jchen10a9042d32017-03-17 08:50:45 +08002463 if (context->getClientVersion() < Version(3, 1) &&
2464 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002465 {
Geoff Lang1a683462015-09-29 15:09:59 -04002466 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002467 return false;
2468 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002469 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2470 ASSERT(found);
2471 }
2472
Jamie Madill48ef11b2016-04-27 15:21:52 -04002473 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002474 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002475 {
Jamie Madillf6113162015-05-07 11:49:21 -04002476 infoLog << "Transform feedback varying total components (" << totalComponents
2477 << ") exceed the maximum interleaved components ("
2478 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002479 return false;
2480 }
2481
2482 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002483}
2484
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002485bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2486{
2487 const std::vector<sh::Uniform> &vertexUniforms =
2488 mState.mAttachedVertexShader->getUniforms(context);
2489 const std::vector<sh::Uniform> &fragmentUniforms =
2490 mState.mAttachedFragmentShader->getUniforms(context);
2491 const std::vector<sh::Attribute> &attributes =
2492 mState.mAttachedVertexShader->getActiveAttributes(context);
2493 for (const auto &attrib : attributes)
2494 {
2495 for (const auto &uniform : vertexUniforms)
2496 {
2497 if (uniform.name == attrib.name)
2498 {
2499 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2500 return false;
2501 }
2502 }
2503 for (const auto &uniform : fragmentUniforms)
2504 {
2505 if (uniform.name == attrib.name)
2506 {
2507 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2508 return false;
2509 }
2510 }
2511 }
2512 return true;
2513}
2514
Jamie Madill192745a2016-12-22 15:58:21 -05002515void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002516{
2517 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002518 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002519 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002520 {
jchen10a9042d32017-03-17 08:50:45 +08002521 size_t subscript = GL_INVALID_INDEX;
2522 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002523 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002524 {
Jamie Madill192745a2016-12-22 15:58:21 -05002525 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002526 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002527 {
jchen10a9042d32017-03-17 08:50:45 +08002528 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2529 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002530 break;
2531 }
2532 }
2533 }
2534}
2535
Jamie Madillbd044ed2017-06-05 12:59:21 -04002536Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002537{
Jamie Madill192745a2016-12-22 15:58:21 -05002538 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002539
Jamie Madillbd044ed2017-06-05 12:59:21 -04002540 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002541 {
Jamie Madill192745a2016-12-22 15:58:21 -05002542 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002543 }
2544
Jamie Madillbd044ed2017-06-05 12:59:21 -04002545 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002546 {
Jamie Madill192745a2016-12-22 15:58:21 -05002547 merged[varying.name].fragment = &varying;
2548 }
2549
2550 return merged;
2551}
2552
2553std::vector<PackedVarying> Program::getPackedVaryings(
2554 const Program::MergedVaryings &mergedVaryings) const
2555{
2556 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2557 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002558 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002559
2560 for (const auto &ref : mergedVaryings)
2561 {
2562 const sh::Varying *input = ref.second.vertex;
2563 const sh::Varying *output = ref.second.fragment;
2564
2565 // Only pack varyings that have a matched input or output, plus special builtins.
2566 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002567 {
Jamie Madill192745a2016-12-22 15:58:21 -05002568 // Will get the vertex shader interpolation by default.
2569 auto interpolation = ref.second.get()->interpolation;
2570
Olli Etuaho06a06f52017-07-12 12:22:15 +03002571 // Note that we lose the vertex shader static use information here. The data for the
2572 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002573 if (output->isStruct())
2574 {
2575 ASSERT(!output->isArray());
2576 for (const auto &field : output->fields)
2577 {
2578 ASSERT(!field.isStruct() && !field.isArray());
2579 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2580 }
2581 }
2582 else
2583 {
2584 packedVaryings.push_back(PackedVarying(*output, interpolation));
2585 }
2586 continue;
2587 }
2588
2589 // Keep Transform FB varyings in the merged list always.
2590 if (!input)
2591 {
2592 continue;
2593 }
2594
2595 for (const std::string &tfVarying : tfVaryings)
2596 {
jchen10a9042d32017-03-17 08:50:45 +08002597 size_t subscript = GL_INVALID_INDEX;
2598 std::string baseName = ParseResourceName(tfVarying, &subscript);
2599 if (uniqueFullNames.count(tfVarying) > 0)
2600 {
2601 continue;
2602 }
2603 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002604 {
2605 // Transform feedback for varying structs is underspecified.
2606 // See Khronos bug 9856.
2607 // TODO(jmadill): Figure out how to be spec-compliant here.
2608 if (!input->isStruct())
2609 {
2610 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2611 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002612 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2613 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002614 }
jchen10a9042d32017-03-17 08:50:45 +08002615 if (subscript == GL_INVALID_INDEX)
2616 {
2617 break;
2618 }
Jamie Madill192745a2016-12-22 15:58:21 -05002619 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002620 }
2621 }
2622
Jamie Madill192745a2016-12-22 15:58:21 -05002623 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2624
2625 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002626}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002627
Jamie Madillbd044ed2017-06-05 12:59:21 -04002628void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002629{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002630 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002631 ASSERT(fragmentShader != nullptr);
2632
Geoff Lange0cff192017-05-30 13:04:56 -04002633 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002634 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002635
2636 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002637 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002638 {
2639 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2640 outputVariable.name != "gl_FragData")
2641 {
2642 continue;
2643 }
2644
2645 unsigned int baseLocation =
2646 (outputVariable.location == -1 ? 0u
2647 : static_cast<unsigned int>(outputVariable.location));
2648 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2649 elementIndex++)
2650 {
2651 const unsigned int location = baseLocation + elementIndex;
2652 if (location >= mState.mOutputVariableTypes.size())
2653 {
2654 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2655 }
Corentin Walleze7557742017-06-01 13:09:57 -04002656 ASSERT(location < mState.mActiveOutputVariables.size());
2657 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002658 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2659 }
2660 }
2661
Jamie Madill80a6fc02015-08-21 16:53:16 -04002662 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002663 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002664 return;
2665
Jamie Madillbd044ed2017-06-05 12:59:21 -04002666 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002667 // TODO(jmadill): any caps validation here?
2668
jchen1015015f72017-03-16 13:54:21 +08002669 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002670 outputVariableIndex++)
2671 {
jchen1015015f72017-03-16 13:54:21 +08002672 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002673
2674 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2675 if (outputVariable.isBuiltIn())
2676 continue;
2677
2678 // Since multiple output locations must be specified, use 0 for non-specified locations.
2679 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2680
Jamie Madill80a6fc02015-08-21 16:53:16 -04002681 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2682 elementIndex++)
2683 {
2684 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002685 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002686 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002687 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002688 VariableLocation(outputVariable.name, element, outputVariableIndex);
2689 }
2690 }
2691}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002692
Olli Etuaho48fed632017-03-16 12:05:30 +00002693void Program::setUniformValuesFromBindingQualifiers()
2694{
Jamie Madill982f6e02017-06-07 14:33:04 -04002695 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002696 {
2697 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2698 if (samplerUniform.binding != -1)
2699 {
2700 GLint location = mState.getUniformLocation(samplerUniform.name);
2701 ASSERT(location != -1);
2702 std::vector<GLint> boundTextureUnits;
2703 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2704 ++elementIndex)
2705 {
2706 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2707 }
2708 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2709 boundTextureUnits.data());
2710 }
2711 }
2712}
2713
jchen10eaef1e52017-06-13 10:44:11 +08002714void Program::gatherAtomicCounterBuffers()
2715{
2716 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2717 // counter.
2718 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2719}
2720
Jamie Madillbd044ed2017-06-05 12:59:21 -04002721void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002722{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002723 ASSERT(mState.mUniformBlocks.empty());
2724
2725 if (mState.mAttachedComputeShader)
2726 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002727 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002728
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002729 for (const sh::InterfaceBlock &computeBlock : computeShader->getUniformBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002730 {
2731
2732 // Only 'packed' blocks are allowed to be considered inactive.
2733 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2734 continue;
2735
Jamie Madilla2c74982016-12-12 11:20:42 -05002736 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002737 {
2738 if (block.name == computeBlock.name)
2739 {
2740 block.computeStaticUse = computeBlock.staticUse;
2741 }
2742 }
2743
2744 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2745 }
2746 return;
2747 }
2748
Jamie Madill62d31cb2015-09-11 13:25:51 -04002749 std::set<std::string> visitedList;
2750
Jamie Madillbd044ed2017-06-05 12:59:21 -04002751 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002752
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002753 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002754 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002755 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002756 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2757 continue;
2758
2759 if (visitedList.count(vertexBlock.name) > 0)
2760 continue;
2761
2762 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2763 visitedList.insert(vertexBlock.name);
2764 }
2765
Jamie Madillbd044ed2017-06-05 12:59:21 -04002766 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002768 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002769 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002770 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002771 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2772 continue;
2773
2774 if (visitedList.count(fragmentBlock.name) > 0)
2775 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002776 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002777 {
2778 if (block.name == fragmentBlock.name)
2779 {
2780 block.fragmentStaticUse = fragmentBlock.staticUse;
2781 }
2782 }
2783
2784 continue;
2785 }
2786
2787 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2788 visitedList.insert(fragmentBlock.name);
2789 }
jchen10af713a22017-04-19 09:10:56 +08002790 // Set initial bindings from shader.
2791 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2792 {
2793 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2794 bindUniformBlock(blockIndex, uniformBlock.binding);
2795 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002796}
2797
Jamie Madill4a3c2342015-10-08 12:58:45 -04002798template <typename VarT>
2799void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2800 const std::string &prefix,
2801 int blockIndex)
2802{
2803 for (const VarT &field : fields)
2804 {
2805 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2806
2807 if (field.isStruct())
2808 {
2809 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2810 {
2811 const std::string uniformElementName =
2812 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2813 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2814 }
2815 }
2816 else
2817 {
2818 // If getBlockMemberInfo returns false, the uniform is optimized out.
2819 sh::BlockMemberInfo memberInfo;
2820 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2821 {
2822 continue;
2823 }
2824
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002825 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002826 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002827
2828 // Since block uniforms have no location, we don't need to store them in the uniform
2829 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002830 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002831 }
2832 }
2833}
2834
Jamie Madill62d31cb2015-09-11 13:25:51 -04002835void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2836{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002837 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002838 size_t blockSize = 0;
2839
Jamie Madill4a3c2342015-10-08 12:58:45 -04002840 // Track the first and last uniform index to determine the range of active uniforms in the
2841 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002842 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002843 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002844 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002845
2846 std::vector<unsigned int> blockUniformIndexes;
2847 for (size_t blockUniformIndex = firstBlockUniformIndex;
2848 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2849 {
2850 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2851 }
jchen10af713a22017-04-19 09:10:56 +08002852 // ESSL 3.10 section 4.4.4 page 58:
2853 // Any uniform or shader storage block declared without a binding qualifier is initially
2854 // assigned to block binding point zero.
2855 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 if (interfaceBlock.arraySize > 0)
2857 {
2858 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2859 {
jchen10af713a22017-04-19 09:10:56 +08002860 // Don't define this block at all if it's not active in the implementation.
2861 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2862 &blockSize))
2863 {
2864 continue;
2865 }
2866 UniformBlock block(interfaceBlock.name, true, arrayElement,
2867 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002868 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002869
Martin Radev4c4c8e72016-08-04 12:25:34 +03002870 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002871 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002872 case GL_VERTEX_SHADER:
2873 {
2874 block.vertexStaticUse = interfaceBlock.staticUse;
2875 break;
2876 }
2877 case GL_FRAGMENT_SHADER:
2878 {
2879 block.fragmentStaticUse = interfaceBlock.staticUse;
2880 break;
2881 }
2882 case GL_COMPUTE_SHADER:
2883 {
2884 block.computeStaticUse = interfaceBlock.staticUse;
2885 break;
2886 }
2887 default:
2888 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002889 }
2890
Qin Jiajia0350a642016-11-01 17:01:51 +08002891 // Since all block elements in an array share the same active uniforms, they will all be
2892 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2893 // here we will add every block element in the array.
2894 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002895 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002896 }
2897 }
2898 else
2899 {
jchen10af713a22017-04-19 09:10:56 +08002900 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2901 {
2902 return;
2903 }
2904 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002905 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002906
Martin Radev4c4c8e72016-08-04 12:25:34 +03002907 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002909 case GL_VERTEX_SHADER:
2910 {
2911 block.vertexStaticUse = interfaceBlock.staticUse;
2912 break;
2913 }
2914 case GL_FRAGMENT_SHADER:
2915 {
2916 block.fragmentStaticUse = interfaceBlock.staticUse;
2917 break;
2918 }
2919 case GL_COMPUTE_SHADER:
2920 {
2921 block.computeStaticUse = interfaceBlock.staticUse;
2922 break;
2923 }
2924 default:
2925 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926 }
2927
Jamie Madill4a3c2342015-10-08 12:58:45 -04002928 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002929 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002930 }
2931}
2932
Jamie Madille7d84322017-01-10 18:21:59 -05002933template <>
2934void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2935 const uint8_t *destPointer,
2936 GLsizei clampedCount,
2937 const GLint *v)
2938{
2939 // Invalidate the validation cache only if we modify the sampler data.
2940 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2941 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2942 {
2943 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2944 std::vector<GLuint> *boundTextureUnits =
2945 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2946
2947 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2948 mCachedValidateSamplersResult.reset();
2949 }
2950}
2951
2952template <typename T>
2953void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2954 const uint8_t *destPointer,
2955 GLsizei clampedCount,
2956 const T *v)
2957{
2958}
2959
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002961GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002962{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002963 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2964 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002965 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2966
Corentin Wallez15ac5342016-11-03 17:06:39 -04002967 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2968 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2969 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002970 GLsizei maxElementCount =
2971 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2972
2973 GLsizei count = countIn;
2974 GLsizei clampedCount = count * vectorSize;
2975 if (clampedCount > maxElementCount)
2976 {
2977 clampedCount = maxElementCount;
2978 count = maxElementCount / vectorSize;
2979 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002980
Jamie Madill44183cc2017-08-01 12:48:34 -04002981 // VariableComponentType(linkedUniform->type) has a dozens of compares and thus is evil for
2982 // inlining with regards to code size. This version is one subtract and one compare only.
2983 if (IsVariableComponentTypeBool(linkedUniform->type))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002984 {
2985 // Do a cast conversion for boolean types. From the spec:
2986 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2987 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002988 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002989 {
2990 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2991 }
2992 }
2993 else
2994 {
Jamie Madille7d84322017-01-10 18:21:59 -05002995 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002996 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002997 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002998
2999 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003000}
3001
3002template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003003GLsizei Program::setMatrixUniformInternal(GLint location,
3004 GLsizei count,
3005 GLboolean transpose,
3006 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003007{
3008 if (!transpose)
3009 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003010 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003011 }
3012
3013 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003014 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3015 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003016 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003017
3018 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3019 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3020 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3021 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3022
3023 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003024 {
3025 size_t elementOffset = element * rows * cols;
3026
3027 for (size_t row = 0; row < rows; ++row)
3028 {
3029 for (size_t col = 0; col < cols; ++col)
3030 {
3031 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3032 }
3033 }
3034 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003035
3036 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003037}
3038
Jamie Madill54164b02017-08-28 15:17:37 -04003039// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3040// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003041template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003042void Program::getUniformInternal(const Context *context,
3043 DestT *dataOut,
3044 GLint location,
3045 GLenum nativeType,
3046 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003047{
Jamie Madill54164b02017-08-28 15:17:37 -04003048 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003049 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003050 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003051 {
3052 GLint tempValue[16] = {0};
3053 mProgram->getUniformiv(context, location, tempValue);
3054 UniformStateQueryCastLoop<GLboolean>(
3055 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003056 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003057 }
3058 case GL_INT:
3059 {
3060 GLint tempValue[16] = {0};
3061 mProgram->getUniformiv(context, location, tempValue);
3062 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3063 components);
3064 break;
3065 }
3066 case GL_UNSIGNED_INT:
3067 {
3068 GLuint tempValue[16] = {0};
3069 mProgram->getUniformuiv(context, location, tempValue);
3070 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3071 components);
3072 break;
3073 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003074 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003075 {
3076 GLfloat tempValue[16] = {0};
3077 mProgram->getUniformfv(context, location, tempValue);
3078 UniformStateQueryCastLoop<GLfloat>(
3079 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003080 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003081 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003082 default:
3083 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003084 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003085 }
3086}
Jamie Madilla4595b82017-01-11 17:36:34 -05003087
3088bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3089{
3090 // Must be called after samplers are validated.
3091 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3092
3093 for (const auto &binding : mState.mSamplerBindings)
3094 {
3095 GLenum textureType = binding.textureType;
3096 for (const auto &unit : binding.boundTextureUnits)
3097 {
3098 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3099 if (programTextureID == textureID)
3100 {
3101 // TODO(jmadill): Check for appropriate overlap.
3102 return true;
3103 }
3104 }
3105 }
3106
3107 return false;
3108}
3109
Jamie Madilla2c74982016-12-12 11:20:42 -05003110} // namespace gl