blob: 10eb5cae75df69d976c73042c4aa6a099fd6e16f [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{
Olli Etuahoc8538042017-09-27 11:20:15 +0300143 std::vector<unsigned int> subscripts;
144 std::string baseName = ParseResourceName(name, &subscripts);
jchen1015015f72017-03-16 13:54:21 +0800145
146 // The app is not allowed to specify array indices other than 0 for arrays of basic types
Olli Etuahoc8538042017-09-27 11:20:15 +0300147 for (unsigned int subscript : subscripts)
jchen1015015f72017-03-16 13:54:21 +0800148 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300149 if (subscript != 0u)
150 {
151 return GL_INVALID_INDEX;
152 }
jchen1015015f72017-03-16 13:54:21 +0800153 }
154
155 for (size_t index = 0; index < list.size(); index++)
156 {
157 const VarT &resource = list[index];
158 if (resource.name == baseName)
159 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300160 // TODO(oetuaho@nvidia.com): Check array nesting >= number of specified
161 // subscripts once arrays of arrays are supported in ShaderVariable.
162 if ((resource.isArray() || subscripts.empty()) && subscripts.size() <= 1u)
jchen1015015f72017-03-16 13:54:21 +0800163 {
164 return static_cast<GLuint>(index);
165 }
166 }
167 }
168
169 return GL_INVALID_INDEX;
170}
171
jchen10fd7c3b52017-03-21 15:36:03 +0800172void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
173{
174 ASSERT(bufSize > 0);
175 strncpy(buffer, string.c_str(), bufSize);
176 buffer[bufSize - 1] = '\0';
177
178 if (length)
179 {
180 *length = static_cast<GLsizei>(strlen(buffer));
181 }
182}
183
jchen10a9042d32017-03-17 08:50:45 +0800184bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
185{
Olli Etuahoc8538042017-09-27 11:20:15 +0300186 std::vector<unsigned int> subscripts;
187 std::string baseName = ParseResourceName(name, &subscripts);
188 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800189 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300190 std::vector<unsigned int> arrayIndices;
191 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
192 if (baseName == arrayName &&
193 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800194 {
195 return true;
196 }
197 }
198 return false;
199}
200
Jiajia Qin729b2c62017-08-14 09:36:11 +0800201bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
202 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
203 const std::string &errorMessage,
204 InfoLog &infoLog)
205{
206 GLuint blockCount = 0;
207 for (const sh::InterfaceBlock &block : interfaceBlocks)
208 {
209 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
210 {
211 blockCount += (block.arraySize ? block.arraySize : 1);
212 if (blockCount > maxInterfaceBlocks)
213 {
214 infoLog << errorMessage << maxInterfaceBlocks << ")";
215 return false;
216 }
217 }
218 }
219 return true;
220}
221
Jamie Madill62d31cb2015-09-11 13:25:51 -0400222} // anonymous namespace
223
Jamie Madill4a3c2342015-10-08 12:58:45 -0400224const char *const g_fakepath = "C:\\fakepath";
225
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400226InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000227{
228}
229
230InfoLog::~InfoLog()
231{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000232}
233
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400234size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000235{
Jamie Madill23176ce2017-07-31 14:14:33 -0400236 if (!mLazyStream)
237 {
238 return 0;
239 }
240
241 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400242 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000243}
244
Geoff Lange1a27752015-10-05 13:16:04 -0400245void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000246{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400247 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000248
249 if (bufSize > 0)
250 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400251 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400252
Jamie Madill23176ce2017-07-31 14:14:33 -0400253 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000254 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400255 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
256 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000257 }
258
259 infoLog[index] = '\0';
260 }
261
262 if (length)
263 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400264 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265 }
266}
267
268// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300269// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000270// messages, so lets remove all occurrences of this fake file path from the log.
271void InfoLog::appendSanitized(const char *message)
272{
Jamie Madill23176ce2017-07-31 14:14:33 -0400273 ensureInitialized();
274
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000275 std::string msg(message);
276
277 size_t found;
278 do
279 {
280 found = msg.find(g_fakepath);
281 if (found != std::string::npos)
282 {
283 msg.erase(found, strlen(g_fakepath));
284 }
285 }
286 while (found != std::string::npos);
287
Jamie Madill23176ce2017-07-31 14:14:33 -0400288 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000289}
290
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000291void InfoLog::reset()
292{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000293}
294
Olli Etuahoc8538042017-09-27 11:20:15 +0300295VariableLocation::VariableLocation() : index(kUnused), flattenedArrayOffset(0u), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000296{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500297}
298
Olli Etuahoc8538042017-09-27 11:20:15 +0300299VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
300 : arrayIndices(1, arrayIndex), index(index), flattenedArrayOffset(arrayIndex), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500301{
Olli Etuahoc8538042017-09-27 11:20:15 +0300302 ASSERT(arrayIndex != GL_INVALID_INDEX);
303}
304
305bool VariableLocation::areAllArrayIndicesZero() const
306{
307 for (unsigned int arrayIndex : arrayIndices)
308 {
309 if (arrayIndex != 0)
310 {
311 return false;
312 }
313 }
314 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500315}
316
Geoff Langd8605522016-04-13 10:19:12 -0400317void Program::Bindings::bindLocation(GLuint index, const std::string &name)
318{
319 mBindings[name] = index;
320}
321
322int Program::Bindings::getBinding(const std::string &name) const
323{
324 auto iter = mBindings.find(name);
325 return (iter != mBindings.end()) ? iter->second : -1;
326}
327
328Program::Bindings::const_iterator Program::Bindings::begin() const
329{
330 return mBindings.begin();
331}
332
333Program::Bindings::const_iterator Program::Bindings::end() const
334{
335 return mBindings.end();
336}
337
Jamie Madill48ef11b2016-04-27 15:21:52 -0400338ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500339 : mLabel(),
340 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400341 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300342 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500343 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400344 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500345 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800346 mImageUniformRange(0, 0),
347 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300348 mBinaryRetrieveableHint(false),
349 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400350{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300351 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400352}
353
Jamie Madill48ef11b2016-04-27 15:21:52 -0400354ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400355{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500356 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400357}
358
Jamie Madill48ef11b2016-04-27 15:21:52 -0400359const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500360{
361 return mLabel;
362}
363
Jamie Madill48ef11b2016-04-27 15:21:52 -0400364GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400365{
Olli Etuahoc8538042017-09-27 11:20:15 +0300366 std::vector<unsigned int> subscripts;
367 std::string baseName = ParseResourceName(name, &subscripts);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400368
369 for (size_t location = 0; location < mUniformLocations.size(); ++location)
370 {
371 const VariableLocation &uniformLocation = mUniformLocations[location];
Jamie Madillfb997ec2017-09-20 15:44:27 -0400372 if (!uniformLocation.used())
Geoff Langd8605522016-04-13 10:19:12 -0400373 {
374 continue;
375 }
376
377 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400378
379 if (uniform.name == baseName)
380 {
Geoff Langd8605522016-04-13 10:19:12 -0400381 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400382 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300383 if (uniformLocation.arrayIndices == subscripts ||
384 (uniformLocation.areAllArrayIndicesZero() && subscripts.empty()))
Geoff Langd8605522016-04-13 10:19:12 -0400385 {
386 return static_cast<GLint>(location);
387 }
388 }
389 else
390 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300391 if (subscripts.empty())
Geoff Langd8605522016-04-13 10:19:12 -0400392 {
393 return static_cast<GLint>(location);
394 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400395 }
396 }
397 }
398
399 return -1;
400}
401
Jamie Madille7d84322017-01-10 18:21:59 -0500402GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400403{
jchen1015015f72017-03-16 13:54:21 +0800404 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400405}
406
Jamie Madille7d84322017-01-10 18:21:59 -0500407GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
408{
409 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
410 return mUniformLocations[location].index;
411}
412
413Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
414{
415 GLuint index = getUniformIndexFromLocation(location);
416 if (!isSamplerUniformIndex(index))
417 {
418 return Optional<GLuint>::Invalid();
419 }
420
421 return getSamplerIndexFromUniformIndex(index);
422}
423
424bool ProgramState::isSamplerUniformIndex(GLuint index) const
425{
Jamie Madill982f6e02017-06-07 14:33:04 -0400426 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500427}
428
429GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
430{
431 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400432 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500433}
434
Jamie Madill34ca4f52017-06-13 11:49:39 -0400435GLuint ProgramState::getAttributeLocation(const std::string &name) const
436{
437 for (const sh::Attribute &attribute : mAttributes)
438 {
439 if (attribute.name == name)
440 {
441 return attribute.location;
442 }
443 }
444
445 return static_cast<GLuint>(-1);
446}
447
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500448Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400449 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400450 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500451 mLinked(false),
452 mDeleteStatus(false),
453 mRefCount(0),
454 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500455 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500456{
457 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000458
Geoff Lang7dd2e102014-11-10 15:19:26 -0500459 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460}
461
462Program::~Program()
463{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400464 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465}
466
Jamie Madill4928b7c2017-06-20 12:57:39 -0400467void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500468{
469 if (mState.mAttachedVertexShader != nullptr)
470 {
471 mState.mAttachedVertexShader->release(context);
472 mState.mAttachedVertexShader = nullptr;
473 }
474
475 if (mState.mAttachedFragmentShader != nullptr)
476 {
477 mState.mAttachedFragmentShader->release(context);
478 mState.mAttachedFragmentShader = nullptr;
479 }
480
481 if (mState.mAttachedComputeShader != nullptr)
482 {
483 mState.mAttachedComputeShader->release(context);
484 mState.mAttachedComputeShader = nullptr;
485 }
486
Jamie Madillc564c072017-06-01 12:45:42 -0400487 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400488
489 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
490 !mState.mAttachedComputeShader);
491 SafeDelete(mProgram);
492
493 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500494}
495
Geoff Lang70d0f492015-12-10 17:45:46 -0500496void Program::setLabel(const std::string &label)
497{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400498 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500499}
500
501const std::string &Program::getLabel() const
502{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400503 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500504}
505
Jamie Madillef300b12016-10-07 15:12:09 -0400506void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300508 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300510 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511 {
Jamie Madillef300b12016-10-07 15:12:09 -0400512 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300513 mState.mAttachedVertexShader = shader;
514 mState.mAttachedVertexShader->addRef();
515 break;
516 }
517 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518 {
Jamie Madillef300b12016-10-07 15:12:09 -0400519 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300520 mState.mAttachedFragmentShader = shader;
521 mState.mAttachedFragmentShader->addRef();
522 break;
523 }
524 case GL_COMPUTE_SHADER:
525 {
Jamie Madillef300b12016-10-07 15:12:09 -0400526 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300527 mState.mAttachedComputeShader = shader;
528 mState.mAttachedComputeShader->addRef();
529 break;
530 }
531 default:
532 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534}
535
Jamie Madillc1d770e2017-04-13 17:31:24 -0400536void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300538 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300540 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400542 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500543 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300544 mState.mAttachedVertexShader = nullptr;
545 break;
546 }
547 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400549 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500550 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300551 mState.mAttachedFragmentShader = nullptr;
552 break;
553 }
554 case GL_COMPUTE_SHADER:
555 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400556 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500557 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300558 mState.mAttachedComputeShader = nullptr;
559 break;
560 }
561 default:
562 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564}
565
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000566int Program::getAttachedShadersCount() const
567{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300568 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
569 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000570}
571
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572void Program::bindAttributeLocation(GLuint index, const char *name)
573{
Geoff Langd8605522016-04-13 10:19:12 -0400574 mAttributeBindings.bindLocation(index, name);
575}
576
577void Program::bindUniformLocation(GLuint index, const char *name)
578{
579 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800580 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581}
582
Sami Väisänen46eaa942016-06-29 10:26:37 +0300583void Program::bindFragmentInputLocation(GLint index, const char *name)
584{
585 mFragmentInputBindings.bindLocation(index, name);
586}
587
Jamie Madillbd044ed2017-06-05 12:59:21 -0400588BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300589{
590 BindingInfo ret;
591 ret.type = GL_NONE;
592 ret.valid = false;
593
Jamie Madillbd044ed2017-06-05 12:59:21 -0400594 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300595 ASSERT(fragmentShader);
596
597 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800598 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300599
600 for (const auto &binding : mFragmentInputBindings)
601 {
602 if (binding.second != static_cast<GLuint>(index))
603 continue;
604
605 ret.valid = true;
606
607 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400608 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300609
610 for (const auto &in : inputs)
611 {
612 if (in.name == originalName)
613 {
614 if (in.isArray())
615 {
616 // The client wants to bind either "name" or "name[0]".
617 // GL ES 3.1 spec refers to active array names with language such as:
618 // "if the string identifies the base name of an active array, where the
619 // string would exactly match the name of the variable if the suffix "[0]"
620 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400621 if (arrayIndex == GL_INVALID_INDEX)
622 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300623
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400624 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300625 }
626 else
627 {
628 ret.name = in.mappedName;
629 }
630 ret.type = in.type;
631 return ret;
632 }
633 }
634 }
635
636 return ret;
637}
638
Jamie Madillbd044ed2017-06-05 12:59:21 -0400639void Program::pathFragmentInputGen(const Context *context,
640 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300641 GLenum genMode,
642 GLint components,
643 const GLfloat *coeffs)
644{
645 // If the location is -1 then the command is silently ignored
646 if (index == -1)
647 return;
648
Jamie Madillbd044ed2017-06-05 12:59:21 -0400649 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300650
651 // If the input doesn't exist then then the command is silently ignored
652 // This could happen through optimization for example, the shader translator
653 // decides that a variable is not actually being used and optimizes it away.
654 if (binding.name.empty())
655 return;
656
657 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
658}
659
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660// The attached shaders are checked for linking errors by matching up their variables.
661// Uniform, input and output variables get collected.
662// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500663Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000664{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500665 const auto &data = context->getContextState();
666
Jamie Madill6c58b062017-08-01 13:44:25 -0400667 auto *platform = ANGLEPlatformCurrent();
668 double startTime = platform->currentTime(platform);
669
Jamie Madill6c1f6712017-02-14 19:08:04 -0500670 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000671
Jamie Madill32447362017-06-28 14:53:52 -0400672 ProgramHash programHash;
673 auto *cache = context->getMemoryProgramCache();
674 if (cache)
675 {
676 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400677 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400678 }
679
680 if (mLinked)
681 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400682 double delta = platform->currentTime(platform) - startTime;
683 int us = static_cast<int>(delta * 1000000.0);
684 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400685 return NoError();
686 }
687
688 // Cache load failed, fall through to normal linking.
689 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000690 mInfoLog.reset();
691
Martin Radev4c4c8e72016-08-04 12:25:34 +0300692 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500693
Jamie Madill192745a2016-12-22 15:58:21 -0500694 auto vertexShader = mState.mAttachedVertexShader;
695 auto fragmentShader = mState.mAttachedFragmentShader;
696 auto computeShader = mState.mAttachedComputeShader;
697
698 bool isComputeShaderAttached = (computeShader != nullptr);
699 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300700 // Check whether we both have a compute and non-compute shaders attached.
701 // If there are of both types attached, then linking should fail.
702 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
703 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500704 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
706 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400707 }
708
Jamie Madill192745a2016-12-22 15:58:21 -0500709 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500710 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400711 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300712 {
713 mInfoLog << "Attached compute shader is not compiled.";
714 return NoError();
715 }
Jamie Madill192745a2016-12-22 15:58:21 -0500716 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717
Jamie Madillbd044ed2017-06-05 12:59:21 -0400718 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300719
720 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
721 // If the work group size is not specified, a link time error should occur.
722 if (!mState.mComputeShaderLocalSize.isDeclared())
723 {
724 mInfoLog << "Work group size is not specified.";
725 return NoError();
726 }
727
Jamie Madillbd044ed2017-06-05 12:59:21 -0400728 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300729 {
730 return NoError();
731 }
732
Jiajia Qin729b2c62017-08-14 09:36:11 +0800733 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300734 {
735 return NoError();
736 }
737
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500738 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400739 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500740 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300741 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500742 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300743 }
744 }
745 else
746 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400747 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300748 {
749 return NoError();
750 }
Jamie Madill192745a2016-12-22 15:58:21 -0500751 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300752
Jamie Madillbd044ed2017-06-05 12:59:21 -0400753 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300754 {
755 return NoError();
756 }
Jamie Madill192745a2016-12-22 15:58:21 -0500757 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300758
Jamie Madillbd044ed2017-06-05 12:59:21 -0400759 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300760 {
761 mInfoLog << "Fragment shader version does not match vertex shader version.";
762 return NoError();
763 }
764
Jamie Madillbd044ed2017-06-05 12:59:21 -0400765 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300766 {
767 return NoError();
768 }
769
Jamie Madillbd044ed2017-06-05 12:59:21 -0400770 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300771 {
772 return NoError();
773 }
774
Jamie Madillbd044ed2017-06-05 12:59:21 -0400775 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300776 {
777 return NoError();
778 }
779
Jiajia Qin729b2c62017-08-14 09:36:11 +0800780 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300781 {
782 return NoError();
783 }
784
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400785 if (!linkValidateGlobalNames(context, mInfoLog))
786 {
787 return NoError();
788 }
789
Jamie Madillbd044ed2017-06-05 12:59:21 -0400790 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300791
Martin Radev7cf61662017-07-26 17:10:53 +0300792 mState.mNumViews = vertexShader->getNumViews(context);
793
Jamie Madillbd044ed2017-06-05 12:59:21 -0400794 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300795
Jamie Madill192745a2016-12-22 15:58:21 -0500796 // Validate we can pack the varyings.
797 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
798
799 // Map the varyings to the register file
800 // In WebGL, we use a slightly different handling for packing variables.
801 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
802 : PackMode::ANGLE_RELAXED;
803 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
804 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
805 mState.getTransformFeedbackVaryingNames()))
806 {
807 return NoError();
808 }
809
Olli Etuaho39e78122017-08-29 14:34:22 +0300810 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
811 {
812 return NoError();
813 }
814
Jamie Madillc564c072017-06-01 12:45:42 -0400815 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500816 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300817 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500818 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300819 }
820
821 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500822 }
823
jchen10eaef1e52017-06-13 10:44:11 +0800824 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400825 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400826
jchen10eaef1e52017-06-13 10:44:11 +0800827 setUniformValuesFromBindingQualifiers();
828
Jamie Madill54164b02017-08-28 15:17:37 -0400829 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400830 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400831
Jamie Madill32447362017-06-28 14:53:52 -0400832 // Save to the program cache.
833 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
834 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
835 {
836 cache->putProgram(programHash, context, this);
837 }
838
Jamie Madill6c58b062017-08-01 13:44:25 -0400839 double delta = platform->currentTime(platform) - startTime;
840 int us = static_cast<int>(delta * 1000000.0);
841 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
842
Martin Radev4c4c8e72016-08-04 12:25:34 +0300843 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000844}
845
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000846// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500847void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400849 mState.mAttributes.clear();
850 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400851 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800852 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400853 mState.mUniforms.clear();
854 mState.mUniformLocations.clear();
855 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800856 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800857 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400858 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800859 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400860 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400861 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300862 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500863 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800864 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300865 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500866
Geoff Lang7dd2e102014-11-10 15:19:26 -0500867 mValidated = false;
868
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000869 mLinked = false;
870}
871
Geoff Lange1a27752015-10-05 13:16:04 -0400872bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000873{
874 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875}
876
Jamie Madilla2c74982016-12-12 11:20:42 -0500877Error Program::loadBinary(const Context *context,
878 GLenum binaryFormat,
879 const void *binary,
880 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000881{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500882 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000883
Geoff Lang7dd2e102014-11-10 15:19:26 -0500884#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800885 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500886#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400887 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
888 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000889 {
Jamie Madillf6113162015-05-07 11:49:21 -0400890 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800891 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892 }
893
Jamie Madill4f86d052017-06-05 12:59:26 -0400894 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
895 ANGLE_TRY_RESULT(
896 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400897
898 // Currently we require the full shader text to compute the program hash.
899 // TODO(jmadill): Store the binary in the internal program cache.
900
Jamie Madillb0a838b2016-11-13 20:02:12 -0500901 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500902#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500903}
904
Jamie Madilla2c74982016-12-12 11:20:42 -0500905Error Program::saveBinary(const Context *context,
906 GLenum *binaryFormat,
907 void *binary,
908 GLsizei bufSize,
909 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500910{
911 if (binaryFormat)
912 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400913 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500914 }
915
Jamie Madill4f86d052017-06-05 12:59:26 -0400916 angle::MemoryBuffer memoryBuf;
917 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500918
Jamie Madill4f86d052017-06-05 12:59:26 -0400919 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
920 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500921
922 if (streamLength > bufSize)
923 {
924 if (length)
925 {
926 *length = 0;
927 }
928
929 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
930 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
931 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500932 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500933 }
934
935 if (binary)
936 {
937 char *ptr = reinterpret_cast<char*>(binary);
938
Jamie Madill48ef11b2016-04-27 15:21:52 -0400939 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500940 ptr += streamLength;
941
942 ASSERT(ptr - streamLength == binary);
943 }
944
945 if (length)
946 {
947 *length = streamLength;
948 }
949
He Yunchaoacd18982017-01-04 10:46:42 +0800950 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951}
952
Jamie Madillffe00c02017-06-27 16:26:55 -0400953GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500954{
955 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400956 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500957 if (error.isError())
958 {
959 return 0;
960 }
961
962 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000963}
964
Geoff Langc5629752015-12-07 16:29:04 -0500965void Program::setBinaryRetrievableHint(bool retrievable)
966{
967 // TODO(jmadill) : replace with dirty bits
968 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400969 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500970}
971
972bool Program::getBinaryRetrievableHint() const
973{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400974 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500975}
976
Yunchao He61afff12017-03-14 15:34:03 +0800977void Program::setSeparable(bool separable)
978{
979 // TODO(yunchao) : replace with dirty bits
980 if (mState.mSeparable != separable)
981 {
982 mProgram->setSeparable(separable);
983 mState.mSeparable = separable;
984 }
985}
986
987bool Program::isSeparable() const
988{
989 return mState.mSeparable;
990}
991
Jamie Madill6c1f6712017-02-14 19:08:04 -0500992void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000993{
994 mRefCount--;
995
996 if (mRefCount == 0 && mDeleteStatus)
997 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500998 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000999 }
1000}
1001
1002void Program::addRef()
1003{
1004 mRefCount++;
1005}
1006
1007unsigned int Program::getRefCount() const
1008{
1009 return mRefCount;
1010}
1011
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001012int Program::getInfoLogLength() const
1013{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001014 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001015}
1016
Geoff Lange1a27752015-10-05 13:16:04 -04001017void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001018{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001019 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001020}
1021
Geoff Lange1a27752015-10-05 13:16:04 -04001022void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001023{
1024 int total = 0;
1025
Martin Radev4c4c8e72016-08-04 12:25:34 +03001026 if (mState.mAttachedComputeShader)
1027 {
1028 if (total < maxCount)
1029 {
1030 shaders[total] = mState.mAttachedComputeShader->getHandle();
1031 total++;
1032 }
1033 }
1034
Jamie Madill48ef11b2016-04-27 15:21:52 -04001035 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001036 {
1037 if (total < maxCount)
1038 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001039 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001040 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001041 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001042 }
1043
Jamie Madill48ef11b2016-04-27 15:21:52 -04001044 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001045 {
1046 if (total < maxCount)
1047 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001048 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001049 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001050 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001051 }
1052
1053 if (count)
1054 {
1055 *count = total;
1056 }
1057}
1058
Geoff Lange1a27752015-10-05 13:16:04 -04001059GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001060{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001061 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001062}
1063
Jamie Madill63805b42015-08-25 13:17:39 -04001064bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001065{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001066 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1067 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001068}
1069
jchen10fd7c3b52017-03-21 15:36:03 +08001070void Program::getActiveAttribute(GLuint index,
1071 GLsizei bufsize,
1072 GLsizei *length,
1073 GLint *size,
1074 GLenum *type,
1075 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001076{
Jamie Madillc349ec02015-08-21 16:53:12 -04001077 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001078 {
1079 if (bufsize > 0)
1080 {
1081 name[0] = '\0';
1082 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001084 if (length)
1085 {
1086 *length = 0;
1087 }
1088
1089 *type = GL_NONE;
1090 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001091 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001093
jchen1036e120e2017-03-14 14:53:58 +08001094 ASSERT(index < mState.mAttributes.size());
1095 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001096
1097 if (bufsize > 0)
1098 {
jchen10fd7c3b52017-03-21 15:36:03 +08001099 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001100 }
1101
1102 // Always a single 'type' instance
1103 *size = 1;
1104 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001105}
1106
Geoff Lange1a27752015-10-05 13:16:04 -04001107GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108{
Jamie Madillc349ec02015-08-21 16:53:12 -04001109 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001110 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001111 return 0;
1112 }
1113
jchen1036e120e2017-03-14 14:53:58 +08001114 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001115}
1116
Geoff Lange1a27752015-10-05 13:16:04 -04001117GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001118{
Jamie Madillc349ec02015-08-21 16:53:12 -04001119 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001120 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001121 return 0;
1122 }
1123
1124 size_t maxLength = 0;
1125
Jamie Madill48ef11b2016-04-27 15:21:52 -04001126 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001127 {
jchen1036e120e2017-03-14 14:53:58 +08001128 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001129 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130
Jamie Madillc349ec02015-08-21 16:53:12 -04001131 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001132}
1133
jchen1015015f72017-03-16 13:54:21 +08001134GLuint Program::getInputResourceIndex(const GLchar *name) const
1135{
1136 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1137 {
1138 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1139 if (attribute.name == name)
1140 {
1141 return attributeIndex;
1142 }
1143 }
1144 return GL_INVALID_INDEX;
1145}
1146
1147GLuint Program::getOutputResourceIndex(const GLchar *name) const
1148{
1149 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1150}
1151
jchen10fd7c3b52017-03-21 15:36:03 +08001152size_t Program::getOutputResourceCount() const
1153{
1154 return (mLinked ? mState.mOutputVariables.size() : 0);
1155}
1156
jchen10baf5d942017-08-28 20:45:48 +08001157template <typename T>
1158void Program::getResourceName(GLuint index,
1159 const std::vector<T> &resources,
1160 GLsizei bufSize,
1161 GLsizei *length,
1162 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001163{
1164 if (length)
1165 {
1166 *length = 0;
1167 }
1168
1169 if (!mLinked)
1170 {
1171 if (bufSize > 0)
1172 {
1173 name[0] = '\0';
1174 }
1175 return;
1176 }
jchen10baf5d942017-08-28 20:45:48 +08001177 ASSERT(index < resources.size());
1178 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001179
1180 if (bufSize > 0)
1181 {
jchen10baf5d942017-08-28 20:45:48 +08001182 std::string nameWithArray = (resource.isArray() ? resource.name + "[0]" : resource.name);
jchen10fd7c3b52017-03-21 15:36:03 +08001183
1184 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1185 }
1186}
1187
jchen10baf5d942017-08-28 20:45:48 +08001188void Program::getInputResourceName(GLuint index,
1189 GLsizei bufSize,
1190 GLsizei *length,
1191 GLchar *name) const
1192{
1193 getResourceName(index, mState.mAttributes, bufSize, length, name);
1194}
1195
1196void Program::getOutputResourceName(GLuint index,
1197 GLsizei bufSize,
1198 GLsizei *length,
1199 GLchar *name) const
1200{
1201 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1202}
1203
1204void Program::getUniformResourceName(GLuint index,
1205 GLsizei bufSize,
1206 GLsizei *length,
1207 GLchar *name) const
1208{
1209 getResourceName(index, mState.mUniforms, bufSize, length, name);
1210}
1211
jchen10880683b2017-04-12 16:21:55 +08001212const sh::Attribute &Program::getInputResource(GLuint index) const
1213{
1214 ASSERT(index < mState.mAttributes.size());
1215 return mState.mAttributes[index];
1216}
1217
1218const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1219{
1220 ASSERT(index < mState.mOutputVariables.size());
1221 return mState.mOutputVariables[index];
1222}
1223
Geoff Lang7dd2e102014-11-10 15:19:26 -05001224GLint Program::getFragDataLocation(const std::string &name) const
1225{
1226 std::string baseName(name);
1227 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001228 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001229 {
Jamie Madillfb997ec2017-09-20 15:44:27 -04001230 const VariableLocation &locationInfo = outputPair.second;
1231 const sh::OutputVariable &outputVariable = mState.mOutputVariables[locationInfo.index];
Olli Etuahoc8538042017-09-27 11:20:15 +03001232 ASSERT(locationInfo.arrayIndices.size() <= 1);
Jamie Madillfb997ec2017-09-20 15:44:27 -04001233 if (outputVariable.name == baseName &&
Olli Etuahoc8538042017-09-27 11:20:15 +03001234 (arrayIndex == GL_INVALID_INDEX || (!locationInfo.arrayIndices.empty() &&
1235 arrayIndex == locationInfo.arrayIndices.back())))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001236 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001237 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001239 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241}
1242
Geoff Lange1a27752015-10-05 13:16:04 -04001243void Program::getActiveUniform(GLuint index,
1244 GLsizei bufsize,
1245 GLsizei *length,
1246 GLint *size,
1247 GLenum *type,
1248 GLchar *name) 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 Madill62d31cb2015-09-11 13:25:51 -04001252 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001253 ASSERT(index < mState.mUniforms.size());
1254 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001255
1256 if (bufsize > 0)
1257 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001258 std::string string = uniform.name;
1259 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001260 {
1261 string += "[0]";
1262 }
jchen10fd7c3b52017-03-21 15:36:03 +08001263 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264 }
1265
Jamie Madill62d31cb2015-09-11 13:25:51 -04001266 *size = uniform.elementCount();
1267 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001268 }
1269 else
1270 {
1271 if (bufsize > 0)
1272 {
1273 name[0] = '\0';
1274 }
1275
1276 if (length)
1277 {
1278 *length = 0;
1279 }
1280
1281 *size = 0;
1282 *type = GL_NONE;
1283 }
1284}
1285
Geoff Lange1a27752015-10-05 13:16:04 -04001286GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001287{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001290 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001291 }
1292 else
1293 {
1294 return 0;
1295 }
1296}
1297
Geoff Lange1a27752015-10-05 13:16:04 -04001298GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001299{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001300 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001301
1302 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001303 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001304 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001306 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001308 size_t length = uniform.name.length() + 1u;
1309 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310 {
1311 length += 3; // Counting in "[0]".
1312 }
1313 maxLength = std::max(length, maxLength);
1314 }
1315 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001316 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001317
Jamie Madill62d31cb2015-09-11 13:25:51 -04001318 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
Geoff Lang7dd2e102014-11-10 15:19:26 -05001321bool Program::isValidUniformLocation(GLint location) const
1322{
Jamie Madille2e406c2016-06-02 13:04:10 -04001323 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001324 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001325 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001326}
1327
Jamie Madill62d31cb2015-09-11 13:25:51 -04001328const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001330 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001331 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
Jamie Madillac4e9c32017-01-13 14:07:12 -05001334const VariableLocation &Program::getUniformLocation(GLint location) const
1335{
1336 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1337 return mState.mUniformLocations[location];
1338}
1339
1340const std::vector<VariableLocation> &Program::getUniformLocations() const
1341{
1342 return mState.mUniformLocations;
1343}
1344
1345const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1346{
1347 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1348 return mState.mUniforms[index];
1349}
1350
Jamie Madill62d31cb2015-09-11 13:25:51 -04001351GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001352{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001353 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354}
1355
Jamie Madill62d31cb2015-09-11 13:25:51 -04001356GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357{
Jamie Madille7d84322017-01-10 18:21:59 -05001358 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359}
1360
1361void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1362{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001363 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1364 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001365 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001366}
1367
1368void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1369{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001370 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1371 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001372 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
1375void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1376{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001377 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1378 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001379 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380}
1381
1382void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1383{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001384 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1385 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001386 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387}
1388
Jamie Madill81c2e252017-09-09 23:32:46 -04001389Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001391 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1392 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1393
Jamie Madill81c2e252017-09-09 23:32:46 -04001394 mProgram->setUniform1iv(location, clampedCount, v);
1395
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001396 if (mState.isSamplerUniformIndex(locationInfo.index))
1397 {
1398 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001399 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001400 }
1401
Jamie Madill81c2e252017-09-09 23:32:46 -04001402 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
1405void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1406{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001407 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1408 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001409 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410}
1411
1412void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1413{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001414 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1415 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001416 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417}
1418
1419void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1420{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001421 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1422 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001423 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424}
1425
1426void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1427{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001428 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1429 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001430 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431}
1432
1433void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1434{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001435 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1436 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001437 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438}
1439
1440void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1441{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001442 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1443 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001444 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445}
1446
1447void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1448{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001449 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1450 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001451 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001452}
1453
1454void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1455{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001456 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001457 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458}
1459
1460void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1461{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001462 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001463 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464}
1465
1466void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1467{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001468 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001469 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470}
1471
1472void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1473{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001474 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001475 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476}
1477
1478void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1479{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001480 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001481 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001482}
1483
1484void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1485{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001486 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001487 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001488}
1489
1490void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1491{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001492 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001493 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494}
1495
1496void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1497{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001498 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001499 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001500}
1501
1502void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1503{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001504 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001505 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001506}
1507
Jamie Madill54164b02017-08-28 15:17:37 -04001508void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509{
Jamie Madill54164b02017-08-28 15:17:37 -04001510 const auto &uniformLocation = mState.getUniformLocations()[location];
1511 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1512
1513 GLenum nativeType = gl::VariableComponentType(uniform.type);
1514 if (nativeType == GL_FLOAT)
1515 {
1516 mProgram->getUniformfv(context, location, v);
1517 }
1518 else
1519 {
1520 getUniformInternal(context, v, location, nativeType,
1521 gl::VariableComponentCount(uniform.type));
1522 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523}
1524
Jamie Madill54164b02017-08-28 15:17:37 -04001525void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001526{
Jamie Madill54164b02017-08-28 15:17:37 -04001527 const auto &uniformLocation = mState.getUniformLocations()[location];
1528 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1529
1530 GLenum nativeType = gl::VariableComponentType(uniform.type);
1531 if (nativeType == GL_INT || nativeType == GL_BOOL)
1532 {
1533 mProgram->getUniformiv(context, location, v);
1534 }
1535 else
1536 {
1537 getUniformInternal(context, v, location, nativeType,
1538 gl::VariableComponentCount(uniform.type));
1539 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540}
1541
Jamie Madill54164b02017-08-28 15:17:37 -04001542void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543{
Jamie Madill54164b02017-08-28 15:17:37 -04001544 const auto &uniformLocation = mState.getUniformLocations()[location];
1545 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1546
1547 GLenum nativeType = gl::VariableComponentType(uniform.type);
1548 if (nativeType == GL_UNSIGNED_INT)
1549 {
1550 mProgram->getUniformuiv(context, location, v);
1551 }
1552 else
1553 {
1554 getUniformInternal(context, v, location, nativeType,
1555 gl::VariableComponentCount(uniform.type));
1556 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557}
1558
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559void Program::flagForDeletion()
1560{
1561 mDeleteStatus = true;
1562}
1563
1564bool Program::isFlaggedForDeletion() const
1565{
1566 return mDeleteStatus;
1567}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001568
Brandon Jones43a53e22014-08-28 16:23:22 -07001569void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001570{
1571 mInfoLog.reset();
1572
Geoff Lang7dd2e102014-11-10 15:19:26 -05001573 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001574 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001575 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001576 }
1577 else
1578 {
Jamie Madillf6113162015-05-07 11:49:21 -04001579 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001580 }
1581}
1582
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1584{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001585 // Skip cache if we're using an infolog, so we get the full error.
1586 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1587 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1588 {
1589 return mCachedValidateSamplersResult.value();
1590 }
1591
1592 if (mTextureUnitTypesCache.empty())
1593 {
1594 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1595 }
1596 else
1597 {
1598 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1599 }
1600
1601 // if any two active samplers in a program are of different types, but refer to the same
1602 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1603 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001604 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001605 {
Jamie Madill54164b02017-08-28 15:17:37 -04001606 if (samplerBinding.unreferenced)
1607 continue;
1608
Jamie Madille7d84322017-01-10 18:21:59 -05001609 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001610
Jamie Madille7d84322017-01-10 18:21:59 -05001611 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001612 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001613 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1614 {
1615 if (infoLog)
1616 {
1617 (*infoLog) << "Sampler uniform (" << textureUnit
1618 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1619 << caps.maxCombinedTextureImageUnits << ")";
1620 }
1621
1622 mCachedValidateSamplersResult = false;
1623 return false;
1624 }
1625
1626 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1627 {
1628 if (textureType != mTextureUnitTypesCache[textureUnit])
1629 {
1630 if (infoLog)
1631 {
1632 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1633 "image unit ("
1634 << textureUnit << ").";
1635 }
1636
1637 mCachedValidateSamplersResult = false;
1638 return false;
1639 }
1640 }
1641 else
1642 {
1643 mTextureUnitTypesCache[textureUnit] = textureType;
1644 }
1645 }
1646 }
1647
1648 mCachedValidateSamplersResult = true;
1649 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650}
1651
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001652bool Program::isValidated() const
1653{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654 return mValidated;
1655}
1656
Geoff Lange1a27752015-10-05 13:16:04 -04001657GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001659 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001660}
1661
Jiajia Qin729b2c62017-08-14 09:36:11 +08001662GLuint Program::getActiveShaderStorageBlockCount() const
1663{
1664 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1665}
1666
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1668{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001669 ASSERT(
1670 uniformBlockIndex <
1671 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672
Jiajia Qin729b2c62017-08-14 09:36:11 +08001673 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674
1675 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001676 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677 std::string string = uniformBlock.name;
1678
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001681 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682 }
jchen10fd7c3b52017-03-21 15:36:03 +08001683 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001684 }
1685}
1686
Geoff Lange1a27752015-10-05 13:16:04 -04001687GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001688{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689 int maxLength = 0;
1690
1691 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001692 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001693 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001694 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1695 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001696 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697 if (!uniformBlock.name.empty())
1698 {
jchen10af713a22017-04-19 09:10:56 +08001699 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1700 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001701 }
1702 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001703 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704
1705 return maxLength;
1706}
1707
Geoff Lange1a27752015-10-05 13:16:04 -04001708GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709{
Olli Etuahoc8538042017-09-27 11:20:15 +03001710 std::vector<unsigned int> subscripts;
1711 std::string baseName = ParseResourceName(name, &subscripts);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001712
Jamie Madill48ef11b2016-04-27 15:21:52 -04001713 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001714 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1715 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001716 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717 if (uniformBlock.name == baseName)
1718 {
1719 const bool arrayElementZero =
Olli Etuahoc8538042017-09-27 11:20:15 +03001720 (subscripts.empty() && (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1721 const bool arrayElementMatches =
1722 (subscripts.size() == 1 && subscripts[0] == uniformBlock.arrayElement);
1723 if (arrayElementMatches || arrayElementZero)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001724 {
1725 return blockIndex;
1726 }
1727 }
1728 }
1729
1730 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001731}
1732
Jiajia Qin729b2c62017-08-14 09:36:11 +08001733const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001734{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001735 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1736 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001737}
1738
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001739void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1740{
jchen107a20b972017-06-13 14:25:26 +08001741 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001742 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001743 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001744}
1745
1746GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1747{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001748 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001749}
1750
Jiajia Qin729b2c62017-08-14 09:36:11 +08001751GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1752{
1753 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1754}
1755
Geoff Lang48dcae72014-02-05 16:28:24 -05001756void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1757{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001758 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001759 for (GLsizei i = 0; i < count; i++)
1760 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001761 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001762 }
1763
Jamie Madill48ef11b2016-04-27 15:21:52 -04001764 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001765}
1766
1767void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1768{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001769 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001770 {
jchen10a9042d32017-03-17 08:50:45 +08001771 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1772 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1773 std::string varName = var.nameWithArrayIndex();
1774 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001775 if (length)
1776 {
1777 *length = lastNameIdx;
1778 }
1779 if (size)
1780 {
jchen10a9042d32017-03-17 08:50:45 +08001781 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001782 }
1783 if (type)
1784 {
jchen10a9042d32017-03-17 08:50:45 +08001785 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001786 }
1787 if (name)
1788 {
jchen10a9042d32017-03-17 08:50:45 +08001789 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001790 name[lastNameIdx] = '\0';
1791 }
1792 }
1793}
1794
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001795GLsizei Program::getTransformFeedbackVaryingCount() const
1796{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001798 {
jchen10a9042d32017-03-17 08:50:45 +08001799 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001800 }
1801 else
1802 {
1803 return 0;
1804 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001805}
1806
1807GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1808{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001810 {
1811 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001812 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001813 {
jchen10a9042d32017-03-17 08:50:45 +08001814 maxSize =
1815 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001816 }
1817
1818 return maxSize;
1819 }
1820 else
1821 {
1822 return 0;
1823 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001824}
1825
1826GLenum Program::getTransformFeedbackBufferMode() const
1827{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001828 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829}
1830
Jamie Madillbd044ed2017-06-05 12:59:21 -04001831bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001833 Shader *vertexShader = mState.mAttachedVertexShader;
1834 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001835
Jamie Madillbd044ed2017-06-05 12:59:21 -04001836 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001837
Jiawei Shao3d404882017-10-16 13:30:48 +08001838 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
1839 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840
Sami Väisänen46eaa942016-06-29 10:26:37 +03001841 std::map<GLuint, std::string> staticFragmentInputLocations;
1842
Jamie Madill4cff2472015-08-21 16:53:18 -04001843 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001845 bool matched = false;
1846
1847 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001848 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849 {
1850 continue;
1851 }
1852
Jamie Madill4cff2472015-08-21 16:53:18 -04001853 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001855 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001857 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001858 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001859 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001860 {
1861 return false;
1862 }
1863
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 matched = true;
1865 break;
1866 }
1867 }
1868
1869 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001870 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001872 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873 return false;
1874 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001875
1876 // Check for aliased path rendering input bindings (if any).
1877 // If more than one binding refer statically to the same
1878 // location the link must fail.
1879
1880 if (!output.staticUse)
1881 continue;
1882
1883 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1884 if (inputBinding == -1)
1885 continue;
1886
1887 const auto it = staticFragmentInputLocations.find(inputBinding);
1888 if (it == std::end(staticFragmentInputLocations))
1889 {
1890 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1891 }
1892 else
1893 {
1894 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1895 << it->second;
1896 return false;
1897 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898 }
1899
Jamie Madillbd044ed2017-06-05 12:59:21 -04001900 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001901 {
1902 return false;
1903 }
1904
Jamie Madillada9ecc2015-08-17 12:53:37 -04001905 // TODO(jmadill): verify no unmatched vertex varyings?
1906
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 return true;
1908}
1909
Jamie Madillbd044ed2017-06-05 12:59:21 -04001910bool Program::linkUniforms(const Context *context,
1911 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001912 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001913{
Olli Etuahob78707c2017-03-09 15:03:11 +00001914 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001915 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001916 {
1917 return false;
1918 }
1919
Olli Etuahob78707c2017-03-09 15:03:11 +00001920 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001922 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001923
jchen10eaef1e52017-06-13 10:44:11 +08001924 if (!linkAtomicCounterBuffers())
1925 {
1926 return false;
1927 }
1928
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001929 return true;
1930}
1931
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001932void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001933{
Jamie Madill982f6e02017-06-07 14:33:04 -04001934 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1935 unsigned int low = high;
1936
jchen10eaef1e52017-06-13 10:44:11 +08001937 for (auto counterIter = mState.mUniforms.rbegin();
1938 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1939 {
1940 --low;
1941 }
1942
1943 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1944
1945 high = low;
1946
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001947 for (auto imageIter = mState.mUniforms.rbegin();
1948 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1949 {
1950 --low;
1951 }
1952
1953 mState.mImageUniformRange = RangeUI(low, high);
1954
1955 // If uniform is a image type, insert it into the mImageBindings array.
1956 for (unsigned int imageIndex : mState.mImageUniformRange)
1957 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001958 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1959 // cannot load values into a uniform defined as an image. if declare without a
1960 // binding qualifier, any uniform image variable (include all elements of
1961 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001962 auto &imageUniform = mState.mUniforms[imageIndex];
1963 if (imageUniform.binding == -1)
1964 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001965 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001966 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001967 else
1968 {
1969 mState.mImageBindings.emplace_back(
1970 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1971 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001972 }
1973
1974 high = low;
1975
1976 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001977 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001978 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001979 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001980 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001981
1982 mState.mSamplerUniformRange = RangeUI(low, high);
1983
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001984 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001985 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001986 {
1987 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1988 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1989 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001990 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001991 }
1992}
1993
jchen10eaef1e52017-06-13 10:44:11 +08001994bool Program::linkAtomicCounterBuffers()
1995{
1996 for (unsigned int index : mState.mAtomicCounterUniformRange)
1997 {
1998 auto &uniform = mState.mUniforms[index];
1999 bool found = false;
2000 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2001 ++bufferIndex)
2002 {
2003 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2004 if (buffer.binding == uniform.binding)
2005 {
2006 buffer.memberIndexes.push_back(index);
2007 uniform.bufferIndex = bufferIndex;
2008 found = true;
2009 break;
2010 }
2011 }
2012 if (!found)
2013 {
2014 AtomicCounterBuffer atomicCounterBuffer;
2015 atomicCounterBuffer.binding = uniform.binding;
2016 atomicCounterBuffer.memberIndexes.push_back(index);
2017 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2018 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2019 }
2020 }
2021 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2022 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2023
2024 return true;
2025}
2026
Martin Radev4c4c8e72016-08-04 12:25:34 +03002027bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2028 const std::string &uniformName,
2029 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002030 const sh::InterfaceBlockField &fragmentUniform,
2031 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002033 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2034 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2035 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 {
2037 return false;
2038 }
2039
2040 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2041 {
Jamie Madillf6113162015-05-07 11:49:21 -04002042 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 return false;
2044 }
2045
2046 return true;
2047}
2048
Jamie Madilleb979bf2016-11-15 12:28:46 -05002049// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002050bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002052 const ContextState &data = context->getContextState();
2053 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002054
Geoff Lang7dd2e102014-11-10 15:19:26 -05002055 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002056 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002057 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002058
2059 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002060 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002061 {
Jamie Madillf6113162015-05-07 11:49:21 -04002062 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002063 return false;
2064 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002066 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002067
Jamie Madillc349ec02015-08-21 16:53:12 -04002068 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002069 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002071 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002072 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002073 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002074 attribute.location = bindingLocation;
2075 }
2076
2077 if (attribute.location != -1)
2078 {
2079 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002080 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081
Jamie Madill63805b42015-08-25 13:17:39 -04002082 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 {
Jamie Madillf6113162015-05-07 11:49:21 -04002084 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002085 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086
2087 return false;
2088 }
2089
Jamie Madill63805b42015-08-25 13:17:39 -04002090 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 {
Jamie Madill63805b42015-08-25 13:17:39 -04002092 const int regLocation = attribute.location + reg;
2093 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094
2095 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002096 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002097 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002099 // TODO(jmadill): fix aliasing on ES2
2100 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002102 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002103 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 return false;
2105 }
2106 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002107 else
2108 {
Jamie Madill63805b42015-08-25 13:17:39 -04002109 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002110 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111
Jamie Madill63805b42015-08-25 13:17:39 -04002112 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113 }
2114 }
2115 }
2116
2117 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002118 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002120 // Not set by glBindAttribLocation or by location layout qualifier
2121 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122 {
Jamie Madill63805b42015-08-25 13:17:39 -04002123 int regs = VariableRegisterCount(attribute.type);
2124 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125
Jamie Madill63805b42015-08-25 13:17:39 -04002126 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 {
Jamie Madillf6113162015-05-07 11:49:21 -04002128 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002129 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 }
2131
Jamie Madillc349ec02015-08-21 16:53:12 -04002132 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133 }
2134 }
2135
Jamie Madill48ef11b2016-04-27 15:21:52 -04002136 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 {
Jamie Madill63805b42015-08-25 13:17:39 -04002138 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002139 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002140
Jamie Madillbd159f02017-10-09 19:39:06 -04002141 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002143 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2144 mState.mActiveAttribLocationsMask.set(location);
2145 mState.mMaxActiveAttribLocation =
2146 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147 }
2148 }
2149
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150 return true;
2151}
2152
Martin Radev4c4c8e72016-08-04 12:25:34 +03002153bool Program::validateVertexAndFragmentInterfaceBlocks(
2154 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2155 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002156 InfoLog &infoLog,
2157 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002158{
2159 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002160 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2161 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002162
2163 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2164 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002165 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002166 }
2167
Jamie Madille473dee2015-08-18 14:49:01 -04002168 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002170 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2171 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
2173 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002174 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2175 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176 {
2177 return false;
2178 }
2179 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002180 // TODO(jiajia.qin@intel.com): Add
2181 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002182 }
2183 return true;
2184}
Jamie Madille473dee2015-08-18 14:49:01 -04002185
Jiajia Qin729b2c62017-08-14 09:36:11 +08002186bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002187{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002188 const auto &caps = context->getCaps();
2189
Martin Radev4c4c8e72016-08-04 12:25:34 +03002190 if (mState.mAttachedComputeShader)
2191 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002192 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002193 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002194
Jiajia Qin729b2c62017-08-14 09:36:11 +08002195 if (!validateInterfaceBlocksCount(
2196 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002197 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2198 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002199 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002200 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002201 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002202
2203 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2204 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2205 computeShaderStorageBlocks,
2206 "Compute shader shader storage block count exceeds "
2207 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2208 infoLog))
2209 {
2210 return false;
2211 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002212 return true;
2213 }
2214
Jamie Madillbd044ed2017-06-05 12:59:21 -04002215 Shader &vertexShader = *mState.mAttachedVertexShader;
2216 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002217
Jiajia Qin729b2c62017-08-14 09:36:11 +08002218 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2219 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002220
Jiajia Qin729b2c62017-08-14 09:36:11 +08002221 if (!validateInterfaceBlocksCount(
2222 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002223 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2224 {
2225 return false;
2226 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002227 if (!validateInterfaceBlocksCount(
2228 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002229 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2230 infoLog))
2231 {
2232
2233 return false;
2234 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002235
2236 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002237 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002238 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002239 {
2240 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002241 }
Jamie Madille473dee2015-08-18 14:49:01 -04002242
Jiajia Qin729b2c62017-08-14 09:36:11 +08002243 if (context->getClientVersion() >= Version(3, 1))
2244 {
2245 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2246 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2247
2248 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2249 vertexShaderStorageBlocks,
2250 "Vertex shader shader storage block count exceeds "
2251 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2252 infoLog))
2253 {
2254 return false;
2255 }
2256 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2257 fragmentShaderStorageBlocks,
2258 "Fragment shader shader storage block count exceeds "
2259 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2260 infoLog))
2261 {
2262
2263 return false;
2264 }
2265
2266 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2267 fragmentShaderStorageBlocks, infoLog,
2268 webglCompatibility))
2269 {
2270 return false;
2271 }
2272 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002273 return true;
2274}
2275
Jamie Madilla2c74982016-12-12 11:20:42 -05002276bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002277 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002278 const sh::InterfaceBlock &fragmentInterfaceBlock,
2279 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280{
2281 const char* blockName = vertexInterfaceBlock.name.c_str();
2282 // validate blocks for the same member types
2283 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2284 {
Jamie Madillf6113162015-05-07 11:49:21 -04002285 infoLog << "Types for interface block '" << blockName
2286 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002287 return false;
2288 }
2289 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2290 {
Jamie Madillf6113162015-05-07 11:49:21 -04002291 infoLog << "Array sizes differ for interface block '" << blockName
2292 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293 return false;
2294 }
jchen10af713a22017-04-19 09:10:56 +08002295 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2296 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2297 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002298 {
Jamie Madillf6113162015-05-07 11:49:21 -04002299 infoLog << "Layout qualifiers differ for interface block '" << blockName
2300 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002301 return false;
2302 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002303 const unsigned int numBlockMembers =
2304 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002305 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2306 {
2307 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2308 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2309 if (vertexMember.name != fragmentMember.name)
2310 {
Jamie Madillf6113162015-05-07 11:49:21 -04002311 infoLog << "Name mismatch for field " << blockMemberIndex
2312 << " of interface block '" << blockName
2313 << "': (in vertex: '" << vertexMember.name
2314 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315 return false;
2316 }
2317 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002318 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2319 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002320 {
2321 return false;
2322 }
2323 }
2324 return true;
2325}
2326
2327bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2328 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2329{
2330 if (vertexVariable.type != fragmentVariable.type)
2331 {
Jamie Madillf6113162015-05-07 11:49:21 -04002332 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002333 return false;
2334 }
2335 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2336 {
Jamie Madillf6113162015-05-07 11:49:21 -04002337 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002338 return false;
2339 }
2340 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2341 {
Jamie Madillf6113162015-05-07 11:49:21 -04002342 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343 return false;
2344 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002345 if (vertexVariable.structName != fragmentVariable.structName)
2346 {
2347 infoLog << "Structure names for " << variableName
2348 << " differ between vertex and fragment shaders";
2349 return false;
2350 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351
2352 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2353 {
Jamie Madillf6113162015-05-07 11:49:21 -04002354 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002355 return false;
2356 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002357 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2359 {
2360 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2361 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2362
2363 if (vertexMember.name != fragmentMember.name)
2364 {
Jamie Madillf6113162015-05-07 11:49:21 -04002365 infoLog << "Name mismatch for field '" << memberIndex
2366 << "' of " << variableName
2367 << ": (in vertex: '" << vertexMember.name
2368 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369 return false;
2370 }
2371
2372 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2373 vertexMember.name + "'";
2374
2375 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2376 {
2377 return false;
2378 }
2379 }
2380
2381 return true;
2382}
2383
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002384bool Program::linkValidateVaryings(InfoLog &infoLog,
2385 const std::string &varyingName,
2386 const sh::Varying &vertexVarying,
2387 const sh::Varying &fragmentVarying,
2388 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002389{
2390 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2391 {
2392 return false;
2393 }
2394
Jamie Madille9cc4692015-02-19 16:00:13 -05002395 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002397 infoLog << "Interpolation types for " << varyingName
2398 << " differ between vertex and fragment shaders.";
2399 return false;
2400 }
2401
2402 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2403 {
2404 infoLog << "Invariance for " << varyingName
2405 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002406 return false;
2407 }
2408
2409 return true;
2410}
2411
Jamie Madillbd044ed2017-06-05 12:59:21 -04002412bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002413{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002414 Shader *vertexShader = mState.mAttachedVertexShader;
2415 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002416 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2417 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002418 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002419
2420 if (shaderVersion != 100)
2421 {
2422 // Only ESSL 1.0 has restrictions on matching input and output invariance
2423 return true;
2424 }
2425
2426 bool glPositionIsInvariant = false;
2427 bool glPointSizeIsInvariant = false;
2428 bool glFragCoordIsInvariant = false;
2429 bool glPointCoordIsInvariant = false;
2430
2431 for (const sh::Varying &varying : vertexVaryings)
2432 {
2433 if (!varying.isBuiltIn())
2434 {
2435 continue;
2436 }
2437 if (varying.name.compare("gl_Position") == 0)
2438 {
2439 glPositionIsInvariant = varying.isInvariant;
2440 }
2441 else if (varying.name.compare("gl_PointSize") == 0)
2442 {
2443 glPointSizeIsInvariant = varying.isInvariant;
2444 }
2445 }
2446
2447 for (const sh::Varying &varying : fragmentVaryings)
2448 {
2449 if (!varying.isBuiltIn())
2450 {
2451 continue;
2452 }
2453 if (varying.name.compare("gl_FragCoord") == 0)
2454 {
2455 glFragCoordIsInvariant = varying.isInvariant;
2456 }
2457 else if (varying.name.compare("gl_PointCoord") == 0)
2458 {
2459 glPointCoordIsInvariant = varying.isInvariant;
2460 }
2461 }
2462
2463 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2464 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2465 // Not requiring invariance to match is supported by:
2466 // dEQP, WebGL CTS, Nexus 5X GLES
2467 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2468 {
2469 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2470 "declared invariant.";
2471 return false;
2472 }
2473 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2474 {
2475 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2476 "declared invariant.";
2477 return false;
2478 }
2479
2480 return true;
2481}
2482
jchen10a9042d32017-03-17 08:50:45 +08002483bool Program::linkValidateTransformFeedback(const gl::Context *context,
2484 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002485 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002486 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002487{
2488 size_t totalComponents = 0;
2489
Jamie Madillccdf74b2015-08-18 10:46:12 -04002490 std::set<std::string> uniqueNames;
2491
Jamie Madill48ef11b2016-04-27 15:21:52 -04002492 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002493 {
2494 bool found = false;
Olli Etuahoc8538042017-09-27 11:20:15 +03002495 std::vector<unsigned int> subscripts;
2496 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002497
Jamie Madill192745a2016-12-22 15:58:21 -05002498 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002499 {
Jamie Madill192745a2016-12-22 15:58:21 -05002500 const sh::Varying *varying = ref.second.get();
2501
jchen10a9042d32017-03-17 08:50:45 +08002502 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002503 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002504 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002505 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002506 infoLog << "Two transform feedback varyings specify the same output variable ("
2507 << tfVaryingName << ").";
2508 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002509 }
jchen10a9042d32017-03-17 08:50:45 +08002510 if (context->getClientVersion() >= Version(3, 1))
2511 {
2512 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2513 {
2514 infoLog
2515 << "Two transform feedback varyings include the same array element ("
2516 << tfVaryingName << ").";
2517 return false;
2518 }
2519 }
2520 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002521 {
2522 infoLog << "Capture of arrays is undefined and not supported.";
2523 return false;
2524 }
2525
jchen10a9042d32017-03-17 08:50:45 +08002526 uniqueNames.insert(tfVaryingName);
2527
Jamie Madillccdf74b2015-08-18 10:46:12 -04002528 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002529 size_t elementCount =
Olli Etuahoc8538042017-09-27 11:20:15 +03002530 ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1);
jchen10a9042d32017-03-17 08:50:45 +08002531 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002532 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002533 componentCount > caps.maxTransformFeedbackSeparateComponents)
2534 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002535 infoLog << "Transform feedback varying's " << varying->name << " components ("
2536 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002537 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002538 return false;
2539 }
2540
2541 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002542 found = true;
2543 break;
2544 }
2545 }
jchen10a9042d32017-03-17 08:50:45 +08002546 if (context->getClientVersion() < Version(3, 1) &&
2547 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002548 {
Geoff Lang1a683462015-09-29 15:09:59 -04002549 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002550 return false;
2551 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002552 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2553 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002554 ASSERT(found);
2555 }
2556
Jamie Madill48ef11b2016-04-27 15:21:52 -04002557 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002558 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002559 {
Jamie Madillf6113162015-05-07 11:49:21 -04002560 infoLog << "Transform feedback varying total components (" << totalComponents
2561 << ") exceed the maximum interleaved components ("
2562 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002563 return false;
2564 }
2565
2566 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002567}
2568
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002569bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2570{
2571 const std::vector<sh::Uniform> &vertexUniforms =
2572 mState.mAttachedVertexShader->getUniforms(context);
2573 const std::vector<sh::Uniform> &fragmentUniforms =
2574 mState.mAttachedFragmentShader->getUniforms(context);
2575 const std::vector<sh::Attribute> &attributes =
2576 mState.mAttachedVertexShader->getActiveAttributes(context);
2577 for (const auto &attrib : attributes)
2578 {
2579 for (const auto &uniform : vertexUniforms)
2580 {
2581 if (uniform.name == attrib.name)
2582 {
2583 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2584 return false;
2585 }
2586 }
2587 for (const auto &uniform : fragmentUniforms)
2588 {
2589 if (uniform.name == attrib.name)
2590 {
2591 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2592 return false;
2593 }
2594 }
2595 }
2596 return true;
2597}
2598
Jamie Madill192745a2016-12-22 15:58:21 -05002599void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002600{
2601 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002602 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002603 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002604 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002605 std::vector<unsigned int> subscripts;
2606 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002607 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002608 if (!subscripts.empty())
2609 {
2610 subscript = subscripts.back();
2611 }
Jamie Madill192745a2016-12-22 15:58:21 -05002612 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002613 {
Jamie Madill192745a2016-12-22 15:58:21 -05002614 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002615 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002616 {
jchen10a9042d32017-03-17 08:50:45 +08002617 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2618 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002619 break;
2620 }
2621 }
2622 }
2623}
2624
Jamie Madillbd044ed2017-06-05 12:59:21 -04002625Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002626{
Jamie Madill192745a2016-12-22 15:58:21 -05002627 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002628
Jiawei Shao3d404882017-10-16 13:30:48 +08002629 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002630 {
Jamie Madill192745a2016-12-22 15:58:21 -05002631 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002632 }
2633
Jiawei Shao3d404882017-10-16 13:30:48 +08002634 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002635 {
Jamie Madill192745a2016-12-22 15:58:21 -05002636 merged[varying.name].fragment = &varying;
2637 }
2638
2639 return merged;
2640}
2641
2642std::vector<PackedVarying> Program::getPackedVaryings(
2643 const Program::MergedVaryings &mergedVaryings) const
2644{
2645 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2646 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002647 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002648
2649 for (const auto &ref : mergedVaryings)
2650 {
2651 const sh::Varying *input = ref.second.vertex;
2652 const sh::Varying *output = ref.second.fragment;
2653
2654 // Only pack varyings that have a matched input or output, plus special builtins.
2655 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002656 {
Jamie Madill192745a2016-12-22 15:58:21 -05002657 // Will get the vertex shader interpolation by default.
2658 auto interpolation = ref.second.get()->interpolation;
2659
Olli Etuaho06a06f52017-07-12 12:22:15 +03002660 // Note that we lose the vertex shader static use information here. The data for the
2661 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002662 if (output->isStruct())
2663 {
2664 ASSERT(!output->isArray());
2665 for (const auto &field : output->fields)
2666 {
2667 ASSERT(!field.isStruct() && !field.isArray());
2668 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2669 }
2670 }
2671 else
2672 {
2673 packedVaryings.push_back(PackedVarying(*output, interpolation));
2674 }
2675 continue;
2676 }
2677
2678 // Keep Transform FB varyings in the merged list always.
2679 if (!input)
2680 {
2681 continue;
2682 }
2683
2684 for (const std::string &tfVarying : tfVaryings)
2685 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002686 std::vector<unsigned int> subscripts;
2687 std::string baseName = ParseResourceName(tfVarying, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002688 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002689 if (!subscripts.empty())
2690 {
2691 subscript = subscripts.back();
2692 }
jchen10a9042d32017-03-17 08:50:45 +08002693 if (uniqueFullNames.count(tfVarying) > 0)
2694 {
2695 continue;
2696 }
2697 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002698 {
2699 // Transform feedback for varying structs is underspecified.
2700 // See Khronos bug 9856.
2701 // TODO(jmadill): Figure out how to be spec-compliant here.
2702 if (!input->isStruct())
2703 {
2704 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2705 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002706 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2707 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002708 }
jchen10a9042d32017-03-17 08:50:45 +08002709 if (subscript == GL_INVALID_INDEX)
2710 {
2711 break;
2712 }
Jamie Madill192745a2016-12-22 15:58:21 -05002713 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002714 }
2715 }
2716
Jamie Madill192745a2016-12-22 15:58:21 -05002717 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2718
2719 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002720}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002721
Jamie Madillbd044ed2017-06-05 12:59:21 -04002722void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002723{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002724 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002725 ASSERT(fragmentShader != nullptr);
2726
Geoff Lange0cff192017-05-30 13:04:56 -04002727 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002728 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002729
2730 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002731 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002732 {
2733 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2734 outputVariable.name != "gl_FragData")
2735 {
2736 continue;
2737 }
2738
2739 unsigned int baseLocation =
2740 (outputVariable.location == -1 ? 0u
2741 : static_cast<unsigned int>(outputVariable.location));
2742 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2743 elementIndex++)
2744 {
2745 const unsigned int location = baseLocation + elementIndex;
2746 if (location >= mState.mOutputVariableTypes.size())
2747 {
2748 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2749 }
Corentin Walleze7557742017-06-01 13:09:57 -04002750 ASSERT(location < mState.mActiveOutputVariables.size());
2751 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002752 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2753 }
2754 }
2755
Jamie Madill80a6fc02015-08-21 16:53:16 -04002756 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002757 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002758 return;
2759
Jamie Madillbd044ed2017-06-05 12:59:21 -04002760 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002761 // TODO(jmadill): any caps validation here?
2762
jchen1015015f72017-03-16 13:54:21 +08002763 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002764 outputVariableIndex++)
2765 {
jchen1015015f72017-03-16 13:54:21 +08002766 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002767
2768 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2769 if (outputVariable.isBuiltIn())
2770 continue;
2771
2772 // Since multiple output locations must be specified, use 0 for non-specified locations.
2773 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2774
Jamie Madill80a6fc02015-08-21 16:53:16 -04002775 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2776 elementIndex++)
2777 {
2778 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002779 ASSERT(mState.mOutputLocations.count(location) == 0);
Olli Etuahoc8538042017-09-27 11:20:15 +03002780 if (outputVariable.isArray())
2781 {
2782 mState.mOutputLocations[location] =
2783 VariableLocation(elementIndex, outputVariableIndex);
2784 }
2785 else
2786 {
2787 VariableLocation locationInfo;
2788 locationInfo.index = outputVariableIndex;
2789 mState.mOutputLocations[location] = locationInfo;
2790 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002791 }
2792 }
2793}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002794
Olli Etuaho48fed632017-03-16 12:05:30 +00002795void Program::setUniformValuesFromBindingQualifiers()
2796{
Jamie Madill982f6e02017-06-07 14:33:04 -04002797 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002798 {
2799 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2800 if (samplerUniform.binding != -1)
2801 {
2802 GLint location = mState.getUniformLocation(samplerUniform.name);
2803 ASSERT(location != -1);
2804 std::vector<GLint> boundTextureUnits;
2805 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2806 ++elementIndex)
2807 {
2808 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2809 }
2810 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2811 boundTextureUnits.data());
2812 }
2813 }
2814}
2815
jchen10eaef1e52017-06-13 10:44:11 +08002816void Program::gatherAtomicCounterBuffers()
2817{
jchen10baf5d942017-08-28 20:45:48 +08002818 for (unsigned int index : mState.mAtomicCounterUniformRange)
2819 {
2820 auto &uniform = mState.mUniforms[index];
2821 uniform.blockInfo.offset = uniform.offset;
2822 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2823 uniform.blockInfo.matrixStride = 0;
2824 uniform.blockInfo.isRowMajorMatrix = false;
2825 }
2826
jchen10eaef1e52017-06-13 10:44:11 +08002827 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2828}
2829
Jiajia Qin729b2c62017-08-14 09:36:11 +08002830void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002831{
Jiajia Qin729b2c62017-08-14 09:36:11 +08002832 for (const sh::InterfaceBlock &computeBlock : computeBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002833 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002834
Jiajia Qin729b2c62017-08-14 09:36:11 +08002835 // Only 'packed' blocks are allowed to be considered inactive.
2836 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2837 continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002838
Jiajia Qin729b2c62017-08-14 09:36:11 +08002839 defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002840 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002841}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002842
Jiajia Qin729b2c62017-08-14 09:36:11 +08002843void Program::gatherVertexAndFragmentBlockInfo(
2844 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2845 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks)
2846{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002847 std::set<std::string> visitedList;
2848
Jiajia Qin729b2c62017-08-14 09:36:11 +08002849 for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002850 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002851 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002852 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2853 continue;
2854
Jiajia Qin729b2c62017-08-14 09:36:11 +08002855 defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 visitedList.insert(vertexBlock.name);
2857 }
2858
Jiajia Qin729b2c62017-08-14 09:36:11 +08002859 for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002860 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002861 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002862 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2863 continue;
2864
2865 if (visitedList.count(fragmentBlock.name) > 0)
2866 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002867 if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002868 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002869 for (InterfaceBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002870 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002871 if (block.name == fragmentBlock.name)
2872 {
2873 block.fragmentStaticUse = fragmentBlock.staticUse;
2874 }
2875 }
2876 }
2877 else
2878 {
2879 ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2880 for (InterfaceBlock &block : mState.mShaderStorageBlocks)
2881 {
2882 if (block.name == fragmentBlock.name)
2883 {
2884 block.fragmentStaticUse = fragmentBlock.staticUse;
2885 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002886 }
2887 }
2888
2889 continue;
2890 }
2891
Jiajia Qin729b2c62017-08-14 09:36:11 +08002892 defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002893 visitedList.insert(fragmentBlock.name);
2894 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002895}
2896
2897void Program::gatherInterfaceBlockInfo(const Context *context)
2898{
2899 ASSERT(mState.mUniformBlocks.empty());
2900 ASSERT(mState.mShaderStorageBlocks.empty());
2901
2902 if (mState.mAttachedComputeShader)
2903 {
2904 Shader *computeShader = mState.getAttachedComputeShader();
2905
2906 gatherComputeBlockInfo(computeShader->getUniformBlocks(context));
2907 gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context));
2908 return;
2909 }
2910
2911 Shader *vertexShader = mState.getAttachedVertexShader();
2912 Shader *fragmentShader = mState.getAttachedFragmentShader();
2913
2914 gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context),
2915 fragmentShader->getUniformBlocks(context));
2916 if (context->getClientVersion() >= Version(3, 1))
2917 {
2918 gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context),
2919 fragmentShader->getShaderStorageBlocks(context));
2920 }
2921
jchen10af713a22017-04-19 09:10:56 +08002922 // Set initial bindings from shader.
2923 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2924 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002925 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002926 bindUniformBlock(blockIndex, uniformBlock.binding);
2927 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002928}
2929
Jamie Madill4a3c2342015-10-08 12:58:45 -04002930template <typename VarT>
2931void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2932 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002933 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002934 int blockIndex)
2935{
2936 for (const VarT &field : fields)
2937 {
2938 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2939
Olli Etuaho855d9642017-05-17 14:05:06 +03002940 const std::string &fullMappedName =
2941 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2942
Jamie Madill4a3c2342015-10-08 12:58:45 -04002943 if (field.isStruct())
2944 {
2945 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2946 {
2947 const std::string uniformElementName =
2948 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002949 const std::string uniformElementMappedName =
2950 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2951 defineUniformBlockMembers(field.fields, uniformElementName,
2952 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002953 }
2954 }
2955 else
2956 {
2957 // If getBlockMemberInfo returns false, the uniform is optimized out.
2958 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002959 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002960 {
2961 continue;
2962 }
2963
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002964 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002965 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002966 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002967
2968 // Since block uniforms have no location, we don't need to store them in the uniform
2969 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002970 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002971 }
2972 }
2973}
2974
Jiajia Qin729b2c62017-08-14 09:36:11 +08002975void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002976{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002977 size_t blockSize = 0;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002978 std::vector<unsigned int> blockIndexes;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002979
Jiajia Qin729b2c62017-08-14 09:36:11 +08002980 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002981 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002982 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
2983 // Track the first and last uniform index to determine the range of active uniforms in the
2984 // block.
2985 size_t firstBlockUniformIndex = mState.mUniforms.size();
2986 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2987 interfaceBlock.fieldMappedPrefix(), blockIndex);
2988 size_t lastBlockUniformIndex = mState.mUniforms.size();
2989
2990 for (size_t blockUniformIndex = firstBlockUniformIndex;
2991 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2992 {
2993 blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2994 }
2995 }
2996 else
2997 {
2998 // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index.
2999 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003000 }
jchen10af713a22017-04-19 09:10:56 +08003001 // ESSL 3.10 section 4.4.4 page 58:
3002 // Any uniform or shader storage block declared without a binding qualifier is initially
3003 // assigned to block binding point zero.
3004 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003005 if (interfaceBlock.arraySize > 0)
3006 {
3007 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
3008 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003009 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3010 // of UniformBlock and ShaderStorageBlock.
3011 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003012 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003013 // Don't define this block at all if it's not active in the implementation.
3014 if (!mProgram->getUniformBlockSize(
3015 interfaceBlock.name + ArrayString(arrayElement),
3016 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
3017 {
3018 continue;
3019 }
jchen10af713a22017-04-19 09:10:56 +08003020 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003021
3022 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
3023 blockBinding + arrayElement);
3024 block.memberIndexes = blockIndexes;
jchen10baf5d942017-08-28 20:45:48 +08003025 MarkResourceStaticUse(&block, shaderType, interfaceBlock.staticUse);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003026
Jiajia Qin729b2c62017-08-14 09:36:11 +08003027 // Since all block elements in an array share the same active interface blocks, they
3028 // will all be active once any block member is used. So, since interfaceBlock.name[0]
3029 // was active, here we will add every block element in the array.
Qin Jiajia0350a642016-11-01 17:01:51 +08003030 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003031 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3032 {
3033 mState.mUniformBlocks.push_back(block);
3034 }
3035 else
3036 {
3037 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3038 mState.mShaderStorageBlocks.push_back(block);
3039 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003040 }
3041 }
3042 else
3043 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003044 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3045 // of UniformBlock and ShaderStorageBlock.
3046 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003047 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003048 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
3049 &blockSize))
3050 {
3051 return;
3052 }
jchen10af713a22017-04-19 09:10:56 +08003053 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003054
3055 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0,
3056 blockBinding);
3057 block.memberIndexes = blockIndexes;
jchen10baf5d942017-08-28 20:45:48 +08003058 MarkResourceStaticUse(&block, shaderType, interfaceBlock.staticUse);
Jamie Madill4a3c2342015-10-08 12:58:45 -04003059 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003060 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3061 {
3062 mState.mUniformBlocks.push_back(block);
3063 }
3064 else
3065 {
3066 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3067 mState.mShaderStorageBlocks.push_back(block);
3068 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003069 }
3070}
3071
Jamie Madille7d84322017-01-10 18:21:59 -05003072void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003073 GLsizei clampedCount,
3074 const GLint *v)
3075{
Jamie Madill81c2e252017-09-09 23:32:46 -04003076 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3077 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3078 std::vector<GLuint> *boundTextureUnits =
3079 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003080
Olli Etuahoc8538042017-09-27 11:20:15 +03003081 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.flattenedArrayOffset);
Jamie Madilld68248b2017-09-11 14:34:14 -04003082
3083 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003084 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003085}
3086
3087template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003088GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3089 GLsizei count,
3090 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003091 const T *v)
3092{
Jamie Madill134f93d2017-08-31 17:11:00 -04003093 if (count == 1)
3094 return 1;
3095
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003096 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003097
Corentin Wallez15ac5342016-11-03 17:06:39 -04003098 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3099 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03003100 unsigned int remainingElements =
3101 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003102 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003103 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003104
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003105 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003106 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003107 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003108 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003109
3110 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003111}
3112
3113template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003114GLsizei Program::clampMatrixUniformCount(GLint location,
3115 GLsizei count,
3116 GLboolean transpose,
3117 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003118{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003119 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3120
Jamie Madill62d31cb2015-09-11 13:25:51 -04003121 if (!transpose)
3122 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003123 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003124 }
3125
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003126 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003127
3128 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3129 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03003130 unsigned int remainingElements =
3131 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003132 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003133}
3134
Jamie Madill54164b02017-08-28 15:17:37 -04003135// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3136// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003137template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003138void Program::getUniformInternal(const Context *context,
3139 DestT *dataOut,
3140 GLint location,
3141 GLenum nativeType,
3142 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003143{
Jamie Madill54164b02017-08-28 15:17:37 -04003144 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003145 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003146 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003147 {
3148 GLint tempValue[16] = {0};
3149 mProgram->getUniformiv(context, location, tempValue);
3150 UniformStateQueryCastLoop<GLboolean>(
3151 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003152 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003153 }
3154 case GL_INT:
3155 {
3156 GLint tempValue[16] = {0};
3157 mProgram->getUniformiv(context, location, tempValue);
3158 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3159 components);
3160 break;
3161 }
3162 case GL_UNSIGNED_INT:
3163 {
3164 GLuint tempValue[16] = {0};
3165 mProgram->getUniformuiv(context, location, tempValue);
3166 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3167 components);
3168 break;
3169 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003170 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003171 {
3172 GLfloat tempValue[16] = {0};
3173 mProgram->getUniformfv(context, location, tempValue);
3174 UniformStateQueryCastLoop<GLfloat>(
3175 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003176 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003177 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003178 default:
3179 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003180 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003181 }
3182}
Jamie Madilla4595b82017-01-11 17:36:34 -05003183
3184bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3185{
3186 // Must be called after samplers are validated.
3187 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3188
3189 for (const auto &binding : mState.mSamplerBindings)
3190 {
3191 GLenum textureType = binding.textureType;
3192 for (const auto &unit : binding.boundTextureUnits)
3193 {
3194 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3195 if (programTextureID == textureID)
3196 {
3197 // TODO(jmadill): Check for appropriate overlap.
3198 return true;
3199 }
3200 }
3201 }
3202
3203 return false;
3204}
3205
Jamie Madilla2c74982016-12-12 11:20:42 -05003206} // namespace gl