blob: de9724c979c0a068e6985a4f85243e1b590cd2e9 [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"
Olli Etuahod2551232017-10-26 20:03:33 +030017#include "common/string_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040021#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050022#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000024#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040025#include "libANGLE/VaryingPacking.h"
26#include "libANGLE/features.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040027#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040028#include "libANGLE/queryconversions.h"
29#include "libANGLE/renderer/GLImplFactory.h"
30#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040031#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050032
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033namespace gl
34{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000035
Geoff Lang7dd2e102014-11-10 15:19:26 -050036namespace
37{
38
Jamie Madill62d31cb2015-09-11 13:25:51 -040039// This simplified cast function doesn't need to worry about advanced concepts like
40// depth range values, or casting to bool.
41template <typename DestT, typename SrcT>
42DestT UniformStateQueryCast(SrcT value);
43
44// From-Float-To-Integer Casts
45template <>
46GLint UniformStateQueryCast(GLfloat value)
47{
48 return clampCast<GLint>(roundf(value));
49}
50
51template <>
52GLuint UniformStateQueryCast(GLfloat value)
53{
54 return clampCast<GLuint>(roundf(value));
55}
56
57// From-Integer-to-Integer Casts
58template <>
59GLint UniformStateQueryCast(GLuint value)
60{
61 return clampCast<GLint>(value);
62}
63
64template <>
65GLuint UniformStateQueryCast(GLint value)
66{
67 return clampCast<GLuint>(value);
68}
69
70// From-Boolean-to-Anything Casts
71template <>
72GLfloat UniformStateQueryCast(GLboolean value)
73{
74 return (value == GL_TRUE ? 1.0f : 0.0f);
75}
76
77template <>
78GLint UniformStateQueryCast(GLboolean value)
79{
80 return (value == GL_TRUE ? 1 : 0);
81}
82
83template <>
84GLuint UniformStateQueryCast(GLboolean value)
85{
86 return (value == GL_TRUE ? 1u : 0u);
87}
88
89// Default to static_cast
90template <typename DestT, typename SrcT>
91DestT UniformStateQueryCast(SrcT value)
92{
93 return static_cast<DestT>(value);
94}
95
96template <typename SrcT, typename DestT>
97void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
98{
99 for (int comp = 0; comp < components; ++comp)
100 {
101 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
102 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
103 size_t offset = comp * 4;
104 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
105 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
106 }
107}
108
Jamie Madill192745a2016-12-22 15:58:21 -0500109// true if varying x has a higher priority in packing than y
110bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
111{
jchen10a9042d32017-03-17 08:50:45 +0800112 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
113 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
114 sh::ShaderVariable vx, vy;
115 const sh::ShaderVariable *px, *py;
116 if (x.isArrayElement())
117 {
118 vx = *x.varying;
119 vx.arraySize = 0;
120 px = &vx;
121 }
122 else
123 {
124 px = x.varying;
125 }
126
127 if (y.isArrayElement())
128 {
129 vy = *y.varying;
130 vy.arraySize = 0;
131 py = &vy;
132 }
133 else
134 {
135 py = y.varying;
136 }
137
138 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500139}
140
jchen1015015f72017-03-16 13:54:21 +0800141template <typename VarT>
142GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
143{
Olli Etuahod2551232017-10-26 20:03:33 +0300144 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800145 for (size_t index = 0; index < list.size(); index++)
146 {
147 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300148 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800149 {
Olli Etuahod2551232017-10-26 20:03:33 +0300150 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800151 }
152 }
153
154 return GL_INVALID_INDEX;
155}
156
Olli Etuahod2551232017-10-26 20:03:33 +0300157template <typename VarT>
158GLint GetVariableLocation(const std::vector<VarT> &list,
159 const std::vector<VariableLocation> &locationList,
160 const std::string &name)
161{
162 size_t nameLengthWithoutArrayIndex;
163 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
164
165 for (size_t location = 0u; location < locationList.size(); ++location)
166 {
167 const VariableLocation &variableLocation = locationList[location];
168 if (!variableLocation.used())
169 {
170 continue;
171 }
172
173 const VarT &variable = list[variableLocation.index];
174
175 if (angle::BeginsWith(variable.name, name))
176 {
177 if (name.length() == variable.name.length())
178 {
179 ASSERT(name == variable.name);
180 // GLES 3.1 November 2016 page 87.
181 // The string exactly matches the name of the active variable.
182 return static_cast<GLint>(location);
183 }
184 if (name.length() + 3u == variable.name.length() && variable.isArray())
185 {
186 ASSERT(name + "[0]" == variable.name);
187 // The string identifies the base name of an active array, where the string would
188 // exactly match the name of the variable if the suffix "[0]" were appended to the
189 // string.
190 return static_cast<GLint>(location);
191 }
192 }
193 if (variable.isArray() && variableLocation.arrayIndices[0] == arrayIndex &&
194 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
195 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
196 {
197 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
198 // The string identifies an active element of the array, where the string ends with the
199 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
200 // zeroes, or whitespace) identifying an array element, and the "]" character, the
201 // integer is less than the number of active elements of the array variable, and where
202 // the string would exactly match the enumerated name of the array if the decimal
203 // integer were replaced with zero.
204 return static_cast<GLint>(location);
205 }
206 }
207
208 return -1;
209}
210
jchen10fd7c3b52017-03-21 15:36:03 +0800211void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
212{
213 ASSERT(bufSize > 0);
214 strncpy(buffer, string.c_str(), bufSize);
215 buffer[bufSize - 1] = '\0';
216
217 if (length)
218 {
219 *length = static_cast<GLsizei>(strlen(buffer));
220 }
221}
222
jchen10a9042d32017-03-17 08:50:45 +0800223bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
224{
Olli Etuahoc8538042017-09-27 11:20:15 +0300225 std::vector<unsigned int> subscripts;
226 std::string baseName = ParseResourceName(name, &subscripts);
227 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800228 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300229 std::vector<unsigned int> arrayIndices;
230 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
231 if (baseName == arrayName &&
232 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800233 {
234 return true;
235 }
236 }
237 return false;
238}
239
Jiajia Qin729b2c62017-08-14 09:36:11 +0800240bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
241 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
242 const std::string &errorMessage,
243 InfoLog &infoLog)
244{
245 GLuint blockCount = 0;
246 for (const sh::InterfaceBlock &block : interfaceBlocks)
247 {
248 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
249 {
250 blockCount += (block.arraySize ? block.arraySize : 1);
251 if (blockCount > maxInterfaceBlocks)
252 {
253 infoLog << errorMessage << maxInterfaceBlocks << ")";
254 return false;
255 }
256 }
257 }
258 return true;
259}
260
Jamie Madill62d31cb2015-09-11 13:25:51 -0400261} // anonymous namespace
262
Jamie Madill4a3c2342015-10-08 12:58:45 -0400263const char *const g_fakepath = "C:\\fakepath";
264
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400265InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000266{
267}
268
269InfoLog::~InfoLog()
270{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000271}
272
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400273size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000274{
Jamie Madill23176ce2017-07-31 14:14:33 -0400275 if (!mLazyStream)
276 {
277 return 0;
278 }
279
280 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400281 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000282}
283
Geoff Lange1a27752015-10-05 13:16:04 -0400284void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000285{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400286 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000287
288 if (bufSize > 0)
289 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400290 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400291
Jamie Madill23176ce2017-07-31 14:14:33 -0400292 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000293 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400294 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
295 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000296 }
297
298 infoLog[index] = '\0';
299 }
300
301 if (length)
302 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400303 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000304 }
305}
306
307// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300308// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000309// messages, so lets remove all occurrences of this fake file path from the log.
310void InfoLog::appendSanitized(const char *message)
311{
Jamie Madill23176ce2017-07-31 14:14:33 -0400312 ensureInitialized();
313
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000314 std::string msg(message);
315
316 size_t found;
317 do
318 {
319 found = msg.find(g_fakepath);
320 if (found != std::string::npos)
321 {
322 msg.erase(found, strlen(g_fakepath));
323 }
324 }
325 while (found != std::string::npos);
326
Jamie Madill23176ce2017-07-31 14:14:33 -0400327 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000328}
329
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000330void InfoLog::reset()
331{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000332}
333
Olli Etuahoc8538042017-09-27 11:20:15 +0300334VariableLocation::VariableLocation() : index(kUnused), flattenedArrayOffset(0u), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000335{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500336}
337
Olli Etuahoc8538042017-09-27 11:20:15 +0300338VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
339 : arrayIndices(1, arrayIndex), index(index), flattenedArrayOffset(arrayIndex), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500340{
Olli Etuahoc8538042017-09-27 11:20:15 +0300341 ASSERT(arrayIndex != GL_INVALID_INDEX);
342}
343
344bool VariableLocation::areAllArrayIndicesZero() const
345{
346 for (unsigned int arrayIndex : arrayIndices)
347 {
348 if (arrayIndex != 0)
349 {
350 return false;
351 }
352 }
353 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500354}
355
Geoff Langd8605522016-04-13 10:19:12 -0400356void Program::Bindings::bindLocation(GLuint index, const std::string &name)
357{
358 mBindings[name] = index;
359}
360
361int Program::Bindings::getBinding(const std::string &name) const
362{
363 auto iter = mBindings.find(name);
364 return (iter != mBindings.end()) ? iter->second : -1;
365}
366
367Program::Bindings::const_iterator Program::Bindings::begin() const
368{
369 return mBindings.begin();
370}
371
372Program::Bindings::const_iterator Program::Bindings::end() const
373{
374 return mBindings.end();
375}
376
Jamie Madill48ef11b2016-04-27 15:21:52 -0400377ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500378 : mLabel(),
379 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400380 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300381 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500382 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400383 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500384 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800385 mImageUniformRange(0, 0),
386 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300387 mBinaryRetrieveableHint(false),
388 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400389{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300390 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400391}
392
Jamie Madill48ef11b2016-04-27 15:21:52 -0400393ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400394{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500395 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400396}
397
Jamie Madill48ef11b2016-04-27 15:21:52 -0400398const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500399{
400 return mLabel;
401}
402
Jamie Madille7d84322017-01-10 18:21:59 -0500403GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400404{
jchen1015015f72017-03-16 13:54:21 +0800405 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400406}
407
Jamie Madille7d84322017-01-10 18:21:59 -0500408GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
409{
410 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
411 return mUniformLocations[location].index;
412}
413
414Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
415{
416 GLuint index = getUniformIndexFromLocation(location);
417 if (!isSamplerUniformIndex(index))
418 {
419 return Optional<GLuint>::Invalid();
420 }
421
422 return getSamplerIndexFromUniformIndex(index);
423}
424
425bool ProgramState::isSamplerUniformIndex(GLuint index) const
426{
Jamie Madill982f6e02017-06-07 14:33:04 -0400427 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500428}
429
430GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
431{
432 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400433 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500434}
435
Jamie Madill34ca4f52017-06-13 11:49:39 -0400436GLuint ProgramState::getAttributeLocation(const std::string &name) const
437{
438 for (const sh::Attribute &attribute : mAttributes)
439 {
440 if (attribute.name == name)
441 {
442 return attribute.location;
443 }
444 }
445
446 return static_cast<GLuint>(-1);
447}
448
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500449Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400450 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400451 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500452 mLinked(false),
453 mDeleteStatus(false),
454 mRefCount(0),
455 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500456 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500457{
458 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000459
Geoff Lang7dd2e102014-11-10 15:19:26 -0500460 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461}
462
463Program::~Program()
464{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400465 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466}
467
Jamie Madill4928b7c2017-06-20 12:57:39 -0400468void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500469{
470 if (mState.mAttachedVertexShader != nullptr)
471 {
472 mState.mAttachedVertexShader->release(context);
473 mState.mAttachedVertexShader = nullptr;
474 }
475
476 if (mState.mAttachedFragmentShader != nullptr)
477 {
478 mState.mAttachedFragmentShader->release(context);
479 mState.mAttachedFragmentShader = nullptr;
480 }
481
482 if (mState.mAttachedComputeShader != nullptr)
483 {
484 mState.mAttachedComputeShader->release(context);
485 mState.mAttachedComputeShader = nullptr;
486 }
487
Jamie Madillc564c072017-06-01 12:45:42 -0400488 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400489
490 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
491 !mState.mAttachedComputeShader);
492 SafeDelete(mProgram);
493
494 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500495}
496
Geoff Lang70d0f492015-12-10 17:45:46 -0500497void Program::setLabel(const std::string &label)
498{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400499 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500500}
501
502const std::string &Program::getLabel() const
503{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400504 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500505}
506
Jamie Madillef300b12016-10-07 15:12:09 -0400507void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300509 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300511 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512 {
Jamie Madillef300b12016-10-07 15:12:09 -0400513 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300514 mState.mAttachedVertexShader = shader;
515 mState.mAttachedVertexShader->addRef();
516 break;
517 }
518 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519 {
Jamie Madillef300b12016-10-07 15:12:09 -0400520 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300521 mState.mAttachedFragmentShader = shader;
522 mState.mAttachedFragmentShader->addRef();
523 break;
524 }
525 case GL_COMPUTE_SHADER:
526 {
Jamie Madillef300b12016-10-07 15:12:09 -0400527 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300528 mState.mAttachedComputeShader = shader;
529 mState.mAttachedComputeShader->addRef();
530 break;
531 }
532 default:
533 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535}
536
Jamie Madillc1d770e2017-04-13 17:31:24 -0400537void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300539 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300541 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400543 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500544 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300545 mState.mAttachedVertexShader = nullptr;
546 break;
547 }
548 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400550 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500551 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300552 mState.mAttachedFragmentShader = nullptr;
553 break;
554 }
555 case GL_COMPUTE_SHADER:
556 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400557 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500558 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300559 mState.mAttachedComputeShader = nullptr;
560 break;
561 }
562 default:
563 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565}
566
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000567int Program::getAttachedShadersCount() const
568{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300569 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
570 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000571}
572
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573void Program::bindAttributeLocation(GLuint index, const char *name)
574{
Geoff Langd8605522016-04-13 10:19:12 -0400575 mAttributeBindings.bindLocation(index, name);
576}
577
578void Program::bindUniformLocation(GLuint index, const char *name)
579{
Olli Etuahod2551232017-10-26 20:03:33 +0300580 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581}
582
Sami Väisänen46eaa942016-06-29 10:26:37 +0300583void Program::bindFragmentInputLocation(GLint index, const char *name)
584{
585 mFragmentInputBindings.bindLocation(index, name);
586}
587
Jamie Madillbd044ed2017-06-05 12:59:21 -0400588BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300589{
590 BindingInfo ret;
591 ret.type = GL_NONE;
592 ret.valid = false;
593
Jamie Madillbd044ed2017-06-05 12:59:21 -0400594 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300595 ASSERT(fragmentShader);
596
597 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800598 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300599
600 for (const auto &binding : mFragmentInputBindings)
601 {
602 if (binding.second != static_cast<GLuint>(index))
603 continue;
604
605 ret.valid = true;
606
Olli Etuahod2551232017-10-26 20:03:33 +0300607 size_t nameLengthWithoutArrayIndex;
608 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300609
610 for (const auto &in : inputs)
611 {
Olli Etuahod2551232017-10-26 20:03:33 +0300612 if (in.name.length() == nameLengthWithoutArrayIndex &&
613 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300614 {
615 if (in.isArray())
616 {
617 // The client wants to bind either "name" or "name[0]".
618 // GL ES 3.1 spec refers to active array names with language such as:
619 // "if the string identifies the base name of an active array, where the
620 // string would exactly match the name of the variable if the suffix "[0]"
621 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400622 if (arrayIndex == GL_INVALID_INDEX)
623 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300624
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400625 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300626 }
627 else
628 {
629 ret.name = in.mappedName;
630 }
631 ret.type = in.type;
632 return ret;
633 }
634 }
635 }
636
637 return ret;
638}
639
Jamie Madillbd044ed2017-06-05 12:59:21 -0400640void Program::pathFragmentInputGen(const Context *context,
641 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300642 GLenum genMode,
643 GLint components,
644 const GLfloat *coeffs)
645{
646 // If the location is -1 then the command is silently ignored
647 if (index == -1)
648 return;
649
Jamie Madillbd044ed2017-06-05 12:59:21 -0400650 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300651
652 // If the input doesn't exist then then the command is silently ignored
653 // This could happen through optimization for example, the shader translator
654 // decides that a variable is not actually being used and optimizes it away.
655 if (binding.name.empty())
656 return;
657
658 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
659}
660
Martin Radev4c4c8e72016-08-04 12:25:34 +0300661// The attached shaders are checked for linking errors by matching up their variables.
662// Uniform, input and output variables get collected.
663// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500664Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000665{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500666 const auto &data = context->getContextState();
667
Jamie Madill6c58b062017-08-01 13:44:25 -0400668 auto *platform = ANGLEPlatformCurrent();
669 double startTime = platform->currentTime(platform);
670
Jamie Madill6c1f6712017-02-14 19:08:04 -0500671 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000672
Jamie Madill32447362017-06-28 14:53:52 -0400673 ProgramHash programHash;
674 auto *cache = context->getMemoryProgramCache();
675 if (cache)
676 {
677 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400678 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400679 }
680
681 if (mLinked)
682 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400683 double delta = platform->currentTime(platform) - startTime;
684 int us = static_cast<int>(delta * 1000000.0);
685 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400686 return NoError();
687 }
688
689 // Cache load failed, fall through to normal linking.
690 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000691 mInfoLog.reset();
692
Martin Radev4c4c8e72016-08-04 12:25:34 +0300693 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500694
Jamie Madill192745a2016-12-22 15:58:21 -0500695 auto vertexShader = mState.mAttachedVertexShader;
696 auto fragmentShader = mState.mAttachedFragmentShader;
697 auto computeShader = mState.mAttachedComputeShader;
698
699 bool isComputeShaderAttached = (computeShader != nullptr);
700 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701 // Check whether we both have a compute and non-compute shaders attached.
702 // If there are of both types attached, then linking should fail.
703 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
704 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500705 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300706 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
707 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400708 }
709
Jamie Madill192745a2016-12-22 15:58:21 -0500710 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500711 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400712 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300713 {
714 mInfoLog << "Attached compute shader is not compiled.";
715 return NoError();
716 }
Jamie Madill192745a2016-12-22 15:58:21 -0500717 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300718
Jamie Madillbd044ed2017-06-05 12:59:21 -0400719 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720
721 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
722 // If the work group size is not specified, a link time error should occur.
723 if (!mState.mComputeShaderLocalSize.isDeclared())
724 {
725 mInfoLog << "Work group size is not specified.";
726 return NoError();
727 }
728
Jamie Madillbd044ed2017-06-05 12:59:21 -0400729 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300730 {
731 return NoError();
732 }
733
Jiajia Qin729b2c62017-08-14 09:36:11 +0800734 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300735 {
736 return NoError();
737 }
738
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500739 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400740 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500741 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300742 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500743 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300744 }
745 }
746 else
747 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400748 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300749 {
750 return NoError();
751 }
Jamie Madill192745a2016-12-22 15:58:21 -0500752 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753
Jamie Madillbd044ed2017-06-05 12:59:21 -0400754 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300755 {
756 return NoError();
757 }
Jamie Madill192745a2016-12-22 15:58:21 -0500758 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300759
Jamie Madillbd044ed2017-06-05 12:59:21 -0400760 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300761 {
762 mInfoLog << "Fragment shader version does not match vertex shader version.";
763 return NoError();
764 }
765
Jamie Madillbd044ed2017-06-05 12:59:21 -0400766 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300767 {
768 return NoError();
769 }
770
Jamie Madillbd044ed2017-06-05 12:59:21 -0400771 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300772 {
773 return NoError();
774 }
775
Jamie Madillbd044ed2017-06-05 12:59:21 -0400776 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300777 {
778 return NoError();
779 }
780
Jiajia Qin729b2c62017-08-14 09:36:11 +0800781 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300782 {
783 return NoError();
784 }
785
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400786 if (!linkValidateGlobalNames(context, mInfoLog))
787 {
788 return NoError();
789 }
790
Jamie Madillbd044ed2017-06-05 12:59:21 -0400791 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300792
Martin Radev7cf61662017-07-26 17:10:53 +0300793 mState.mNumViews = vertexShader->getNumViews(context);
794
Jamie Madillbd044ed2017-06-05 12:59:21 -0400795 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300796
Jamie Madill192745a2016-12-22 15:58:21 -0500797 // Validate we can pack the varyings.
798 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
799
800 // Map the varyings to the register file
801 // In WebGL, we use a slightly different handling for packing variables.
802 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
803 : PackMode::ANGLE_RELAXED;
804 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
805 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
806 mState.getTransformFeedbackVaryingNames()))
807 {
808 return NoError();
809 }
810
Olli Etuaho39e78122017-08-29 14:34:22 +0300811 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
812 {
813 return NoError();
814 }
815
Jamie Madillc564c072017-06-01 12:45:42 -0400816 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500817 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300818 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500819 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300820 }
821
822 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500823 }
824
jchen10eaef1e52017-06-13 10:44:11 +0800825 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400826 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400827
jchen10eaef1e52017-06-13 10:44:11 +0800828 setUniformValuesFromBindingQualifiers();
829
Jamie Madill54164b02017-08-28 15:17:37 -0400830 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400831 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400832
Jamie Madill32447362017-06-28 14:53:52 -0400833 // Save to the program cache.
834 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
835 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
836 {
837 cache->putProgram(programHash, context, this);
838 }
839
Jamie Madill6c58b062017-08-01 13:44:25 -0400840 double delta = platform->currentTime(platform) - startTime;
841 int us = static_cast<int>(delta * 1000000.0);
842 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
843
Martin Radev4c4c8e72016-08-04 12:25:34 +0300844 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000845}
846
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000847// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500848void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400850 mState.mAttributes.clear();
851 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400852 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800853 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400854 mState.mUniforms.clear();
855 mState.mUniformLocations.clear();
856 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800857 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800858 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400859 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800860 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400861 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400862 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300863 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500864 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800865 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300866 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500867
Geoff Lang7dd2e102014-11-10 15:19:26 -0500868 mValidated = false;
869
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000870 mLinked = false;
871}
872
Geoff Lange1a27752015-10-05 13:16:04 -0400873bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000874{
875 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876}
877
Jamie Madilla2c74982016-12-12 11:20:42 -0500878Error Program::loadBinary(const Context *context,
879 GLenum binaryFormat,
880 const void *binary,
881 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000882{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500883 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000884
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800886 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500887#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400888 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
889 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000890 {
Jamie Madillf6113162015-05-07 11:49:21 -0400891 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800892 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500893 }
894
Jamie Madill4f86d052017-06-05 12:59:26 -0400895 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
896 ANGLE_TRY_RESULT(
897 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400898
899 // Currently we require the full shader text to compute the program hash.
900 // TODO(jmadill): Store the binary in the internal program cache.
901
Jamie Madillb0a838b2016-11-13 20:02:12 -0500902 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500903#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904}
905
Jamie Madilla2c74982016-12-12 11:20:42 -0500906Error Program::saveBinary(const Context *context,
907 GLenum *binaryFormat,
908 void *binary,
909 GLsizei bufSize,
910 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500911{
912 if (binaryFormat)
913 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400914 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500915 }
916
Jamie Madill4f86d052017-06-05 12:59:26 -0400917 angle::MemoryBuffer memoryBuf;
918 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500919
Jamie Madill4f86d052017-06-05 12:59:26 -0400920 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
921 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500922
923 if (streamLength > bufSize)
924 {
925 if (length)
926 {
927 *length = 0;
928 }
929
930 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
931 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
932 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500933 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500934 }
935
936 if (binary)
937 {
938 char *ptr = reinterpret_cast<char*>(binary);
939
Jamie Madill48ef11b2016-04-27 15:21:52 -0400940 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500941 ptr += streamLength;
942
943 ASSERT(ptr - streamLength == binary);
944 }
945
946 if (length)
947 {
948 *length = streamLength;
949 }
950
He Yunchaoacd18982017-01-04 10:46:42 +0800951 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500952}
953
Jamie Madillffe00c02017-06-27 16:26:55 -0400954GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500955{
956 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400957 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500958 if (error.isError())
959 {
960 return 0;
961 }
962
963 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000964}
965
Geoff Langc5629752015-12-07 16:29:04 -0500966void Program::setBinaryRetrievableHint(bool retrievable)
967{
968 // TODO(jmadill) : replace with dirty bits
969 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400970 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500971}
972
973bool Program::getBinaryRetrievableHint() const
974{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400975 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500976}
977
Yunchao He61afff12017-03-14 15:34:03 +0800978void Program::setSeparable(bool separable)
979{
980 // TODO(yunchao) : replace with dirty bits
981 if (mState.mSeparable != separable)
982 {
983 mProgram->setSeparable(separable);
984 mState.mSeparable = separable;
985 }
986}
987
988bool Program::isSeparable() const
989{
990 return mState.mSeparable;
991}
992
Jamie Madill6c1f6712017-02-14 19:08:04 -0500993void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000994{
995 mRefCount--;
996
997 if (mRefCount == 0 && mDeleteStatus)
998 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500999 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001000 }
1001}
1002
1003void Program::addRef()
1004{
1005 mRefCount++;
1006}
1007
1008unsigned int Program::getRefCount() const
1009{
1010 return mRefCount;
1011}
1012
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001013int Program::getInfoLogLength() const
1014{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001015 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001016}
1017
Geoff Lange1a27752015-10-05 13:16:04 -04001018void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001019{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001020 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001021}
1022
Geoff Lange1a27752015-10-05 13:16:04 -04001023void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001024{
1025 int total = 0;
1026
Martin Radev4c4c8e72016-08-04 12:25:34 +03001027 if (mState.mAttachedComputeShader)
1028 {
1029 if (total < maxCount)
1030 {
1031 shaders[total] = mState.mAttachedComputeShader->getHandle();
1032 total++;
1033 }
1034 }
1035
Jamie Madill48ef11b2016-04-27 15:21:52 -04001036 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001037 {
1038 if (total < maxCount)
1039 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001040 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001041 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001042 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001043 }
1044
Jamie Madill48ef11b2016-04-27 15:21:52 -04001045 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001046 {
1047 if (total < maxCount)
1048 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001049 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001050 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001051 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001052 }
1053
1054 if (count)
1055 {
1056 *count = total;
1057 }
1058}
1059
Geoff Lange1a27752015-10-05 13:16:04 -04001060GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001061{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001062 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001063}
1064
Jamie Madill63805b42015-08-25 13:17:39 -04001065bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001066{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001067 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1068 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069}
1070
jchen10fd7c3b52017-03-21 15:36:03 +08001071void Program::getActiveAttribute(GLuint index,
1072 GLsizei bufsize,
1073 GLsizei *length,
1074 GLint *size,
1075 GLenum *type,
1076 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001077{
Jamie Madillc349ec02015-08-21 16:53:12 -04001078 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001079 {
1080 if (bufsize > 0)
1081 {
1082 name[0] = '\0';
1083 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001085 if (length)
1086 {
1087 *length = 0;
1088 }
1089
1090 *type = GL_NONE;
1091 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001092 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001093 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001094
jchen1036e120e2017-03-14 14:53:58 +08001095 ASSERT(index < mState.mAttributes.size());
1096 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001097
1098 if (bufsize > 0)
1099 {
jchen10fd7c3b52017-03-21 15:36:03 +08001100 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001101 }
1102
1103 // Always a single 'type' instance
1104 *size = 1;
1105 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001106}
1107
Geoff Lange1a27752015-10-05 13:16:04 -04001108GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001109{
Jamie Madillc349ec02015-08-21 16:53:12 -04001110 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001111 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001112 return 0;
1113 }
1114
jchen1036e120e2017-03-14 14:53:58 +08001115 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116}
1117
Geoff Lange1a27752015-10-05 13:16:04 -04001118GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001119{
Jamie Madillc349ec02015-08-21 16:53:12 -04001120 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001121 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001122 return 0;
1123 }
1124
1125 size_t maxLength = 0;
1126
Jamie Madill48ef11b2016-04-27 15:21:52 -04001127 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001128 {
jchen1036e120e2017-03-14 14:53:58 +08001129 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001130 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001131
Jamie Madillc349ec02015-08-21 16:53:12 -04001132 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133}
1134
jchen1015015f72017-03-16 13:54:21 +08001135GLuint Program::getInputResourceIndex(const GLchar *name) const
1136{
Olli Etuahod2551232017-10-26 20:03:33 +03001137 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001138}
1139
1140GLuint Program::getOutputResourceIndex(const GLchar *name) const
1141{
1142 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1143}
1144
jchen10fd7c3b52017-03-21 15:36:03 +08001145size_t Program::getOutputResourceCount() const
1146{
1147 return (mLinked ? mState.mOutputVariables.size() : 0);
1148}
1149
jchen10baf5d942017-08-28 20:45:48 +08001150template <typename T>
1151void Program::getResourceName(GLuint index,
1152 const std::vector<T> &resources,
1153 GLsizei bufSize,
1154 GLsizei *length,
1155 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001156{
1157 if (length)
1158 {
1159 *length = 0;
1160 }
1161
1162 if (!mLinked)
1163 {
1164 if (bufSize > 0)
1165 {
1166 name[0] = '\0';
1167 }
1168 return;
1169 }
jchen10baf5d942017-08-28 20:45:48 +08001170 ASSERT(index < resources.size());
1171 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001172
1173 if (bufSize > 0)
1174 {
Olli Etuahod2551232017-10-26 20:03:33 +03001175 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001176 }
1177}
1178
jchen10baf5d942017-08-28 20:45:48 +08001179void Program::getInputResourceName(GLuint index,
1180 GLsizei bufSize,
1181 GLsizei *length,
1182 GLchar *name) const
1183{
1184 getResourceName(index, mState.mAttributes, bufSize, length, name);
1185}
1186
1187void Program::getOutputResourceName(GLuint index,
1188 GLsizei bufSize,
1189 GLsizei *length,
1190 GLchar *name) const
1191{
1192 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1193}
1194
1195void Program::getUniformResourceName(GLuint index,
1196 GLsizei bufSize,
1197 GLsizei *length,
1198 GLchar *name) const
1199{
1200 getResourceName(index, mState.mUniforms, bufSize, length, name);
1201}
1202
jchen10880683b2017-04-12 16:21:55 +08001203const sh::Attribute &Program::getInputResource(GLuint index) const
1204{
1205 ASSERT(index < mState.mAttributes.size());
1206 return mState.mAttributes[index];
1207}
1208
1209const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1210{
1211 ASSERT(index < mState.mOutputVariables.size());
1212 return mState.mOutputVariables[index];
1213}
1214
Geoff Lang7dd2e102014-11-10 15:19:26 -05001215GLint Program::getFragDataLocation(const std::string &name) const
1216{
Olli Etuahod2551232017-10-26 20:03:33 +03001217 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001218}
1219
Geoff Lange1a27752015-10-05 13:16:04 -04001220void Program::getActiveUniform(GLuint index,
1221 GLsizei bufsize,
1222 GLsizei *length,
1223 GLint *size,
1224 GLenum *type,
1225 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001226{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001227 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001228 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001229 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001230 ASSERT(index < mState.mUniforms.size());
1231 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232
1233 if (bufsize > 0)
1234 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001235 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001236 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237 }
1238
Jamie Madill62d31cb2015-09-11 13:25:51 -04001239 *size = uniform.elementCount();
1240 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241 }
1242 else
1243 {
1244 if (bufsize > 0)
1245 {
1246 name[0] = '\0';
1247 }
1248
1249 if (length)
1250 {
1251 *length = 0;
1252 }
1253
1254 *size = 0;
1255 *type = GL_NONE;
1256 }
1257}
1258
Geoff Lange1a27752015-10-05 13:16:04 -04001259GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001260{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001261 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001262 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001263 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001264 }
1265 else
1266 {
1267 return 0;
1268 }
1269}
1270
Geoff Lange1a27752015-10-05 13:16:04 -04001271GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001272{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001273 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274
1275 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001276 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001277 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001279 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001281 size_t length = uniform.name.length() + 1u;
1282 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283 {
1284 length += 3; // Counting in "[0]".
1285 }
1286 maxLength = std::max(length, maxLength);
1287 }
1288 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290
Jamie Madill62d31cb2015-09-11 13:25:51 -04001291 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292}
1293
Geoff Lang7dd2e102014-11-10 15:19:26 -05001294bool Program::isValidUniformLocation(GLint location) const
1295{
Jamie Madille2e406c2016-06-02 13:04:10 -04001296 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001297 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001298 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001299}
1300
Jamie Madill62d31cb2015-09-11 13:25:51 -04001301const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001303 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001304 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305}
1306
Jamie Madillac4e9c32017-01-13 14:07:12 -05001307const VariableLocation &Program::getUniformLocation(GLint location) const
1308{
1309 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1310 return mState.mUniformLocations[location];
1311}
1312
1313const std::vector<VariableLocation> &Program::getUniformLocations() const
1314{
1315 return mState.mUniformLocations;
1316}
1317
1318const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1319{
1320 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1321 return mState.mUniforms[index];
1322}
1323
Jamie Madill62d31cb2015-09-11 13:25:51 -04001324GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325{
Olli Etuahod2551232017-10-26 20:03:33 +03001326 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001327}
1328
Jamie Madill62d31cb2015-09-11 13:25:51 -04001329GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330{
Jamie Madille7d84322017-01-10 18:21:59 -05001331 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
1334void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1335{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001336 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1337 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001338 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001339}
1340
1341void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1342{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001343 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1344 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001345 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346}
1347
1348void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1349{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001350 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1351 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001352 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
1355void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1356{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001357 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1358 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001359 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001360}
1361
Jamie Madill81c2e252017-09-09 23:32:46 -04001362Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001363{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001364 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1365 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1366
Jamie Madill81c2e252017-09-09 23:32:46 -04001367 mProgram->setUniform1iv(location, clampedCount, v);
1368
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001369 if (mState.isSamplerUniformIndex(locationInfo.index))
1370 {
1371 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001372 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001373 }
1374
Jamie Madill81c2e252017-09-09 23:32:46 -04001375 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376}
1377
1378void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1379{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001380 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1381 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001382 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383}
1384
1385void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1386{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001387 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1388 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001389 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390}
1391
1392void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1393{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001394 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1395 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001396 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397}
1398
1399void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1400{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001401 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1402 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001403 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404}
1405
1406void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1407{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001408 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1409 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001410 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411}
1412
1413void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1414{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001415 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1416 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001417 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418}
1419
1420void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1421{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001422 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1423 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001424 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425}
1426
1427void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1428{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001429 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001430 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431}
1432
1433void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1434{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001435 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001436 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437}
1438
1439void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1440{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001441 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001442 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443}
1444
1445void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1446{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001447 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001448 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449}
1450
1451void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1452{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001453 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001454 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455}
1456
1457void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1458{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001459 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001460 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461}
1462
1463void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1464{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001465 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001466 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467}
1468
1469void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1470{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001471 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001472 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001473}
1474
1475void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1476{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001477 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001478 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479}
1480
Jamie Madill54164b02017-08-28 15:17:37 -04001481void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001482{
Jamie Madill54164b02017-08-28 15:17:37 -04001483 const auto &uniformLocation = mState.getUniformLocations()[location];
1484 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1485
1486 GLenum nativeType = gl::VariableComponentType(uniform.type);
1487 if (nativeType == GL_FLOAT)
1488 {
1489 mProgram->getUniformfv(context, location, v);
1490 }
1491 else
1492 {
1493 getUniformInternal(context, v, location, nativeType,
1494 gl::VariableComponentCount(uniform.type));
1495 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496}
1497
Jamie Madill54164b02017-08-28 15:17:37 -04001498void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499{
Jamie Madill54164b02017-08-28 15:17:37 -04001500 const auto &uniformLocation = mState.getUniformLocations()[location];
1501 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1502
1503 GLenum nativeType = gl::VariableComponentType(uniform.type);
1504 if (nativeType == GL_INT || nativeType == GL_BOOL)
1505 {
1506 mProgram->getUniformiv(context, location, v);
1507 }
1508 else
1509 {
1510 getUniformInternal(context, v, location, nativeType,
1511 gl::VariableComponentCount(uniform.type));
1512 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513}
1514
Jamie Madill54164b02017-08-28 15:17:37 -04001515void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001516{
Jamie Madill54164b02017-08-28 15:17:37 -04001517 const auto &uniformLocation = mState.getUniformLocations()[location];
1518 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1519
1520 GLenum nativeType = gl::VariableComponentType(uniform.type);
1521 if (nativeType == GL_UNSIGNED_INT)
1522 {
1523 mProgram->getUniformuiv(context, location, v);
1524 }
1525 else
1526 {
1527 getUniformInternal(context, v, location, nativeType,
1528 gl::VariableComponentCount(uniform.type));
1529 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530}
1531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532void Program::flagForDeletion()
1533{
1534 mDeleteStatus = true;
1535}
1536
1537bool Program::isFlaggedForDeletion() const
1538{
1539 return mDeleteStatus;
1540}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001541
Brandon Jones43a53e22014-08-28 16:23:22 -07001542void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001543{
1544 mInfoLog.reset();
1545
Geoff Lang7dd2e102014-11-10 15:19:26 -05001546 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001547 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001548 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001549 }
1550 else
1551 {
Jamie Madillf6113162015-05-07 11:49:21 -04001552 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001553 }
1554}
1555
Geoff Lang7dd2e102014-11-10 15:19:26 -05001556bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1557{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001558 // Skip cache if we're using an infolog, so we get the full error.
1559 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1560 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1561 {
1562 return mCachedValidateSamplersResult.value();
1563 }
1564
1565 if (mTextureUnitTypesCache.empty())
1566 {
1567 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1568 }
1569 else
1570 {
1571 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1572 }
1573
1574 // if any two active samplers in a program are of different types, but refer to the same
1575 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1576 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001577 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001578 {
Jamie Madill54164b02017-08-28 15:17:37 -04001579 if (samplerBinding.unreferenced)
1580 continue;
1581
Jamie Madille7d84322017-01-10 18:21:59 -05001582 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001583
Jamie Madille7d84322017-01-10 18:21:59 -05001584 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001585 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001586 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1587 {
1588 if (infoLog)
1589 {
1590 (*infoLog) << "Sampler uniform (" << textureUnit
1591 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1592 << caps.maxCombinedTextureImageUnits << ")";
1593 }
1594
1595 mCachedValidateSamplersResult = false;
1596 return false;
1597 }
1598
1599 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1600 {
1601 if (textureType != mTextureUnitTypesCache[textureUnit])
1602 {
1603 if (infoLog)
1604 {
1605 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1606 "image unit ("
1607 << textureUnit << ").";
1608 }
1609
1610 mCachedValidateSamplersResult = false;
1611 return false;
1612 }
1613 }
1614 else
1615 {
1616 mTextureUnitTypesCache[textureUnit] = textureType;
1617 }
1618 }
1619 }
1620
1621 mCachedValidateSamplersResult = true;
1622 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623}
1624
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001625bool Program::isValidated() const
1626{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627 return mValidated;
1628}
1629
Geoff Lange1a27752015-10-05 13:16:04 -04001630GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001632 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001633}
1634
jchen1058f67be2017-10-27 08:59:27 +08001635GLuint Program::getActiveAtomicCounterBufferCount() const
1636{
1637 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1638}
1639
Jiajia Qin729b2c62017-08-14 09:36:11 +08001640GLuint Program::getActiveShaderStorageBlockCount() const
1641{
1642 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1643}
1644
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1646{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001647 ASSERT(
1648 uniformBlockIndex <
1649 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650
Jiajia Qin729b2c62017-08-14 09:36:11 +08001651 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652
1653 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001654 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655 std::string string = uniformBlock.name;
1656
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001660 }
jchen10fd7c3b52017-03-21 15:36:03 +08001661 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001662 }
1663}
1664
Geoff Lange1a27752015-10-05 13:16:04 -04001665GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001666{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 int maxLength = 0;
1668
1669 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001670 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001671 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1673 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001674 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675 if (!uniformBlock.name.empty())
1676 {
jchen10af713a22017-04-19 09:10:56 +08001677 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1678 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679 }
1680 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001681 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682
1683 return maxLength;
1684}
1685
Geoff Lange1a27752015-10-05 13:16:04 -04001686GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687{
Olli Etuahoc8538042017-09-27 11:20:15 +03001688 std::vector<unsigned int> subscripts;
1689 std::string baseName = ParseResourceName(name, &subscripts);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001690
Jamie Madill48ef11b2016-04-27 15:21:52 -04001691 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001692 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1693 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001694 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001695 if (uniformBlock.name == baseName)
1696 {
1697 const bool arrayElementZero =
Olli Etuahoc8538042017-09-27 11:20:15 +03001698 (subscripts.empty() && (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1699 const bool arrayElementMatches =
1700 (subscripts.size() == 1 && subscripts[0] == uniformBlock.arrayElement);
1701 if (arrayElementMatches || arrayElementZero)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001702 {
1703 return blockIndex;
1704 }
1705 }
1706 }
1707
1708 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001709}
1710
Jiajia Qin729b2c62017-08-14 09:36:11 +08001711const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001712{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001713 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1714 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001715}
1716
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001717void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1718{
jchen107a20b972017-06-13 14:25:26 +08001719 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001720 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001721 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001722}
1723
1724GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1725{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001726 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001727}
1728
Jiajia Qin729b2c62017-08-14 09:36:11 +08001729GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1730{
1731 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1732}
1733
Geoff Lang48dcae72014-02-05 16:28:24 -05001734void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1735{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001736 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001737 for (GLsizei i = 0; i < count; i++)
1738 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001739 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001740 }
1741
Jamie Madill48ef11b2016-04-27 15:21:52 -04001742 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001743}
1744
1745void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1746{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001747 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001748 {
jchen10a9042d32017-03-17 08:50:45 +08001749 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1750 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1751 std::string varName = var.nameWithArrayIndex();
1752 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001753 if (length)
1754 {
1755 *length = lastNameIdx;
1756 }
1757 if (size)
1758 {
jchen10a9042d32017-03-17 08:50:45 +08001759 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001760 }
1761 if (type)
1762 {
jchen10a9042d32017-03-17 08:50:45 +08001763 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001764 }
1765 if (name)
1766 {
jchen10a9042d32017-03-17 08:50:45 +08001767 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001768 name[lastNameIdx] = '\0';
1769 }
1770 }
1771}
1772
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001773GLsizei Program::getTransformFeedbackVaryingCount() const
1774{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001776 {
jchen10a9042d32017-03-17 08:50:45 +08001777 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001778 }
1779 else
1780 {
1781 return 0;
1782 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001783}
1784
1785GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1786{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001788 {
1789 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001790 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001791 {
jchen10a9042d32017-03-17 08:50:45 +08001792 maxSize =
1793 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001794 }
1795
1796 return maxSize;
1797 }
1798 else
1799 {
1800 return 0;
1801 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001802}
1803
1804GLenum Program::getTransformFeedbackBufferMode() const
1805{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001806 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807}
1808
Jamie Madillbd044ed2017-06-05 12:59:21 -04001809bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001811 Shader *vertexShader = mState.mAttachedVertexShader;
1812 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001813
Jamie Madillbd044ed2017-06-05 12:59:21 -04001814 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001815
Jiawei Shao3d404882017-10-16 13:30:48 +08001816 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
1817 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818
Sami Väisänen46eaa942016-06-29 10:26:37 +03001819 std::map<GLuint, std::string> staticFragmentInputLocations;
1820
Jamie Madill4cff2472015-08-21 16:53:18 -04001821 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 bool matched = false;
1824
1825 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001826 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827 {
1828 continue;
1829 }
1830
Jamie Madill4cff2472015-08-21 16:53:18 -04001831 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001833 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001835 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001836 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001837 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838 {
1839 return false;
1840 }
1841
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 matched = true;
1843 break;
1844 }
1845 }
1846
1847 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001848 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001850 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 return false;
1852 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001853
1854 // Check for aliased path rendering input bindings (if any).
1855 // If more than one binding refer statically to the same
1856 // location the link must fail.
1857
1858 if (!output.staticUse)
1859 continue;
1860
1861 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1862 if (inputBinding == -1)
1863 continue;
1864
1865 const auto it = staticFragmentInputLocations.find(inputBinding);
1866 if (it == std::end(staticFragmentInputLocations))
1867 {
1868 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1869 }
1870 else
1871 {
1872 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1873 << it->second;
1874 return false;
1875 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001876 }
1877
Jamie Madillbd044ed2017-06-05 12:59:21 -04001878 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001879 {
1880 return false;
1881 }
1882
Jamie Madillada9ecc2015-08-17 12:53:37 -04001883 // TODO(jmadill): verify no unmatched vertex varyings?
1884
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885 return true;
1886}
1887
Jamie Madillbd044ed2017-06-05 12:59:21 -04001888bool Program::linkUniforms(const Context *context,
1889 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001890 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001891{
Olli Etuahob78707c2017-03-09 15:03:11 +00001892 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001893 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001894 {
1895 return false;
1896 }
1897
Olli Etuahob78707c2017-03-09 15:03:11 +00001898 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001900 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001901
jchen10eaef1e52017-06-13 10:44:11 +08001902 if (!linkAtomicCounterBuffers())
1903 {
1904 return false;
1905 }
1906
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001907 return true;
1908}
1909
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001910void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001911{
Jamie Madill982f6e02017-06-07 14:33:04 -04001912 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1913 unsigned int low = high;
1914
jchen10eaef1e52017-06-13 10:44:11 +08001915 for (auto counterIter = mState.mUniforms.rbegin();
1916 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1917 {
1918 --low;
1919 }
1920
1921 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1922
1923 high = low;
1924
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001925 for (auto imageIter = mState.mUniforms.rbegin();
1926 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1927 {
1928 --low;
1929 }
1930
1931 mState.mImageUniformRange = RangeUI(low, high);
1932
1933 // If uniform is a image type, insert it into the mImageBindings array.
1934 for (unsigned int imageIndex : mState.mImageUniformRange)
1935 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001936 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1937 // cannot load values into a uniform defined as an image. if declare without a
1938 // binding qualifier, any uniform image variable (include all elements of
1939 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001940 auto &imageUniform = mState.mUniforms[imageIndex];
1941 if (imageUniform.binding == -1)
1942 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001943 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001944 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001945 else
1946 {
1947 mState.mImageBindings.emplace_back(
1948 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1949 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001950 }
1951
1952 high = low;
1953
1954 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001955 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001956 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001957 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001958 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001959
1960 mState.mSamplerUniformRange = RangeUI(low, high);
1961
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001962 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001963 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001964 {
1965 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1966 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1967 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001968 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001969 }
1970}
1971
jchen10eaef1e52017-06-13 10:44:11 +08001972bool Program::linkAtomicCounterBuffers()
1973{
1974 for (unsigned int index : mState.mAtomicCounterUniformRange)
1975 {
1976 auto &uniform = mState.mUniforms[index];
1977 bool found = false;
1978 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1979 ++bufferIndex)
1980 {
1981 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1982 if (buffer.binding == uniform.binding)
1983 {
1984 buffer.memberIndexes.push_back(index);
1985 uniform.bufferIndex = bufferIndex;
1986 found = true;
jchen1058f67be2017-10-27 08:59:27 +08001987 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08001988 break;
1989 }
1990 }
1991 if (!found)
1992 {
1993 AtomicCounterBuffer atomicCounterBuffer;
1994 atomicCounterBuffer.binding = uniform.binding;
1995 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08001996 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08001997 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1998 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1999 }
2000 }
2001 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2002 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2003
2004 return true;
2005}
2006
Martin Radev4c4c8e72016-08-04 12:25:34 +03002007bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2008 const std::string &uniformName,
2009 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002010 const sh::InterfaceBlockField &fragmentUniform,
2011 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002013 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2014 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2015 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002016 {
2017 return false;
2018 }
2019
2020 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2021 {
Jamie Madillf6113162015-05-07 11:49:21 -04002022 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023 return false;
2024 }
2025
2026 return true;
2027}
2028
Jamie Madilleb979bf2016-11-15 12:28:46 -05002029// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002030bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002032 const ContextState &data = context->getContextState();
2033 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002034
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002036 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002037 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002038
2039 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002040 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002041 {
Jamie Madillf6113162015-05-07 11:49:21 -04002042 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002043 return false;
2044 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002046 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002047
Jamie Madillc349ec02015-08-21 16:53:12 -04002048 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002049 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002050 {
Olli Etuahod2551232017-10-26 20:03:33 +03002051 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2052 // structures, so we don't need to worry about adjusting their names or generating entries
2053 // for each member/element (unlike uniforms for example).
2054 ASSERT(!attribute.isArray() && !attribute.isStruct());
2055
Jamie Madilleb979bf2016-11-15 12:28:46 -05002056 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002057 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002058 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002059 attribute.location = bindingLocation;
2060 }
2061
2062 if (attribute.location != -1)
2063 {
2064 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002065 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066
Jamie Madill63805b42015-08-25 13:17:39 -04002067 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 {
Jamie Madillf6113162015-05-07 11:49:21 -04002069 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071
2072 return false;
2073 }
2074
Jamie Madill63805b42015-08-25 13:17:39 -04002075 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076 {
Jamie Madill63805b42015-08-25 13:17:39 -04002077 const int regLocation = attribute.location + reg;
2078 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079
2080 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002081 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002082 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002084 // TODO(jmadill): fix aliasing on ES2
2085 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002087 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002088 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 return false;
2090 }
2091 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002092 else
2093 {
Jamie Madill63805b42015-08-25 13:17:39 -04002094 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002095 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096
Jamie Madill63805b42015-08-25 13:17:39 -04002097 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 }
2099 }
2100 }
2101
2102 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002103 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002105 // Not set by glBindAttribLocation or by location layout qualifier
2106 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 {
Jamie Madill63805b42015-08-25 13:17:39 -04002108 int regs = VariableRegisterCount(attribute.type);
2109 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110
Jamie Madill63805b42015-08-25 13:17:39 -04002111 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 {
Jamie Madillf6113162015-05-07 11:49:21 -04002113 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002114 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115 }
2116
Jamie Madillc349ec02015-08-21 16:53:12 -04002117 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118 }
2119 }
2120
Jamie Madill48ef11b2016-04-27 15:21:52 -04002121 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122 {
Jamie Madill63805b42015-08-25 13:17:39 -04002123 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002124 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002125
Jamie Madillbd159f02017-10-09 19:39:06 -04002126 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002128 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2129 mState.mActiveAttribLocationsMask.set(location);
2130 mState.mMaxActiveAttribLocation =
2131 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132 }
2133 }
2134
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 return true;
2136}
2137
Martin Radev4c4c8e72016-08-04 12:25:34 +03002138bool Program::validateVertexAndFragmentInterfaceBlocks(
2139 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2140 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002141 InfoLog &infoLog,
2142 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002143{
2144 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002145 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2146 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002147
2148 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2149 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002150 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002151 }
2152
Jamie Madille473dee2015-08-18 14:49:01 -04002153 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002155 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2156 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 {
2158 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002159 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2160 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 {
2162 return false;
2163 }
2164 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002165 // TODO(jiajia.qin@intel.com): Add
2166 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002167 }
2168 return true;
2169}
Jamie Madille473dee2015-08-18 14:49:01 -04002170
Jiajia Qin729b2c62017-08-14 09:36:11 +08002171bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002172{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002173 const auto &caps = context->getCaps();
2174
Martin Radev4c4c8e72016-08-04 12:25:34 +03002175 if (mState.mAttachedComputeShader)
2176 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002177 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002178 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002179
Jiajia Qin729b2c62017-08-14 09:36:11 +08002180 if (!validateInterfaceBlocksCount(
2181 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002182 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2183 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002184 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002186 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002187
2188 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2189 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2190 computeShaderStorageBlocks,
2191 "Compute shader shader storage block count exceeds "
2192 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2193 infoLog))
2194 {
2195 return false;
2196 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002197 return true;
2198 }
2199
Jamie Madillbd044ed2017-06-05 12:59:21 -04002200 Shader &vertexShader = *mState.mAttachedVertexShader;
2201 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002202
Jiajia Qin729b2c62017-08-14 09:36:11 +08002203 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2204 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002205
Jiajia Qin729b2c62017-08-14 09:36:11 +08002206 if (!validateInterfaceBlocksCount(
2207 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002208 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2209 {
2210 return false;
2211 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002212 if (!validateInterfaceBlocksCount(
2213 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002214 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2215 infoLog))
2216 {
2217
2218 return false;
2219 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002220
2221 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002222 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002223 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002224 {
2225 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002226 }
Jamie Madille473dee2015-08-18 14:49:01 -04002227
Jiajia Qin729b2c62017-08-14 09:36:11 +08002228 if (context->getClientVersion() >= Version(3, 1))
2229 {
2230 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2231 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2232
2233 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2234 vertexShaderStorageBlocks,
2235 "Vertex shader shader storage block count exceeds "
2236 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2237 infoLog))
2238 {
2239 return false;
2240 }
2241 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2242 fragmentShaderStorageBlocks,
2243 "Fragment shader shader storage block count exceeds "
2244 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2245 infoLog))
2246 {
2247
2248 return false;
2249 }
2250
2251 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2252 fragmentShaderStorageBlocks, infoLog,
2253 webglCompatibility))
2254 {
2255 return false;
2256 }
2257 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002258 return true;
2259}
2260
Jamie Madilla2c74982016-12-12 11:20:42 -05002261bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002262 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002263 const sh::InterfaceBlock &fragmentInterfaceBlock,
2264 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265{
2266 const char* blockName = vertexInterfaceBlock.name.c_str();
2267 // validate blocks for the same member types
2268 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2269 {
Jamie Madillf6113162015-05-07 11:49:21 -04002270 infoLog << "Types for interface block '" << blockName
2271 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002272 return false;
2273 }
2274 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2275 {
Jamie Madillf6113162015-05-07 11:49:21 -04002276 infoLog << "Array sizes differ for interface block '" << blockName
2277 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 return false;
2279 }
jchen10af713a22017-04-19 09:10:56 +08002280 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2281 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2282 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002283 {
Jamie Madillf6113162015-05-07 11:49:21 -04002284 infoLog << "Layout qualifiers differ for interface block '" << blockName
2285 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002286 return false;
2287 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002288 const unsigned int numBlockMembers =
2289 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002290 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2291 {
2292 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2293 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2294 if (vertexMember.name != fragmentMember.name)
2295 {
Jamie Madillf6113162015-05-07 11:49:21 -04002296 infoLog << "Name mismatch for field " << blockMemberIndex
2297 << " of interface block '" << blockName
2298 << "': (in vertex: '" << vertexMember.name
2299 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300 return false;
2301 }
2302 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002303 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2304 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002305 {
2306 return false;
2307 }
2308 }
2309 return true;
2310}
2311
2312bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2313 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2314{
2315 if (vertexVariable.type != fragmentVariable.type)
2316 {
Jamie Madillf6113162015-05-07 11:49:21 -04002317 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002318 return false;
2319 }
2320 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2321 {
Jamie Madillf6113162015-05-07 11:49:21 -04002322 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323 return false;
2324 }
2325 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2326 {
Jamie Madillf6113162015-05-07 11:49:21 -04002327 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002328 return false;
2329 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002330 if (vertexVariable.structName != fragmentVariable.structName)
2331 {
2332 infoLog << "Structure names for " << variableName
2333 << " differ between vertex and fragment shaders";
2334 return false;
2335 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002336
2337 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2338 {
Jamie Madillf6113162015-05-07 11:49:21 -04002339 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002340 return false;
2341 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002342 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2344 {
2345 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2346 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2347
2348 if (vertexMember.name != fragmentMember.name)
2349 {
Jamie Madillf6113162015-05-07 11:49:21 -04002350 infoLog << "Name mismatch for field '" << memberIndex
2351 << "' of " << variableName
2352 << ": (in vertex: '" << vertexMember.name
2353 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 return false;
2355 }
2356
2357 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2358 vertexMember.name + "'";
2359
2360 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2361 {
2362 return false;
2363 }
2364 }
2365
2366 return true;
2367}
2368
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002369bool Program::linkValidateVaryings(InfoLog &infoLog,
2370 const std::string &varyingName,
2371 const sh::Varying &vertexVarying,
2372 const sh::Varying &fragmentVarying,
2373 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374{
2375 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2376 {
2377 return false;
2378 }
2379
Jamie Madille9cc4692015-02-19 16:00:13 -05002380 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002381 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002382 infoLog << "Interpolation types for " << varyingName
2383 << " differ between vertex and fragment shaders.";
2384 return false;
2385 }
2386
2387 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2388 {
2389 infoLog << "Invariance for " << varyingName
2390 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 return false;
2392 }
2393
2394 return true;
2395}
2396
Jamie Madillbd044ed2017-06-05 12:59:21 -04002397bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002398{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002399 Shader *vertexShader = mState.mAttachedVertexShader;
2400 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002401 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2402 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002403 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002404
2405 if (shaderVersion != 100)
2406 {
2407 // Only ESSL 1.0 has restrictions on matching input and output invariance
2408 return true;
2409 }
2410
2411 bool glPositionIsInvariant = false;
2412 bool glPointSizeIsInvariant = false;
2413 bool glFragCoordIsInvariant = false;
2414 bool glPointCoordIsInvariant = false;
2415
2416 for (const sh::Varying &varying : vertexVaryings)
2417 {
2418 if (!varying.isBuiltIn())
2419 {
2420 continue;
2421 }
2422 if (varying.name.compare("gl_Position") == 0)
2423 {
2424 glPositionIsInvariant = varying.isInvariant;
2425 }
2426 else if (varying.name.compare("gl_PointSize") == 0)
2427 {
2428 glPointSizeIsInvariant = varying.isInvariant;
2429 }
2430 }
2431
2432 for (const sh::Varying &varying : fragmentVaryings)
2433 {
2434 if (!varying.isBuiltIn())
2435 {
2436 continue;
2437 }
2438 if (varying.name.compare("gl_FragCoord") == 0)
2439 {
2440 glFragCoordIsInvariant = varying.isInvariant;
2441 }
2442 else if (varying.name.compare("gl_PointCoord") == 0)
2443 {
2444 glPointCoordIsInvariant = varying.isInvariant;
2445 }
2446 }
2447
2448 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2449 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2450 // Not requiring invariance to match is supported by:
2451 // dEQP, WebGL CTS, Nexus 5X GLES
2452 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2453 {
2454 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2455 "declared invariant.";
2456 return false;
2457 }
2458 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2459 {
2460 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2461 "declared invariant.";
2462 return false;
2463 }
2464
2465 return true;
2466}
2467
jchen10a9042d32017-03-17 08:50:45 +08002468bool Program::linkValidateTransformFeedback(const gl::Context *context,
2469 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002470 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002471 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472{
2473 size_t totalComponents = 0;
2474
Jamie Madillccdf74b2015-08-18 10:46:12 -04002475 std::set<std::string> uniqueNames;
2476
Jamie Madill48ef11b2016-04-27 15:21:52 -04002477 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002478 {
2479 bool found = false;
Olli Etuahoc8538042017-09-27 11:20:15 +03002480 std::vector<unsigned int> subscripts;
2481 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002482
Jamie Madill192745a2016-12-22 15:58:21 -05002483 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002484 {
Jamie Madill192745a2016-12-22 15:58:21 -05002485 const sh::Varying *varying = ref.second.get();
2486
jchen10a9042d32017-03-17 08:50:45 +08002487 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002488 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002489 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002491 infoLog << "Two transform feedback varyings specify the same output variable ("
2492 << tfVaryingName << ").";
2493 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494 }
jchen10a9042d32017-03-17 08:50:45 +08002495 if (context->getClientVersion() >= Version(3, 1))
2496 {
2497 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2498 {
2499 infoLog
2500 << "Two transform feedback varyings include the same array element ("
2501 << tfVaryingName << ").";
2502 return false;
2503 }
2504 }
2505 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002506 {
2507 infoLog << "Capture of arrays is undefined and not supported.";
2508 return false;
2509 }
2510
jchen10a9042d32017-03-17 08:50:45 +08002511 uniqueNames.insert(tfVaryingName);
2512
Jamie Madillccdf74b2015-08-18 10:46:12 -04002513 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002514 size_t elementCount =
Olli Etuahoc8538042017-09-27 11:20:15 +03002515 ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1);
jchen10a9042d32017-03-17 08:50:45 +08002516 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002517 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002518 componentCount > caps.maxTransformFeedbackSeparateComponents)
2519 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002520 infoLog << "Transform feedback varying's " << varying->name << " components ("
2521 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002522 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002523 return false;
2524 }
2525
2526 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002527 found = true;
2528 break;
2529 }
2530 }
jchen10a9042d32017-03-17 08:50:45 +08002531 if (context->getClientVersion() < Version(3, 1) &&
2532 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002533 {
Geoff Lang1a683462015-09-29 15:09:59 -04002534 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002535 return false;
2536 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002537 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2538 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002539 ASSERT(found);
2540 }
2541
Jamie Madill48ef11b2016-04-27 15:21:52 -04002542 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002543 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002544 {
Jamie Madillf6113162015-05-07 11:49:21 -04002545 infoLog << "Transform feedback varying total components (" << totalComponents
2546 << ") exceed the maximum interleaved components ("
2547 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002548 return false;
2549 }
2550
2551 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002552}
2553
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002554bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2555{
2556 const std::vector<sh::Uniform> &vertexUniforms =
2557 mState.mAttachedVertexShader->getUniforms(context);
2558 const std::vector<sh::Uniform> &fragmentUniforms =
2559 mState.mAttachedFragmentShader->getUniforms(context);
2560 const std::vector<sh::Attribute> &attributes =
2561 mState.mAttachedVertexShader->getActiveAttributes(context);
2562 for (const auto &attrib : attributes)
2563 {
2564 for (const auto &uniform : vertexUniforms)
2565 {
2566 if (uniform.name == attrib.name)
2567 {
2568 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2569 return false;
2570 }
2571 }
2572 for (const auto &uniform : fragmentUniforms)
2573 {
2574 if (uniform.name == attrib.name)
2575 {
2576 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2577 return false;
2578 }
2579 }
2580 }
2581 return true;
2582}
2583
Jamie Madill192745a2016-12-22 15:58:21 -05002584void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002585{
2586 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002587 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002588 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002589 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002590 std::vector<unsigned int> subscripts;
2591 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002592 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002593 if (!subscripts.empty())
2594 {
2595 subscript = subscripts.back();
2596 }
Jamie Madill192745a2016-12-22 15:58:21 -05002597 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002598 {
Jamie Madill192745a2016-12-22 15:58:21 -05002599 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002600 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002601 {
jchen10a9042d32017-03-17 08:50:45 +08002602 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2603 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002604 break;
2605 }
2606 }
2607 }
2608}
2609
Jamie Madillbd044ed2017-06-05 12:59:21 -04002610Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002611{
Jamie Madill192745a2016-12-22 15:58:21 -05002612 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002613
Jiawei Shao3d404882017-10-16 13:30:48 +08002614 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002615 {
Jamie Madill192745a2016-12-22 15:58:21 -05002616 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002617 }
2618
Jiawei Shao3d404882017-10-16 13:30:48 +08002619 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002620 {
Jamie Madill192745a2016-12-22 15:58:21 -05002621 merged[varying.name].fragment = &varying;
2622 }
2623
2624 return merged;
2625}
2626
2627std::vector<PackedVarying> Program::getPackedVaryings(
2628 const Program::MergedVaryings &mergedVaryings) const
2629{
2630 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2631 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002632 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002633
2634 for (const auto &ref : mergedVaryings)
2635 {
2636 const sh::Varying *input = ref.second.vertex;
2637 const sh::Varying *output = ref.second.fragment;
2638
2639 // Only pack varyings that have a matched input or output, plus special builtins.
2640 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002641 {
Jamie Madill192745a2016-12-22 15:58:21 -05002642 // Will get the vertex shader interpolation by default.
2643 auto interpolation = ref.second.get()->interpolation;
2644
Olli Etuaho06a06f52017-07-12 12:22:15 +03002645 // Note that we lose the vertex shader static use information here. The data for the
2646 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002647 if (output->isStruct())
2648 {
2649 ASSERT(!output->isArray());
2650 for (const auto &field : output->fields)
2651 {
2652 ASSERT(!field.isStruct() && !field.isArray());
2653 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2654 }
2655 }
2656 else
2657 {
2658 packedVaryings.push_back(PackedVarying(*output, interpolation));
2659 }
2660 continue;
2661 }
2662
2663 // Keep Transform FB varyings in the merged list always.
2664 if (!input)
2665 {
2666 continue;
2667 }
2668
2669 for (const std::string &tfVarying : tfVaryings)
2670 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002671 std::vector<unsigned int> subscripts;
2672 std::string baseName = ParseResourceName(tfVarying, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002673 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002674 if (!subscripts.empty())
2675 {
2676 subscript = subscripts.back();
2677 }
jchen10a9042d32017-03-17 08:50:45 +08002678 if (uniqueFullNames.count(tfVarying) > 0)
2679 {
2680 continue;
2681 }
2682 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002683 {
2684 // Transform feedback for varying structs is underspecified.
2685 // See Khronos bug 9856.
2686 // TODO(jmadill): Figure out how to be spec-compliant here.
2687 if (!input->isStruct())
2688 {
2689 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2690 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002691 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2692 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002693 }
jchen10a9042d32017-03-17 08:50:45 +08002694 if (subscript == GL_INVALID_INDEX)
2695 {
2696 break;
2697 }
Jamie Madill192745a2016-12-22 15:58:21 -05002698 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002699 }
2700 }
2701
Jamie Madill192745a2016-12-22 15:58:21 -05002702 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2703
2704 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002705}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002706
Jamie Madillbd044ed2017-06-05 12:59:21 -04002707void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002708{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002709 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002710 ASSERT(fragmentShader != nullptr);
2711
Geoff Lange0cff192017-05-30 13:04:56 -04002712 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002713 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002714
2715 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002716 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002717 {
2718 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2719 outputVariable.name != "gl_FragData")
2720 {
2721 continue;
2722 }
2723
2724 unsigned int baseLocation =
2725 (outputVariable.location == -1 ? 0u
2726 : static_cast<unsigned int>(outputVariable.location));
2727 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2728 elementIndex++)
2729 {
2730 const unsigned int location = baseLocation + elementIndex;
2731 if (location >= mState.mOutputVariableTypes.size())
2732 {
2733 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2734 }
Corentin Walleze7557742017-06-01 13:09:57 -04002735 ASSERT(location < mState.mActiveOutputVariables.size());
2736 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002737 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2738 }
2739 }
2740
Jamie Madill80a6fc02015-08-21 16:53:16 -04002741 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002742 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002743 return;
2744
Jamie Madillbd044ed2017-06-05 12:59:21 -04002745 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002746 // TODO(jmadill): any caps validation here?
2747
jchen1015015f72017-03-16 13:54:21 +08002748 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002749 outputVariableIndex++)
2750 {
jchen1015015f72017-03-16 13:54:21 +08002751 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002752
Olli Etuahod2551232017-10-26 20:03:33 +03002753 if (outputVariable.isArray())
2754 {
2755 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
2756 // Resources and including [0] at the end of array variable names.
2757 mState.mOutputVariables[outputVariableIndex].name += "[0]";
2758 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
2759 }
2760
Jamie Madill80a6fc02015-08-21 16:53:16 -04002761 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2762 if (outputVariable.isBuiltIn())
2763 continue;
2764
2765 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03002766 unsigned int baseLocation =
2767 (outputVariable.location == -1 ? 0u
2768 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04002769
Jamie Madill80a6fc02015-08-21 16:53:16 -04002770 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2771 elementIndex++)
2772 {
Olli Etuahod2551232017-10-26 20:03:33 +03002773 const unsigned int location = baseLocation + elementIndex;
2774 if (location >= mState.mOutputLocations.size())
2775 {
2776 mState.mOutputLocations.resize(location + 1);
2777 }
2778 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03002779 if (outputVariable.isArray())
2780 {
2781 mState.mOutputLocations[location] =
2782 VariableLocation(elementIndex, outputVariableIndex);
2783 }
2784 else
2785 {
2786 VariableLocation locationInfo;
2787 locationInfo.index = outputVariableIndex;
2788 mState.mOutputLocations[location] = locationInfo;
2789 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002790 }
2791 }
2792}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002793
Olli Etuaho48fed632017-03-16 12:05:30 +00002794void Program::setUniformValuesFromBindingQualifiers()
2795{
Jamie Madill982f6e02017-06-07 14:33:04 -04002796 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002797 {
2798 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2799 if (samplerUniform.binding != -1)
2800 {
Olli Etuahod2551232017-10-26 20:03:33 +03002801 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00002802 ASSERT(location != -1);
2803 std::vector<GLint> boundTextureUnits;
2804 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2805 ++elementIndex)
2806 {
2807 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2808 }
2809 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2810 boundTextureUnits.data());
2811 }
2812 }
2813}
2814
jchen10eaef1e52017-06-13 10:44:11 +08002815void Program::gatherAtomicCounterBuffers()
2816{
jchen10baf5d942017-08-28 20:45:48 +08002817 for (unsigned int index : mState.mAtomicCounterUniformRange)
2818 {
2819 auto &uniform = mState.mUniforms[index];
2820 uniform.blockInfo.offset = uniform.offset;
2821 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2822 uniform.blockInfo.matrixStride = 0;
2823 uniform.blockInfo.isRowMajorMatrix = false;
2824 }
2825
jchen10eaef1e52017-06-13 10:44:11 +08002826 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2827}
2828
Jamie Madill977abce2017-11-07 08:03:19 -05002829void Program::gatherUniformBlockInfo(const gl::Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002830{
Jamie Madill977abce2017-11-07 08:03:19 -05002831 UniformBlockLinker blockLinker(&mState.mUniformBlocks, &mState.mUniforms);
2832
2833 if (mState.mAttachedVertexShader)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002834 {
Jamie Madill977abce2017-11-07 08:03:19 -05002835 blockLinker.addShaderBlocks(GL_VERTEX_SHADER,
2836 &mState.mAttachedVertexShader->getUniformBlocks(context));
Martin Radev4c4c8e72016-08-04 12:25:34 +03002837 }
Jamie Madill977abce2017-11-07 08:03:19 -05002838
2839 if (mState.mAttachedFragmentShader)
2840 {
2841 blockLinker.addShaderBlocks(GL_FRAGMENT_SHADER,
2842 &mState.mAttachedFragmentShader->getUniformBlocks(context));
2843 }
2844
2845 if (mState.mAttachedComputeShader)
2846 {
2847 blockLinker.addShaderBlocks(GL_COMPUTE_SHADER,
2848 &mState.mAttachedComputeShader->getUniformBlocks(context));
2849 }
2850
2851 auto getImplBlockSize = [this](const std::string &name, const std::string &mappedName,
2852 size_t *sizeOut) {
2853 return this->mProgram->getUniformBlockSize(name, mappedName, sizeOut);
2854 };
2855
2856 auto getImplMemberInfo = [this](const std::string &name, const std::string &mappedName,
2857 sh::BlockMemberInfo *infoOut) {
2858 return this->mProgram->getUniformBlockMemberInfo(name, mappedName, infoOut);
2859 };
2860
2861 blockLinker.linkBlocks(getImplBlockSize, getImplMemberInfo);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002862}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002863
Jamie Madill977abce2017-11-07 08:03:19 -05002864void Program::gatherShaderStorageBlockInfo(const gl::Context *context)
Jiajia Qin729b2c62017-08-14 09:36:11 +08002865{
Jamie Madill977abce2017-11-07 08:03:19 -05002866 ShaderStorageBlockLinker blockLinker(&mState.mShaderStorageBlocks);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002867
Jamie Madill977abce2017-11-07 08:03:19 -05002868 if (mState.mAttachedVertexShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002869 {
Jamie Madill977abce2017-11-07 08:03:19 -05002870 blockLinker.addShaderBlocks(GL_VERTEX_SHADER,
2871 &mState.mAttachedVertexShader->getShaderStorageBlocks(context));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002872 }
2873
Jamie Madill977abce2017-11-07 08:03:19 -05002874 if (mState.mAttachedFragmentShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002875 {
Jamie Madill977abce2017-11-07 08:03:19 -05002876 blockLinker.addShaderBlocks(
2877 GL_FRAGMENT_SHADER, &mState.mAttachedFragmentShader->getShaderStorageBlocks(context));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002878 }
Jamie Madill977abce2017-11-07 08:03:19 -05002879
2880 if (mState.mAttachedComputeShader)
2881 {
2882 blockLinker.addShaderBlocks(
2883 GL_COMPUTE_SHADER, &mState.mAttachedComputeShader->getShaderStorageBlocks(context));
2884 }
2885
2886 // We don't have a way of determining block info for shader storage blocks yet.
2887 // TODO(jiajia.qin@intel.com): Determine correct block size and layout.
2888 auto getImplBlockSize = [this](const std::string &name, const std::string &mappedName,
2889 size_t *sizeOut) {
2890 return this->mProgram->getUniformBlockSize(name, mappedName, sizeOut);
2891 };
2892
2893 auto getImplMemberInfo = [this](const std::string &name, const std::string &mappedName,
2894 sh::BlockMemberInfo *infoOut) {
2895 return this->mProgram->getUniformBlockMemberInfo(name, mappedName, infoOut);
2896 };
2897
2898 blockLinker.linkBlocks(getImplBlockSize, getImplMemberInfo);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002899}
2900
2901void Program::gatherInterfaceBlockInfo(const Context *context)
2902{
2903 ASSERT(mState.mUniformBlocks.empty());
2904 ASSERT(mState.mShaderStorageBlocks.empty());
2905
Jamie Madill977abce2017-11-07 08:03:19 -05002906 gatherUniformBlockInfo(context);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002907 if (context->getClientVersion() >= Version(3, 1))
2908 {
Jamie Madill977abce2017-11-07 08:03:19 -05002909 gatherShaderStorageBlockInfo(context);
Jiajia Qin729b2c62017-08-14 09:36:11 +08002910 }
2911
jchen10af713a22017-04-19 09:10:56 +08002912 // Set initial bindings from shader.
2913 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2914 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002915 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002916 bindUniformBlock(blockIndex, uniformBlock.binding);
2917 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002918}
2919
Jamie Madille7d84322017-01-10 18:21:59 -05002920void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002921 GLsizei clampedCount,
2922 const GLint *v)
2923{
Jamie Madill81c2e252017-09-09 23:32:46 -04002924 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
2925 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2926 std::vector<GLuint> *boundTextureUnits =
2927 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05002928
Olli Etuahoc8538042017-09-27 11:20:15 +03002929 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.flattenedArrayOffset);
Jamie Madilld68248b2017-09-11 14:34:14 -04002930
2931 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04002932 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05002933}
2934
2935template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002936GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2937 GLsizei count,
2938 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002939 const T *v)
2940{
Jamie Madill134f93d2017-08-31 17:11:00 -04002941 if (count == 1)
2942 return 1;
2943
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002944 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002945
Corentin Wallez15ac5342016-11-03 17:06:39 -04002946 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2947 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03002948 unsigned int remainingElements =
2949 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002950 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002951 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002952
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002953 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002954 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002955 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002956 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002957
2958 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002959}
2960
2961template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002962GLsizei Program::clampMatrixUniformCount(GLint location,
2963 GLsizei count,
2964 GLboolean transpose,
2965 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002966{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002967 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2968
Jamie Madill62d31cb2015-09-11 13:25:51 -04002969 if (!transpose)
2970 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002971 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002972 }
2973
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002974 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04002975
2976 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2977 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuahoc8538042017-09-27 11:20:15 +03002978 unsigned int remainingElements =
2979 linkedUniform.elementCount() - locationInfo.flattenedArrayOffset;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002980 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002981}
2982
Jamie Madill54164b02017-08-28 15:17:37 -04002983// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
2984// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002985template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04002986void Program::getUniformInternal(const Context *context,
2987 DestT *dataOut,
2988 GLint location,
2989 GLenum nativeType,
2990 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04002991{
Jamie Madill54164b02017-08-28 15:17:37 -04002992 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002993 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002994 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04002995 {
2996 GLint tempValue[16] = {0};
2997 mProgram->getUniformiv(context, location, tempValue);
2998 UniformStateQueryCastLoop<GLboolean>(
2999 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003000 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003001 }
3002 case GL_INT:
3003 {
3004 GLint tempValue[16] = {0};
3005 mProgram->getUniformiv(context, location, tempValue);
3006 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3007 components);
3008 break;
3009 }
3010 case GL_UNSIGNED_INT:
3011 {
3012 GLuint tempValue[16] = {0};
3013 mProgram->getUniformuiv(context, location, tempValue);
3014 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3015 components);
3016 break;
3017 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003018 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003019 {
3020 GLfloat tempValue[16] = {0};
3021 mProgram->getUniformfv(context, location, tempValue);
3022 UniformStateQueryCastLoop<GLfloat>(
3023 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003024 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003025 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003026 default:
3027 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003028 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003029 }
3030}
Jamie Madilla4595b82017-01-11 17:36:34 -05003031
3032bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3033{
3034 // Must be called after samplers are validated.
3035 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3036
3037 for (const auto &binding : mState.mSamplerBindings)
3038 {
3039 GLenum textureType = binding.textureType;
3040 for (const auto &unit : binding.boundTextureUnits)
3041 {
3042 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3043 if (programTextureID == textureID)
3044 {
3045 // TODO(jmadill): Check for appropriate overlap.
3046 return true;
3047 }
3048 }
3049 }
3050
3051 return false;
3052}
3053
Jamie Madilla2c74982016-12-12 11:20:42 -05003054} // namespace gl