blob: f36301e2a7507683d3f8a7cbfff36268d6f45c64 [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
jchen1058f67be2017-10-27 08:59:27 +08001662GLuint Program::getActiveAtomicCounterBufferCount() const
1663{
1664 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1665}
1666
Jiajia Qin729b2c62017-08-14 09:36:11 +08001667GLuint Program::getActiveShaderStorageBlockCount() const
1668{
1669 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1670}
1671
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1673{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001674 ASSERT(
1675 uniformBlockIndex <
1676 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677
Jiajia Qin729b2c62017-08-14 09:36:11 +08001678 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679
1680 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001681 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682 std::string string = uniformBlock.name;
1683
Jamie Madill62d31cb2015-09-11 13:25:51 -04001684 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001685 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001686 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687 }
jchen10fd7c3b52017-03-21 15:36:03 +08001688 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001689 }
1690}
1691
Geoff Lange1a27752015-10-05 13:16:04 -04001692GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001693{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001694 int maxLength = 0;
1695
1696 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001697 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001698 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001699 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1700 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001701 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702 if (!uniformBlock.name.empty())
1703 {
jchen10af713a22017-04-19 09:10:56 +08001704 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1705 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706 }
1707 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001708 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709
1710 return maxLength;
1711}
1712
Geoff Lange1a27752015-10-05 13:16:04 -04001713GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001714{
Olli Etuahoc8538042017-09-27 11:20:15 +03001715 std::vector<unsigned int> subscripts;
1716 std::string baseName = ParseResourceName(name, &subscripts);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717
Jamie Madill48ef11b2016-04-27 15:21:52 -04001718 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001719 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1720 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001721 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001722 if (uniformBlock.name == baseName)
1723 {
1724 const bool arrayElementZero =
Olli Etuahoc8538042017-09-27 11:20:15 +03001725 (subscripts.empty() && (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1726 const bool arrayElementMatches =
1727 (subscripts.size() == 1 && subscripts[0] == uniformBlock.arrayElement);
1728 if (arrayElementMatches || arrayElementZero)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001729 {
1730 return blockIndex;
1731 }
1732 }
1733 }
1734
1735 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001736}
1737
Jiajia Qin729b2c62017-08-14 09:36:11 +08001738const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001739{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001740 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1741 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001742}
1743
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001744void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1745{
jchen107a20b972017-06-13 14:25:26 +08001746 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001747 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001748 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001749}
1750
1751GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1752{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001753 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001754}
1755
Jiajia Qin729b2c62017-08-14 09:36:11 +08001756GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1757{
1758 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1759}
1760
Geoff Lang48dcae72014-02-05 16:28:24 -05001761void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1762{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001763 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001764 for (GLsizei i = 0; i < count; i++)
1765 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001766 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001767 }
1768
Jamie Madill48ef11b2016-04-27 15:21:52 -04001769 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001770}
1771
1772void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1773{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001774 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001775 {
jchen10a9042d32017-03-17 08:50:45 +08001776 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1777 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1778 std::string varName = var.nameWithArrayIndex();
1779 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001780 if (length)
1781 {
1782 *length = lastNameIdx;
1783 }
1784 if (size)
1785 {
jchen10a9042d32017-03-17 08:50:45 +08001786 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001787 }
1788 if (type)
1789 {
jchen10a9042d32017-03-17 08:50:45 +08001790 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001791 }
1792 if (name)
1793 {
jchen10a9042d32017-03-17 08:50:45 +08001794 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001795 name[lastNameIdx] = '\0';
1796 }
1797 }
1798}
1799
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001800GLsizei Program::getTransformFeedbackVaryingCount() const
1801{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001803 {
jchen10a9042d32017-03-17 08:50:45 +08001804 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001805 }
1806 else
1807 {
1808 return 0;
1809 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001810}
1811
1812GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1813{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001815 {
1816 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001817 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001818 {
jchen10a9042d32017-03-17 08:50:45 +08001819 maxSize =
1820 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001821 }
1822
1823 return maxSize;
1824 }
1825 else
1826 {
1827 return 0;
1828 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001829}
1830
1831GLenum Program::getTransformFeedbackBufferMode() const
1832{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001833 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834}
1835
Jamie Madillbd044ed2017-06-05 12:59:21 -04001836bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001838 Shader *vertexShader = mState.mAttachedVertexShader;
1839 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001840
Jamie Madillbd044ed2017-06-05 12:59:21 -04001841 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001842
Jiawei Shao3d404882017-10-16 13:30:48 +08001843 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
1844 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001845
Sami Väisänen46eaa942016-06-29 10:26:37 +03001846 std::map<GLuint, std::string> staticFragmentInputLocations;
1847
Jamie Madill4cff2472015-08-21 16:53:18 -04001848 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 bool matched = false;
1851
1852 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001853 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 {
1855 continue;
1856 }
1857
Jamie Madill4cff2472015-08-21 16:53:18 -04001858 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001860 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001862 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001863 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001864 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865 {
1866 return false;
1867 }
1868
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 matched = true;
1870 break;
1871 }
1872 }
1873
1874 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001875 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001876 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001877 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 return false;
1879 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001880
1881 // Check for aliased path rendering input bindings (if any).
1882 // If more than one binding refer statically to the same
1883 // location the link must fail.
1884
1885 if (!output.staticUse)
1886 continue;
1887
1888 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1889 if (inputBinding == -1)
1890 continue;
1891
1892 const auto it = staticFragmentInputLocations.find(inputBinding);
1893 if (it == std::end(staticFragmentInputLocations))
1894 {
1895 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1896 }
1897 else
1898 {
1899 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1900 << it->second;
1901 return false;
1902 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001903 }
1904
Jamie Madillbd044ed2017-06-05 12:59:21 -04001905 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001906 {
1907 return false;
1908 }
1909
Jamie Madillada9ecc2015-08-17 12:53:37 -04001910 // TODO(jmadill): verify no unmatched vertex varyings?
1911
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 return true;
1913}
1914
Jamie Madillbd044ed2017-06-05 12:59:21 -04001915bool Program::linkUniforms(const Context *context,
1916 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001917 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001918{
Olli Etuahob78707c2017-03-09 15:03:11 +00001919 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001920 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 {
1922 return false;
1923 }
1924
Olli Etuahob78707c2017-03-09 15:03:11 +00001925 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001926
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001927 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001928
jchen10eaef1e52017-06-13 10:44:11 +08001929 if (!linkAtomicCounterBuffers())
1930 {
1931 return false;
1932 }
1933
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001934 return true;
1935}
1936
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001937void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001938{
Jamie Madill982f6e02017-06-07 14:33:04 -04001939 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1940 unsigned int low = high;
1941
jchen10eaef1e52017-06-13 10:44:11 +08001942 for (auto counterIter = mState.mUniforms.rbegin();
1943 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1944 {
1945 --low;
1946 }
1947
1948 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1949
1950 high = low;
1951
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001952 for (auto imageIter = mState.mUniforms.rbegin();
1953 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1954 {
1955 --low;
1956 }
1957
1958 mState.mImageUniformRange = RangeUI(low, high);
1959
1960 // If uniform is a image type, insert it into the mImageBindings array.
1961 for (unsigned int imageIndex : mState.mImageUniformRange)
1962 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001963 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1964 // cannot load values into a uniform defined as an image. if declare without a
1965 // binding qualifier, any uniform image variable (include all elements of
1966 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001967 auto &imageUniform = mState.mUniforms[imageIndex];
1968 if (imageUniform.binding == -1)
1969 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001970 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001971 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001972 else
1973 {
1974 mState.mImageBindings.emplace_back(
1975 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1976 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001977 }
1978
1979 high = low;
1980
1981 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001982 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001983 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001984 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001985 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001986
1987 mState.mSamplerUniformRange = RangeUI(low, high);
1988
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001989 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001990 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001991 {
1992 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1993 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1994 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001995 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001996 }
1997}
1998
jchen10eaef1e52017-06-13 10:44:11 +08001999bool Program::linkAtomicCounterBuffers()
2000{
2001 for (unsigned int index : mState.mAtomicCounterUniformRange)
2002 {
2003 auto &uniform = mState.mUniforms[index];
2004 bool found = false;
2005 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2006 ++bufferIndex)
2007 {
2008 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2009 if (buffer.binding == uniform.binding)
2010 {
2011 buffer.memberIndexes.push_back(index);
2012 uniform.bufferIndex = bufferIndex;
2013 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002014 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002015 break;
2016 }
2017 }
2018 if (!found)
2019 {
2020 AtomicCounterBuffer atomicCounterBuffer;
2021 atomicCounterBuffer.binding = uniform.binding;
2022 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002023 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002024 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2025 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2026 }
2027 }
2028 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2029 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2030
2031 return true;
2032}
2033
Martin Radev4c4c8e72016-08-04 12:25:34 +03002034bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2035 const std::string &uniformName,
2036 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002037 const sh::InterfaceBlockField &fragmentUniform,
2038 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002040 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2041 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2042 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
2044 return false;
2045 }
2046
2047 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2048 {
Jamie Madillf6113162015-05-07 11:49:21 -04002049 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 return false;
2051 }
2052
2053 return true;
2054}
2055
Jamie Madilleb979bf2016-11-15 12:28:46 -05002056// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002057bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002059 const ContextState &data = context->getContextState();
2060 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002061
Geoff Lang7dd2e102014-11-10 15:19:26 -05002062 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002063 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002064 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002065
2066 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002067 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002068 {
Jamie Madillf6113162015-05-07 11:49:21 -04002069 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002070 return false;
2071 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002072
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002073 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002074
Jamie Madillc349ec02015-08-21 16:53:12 -04002075 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002076 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002077 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002078 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002079 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002080 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002081 attribute.location = bindingLocation;
2082 }
2083
2084 if (attribute.location != -1)
2085 {
2086 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002087 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088
Jamie Madill63805b42015-08-25 13:17:39 -04002089 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 {
Jamie Madillf6113162015-05-07 11:49:21 -04002091 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002092 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093
2094 return false;
2095 }
2096
Jamie Madill63805b42015-08-25 13:17:39 -04002097 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 {
Jamie Madill63805b42015-08-25 13:17:39 -04002099 const int regLocation = attribute.location + reg;
2100 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101
2102 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002103 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002104 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002106 // TODO(jmadill): fix aliasing on ES2
2107 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002109 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002110 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111 return false;
2112 }
2113 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002114 else
2115 {
Jamie Madill63805b42015-08-25 13:17:39 -04002116 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002117 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118
Jamie Madill63805b42015-08-25 13:17:39 -04002119 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 }
2121 }
2122 }
2123
2124 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002125 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002127 // Not set by glBindAttribLocation or by location layout qualifier
2128 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 {
Jamie Madill63805b42015-08-25 13:17:39 -04002130 int regs = VariableRegisterCount(attribute.type);
2131 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132
Jamie Madill63805b42015-08-25 13:17:39 -04002133 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 {
Jamie Madillf6113162015-05-07 11:49:21 -04002135 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002136 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 }
2138
Jamie Madillc349ec02015-08-21 16:53:12 -04002139 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 }
2141 }
2142
Jamie Madill48ef11b2016-04-27 15:21:52 -04002143 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 {
Jamie Madill63805b42015-08-25 13:17:39 -04002145 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002146 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002147
Jamie Madillbd159f02017-10-09 19:39:06 -04002148 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002150 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2151 mState.mActiveAttribLocationsMask.set(location);
2152 mState.mMaxActiveAttribLocation =
2153 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 }
2155 }
2156
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 return true;
2158}
2159
Martin Radev4c4c8e72016-08-04 12:25:34 +03002160bool Program::validateVertexAndFragmentInterfaceBlocks(
2161 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2162 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002163 InfoLog &infoLog,
2164 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002165{
2166 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002167 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2168 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002169
2170 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2171 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002172 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002173 }
2174
Jamie Madille473dee2015-08-18 14:49:01 -04002175 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002177 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2178 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 {
2180 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002181 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2182 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183 {
2184 return false;
2185 }
2186 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002187 // TODO(jiajia.qin@intel.com): Add
2188 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002189 }
2190 return true;
2191}
Jamie Madille473dee2015-08-18 14:49:01 -04002192
Jiajia Qin729b2c62017-08-14 09:36:11 +08002193bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002194{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002195 const auto &caps = context->getCaps();
2196
Martin Radev4c4c8e72016-08-04 12:25:34 +03002197 if (mState.mAttachedComputeShader)
2198 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002199 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002200 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002201
Jiajia Qin729b2c62017-08-14 09:36:11 +08002202 if (!validateInterfaceBlocksCount(
2203 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002204 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2205 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002206 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002207 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002209
2210 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2211 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2212 computeShaderStorageBlocks,
2213 "Compute shader shader storage block count exceeds "
2214 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2215 infoLog))
2216 {
2217 return false;
2218 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002219 return true;
2220 }
2221
Jamie Madillbd044ed2017-06-05 12:59:21 -04002222 Shader &vertexShader = *mState.mAttachedVertexShader;
2223 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002224
Jiajia Qin729b2c62017-08-14 09:36:11 +08002225 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2226 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002227
Jiajia Qin729b2c62017-08-14 09:36:11 +08002228 if (!validateInterfaceBlocksCount(
2229 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002230 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2231 {
2232 return false;
2233 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002234 if (!validateInterfaceBlocksCount(
2235 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002236 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2237 infoLog))
2238 {
2239
2240 return false;
2241 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002242
2243 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002244 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002245 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002246 {
2247 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248 }
Jamie Madille473dee2015-08-18 14:49:01 -04002249
Jiajia Qin729b2c62017-08-14 09:36:11 +08002250 if (context->getClientVersion() >= Version(3, 1))
2251 {
2252 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2253 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2254
2255 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2256 vertexShaderStorageBlocks,
2257 "Vertex shader shader storage block count exceeds "
2258 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2259 infoLog))
2260 {
2261 return false;
2262 }
2263 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2264 fragmentShaderStorageBlocks,
2265 "Fragment shader shader storage block count exceeds "
2266 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2267 infoLog))
2268 {
2269
2270 return false;
2271 }
2272
2273 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2274 fragmentShaderStorageBlocks, infoLog,
2275 webglCompatibility))
2276 {
2277 return false;
2278 }
2279 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280 return true;
2281}
2282
Jamie Madilla2c74982016-12-12 11:20:42 -05002283bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002284 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002285 const sh::InterfaceBlock &fragmentInterfaceBlock,
2286 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002287{
2288 const char* blockName = vertexInterfaceBlock.name.c_str();
2289 // validate blocks for the same member types
2290 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2291 {
Jamie Madillf6113162015-05-07 11:49:21 -04002292 infoLog << "Types for interface block '" << blockName
2293 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294 return false;
2295 }
2296 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2297 {
Jamie Madillf6113162015-05-07 11:49:21 -04002298 infoLog << "Array sizes differ for interface block '" << blockName
2299 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300 return false;
2301 }
jchen10af713a22017-04-19 09:10:56 +08002302 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2303 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2304 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002305 {
Jamie Madillf6113162015-05-07 11:49:21 -04002306 infoLog << "Layout qualifiers differ for interface block '" << blockName
2307 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002308 return false;
2309 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002310 const unsigned int numBlockMembers =
2311 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002312 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2313 {
2314 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2315 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2316 if (vertexMember.name != fragmentMember.name)
2317 {
Jamie Madillf6113162015-05-07 11:49:21 -04002318 infoLog << "Name mismatch for field " << blockMemberIndex
2319 << " of interface block '" << blockName
2320 << "': (in vertex: '" << vertexMember.name
2321 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 return false;
2323 }
2324 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002325 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2326 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002327 {
2328 return false;
2329 }
2330 }
2331 return true;
2332}
2333
2334bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2335 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2336{
2337 if (vertexVariable.type != fragmentVariable.type)
2338 {
Jamie Madillf6113162015-05-07 11:49:21 -04002339 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002340 return false;
2341 }
2342 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2343 {
Jamie Madillf6113162015-05-07 11:49:21 -04002344 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345 return false;
2346 }
2347 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2348 {
Jamie Madillf6113162015-05-07 11:49:21 -04002349 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 return false;
2351 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002352 if (vertexVariable.structName != fragmentVariable.structName)
2353 {
2354 infoLog << "Structure names for " << variableName
2355 << " differ between vertex and fragment shaders";
2356 return false;
2357 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358
2359 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2360 {
Jamie Madillf6113162015-05-07 11:49:21 -04002361 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002362 return false;
2363 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002364 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2366 {
2367 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2368 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2369
2370 if (vertexMember.name != fragmentMember.name)
2371 {
Jamie Madillf6113162015-05-07 11:49:21 -04002372 infoLog << "Name mismatch for field '" << memberIndex
2373 << "' of " << variableName
2374 << ": (in vertex: '" << vertexMember.name
2375 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002376 return false;
2377 }
2378
2379 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2380 vertexMember.name + "'";
2381
2382 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2383 {
2384 return false;
2385 }
2386 }
2387
2388 return true;
2389}
2390
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002391bool Program::linkValidateVaryings(InfoLog &infoLog,
2392 const std::string &varyingName,
2393 const sh::Varying &vertexVarying,
2394 const sh::Varying &fragmentVarying,
2395 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396{
2397 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2398 {
2399 return false;
2400 }
2401
Jamie Madille9cc4692015-02-19 16:00:13 -05002402 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002404 infoLog << "Interpolation types for " << varyingName
2405 << " differ between vertex and fragment shaders.";
2406 return false;
2407 }
2408
2409 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2410 {
2411 infoLog << "Invariance for " << varyingName
2412 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002413 return false;
2414 }
2415
2416 return true;
2417}
2418
Jamie Madillbd044ed2017-06-05 12:59:21 -04002419bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002420{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002421 Shader *vertexShader = mState.mAttachedVertexShader;
2422 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002423 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2424 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002425 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002426
2427 if (shaderVersion != 100)
2428 {
2429 // Only ESSL 1.0 has restrictions on matching input and output invariance
2430 return true;
2431 }
2432
2433 bool glPositionIsInvariant = false;
2434 bool glPointSizeIsInvariant = false;
2435 bool glFragCoordIsInvariant = false;
2436 bool glPointCoordIsInvariant = false;
2437
2438 for (const sh::Varying &varying : vertexVaryings)
2439 {
2440 if (!varying.isBuiltIn())
2441 {
2442 continue;
2443 }
2444 if (varying.name.compare("gl_Position") == 0)
2445 {
2446 glPositionIsInvariant = varying.isInvariant;
2447 }
2448 else if (varying.name.compare("gl_PointSize") == 0)
2449 {
2450 glPointSizeIsInvariant = varying.isInvariant;
2451 }
2452 }
2453
2454 for (const sh::Varying &varying : fragmentVaryings)
2455 {
2456 if (!varying.isBuiltIn())
2457 {
2458 continue;
2459 }
2460 if (varying.name.compare("gl_FragCoord") == 0)
2461 {
2462 glFragCoordIsInvariant = varying.isInvariant;
2463 }
2464 else if (varying.name.compare("gl_PointCoord") == 0)
2465 {
2466 glPointCoordIsInvariant = varying.isInvariant;
2467 }
2468 }
2469
2470 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2471 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2472 // Not requiring invariance to match is supported by:
2473 // dEQP, WebGL CTS, Nexus 5X GLES
2474 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2475 {
2476 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2477 "declared invariant.";
2478 return false;
2479 }
2480 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2481 {
2482 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2483 "declared invariant.";
2484 return false;
2485 }
2486
2487 return true;
2488}
2489
jchen10a9042d32017-03-17 08:50:45 +08002490bool Program::linkValidateTransformFeedback(const gl::Context *context,
2491 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002492 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002493 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494{
2495 size_t totalComponents = 0;
2496
Jamie Madillccdf74b2015-08-18 10:46:12 -04002497 std::set<std::string> uniqueNames;
2498
Jamie Madill48ef11b2016-04-27 15:21:52 -04002499 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002500 {
2501 bool found = false;
Olli Etuahoc8538042017-09-27 11:20:15 +03002502 std::vector<unsigned int> subscripts;
2503 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002504
Jamie Madill192745a2016-12-22 15:58:21 -05002505 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002506 {
Jamie Madill192745a2016-12-22 15:58:21 -05002507 const sh::Varying *varying = ref.second.get();
2508
jchen10a9042d32017-03-17 08:50:45 +08002509 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002510 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002511 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002512 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002513 infoLog << "Two transform feedback varyings specify the same output variable ("
2514 << tfVaryingName << ").";
2515 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002516 }
jchen10a9042d32017-03-17 08:50:45 +08002517 if (context->getClientVersion() >= Version(3, 1))
2518 {
2519 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2520 {
2521 infoLog
2522 << "Two transform feedback varyings include the same array element ("
2523 << tfVaryingName << ").";
2524 return false;
2525 }
2526 }
2527 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002528 {
2529 infoLog << "Capture of arrays is undefined and not supported.";
2530 return false;
2531 }
2532
jchen10a9042d32017-03-17 08:50:45 +08002533 uniqueNames.insert(tfVaryingName);
2534
Jamie Madillccdf74b2015-08-18 10:46:12 -04002535 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002536 size_t elementCount =
Olli Etuahoc8538042017-09-27 11:20:15 +03002537 ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1);
jchen10a9042d32017-03-17 08:50:45 +08002538 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002539 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002540 componentCount > caps.maxTransformFeedbackSeparateComponents)
2541 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002542 infoLog << "Transform feedback varying's " << varying->name << " components ("
2543 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002544 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002545 return false;
2546 }
2547
2548 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002549 found = true;
2550 break;
2551 }
2552 }
jchen10a9042d32017-03-17 08:50:45 +08002553 if (context->getClientVersion() < Version(3, 1) &&
2554 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002555 {
Geoff Lang1a683462015-09-29 15:09:59 -04002556 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002557 return false;
2558 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002559 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2560 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002561 ASSERT(found);
2562 }
2563
Jamie Madill48ef11b2016-04-27 15:21:52 -04002564 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002565 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002566 {
Jamie Madillf6113162015-05-07 11:49:21 -04002567 infoLog << "Transform feedback varying total components (" << totalComponents
2568 << ") exceed the maximum interleaved components ("
2569 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002570 return false;
2571 }
2572
2573 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002574}
2575
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002576bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2577{
2578 const std::vector<sh::Uniform> &vertexUniforms =
2579 mState.mAttachedVertexShader->getUniforms(context);
2580 const std::vector<sh::Uniform> &fragmentUniforms =
2581 mState.mAttachedFragmentShader->getUniforms(context);
2582 const std::vector<sh::Attribute> &attributes =
2583 mState.mAttachedVertexShader->getActiveAttributes(context);
2584 for (const auto &attrib : attributes)
2585 {
2586 for (const auto &uniform : vertexUniforms)
2587 {
2588 if (uniform.name == attrib.name)
2589 {
2590 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2591 return false;
2592 }
2593 }
2594 for (const auto &uniform : fragmentUniforms)
2595 {
2596 if (uniform.name == attrib.name)
2597 {
2598 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2599 return false;
2600 }
2601 }
2602 }
2603 return true;
2604}
2605
Jamie Madill192745a2016-12-22 15:58:21 -05002606void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002607{
2608 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002609 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002610 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002611 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002612 std::vector<unsigned int> subscripts;
2613 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002614 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002615 if (!subscripts.empty())
2616 {
2617 subscript = subscripts.back();
2618 }
Jamie Madill192745a2016-12-22 15:58:21 -05002619 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002620 {
Jamie Madill192745a2016-12-22 15:58:21 -05002621 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002622 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002623 {
jchen10a9042d32017-03-17 08:50:45 +08002624 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2625 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002626 break;
2627 }
2628 }
2629 }
2630}
2631
Jamie Madillbd044ed2017-06-05 12:59:21 -04002632Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002633{
Jamie Madill192745a2016-12-22 15:58:21 -05002634 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002635
Jiawei Shao3d404882017-10-16 13:30:48 +08002636 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002637 {
Jamie Madill192745a2016-12-22 15:58:21 -05002638 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002639 }
2640
Jiawei Shao3d404882017-10-16 13:30:48 +08002641 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002642 {
Jamie Madill192745a2016-12-22 15:58:21 -05002643 merged[varying.name].fragment = &varying;
2644 }
2645
2646 return merged;
2647}
2648
2649std::vector<PackedVarying> Program::getPackedVaryings(
2650 const Program::MergedVaryings &mergedVaryings) const
2651{
2652 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2653 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002654 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002655
2656 for (const auto &ref : mergedVaryings)
2657 {
2658 const sh::Varying *input = ref.second.vertex;
2659 const sh::Varying *output = ref.second.fragment;
2660
2661 // Only pack varyings that have a matched input or output, plus special builtins.
2662 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002663 {
Jamie Madill192745a2016-12-22 15:58:21 -05002664 // Will get the vertex shader interpolation by default.
2665 auto interpolation = ref.second.get()->interpolation;
2666
Olli Etuaho06a06f52017-07-12 12:22:15 +03002667 // Note that we lose the vertex shader static use information here. The data for the
2668 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002669 if (output->isStruct())
2670 {
2671 ASSERT(!output->isArray());
2672 for (const auto &field : output->fields)
2673 {
2674 ASSERT(!field.isStruct() && !field.isArray());
2675 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2676 }
2677 }
2678 else
2679 {
2680 packedVaryings.push_back(PackedVarying(*output, interpolation));
2681 }
2682 continue;
2683 }
2684
2685 // Keep Transform FB varyings in the merged list always.
2686 if (!input)
2687 {
2688 continue;
2689 }
2690
2691 for (const std::string &tfVarying : tfVaryings)
2692 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002693 std::vector<unsigned int> subscripts;
2694 std::string baseName = ParseResourceName(tfVarying, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002695 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002696 if (!subscripts.empty())
2697 {
2698 subscript = subscripts.back();
2699 }
jchen10a9042d32017-03-17 08:50:45 +08002700 if (uniqueFullNames.count(tfVarying) > 0)
2701 {
2702 continue;
2703 }
2704 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002705 {
2706 // Transform feedback for varying structs is underspecified.
2707 // See Khronos bug 9856.
2708 // TODO(jmadill): Figure out how to be spec-compliant here.
2709 if (!input->isStruct())
2710 {
2711 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2712 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002713 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2714 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002715 }
jchen10a9042d32017-03-17 08:50:45 +08002716 if (subscript == GL_INVALID_INDEX)
2717 {
2718 break;
2719 }
Jamie Madill192745a2016-12-22 15:58:21 -05002720 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002721 }
2722 }
2723
Jamie Madill192745a2016-12-22 15:58:21 -05002724 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2725
2726 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002727}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002728
Jamie Madillbd044ed2017-06-05 12:59:21 -04002729void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002730{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002731 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002732 ASSERT(fragmentShader != nullptr);
2733
Geoff Lange0cff192017-05-30 13:04:56 -04002734 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002735 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002736
2737 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002738 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002739 {
2740 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2741 outputVariable.name != "gl_FragData")
2742 {
2743 continue;
2744 }
2745
2746 unsigned int baseLocation =
2747 (outputVariable.location == -1 ? 0u
2748 : static_cast<unsigned int>(outputVariable.location));
2749 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2750 elementIndex++)
2751 {
2752 const unsigned int location = baseLocation + elementIndex;
2753 if (location >= mState.mOutputVariableTypes.size())
2754 {
2755 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2756 }
Corentin Walleze7557742017-06-01 13:09:57 -04002757 ASSERT(location < mState.mActiveOutputVariables.size());
2758 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002759 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2760 }
2761 }
2762
Jamie Madill80a6fc02015-08-21 16:53:16 -04002763 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002764 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002765 return;
2766
Jamie Madillbd044ed2017-06-05 12:59:21 -04002767 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002768 // TODO(jmadill): any caps validation here?
2769
jchen1015015f72017-03-16 13:54:21 +08002770 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002771 outputVariableIndex++)
2772 {
jchen1015015f72017-03-16 13:54:21 +08002773 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002774
2775 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2776 if (outputVariable.isBuiltIn())
2777 continue;
2778
2779 // Since multiple output locations must be specified, use 0 for non-specified locations.
2780 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2781
Jamie Madill80a6fc02015-08-21 16:53:16 -04002782 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2783 elementIndex++)
2784 {
2785 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002786 ASSERT(mState.mOutputLocations.count(location) == 0);
Olli Etuahoc8538042017-09-27 11:20:15 +03002787 if (outputVariable.isArray())
2788 {
2789 mState.mOutputLocations[location] =
2790 VariableLocation(elementIndex, outputVariableIndex);
2791 }
2792 else
2793 {
2794 VariableLocation locationInfo;
2795 locationInfo.index = outputVariableIndex;
2796 mState.mOutputLocations[location] = locationInfo;
2797 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002798 }
2799 }
2800}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002801
Olli Etuaho48fed632017-03-16 12:05:30 +00002802void Program::setUniformValuesFromBindingQualifiers()
2803{
Jamie Madill982f6e02017-06-07 14:33:04 -04002804 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002805 {
2806 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2807 if (samplerUniform.binding != -1)
2808 {
2809 GLint location = mState.getUniformLocation(samplerUniform.name);
2810 ASSERT(location != -1);
2811 std::vector<GLint> boundTextureUnits;
2812 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2813 ++elementIndex)
2814 {
2815 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2816 }
2817 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2818 boundTextureUnits.data());
2819 }
2820 }
2821}
2822
jchen10eaef1e52017-06-13 10:44:11 +08002823void Program::gatherAtomicCounterBuffers()
2824{
jchen10baf5d942017-08-28 20:45:48 +08002825 for (unsigned int index : mState.mAtomicCounterUniformRange)
2826 {
2827 auto &uniform = mState.mUniforms[index];
2828 uniform.blockInfo.offset = uniform.offset;
2829 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2830 uniform.blockInfo.matrixStride = 0;
2831 uniform.blockInfo.isRowMajorMatrix = false;
2832 }
2833
jchen10eaef1e52017-06-13 10:44:11 +08002834 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2835}
2836
Jiajia Qin729b2c62017-08-14 09:36:11 +08002837void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002838{
Jiajia Qin729b2c62017-08-14 09:36:11 +08002839 for (const sh::InterfaceBlock &computeBlock : computeBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002840 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002841
Jiajia Qin729b2c62017-08-14 09:36:11 +08002842 // Only 'packed' blocks are allowed to be considered inactive.
2843 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2844 continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002845
Jiajia Qin729b2c62017-08-14 09:36:11 +08002846 defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002847 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002848}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002849
Jiajia Qin729b2c62017-08-14 09:36:11 +08002850void Program::gatherVertexAndFragmentBlockInfo(
2851 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2852 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks)
2853{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002854 std::set<std::string> visitedList;
2855
Jiajia Qin729b2c62017-08-14 09:36:11 +08002856 for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002857 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002858 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002859 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2860 continue;
2861
Jiajia Qin729b2c62017-08-14 09:36:11 +08002862 defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002863 visitedList.insert(vertexBlock.name);
2864 }
2865
Jiajia Qin729b2c62017-08-14 09:36:11 +08002866 for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002867 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002868 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002869 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2870 continue;
2871
2872 if (visitedList.count(fragmentBlock.name) > 0)
2873 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002874 if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002875 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002876 for (InterfaceBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002877 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002878 if (block.name == fragmentBlock.name)
2879 {
2880 block.fragmentStaticUse = fragmentBlock.staticUse;
2881 }
2882 }
2883 }
2884 else
2885 {
2886 ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2887 for (InterfaceBlock &block : mState.mShaderStorageBlocks)
2888 {
2889 if (block.name == fragmentBlock.name)
2890 {
2891 block.fragmentStaticUse = fragmentBlock.staticUse;
2892 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002893 }
2894 }
2895
2896 continue;
2897 }
2898
Jiajia Qin729b2c62017-08-14 09:36:11 +08002899 defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002900 visitedList.insert(fragmentBlock.name);
2901 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002902}
2903
2904void Program::gatherInterfaceBlockInfo(const Context *context)
2905{
2906 ASSERT(mState.mUniformBlocks.empty());
2907 ASSERT(mState.mShaderStorageBlocks.empty());
2908
2909 if (mState.mAttachedComputeShader)
2910 {
2911 Shader *computeShader = mState.getAttachedComputeShader();
2912
2913 gatherComputeBlockInfo(computeShader->getUniformBlocks(context));
2914 gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context));
2915 return;
2916 }
2917
2918 Shader *vertexShader = mState.getAttachedVertexShader();
2919 Shader *fragmentShader = mState.getAttachedFragmentShader();
2920
2921 gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context),
2922 fragmentShader->getUniformBlocks(context));
2923 if (context->getClientVersion() >= Version(3, 1))
2924 {
2925 gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context),
2926 fragmentShader->getShaderStorageBlocks(context));
2927 }
2928
jchen10af713a22017-04-19 09:10:56 +08002929 // Set initial bindings from shader.
2930 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2931 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002932 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002933 bindUniformBlock(blockIndex, uniformBlock.binding);
2934 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002935}
2936
Jamie Madill4a3c2342015-10-08 12:58:45 -04002937template <typename VarT>
2938void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2939 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002940 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002941 int blockIndex)
2942{
2943 for (const VarT &field : fields)
2944 {
2945 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2946
Olli Etuaho855d9642017-05-17 14:05:06 +03002947 const std::string &fullMappedName =
2948 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2949
Jamie Madill4a3c2342015-10-08 12:58:45 -04002950 if (field.isStruct())
2951 {
2952 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2953 {
2954 const std::string uniformElementName =
2955 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002956 const std::string uniformElementMappedName =
2957 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2958 defineUniformBlockMembers(field.fields, uniformElementName,
2959 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002960 }
2961 }
2962 else
2963 {
2964 // If getBlockMemberInfo returns false, the uniform is optimized out.
2965 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002966 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002967 {
2968 continue;
2969 }
2970
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002971 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002972 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002973 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002974
2975 // Since block uniforms have no location, we don't need to store them in the uniform
2976 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002977 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002978 }
2979 }
2980}
2981
Jiajia Qin729b2c62017-08-14 09:36:11 +08002982void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002983{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002984 size_t blockSize = 0;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002985 std::vector<unsigned int> blockIndexes;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002986
Jiajia Qin729b2c62017-08-14 09:36:11 +08002987 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002988 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002989 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
2990 // Track the first and last uniform index to determine the range of active uniforms in the
2991 // block.
2992 size_t firstBlockUniformIndex = mState.mUniforms.size();
2993 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2994 interfaceBlock.fieldMappedPrefix(), blockIndex);
2995 size_t lastBlockUniformIndex = mState.mUniforms.size();
2996
2997 for (size_t blockUniformIndex = firstBlockUniformIndex;
2998 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2999 {
3000 blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
3001 }
3002 }
3003 else
3004 {
3005 // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index.
3006 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003007 }
jchen10af713a22017-04-19 09:10:56 +08003008 // ESSL 3.10 section 4.4.4 page 58:
3009 // Any uniform or shader storage block declared without a binding qualifier is initially
3010 // assigned to block binding point zero.
3011 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003012 if (interfaceBlock.arraySize > 0)
3013 {
3014 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
3015 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003016 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3017 // of UniformBlock and ShaderStorageBlock.
3018 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003019 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003020 // Don't define this block at all if it's not active in the implementation.
3021 if (!mProgram->getUniformBlockSize(
3022 interfaceBlock.name + ArrayString(arrayElement),
3023 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
3024 {
3025 continue;
3026 }
jchen10af713a22017-04-19 09:10:56 +08003027 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003028
3029 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
3030 blockBinding + arrayElement);
3031 block.memberIndexes = blockIndexes;
jchen104ef25032017-11-01 09:36:51 +08003032 block.setStaticUse(shaderType, interfaceBlock.staticUse);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003033
Jiajia Qin729b2c62017-08-14 09:36:11 +08003034 // Since all block elements in an array share the same active interface blocks, they
3035 // will all be active once any block member is used. So, since interfaceBlock.name[0]
3036 // was active, here we will add every block element in the array.
Qin Jiajia0350a642016-11-01 17:01:51 +08003037 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003038 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3039 {
3040 mState.mUniformBlocks.push_back(block);
3041 }
3042 else
3043 {
3044 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3045 mState.mShaderStorageBlocks.push_back(block);
3046 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003047 }
3048 }
3049 else
3050 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003051 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3052 // of UniformBlock and ShaderStorageBlock.
3053 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003054 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003055 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
3056 &blockSize))
3057 {
3058 return;
3059 }
jchen10af713a22017-04-19 09:10:56 +08003060 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003061
3062 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0,
3063 blockBinding);
3064 block.memberIndexes = blockIndexes;
jchen104ef25032017-11-01 09:36:51 +08003065 block.setStaticUse(shaderType, interfaceBlock.staticUse);
Jamie Madill4a3c2342015-10-08 12:58:45 -04003066 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003067 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3068 {
3069 mState.mUniformBlocks.push_back(block);
3070 }
3071 else
3072 {
3073 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3074 mState.mShaderStorageBlocks.push_back(block);
3075 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003076 }
3077}
3078
Jamie Madille7d84322017-01-10 18:21:59 -05003079void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003080 GLsizei clampedCount,
3081 const GLint *v)
3082{
Jamie Madill81c2e252017-09-09 23:32:46 -04003083 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3084 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3085 std::vector<GLuint> *boundTextureUnits =
3086 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003087
Olli Etuahoc8538042017-09-27 11:20:15 +03003088 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.flattenedArrayOffset);
Jamie Madilld68248b2017-09-11 14:34:14 -04003089
3090 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003091 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003092}
3093
3094template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003095GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3096 GLsizei count,
3097 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003098 const T *v)
3099{
Jamie Madill134f93d2017-08-31 17:11:00 -04003100 if (count == 1)
3101 return 1;
3102
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003103 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003104
Corentin Wallez15ac5342016-11-03 17:06:39 -04003105 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3106 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03003107 unsigned int remainingElements =
3108 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003109 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003110 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003111
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003112 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003113 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003114 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003115 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003116
3117 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003118}
3119
3120template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003121GLsizei Program::clampMatrixUniformCount(GLint location,
3122 GLsizei count,
3123 GLboolean transpose,
3124 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003125{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003126 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3127
Jamie Madill62d31cb2015-09-11 13:25:51 -04003128 if (!transpose)
3129 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003130 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003131 }
3132
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003133 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003134
3135 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3136 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03003137 unsigned int remainingElements =
3138 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003139 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003140}
3141
Jamie Madill54164b02017-08-28 15:17:37 -04003142// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3143// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003144template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003145void Program::getUniformInternal(const Context *context,
3146 DestT *dataOut,
3147 GLint location,
3148 GLenum nativeType,
3149 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003150{
Jamie Madill54164b02017-08-28 15:17:37 -04003151 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003152 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003153 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003154 {
3155 GLint tempValue[16] = {0};
3156 mProgram->getUniformiv(context, location, tempValue);
3157 UniformStateQueryCastLoop<GLboolean>(
3158 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003159 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003160 }
3161 case GL_INT:
3162 {
3163 GLint tempValue[16] = {0};
3164 mProgram->getUniformiv(context, location, tempValue);
3165 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3166 components);
3167 break;
3168 }
3169 case GL_UNSIGNED_INT:
3170 {
3171 GLuint tempValue[16] = {0};
3172 mProgram->getUniformuiv(context, location, tempValue);
3173 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3174 components);
3175 break;
3176 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003177 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003178 {
3179 GLfloat tempValue[16] = {0};
3180 mProgram->getUniformfv(context, location, tempValue);
3181 UniformStateQueryCastLoop<GLfloat>(
3182 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003183 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003184 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003185 default:
3186 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003187 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003188 }
3189}
Jamie Madilla4595b82017-01-11 17:36:34 -05003190
3191bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3192{
3193 // Must be called after samplers are validated.
3194 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3195
3196 for (const auto &binding : mState.mSamplerBindings)
3197 {
3198 GLenum textureType = binding.textureType;
3199 for (const auto &unit : binding.boundTextureUnits)
3200 {
3201 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3202 if (programTextureID == textureID)
3203 {
3204 // TODO(jmadill): Check for appropriate overlap.
3205 return true;
3206 }
3207 }
3208 }
3209
3210 return false;
3211}
3212
Jamie Madilla2c74982016-12-12 11:20:42 -05003213} // namespace gl