blob: a13f55be264dd659701a2f0814d818f9b7b162ab [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
Jiajia Qin729b2c62017-08-14 09:36:11 +0800196bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
197 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
198 const std::string &errorMessage,
199 InfoLog &infoLog)
200{
201 GLuint blockCount = 0;
202 for (const sh::InterfaceBlock &block : interfaceBlocks)
203 {
204 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
205 {
206 blockCount += (block.arraySize ? block.arraySize : 1);
207 if (blockCount > maxInterfaceBlocks)
208 {
209 infoLog << errorMessage << maxInterfaceBlocks << ")";
210 return false;
211 }
212 }
213 }
214 return true;
215}
216
Jamie Madill62d31cb2015-09-11 13:25:51 -0400217} // anonymous namespace
218
Jamie Madill4a3c2342015-10-08 12:58:45 -0400219const char *const g_fakepath = "C:\\fakepath";
220
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400221InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000222{
223}
224
225InfoLog::~InfoLog()
226{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000227}
228
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400229size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000230{
Jamie Madill23176ce2017-07-31 14:14:33 -0400231 if (!mLazyStream)
232 {
233 return 0;
234 }
235
236 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400237 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000238}
239
Geoff Lange1a27752015-10-05 13:16:04 -0400240void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000241{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400242 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000243
244 if (bufSize > 0)
245 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400246 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400247
Jamie Madill23176ce2017-07-31 14:14:33 -0400248 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000249 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400250 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
251 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000252 }
253
254 infoLog[index] = '\0';
255 }
256
257 if (length)
258 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400259 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000260 }
261}
262
263// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300264// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265// messages, so lets remove all occurrences of this fake file path from the log.
266void InfoLog::appendSanitized(const char *message)
267{
Jamie Madill23176ce2017-07-31 14:14:33 -0400268 ensureInitialized();
269
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000270 std::string msg(message);
271
272 size_t found;
273 do
274 {
275 found = msg.find(g_fakepath);
276 if (found != std::string::npos)
277 {
278 msg.erase(found, strlen(g_fakepath));
279 }
280 }
281 while (found != std::string::npos);
282
Jamie Madill23176ce2017-07-31 14:14:33 -0400283 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000284}
285
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000286void InfoLog::reset()
287{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000288}
289
Jamie Madillfb997ec2017-09-20 15:44:27 -0400290VariableLocation::VariableLocation() : element(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000291{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500292}
293
Jamie Madillfb997ec2017-09-20 15:44:27 -0400294VariableLocation::VariableLocation(unsigned int element, unsigned int index)
295 : element(element), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500296{
297}
298
Geoff Langd8605522016-04-13 10:19:12 -0400299void Program::Bindings::bindLocation(GLuint index, const std::string &name)
300{
301 mBindings[name] = index;
302}
303
304int Program::Bindings::getBinding(const std::string &name) const
305{
306 auto iter = mBindings.find(name);
307 return (iter != mBindings.end()) ? iter->second : -1;
308}
309
310Program::Bindings::const_iterator Program::Bindings::begin() const
311{
312 return mBindings.begin();
313}
314
315Program::Bindings::const_iterator Program::Bindings::end() const
316{
317 return mBindings.end();
318}
319
Jamie Madill48ef11b2016-04-27 15:21:52 -0400320ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500321 : mLabel(),
322 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400323 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300324 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500325 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500326 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800327 mImageUniformRange(0, 0),
328 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300329 mBinaryRetrieveableHint(false),
330 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400331{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300332 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400333}
334
Jamie Madill48ef11b2016-04-27 15:21:52 -0400335ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400336{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500337 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400338}
339
Jamie Madill48ef11b2016-04-27 15:21:52 -0400340const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500341{
342 return mLabel;
343}
344
Jamie Madill48ef11b2016-04-27 15:21:52 -0400345GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400346{
347 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800348 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400349
350 for (size_t location = 0; location < mUniformLocations.size(); ++location)
351 {
352 const VariableLocation &uniformLocation = mUniformLocations[location];
Jamie Madillfb997ec2017-09-20 15:44:27 -0400353 if (!uniformLocation.used())
Geoff Langd8605522016-04-13 10:19:12 -0400354 {
355 continue;
356 }
357
358 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400359
360 if (uniform.name == baseName)
361 {
Geoff Langd8605522016-04-13 10:19:12 -0400362 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400363 {
Geoff Langd8605522016-04-13 10:19:12 -0400364 if (uniformLocation.element == subscript ||
365 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
366 {
367 return static_cast<GLint>(location);
368 }
369 }
370 else
371 {
372 if (subscript == GL_INVALID_INDEX)
373 {
374 return static_cast<GLint>(location);
375 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400376 }
377 }
378 }
379
380 return -1;
381}
382
Jamie Madille7d84322017-01-10 18:21:59 -0500383GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400384{
jchen1015015f72017-03-16 13:54:21 +0800385 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400386}
387
Jamie Madille7d84322017-01-10 18:21:59 -0500388GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
389{
390 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
391 return mUniformLocations[location].index;
392}
393
394Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
395{
396 GLuint index = getUniformIndexFromLocation(location);
397 if (!isSamplerUniformIndex(index))
398 {
399 return Optional<GLuint>::Invalid();
400 }
401
402 return getSamplerIndexFromUniformIndex(index);
403}
404
405bool ProgramState::isSamplerUniformIndex(GLuint index) const
406{
Jamie Madill982f6e02017-06-07 14:33:04 -0400407 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500408}
409
410GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
411{
412 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400413 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500414}
415
Jamie Madill34ca4f52017-06-13 11:49:39 -0400416GLuint ProgramState::getAttributeLocation(const std::string &name) const
417{
418 for (const sh::Attribute &attribute : mAttributes)
419 {
420 if (attribute.name == name)
421 {
422 return attribute.location;
423 }
424 }
425
426 return static_cast<GLuint>(-1);
427}
428
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500429Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400430 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400431 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500432 mLinked(false),
433 mDeleteStatus(false),
434 mRefCount(0),
435 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500436 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500437{
438 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000439
Geoff Lang7dd2e102014-11-10 15:19:26 -0500440 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441}
442
443Program::~Program()
444{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400445 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446}
447
Jamie Madill4928b7c2017-06-20 12:57:39 -0400448void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500449{
450 if (mState.mAttachedVertexShader != nullptr)
451 {
452 mState.mAttachedVertexShader->release(context);
453 mState.mAttachedVertexShader = nullptr;
454 }
455
456 if (mState.mAttachedFragmentShader != nullptr)
457 {
458 mState.mAttachedFragmentShader->release(context);
459 mState.mAttachedFragmentShader = nullptr;
460 }
461
462 if (mState.mAttachedComputeShader != nullptr)
463 {
464 mState.mAttachedComputeShader->release(context);
465 mState.mAttachedComputeShader = nullptr;
466 }
467
Jamie Madillc564c072017-06-01 12:45:42 -0400468 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400469
470 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
471 !mState.mAttachedComputeShader);
472 SafeDelete(mProgram);
473
474 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500475}
476
Geoff Lang70d0f492015-12-10 17:45:46 -0500477void Program::setLabel(const std::string &label)
478{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400479 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500480}
481
482const std::string &Program::getLabel() const
483{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400484 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500485}
486
Jamie Madillef300b12016-10-07 15:12:09 -0400487void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300489 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300491 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492 {
Jamie Madillef300b12016-10-07 15:12:09 -0400493 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300494 mState.mAttachedVertexShader = shader;
495 mState.mAttachedVertexShader->addRef();
496 break;
497 }
498 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499 {
Jamie Madillef300b12016-10-07 15:12:09 -0400500 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 mState.mAttachedFragmentShader = shader;
502 mState.mAttachedFragmentShader->addRef();
503 break;
504 }
505 case GL_COMPUTE_SHADER:
506 {
Jamie Madillef300b12016-10-07 15:12:09 -0400507 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300508 mState.mAttachedComputeShader = shader;
509 mState.mAttachedComputeShader->addRef();
510 break;
511 }
512 default:
513 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515}
516
Jamie Madillc1d770e2017-04-13 17:31:24 -0400517void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300519 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300521 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400523 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500524 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300525 mState.mAttachedVertexShader = nullptr;
526 break;
527 }
528 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400530 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500531 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300532 mState.mAttachedFragmentShader = nullptr;
533 break;
534 }
535 case GL_COMPUTE_SHADER:
536 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400537 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500538 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300539 mState.mAttachedComputeShader = nullptr;
540 break;
541 }
542 default:
543 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545}
546
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000547int Program::getAttachedShadersCount() const
548{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300549 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
550 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000551}
552
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553void Program::bindAttributeLocation(GLuint index, const char *name)
554{
Geoff Langd8605522016-04-13 10:19:12 -0400555 mAttributeBindings.bindLocation(index, name);
556}
557
558void Program::bindUniformLocation(GLuint index, const char *name)
559{
560 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800561 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562}
563
Sami Väisänen46eaa942016-06-29 10:26:37 +0300564void Program::bindFragmentInputLocation(GLint index, const char *name)
565{
566 mFragmentInputBindings.bindLocation(index, name);
567}
568
Jamie Madillbd044ed2017-06-05 12:59:21 -0400569BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300570{
571 BindingInfo ret;
572 ret.type = GL_NONE;
573 ret.valid = false;
574
Jamie Madillbd044ed2017-06-05 12:59:21 -0400575 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300576 ASSERT(fragmentShader);
577
578 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400579 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300580
581 for (const auto &binding : mFragmentInputBindings)
582 {
583 if (binding.second != static_cast<GLuint>(index))
584 continue;
585
586 ret.valid = true;
587
588 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400589 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300590
591 for (const auto &in : inputs)
592 {
593 if (in.name == originalName)
594 {
595 if (in.isArray())
596 {
597 // The client wants to bind either "name" or "name[0]".
598 // GL ES 3.1 spec refers to active array names with language such as:
599 // "if the string identifies the base name of an active array, where the
600 // string would exactly match the name of the variable if the suffix "[0]"
601 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400602 if (arrayIndex == GL_INVALID_INDEX)
603 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300604
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400605 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300606 }
607 else
608 {
609 ret.name = in.mappedName;
610 }
611 ret.type = in.type;
612 return ret;
613 }
614 }
615 }
616
617 return ret;
618}
619
Jamie Madillbd044ed2017-06-05 12:59:21 -0400620void Program::pathFragmentInputGen(const Context *context,
621 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300622 GLenum genMode,
623 GLint components,
624 const GLfloat *coeffs)
625{
626 // If the location is -1 then the command is silently ignored
627 if (index == -1)
628 return;
629
Jamie Madillbd044ed2017-06-05 12:59:21 -0400630 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300631
632 // If the input doesn't exist then then the command is silently ignored
633 // This could happen through optimization for example, the shader translator
634 // decides that a variable is not actually being used and optimizes it away.
635 if (binding.name.empty())
636 return;
637
638 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
639}
640
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641// The attached shaders are checked for linking errors by matching up their variables.
642// Uniform, input and output variables get collected.
643// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500644Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000645{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500646 const auto &data = context->getContextState();
647
Jamie Madill6c58b062017-08-01 13:44:25 -0400648 auto *platform = ANGLEPlatformCurrent();
649 double startTime = platform->currentTime(platform);
650
Jamie Madill6c1f6712017-02-14 19:08:04 -0500651 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000652
Jamie Madill32447362017-06-28 14:53:52 -0400653 ProgramHash programHash;
654 auto *cache = context->getMemoryProgramCache();
655 if (cache)
656 {
657 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400658 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400659 }
660
661 if (mLinked)
662 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400663 double delta = platform->currentTime(platform) - startTime;
664 int us = static_cast<int>(delta * 1000000.0);
665 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400666 return NoError();
667 }
668
669 // Cache load failed, fall through to normal linking.
670 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000671 mInfoLog.reset();
672
Martin Radev4c4c8e72016-08-04 12:25:34 +0300673 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500674
Jamie Madill192745a2016-12-22 15:58:21 -0500675 auto vertexShader = mState.mAttachedVertexShader;
676 auto fragmentShader = mState.mAttachedFragmentShader;
677 auto computeShader = mState.mAttachedComputeShader;
678
679 bool isComputeShaderAttached = (computeShader != nullptr);
680 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300681 // Check whether we both have a compute and non-compute shaders attached.
682 // If there are of both types attached, then linking should fail.
683 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
684 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500685 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300686 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
687 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400688 }
689
Jamie Madill192745a2016-12-22 15:58:21 -0500690 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500691 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400692 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300693 {
694 mInfoLog << "Attached compute shader is not compiled.";
695 return NoError();
696 }
Jamie Madill192745a2016-12-22 15:58:21 -0500697 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300698
Jamie Madillbd044ed2017-06-05 12:59:21 -0400699 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300700
701 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
702 // If the work group size is not specified, a link time error should occur.
703 if (!mState.mComputeShaderLocalSize.isDeclared())
704 {
705 mInfoLog << "Work group size is not specified.";
706 return NoError();
707 }
708
Jamie Madillbd044ed2017-06-05 12:59:21 -0400709 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300710 {
711 return NoError();
712 }
713
Jiajia Qin729b2c62017-08-14 09:36:11 +0800714 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300715 {
716 return NoError();
717 }
718
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500719 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400720 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500721 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300722 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500723 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 }
725 }
726 else
727 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400728 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300729 {
730 return NoError();
731 }
Jamie Madill192745a2016-12-22 15:58:21 -0500732 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733
Jamie Madillbd044ed2017-06-05 12:59:21 -0400734 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300735 {
736 return NoError();
737 }
Jamie Madill192745a2016-12-22 15:58:21 -0500738 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300739
Jamie Madillbd044ed2017-06-05 12:59:21 -0400740 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300741 {
742 mInfoLog << "Fragment shader version does not match vertex shader version.";
743 return NoError();
744 }
745
Jamie Madillbd044ed2017-06-05 12:59:21 -0400746 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300747 {
748 return NoError();
749 }
750
Jamie Madillbd044ed2017-06-05 12:59:21 -0400751 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300752 {
753 return NoError();
754 }
755
Jamie Madillbd044ed2017-06-05 12:59:21 -0400756 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300757 {
758 return NoError();
759 }
760
Jiajia Qin729b2c62017-08-14 09:36:11 +0800761 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300762 {
763 return NoError();
764 }
765
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400766 if (!linkValidateGlobalNames(context, mInfoLog))
767 {
768 return NoError();
769 }
770
Jamie Madillbd044ed2017-06-05 12:59:21 -0400771 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300772
Martin Radev7cf61662017-07-26 17:10:53 +0300773 mState.mNumViews = vertexShader->getNumViews(context);
774
Jamie Madillbd044ed2017-06-05 12:59:21 -0400775 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300776
Jamie Madill192745a2016-12-22 15:58:21 -0500777 // Validate we can pack the varyings.
778 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
779
780 // Map the varyings to the register file
781 // In WebGL, we use a slightly different handling for packing variables.
782 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
783 : PackMode::ANGLE_RELAXED;
784 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
785 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
786 mState.getTransformFeedbackVaryingNames()))
787 {
788 return NoError();
789 }
790
Olli Etuaho39e78122017-08-29 14:34:22 +0300791 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
792 {
793 return NoError();
794 }
795
Jamie Madillc564c072017-06-01 12:45:42 -0400796 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500797 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300798 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500799 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300800 }
801
802 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500803 }
804
jchen10eaef1e52017-06-13 10:44:11 +0800805 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400806 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400807
jchen10eaef1e52017-06-13 10:44:11 +0800808 setUniformValuesFromBindingQualifiers();
809
Jamie Madill54164b02017-08-28 15:17:37 -0400810 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400811 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400812
Jamie Madill32447362017-06-28 14:53:52 -0400813 // Save to the program cache.
814 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
815 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
816 {
817 cache->putProgram(programHash, context, this);
818 }
819
Jamie Madill6c58b062017-08-01 13:44:25 -0400820 double delta = platform->currentTime(platform) - startTime;
821 int us = static_cast<int>(delta * 1000000.0);
822 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
823
Martin Radev4c4c8e72016-08-04 12:25:34 +0300824 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000825}
826
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000827// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500828void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400830 mState.mAttributes.clear();
831 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800832 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400833 mState.mUniforms.clear();
834 mState.mUniformLocations.clear();
835 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800836 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800837 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400838 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800839 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400840 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400841 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300842 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500843 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800844 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300845 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500846
Geoff Lang7dd2e102014-11-10 15:19:26 -0500847 mValidated = false;
848
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000849 mLinked = false;
850}
851
Geoff Lange1a27752015-10-05 13:16:04 -0400852bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000853{
854 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855}
856
Jamie Madilla2c74982016-12-12 11:20:42 -0500857Error Program::loadBinary(const Context *context,
858 GLenum binaryFormat,
859 const void *binary,
860 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000861{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500862 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000863
Geoff Lang7dd2e102014-11-10 15:19:26 -0500864#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800865 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500866#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400867 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
868 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000869 {
Jamie Madillf6113162015-05-07 11:49:21 -0400870 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800871 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500872 }
873
Jamie Madill4f86d052017-06-05 12:59:26 -0400874 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
875 ANGLE_TRY_RESULT(
876 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400877
878 // Currently we require the full shader text to compute the program hash.
879 // TODO(jmadill): Store the binary in the internal program cache.
880
Jamie Madillb0a838b2016-11-13 20:02:12 -0500881 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500882#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500883}
884
Jamie Madilla2c74982016-12-12 11:20:42 -0500885Error Program::saveBinary(const Context *context,
886 GLenum *binaryFormat,
887 void *binary,
888 GLsizei bufSize,
889 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500890{
891 if (binaryFormat)
892 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400893 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894 }
895
Jamie Madill4f86d052017-06-05 12:59:26 -0400896 angle::MemoryBuffer memoryBuf;
897 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898
Jamie Madill4f86d052017-06-05 12:59:26 -0400899 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
900 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901
902 if (streamLength > bufSize)
903 {
904 if (length)
905 {
906 *length = 0;
907 }
908
909 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
910 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
911 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500912 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500913 }
914
915 if (binary)
916 {
917 char *ptr = reinterpret_cast<char*>(binary);
918
Jamie Madill48ef11b2016-04-27 15:21:52 -0400919 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500920 ptr += streamLength;
921
922 ASSERT(ptr - streamLength == binary);
923 }
924
925 if (length)
926 {
927 *length = streamLength;
928 }
929
He Yunchaoacd18982017-01-04 10:46:42 +0800930 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500931}
932
Jamie Madillffe00c02017-06-27 16:26:55 -0400933GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500934{
935 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400936 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500937 if (error.isError())
938 {
939 return 0;
940 }
941
942 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000943}
944
Geoff Langc5629752015-12-07 16:29:04 -0500945void Program::setBinaryRetrievableHint(bool retrievable)
946{
947 // TODO(jmadill) : replace with dirty bits
948 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400949 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500950}
951
952bool Program::getBinaryRetrievableHint() const
953{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400954 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500955}
956
Yunchao He61afff12017-03-14 15:34:03 +0800957void Program::setSeparable(bool separable)
958{
959 // TODO(yunchao) : replace with dirty bits
960 if (mState.mSeparable != separable)
961 {
962 mProgram->setSeparable(separable);
963 mState.mSeparable = separable;
964 }
965}
966
967bool Program::isSeparable() const
968{
969 return mState.mSeparable;
970}
971
Jamie Madill6c1f6712017-02-14 19:08:04 -0500972void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000973{
974 mRefCount--;
975
976 if (mRefCount == 0 && mDeleteStatus)
977 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500978 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000979 }
980}
981
982void Program::addRef()
983{
984 mRefCount++;
985}
986
987unsigned int Program::getRefCount() const
988{
989 return mRefCount;
990}
991
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000992int Program::getInfoLogLength() const
993{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400994 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000995}
996
Geoff Lange1a27752015-10-05 13:16:04 -0400997void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000998{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000999 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001000}
1001
Geoff Lange1a27752015-10-05 13:16:04 -04001002void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001003{
1004 int total = 0;
1005
Martin Radev4c4c8e72016-08-04 12:25:34 +03001006 if (mState.mAttachedComputeShader)
1007 {
1008 if (total < maxCount)
1009 {
1010 shaders[total] = mState.mAttachedComputeShader->getHandle();
1011 total++;
1012 }
1013 }
1014
Jamie Madill48ef11b2016-04-27 15:21:52 -04001015 if (mState.mAttachedVertexShader)
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.mAttachedVertexShader->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
Jamie Madill48ef11b2016-04-27 15:21:52 -04001024 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001025 {
1026 if (total < maxCount)
1027 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001028 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001029 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001030 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001031 }
1032
1033 if (count)
1034 {
1035 *count = total;
1036 }
1037}
1038
Geoff Lange1a27752015-10-05 13:16:04 -04001039GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001040{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001041 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001042}
1043
Jamie Madill63805b42015-08-25 13:17:39 -04001044bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001045{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001046 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1047 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001048}
1049
jchen10fd7c3b52017-03-21 15:36:03 +08001050void Program::getActiveAttribute(GLuint index,
1051 GLsizei bufsize,
1052 GLsizei *length,
1053 GLint *size,
1054 GLenum *type,
1055 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001056{
Jamie Madillc349ec02015-08-21 16:53:12 -04001057 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001058 {
1059 if (bufsize > 0)
1060 {
1061 name[0] = '\0';
1062 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001063
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001064 if (length)
1065 {
1066 *length = 0;
1067 }
1068
1069 *type = GL_NONE;
1070 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001071 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001072 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001073
jchen1036e120e2017-03-14 14:53:58 +08001074 ASSERT(index < mState.mAttributes.size());
1075 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001076
1077 if (bufsize > 0)
1078 {
jchen10fd7c3b52017-03-21 15:36:03 +08001079 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001080 }
1081
1082 // Always a single 'type' instance
1083 *size = 1;
1084 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001085}
1086
Geoff Lange1a27752015-10-05 13:16:04 -04001087GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001088{
Jamie Madillc349ec02015-08-21 16:53:12 -04001089 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001090 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001091 return 0;
1092 }
1093
jchen1036e120e2017-03-14 14:53:58 +08001094 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001095}
1096
Geoff Lange1a27752015-10-05 13:16:04 -04001097GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001098{
Jamie Madillc349ec02015-08-21 16:53:12 -04001099 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001100 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001101 return 0;
1102 }
1103
1104 size_t maxLength = 0;
1105
Jamie Madill48ef11b2016-04-27 15:21:52 -04001106 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001107 {
jchen1036e120e2017-03-14 14:53:58 +08001108 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001109 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001110
Jamie Madillc349ec02015-08-21 16:53:12 -04001111 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001112}
1113
jchen1015015f72017-03-16 13:54:21 +08001114GLuint Program::getInputResourceIndex(const GLchar *name) const
1115{
1116 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1117 {
1118 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1119 if (attribute.name == name)
1120 {
1121 return attributeIndex;
1122 }
1123 }
1124 return GL_INVALID_INDEX;
1125}
1126
1127GLuint Program::getOutputResourceIndex(const GLchar *name) const
1128{
1129 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1130}
1131
jchen10fd7c3b52017-03-21 15:36:03 +08001132size_t Program::getOutputResourceCount() const
1133{
1134 return (mLinked ? mState.mOutputVariables.size() : 0);
1135}
1136
jchen10baf5d942017-08-28 20:45:48 +08001137template <typename T>
1138void Program::getResourceName(GLuint index,
1139 const std::vector<T> &resources,
1140 GLsizei bufSize,
1141 GLsizei *length,
1142 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001143{
1144 if (length)
1145 {
1146 *length = 0;
1147 }
1148
1149 if (!mLinked)
1150 {
1151 if (bufSize > 0)
1152 {
1153 name[0] = '\0';
1154 }
1155 return;
1156 }
jchen10baf5d942017-08-28 20:45:48 +08001157 ASSERT(index < resources.size());
1158 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001159
1160 if (bufSize > 0)
1161 {
jchen10baf5d942017-08-28 20:45:48 +08001162 std::string nameWithArray = (resource.isArray() ? resource.name + "[0]" : resource.name);
jchen10fd7c3b52017-03-21 15:36:03 +08001163
1164 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1165 }
1166}
1167
jchen10baf5d942017-08-28 20:45:48 +08001168void Program::getInputResourceName(GLuint index,
1169 GLsizei bufSize,
1170 GLsizei *length,
1171 GLchar *name) const
1172{
1173 getResourceName(index, mState.mAttributes, bufSize, length, name);
1174}
1175
1176void Program::getOutputResourceName(GLuint index,
1177 GLsizei bufSize,
1178 GLsizei *length,
1179 GLchar *name) const
1180{
1181 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1182}
1183
1184void Program::getUniformResourceName(GLuint index,
1185 GLsizei bufSize,
1186 GLsizei *length,
1187 GLchar *name) const
1188{
1189 getResourceName(index, mState.mUniforms, bufSize, length, name);
1190}
1191
jchen10880683b2017-04-12 16:21:55 +08001192const sh::Attribute &Program::getInputResource(GLuint index) const
1193{
1194 ASSERT(index < mState.mAttributes.size());
1195 return mState.mAttributes[index];
1196}
1197
1198const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1199{
1200 ASSERT(index < mState.mOutputVariables.size());
1201 return mState.mOutputVariables[index];
1202}
1203
Geoff Lang7dd2e102014-11-10 15:19:26 -05001204GLint Program::getFragDataLocation(const std::string &name) const
1205{
1206 std::string baseName(name);
1207 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001208 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001209 {
Jamie Madillfb997ec2017-09-20 15:44:27 -04001210 const VariableLocation &locationInfo = outputPair.second;
1211 const sh::OutputVariable &outputVariable = mState.mOutputVariables[locationInfo.index];
1212 if (outputVariable.name == baseName &&
1213 (arrayIndex == GL_INVALID_INDEX || arrayIndex == locationInfo.element))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001215 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001217 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001218 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001219}
1220
Geoff Lange1a27752015-10-05 13:16:04 -04001221void Program::getActiveUniform(GLuint index,
1222 GLsizei bufsize,
1223 GLsizei *length,
1224 GLint *size,
1225 GLenum *type,
1226 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001227{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001229 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001230 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001231 ASSERT(index < mState.mUniforms.size());
1232 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233
1234 if (bufsize > 0)
1235 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236 std::string string = uniform.name;
1237 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 {
1239 string += "[0]";
1240 }
jchen10fd7c3b52017-03-21 15:36:03 +08001241 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242 }
1243
Jamie Madill62d31cb2015-09-11 13:25:51 -04001244 *size = uniform.elementCount();
1245 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001246 }
1247 else
1248 {
1249 if (bufsize > 0)
1250 {
1251 name[0] = '\0';
1252 }
1253
1254 if (length)
1255 {
1256 *length = 0;
1257 }
1258
1259 *size = 0;
1260 *type = GL_NONE;
1261 }
1262}
1263
Geoff Lange1a27752015-10-05 13:16:04 -04001264GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001265{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001266 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001267 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001268 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001269 }
1270 else
1271 {
1272 return 0;
1273 }
1274}
1275
Geoff Lange1a27752015-10-05 13:16:04 -04001276GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001277{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001278 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001279
1280 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001281 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001282 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001284 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001286 size_t length = uniform.name.length() + 1u;
1287 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288 {
1289 length += 3; // Counting in "[0]".
1290 }
1291 maxLength = std::max(length, maxLength);
1292 }
1293 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001294 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295
Jamie Madill62d31cb2015-09-11 13:25:51 -04001296 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001297}
1298
Geoff Lang7dd2e102014-11-10 15:19:26 -05001299bool Program::isValidUniformLocation(GLint location) const
1300{
Jamie Madille2e406c2016-06-02 13:04:10 -04001301 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001302 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001303 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001304}
1305
Jamie Madill62d31cb2015-09-11 13:25:51 -04001306const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001308 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001309 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310}
1311
Jamie Madillac4e9c32017-01-13 14:07:12 -05001312const VariableLocation &Program::getUniformLocation(GLint location) const
1313{
1314 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1315 return mState.mUniformLocations[location];
1316}
1317
1318const std::vector<VariableLocation> &Program::getUniformLocations() const
1319{
1320 return mState.mUniformLocations;
1321}
1322
1323const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1324{
1325 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1326 return mState.mUniforms[index];
1327}
1328
Jamie Madill62d31cb2015-09-11 13:25:51 -04001329GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001331 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
Jamie Madill62d31cb2015-09-11 13:25:51 -04001334GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335{
Jamie Madille7d84322017-01-10 18:21:59 -05001336 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
1339void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1340{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001341 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1342 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001343 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344}
1345
1346void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1347{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001348 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1349 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001350 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001351}
1352
1353void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1354{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001355 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1356 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001357 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001358}
1359
1360void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1361{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001362 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1363 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001364 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365}
1366
Jamie Madill81c2e252017-09-09 23:32:46 -04001367Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001369 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1370 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1371
Jamie Madill81c2e252017-09-09 23:32:46 -04001372 mProgram->setUniform1iv(location, clampedCount, v);
1373
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001374 if (mState.isSamplerUniformIndex(locationInfo.index))
1375 {
1376 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001377 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001378 }
1379
Jamie Madill81c2e252017-09-09 23:32:46 -04001380 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381}
1382
1383void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1384{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001385 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1386 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001387 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388}
1389
1390void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1391{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001392 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1393 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001394 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395}
1396
1397void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1398{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001399 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1400 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001401 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402}
1403
1404void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1405{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001406 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1407 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001408 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
1411void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1412{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001413 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1414 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001415 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416}
1417
1418void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1419{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001420 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1421 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001422 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423}
1424
1425void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1426{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001427 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1428 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001429 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430}
1431
1432void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1433{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001434 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001435 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436}
1437
1438void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1439{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001440 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001441 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442}
1443
1444void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1445{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001446 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001447 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448}
1449
1450void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1451{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001452 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001453 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454}
1455
1456void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1457{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001458 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001459 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460}
1461
1462void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1463{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001464 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001465 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466}
1467
1468void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1469{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001470 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001471 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472}
1473
1474void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1475{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001476 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001477 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478}
1479
1480void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1481{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001482 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001483 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484}
1485
Jamie Madill54164b02017-08-28 15:17:37 -04001486void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487{
Jamie Madill54164b02017-08-28 15:17:37 -04001488 const auto &uniformLocation = mState.getUniformLocations()[location];
1489 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1490
1491 GLenum nativeType = gl::VariableComponentType(uniform.type);
1492 if (nativeType == GL_FLOAT)
1493 {
1494 mProgram->getUniformfv(context, location, v);
1495 }
1496 else
1497 {
1498 getUniformInternal(context, v, location, nativeType,
1499 gl::VariableComponentCount(uniform.type));
1500 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501}
1502
Jamie Madill54164b02017-08-28 15:17:37 -04001503void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504{
Jamie Madill54164b02017-08-28 15:17:37 -04001505 const auto &uniformLocation = mState.getUniformLocations()[location];
1506 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1507
1508 GLenum nativeType = gl::VariableComponentType(uniform.type);
1509 if (nativeType == GL_INT || nativeType == GL_BOOL)
1510 {
1511 mProgram->getUniformiv(context, location, v);
1512 }
1513 else
1514 {
1515 getUniformInternal(context, v, location, nativeType,
1516 gl::VariableComponentCount(uniform.type));
1517 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518}
1519
Jamie Madill54164b02017-08-28 15:17:37 -04001520void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521{
Jamie Madill54164b02017-08-28 15:17:37 -04001522 const auto &uniformLocation = mState.getUniformLocations()[location];
1523 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1524
1525 GLenum nativeType = gl::VariableComponentType(uniform.type);
1526 if (nativeType == GL_UNSIGNED_INT)
1527 {
1528 mProgram->getUniformuiv(context, location, v);
1529 }
1530 else
1531 {
1532 getUniformInternal(context, v, location, nativeType,
1533 gl::VariableComponentCount(uniform.type));
1534 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535}
1536
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001537void Program::flagForDeletion()
1538{
1539 mDeleteStatus = true;
1540}
1541
1542bool Program::isFlaggedForDeletion() const
1543{
1544 return mDeleteStatus;
1545}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001546
Brandon Jones43a53e22014-08-28 16:23:22 -07001547void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001548{
1549 mInfoLog.reset();
1550
Geoff Lang7dd2e102014-11-10 15:19:26 -05001551 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001552 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001553 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001554 }
1555 else
1556 {
Jamie Madillf6113162015-05-07 11:49:21 -04001557 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001558 }
1559}
1560
Geoff Lang7dd2e102014-11-10 15:19:26 -05001561bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1562{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001563 // Skip cache if we're using an infolog, so we get the full error.
1564 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1565 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1566 {
1567 return mCachedValidateSamplersResult.value();
1568 }
1569
1570 if (mTextureUnitTypesCache.empty())
1571 {
1572 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1573 }
1574 else
1575 {
1576 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1577 }
1578
1579 // if any two active samplers in a program are of different types, but refer to the same
1580 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1581 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001582 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001583 {
Jamie Madill54164b02017-08-28 15:17:37 -04001584 if (samplerBinding.unreferenced)
1585 continue;
1586
Jamie Madille7d84322017-01-10 18:21:59 -05001587 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001588
Jamie Madille7d84322017-01-10 18:21:59 -05001589 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001590 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001591 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1592 {
1593 if (infoLog)
1594 {
1595 (*infoLog) << "Sampler uniform (" << textureUnit
1596 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1597 << caps.maxCombinedTextureImageUnits << ")";
1598 }
1599
1600 mCachedValidateSamplersResult = false;
1601 return false;
1602 }
1603
1604 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1605 {
1606 if (textureType != mTextureUnitTypesCache[textureUnit])
1607 {
1608 if (infoLog)
1609 {
1610 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1611 "image unit ("
1612 << textureUnit << ").";
1613 }
1614
1615 mCachedValidateSamplersResult = false;
1616 return false;
1617 }
1618 }
1619 else
1620 {
1621 mTextureUnitTypesCache[textureUnit] = textureType;
1622 }
1623 }
1624 }
1625
1626 mCachedValidateSamplersResult = true;
1627 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628}
1629
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001630bool Program::isValidated() const
1631{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 return mValidated;
1633}
1634
Geoff Lange1a27752015-10-05 13:16:04 -04001635GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001637 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638}
1639
Jiajia Qin729b2c62017-08-14 09:36:11 +08001640GLuint Program::getActiveShaderStorageBlockCount() const
1641{
1642 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1643}
1644
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1646{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001647 ASSERT(
1648 uniformBlockIndex <
1649 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650
Jiajia Qin729b2c62017-08-14 09:36:11 +08001651 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652
1653 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001654 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655 std::string string = uniformBlock.name;
1656
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001660 }
jchen10fd7c3b52017-03-21 15:36:03 +08001661 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001662 }
1663}
1664
Geoff Lange1a27752015-10-05 13:16:04 -04001665GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001666{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 int maxLength = 0;
1668
1669 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001670 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001671 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1673 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001674 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675 if (!uniformBlock.name.empty())
1676 {
jchen10af713a22017-04-19 09:10:56 +08001677 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1678 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679 }
1680 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001681 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682
1683 return maxLength;
1684}
1685
Geoff Lange1a27752015-10-05 13:16:04 -04001686GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001689 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001690
Jamie Madill48ef11b2016-04-27 15:21:52 -04001691 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001692 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1693 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001694 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001695 if (uniformBlock.name == baseName)
1696 {
1697 const bool arrayElementZero =
1698 (subscript == GL_INVALID_INDEX &&
1699 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1700 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1701 {
1702 return blockIndex;
1703 }
1704 }
1705 }
1706
1707 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001708}
1709
Jiajia Qin729b2c62017-08-14 09:36:11 +08001710const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001711{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001712 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1713 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001714}
1715
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001716void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1717{
jchen107a20b972017-06-13 14:25:26 +08001718 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001719 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001720 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001721}
1722
1723GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1724{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001725 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001726}
1727
Jiajia Qin729b2c62017-08-14 09:36:11 +08001728GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1729{
1730 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1731}
1732
Geoff Lang48dcae72014-02-05 16:28:24 -05001733void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1734{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001735 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001736 for (GLsizei i = 0; i < count; i++)
1737 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001738 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001739 }
1740
Jamie Madill48ef11b2016-04-27 15:21:52 -04001741 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001742}
1743
1744void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1745{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001746 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001747 {
jchen10a9042d32017-03-17 08:50:45 +08001748 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1749 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1750 std::string varName = var.nameWithArrayIndex();
1751 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001752 if (length)
1753 {
1754 *length = lastNameIdx;
1755 }
1756 if (size)
1757 {
jchen10a9042d32017-03-17 08:50:45 +08001758 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001759 }
1760 if (type)
1761 {
jchen10a9042d32017-03-17 08:50:45 +08001762 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001763 }
1764 if (name)
1765 {
jchen10a9042d32017-03-17 08:50:45 +08001766 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001767 name[lastNameIdx] = '\0';
1768 }
1769 }
1770}
1771
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001772GLsizei Program::getTransformFeedbackVaryingCount() const
1773{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001774 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001775 {
jchen10a9042d32017-03-17 08:50:45 +08001776 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001777 }
1778 else
1779 {
1780 return 0;
1781 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001782}
1783
1784GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1785{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001786 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001787 {
1788 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001789 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001790 {
jchen10a9042d32017-03-17 08:50:45 +08001791 maxSize =
1792 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001793 }
1794
1795 return maxSize;
1796 }
1797 else
1798 {
1799 return 0;
1800 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001801}
1802
1803GLenum Program::getTransformFeedbackBufferMode() const
1804{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001805 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806}
1807
Jamie Madillbd044ed2017-06-05 12:59:21 -04001808bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001810 Shader *vertexShader = mState.mAttachedVertexShader;
1811 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001812
Jamie Madillbd044ed2017-06-05 12:59:21 -04001813 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001814
Jamie Madillbd044ed2017-06-05 12:59:21 -04001815 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1816 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817
Sami Väisänen46eaa942016-06-29 10:26:37 +03001818 std::map<GLuint, std::string> staticFragmentInputLocations;
1819
Jamie Madill4cff2472015-08-21 16:53:18 -04001820 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 bool matched = false;
1823
1824 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001825 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826 {
1827 continue;
1828 }
1829
Jamie Madill4cff2472015-08-21 16:53:18 -04001830 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001832 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001834 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001835 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001836 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837 {
1838 return false;
1839 }
1840
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 matched = true;
1842 break;
1843 }
1844 }
1845
1846 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001847 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001849 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 return false;
1851 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001852
1853 // Check for aliased path rendering input bindings (if any).
1854 // If more than one binding refer statically to the same
1855 // location the link must fail.
1856
1857 if (!output.staticUse)
1858 continue;
1859
1860 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1861 if (inputBinding == -1)
1862 continue;
1863
1864 const auto it = staticFragmentInputLocations.find(inputBinding);
1865 if (it == std::end(staticFragmentInputLocations))
1866 {
1867 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1868 }
1869 else
1870 {
1871 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1872 << it->second;
1873 return false;
1874 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875 }
1876
Jamie Madillbd044ed2017-06-05 12:59:21 -04001877 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001878 {
1879 return false;
1880 }
1881
Jamie Madillada9ecc2015-08-17 12:53:37 -04001882 // TODO(jmadill): verify no unmatched vertex varyings?
1883
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 return true;
1885}
1886
Jamie Madillbd044ed2017-06-05 12:59:21 -04001887bool Program::linkUniforms(const Context *context,
1888 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001889 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001890{
Olli Etuahob78707c2017-03-09 15:03:11 +00001891 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001892 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001893 {
1894 return false;
1895 }
1896
Olli Etuahob78707c2017-03-09 15:03:11 +00001897 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001898
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001899 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001900
jchen10eaef1e52017-06-13 10:44:11 +08001901 if (!linkAtomicCounterBuffers())
1902 {
1903 return false;
1904 }
1905
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001906 return true;
1907}
1908
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001909void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001910{
Jamie Madill982f6e02017-06-07 14:33:04 -04001911 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1912 unsigned int low = high;
1913
jchen10eaef1e52017-06-13 10:44:11 +08001914 for (auto counterIter = mState.mUniforms.rbegin();
1915 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1916 {
1917 --low;
1918 }
1919
1920 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1921
1922 high = low;
1923
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001924 for (auto imageIter = mState.mUniforms.rbegin();
1925 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1926 {
1927 --low;
1928 }
1929
1930 mState.mImageUniformRange = RangeUI(low, high);
1931
1932 // If uniform is a image type, insert it into the mImageBindings array.
1933 for (unsigned int imageIndex : mState.mImageUniformRange)
1934 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001935 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1936 // cannot load values into a uniform defined as an image. if declare without a
1937 // binding qualifier, any uniform image variable (include all elements of
1938 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001939 auto &imageUniform = mState.mUniforms[imageIndex];
1940 if (imageUniform.binding == -1)
1941 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001942 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001943 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001944 else
1945 {
1946 mState.mImageBindings.emplace_back(
1947 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1948 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001949 }
1950
1951 high = low;
1952
1953 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001954 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001955 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001956 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001957 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001958
1959 mState.mSamplerUniformRange = RangeUI(low, high);
1960
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001961 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001962 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001963 {
1964 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1965 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1966 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001967 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001968 }
1969}
1970
jchen10eaef1e52017-06-13 10:44:11 +08001971bool Program::linkAtomicCounterBuffers()
1972{
1973 for (unsigned int index : mState.mAtomicCounterUniformRange)
1974 {
1975 auto &uniform = mState.mUniforms[index];
1976 bool found = false;
1977 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1978 ++bufferIndex)
1979 {
1980 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1981 if (buffer.binding == uniform.binding)
1982 {
1983 buffer.memberIndexes.push_back(index);
1984 uniform.bufferIndex = bufferIndex;
1985 found = true;
1986 break;
1987 }
1988 }
1989 if (!found)
1990 {
1991 AtomicCounterBuffer atomicCounterBuffer;
1992 atomicCounterBuffer.binding = uniform.binding;
1993 atomicCounterBuffer.memberIndexes.push_back(index);
1994 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1995 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1996 }
1997 }
1998 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1999 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2000
2001 return true;
2002}
2003
Martin Radev4c4c8e72016-08-04 12:25:34 +03002004bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2005 const std::string &uniformName,
2006 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002007 const sh::InterfaceBlockField &fragmentUniform,
2008 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002010 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2011 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2012 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002013 {
2014 return false;
2015 }
2016
2017 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2018 {
Jamie Madillf6113162015-05-07 11:49:21 -04002019 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020 return false;
2021 }
2022
2023 return true;
2024}
2025
Jamie Madilleb979bf2016-11-15 12:28:46 -05002026// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002027bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002029 const ContextState &data = context->getContextState();
2030 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002031
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002033 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002034 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002035
2036 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002037 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002038 {
Jamie Madillf6113162015-05-07 11:49:21 -04002039 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002040 return false;
2041 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002043 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002044
Jamie Madillc349ec02015-08-21 16:53:12 -04002045 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002046 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002047 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002048 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002049 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002050 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002051 attribute.location = bindingLocation;
2052 }
2053
2054 if (attribute.location != -1)
2055 {
2056 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002057 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058
Jamie Madill63805b42015-08-25 13:17:39 -04002059 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002060 {
Jamie Madillf6113162015-05-07 11:49:21 -04002061 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002062 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063
2064 return false;
2065 }
2066
Jamie Madill63805b42015-08-25 13:17:39 -04002067 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 {
Jamie Madill63805b42015-08-25 13:17:39 -04002069 const int regLocation = attribute.location + reg;
2070 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071
2072 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002073 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002074 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002076 // TODO(jmadill): fix aliasing on ES2
2077 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002079 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002080 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 return false;
2082 }
2083 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002084 else
2085 {
Jamie Madill63805b42015-08-25 13:17:39 -04002086 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002087 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088
Jamie Madill63805b42015-08-25 13:17:39 -04002089 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 }
2091 }
2092 }
2093
2094 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002095 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002097 // Not set by glBindAttribLocation or by location layout qualifier
2098 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 {
Jamie Madill63805b42015-08-25 13:17:39 -04002100 int regs = VariableRegisterCount(attribute.type);
2101 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102
Jamie Madill63805b42015-08-25 13:17:39 -04002103 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 {
Jamie Madillf6113162015-05-07 11:49:21 -04002105 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002106 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 }
2108
Jamie Madillc349ec02015-08-21 16:53:12 -04002109 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 }
2111 }
2112
Jamie Madill48ef11b2016-04-27 15:21:52 -04002113 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114 {
Jamie Madill63805b42015-08-25 13:17:39 -04002115 ASSERT(attribute.location != -1);
2116 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002117
Jamie Madill63805b42015-08-25 13:17:39 -04002118 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002120 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 }
2122 }
2123
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124 return true;
2125}
2126
Martin Radev4c4c8e72016-08-04 12:25:34 +03002127bool Program::validateVertexAndFragmentInterfaceBlocks(
2128 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2129 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002130 InfoLog &infoLog,
2131 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002132{
2133 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002134 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2135 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002136
2137 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2138 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002139 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002140 }
2141
Jamie Madille473dee2015-08-18 14:49:01 -04002142 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002144 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2145 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146 {
2147 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002148 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2149 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150 {
2151 return false;
2152 }
2153 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002154 // TODO(jiajia.qin@intel.com): Add
2155 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002156 }
2157 return true;
2158}
Jamie Madille473dee2015-08-18 14:49:01 -04002159
Jiajia Qin729b2c62017-08-14 09:36:11 +08002160bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002161{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002162 const auto &caps = context->getCaps();
2163
Martin Radev4c4c8e72016-08-04 12:25:34 +03002164 if (mState.mAttachedComputeShader)
2165 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002166 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002167 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002168
Jiajia Qin729b2c62017-08-14 09:36:11 +08002169 if (!validateInterfaceBlocksCount(
2170 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002171 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2172 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002174 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002176
2177 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2178 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2179 computeShaderStorageBlocks,
2180 "Compute shader shader storage block count exceeds "
2181 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2182 infoLog))
2183 {
2184 return false;
2185 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002186 return true;
2187 }
2188
Jamie Madillbd044ed2017-06-05 12:59:21 -04002189 Shader &vertexShader = *mState.mAttachedVertexShader;
2190 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002191
Jiajia Qin729b2c62017-08-14 09:36:11 +08002192 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2193 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002194
Jiajia Qin729b2c62017-08-14 09:36:11 +08002195 if (!validateInterfaceBlocksCount(
2196 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002197 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2198 {
2199 return false;
2200 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002201 if (!validateInterfaceBlocksCount(
2202 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002203 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2204 infoLog))
2205 {
2206
2207 return false;
2208 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002209
2210 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002211 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002212 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002213 {
2214 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002215 }
Jamie Madille473dee2015-08-18 14:49:01 -04002216
Jiajia Qin729b2c62017-08-14 09:36:11 +08002217 if (context->getClientVersion() >= Version(3, 1))
2218 {
2219 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2220 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2221
2222 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2223 vertexShaderStorageBlocks,
2224 "Vertex shader shader storage block count exceeds "
2225 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2226 infoLog))
2227 {
2228 return false;
2229 }
2230 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2231 fragmentShaderStorageBlocks,
2232 "Fragment shader shader storage block count exceeds "
2233 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2234 infoLog))
2235 {
2236
2237 return false;
2238 }
2239
2240 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2241 fragmentShaderStorageBlocks, infoLog,
2242 webglCompatibility))
2243 {
2244 return false;
2245 }
2246 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002247 return true;
2248}
2249
Jamie Madilla2c74982016-12-12 11:20:42 -05002250bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002251 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002252 const sh::InterfaceBlock &fragmentInterfaceBlock,
2253 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002254{
2255 const char* blockName = vertexInterfaceBlock.name.c_str();
2256 // validate blocks for the same member types
2257 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2258 {
Jamie Madillf6113162015-05-07 11:49:21 -04002259 infoLog << "Types for interface block '" << blockName
2260 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002261 return false;
2262 }
2263 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2264 {
Jamie Madillf6113162015-05-07 11:49:21 -04002265 infoLog << "Array sizes differ for interface block '" << blockName
2266 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002267 return false;
2268 }
jchen10af713a22017-04-19 09:10:56 +08002269 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2270 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2271 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002272 {
Jamie Madillf6113162015-05-07 11:49:21 -04002273 infoLog << "Layout qualifiers differ for interface block '" << blockName
2274 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002275 return false;
2276 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002277 const unsigned int numBlockMembers =
2278 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002279 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2280 {
2281 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2282 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2283 if (vertexMember.name != fragmentMember.name)
2284 {
Jamie Madillf6113162015-05-07 11:49:21 -04002285 infoLog << "Name mismatch for field " << blockMemberIndex
2286 << " of interface block '" << blockName
2287 << "': (in vertex: '" << vertexMember.name
2288 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289 return false;
2290 }
2291 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002292 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2293 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294 {
2295 return false;
2296 }
2297 }
2298 return true;
2299}
2300
2301bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2302 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2303{
2304 if (vertexVariable.type != fragmentVariable.type)
2305 {
Jamie Madillf6113162015-05-07 11:49:21 -04002306 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002307 return false;
2308 }
2309 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2310 {
Jamie Madillf6113162015-05-07 11:49:21 -04002311 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002312 return false;
2313 }
2314 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2315 {
Jamie Madillf6113162015-05-07 11:49:21 -04002316 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002317 return false;
2318 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002319 if (vertexVariable.structName != fragmentVariable.structName)
2320 {
2321 infoLog << "Structure names for " << variableName
2322 << " differ between vertex and fragment shaders";
2323 return false;
2324 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325
2326 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2327 {
Jamie Madillf6113162015-05-07 11:49:21 -04002328 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002329 return false;
2330 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002331 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002332 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2333 {
2334 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2335 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2336
2337 if (vertexMember.name != fragmentMember.name)
2338 {
Jamie Madillf6113162015-05-07 11:49:21 -04002339 infoLog << "Name mismatch for field '" << memberIndex
2340 << "' of " << variableName
2341 << ": (in vertex: '" << vertexMember.name
2342 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343 return false;
2344 }
2345
2346 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2347 vertexMember.name + "'";
2348
2349 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2350 {
2351 return false;
2352 }
2353 }
2354
2355 return true;
2356}
2357
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002358bool Program::linkValidateVaryings(InfoLog &infoLog,
2359 const std::string &varyingName,
2360 const sh::Varying &vertexVarying,
2361 const sh::Varying &fragmentVarying,
2362 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363{
2364 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2365 {
2366 return false;
2367 }
2368
Jamie Madille9cc4692015-02-19 16:00:13 -05002369 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002371 infoLog << "Interpolation types for " << varyingName
2372 << " differ between vertex and fragment shaders.";
2373 return false;
2374 }
2375
2376 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2377 {
2378 infoLog << "Invariance for " << varyingName
2379 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002380 return false;
2381 }
2382
2383 return true;
2384}
2385
Jamie Madillbd044ed2017-06-05 12:59:21 -04002386bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002387{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002388 Shader *vertexShader = mState.mAttachedVertexShader;
2389 Shader *fragmentShader = mState.mAttachedFragmentShader;
2390 const auto &vertexVaryings = vertexShader->getVaryings(context);
2391 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2392 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002393
2394 if (shaderVersion != 100)
2395 {
2396 // Only ESSL 1.0 has restrictions on matching input and output invariance
2397 return true;
2398 }
2399
2400 bool glPositionIsInvariant = false;
2401 bool glPointSizeIsInvariant = false;
2402 bool glFragCoordIsInvariant = false;
2403 bool glPointCoordIsInvariant = false;
2404
2405 for (const sh::Varying &varying : vertexVaryings)
2406 {
2407 if (!varying.isBuiltIn())
2408 {
2409 continue;
2410 }
2411 if (varying.name.compare("gl_Position") == 0)
2412 {
2413 glPositionIsInvariant = varying.isInvariant;
2414 }
2415 else if (varying.name.compare("gl_PointSize") == 0)
2416 {
2417 glPointSizeIsInvariant = varying.isInvariant;
2418 }
2419 }
2420
2421 for (const sh::Varying &varying : fragmentVaryings)
2422 {
2423 if (!varying.isBuiltIn())
2424 {
2425 continue;
2426 }
2427 if (varying.name.compare("gl_FragCoord") == 0)
2428 {
2429 glFragCoordIsInvariant = varying.isInvariant;
2430 }
2431 else if (varying.name.compare("gl_PointCoord") == 0)
2432 {
2433 glPointCoordIsInvariant = varying.isInvariant;
2434 }
2435 }
2436
2437 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2438 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2439 // Not requiring invariance to match is supported by:
2440 // dEQP, WebGL CTS, Nexus 5X GLES
2441 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2442 {
2443 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2444 "declared invariant.";
2445 return false;
2446 }
2447 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2448 {
2449 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2450 "declared invariant.";
2451 return false;
2452 }
2453
2454 return true;
2455}
2456
jchen10a9042d32017-03-17 08:50:45 +08002457bool Program::linkValidateTransformFeedback(const gl::Context *context,
2458 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002459 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002460 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002461{
2462 size_t totalComponents = 0;
2463
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464 std::set<std::string> uniqueNames;
2465
Jamie Madill48ef11b2016-04-27 15:21:52 -04002466 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002467 {
2468 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002469 size_t subscript = GL_INVALID_INDEX;
2470 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2471
Jamie Madill192745a2016-12-22 15:58:21 -05002472 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 {
Jamie Madill192745a2016-12-22 15:58:21 -05002474 const sh::Varying *varying = ref.second.get();
2475
jchen10a9042d32017-03-17 08:50:45 +08002476 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002478 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002479 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002480 infoLog << "Two transform feedback varyings specify the same output variable ("
2481 << tfVaryingName << ").";
2482 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002483 }
jchen10a9042d32017-03-17 08:50:45 +08002484 if (context->getClientVersion() >= Version(3, 1))
2485 {
2486 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2487 {
2488 infoLog
2489 << "Two transform feedback varyings include the same array element ("
2490 << tfVaryingName << ").";
2491 return false;
2492 }
2493 }
2494 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002495 {
2496 infoLog << "Capture of arrays is undefined and not supported.";
2497 return false;
2498 }
2499
jchen10a9042d32017-03-17 08:50:45 +08002500 uniqueNames.insert(tfVaryingName);
2501
Jamie Madillccdf74b2015-08-18 10:46:12 -04002502 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002503 size_t elementCount =
2504 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2505 : 1);
2506 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002507 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002508 componentCount > caps.maxTransformFeedbackSeparateComponents)
2509 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002510 infoLog << "Transform feedback varying's " << varying->name << " components ("
2511 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002512 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002513 return false;
2514 }
2515
2516 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002517 found = true;
2518 break;
2519 }
2520 }
jchen10a9042d32017-03-17 08:50:45 +08002521 if (context->getClientVersion() < Version(3, 1) &&
2522 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002523 {
Geoff Lang1a683462015-09-29 15:09:59 -04002524 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002525 return false;
2526 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002527 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2528 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002529 ASSERT(found);
2530 }
2531
Jamie Madill48ef11b2016-04-27 15:21:52 -04002532 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002533 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002534 {
Jamie Madillf6113162015-05-07 11:49:21 -04002535 infoLog << "Transform feedback varying total components (" << totalComponents
2536 << ") exceed the maximum interleaved components ("
2537 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002538 return false;
2539 }
2540
2541 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002542}
2543
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002544bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2545{
2546 const std::vector<sh::Uniform> &vertexUniforms =
2547 mState.mAttachedVertexShader->getUniforms(context);
2548 const std::vector<sh::Uniform> &fragmentUniforms =
2549 mState.mAttachedFragmentShader->getUniforms(context);
2550 const std::vector<sh::Attribute> &attributes =
2551 mState.mAttachedVertexShader->getActiveAttributes(context);
2552 for (const auto &attrib : attributes)
2553 {
2554 for (const auto &uniform : vertexUniforms)
2555 {
2556 if (uniform.name == attrib.name)
2557 {
2558 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2559 return false;
2560 }
2561 }
2562 for (const auto &uniform : fragmentUniforms)
2563 {
2564 if (uniform.name == attrib.name)
2565 {
2566 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2567 return false;
2568 }
2569 }
2570 }
2571 return true;
2572}
2573
Jamie Madill192745a2016-12-22 15:58:21 -05002574void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002575{
2576 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002577 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002578 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002579 {
jchen10a9042d32017-03-17 08:50:45 +08002580 size_t subscript = GL_INVALID_INDEX;
2581 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002582 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002583 {
Jamie Madill192745a2016-12-22 15:58:21 -05002584 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002585 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002586 {
jchen10a9042d32017-03-17 08:50:45 +08002587 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2588 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002589 break;
2590 }
2591 }
2592 }
2593}
2594
Jamie Madillbd044ed2017-06-05 12:59:21 -04002595Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002596{
Jamie Madill192745a2016-12-22 15:58:21 -05002597 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002598
Jamie Madillbd044ed2017-06-05 12:59:21 -04002599 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002600 {
Jamie Madill192745a2016-12-22 15:58:21 -05002601 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002602 }
2603
Jamie Madillbd044ed2017-06-05 12:59:21 -04002604 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002605 {
Jamie Madill192745a2016-12-22 15:58:21 -05002606 merged[varying.name].fragment = &varying;
2607 }
2608
2609 return merged;
2610}
2611
2612std::vector<PackedVarying> Program::getPackedVaryings(
2613 const Program::MergedVaryings &mergedVaryings) const
2614{
2615 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2616 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002617 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002618
2619 for (const auto &ref : mergedVaryings)
2620 {
2621 const sh::Varying *input = ref.second.vertex;
2622 const sh::Varying *output = ref.second.fragment;
2623
2624 // Only pack varyings that have a matched input or output, plus special builtins.
2625 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002626 {
Jamie Madill192745a2016-12-22 15:58:21 -05002627 // Will get the vertex shader interpolation by default.
2628 auto interpolation = ref.second.get()->interpolation;
2629
Olli Etuaho06a06f52017-07-12 12:22:15 +03002630 // Note that we lose the vertex shader static use information here. The data for the
2631 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002632 if (output->isStruct())
2633 {
2634 ASSERT(!output->isArray());
2635 for (const auto &field : output->fields)
2636 {
2637 ASSERT(!field.isStruct() && !field.isArray());
2638 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2639 }
2640 }
2641 else
2642 {
2643 packedVaryings.push_back(PackedVarying(*output, interpolation));
2644 }
2645 continue;
2646 }
2647
2648 // Keep Transform FB varyings in the merged list always.
2649 if (!input)
2650 {
2651 continue;
2652 }
2653
2654 for (const std::string &tfVarying : tfVaryings)
2655 {
jchen10a9042d32017-03-17 08:50:45 +08002656 size_t subscript = GL_INVALID_INDEX;
2657 std::string baseName = ParseResourceName(tfVarying, &subscript);
2658 if (uniqueFullNames.count(tfVarying) > 0)
2659 {
2660 continue;
2661 }
2662 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002663 {
2664 // Transform feedback for varying structs is underspecified.
2665 // See Khronos bug 9856.
2666 // TODO(jmadill): Figure out how to be spec-compliant here.
2667 if (!input->isStruct())
2668 {
2669 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2670 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002671 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2672 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002673 }
jchen10a9042d32017-03-17 08:50:45 +08002674 if (subscript == GL_INVALID_INDEX)
2675 {
2676 break;
2677 }
Jamie Madill192745a2016-12-22 15:58:21 -05002678 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002679 }
2680 }
2681
Jamie Madill192745a2016-12-22 15:58:21 -05002682 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2683
2684 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002685}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002686
Jamie Madillbd044ed2017-06-05 12:59:21 -04002687void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002688{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002689 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002690 ASSERT(fragmentShader != nullptr);
2691
Geoff Lange0cff192017-05-30 13:04:56 -04002692 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002693 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002694
2695 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002696 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002697 {
2698 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2699 outputVariable.name != "gl_FragData")
2700 {
2701 continue;
2702 }
2703
2704 unsigned int baseLocation =
2705 (outputVariable.location == -1 ? 0u
2706 : static_cast<unsigned int>(outputVariable.location));
2707 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2708 elementIndex++)
2709 {
2710 const unsigned int location = baseLocation + elementIndex;
2711 if (location >= mState.mOutputVariableTypes.size())
2712 {
2713 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2714 }
Corentin Walleze7557742017-06-01 13:09:57 -04002715 ASSERT(location < mState.mActiveOutputVariables.size());
2716 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002717 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2718 }
2719 }
2720
Jamie Madill80a6fc02015-08-21 16:53:16 -04002721 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002722 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002723 return;
2724
Jamie Madillbd044ed2017-06-05 12:59:21 -04002725 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002726 // TODO(jmadill): any caps validation here?
2727
jchen1015015f72017-03-16 13:54:21 +08002728 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002729 outputVariableIndex++)
2730 {
jchen1015015f72017-03-16 13:54:21 +08002731 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002732
2733 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2734 if (outputVariable.isBuiltIn())
2735 continue;
2736
2737 // Since multiple output locations must be specified, use 0 for non-specified locations.
2738 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2739
Jamie Madill80a6fc02015-08-21 16:53:16 -04002740 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2741 elementIndex++)
2742 {
2743 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002744 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002745 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madillfb997ec2017-09-20 15:44:27 -04002746 mState.mOutputLocations[location] = VariableLocation(element, outputVariableIndex);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002747 }
2748 }
2749}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002750
Olli Etuaho48fed632017-03-16 12:05:30 +00002751void Program::setUniformValuesFromBindingQualifiers()
2752{
Jamie Madill982f6e02017-06-07 14:33:04 -04002753 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002754 {
2755 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2756 if (samplerUniform.binding != -1)
2757 {
2758 GLint location = mState.getUniformLocation(samplerUniform.name);
2759 ASSERT(location != -1);
2760 std::vector<GLint> boundTextureUnits;
2761 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2762 ++elementIndex)
2763 {
2764 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2765 }
2766 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2767 boundTextureUnits.data());
2768 }
2769 }
2770}
2771
jchen10eaef1e52017-06-13 10:44:11 +08002772void Program::gatherAtomicCounterBuffers()
2773{
jchen10baf5d942017-08-28 20:45:48 +08002774 for (unsigned int index : mState.mAtomicCounterUniformRange)
2775 {
2776 auto &uniform = mState.mUniforms[index];
2777 uniform.blockInfo.offset = uniform.offset;
2778 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2779 uniform.blockInfo.matrixStride = 0;
2780 uniform.blockInfo.isRowMajorMatrix = false;
2781 }
2782
jchen10eaef1e52017-06-13 10:44:11 +08002783 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2784}
2785
Jiajia Qin729b2c62017-08-14 09:36:11 +08002786void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002787{
Jiajia Qin729b2c62017-08-14 09:36:11 +08002788 for (const sh::InterfaceBlock &computeBlock : computeBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002789 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002790
Jiajia Qin729b2c62017-08-14 09:36:11 +08002791 // Only 'packed' blocks are allowed to be considered inactive.
2792 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2793 continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002794
Jiajia Qin729b2c62017-08-14 09:36:11 +08002795 defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002796 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002797}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002798
Jiajia Qin729b2c62017-08-14 09:36:11 +08002799void Program::gatherVertexAndFragmentBlockInfo(
2800 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2801 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks)
2802{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002803 std::set<std::string> visitedList;
2804
Jiajia Qin729b2c62017-08-14 09:36:11 +08002805 for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002806 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002807 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002808 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2809 continue;
2810
Jiajia Qin729b2c62017-08-14 09:36:11 +08002811 defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002812 visitedList.insert(vertexBlock.name);
2813 }
2814
Jiajia Qin729b2c62017-08-14 09:36:11 +08002815 for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002816 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002817 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2819 continue;
2820
2821 if (visitedList.count(fragmentBlock.name) > 0)
2822 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002823 if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002824 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002825 for (InterfaceBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002827 if (block.name == fragmentBlock.name)
2828 {
2829 block.fragmentStaticUse = fragmentBlock.staticUse;
2830 }
2831 }
2832 }
2833 else
2834 {
2835 ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2836 for (InterfaceBlock &block : mState.mShaderStorageBlocks)
2837 {
2838 if (block.name == fragmentBlock.name)
2839 {
2840 block.fragmentStaticUse = fragmentBlock.staticUse;
2841 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842 }
2843 }
2844
2845 continue;
2846 }
2847
Jiajia Qin729b2c62017-08-14 09:36:11 +08002848 defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002849 visitedList.insert(fragmentBlock.name);
2850 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002851}
2852
2853void Program::gatherInterfaceBlockInfo(const Context *context)
2854{
2855 ASSERT(mState.mUniformBlocks.empty());
2856 ASSERT(mState.mShaderStorageBlocks.empty());
2857
2858 if (mState.mAttachedComputeShader)
2859 {
2860 Shader *computeShader = mState.getAttachedComputeShader();
2861
2862 gatherComputeBlockInfo(computeShader->getUniformBlocks(context));
2863 gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context));
2864 return;
2865 }
2866
2867 Shader *vertexShader = mState.getAttachedVertexShader();
2868 Shader *fragmentShader = mState.getAttachedFragmentShader();
2869
2870 gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context),
2871 fragmentShader->getUniformBlocks(context));
2872 if (context->getClientVersion() >= Version(3, 1))
2873 {
2874 gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context),
2875 fragmentShader->getShaderStorageBlocks(context));
2876 }
2877
jchen10af713a22017-04-19 09:10:56 +08002878 // Set initial bindings from shader.
2879 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2880 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002881 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002882 bindUniformBlock(blockIndex, uniformBlock.binding);
2883 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002884}
2885
Jamie Madill4a3c2342015-10-08 12:58:45 -04002886template <typename VarT>
2887void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2888 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002889 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002890 int blockIndex)
2891{
2892 for (const VarT &field : fields)
2893 {
2894 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2895
Olli Etuaho855d9642017-05-17 14:05:06 +03002896 const std::string &fullMappedName =
2897 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2898
Jamie Madill4a3c2342015-10-08 12:58:45 -04002899 if (field.isStruct())
2900 {
2901 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2902 {
2903 const std::string uniformElementName =
2904 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002905 const std::string uniformElementMappedName =
2906 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2907 defineUniformBlockMembers(field.fields, uniformElementName,
2908 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002909 }
2910 }
2911 else
2912 {
2913 // If getBlockMemberInfo returns false, the uniform is optimized out.
2914 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002915 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002916 {
2917 continue;
2918 }
2919
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002920 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002921 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002922 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002923
2924 // Since block uniforms have no location, we don't need to store them in the uniform
2925 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002926 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002927 }
2928 }
2929}
2930
Jiajia Qin729b2c62017-08-14 09:36:11 +08002931void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002932{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002933 size_t blockSize = 0;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002934 std::vector<unsigned int> blockIndexes;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002935
Jiajia Qin729b2c62017-08-14 09:36:11 +08002936 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002937 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002938 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
2939 // Track the first and last uniform index to determine the range of active uniforms in the
2940 // block.
2941 size_t firstBlockUniformIndex = mState.mUniforms.size();
2942 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2943 interfaceBlock.fieldMappedPrefix(), blockIndex);
2944 size_t lastBlockUniformIndex = mState.mUniforms.size();
2945
2946 for (size_t blockUniformIndex = firstBlockUniformIndex;
2947 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2948 {
2949 blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2950 }
2951 }
2952 else
2953 {
2954 // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index.
2955 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002956 }
jchen10af713a22017-04-19 09:10:56 +08002957 // ESSL 3.10 section 4.4.4 page 58:
2958 // Any uniform or shader storage block declared without a binding qualifier is initially
2959 // assigned to block binding point zero.
2960 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002961 if (interfaceBlock.arraySize > 0)
2962 {
2963 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2964 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002965 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
2966 // of UniformBlock and ShaderStorageBlock.
2967 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08002968 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002969 // Don't define this block at all if it's not active in the implementation.
2970 if (!mProgram->getUniformBlockSize(
2971 interfaceBlock.name + ArrayString(arrayElement),
2972 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
2973 {
2974 continue;
2975 }
jchen10af713a22017-04-19 09:10:56 +08002976 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002977
2978 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
2979 blockBinding + arrayElement);
2980 block.memberIndexes = blockIndexes;
jchen10baf5d942017-08-28 20:45:48 +08002981 MarkResourceStaticUse(&block, shaderType, interfaceBlock.staticUse);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002982
Jiajia Qin729b2c62017-08-14 09:36:11 +08002983 // Since all block elements in an array share the same active interface blocks, they
2984 // will all be active once any block member is used. So, since interfaceBlock.name[0]
2985 // was active, here we will add every block element in the array.
Qin Jiajia0350a642016-11-01 17:01:51 +08002986 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002987 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
2988 {
2989 mState.mUniformBlocks.push_back(block);
2990 }
2991 else
2992 {
2993 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2994 mState.mShaderStorageBlocks.push_back(block);
2995 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002996 }
2997 }
2998 else
2999 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003000 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3001 // of UniformBlock and ShaderStorageBlock.
3002 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003003 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003004 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
3005 &blockSize))
3006 {
3007 return;
3008 }
jchen10af713a22017-04-19 09:10:56 +08003009 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003010
3011 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0,
3012 blockBinding);
3013 block.memberIndexes = blockIndexes;
jchen10baf5d942017-08-28 20:45:48 +08003014 MarkResourceStaticUse(&block, shaderType, interfaceBlock.staticUse);
Jamie Madill4a3c2342015-10-08 12:58:45 -04003015 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003016 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3017 {
3018 mState.mUniformBlocks.push_back(block);
3019 }
3020 else
3021 {
3022 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3023 mState.mShaderStorageBlocks.push_back(block);
3024 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003025 }
3026}
3027
Jamie Madille7d84322017-01-10 18:21:59 -05003028void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003029 GLsizei clampedCount,
3030 const GLint *v)
3031{
Jamie Madill81c2e252017-09-09 23:32:46 -04003032 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3033 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3034 std::vector<GLuint> *boundTextureUnits =
3035 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003036
Jamie Madill81c2e252017-09-09 23:32:46 -04003037 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
Jamie Madilld68248b2017-09-11 14:34:14 -04003038
3039 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003040 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003041}
3042
3043template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003044GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3045 GLsizei count,
3046 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003047 const T *v)
3048{
Jamie Madill134f93d2017-08-31 17:11:00 -04003049 if (count == 1)
3050 return 1;
3051
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003052 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003053
Corentin Wallez15ac5342016-11-03 17:06:39 -04003054 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3055 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003056 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003057 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003058 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003059
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003060 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003061 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003062 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003063 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003064
3065 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003066}
3067
3068template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003069GLsizei Program::clampMatrixUniformCount(GLint location,
3070 GLsizei count,
3071 GLboolean transpose,
3072 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003073{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003074 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3075
Jamie Madill62d31cb2015-09-11 13:25:51 -04003076 if (!transpose)
3077 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003078 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003079 }
3080
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003081 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003082
3083 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3084 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003085 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
3086 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003087}
3088
Jamie Madill54164b02017-08-28 15:17:37 -04003089// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3090// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003091template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003092void Program::getUniformInternal(const Context *context,
3093 DestT *dataOut,
3094 GLint location,
3095 GLenum nativeType,
3096 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003097{
Jamie Madill54164b02017-08-28 15:17:37 -04003098 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003099 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003100 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003101 {
3102 GLint tempValue[16] = {0};
3103 mProgram->getUniformiv(context, location, tempValue);
3104 UniformStateQueryCastLoop<GLboolean>(
3105 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003106 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003107 }
3108 case GL_INT:
3109 {
3110 GLint tempValue[16] = {0};
3111 mProgram->getUniformiv(context, location, tempValue);
3112 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3113 components);
3114 break;
3115 }
3116 case GL_UNSIGNED_INT:
3117 {
3118 GLuint tempValue[16] = {0};
3119 mProgram->getUniformuiv(context, location, tempValue);
3120 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3121 components);
3122 break;
3123 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003124 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003125 {
3126 GLfloat tempValue[16] = {0};
3127 mProgram->getUniformfv(context, location, tempValue);
3128 UniformStateQueryCastLoop<GLfloat>(
3129 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003130 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003131 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003132 default:
3133 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003134 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003135 }
3136}
Jamie Madilla4595b82017-01-11 17:36:34 -05003137
3138bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3139{
3140 // Must be called after samplers are validated.
3141 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3142
3143 for (const auto &binding : mState.mSamplerBindings)
3144 {
3145 GLenum textureType = binding.textureType;
3146 for (const auto &unit : binding.boundTextureUnits)
3147 {
3148 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3149 if (programTextureID == textureID)
3150 {
3151 // TODO(jmadill): Check for appropriate overlap.
3152 return true;
3153 }
3154 }
3155 }
3156
3157 return false;
3158}
3159
Jamie Madilla2c74982016-12-12 11:20:42 -05003160} // namespace gl