blob: 3b0d9047c1ae7752c41be0b33fc494647927f347 [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 Madille7d84322017-01-10 18:21:59 -0500344 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800345 mImageUniformRange(0, 0),
346 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300347 mBinaryRetrieveableHint(false),
348 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300350 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400351}
352
Jamie Madill48ef11b2016-04-27 15:21:52 -0400353ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400354{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500355 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400356}
357
Jamie Madill48ef11b2016-04-27 15:21:52 -0400358const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500359{
360 return mLabel;
361}
362
Jamie Madill48ef11b2016-04-27 15:21:52 -0400363GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400364{
Olli Etuahoc8538042017-09-27 11:20:15 +0300365 std::vector<unsigned int> subscripts;
366 std::string baseName = ParseResourceName(name, &subscripts);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400367
368 for (size_t location = 0; location < mUniformLocations.size(); ++location)
369 {
370 const VariableLocation &uniformLocation = mUniformLocations[location];
Jamie Madillfb997ec2017-09-20 15:44:27 -0400371 if (!uniformLocation.used())
Geoff Langd8605522016-04-13 10:19:12 -0400372 {
373 continue;
374 }
375
376 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400377
378 if (uniform.name == baseName)
379 {
Geoff Langd8605522016-04-13 10:19:12 -0400380 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400381 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300382 if (uniformLocation.arrayIndices == subscripts ||
383 (uniformLocation.areAllArrayIndicesZero() && subscripts.empty()))
Geoff Langd8605522016-04-13 10:19:12 -0400384 {
385 return static_cast<GLint>(location);
386 }
387 }
388 else
389 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300390 if (subscripts.empty())
Geoff Langd8605522016-04-13 10:19:12 -0400391 {
392 return static_cast<GLint>(location);
393 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400394 }
395 }
396 }
397
398 return -1;
399}
400
Jamie Madille7d84322017-01-10 18:21:59 -0500401GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400402{
jchen1015015f72017-03-16 13:54:21 +0800403 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400404}
405
Jamie Madille7d84322017-01-10 18:21:59 -0500406GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
407{
408 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
409 return mUniformLocations[location].index;
410}
411
412Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
413{
414 GLuint index = getUniformIndexFromLocation(location);
415 if (!isSamplerUniformIndex(index))
416 {
417 return Optional<GLuint>::Invalid();
418 }
419
420 return getSamplerIndexFromUniformIndex(index);
421}
422
423bool ProgramState::isSamplerUniformIndex(GLuint index) const
424{
Jamie Madill982f6e02017-06-07 14:33:04 -0400425 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500426}
427
428GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
429{
430 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400431 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500432}
433
Jamie Madill34ca4f52017-06-13 11:49:39 -0400434GLuint ProgramState::getAttributeLocation(const std::string &name) const
435{
436 for (const sh::Attribute &attribute : mAttributes)
437 {
438 if (attribute.name == name)
439 {
440 return attribute.location;
441 }
442 }
443
444 return static_cast<GLuint>(-1);
445}
446
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500447Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400448 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400449 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500450 mLinked(false),
451 mDeleteStatus(false),
452 mRefCount(0),
453 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500454 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500455{
456 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000457
Geoff Lang7dd2e102014-11-10 15:19:26 -0500458 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459}
460
461Program::~Program()
462{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400463 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464}
465
Jamie Madill4928b7c2017-06-20 12:57:39 -0400466void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500467{
468 if (mState.mAttachedVertexShader != nullptr)
469 {
470 mState.mAttachedVertexShader->release(context);
471 mState.mAttachedVertexShader = nullptr;
472 }
473
474 if (mState.mAttachedFragmentShader != nullptr)
475 {
476 mState.mAttachedFragmentShader->release(context);
477 mState.mAttachedFragmentShader = nullptr;
478 }
479
480 if (mState.mAttachedComputeShader != nullptr)
481 {
482 mState.mAttachedComputeShader->release(context);
483 mState.mAttachedComputeShader = nullptr;
484 }
485
Jamie Madillc564c072017-06-01 12:45:42 -0400486 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400487
488 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
489 !mState.mAttachedComputeShader);
490 SafeDelete(mProgram);
491
492 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500493}
494
Geoff Lang70d0f492015-12-10 17:45:46 -0500495void Program::setLabel(const std::string &label)
496{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400497 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500498}
499
500const std::string &Program::getLabel() const
501{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400502 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500503}
504
Jamie Madillef300b12016-10-07 15:12:09 -0400505void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300507 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300509 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 {
Jamie Madillef300b12016-10-07 15:12:09 -0400511 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300512 mState.mAttachedVertexShader = shader;
513 mState.mAttachedVertexShader->addRef();
514 break;
515 }
516 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517 {
Jamie Madillef300b12016-10-07 15:12:09 -0400518 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300519 mState.mAttachedFragmentShader = shader;
520 mState.mAttachedFragmentShader->addRef();
521 break;
522 }
523 case GL_COMPUTE_SHADER:
524 {
Jamie Madillef300b12016-10-07 15:12:09 -0400525 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300526 mState.mAttachedComputeShader = shader;
527 mState.mAttachedComputeShader->addRef();
528 break;
529 }
530 default:
531 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533}
534
Jamie Madillc1d770e2017-04-13 17:31:24 -0400535void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000536{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300537 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300539 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400541 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500542 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300543 mState.mAttachedVertexShader = nullptr;
544 break;
545 }
546 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400548 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500549 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300550 mState.mAttachedFragmentShader = nullptr;
551 break;
552 }
553 case GL_COMPUTE_SHADER:
554 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400555 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500556 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300557 mState.mAttachedComputeShader = nullptr;
558 break;
559 }
560 default:
561 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563}
564
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000565int Program::getAttachedShadersCount() const
566{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300567 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
568 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000569}
570
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571void Program::bindAttributeLocation(GLuint index, const char *name)
572{
Geoff Langd8605522016-04-13 10:19:12 -0400573 mAttributeBindings.bindLocation(index, name);
574}
575
576void Program::bindUniformLocation(GLuint index, const char *name)
577{
578 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800579 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580}
581
Sami Väisänen46eaa942016-06-29 10:26:37 +0300582void Program::bindFragmentInputLocation(GLint index, const char *name)
583{
584 mFragmentInputBindings.bindLocation(index, name);
585}
586
Jamie Madillbd044ed2017-06-05 12:59:21 -0400587BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300588{
589 BindingInfo ret;
590 ret.type = GL_NONE;
591 ret.valid = false;
592
Jamie Madillbd044ed2017-06-05 12:59:21 -0400593 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300594 ASSERT(fragmentShader);
595
596 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400597 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300598
599 for (const auto &binding : mFragmentInputBindings)
600 {
601 if (binding.second != static_cast<GLuint>(index))
602 continue;
603
604 ret.valid = true;
605
606 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400607 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300608
609 for (const auto &in : inputs)
610 {
611 if (in.name == originalName)
612 {
613 if (in.isArray())
614 {
615 // The client wants to bind either "name" or "name[0]".
616 // GL ES 3.1 spec refers to active array names with language such as:
617 // "if the string identifies the base name of an active array, where the
618 // string would exactly match the name of the variable if the suffix "[0]"
619 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400620 if (arrayIndex == GL_INVALID_INDEX)
621 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300622
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400623 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300624 }
625 else
626 {
627 ret.name = in.mappedName;
628 }
629 ret.type = in.type;
630 return ret;
631 }
632 }
633 }
634
635 return ret;
636}
637
Jamie Madillbd044ed2017-06-05 12:59:21 -0400638void Program::pathFragmentInputGen(const Context *context,
639 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300640 GLenum genMode,
641 GLint components,
642 const GLfloat *coeffs)
643{
644 // If the location is -1 then the command is silently ignored
645 if (index == -1)
646 return;
647
Jamie Madillbd044ed2017-06-05 12:59:21 -0400648 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300649
650 // If the input doesn't exist then then the command is silently ignored
651 // This could happen through optimization for example, the shader translator
652 // decides that a variable is not actually being used and optimizes it away.
653 if (binding.name.empty())
654 return;
655
656 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
657}
658
Martin Radev4c4c8e72016-08-04 12:25:34 +0300659// The attached shaders are checked for linking errors by matching up their variables.
660// Uniform, input and output variables get collected.
661// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500662Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000663{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500664 const auto &data = context->getContextState();
665
Jamie Madill6c58b062017-08-01 13:44:25 -0400666 auto *platform = ANGLEPlatformCurrent();
667 double startTime = platform->currentTime(platform);
668
Jamie Madill6c1f6712017-02-14 19:08:04 -0500669 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000670
Jamie Madill32447362017-06-28 14:53:52 -0400671 ProgramHash programHash;
672 auto *cache = context->getMemoryProgramCache();
673 if (cache)
674 {
675 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400676 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400677 }
678
679 if (mLinked)
680 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400681 double delta = platform->currentTime(platform) - startTime;
682 int us = static_cast<int>(delta * 1000000.0);
683 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400684 return NoError();
685 }
686
687 // Cache load failed, fall through to normal linking.
688 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000689 mInfoLog.reset();
690
Martin Radev4c4c8e72016-08-04 12:25:34 +0300691 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500692
Jamie Madill192745a2016-12-22 15:58:21 -0500693 auto vertexShader = mState.mAttachedVertexShader;
694 auto fragmentShader = mState.mAttachedFragmentShader;
695 auto computeShader = mState.mAttachedComputeShader;
696
697 bool isComputeShaderAttached = (computeShader != nullptr);
698 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300699 // Check whether we both have a compute and non-compute shaders attached.
700 // If there are of both types attached, then linking should fail.
701 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
702 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500703 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300704 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
705 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400706 }
707
Jamie Madill192745a2016-12-22 15:58:21 -0500708 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500709 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400710 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300711 {
712 mInfoLog << "Attached compute shader is not compiled.";
713 return NoError();
714 }
Jamie Madill192745a2016-12-22 15:58:21 -0500715 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300716
Jamie Madillbd044ed2017-06-05 12:59:21 -0400717 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300718
719 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
720 // If the work group size is not specified, a link time error should occur.
721 if (!mState.mComputeShaderLocalSize.isDeclared())
722 {
723 mInfoLog << "Work group size is not specified.";
724 return NoError();
725 }
726
Jamie Madillbd044ed2017-06-05 12:59:21 -0400727 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300728 {
729 return NoError();
730 }
731
Jiajia Qin729b2c62017-08-14 09:36:11 +0800732 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733 {
734 return NoError();
735 }
736
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500737 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400738 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500739 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300740 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500741 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300742 }
743 }
744 else
745 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400746 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300747 {
748 return NoError();
749 }
Jamie Madill192745a2016-12-22 15:58:21 -0500750 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300751
Jamie Madillbd044ed2017-06-05 12:59:21 -0400752 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753 {
754 return NoError();
755 }
Jamie Madill192745a2016-12-22 15:58:21 -0500756 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300757
Jamie Madillbd044ed2017-06-05 12:59:21 -0400758 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300759 {
760 mInfoLog << "Fragment shader version does not match vertex shader version.";
761 return NoError();
762 }
763
Jamie Madillbd044ed2017-06-05 12:59:21 -0400764 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300765 {
766 return NoError();
767 }
768
Jamie Madillbd044ed2017-06-05 12:59:21 -0400769 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300770 {
771 return NoError();
772 }
773
Jamie Madillbd044ed2017-06-05 12:59:21 -0400774 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300775 {
776 return NoError();
777 }
778
Jiajia Qin729b2c62017-08-14 09:36:11 +0800779 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300780 {
781 return NoError();
782 }
783
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400784 if (!linkValidateGlobalNames(context, mInfoLog))
785 {
786 return NoError();
787 }
788
Jamie Madillbd044ed2017-06-05 12:59:21 -0400789 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300790
Martin Radev7cf61662017-07-26 17:10:53 +0300791 mState.mNumViews = vertexShader->getNumViews(context);
792
Jamie Madillbd044ed2017-06-05 12:59:21 -0400793 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300794
Jamie Madill192745a2016-12-22 15:58:21 -0500795 // Validate we can pack the varyings.
796 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
797
798 // Map the varyings to the register file
799 // In WebGL, we use a slightly different handling for packing variables.
800 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
801 : PackMode::ANGLE_RELAXED;
802 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
803 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
804 mState.getTransformFeedbackVaryingNames()))
805 {
806 return NoError();
807 }
808
Olli Etuaho39e78122017-08-29 14:34:22 +0300809 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
810 {
811 return NoError();
812 }
813
Jamie Madillc564c072017-06-01 12:45:42 -0400814 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500815 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300816 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500817 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300818 }
819
820 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500821 }
822
jchen10eaef1e52017-06-13 10:44:11 +0800823 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400824 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400825
jchen10eaef1e52017-06-13 10:44:11 +0800826 setUniformValuesFromBindingQualifiers();
827
Jamie Madill54164b02017-08-28 15:17:37 -0400828 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400829 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400830
Jamie Madill32447362017-06-28 14:53:52 -0400831 // Save to the program cache.
832 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
833 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
834 {
835 cache->putProgram(programHash, context, this);
836 }
837
Jamie Madill6c58b062017-08-01 13:44:25 -0400838 double delta = platform->currentTime(platform) - startTime;
839 int us = static_cast<int>(delta * 1000000.0);
840 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
841
Martin Radev4c4c8e72016-08-04 12:25:34 +0300842 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000843}
844
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000845// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500846void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400848 mState.mAttributes.clear();
849 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800850 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400851 mState.mUniforms.clear();
852 mState.mUniformLocations.clear();
853 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800854 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800855 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400856 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800857 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400858 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400859 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300860 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500861 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800862 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300863 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500864
Geoff Lang7dd2e102014-11-10 15:19:26 -0500865 mValidated = false;
866
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000867 mLinked = false;
868}
869
Geoff Lange1a27752015-10-05 13:16:04 -0400870bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000871{
872 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
Jamie Madilla2c74982016-12-12 11:20:42 -0500875Error Program::loadBinary(const Context *context,
876 GLenum binaryFormat,
877 const void *binary,
878 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000879{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500880 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000881
Geoff Lang7dd2e102014-11-10 15:19:26 -0500882#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800883 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500884#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400885 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
886 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000887 {
Jamie Madillf6113162015-05-07 11:49:21 -0400888 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800889 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500890 }
891
Jamie Madill4f86d052017-06-05 12:59:26 -0400892 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
893 ANGLE_TRY_RESULT(
894 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400895
896 // Currently we require the full shader text to compute the program hash.
897 // TODO(jmadill): Store the binary in the internal program cache.
898
Jamie Madillb0a838b2016-11-13 20:02:12 -0500899 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500900#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901}
902
Jamie Madilla2c74982016-12-12 11:20:42 -0500903Error Program::saveBinary(const Context *context,
904 GLenum *binaryFormat,
905 void *binary,
906 GLsizei bufSize,
907 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500908{
909 if (binaryFormat)
910 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400911 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500912 }
913
Jamie Madill4f86d052017-06-05 12:59:26 -0400914 angle::MemoryBuffer memoryBuf;
915 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500916
Jamie Madill4f86d052017-06-05 12:59:26 -0400917 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
918 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500919
920 if (streamLength > bufSize)
921 {
922 if (length)
923 {
924 *length = 0;
925 }
926
927 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
928 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
929 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500930 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500931 }
932
933 if (binary)
934 {
935 char *ptr = reinterpret_cast<char*>(binary);
936
Jamie Madill48ef11b2016-04-27 15:21:52 -0400937 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500938 ptr += streamLength;
939
940 ASSERT(ptr - streamLength == binary);
941 }
942
943 if (length)
944 {
945 *length = streamLength;
946 }
947
He Yunchaoacd18982017-01-04 10:46:42 +0800948 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500949}
950
Jamie Madillffe00c02017-06-27 16:26:55 -0400951GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500952{
953 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400954 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500955 if (error.isError())
956 {
957 return 0;
958 }
959
960 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000961}
962
Geoff Langc5629752015-12-07 16:29:04 -0500963void Program::setBinaryRetrievableHint(bool retrievable)
964{
965 // TODO(jmadill) : replace with dirty bits
966 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400967 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500968}
969
970bool Program::getBinaryRetrievableHint() const
971{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400972 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500973}
974
Yunchao He61afff12017-03-14 15:34:03 +0800975void Program::setSeparable(bool separable)
976{
977 // TODO(yunchao) : replace with dirty bits
978 if (mState.mSeparable != separable)
979 {
980 mProgram->setSeparable(separable);
981 mState.mSeparable = separable;
982 }
983}
984
985bool Program::isSeparable() const
986{
987 return mState.mSeparable;
988}
989
Jamie Madill6c1f6712017-02-14 19:08:04 -0500990void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000991{
992 mRefCount--;
993
994 if (mRefCount == 0 && mDeleteStatus)
995 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500996 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000997 }
998}
999
1000void Program::addRef()
1001{
1002 mRefCount++;
1003}
1004
1005unsigned int Program::getRefCount() const
1006{
1007 return mRefCount;
1008}
1009
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001010int Program::getInfoLogLength() const
1011{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001012 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001013}
1014
Geoff Lange1a27752015-10-05 13:16:04 -04001015void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001016{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001017 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001018}
1019
Geoff Lange1a27752015-10-05 13:16:04 -04001020void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001021{
1022 int total = 0;
1023
Martin Radev4c4c8e72016-08-04 12:25:34 +03001024 if (mState.mAttachedComputeShader)
1025 {
1026 if (total < maxCount)
1027 {
1028 shaders[total] = mState.mAttachedComputeShader->getHandle();
1029 total++;
1030 }
1031 }
1032
Jamie Madill48ef11b2016-04-27 15:21:52 -04001033 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001034 {
1035 if (total < maxCount)
1036 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001037 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001038 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001039 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001040 }
1041
Jamie Madill48ef11b2016-04-27 15:21:52 -04001042 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001043 {
1044 if (total < maxCount)
1045 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001046 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001047 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001048 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001049 }
1050
1051 if (count)
1052 {
1053 *count = total;
1054 }
1055}
1056
Geoff Lange1a27752015-10-05 13:16:04 -04001057GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001058{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001059 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001060}
1061
Jamie Madill63805b42015-08-25 13:17:39 -04001062bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001063{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001064 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1065 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001066}
1067
jchen10fd7c3b52017-03-21 15:36:03 +08001068void Program::getActiveAttribute(GLuint index,
1069 GLsizei bufsize,
1070 GLsizei *length,
1071 GLint *size,
1072 GLenum *type,
1073 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001074{
Jamie Madillc349ec02015-08-21 16:53:12 -04001075 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001076 {
1077 if (bufsize > 0)
1078 {
1079 name[0] = '\0';
1080 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001082 if (length)
1083 {
1084 *length = 0;
1085 }
1086
1087 *type = GL_NONE;
1088 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001089 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001090 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001091
jchen1036e120e2017-03-14 14:53:58 +08001092 ASSERT(index < mState.mAttributes.size());
1093 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001094
1095 if (bufsize > 0)
1096 {
jchen10fd7c3b52017-03-21 15:36:03 +08001097 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001098 }
1099
1100 // Always a single 'type' instance
1101 *size = 1;
1102 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001103}
1104
Geoff Lange1a27752015-10-05 13:16:04 -04001105GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001106{
Jamie Madillc349ec02015-08-21 16:53:12 -04001107 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001108 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001109 return 0;
1110 }
1111
jchen1036e120e2017-03-14 14:53:58 +08001112 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001113}
1114
Geoff Lange1a27752015-10-05 13:16:04 -04001115GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116{
Jamie Madillc349ec02015-08-21 16:53:12 -04001117 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001118 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001119 return 0;
1120 }
1121
1122 size_t maxLength = 0;
1123
Jamie Madill48ef11b2016-04-27 15:21:52 -04001124 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001125 {
jchen1036e120e2017-03-14 14:53:58 +08001126 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001127 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128
Jamie Madillc349ec02015-08-21 16:53:12 -04001129 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130}
1131
jchen1015015f72017-03-16 13:54:21 +08001132GLuint Program::getInputResourceIndex(const GLchar *name) const
1133{
1134 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1135 {
1136 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1137 if (attribute.name == name)
1138 {
1139 return attributeIndex;
1140 }
1141 }
1142 return GL_INVALID_INDEX;
1143}
1144
1145GLuint Program::getOutputResourceIndex(const GLchar *name) const
1146{
1147 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1148}
1149
jchen10fd7c3b52017-03-21 15:36:03 +08001150size_t Program::getOutputResourceCount() const
1151{
1152 return (mLinked ? mState.mOutputVariables.size() : 0);
1153}
1154
jchen10baf5d942017-08-28 20:45:48 +08001155template <typename T>
1156void Program::getResourceName(GLuint index,
1157 const std::vector<T> &resources,
1158 GLsizei bufSize,
1159 GLsizei *length,
1160 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001161{
1162 if (length)
1163 {
1164 *length = 0;
1165 }
1166
1167 if (!mLinked)
1168 {
1169 if (bufSize > 0)
1170 {
1171 name[0] = '\0';
1172 }
1173 return;
1174 }
jchen10baf5d942017-08-28 20:45:48 +08001175 ASSERT(index < resources.size());
1176 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001177
1178 if (bufSize > 0)
1179 {
jchen10baf5d942017-08-28 20:45:48 +08001180 std::string nameWithArray = (resource.isArray() ? resource.name + "[0]" : resource.name);
jchen10fd7c3b52017-03-21 15:36:03 +08001181
1182 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1183 }
1184}
1185
jchen10baf5d942017-08-28 20:45:48 +08001186void Program::getInputResourceName(GLuint index,
1187 GLsizei bufSize,
1188 GLsizei *length,
1189 GLchar *name) const
1190{
1191 getResourceName(index, mState.mAttributes, bufSize, length, name);
1192}
1193
1194void Program::getOutputResourceName(GLuint index,
1195 GLsizei bufSize,
1196 GLsizei *length,
1197 GLchar *name) const
1198{
1199 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1200}
1201
1202void Program::getUniformResourceName(GLuint index,
1203 GLsizei bufSize,
1204 GLsizei *length,
1205 GLchar *name) const
1206{
1207 getResourceName(index, mState.mUniforms, bufSize, length, name);
1208}
1209
jchen10880683b2017-04-12 16:21:55 +08001210const sh::Attribute &Program::getInputResource(GLuint index) const
1211{
1212 ASSERT(index < mState.mAttributes.size());
1213 return mState.mAttributes[index];
1214}
1215
1216const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1217{
1218 ASSERT(index < mState.mOutputVariables.size());
1219 return mState.mOutputVariables[index];
1220}
1221
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222GLint Program::getFragDataLocation(const std::string &name) const
1223{
1224 std::string baseName(name);
1225 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001226 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001227 {
Jamie Madillfb997ec2017-09-20 15:44:27 -04001228 const VariableLocation &locationInfo = outputPair.second;
1229 const sh::OutputVariable &outputVariable = mState.mOutputVariables[locationInfo.index];
Olli Etuahoc8538042017-09-27 11:20:15 +03001230 ASSERT(locationInfo.arrayIndices.size() <= 1);
Jamie Madillfb997ec2017-09-20 15:44:27 -04001231 if (outputVariable.name == baseName &&
Olli Etuahoc8538042017-09-27 11:20:15 +03001232 (arrayIndex == GL_INVALID_INDEX || (!locationInfo.arrayIndices.empty() &&
1233 arrayIndex == locationInfo.arrayIndices.back())))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001235 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001236 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001237 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001239}
1240
Geoff Lange1a27752015-10-05 13:16:04 -04001241void Program::getActiveUniform(GLuint index,
1242 GLsizei bufsize,
1243 GLsizei *length,
1244 GLint *size,
1245 GLenum *type,
1246 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001247{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001248 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001249 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001250 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001251 ASSERT(index < mState.mUniforms.size());
1252 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253
1254 if (bufsize > 0)
1255 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001256 std::string string = uniform.name;
1257 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 {
1259 string += "[0]";
1260 }
jchen10fd7c3b52017-03-21 15:36:03 +08001261 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262 }
1263
Jamie Madill62d31cb2015-09-11 13:25:51 -04001264 *size = uniform.elementCount();
1265 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001266 }
1267 else
1268 {
1269 if (bufsize > 0)
1270 {
1271 name[0] = '\0';
1272 }
1273
1274 if (length)
1275 {
1276 *length = 0;
1277 }
1278
1279 *size = 0;
1280 *type = GL_NONE;
1281 }
1282}
1283
Geoff Lange1a27752015-10-05 13:16:04 -04001284GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001285{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001286 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001287 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001288 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289 }
1290 else
1291 {
1292 return 0;
1293 }
1294}
1295
Geoff Lange1a27752015-10-05 13:16:04 -04001296GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001297{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001298 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001299
1300 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001301 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001302 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001303 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001304 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001306 size_t length = uniform.name.length() + 1u;
1307 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308 {
1309 length += 3; // Counting in "[0]".
1310 }
1311 maxLength = std::max(length, maxLength);
1312 }
1313 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001314 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001315
Jamie Madill62d31cb2015-09-11 13:25:51 -04001316 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001317}
1318
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319bool Program::isValidUniformLocation(GLint location) const
1320{
Jamie Madille2e406c2016-06-02 13:04:10 -04001321 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001322 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001323 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001324}
1325
Jamie Madill62d31cb2015-09-11 13:25:51 -04001326const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001327{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001328 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001329 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330}
1331
Jamie Madillac4e9c32017-01-13 14:07:12 -05001332const VariableLocation &Program::getUniformLocation(GLint location) const
1333{
1334 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1335 return mState.mUniformLocations[location];
1336}
1337
1338const std::vector<VariableLocation> &Program::getUniformLocations() const
1339{
1340 return mState.mUniformLocations;
1341}
1342
1343const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1344{
1345 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1346 return mState.mUniforms[index];
1347}
1348
Jamie Madill62d31cb2015-09-11 13:25:51 -04001349GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001351 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001352}
1353
Jamie Madill62d31cb2015-09-11 13:25:51 -04001354GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355{
Jamie Madille7d84322017-01-10 18:21:59 -05001356 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357}
1358
1359void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1360{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001361 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1362 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001363 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364}
1365
1366void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1367{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001368 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1369 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001370 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371}
1372
1373void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1374{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001375 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1376 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001377 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378}
1379
1380void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1381{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001382 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1383 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001384 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385}
1386
Jamie Madill81c2e252017-09-09 23:32:46 -04001387Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001389 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1390 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1391
Jamie Madill81c2e252017-09-09 23:32:46 -04001392 mProgram->setUniform1iv(location, clampedCount, v);
1393
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001394 if (mState.isSamplerUniformIndex(locationInfo.index))
1395 {
1396 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001397 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001398 }
1399
Jamie Madill81c2e252017-09-09 23:32:46 -04001400 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401}
1402
1403void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1404{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001405 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1406 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001407 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408}
1409
1410void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1411{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001412 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1413 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001414 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
1417void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1418{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001419 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1420 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001421 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422}
1423
1424void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1425{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001426 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1427 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001428 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429}
1430
1431void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1432{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001433 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1434 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001435 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436}
1437
1438void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1439{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001440 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1441 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001442 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443}
1444
1445void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1446{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001447 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1448 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001449 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450}
1451
1452void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1453{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001454 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001455 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456}
1457
1458void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1459{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001460 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001461 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462}
1463
1464void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1465{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001466 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001467 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468}
1469
1470void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1471{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001472 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001473 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
1476void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1477{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001478 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001479 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
1482void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1483{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001484 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001485 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486}
1487
1488void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1489{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001490 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001491 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492}
1493
1494void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1495{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001496 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001497 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498}
1499
1500void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1501{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001502 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001503 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
Jamie Madill54164b02017-08-28 15:17:37 -04001506void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001507{
Jamie Madill54164b02017-08-28 15:17:37 -04001508 const auto &uniformLocation = mState.getUniformLocations()[location];
1509 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1510
1511 GLenum nativeType = gl::VariableComponentType(uniform.type);
1512 if (nativeType == GL_FLOAT)
1513 {
1514 mProgram->getUniformfv(context, location, v);
1515 }
1516 else
1517 {
1518 getUniformInternal(context, v, location, nativeType,
1519 gl::VariableComponentCount(uniform.type));
1520 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521}
1522
Jamie Madill54164b02017-08-28 15:17:37 -04001523void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524{
Jamie Madill54164b02017-08-28 15:17:37 -04001525 const auto &uniformLocation = mState.getUniformLocations()[location];
1526 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1527
1528 GLenum nativeType = gl::VariableComponentType(uniform.type);
1529 if (nativeType == GL_INT || nativeType == GL_BOOL)
1530 {
1531 mProgram->getUniformiv(context, location, v);
1532 }
1533 else
1534 {
1535 getUniformInternal(context, v, location, nativeType,
1536 gl::VariableComponentCount(uniform.type));
1537 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001538}
1539
Jamie Madill54164b02017-08-28 15:17:37 -04001540void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001541{
Jamie Madill54164b02017-08-28 15:17:37 -04001542 const auto &uniformLocation = mState.getUniformLocations()[location];
1543 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1544
1545 GLenum nativeType = gl::VariableComponentType(uniform.type);
1546 if (nativeType == GL_UNSIGNED_INT)
1547 {
1548 mProgram->getUniformuiv(context, location, v);
1549 }
1550 else
1551 {
1552 getUniformInternal(context, v, location, nativeType,
1553 gl::VariableComponentCount(uniform.type));
1554 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555}
1556
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001557void Program::flagForDeletion()
1558{
1559 mDeleteStatus = true;
1560}
1561
1562bool Program::isFlaggedForDeletion() const
1563{
1564 return mDeleteStatus;
1565}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001566
Brandon Jones43a53e22014-08-28 16:23:22 -07001567void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001568{
1569 mInfoLog.reset();
1570
Geoff Lang7dd2e102014-11-10 15:19:26 -05001571 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001572 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001573 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001574 }
1575 else
1576 {
Jamie Madillf6113162015-05-07 11:49:21 -04001577 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001578 }
1579}
1580
Geoff Lang7dd2e102014-11-10 15:19:26 -05001581bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1582{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001583 // Skip cache if we're using an infolog, so we get the full error.
1584 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1585 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1586 {
1587 return mCachedValidateSamplersResult.value();
1588 }
1589
1590 if (mTextureUnitTypesCache.empty())
1591 {
1592 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1593 }
1594 else
1595 {
1596 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1597 }
1598
1599 // if any two active samplers in a program are of different types, but refer to the same
1600 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1601 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001602 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001603 {
Jamie Madill54164b02017-08-28 15:17:37 -04001604 if (samplerBinding.unreferenced)
1605 continue;
1606
Jamie Madille7d84322017-01-10 18:21:59 -05001607 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001608
Jamie Madille7d84322017-01-10 18:21:59 -05001609 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001610 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001611 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1612 {
1613 if (infoLog)
1614 {
1615 (*infoLog) << "Sampler uniform (" << textureUnit
1616 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1617 << caps.maxCombinedTextureImageUnits << ")";
1618 }
1619
1620 mCachedValidateSamplersResult = false;
1621 return false;
1622 }
1623
1624 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1625 {
1626 if (textureType != mTextureUnitTypesCache[textureUnit])
1627 {
1628 if (infoLog)
1629 {
1630 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1631 "image unit ("
1632 << textureUnit << ").";
1633 }
1634
1635 mCachedValidateSamplersResult = false;
1636 return false;
1637 }
1638 }
1639 else
1640 {
1641 mTextureUnitTypesCache[textureUnit] = textureType;
1642 }
1643 }
1644 }
1645
1646 mCachedValidateSamplersResult = true;
1647 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648}
1649
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001650bool Program::isValidated() const
1651{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652 return mValidated;
1653}
1654
Geoff Lange1a27752015-10-05 13:16:04 -04001655GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001657 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658}
1659
Jiajia Qin729b2c62017-08-14 09:36:11 +08001660GLuint Program::getActiveShaderStorageBlockCount() const
1661{
1662 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1663}
1664
Geoff Lang7dd2e102014-11-10 15:19:26 -05001665void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1666{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001667 ASSERT(
1668 uniformBlockIndex <
1669 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670
Jiajia Qin729b2c62017-08-14 09:36:11 +08001671 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672
1673 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001674 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675 std::string string = uniformBlock.name;
1676
Jamie Madill62d31cb2015-09-11 13:25:51 -04001677 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680 }
jchen10fd7c3b52017-03-21 15:36:03 +08001681 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001682 }
1683}
1684
Geoff Lange1a27752015-10-05 13:16:04 -04001685GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001686{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687 int maxLength = 0;
1688
1689 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001690 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001691 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001692 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1693 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001694 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695 if (!uniformBlock.name.empty())
1696 {
jchen10af713a22017-04-19 09:10:56 +08001697 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1698 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001699 }
1700 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001701 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702
1703 return maxLength;
1704}
1705
Geoff Lange1a27752015-10-05 13:16:04 -04001706GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001707{
Olli Etuahoc8538042017-09-27 11:20:15 +03001708 std::vector<unsigned int> subscripts;
1709 std::string baseName = ParseResourceName(name, &subscripts);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001710
Jamie Madill48ef11b2016-04-27 15:21:52 -04001711 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001712 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1713 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001714 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001715 if (uniformBlock.name == baseName)
1716 {
1717 const bool arrayElementZero =
Olli Etuahoc8538042017-09-27 11:20:15 +03001718 (subscripts.empty() && (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1719 const bool arrayElementMatches =
1720 (subscripts.size() == 1 && subscripts[0] == uniformBlock.arrayElement);
1721 if (arrayElementMatches || arrayElementZero)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001722 {
1723 return blockIndex;
1724 }
1725 }
1726 }
1727
1728 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001729}
1730
Jiajia Qin729b2c62017-08-14 09:36:11 +08001731const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001732{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001733 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1734 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001735}
1736
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001737void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1738{
jchen107a20b972017-06-13 14:25:26 +08001739 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001740 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001741 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001742}
1743
1744GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1745{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001746 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001747}
1748
Jiajia Qin729b2c62017-08-14 09:36:11 +08001749GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1750{
1751 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1752}
1753
Geoff Lang48dcae72014-02-05 16:28:24 -05001754void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1755{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001756 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001757 for (GLsizei i = 0; i < count; i++)
1758 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001759 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001760 }
1761
Jamie Madill48ef11b2016-04-27 15:21:52 -04001762 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001763}
1764
1765void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1766{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001767 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001768 {
jchen10a9042d32017-03-17 08:50:45 +08001769 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1770 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1771 std::string varName = var.nameWithArrayIndex();
1772 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001773 if (length)
1774 {
1775 *length = lastNameIdx;
1776 }
1777 if (size)
1778 {
jchen10a9042d32017-03-17 08:50:45 +08001779 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001780 }
1781 if (type)
1782 {
jchen10a9042d32017-03-17 08:50:45 +08001783 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001784 }
1785 if (name)
1786 {
jchen10a9042d32017-03-17 08:50:45 +08001787 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001788 name[lastNameIdx] = '\0';
1789 }
1790 }
1791}
1792
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001793GLsizei Program::getTransformFeedbackVaryingCount() const
1794{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001796 {
jchen10a9042d32017-03-17 08:50:45 +08001797 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001798 }
1799 else
1800 {
1801 return 0;
1802 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001803}
1804
1805GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1806{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001808 {
1809 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001810 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001811 {
jchen10a9042d32017-03-17 08:50:45 +08001812 maxSize =
1813 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001814 }
1815
1816 return maxSize;
1817 }
1818 else
1819 {
1820 return 0;
1821 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001822}
1823
1824GLenum Program::getTransformFeedbackBufferMode() const
1825{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001826 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827}
1828
Jamie Madillbd044ed2017-06-05 12:59:21 -04001829bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001831 Shader *vertexShader = mState.mAttachedVertexShader;
1832 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001833
Jamie Madillbd044ed2017-06-05 12:59:21 -04001834 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001835
Jamie Madillbd044ed2017-06-05 12:59:21 -04001836 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1837 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838
Sami Väisänen46eaa942016-06-29 10:26:37 +03001839 std::map<GLuint, std::string> staticFragmentInputLocations;
1840
Jamie Madill4cff2472015-08-21 16:53:18 -04001841 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001843 bool matched = false;
1844
1845 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001846 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847 {
1848 continue;
1849 }
1850
Jamie Madill4cff2472015-08-21 16:53:18 -04001851 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001853 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001855 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001856 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001857 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001858 {
1859 return false;
1860 }
1861
Geoff Lang7dd2e102014-11-10 15:19:26 -05001862 matched = true;
1863 break;
1864 }
1865 }
1866
1867 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001868 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001870 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 return false;
1872 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001873
1874 // Check for aliased path rendering input bindings (if any).
1875 // If more than one binding refer statically to the same
1876 // location the link must fail.
1877
1878 if (!output.staticUse)
1879 continue;
1880
1881 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1882 if (inputBinding == -1)
1883 continue;
1884
1885 const auto it = staticFragmentInputLocations.find(inputBinding);
1886 if (it == std::end(staticFragmentInputLocations))
1887 {
1888 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1889 }
1890 else
1891 {
1892 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1893 << it->second;
1894 return false;
1895 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896 }
1897
Jamie Madillbd044ed2017-06-05 12:59:21 -04001898 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001899 {
1900 return false;
1901 }
1902
Jamie Madillada9ecc2015-08-17 12:53:37 -04001903 // TODO(jmadill): verify no unmatched vertex varyings?
1904
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905 return true;
1906}
1907
Jamie Madillbd044ed2017-06-05 12:59:21 -04001908bool Program::linkUniforms(const Context *context,
1909 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001910 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001911{
Olli Etuahob78707c2017-03-09 15:03:11 +00001912 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001913 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001914 {
1915 return false;
1916 }
1917
Olli Etuahob78707c2017-03-09 15:03:11 +00001918 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001919
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001920 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001921
jchen10eaef1e52017-06-13 10:44:11 +08001922 if (!linkAtomicCounterBuffers())
1923 {
1924 return false;
1925 }
1926
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001927 return true;
1928}
1929
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001930void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001931{
Jamie Madill982f6e02017-06-07 14:33:04 -04001932 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1933 unsigned int low = high;
1934
jchen10eaef1e52017-06-13 10:44:11 +08001935 for (auto counterIter = mState.mUniforms.rbegin();
1936 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1937 {
1938 --low;
1939 }
1940
1941 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1942
1943 high = low;
1944
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001945 for (auto imageIter = mState.mUniforms.rbegin();
1946 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1947 {
1948 --low;
1949 }
1950
1951 mState.mImageUniformRange = RangeUI(low, high);
1952
1953 // If uniform is a image type, insert it into the mImageBindings array.
1954 for (unsigned int imageIndex : mState.mImageUniformRange)
1955 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001956 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1957 // cannot load values into a uniform defined as an image. if declare without a
1958 // binding qualifier, any uniform image variable (include all elements of
1959 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001960 auto &imageUniform = mState.mUniforms[imageIndex];
1961 if (imageUniform.binding == -1)
1962 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001963 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001964 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001965 else
1966 {
1967 mState.mImageBindings.emplace_back(
1968 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1969 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001970 }
1971
1972 high = low;
1973
1974 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001975 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001976 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001977 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001978 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001979
1980 mState.mSamplerUniformRange = RangeUI(low, high);
1981
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001982 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001983 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001984 {
1985 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1986 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1987 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001988 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001989 }
1990}
1991
jchen10eaef1e52017-06-13 10:44:11 +08001992bool Program::linkAtomicCounterBuffers()
1993{
1994 for (unsigned int index : mState.mAtomicCounterUniformRange)
1995 {
1996 auto &uniform = mState.mUniforms[index];
1997 bool found = false;
1998 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1999 ++bufferIndex)
2000 {
2001 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2002 if (buffer.binding == uniform.binding)
2003 {
2004 buffer.memberIndexes.push_back(index);
2005 uniform.bufferIndex = bufferIndex;
2006 found = true;
2007 break;
2008 }
2009 }
2010 if (!found)
2011 {
2012 AtomicCounterBuffer atomicCounterBuffer;
2013 atomicCounterBuffer.binding = uniform.binding;
2014 atomicCounterBuffer.memberIndexes.push_back(index);
2015 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2016 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2017 }
2018 }
2019 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2020 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2021
2022 return true;
2023}
2024
Martin Radev4c4c8e72016-08-04 12:25:34 +03002025bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2026 const std::string &uniformName,
2027 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002028 const sh::InterfaceBlockField &fragmentUniform,
2029 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002031 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2032 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2033 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034 {
2035 return false;
2036 }
2037
2038 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2039 {
Jamie Madillf6113162015-05-07 11:49:21 -04002040 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041 return false;
2042 }
2043
2044 return true;
2045}
2046
Jamie Madilleb979bf2016-11-15 12:28:46 -05002047// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002048bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002050 const ContextState &data = context->getContextState();
2051 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002052
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002054 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002055 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002056
2057 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002058 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002059 {
Jamie Madillf6113162015-05-07 11:49:21 -04002060 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002061 return false;
2062 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002064 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002065
Jamie Madillc349ec02015-08-21 16:53:12 -04002066 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002067 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002068 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002069 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002071 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002072 attribute.location = bindingLocation;
2073 }
2074
2075 if (attribute.location != -1)
2076 {
2077 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002078 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079
Jamie Madill63805b42015-08-25 13:17:39 -04002080 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 {
Jamie Madillf6113162015-05-07 11:49:21 -04002082 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002083 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084
2085 return false;
2086 }
2087
Jamie Madill63805b42015-08-25 13:17:39 -04002088 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 {
Jamie Madill63805b42015-08-25 13:17:39 -04002090 const int regLocation = attribute.location + reg;
2091 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092
2093 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002094 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002095 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002097 // TODO(jmadill): fix aliasing on ES2
2098 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002100 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002101 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102 return false;
2103 }
2104 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002105 else
2106 {
Jamie Madill63805b42015-08-25 13:17:39 -04002107 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002108 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109
Jamie Madill63805b42015-08-25 13:17:39 -04002110 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111 }
2112 }
2113 }
2114
2115 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002116 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 // Not set by glBindAttribLocation or by location layout qualifier
2119 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 {
Jamie Madill63805b42015-08-25 13:17:39 -04002121 int regs = VariableRegisterCount(attribute.type);
2122 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123
Jamie Madill63805b42015-08-25 13:17:39 -04002124 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 {
Jamie Madillf6113162015-05-07 11:49:21 -04002126 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002127 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002128 }
2129
Jamie Madillc349ec02015-08-21 16:53:12 -04002130 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 }
2132 }
2133
Jamie Madill48ef11b2016-04-27 15:21:52 -04002134 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 {
Jamie Madill63805b42015-08-25 13:17:39 -04002136 ASSERT(attribute.location != -1);
2137 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002138
Jamie Madill63805b42015-08-25 13:17:39 -04002139 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002141 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142 }
2143 }
2144
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 return true;
2146}
2147
Martin Radev4c4c8e72016-08-04 12:25:34 +03002148bool Program::validateVertexAndFragmentInterfaceBlocks(
2149 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2150 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002151 InfoLog &infoLog,
2152 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002153{
2154 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002155 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2156 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002157
2158 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2159 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002160 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002161 }
2162
Jamie Madille473dee2015-08-18 14:49:01 -04002163 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002165 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2166 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 {
2168 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002169 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2170 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171 {
2172 return false;
2173 }
2174 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002175 // TODO(jiajia.qin@intel.com): Add
2176 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002177 }
2178 return true;
2179}
Jamie Madille473dee2015-08-18 14:49:01 -04002180
Jiajia Qin729b2c62017-08-14 09:36:11 +08002181bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002182{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002183 const auto &caps = context->getCaps();
2184
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185 if (mState.mAttachedComputeShader)
2186 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002187 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002188 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002189
Jiajia Qin729b2c62017-08-14 09:36:11 +08002190 if (!validateInterfaceBlocksCount(
2191 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002192 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2193 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002194 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002195 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002196 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002197
2198 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2199 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2200 computeShaderStorageBlocks,
2201 "Compute shader shader storage block count exceeds "
2202 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2203 infoLog))
2204 {
2205 return false;
2206 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002207 return true;
2208 }
2209
Jamie Madillbd044ed2017-06-05 12:59:21 -04002210 Shader &vertexShader = *mState.mAttachedVertexShader;
2211 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002212
Jiajia Qin729b2c62017-08-14 09:36:11 +08002213 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2214 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002215
Jiajia Qin729b2c62017-08-14 09:36:11 +08002216 if (!validateInterfaceBlocksCount(
2217 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002218 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2219 {
2220 return false;
2221 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002222 if (!validateInterfaceBlocksCount(
2223 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002224 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2225 infoLog))
2226 {
2227
2228 return false;
2229 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002230
2231 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002232 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002233 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002234 {
2235 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 }
Jamie Madille473dee2015-08-18 14:49:01 -04002237
Jiajia Qin729b2c62017-08-14 09:36:11 +08002238 if (context->getClientVersion() >= Version(3, 1))
2239 {
2240 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2241 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2242
2243 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2244 vertexShaderStorageBlocks,
2245 "Vertex shader shader storage block count exceeds "
2246 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2247 infoLog))
2248 {
2249 return false;
2250 }
2251 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2252 fragmentShaderStorageBlocks,
2253 "Fragment shader shader storage block count exceeds "
2254 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2255 infoLog))
2256 {
2257
2258 return false;
2259 }
2260
2261 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2262 fragmentShaderStorageBlocks, infoLog,
2263 webglCompatibility))
2264 {
2265 return false;
2266 }
2267 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 return true;
2269}
2270
Jamie Madilla2c74982016-12-12 11:20:42 -05002271bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002272 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002273 const sh::InterfaceBlock &fragmentInterfaceBlock,
2274 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002275{
2276 const char* blockName = vertexInterfaceBlock.name.c_str();
2277 // validate blocks for the same member types
2278 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2279 {
Jamie Madillf6113162015-05-07 11:49:21 -04002280 infoLog << "Types for interface block '" << blockName
2281 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002282 return false;
2283 }
2284 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2285 {
Jamie Madillf6113162015-05-07 11:49:21 -04002286 infoLog << "Array sizes differ for interface block '" << blockName
2287 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002288 return false;
2289 }
jchen10af713a22017-04-19 09:10:56 +08002290 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2291 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2292 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293 {
Jamie Madillf6113162015-05-07 11:49:21 -04002294 infoLog << "Layout qualifiers differ for interface block '" << blockName
2295 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296 return false;
2297 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002298 const unsigned int numBlockMembers =
2299 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2301 {
2302 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2303 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2304 if (vertexMember.name != fragmentMember.name)
2305 {
Jamie Madillf6113162015-05-07 11:49:21 -04002306 infoLog << "Name mismatch for field " << blockMemberIndex
2307 << " of interface block '" << blockName
2308 << "': (in vertex: '" << vertexMember.name
2309 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002310 return false;
2311 }
2312 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002313 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2314 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315 {
2316 return false;
2317 }
2318 }
2319 return true;
2320}
2321
2322bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2323 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2324{
2325 if (vertexVariable.type != fragmentVariable.type)
2326 {
Jamie Madillf6113162015-05-07 11:49:21 -04002327 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002328 return false;
2329 }
2330 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2331 {
Jamie Madillf6113162015-05-07 11:49:21 -04002332 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002333 return false;
2334 }
2335 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2336 {
Jamie Madillf6113162015-05-07 11:49:21 -04002337 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002338 return false;
2339 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002340 if (vertexVariable.structName != fragmentVariable.structName)
2341 {
2342 infoLog << "Structure names for " << variableName
2343 << " differ between vertex and fragment shaders";
2344 return false;
2345 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002346
2347 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2348 {
Jamie Madillf6113162015-05-07 11:49:21 -04002349 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 return false;
2351 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002352 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002353 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2354 {
2355 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2356 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2357
2358 if (vertexMember.name != fragmentMember.name)
2359 {
Jamie Madillf6113162015-05-07 11:49:21 -04002360 infoLog << "Name mismatch for field '" << memberIndex
2361 << "' of " << variableName
2362 << ": (in vertex: '" << vertexMember.name
2363 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 return false;
2365 }
2366
2367 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2368 vertexMember.name + "'";
2369
2370 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2371 {
2372 return false;
2373 }
2374 }
2375
2376 return true;
2377}
2378
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002379bool Program::linkValidateVaryings(InfoLog &infoLog,
2380 const std::string &varyingName,
2381 const sh::Varying &vertexVarying,
2382 const sh::Varying &fragmentVarying,
2383 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002384{
2385 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2386 {
2387 return false;
2388 }
2389
Jamie Madille9cc4692015-02-19 16:00:13 -05002390 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002392 infoLog << "Interpolation types for " << varyingName
2393 << " differ between vertex and fragment shaders.";
2394 return false;
2395 }
2396
2397 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2398 {
2399 infoLog << "Invariance for " << varyingName
2400 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002401 return false;
2402 }
2403
2404 return true;
2405}
2406
Jamie Madillbd044ed2017-06-05 12:59:21 -04002407bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002408{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002409 Shader *vertexShader = mState.mAttachedVertexShader;
2410 Shader *fragmentShader = mState.mAttachedFragmentShader;
2411 const auto &vertexVaryings = vertexShader->getVaryings(context);
2412 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2413 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002414
2415 if (shaderVersion != 100)
2416 {
2417 // Only ESSL 1.0 has restrictions on matching input and output invariance
2418 return true;
2419 }
2420
2421 bool glPositionIsInvariant = false;
2422 bool glPointSizeIsInvariant = false;
2423 bool glFragCoordIsInvariant = false;
2424 bool glPointCoordIsInvariant = false;
2425
2426 for (const sh::Varying &varying : vertexVaryings)
2427 {
2428 if (!varying.isBuiltIn())
2429 {
2430 continue;
2431 }
2432 if (varying.name.compare("gl_Position") == 0)
2433 {
2434 glPositionIsInvariant = varying.isInvariant;
2435 }
2436 else if (varying.name.compare("gl_PointSize") == 0)
2437 {
2438 glPointSizeIsInvariant = varying.isInvariant;
2439 }
2440 }
2441
2442 for (const sh::Varying &varying : fragmentVaryings)
2443 {
2444 if (!varying.isBuiltIn())
2445 {
2446 continue;
2447 }
2448 if (varying.name.compare("gl_FragCoord") == 0)
2449 {
2450 glFragCoordIsInvariant = varying.isInvariant;
2451 }
2452 else if (varying.name.compare("gl_PointCoord") == 0)
2453 {
2454 glPointCoordIsInvariant = varying.isInvariant;
2455 }
2456 }
2457
2458 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2459 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2460 // Not requiring invariance to match is supported by:
2461 // dEQP, WebGL CTS, Nexus 5X GLES
2462 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2463 {
2464 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2465 "declared invariant.";
2466 return false;
2467 }
2468 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2469 {
2470 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2471 "declared invariant.";
2472 return false;
2473 }
2474
2475 return true;
2476}
2477
jchen10a9042d32017-03-17 08:50:45 +08002478bool Program::linkValidateTransformFeedback(const gl::Context *context,
2479 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002480 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002481 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002482{
2483 size_t totalComponents = 0;
2484
Jamie Madillccdf74b2015-08-18 10:46:12 -04002485 std::set<std::string> uniqueNames;
2486
Jamie Madill48ef11b2016-04-27 15:21:52 -04002487 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002488 {
2489 bool found = false;
Olli Etuahoc8538042017-09-27 11:20:15 +03002490 std::vector<unsigned int> subscripts;
2491 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002492
Jamie Madill192745a2016-12-22 15:58:21 -05002493 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494 {
Jamie Madill192745a2016-12-22 15:58:21 -05002495 const sh::Varying *varying = ref.second.get();
2496
jchen10a9042d32017-03-17 08:50:45 +08002497 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002498 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002499 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002500 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002501 infoLog << "Two transform feedback varyings specify the same output variable ("
2502 << tfVaryingName << ").";
2503 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002504 }
jchen10a9042d32017-03-17 08:50:45 +08002505 if (context->getClientVersion() >= Version(3, 1))
2506 {
2507 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2508 {
2509 infoLog
2510 << "Two transform feedback varyings include the same array element ("
2511 << tfVaryingName << ").";
2512 return false;
2513 }
2514 }
2515 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002516 {
2517 infoLog << "Capture of arrays is undefined and not supported.";
2518 return false;
2519 }
2520
jchen10a9042d32017-03-17 08:50:45 +08002521 uniqueNames.insert(tfVaryingName);
2522
Jamie Madillccdf74b2015-08-18 10:46:12 -04002523 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002524 size_t elementCount =
Olli Etuahoc8538042017-09-27 11:20:15 +03002525 ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1);
jchen10a9042d32017-03-17 08:50:45 +08002526 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002527 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002528 componentCount > caps.maxTransformFeedbackSeparateComponents)
2529 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002530 infoLog << "Transform feedback varying's " << varying->name << " components ("
2531 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002532 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002533 return false;
2534 }
2535
2536 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002537 found = true;
2538 break;
2539 }
2540 }
jchen10a9042d32017-03-17 08:50:45 +08002541 if (context->getClientVersion() < Version(3, 1) &&
2542 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002543 {
Geoff Lang1a683462015-09-29 15:09:59 -04002544 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002545 return false;
2546 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002547 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2548 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002549 ASSERT(found);
2550 }
2551
Jamie Madill48ef11b2016-04-27 15:21:52 -04002552 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002553 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002554 {
Jamie Madillf6113162015-05-07 11:49:21 -04002555 infoLog << "Transform feedback varying total components (" << totalComponents
2556 << ") exceed the maximum interleaved components ("
2557 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002558 return false;
2559 }
2560
2561 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002562}
2563
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002564bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2565{
2566 const std::vector<sh::Uniform> &vertexUniforms =
2567 mState.mAttachedVertexShader->getUniforms(context);
2568 const std::vector<sh::Uniform> &fragmentUniforms =
2569 mState.mAttachedFragmentShader->getUniforms(context);
2570 const std::vector<sh::Attribute> &attributes =
2571 mState.mAttachedVertexShader->getActiveAttributes(context);
2572 for (const auto &attrib : attributes)
2573 {
2574 for (const auto &uniform : vertexUniforms)
2575 {
2576 if (uniform.name == attrib.name)
2577 {
2578 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2579 return false;
2580 }
2581 }
2582 for (const auto &uniform : fragmentUniforms)
2583 {
2584 if (uniform.name == attrib.name)
2585 {
2586 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2587 return false;
2588 }
2589 }
2590 }
2591 return true;
2592}
2593
Jamie Madill192745a2016-12-22 15:58:21 -05002594void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002595{
2596 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002597 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002598 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002599 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002600 std::vector<unsigned int> subscripts;
2601 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002602 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002603 if (!subscripts.empty())
2604 {
2605 subscript = subscripts.back();
2606 }
Jamie Madill192745a2016-12-22 15:58:21 -05002607 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002608 {
Jamie Madill192745a2016-12-22 15:58:21 -05002609 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002610 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002611 {
jchen10a9042d32017-03-17 08:50:45 +08002612 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2613 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002614 break;
2615 }
2616 }
2617 }
2618}
2619
Jamie Madillbd044ed2017-06-05 12:59:21 -04002620Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002621{
Jamie Madill192745a2016-12-22 15:58:21 -05002622 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002623
Jamie Madillbd044ed2017-06-05 12:59:21 -04002624 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002625 {
Jamie Madill192745a2016-12-22 15:58:21 -05002626 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002627 }
2628
Jamie Madillbd044ed2017-06-05 12:59:21 -04002629 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002630 {
Jamie Madill192745a2016-12-22 15:58:21 -05002631 merged[varying.name].fragment = &varying;
2632 }
2633
2634 return merged;
2635}
2636
2637std::vector<PackedVarying> Program::getPackedVaryings(
2638 const Program::MergedVaryings &mergedVaryings) const
2639{
2640 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2641 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002642 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002643
2644 for (const auto &ref : mergedVaryings)
2645 {
2646 const sh::Varying *input = ref.second.vertex;
2647 const sh::Varying *output = ref.second.fragment;
2648
2649 // Only pack varyings that have a matched input or output, plus special builtins.
2650 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002651 {
Jamie Madill192745a2016-12-22 15:58:21 -05002652 // Will get the vertex shader interpolation by default.
2653 auto interpolation = ref.second.get()->interpolation;
2654
Olli Etuaho06a06f52017-07-12 12:22:15 +03002655 // Note that we lose the vertex shader static use information here. The data for the
2656 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002657 if (output->isStruct())
2658 {
2659 ASSERT(!output->isArray());
2660 for (const auto &field : output->fields)
2661 {
2662 ASSERT(!field.isStruct() && !field.isArray());
2663 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2664 }
2665 }
2666 else
2667 {
2668 packedVaryings.push_back(PackedVarying(*output, interpolation));
2669 }
2670 continue;
2671 }
2672
2673 // Keep Transform FB varyings in the merged list always.
2674 if (!input)
2675 {
2676 continue;
2677 }
2678
2679 for (const std::string &tfVarying : tfVaryings)
2680 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002681 std::vector<unsigned int> subscripts;
2682 std::string baseName = ParseResourceName(tfVarying, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002683 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002684 if (!subscripts.empty())
2685 {
2686 subscript = subscripts.back();
2687 }
jchen10a9042d32017-03-17 08:50:45 +08002688 if (uniqueFullNames.count(tfVarying) > 0)
2689 {
2690 continue;
2691 }
2692 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002693 {
2694 // Transform feedback for varying structs is underspecified.
2695 // See Khronos bug 9856.
2696 // TODO(jmadill): Figure out how to be spec-compliant here.
2697 if (!input->isStruct())
2698 {
2699 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2700 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002701 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2702 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002703 }
jchen10a9042d32017-03-17 08:50:45 +08002704 if (subscript == GL_INVALID_INDEX)
2705 {
2706 break;
2707 }
Jamie Madill192745a2016-12-22 15:58:21 -05002708 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002709 }
2710 }
2711
Jamie Madill192745a2016-12-22 15:58:21 -05002712 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2713
2714 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002715}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002716
Jamie Madillbd044ed2017-06-05 12:59:21 -04002717void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002718{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002719 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002720 ASSERT(fragmentShader != nullptr);
2721
Geoff Lange0cff192017-05-30 13:04:56 -04002722 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002723 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002724
2725 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002726 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002727 {
2728 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2729 outputVariable.name != "gl_FragData")
2730 {
2731 continue;
2732 }
2733
2734 unsigned int baseLocation =
2735 (outputVariable.location == -1 ? 0u
2736 : static_cast<unsigned int>(outputVariable.location));
2737 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2738 elementIndex++)
2739 {
2740 const unsigned int location = baseLocation + elementIndex;
2741 if (location >= mState.mOutputVariableTypes.size())
2742 {
2743 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2744 }
Corentin Walleze7557742017-06-01 13:09:57 -04002745 ASSERT(location < mState.mActiveOutputVariables.size());
2746 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002747 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2748 }
2749 }
2750
Jamie Madill80a6fc02015-08-21 16:53:16 -04002751 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002752 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002753 return;
2754
Jamie Madillbd044ed2017-06-05 12:59:21 -04002755 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002756 // TODO(jmadill): any caps validation here?
2757
jchen1015015f72017-03-16 13:54:21 +08002758 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002759 outputVariableIndex++)
2760 {
jchen1015015f72017-03-16 13:54:21 +08002761 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002762
2763 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2764 if (outputVariable.isBuiltIn())
2765 continue;
2766
2767 // Since multiple output locations must be specified, use 0 for non-specified locations.
2768 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2769
Jamie Madill80a6fc02015-08-21 16:53:16 -04002770 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2771 elementIndex++)
2772 {
2773 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002774 ASSERT(mState.mOutputLocations.count(location) == 0);
Olli Etuahoc8538042017-09-27 11:20:15 +03002775 if (outputVariable.isArray())
2776 {
2777 mState.mOutputLocations[location] =
2778 VariableLocation(elementIndex, outputVariableIndex);
2779 }
2780 else
2781 {
2782 VariableLocation locationInfo;
2783 locationInfo.index = outputVariableIndex;
2784 mState.mOutputLocations[location] = locationInfo;
2785 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002786 }
2787 }
2788}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002789
Olli Etuaho48fed632017-03-16 12:05:30 +00002790void Program::setUniformValuesFromBindingQualifiers()
2791{
Jamie Madill982f6e02017-06-07 14:33:04 -04002792 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002793 {
2794 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2795 if (samplerUniform.binding != -1)
2796 {
2797 GLint location = mState.getUniformLocation(samplerUniform.name);
2798 ASSERT(location != -1);
2799 std::vector<GLint> boundTextureUnits;
2800 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2801 ++elementIndex)
2802 {
2803 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2804 }
2805 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2806 boundTextureUnits.data());
2807 }
2808 }
2809}
2810
jchen10eaef1e52017-06-13 10:44:11 +08002811void Program::gatherAtomicCounterBuffers()
2812{
jchen10baf5d942017-08-28 20:45:48 +08002813 for (unsigned int index : mState.mAtomicCounterUniformRange)
2814 {
2815 auto &uniform = mState.mUniforms[index];
2816 uniform.blockInfo.offset = uniform.offset;
2817 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2818 uniform.blockInfo.matrixStride = 0;
2819 uniform.blockInfo.isRowMajorMatrix = false;
2820 }
2821
jchen10eaef1e52017-06-13 10:44:11 +08002822 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2823}
2824
Jiajia Qin729b2c62017-08-14 09:36:11 +08002825void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826{
Jiajia Qin729b2c62017-08-14 09:36:11 +08002827 for (const sh::InterfaceBlock &computeBlock : computeBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002828 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002829
Jiajia Qin729b2c62017-08-14 09:36:11 +08002830 // Only 'packed' blocks are allowed to be considered inactive.
2831 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2832 continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002833
Jiajia Qin729b2c62017-08-14 09:36:11 +08002834 defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002835 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002836}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002837
Jiajia Qin729b2c62017-08-14 09:36:11 +08002838void Program::gatherVertexAndFragmentBlockInfo(
2839 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2840 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks)
2841{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842 std::set<std::string> visitedList;
2843
Jiajia Qin729b2c62017-08-14 09:36:11 +08002844 for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002845 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002846 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002847 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2848 continue;
2849
Jiajia Qin729b2c62017-08-14 09:36:11 +08002850 defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002851 visitedList.insert(vertexBlock.name);
2852 }
2853
Jiajia Qin729b2c62017-08-14 09:36:11 +08002854 for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002855 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002856 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002857 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2858 continue;
2859
2860 if (visitedList.count(fragmentBlock.name) > 0)
2861 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002862 if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002863 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002864 for (InterfaceBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002865 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002866 if (block.name == fragmentBlock.name)
2867 {
2868 block.fragmentStaticUse = fragmentBlock.staticUse;
2869 }
2870 }
2871 }
2872 else
2873 {
2874 ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2875 for (InterfaceBlock &block : mState.mShaderStorageBlocks)
2876 {
2877 if (block.name == fragmentBlock.name)
2878 {
2879 block.fragmentStaticUse = fragmentBlock.staticUse;
2880 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002881 }
2882 }
2883
2884 continue;
2885 }
2886
Jiajia Qin729b2c62017-08-14 09:36:11 +08002887 defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002888 visitedList.insert(fragmentBlock.name);
2889 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002890}
2891
2892void Program::gatherInterfaceBlockInfo(const Context *context)
2893{
2894 ASSERT(mState.mUniformBlocks.empty());
2895 ASSERT(mState.mShaderStorageBlocks.empty());
2896
2897 if (mState.mAttachedComputeShader)
2898 {
2899 Shader *computeShader = mState.getAttachedComputeShader();
2900
2901 gatherComputeBlockInfo(computeShader->getUniformBlocks(context));
2902 gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context));
2903 return;
2904 }
2905
2906 Shader *vertexShader = mState.getAttachedVertexShader();
2907 Shader *fragmentShader = mState.getAttachedFragmentShader();
2908
2909 gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context),
2910 fragmentShader->getUniformBlocks(context));
2911 if (context->getClientVersion() >= Version(3, 1))
2912 {
2913 gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context),
2914 fragmentShader->getShaderStorageBlocks(context));
2915 }
2916
jchen10af713a22017-04-19 09:10:56 +08002917 // Set initial bindings from shader.
2918 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2919 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002920 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002921 bindUniformBlock(blockIndex, uniformBlock.binding);
2922 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002923}
2924
Jamie Madill4a3c2342015-10-08 12:58:45 -04002925template <typename VarT>
2926void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2927 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002928 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002929 int blockIndex)
2930{
2931 for (const VarT &field : fields)
2932 {
2933 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2934
Olli Etuaho855d9642017-05-17 14:05:06 +03002935 const std::string &fullMappedName =
2936 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2937
Jamie Madill4a3c2342015-10-08 12:58:45 -04002938 if (field.isStruct())
2939 {
2940 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2941 {
2942 const std::string uniformElementName =
2943 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002944 const std::string uniformElementMappedName =
2945 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2946 defineUniformBlockMembers(field.fields, uniformElementName,
2947 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002948 }
2949 }
2950 else
2951 {
2952 // If getBlockMemberInfo returns false, the uniform is optimized out.
2953 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002954 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002955 {
2956 continue;
2957 }
2958
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002959 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002960 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002961 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002962
2963 // Since block uniforms have no location, we don't need to store them in the uniform
2964 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002965 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002966 }
2967 }
2968}
2969
Jiajia Qin729b2c62017-08-14 09:36:11 +08002970void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002971{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002972 size_t blockSize = 0;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002973 std::vector<unsigned int> blockIndexes;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002974
Jiajia Qin729b2c62017-08-14 09:36:11 +08002975 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002976 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002977 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
2978 // Track the first and last uniform index to determine the range of active uniforms in the
2979 // block.
2980 size_t firstBlockUniformIndex = mState.mUniforms.size();
2981 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2982 interfaceBlock.fieldMappedPrefix(), blockIndex);
2983 size_t lastBlockUniformIndex = mState.mUniforms.size();
2984
2985 for (size_t blockUniformIndex = firstBlockUniformIndex;
2986 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2987 {
2988 blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2989 }
2990 }
2991 else
2992 {
2993 // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index.
2994 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002995 }
jchen10af713a22017-04-19 09:10:56 +08002996 // ESSL 3.10 section 4.4.4 page 58:
2997 // Any uniform or shader storage block declared without a binding qualifier is initially
2998 // assigned to block binding point zero.
2999 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003000 if (interfaceBlock.arraySize > 0)
3001 {
3002 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
3003 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003004 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3005 // of UniformBlock and ShaderStorageBlock.
3006 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003007 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003008 // Don't define this block at all if it's not active in the implementation.
3009 if (!mProgram->getUniformBlockSize(
3010 interfaceBlock.name + ArrayString(arrayElement),
3011 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
3012 {
3013 continue;
3014 }
jchen10af713a22017-04-19 09:10:56 +08003015 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003016
3017 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
3018 blockBinding + arrayElement);
3019 block.memberIndexes = blockIndexes;
jchen10baf5d942017-08-28 20:45:48 +08003020 MarkResourceStaticUse(&block, shaderType, interfaceBlock.staticUse);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003021
Jiajia Qin729b2c62017-08-14 09:36:11 +08003022 // Since all block elements in an array share the same active interface blocks, they
3023 // will all be active once any block member is used. So, since interfaceBlock.name[0]
3024 // was active, here we will add every block element in the array.
Qin Jiajia0350a642016-11-01 17:01:51 +08003025 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003026 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3027 {
3028 mState.mUniformBlocks.push_back(block);
3029 }
3030 else
3031 {
3032 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3033 mState.mShaderStorageBlocks.push_back(block);
3034 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003035 }
3036 }
3037 else
3038 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003039 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3040 // of UniformBlock and ShaderStorageBlock.
3041 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003042 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003043 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
3044 &blockSize))
3045 {
3046 return;
3047 }
jchen10af713a22017-04-19 09:10:56 +08003048 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003049
3050 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0,
3051 blockBinding);
3052 block.memberIndexes = blockIndexes;
jchen10baf5d942017-08-28 20:45:48 +08003053 MarkResourceStaticUse(&block, shaderType, interfaceBlock.staticUse);
Jamie Madill4a3c2342015-10-08 12:58:45 -04003054 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003055 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3056 {
3057 mState.mUniformBlocks.push_back(block);
3058 }
3059 else
3060 {
3061 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3062 mState.mShaderStorageBlocks.push_back(block);
3063 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003064 }
3065}
3066
Jamie Madille7d84322017-01-10 18:21:59 -05003067void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003068 GLsizei clampedCount,
3069 const GLint *v)
3070{
Jamie Madill81c2e252017-09-09 23:32:46 -04003071 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3072 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3073 std::vector<GLuint> *boundTextureUnits =
3074 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003075
Olli Etuahoc8538042017-09-27 11:20:15 +03003076 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.flattenedArrayOffset);
Jamie Madilld68248b2017-09-11 14:34:14 -04003077
3078 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003079 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003080}
3081
3082template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003083GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3084 GLsizei count,
3085 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003086 const T *v)
3087{
Jamie Madill134f93d2017-08-31 17:11:00 -04003088 if (count == 1)
3089 return 1;
3090
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003091 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003092
Corentin Wallez15ac5342016-11-03 17:06:39 -04003093 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3094 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03003095 unsigned int remainingElements =
3096 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003097 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003098 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003099
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003100 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003101 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003102 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003103 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003104
3105 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003106}
3107
3108template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003109GLsizei Program::clampMatrixUniformCount(GLint location,
3110 GLsizei count,
3111 GLboolean transpose,
3112 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003113{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003114 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3115
Jamie Madill62d31cb2015-09-11 13:25:51 -04003116 if (!transpose)
3117 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003118 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003119 }
3120
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003121 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003122
3123 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3124 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03003125 unsigned int remainingElements =
3126 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003127 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003128}
3129
Jamie Madill54164b02017-08-28 15:17:37 -04003130// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3131// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003132template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003133void Program::getUniformInternal(const Context *context,
3134 DestT *dataOut,
3135 GLint location,
3136 GLenum nativeType,
3137 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003138{
Jamie Madill54164b02017-08-28 15:17:37 -04003139 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003140 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003141 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003142 {
3143 GLint tempValue[16] = {0};
3144 mProgram->getUniformiv(context, location, tempValue);
3145 UniformStateQueryCastLoop<GLboolean>(
3146 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003147 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003148 }
3149 case GL_INT:
3150 {
3151 GLint tempValue[16] = {0};
3152 mProgram->getUniformiv(context, location, tempValue);
3153 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3154 components);
3155 break;
3156 }
3157 case GL_UNSIGNED_INT:
3158 {
3159 GLuint tempValue[16] = {0};
3160 mProgram->getUniformuiv(context, location, tempValue);
3161 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3162 components);
3163 break;
3164 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003165 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003166 {
3167 GLfloat tempValue[16] = {0};
3168 mProgram->getUniformfv(context, location, tempValue);
3169 UniformStateQueryCastLoop<GLfloat>(
3170 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003171 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003172 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003173 default:
3174 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003175 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003176 }
3177}
Jamie Madilla4595b82017-01-11 17:36:34 -05003178
3179bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3180{
3181 // Must be called after samplers are validated.
3182 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3183
3184 for (const auto &binding : mState.mSamplerBindings)
3185 {
3186 GLenum textureType = binding.textureType;
3187 for (const auto &unit : binding.boundTextureUnits)
3188 {
3189 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3190 if (programTextureID == textureID)
3191 {
3192 // TODO(jmadill): Check for appropriate overlap.
3193 return true;
3194 }
3195 }
3196 }
3197
3198 return false;
3199}
3200
Jamie Madilla2c74982016-12-12 11:20:42 -05003201} // namespace gl