blob: fbac19258c31cb49d11e7c280c88be2002171421 [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
1137void Program::getInputResourceName(GLuint index,
1138 GLsizei bufSize,
1139 GLsizei *length,
1140 GLchar *name) const
1141{
1142 GLint size;
1143 GLenum type;
1144 getActiveAttribute(index, bufSize, length, &size, &type, name);
1145}
1146
1147void Program::getOutputResourceName(GLuint index,
1148 GLsizei bufSize,
1149 GLsizei *length,
1150 GLchar *name) const
1151{
1152 if (length)
1153 {
1154 *length = 0;
1155 }
1156
1157 if (!mLinked)
1158 {
1159 if (bufSize > 0)
1160 {
1161 name[0] = '\0';
1162 }
1163 return;
1164 }
1165 ASSERT(index < mState.mOutputVariables.size());
1166 const auto &output = mState.mOutputVariables[index];
1167
1168 if (bufSize > 0)
1169 {
1170 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1171
1172 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1173 }
1174}
1175
jchen10880683b2017-04-12 16:21:55 +08001176const sh::Attribute &Program::getInputResource(GLuint index) const
1177{
1178 ASSERT(index < mState.mAttributes.size());
1179 return mState.mAttributes[index];
1180}
1181
1182const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1183{
1184 ASSERT(index < mState.mOutputVariables.size());
1185 return mState.mOutputVariables[index];
1186}
1187
Geoff Lang7dd2e102014-11-10 15:19:26 -05001188GLint Program::getFragDataLocation(const std::string &name) const
1189{
1190 std::string baseName(name);
1191 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001192 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001193 {
Jamie Madillfb997ec2017-09-20 15:44:27 -04001194 const VariableLocation &locationInfo = outputPair.second;
1195 const sh::OutputVariable &outputVariable = mState.mOutputVariables[locationInfo.index];
1196 if (outputVariable.name == baseName &&
1197 (arrayIndex == GL_INVALID_INDEX || arrayIndex == locationInfo.element))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001199 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001200 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001201 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001203}
1204
Geoff Lange1a27752015-10-05 13:16:04 -04001205void Program::getActiveUniform(GLuint index,
1206 GLsizei bufsize,
1207 GLsizei *length,
1208 GLint *size,
1209 GLenum *type,
1210 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001211{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001212 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001213 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001214 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001215 ASSERT(index < mState.mUniforms.size());
1216 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217
1218 if (bufsize > 0)
1219 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001220 std::string string = uniform.name;
1221 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222 {
1223 string += "[0]";
1224 }
jchen10fd7c3b52017-03-21 15:36:03 +08001225 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226 }
1227
Jamie Madill62d31cb2015-09-11 13:25:51 -04001228 *size = uniform.elementCount();
1229 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001230 }
1231 else
1232 {
1233 if (bufsize > 0)
1234 {
1235 name[0] = '\0';
1236 }
1237
1238 if (length)
1239 {
1240 *length = 0;
1241 }
1242
1243 *size = 0;
1244 *type = GL_NONE;
1245 }
1246}
1247
Geoff Lange1a27752015-10-05 13:16:04 -04001248GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001249{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001250 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001251 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001252 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001253 }
1254 else
1255 {
1256 return 0;
1257 }
1258}
1259
Geoff Lange1a27752015-10-05 13:16:04 -04001260GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001261{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001262 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001263
1264 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001265 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001266 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001268 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 size_t length = uniform.name.length() + 1u;
1271 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272 {
1273 length += 3; // Counting in "[0]".
1274 }
1275 maxLength = std::max(length, maxLength);
1276 }
1277 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001278 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001279
Jamie Madill62d31cb2015-09-11 13:25:51 -04001280 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001281}
1282
1283GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1284{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001285 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001286 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001288 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1290 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1291 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001292 case GL_UNIFORM_BLOCK_INDEX:
1293 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001294 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1295 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1296 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1297 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1298 default:
1299 UNREACHABLE();
1300 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001301 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302 return 0;
1303}
1304
1305bool Program::isValidUniformLocation(GLint location) const
1306{
Jamie Madille2e406c2016-06-02 13:04:10 -04001307 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001308 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001309 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001310}
1311
Jamie Madill62d31cb2015-09-11 13:25:51 -04001312const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001314 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001315 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001316}
1317
Jamie Madillac4e9c32017-01-13 14:07:12 -05001318const VariableLocation &Program::getUniformLocation(GLint location) const
1319{
1320 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1321 return mState.mUniformLocations[location];
1322}
1323
1324const std::vector<VariableLocation> &Program::getUniformLocations() const
1325{
1326 return mState.mUniformLocations;
1327}
1328
1329const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1330{
1331 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1332 return mState.mUniforms[index];
1333}
1334
Jamie Madill62d31cb2015-09-11 13:25:51 -04001335GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001336{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001337 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338}
1339
Jamie Madill62d31cb2015-09-11 13:25:51 -04001340GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001341{
Jamie Madille7d84322017-01-10 18:21:59 -05001342 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343}
1344
1345void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1346{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001347 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1348 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001349 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350}
1351
1352void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1353{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001354 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1355 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001356 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357}
1358
1359void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1360{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001361 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1362 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001363 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364}
1365
1366void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1367{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001368 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1369 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001370 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371}
1372
Jamie Madill81c2e252017-09-09 23:32:46 -04001373Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001375 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1376 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1377
Jamie Madill81c2e252017-09-09 23:32:46 -04001378 mProgram->setUniform1iv(location, clampedCount, v);
1379
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001380 if (mState.isSamplerUniformIndex(locationInfo.index))
1381 {
1382 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001383 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001384 }
1385
Jamie Madill81c2e252017-09-09 23:32:46 -04001386 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387}
1388
1389void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1390{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001391 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1392 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001393 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394}
1395
1396void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1397{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001398 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1399 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001400 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401}
1402
1403void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1404{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001405 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1406 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001407 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408}
1409
1410void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1411{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001412 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1413 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001414 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
1417void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1418{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001419 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1420 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001421 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422}
1423
1424void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1425{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001426 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1427 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001428 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429}
1430
1431void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1432{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001433 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1434 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001435 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436}
1437
1438void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1439{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001440 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001441 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442}
1443
1444void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1445{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001446 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001447 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448}
1449
1450void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1451{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001452 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001453 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454}
1455
1456void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1457{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001458 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001459 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460}
1461
1462void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1463{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001464 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001465 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466}
1467
1468void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1469{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001470 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001471 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472}
1473
1474void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1475{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001476 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001477 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478}
1479
1480void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1481{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001482 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001483 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484}
1485
1486void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1487{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001488 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001489 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001490}
1491
Jamie Madill54164b02017-08-28 15:17:37 -04001492void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001493{
Jamie Madill54164b02017-08-28 15:17:37 -04001494 const auto &uniformLocation = mState.getUniformLocations()[location];
1495 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1496
1497 GLenum nativeType = gl::VariableComponentType(uniform.type);
1498 if (nativeType == GL_FLOAT)
1499 {
1500 mProgram->getUniformfv(context, location, v);
1501 }
1502 else
1503 {
1504 getUniformInternal(context, v, location, nativeType,
1505 gl::VariableComponentCount(uniform.type));
1506 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001507}
1508
Jamie Madill54164b02017-08-28 15:17:37 -04001509void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510{
Jamie Madill54164b02017-08-28 15:17:37 -04001511 const auto &uniformLocation = mState.getUniformLocations()[location];
1512 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1513
1514 GLenum nativeType = gl::VariableComponentType(uniform.type);
1515 if (nativeType == GL_INT || nativeType == GL_BOOL)
1516 {
1517 mProgram->getUniformiv(context, location, v);
1518 }
1519 else
1520 {
1521 getUniformInternal(context, v, location, nativeType,
1522 gl::VariableComponentCount(uniform.type));
1523 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524}
1525
Jamie Madill54164b02017-08-28 15:17:37 -04001526void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001527{
Jamie Madill54164b02017-08-28 15:17:37 -04001528 const auto &uniformLocation = mState.getUniformLocations()[location];
1529 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1530
1531 GLenum nativeType = gl::VariableComponentType(uniform.type);
1532 if (nativeType == GL_UNSIGNED_INT)
1533 {
1534 mProgram->getUniformuiv(context, location, v);
1535 }
1536 else
1537 {
1538 getUniformInternal(context, v, location, nativeType,
1539 gl::VariableComponentCount(uniform.type));
1540 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001541}
1542
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543void Program::flagForDeletion()
1544{
1545 mDeleteStatus = true;
1546}
1547
1548bool Program::isFlaggedForDeletion() const
1549{
1550 return mDeleteStatus;
1551}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001552
Brandon Jones43a53e22014-08-28 16:23:22 -07001553void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001554{
1555 mInfoLog.reset();
1556
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001558 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001559 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001560 }
1561 else
1562 {
Jamie Madillf6113162015-05-07 11:49:21 -04001563 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001564 }
1565}
1566
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1568{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001569 // Skip cache if we're using an infolog, so we get the full error.
1570 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1571 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1572 {
1573 return mCachedValidateSamplersResult.value();
1574 }
1575
1576 if (mTextureUnitTypesCache.empty())
1577 {
1578 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1579 }
1580 else
1581 {
1582 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1583 }
1584
1585 // if any two active samplers in a program are of different types, but refer to the same
1586 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1587 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001588 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001589 {
Jamie Madill54164b02017-08-28 15:17:37 -04001590 if (samplerBinding.unreferenced)
1591 continue;
1592
Jamie Madille7d84322017-01-10 18:21:59 -05001593 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001594
Jamie Madille7d84322017-01-10 18:21:59 -05001595 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001596 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001597 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1598 {
1599 if (infoLog)
1600 {
1601 (*infoLog) << "Sampler uniform (" << textureUnit
1602 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1603 << caps.maxCombinedTextureImageUnits << ")";
1604 }
1605
1606 mCachedValidateSamplersResult = false;
1607 return false;
1608 }
1609
1610 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1611 {
1612 if (textureType != mTextureUnitTypesCache[textureUnit])
1613 {
1614 if (infoLog)
1615 {
1616 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1617 "image unit ("
1618 << textureUnit << ").";
1619 }
1620
1621 mCachedValidateSamplersResult = false;
1622 return false;
1623 }
1624 }
1625 else
1626 {
1627 mTextureUnitTypesCache[textureUnit] = textureType;
1628 }
1629 }
1630 }
1631
1632 mCachedValidateSamplersResult = true;
1633 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634}
1635
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001636bool Program::isValidated() const
1637{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638 return mValidated;
1639}
1640
Geoff Lange1a27752015-10-05 13:16:04 -04001641GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001642{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001643 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001644}
1645
Jiajia Qin729b2c62017-08-14 09:36:11 +08001646GLuint Program::getActiveShaderStorageBlockCount() const
1647{
1648 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1649}
1650
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1652{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001653 ASSERT(
1654 uniformBlockIndex <
1655 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656
Jiajia Qin729b2c62017-08-14 09:36:11 +08001657 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658
1659 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001660 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661 std::string string = uniformBlock.name;
1662
Jamie Madill62d31cb2015-09-11 13:25:51 -04001663 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001664 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 }
jchen10fd7c3b52017-03-21 15:36:03 +08001667 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001668 }
1669}
1670
Geoff Lange1a27752015-10-05 13:16:04 -04001671GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001672{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673 int maxLength = 0;
1674
1675 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001676 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001677 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1679 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001680 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 if (!uniformBlock.name.empty())
1682 {
jchen10af713a22017-04-19 09:10:56 +08001683 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1684 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001685 }
1686 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001687 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688
1689 return maxLength;
1690}
1691
Geoff Lange1a27752015-10-05 13:16:04 -04001692GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001694 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001695 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001696
Jamie Madill48ef11b2016-04-27 15:21:52 -04001697 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001698 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1699 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001700 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001701 if (uniformBlock.name == baseName)
1702 {
1703 const bool arrayElementZero =
1704 (subscript == GL_INVALID_INDEX &&
1705 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1706 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1707 {
1708 return blockIndex;
1709 }
1710 }
1711 }
1712
1713 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001714}
1715
Jiajia Qin729b2c62017-08-14 09:36:11 +08001716const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001717{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001718 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1719 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001720}
1721
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001722void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1723{
jchen107a20b972017-06-13 14:25:26 +08001724 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001725 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001726 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001727}
1728
1729GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1730{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001731 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001732}
1733
Jiajia Qin729b2c62017-08-14 09:36:11 +08001734GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1735{
1736 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1737}
1738
Geoff Lang48dcae72014-02-05 16:28:24 -05001739void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1740{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001741 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001742 for (GLsizei i = 0; i < count; i++)
1743 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001744 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001745 }
1746
Jamie Madill48ef11b2016-04-27 15:21:52 -04001747 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001748}
1749
1750void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1751{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001752 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001753 {
jchen10a9042d32017-03-17 08:50:45 +08001754 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1755 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1756 std::string varName = var.nameWithArrayIndex();
1757 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001758 if (length)
1759 {
1760 *length = lastNameIdx;
1761 }
1762 if (size)
1763 {
jchen10a9042d32017-03-17 08:50:45 +08001764 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001765 }
1766 if (type)
1767 {
jchen10a9042d32017-03-17 08:50:45 +08001768 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001769 }
1770 if (name)
1771 {
jchen10a9042d32017-03-17 08:50:45 +08001772 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001773 name[lastNameIdx] = '\0';
1774 }
1775 }
1776}
1777
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001778GLsizei Program::getTransformFeedbackVaryingCount() const
1779{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001781 {
jchen10a9042d32017-03-17 08:50:45 +08001782 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001783 }
1784 else
1785 {
1786 return 0;
1787 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001788}
1789
1790GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1791{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001793 {
1794 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001795 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001796 {
jchen10a9042d32017-03-17 08:50:45 +08001797 maxSize =
1798 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001799 }
1800
1801 return maxSize;
1802 }
1803 else
1804 {
1805 return 0;
1806 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001807}
1808
1809GLenum Program::getTransformFeedbackBufferMode() const
1810{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001811 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812}
1813
Jamie Madillbd044ed2017-06-05 12:59:21 -04001814bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001816 Shader *vertexShader = mState.mAttachedVertexShader;
1817 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001818
Jamie Madillbd044ed2017-06-05 12:59:21 -04001819 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001820
Jamie Madillbd044ed2017-06-05 12:59:21 -04001821 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1822 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823
Sami Väisänen46eaa942016-06-29 10:26:37 +03001824 std::map<GLuint, std::string> staticFragmentInputLocations;
1825
Jamie Madill4cff2472015-08-21 16:53:18 -04001826 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001828 bool matched = false;
1829
1830 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001831 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 {
1833 continue;
1834 }
1835
Jamie Madill4cff2472015-08-21 16:53:18 -04001836 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001838 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001840 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001841 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001842 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001843 {
1844 return false;
1845 }
1846
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847 matched = true;
1848 break;
1849 }
1850 }
1851
1852 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001853 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001855 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856 return false;
1857 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001858
1859 // Check for aliased path rendering input bindings (if any).
1860 // If more than one binding refer statically to the same
1861 // location the link must fail.
1862
1863 if (!output.staticUse)
1864 continue;
1865
1866 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1867 if (inputBinding == -1)
1868 continue;
1869
1870 const auto it = staticFragmentInputLocations.find(inputBinding);
1871 if (it == std::end(staticFragmentInputLocations))
1872 {
1873 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1874 }
1875 else
1876 {
1877 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1878 << it->second;
1879 return false;
1880 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881 }
1882
Jamie Madillbd044ed2017-06-05 12:59:21 -04001883 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001884 {
1885 return false;
1886 }
1887
Jamie Madillada9ecc2015-08-17 12:53:37 -04001888 // TODO(jmadill): verify no unmatched vertex varyings?
1889
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890 return true;
1891}
1892
Jamie Madillbd044ed2017-06-05 12:59:21 -04001893bool Program::linkUniforms(const Context *context,
1894 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001895 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001896{
Olli Etuahob78707c2017-03-09 15:03:11 +00001897 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001898 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899 {
1900 return false;
1901 }
1902
Olli Etuahob78707c2017-03-09 15:03:11 +00001903 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001905 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001906
jchen10eaef1e52017-06-13 10:44:11 +08001907 if (!linkAtomicCounterBuffers())
1908 {
1909 return false;
1910 }
1911
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001912 return true;
1913}
1914
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001915void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001916{
Jamie Madill982f6e02017-06-07 14:33:04 -04001917 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1918 unsigned int low = high;
1919
jchen10eaef1e52017-06-13 10:44:11 +08001920 for (auto counterIter = mState.mUniforms.rbegin();
1921 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1922 {
1923 --low;
1924 }
1925
1926 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1927
1928 high = low;
1929
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001930 for (auto imageIter = mState.mUniforms.rbegin();
1931 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1932 {
1933 --low;
1934 }
1935
1936 mState.mImageUniformRange = RangeUI(low, high);
1937
1938 // If uniform is a image type, insert it into the mImageBindings array.
1939 for (unsigned int imageIndex : mState.mImageUniformRange)
1940 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001941 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1942 // cannot load values into a uniform defined as an image. if declare without a
1943 // binding qualifier, any uniform image variable (include all elements of
1944 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001945 auto &imageUniform = mState.mUniforms[imageIndex];
1946 if (imageUniform.binding == -1)
1947 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001948 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001949 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001950 else
1951 {
1952 mState.mImageBindings.emplace_back(
1953 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1954 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001955 }
1956
1957 high = low;
1958
1959 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001960 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001961 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001962 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001963 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001964
1965 mState.mSamplerUniformRange = RangeUI(low, high);
1966
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001967 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001968 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001969 {
1970 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1971 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1972 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001973 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001974 }
1975}
1976
jchen10eaef1e52017-06-13 10:44:11 +08001977bool Program::linkAtomicCounterBuffers()
1978{
1979 for (unsigned int index : mState.mAtomicCounterUniformRange)
1980 {
1981 auto &uniform = mState.mUniforms[index];
1982 bool found = false;
1983 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1984 ++bufferIndex)
1985 {
1986 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1987 if (buffer.binding == uniform.binding)
1988 {
1989 buffer.memberIndexes.push_back(index);
1990 uniform.bufferIndex = bufferIndex;
1991 found = true;
1992 break;
1993 }
1994 }
1995 if (!found)
1996 {
1997 AtomicCounterBuffer atomicCounterBuffer;
1998 atomicCounterBuffer.binding = uniform.binding;
1999 atomicCounterBuffer.memberIndexes.push_back(index);
2000 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2001 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2002 }
2003 }
2004 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2005 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2006
2007 return true;
2008}
2009
Martin Radev4c4c8e72016-08-04 12:25:34 +03002010bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2011 const std::string &uniformName,
2012 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002013 const sh::InterfaceBlockField &fragmentUniform,
2014 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002016 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2017 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2018 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 {
2020 return false;
2021 }
2022
2023 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2024 {
Jamie Madillf6113162015-05-07 11:49:21 -04002025 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 return false;
2027 }
2028
2029 return true;
2030}
2031
Jamie Madilleb979bf2016-11-15 12:28:46 -05002032// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002033bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002035 const ContextState &data = context->getContextState();
2036 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002037
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002039 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002040 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002041
2042 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002043 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002044 {
Jamie Madillf6113162015-05-07 11:49:21 -04002045 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002046 return false;
2047 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002049 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002050
Jamie Madillc349ec02015-08-21 16:53:12 -04002051 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002052 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002053 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002054 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002055 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002056 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002057 attribute.location = bindingLocation;
2058 }
2059
2060 if (attribute.location != -1)
2061 {
2062 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002063 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064
Jamie Madill63805b42015-08-25 13:17:39 -04002065 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066 {
Jamie Madillf6113162015-05-07 11:49:21 -04002067 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002068 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069
2070 return false;
2071 }
2072
Jamie Madill63805b42015-08-25 13:17:39 -04002073 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 {
Jamie Madill63805b42015-08-25 13:17:39 -04002075 const int regLocation = attribute.location + reg;
2076 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077
2078 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002079 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002080 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002082 // TODO(jmadill): fix aliasing on ES2
2083 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002085 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002086 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087 return false;
2088 }
2089 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002090 else
2091 {
Jamie Madill63805b42015-08-25 13:17:39 -04002092 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002093 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094
Jamie Madill63805b42015-08-25 13:17:39 -04002095 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 }
2097 }
2098 }
2099
2100 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002101 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002103 // Not set by glBindAttribLocation or by location layout qualifier
2104 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 {
Jamie Madill63805b42015-08-25 13:17:39 -04002106 int regs = VariableRegisterCount(attribute.type);
2107 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108
Jamie Madill63805b42015-08-25 13:17:39 -04002109 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 {
Jamie Madillf6113162015-05-07 11:49:21 -04002111 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002112 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113 }
2114
Jamie Madillc349ec02015-08-21 16:53:12 -04002115 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116 }
2117 }
2118
Jamie Madill48ef11b2016-04-27 15:21:52 -04002119 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 {
Jamie Madill63805b42015-08-25 13:17:39 -04002121 ASSERT(attribute.location != -1);
2122 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002123
Jamie Madill63805b42015-08-25 13:17:39 -04002124 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002126 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 }
2128 }
2129
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 return true;
2131}
2132
Martin Radev4c4c8e72016-08-04 12:25:34 +03002133bool Program::validateVertexAndFragmentInterfaceBlocks(
2134 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2135 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002136 InfoLog &infoLog,
2137 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002138{
2139 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002140 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2141 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002142
2143 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2144 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002145 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002146 }
2147
Jamie Madille473dee2015-08-18 14:49:01 -04002148 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002150 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2151 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 {
2153 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002154 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2155 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002156 {
2157 return false;
2158 }
2159 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002160 // TODO(jiajia.qin@intel.com): Add
2161 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002162 }
2163 return true;
2164}
Jamie Madille473dee2015-08-18 14:49:01 -04002165
Jiajia Qin729b2c62017-08-14 09:36:11 +08002166bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002167{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002168 const auto &caps = context->getCaps();
2169
Martin Radev4c4c8e72016-08-04 12:25:34 +03002170 if (mState.mAttachedComputeShader)
2171 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002172 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002173 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002174
Jiajia Qin729b2c62017-08-14 09:36:11 +08002175 if (!validateInterfaceBlocksCount(
2176 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002177 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2178 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002180 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002181 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002182
2183 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2184 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2185 computeShaderStorageBlocks,
2186 "Compute shader shader storage block count exceeds "
2187 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2188 infoLog))
2189 {
2190 return false;
2191 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002192 return true;
2193 }
2194
Jamie Madillbd044ed2017-06-05 12:59:21 -04002195 Shader &vertexShader = *mState.mAttachedVertexShader;
2196 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002197
Jiajia Qin729b2c62017-08-14 09:36:11 +08002198 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2199 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002200
Jiajia Qin729b2c62017-08-14 09:36:11 +08002201 if (!validateInterfaceBlocksCount(
2202 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002203 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2204 {
2205 return false;
2206 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002207 if (!validateInterfaceBlocksCount(
2208 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002209 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2210 infoLog))
2211 {
2212
2213 return false;
2214 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002215
2216 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002217 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002218 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002219 {
2220 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 }
Jamie Madille473dee2015-08-18 14:49:01 -04002222
Jiajia Qin729b2c62017-08-14 09:36:11 +08002223 if (context->getClientVersion() >= Version(3, 1))
2224 {
2225 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2226 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2227
2228 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2229 vertexShaderStorageBlocks,
2230 "Vertex shader shader storage block count exceeds "
2231 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2232 infoLog))
2233 {
2234 return false;
2235 }
2236 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2237 fragmentShaderStorageBlocks,
2238 "Fragment shader shader storage block count exceeds "
2239 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2240 infoLog))
2241 {
2242
2243 return false;
2244 }
2245
2246 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2247 fragmentShaderStorageBlocks, infoLog,
2248 webglCompatibility))
2249 {
2250 return false;
2251 }
2252 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002253 return true;
2254}
2255
Jamie Madilla2c74982016-12-12 11:20:42 -05002256bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002257 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002258 const sh::InterfaceBlock &fragmentInterfaceBlock,
2259 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260{
2261 const char* blockName = vertexInterfaceBlock.name.c_str();
2262 // validate blocks for the same member types
2263 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2264 {
Jamie Madillf6113162015-05-07 11:49:21 -04002265 infoLog << "Types for interface block '" << blockName
2266 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002267 return false;
2268 }
2269 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2270 {
Jamie Madillf6113162015-05-07 11:49:21 -04002271 infoLog << "Array sizes differ for interface block '" << blockName
2272 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002273 return false;
2274 }
jchen10af713a22017-04-19 09:10:56 +08002275 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2276 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2277 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 {
Jamie Madillf6113162015-05-07 11:49:21 -04002279 infoLog << "Layout qualifiers differ for interface block '" << blockName
2280 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002281 return false;
2282 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002283 const unsigned int numBlockMembers =
2284 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002285 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2286 {
2287 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2288 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2289 if (vertexMember.name != fragmentMember.name)
2290 {
Jamie Madillf6113162015-05-07 11:49:21 -04002291 infoLog << "Name mismatch for field " << blockMemberIndex
2292 << " of interface block '" << blockName
2293 << "': (in vertex: '" << vertexMember.name
2294 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002295 return false;
2296 }
2297 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002298 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2299 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300 {
2301 return false;
2302 }
2303 }
2304 return true;
2305}
2306
2307bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2308 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2309{
2310 if (vertexVariable.type != fragmentVariable.type)
2311 {
Jamie Madillf6113162015-05-07 11:49:21 -04002312 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002313 return false;
2314 }
2315 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2316 {
Jamie Madillf6113162015-05-07 11:49:21 -04002317 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002318 return false;
2319 }
2320 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2321 {
Jamie Madillf6113162015-05-07 11:49:21 -04002322 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323 return false;
2324 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002325 if (vertexVariable.structName != fragmentVariable.structName)
2326 {
2327 infoLog << "Structure names for " << variableName
2328 << " differ between vertex and fragment shaders";
2329 return false;
2330 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002331
2332 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2333 {
Jamie Madillf6113162015-05-07 11:49:21 -04002334 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002335 return false;
2336 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002337 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002338 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2339 {
2340 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2341 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2342
2343 if (vertexMember.name != fragmentMember.name)
2344 {
Jamie Madillf6113162015-05-07 11:49:21 -04002345 infoLog << "Name mismatch for field '" << memberIndex
2346 << "' of " << variableName
2347 << ": (in vertex: '" << vertexMember.name
2348 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002349 return false;
2350 }
2351
2352 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2353 vertexMember.name + "'";
2354
2355 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2356 {
2357 return false;
2358 }
2359 }
2360
2361 return true;
2362}
2363
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002364bool Program::linkValidateVaryings(InfoLog &infoLog,
2365 const std::string &varyingName,
2366 const sh::Varying &vertexVarying,
2367 const sh::Varying &fragmentVarying,
2368 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369{
2370 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2371 {
2372 return false;
2373 }
2374
Jamie Madille9cc4692015-02-19 16:00:13 -05002375 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002376 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002377 infoLog << "Interpolation types for " << varyingName
2378 << " differ between vertex and fragment shaders.";
2379 return false;
2380 }
2381
2382 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2383 {
2384 infoLog << "Invariance for " << varyingName
2385 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386 return false;
2387 }
2388
2389 return true;
2390}
2391
Jamie Madillbd044ed2017-06-05 12:59:21 -04002392bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002393{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002394 Shader *vertexShader = mState.mAttachedVertexShader;
2395 Shader *fragmentShader = mState.mAttachedFragmentShader;
2396 const auto &vertexVaryings = vertexShader->getVaryings(context);
2397 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2398 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002399
2400 if (shaderVersion != 100)
2401 {
2402 // Only ESSL 1.0 has restrictions on matching input and output invariance
2403 return true;
2404 }
2405
2406 bool glPositionIsInvariant = false;
2407 bool glPointSizeIsInvariant = false;
2408 bool glFragCoordIsInvariant = false;
2409 bool glPointCoordIsInvariant = false;
2410
2411 for (const sh::Varying &varying : vertexVaryings)
2412 {
2413 if (!varying.isBuiltIn())
2414 {
2415 continue;
2416 }
2417 if (varying.name.compare("gl_Position") == 0)
2418 {
2419 glPositionIsInvariant = varying.isInvariant;
2420 }
2421 else if (varying.name.compare("gl_PointSize") == 0)
2422 {
2423 glPointSizeIsInvariant = varying.isInvariant;
2424 }
2425 }
2426
2427 for (const sh::Varying &varying : fragmentVaryings)
2428 {
2429 if (!varying.isBuiltIn())
2430 {
2431 continue;
2432 }
2433 if (varying.name.compare("gl_FragCoord") == 0)
2434 {
2435 glFragCoordIsInvariant = varying.isInvariant;
2436 }
2437 else if (varying.name.compare("gl_PointCoord") == 0)
2438 {
2439 glPointCoordIsInvariant = varying.isInvariant;
2440 }
2441 }
2442
2443 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2444 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2445 // Not requiring invariance to match is supported by:
2446 // dEQP, WebGL CTS, Nexus 5X GLES
2447 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2448 {
2449 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2450 "declared invariant.";
2451 return false;
2452 }
2453 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2454 {
2455 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2456 "declared invariant.";
2457 return false;
2458 }
2459
2460 return true;
2461}
2462
jchen10a9042d32017-03-17 08:50:45 +08002463bool Program::linkValidateTransformFeedback(const gl::Context *context,
2464 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002465 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002466 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002467{
2468 size_t totalComponents = 0;
2469
Jamie Madillccdf74b2015-08-18 10:46:12 -04002470 std::set<std::string> uniqueNames;
2471
Jamie Madill48ef11b2016-04-27 15:21:52 -04002472 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 {
2474 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002475 size_t subscript = GL_INVALID_INDEX;
2476 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2477
Jamie Madill192745a2016-12-22 15:58:21 -05002478 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002479 {
Jamie Madill192745a2016-12-22 15:58:21 -05002480 const sh::Varying *varying = ref.second.get();
2481
jchen10a9042d32017-03-17 08:50:45 +08002482 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002483 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002484 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002485 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002486 infoLog << "Two transform feedback varyings specify the same output variable ("
2487 << tfVaryingName << ").";
2488 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002489 }
jchen10a9042d32017-03-17 08:50:45 +08002490 if (context->getClientVersion() >= Version(3, 1))
2491 {
2492 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2493 {
2494 infoLog
2495 << "Two transform feedback varyings include the same array element ("
2496 << tfVaryingName << ").";
2497 return false;
2498 }
2499 }
2500 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002501 {
2502 infoLog << "Capture of arrays is undefined and not supported.";
2503 return false;
2504 }
2505
jchen10a9042d32017-03-17 08:50:45 +08002506 uniqueNames.insert(tfVaryingName);
2507
Jamie Madillccdf74b2015-08-18 10:46:12 -04002508 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002509 size_t elementCount =
2510 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2511 : 1);
2512 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002513 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002514 componentCount > caps.maxTransformFeedbackSeparateComponents)
2515 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002516 infoLog << "Transform feedback varying's " << varying->name << " components ("
2517 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002518 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002519 return false;
2520 }
2521
2522 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002523 found = true;
2524 break;
2525 }
2526 }
jchen10a9042d32017-03-17 08:50:45 +08002527 if (context->getClientVersion() < Version(3, 1) &&
2528 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002529 {
Geoff Lang1a683462015-09-29 15:09:59 -04002530 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002531 return false;
2532 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002533 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2534 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002535 ASSERT(found);
2536 }
2537
Jamie Madill48ef11b2016-04-27 15:21:52 -04002538 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002539 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002540 {
Jamie Madillf6113162015-05-07 11:49:21 -04002541 infoLog << "Transform feedback varying total components (" << totalComponents
2542 << ") exceed the maximum interleaved components ("
2543 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002544 return false;
2545 }
2546
2547 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002548}
2549
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002550bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2551{
2552 const std::vector<sh::Uniform> &vertexUniforms =
2553 mState.mAttachedVertexShader->getUniforms(context);
2554 const std::vector<sh::Uniform> &fragmentUniforms =
2555 mState.mAttachedFragmentShader->getUniforms(context);
2556 const std::vector<sh::Attribute> &attributes =
2557 mState.mAttachedVertexShader->getActiveAttributes(context);
2558 for (const auto &attrib : attributes)
2559 {
2560 for (const auto &uniform : vertexUniforms)
2561 {
2562 if (uniform.name == attrib.name)
2563 {
2564 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2565 return false;
2566 }
2567 }
2568 for (const auto &uniform : fragmentUniforms)
2569 {
2570 if (uniform.name == attrib.name)
2571 {
2572 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2573 return false;
2574 }
2575 }
2576 }
2577 return true;
2578}
2579
Jamie Madill192745a2016-12-22 15:58:21 -05002580void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002581{
2582 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002583 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002584 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002585 {
jchen10a9042d32017-03-17 08:50:45 +08002586 size_t subscript = GL_INVALID_INDEX;
2587 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002588 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002589 {
Jamie Madill192745a2016-12-22 15:58:21 -05002590 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002591 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002592 {
jchen10a9042d32017-03-17 08:50:45 +08002593 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2594 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002595 break;
2596 }
2597 }
2598 }
2599}
2600
Jamie Madillbd044ed2017-06-05 12:59:21 -04002601Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002602{
Jamie Madill192745a2016-12-22 15:58:21 -05002603 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002604
Jamie Madillbd044ed2017-06-05 12:59:21 -04002605 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002606 {
Jamie Madill192745a2016-12-22 15:58:21 -05002607 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002608 }
2609
Jamie Madillbd044ed2017-06-05 12:59:21 -04002610 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002611 {
Jamie Madill192745a2016-12-22 15:58:21 -05002612 merged[varying.name].fragment = &varying;
2613 }
2614
2615 return merged;
2616}
2617
2618std::vector<PackedVarying> Program::getPackedVaryings(
2619 const Program::MergedVaryings &mergedVaryings) const
2620{
2621 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2622 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002623 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002624
2625 for (const auto &ref : mergedVaryings)
2626 {
2627 const sh::Varying *input = ref.second.vertex;
2628 const sh::Varying *output = ref.second.fragment;
2629
2630 // Only pack varyings that have a matched input or output, plus special builtins.
2631 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002632 {
Jamie Madill192745a2016-12-22 15:58:21 -05002633 // Will get the vertex shader interpolation by default.
2634 auto interpolation = ref.second.get()->interpolation;
2635
Olli Etuaho06a06f52017-07-12 12:22:15 +03002636 // Note that we lose the vertex shader static use information here. The data for the
2637 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002638 if (output->isStruct())
2639 {
2640 ASSERT(!output->isArray());
2641 for (const auto &field : output->fields)
2642 {
2643 ASSERT(!field.isStruct() && !field.isArray());
2644 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2645 }
2646 }
2647 else
2648 {
2649 packedVaryings.push_back(PackedVarying(*output, interpolation));
2650 }
2651 continue;
2652 }
2653
2654 // Keep Transform FB varyings in the merged list always.
2655 if (!input)
2656 {
2657 continue;
2658 }
2659
2660 for (const std::string &tfVarying : tfVaryings)
2661 {
jchen10a9042d32017-03-17 08:50:45 +08002662 size_t subscript = GL_INVALID_INDEX;
2663 std::string baseName = ParseResourceName(tfVarying, &subscript);
2664 if (uniqueFullNames.count(tfVarying) > 0)
2665 {
2666 continue;
2667 }
2668 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002669 {
2670 // Transform feedback for varying structs is underspecified.
2671 // See Khronos bug 9856.
2672 // TODO(jmadill): Figure out how to be spec-compliant here.
2673 if (!input->isStruct())
2674 {
2675 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2676 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002677 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2678 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002679 }
jchen10a9042d32017-03-17 08:50:45 +08002680 if (subscript == GL_INVALID_INDEX)
2681 {
2682 break;
2683 }
Jamie Madill192745a2016-12-22 15:58:21 -05002684 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002685 }
2686 }
2687
Jamie Madill192745a2016-12-22 15:58:21 -05002688 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2689
2690 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002691}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002692
Jamie Madillbd044ed2017-06-05 12:59:21 -04002693void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002694{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002695 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002696 ASSERT(fragmentShader != nullptr);
2697
Geoff Lange0cff192017-05-30 13:04:56 -04002698 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002699 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002700
2701 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002702 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002703 {
2704 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2705 outputVariable.name != "gl_FragData")
2706 {
2707 continue;
2708 }
2709
2710 unsigned int baseLocation =
2711 (outputVariable.location == -1 ? 0u
2712 : static_cast<unsigned int>(outputVariable.location));
2713 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2714 elementIndex++)
2715 {
2716 const unsigned int location = baseLocation + elementIndex;
2717 if (location >= mState.mOutputVariableTypes.size())
2718 {
2719 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2720 }
Corentin Walleze7557742017-06-01 13:09:57 -04002721 ASSERT(location < mState.mActiveOutputVariables.size());
2722 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002723 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2724 }
2725 }
2726
Jamie Madill80a6fc02015-08-21 16:53:16 -04002727 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002728 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002729 return;
2730
Jamie Madillbd044ed2017-06-05 12:59:21 -04002731 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002732 // TODO(jmadill): any caps validation here?
2733
jchen1015015f72017-03-16 13:54:21 +08002734 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002735 outputVariableIndex++)
2736 {
jchen1015015f72017-03-16 13:54:21 +08002737 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002738
2739 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2740 if (outputVariable.isBuiltIn())
2741 continue;
2742
2743 // Since multiple output locations must be specified, use 0 for non-specified locations.
2744 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2745
Jamie Madill80a6fc02015-08-21 16:53:16 -04002746 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2747 elementIndex++)
2748 {
2749 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002750 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002751 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madillfb997ec2017-09-20 15:44:27 -04002752 mState.mOutputLocations[location] = VariableLocation(element, outputVariableIndex);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002753 }
2754 }
2755}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002756
Olli Etuaho48fed632017-03-16 12:05:30 +00002757void Program::setUniformValuesFromBindingQualifiers()
2758{
Jamie Madill982f6e02017-06-07 14:33:04 -04002759 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002760 {
2761 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2762 if (samplerUniform.binding != -1)
2763 {
2764 GLint location = mState.getUniformLocation(samplerUniform.name);
2765 ASSERT(location != -1);
2766 std::vector<GLint> boundTextureUnits;
2767 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2768 ++elementIndex)
2769 {
2770 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2771 }
2772 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2773 boundTextureUnits.data());
2774 }
2775 }
2776}
2777
jchen10eaef1e52017-06-13 10:44:11 +08002778void Program::gatherAtomicCounterBuffers()
2779{
2780 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2781 // counter.
2782 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2783}
2784
Jiajia Qin729b2c62017-08-14 09:36:11 +08002785void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002786{
Jiajia Qin729b2c62017-08-14 09:36:11 +08002787 for (const sh::InterfaceBlock &computeBlock : computeBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002788 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002789
Jiajia Qin729b2c62017-08-14 09:36:11 +08002790 // Only 'packed' blocks are allowed to be considered inactive.
2791 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2792 continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002793
Jiajia Qin729b2c62017-08-14 09:36:11 +08002794 defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002795 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002796}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002797
Jiajia Qin729b2c62017-08-14 09:36:11 +08002798void Program::gatherVertexAndFragmentBlockInfo(
2799 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2800 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks)
2801{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002802 std::set<std::string> visitedList;
2803
Jiajia Qin729b2c62017-08-14 09:36:11 +08002804 for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002805 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002806 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002807 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2808 continue;
2809
Jiajia Qin729b2c62017-08-14 09:36:11 +08002810 defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002811 visitedList.insert(vertexBlock.name);
2812 }
2813
Jiajia Qin729b2c62017-08-14 09:36:11 +08002814 for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002815 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002816 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2818 continue;
2819
2820 if (visitedList.count(fragmentBlock.name) > 0)
2821 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002822 if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002823 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002824 for (InterfaceBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002825 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002826 if (block.name == fragmentBlock.name)
2827 {
2828 block.fragmentStaticUse = fragmentBlock.staticUse;
2829 }
2830 }
2831 }
2832 else
2833 {
2834 ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2835 for (InterfaceBlock &block : mState.mShaderStorageBlocks)
2836 {
2837 if (block.name == fragmentBlock.name)
2838 {
2839 block.fragmentStaticUse = fragmentBlock.staticUse;
2840 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002841 }
2842 }
2843
2844 continue;
2845 }
2846
Jiajia Qin729b2c62017-08-14 09:36:11 +08002847 defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002848 visitedList.insert(fragmentBlock.name);
2849 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002850}
2851
2852void Program::gatherInterfaceBlockInfo(const Context *context)
2853{
2854 ASSERT(mState.mUniformBlocks.empty());
2855 ASSERT(mState.mShaderStorageBlocks.empty());
2856
2857 if (mState.mAttachedComputeShader)
2858 {
2859 Shader *computeShader = mState.getAttachedComputeShader();
2860
2861 gatherComputeBlockInfo(computeShader->getUniformBlocks(context));
2862 gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context));
2863 return;
2864 }
2865
2866 Shader *vertexShader = mState.getAttachedVertexShader();
2867 Shader *fragmentShader = mState.getAttachedFragmentShader();
2868
2869 gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context),
2870 fragmentShader->getUniformBlocks(context));
2871 if (context->getClientVersion() >= Version(3, 1))
2872 {
2873 gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context),
2874 fragmentShader->getShaderStorageBlocks(context));
2875 }
2876
jchen10af713a22017-04-19 09:10:56 +08002877 // Set initial bindings from shader.
2878 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2879 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002880 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002881 bindUniformBlock(blockIndex, uniformBlock.binding);
2882 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002883}
2884
Jamie Madill4a3c2342015-10-08 12:58:45 -04002885template <typename VarT>
2886void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2887 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002888 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002889 int blockIndex)
2890{
2891 for (const VarT &field : fields)
2892 {
2893 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2894
Olli Etuaho855d9642017-05-17 14:05:06 +03002895 const std::string &fullMappedName =
2896 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2897
Jamie Madill4a3c2342015-10-08 12:58:45 -04002898 if (field.isStruct())
2899 {
2900 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2901 {
2902 const std::string uniformElementName =
2903 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002904 const std::string uniformElementMappedName =
2905 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2906 defineUniformBlockMembers(field.fields, uniformElementName,
2907 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002908 }
2909 }
2910 else
2911 {
2912 // If getBlockMemberInfo returns false, the uniform is optimized out.
2913 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002914 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002915 {
2916 continue;
2917 }
2918
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002919 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002920 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002921 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002922
2923 // Since block uniforms have no location, we don't need to store them in the uniform
2924 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002925 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002926 }
2927 }
2928}
2929
Jiajia Qin729b2c62017-08-14 09:36:11 +08002930void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002931{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002932 size_t blockSize = 0;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002933 std::vector<unsigned int> blockIndexes;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002934
Jiajia Qin729b2c62017-08-14 09:36:11 +08002935 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002936 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002937 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
2938 // Track the first and last uniform index to determine the range of active uniforms in the
2939 // block.
2940 size_t firstBlockUniformIndex = mState.mUniforms.size();
2941 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2942 interfaceBlock.fieldMappedPrefix(), blockIndex);
2943 size_t lastBlockUniformIndex = mState.mUniforms.size();
2944
2945 for (size_t blockUniformIndex = firstBlockUniformIndex;
2946 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2947 {
2948 blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2949 }
2950 }
2951 else
2952 {
2953 // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index.
2954 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002955 }
jchen10af713a22017-04-19 09:10:56 +08002956 // ESSL 3.10 section 4.4.4 page 58:
2957 // Any uniform or shader storage block declared without a binding qualifier is initially
2958 // assigned to block binding point zero.
2959 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960 if (interfaceBlock.arraySize > 0)
2961 {
2962 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2963 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002964 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
2965 // of UniformBlock and ShaderStorageBlock.
2966 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08002967 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002968 // Don't define this block at all if it's not active in the implementation.
2969 if (!mProgram->getUniformBlockSize(
2970 interfaceBlock.name + ArrayString(arrayElement),
2971 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
2972 {
2973 continue;
2974 }
jchen10af713a22017-04-19 09:10:56 +08002975 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002976
2977 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
2978 blockBinding + arrayElement);
2979 block.memberIndexes = blockIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002980
Martin Radev4c4c8e72016-08-04 12:25:34 +03002981 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002982 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002983 case GL_VERTEX_SHADER:
2984 {
2985 block.vertexStaticUse = interfaceBlock.staticUse;
2986 break;
2987 }
2988 case GL_FRAGMENT_SHADER:
2989 {
2990 block.fragmentStaticUse = interfaceBlock.staticUse;
2991 break;
2992 }
2993 case GL_COMPUTE_SHADER:
2994 {
2995 block.computeStaticUse = interfaceBlock.staticUse;
2996 break;
2997 }
2998 default:
2999 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003000 }
3001
Jiajia Qin729b2c62017-08-14 09:36:11 +08003002 // Since all block elements in an array share the same active interface blocks, they
3003 // will all be active once any block member is used. So, since interfaceBlock.name[0]
3004 // was active, here we will add every block element in the array.
Qin Jiajia0350a642016-11-01 17:01:51 +08003005 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003006 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3007 {
3008 mState.mUniformBlocks.push_back(block);
3009 }
3010 else
3011 {
3012 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3013 mState.mShaderStorageBlocks.push_back(block);
3014 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003015 }
3016 }
3017 else
3018 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003019 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3020 // of UniformBlock and ShaderStorageBlock.
3021 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003022 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003023 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
3024 &blockSize))
3025 {
3026 return;
3027 }
jchen10af713a22017-04-19 09:10:56 +08003028 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003029
3030 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0,
3031 blockBinding);
3032 block.memberIndexes = blockIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003033
Martin Radev4c4c8e72016-08-04 12:25:34 +03003034 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003035 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003036 case GL_VERTEX_SHADER:
3037 {
3038 block.vertexStaticUse = interfaceBlock.staticUse;
3039 break;
3040 }
3041 case GL_FRAGMENT_SHADER:
3042 {
3043 block.fragmentStaticUse = interfaceBlock.staticUse;
3044 break;
3045 }
3046 case GL_COMPUTE_SHADER:
3047 {
3048 block.computeStaticUse = interfaceBlock.staticUse;
3049 break;
3050 }
3051 default:
3052 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003053 }
3054
Jamie Madill4a3c2342015-10-08 12:58:45 -04003055 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003056 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3057 {
3058 mState.mUniformBlocks.push_back(block);
3059 }
3060 else
3061 {
3062 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3063 mState.mShaderStorageBlocks.push_back(block);
3064 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003065 }
3066}
3067
Jamie Madille7d84322017-01-10 18:21:59 -05003068void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003069 GLsizei clampedCount,
3070 const GLint *v)
3071{
Jamie Madill81c2e252017-09-09 23:32:46 -04003072 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3073 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3074 std::vector<GLuint> *boundTextureUnits =
3075 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003076
Jamie Madill81c2e252017-09-09 23:32:46 -04003077 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
Jamie Madilld68248b2017-09-11 14:34:14 -04003078
3079 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003080 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003081}
3082
3083template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003084GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3085 GLsizei count,
3086 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003087 const T *v)
3088{
Jamie Madill134f93d2017-08-31 17:11:00 -04003089 if (count == 1)
3090 return 1;
3091
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003092 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003093
Corentin Wallez15ac5342016-11-03 17:06:39 -04003094 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3095 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003096 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003097 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003098 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003099
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003100 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003101 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003102 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003103 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003104
3105 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003106}
3107
3108template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003109GLsizei Program::clampMatrixUniformCount(GLint location,
3110 GLsizei count,
3111 GLboolean transpose,
3112 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003113{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003114 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3115
Jamie Madill62d31cb2015-09-11 13:25:51 -04003116 if (!transpose)
3117 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003118 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003119 }
3120
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003121 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003122
3123 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3124 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003125 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
3126 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003127}
3128
Jamie Madill54164b02017-08-28 15:17:37 -04003129// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3130// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003131template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003132void Program::getUniformInternal(const Context *context,
3133 DestT *dataOut,
3134 GLint location,
3135 GLenum nativeType,
3136 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003137{
Jamie Madill54164b02017-08-28 15:17:37 -04003138 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003139 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003140 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003141 {
3142 GLint tempValue[16] = {0};
3143 mProgram->getUniformiv(context, location, tempValue);
3144 UniformStateQueryCastLoop<GLboolean>(
3145 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003146 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003147 }
3148 case GL_INT:
3149 {
3150 GLint tempValue[16] = {0};
3151 mProgram->getUniformiv(context, location, tempValue);
3152 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3153 components);
3154 break;
3155 }
3156 case GL_UNSIGNED_INT:
3157 {
3158 GLuint tempValue[16] = {0};
3159 mProgram->getUniformuiv(context, location, tempValue);
3160 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3161 components);
3162 break;
3163 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003164 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003165 {
3166 GLfloat tempValue[16] = {0};
3167 mProgram->getUniformfv(context, location, tempValue);
3168 UniformStateQueryCastLoop<GLfloat>(
3169 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003170 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003171 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003172 default:
3173 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003174 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003175 }
3176}
Jamie Madilla4595b82017-01-11 17:36:34 -05003177
3178bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3179{
3180 // Must be called after samplers are validated.
3181 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3182
3183 for (const auto &binding : mState.mSamplerBindings)
3184 {
3185 GLenum textureType = binding.textureType;
3186 for (const auto &unit : binding.boundTextureUnits)
3187 {
3188 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3189 if (programTextureID == textureID)
3190 {
3191 // TODO(jmadill): Check for appropriate overlap.
3192 return true;
3193 }
3194 }
3195 }
3196
3197 return false;
3198}
3199
Jamie Madilla2c74982016-12-12 11:20:42 -05003200} // namespace gl