blob: c5a379cba20f4a034224b1d90c0cc4d0ef572419 [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 Madill7af0de52017-11-06 17:09:33 -050022#include "libANGLE/ProgramLinkedResources.h"
Jamie Madill437d2662014-12-05 14:23:35 -050023#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040024#include "libANGLE/Uniform.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{
Geoff Lang92019432017-11-20 13:09:34 -050074 return (ConvertToBool(value) ? 1.0f : 0.0f);
Jamie Madill62d31cb2015-09-11 13:25:51 -040075}
76
77template <>
78GLint UniformStateQueryCast(GLboolean value)
79{
Geoff Lang92019432017-11-20 13:09:34 -050080 return (ConvertToBool(value) ? 1 : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -040081}
82
83template <>
84GLuint UniformStateQueryCast(GLboolean value)
85{
Geoff Lang92019432017-11-20 13:09:34 -050086 return (ConvertToBool(value) ? 1u : 0u);
Jamie Madill62d31cb2015-09-11 13:25:51 -040087}
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
jchen1015015f72017-03-16 13:54:21 +0800110template <typename VarT>
111GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
112{
Olli Etuahod2551232017-10-26 20:03:33 +0300113 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800114 for (size_t index = 0; index < list.size(); index++)
115 {
116 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300117 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800118 {
Olli Etuahod2551232017-10-26 20:03:33 +0300119 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800120 }
121 }
122
123 return GL_INVALID_INDEX;
124}
125
Olli Etuahod2551232017-10-26 20:03:33 +0300126template <typename VarT>
127GLint GetVariableLocation(const std::vector<VarT> &list,
128 const std::vector<VariableLocation> &locationList,
129 const std::string &name)
130{
131 size_t nameLengthWithoutArrayIndex;
132 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
133
134 for (size_t location = 0u; location < locationList.size(); ++location)
135 {
136 const VariableLocation &variableLocation = locationList[location];
137 if (!variableLocation.used())
138 {
139 continue;
140 }
141
142 const VarT &variable = list[variableLocation.index];
143
144 if (angle::BeginsWith(variable.name, name))
145 {
146 if (name.length() == variable.name.length())
147 {
148 ASSERT(name == variable.name);
149 // GLES 3.1 November 2016 page 87.
150 // The string exactly matches the name of the active variable.
151 return static_cast<GLint>(location);
152 }
153 if (name.length() + 3u == variable.name.length() && variable.isArray())
154 {
155 ASSERT(name + "[0]" == variable.name);
156 // The string identifies the base name of an active array, where the string would
157 // exactly match the name of the variable if the suffix "[0]" were appended to the
158 // string.
159 return static_cast<GLint>(location);
160 }
161 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300162 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300163 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
164 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
165 {
166 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
167 // The string identifies an active element of the array, where the string ends with the
168 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
169 // zeroes, or whitespace) identifying an array element, and the "]" character, the
170 // integer is less than the number of active elements of the array variable, and where
171 // the string would exactly match the enumerated name of the array if the decimal
172 // integer were replaced with zero.
173 return static_cast<GLint>(location);
174 }
175 }
176
177 return -1;
178}
179
jchen10fd7c3b52017-03-21 15:36:03 +0800180void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
181{
182 ASSERT(bufSize > 0);
183 strncpy(buffer, string.c_str(), bufSize);
184 buffer[bufSize - 1] = '\0';
185
186 if (length)
187 {
188 *length = static_cast<GLsizei>(strlen(buffer));
189 }
190}
191
jchen10a9042d32017-03-17 08:50:45 +0800192bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
193{
Olli Etuahoc8538042017-09-27 11:20:15 +0300194 std::vector<unsigned int> subscripts;
195 std::string baseName = ParseResourceName(name, &subscripts);
196 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800197 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300198 std::vector<unsigned int> arrayIndices;
199 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
200 if (baseName == arrayName &&
201 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800202 {
203 return true;
204 }
205 }
206 return false;
207}
208
Jiajia Qin729b2c62017-08-14 09:36:11 +0800209bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
210 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
211 const std::string &errorMessage,
212 InfoLog &infoLog)
213{
214 GLuint blockCount = 0;
215 for (const sh::InterfaceBlock &block : interfaceBlocks)
216 {
217 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
218 {
219 blockCount += (block.arraySize ? block.arraySize : 1);
220 if (blockCount > maxInterfaceBlocks)
221 {
222 infoLog << errorMessage << maxInterfaceBlocks << ")";
223 return false;
224 }
225 }
226 }
227 return true;
228}
229
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800230GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
231{
232 std::vector<unsigned int> subscripts;
233 std::string baseName = ParseResourceName(name, &subscripts);
234
235 unsigned int numBlocks = static_cast<unsigned int>(list.size());
236 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
237 {
238 const auto &block = list[blockIndex];
239 if (block.name == baseName)
240 {
241 const bool arrayElementZero =
242 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
243 const bool arrayElementMatches =
244 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
245 if (arrayElementMatches || arrayElementZero)
246 {
247 return blockIndex;
248 }
249 }
250 }
251
252 return GL_INVALID_INDEX;
253}
254
255void GetInterfaceBlockName(const GLuint index,
256 const std::vector<InterfaceBlock> &list,
257 GLsizei bufSize,
258 GLsizei *length,
259 GLchar *name)
260{
261 ASSERT(index < list.size());
262
263 const auto &block = list[index];
264
265 if (bufSize > 0)
266 {
267 std::string blockName = block.name;
268
269 if (block.isArray)
270 {
271 blockName += ArrayString(block.arrayElement);
272 }
273 CopyStringToBuffer(name, blockName, bufSize, length);
274 }
275}
276
Jamie Madillc9727f32017-11-07 12:37:07 -0500277void InitUniformBlockLinker(const gl::Context *context,
278 const ProgramState &state,
279 UniformBlockLinker *blockLinker)
280{
281 if (state.getAttachedVertexShader())
282 {
283 blockLinker->addShaderBlocks(GL_VERTEX_SHADER,
284 &state.getAttachedVertexShader()->getUniformBlocks(context));
285 }
286
287 if (state.getAttachedFragmentShader())
288 {
289 blockLinker->addShaderBlocks(GL_FRAGMENT_SHADER,
290 &state.getAttachedFragmentShader()->getUniformBlocks(context));
291 }
292
293 if (state.getAttachedComputeShader())
294 {
295 blockLinker->addShaderBlocks(GL_COMPUTE_SHADER,
296 &state.getAttachedComputeShader()->getUniformBlocks(context));
297 }
298}
299
300void InitShaderStorageBlockLinker(const gl::Context *context,
301 const ProgramState &state,
302 ShaderStorageBlockLinker *blockLinker)
303{
304 if (state.getAttachedVertexShader())
305 {
306 blockLinker->addShaderBlocks(
307 GL_VERTEX_SHADER, &state.getAttachedVertexShader()->getShaderStorageBlocks(context));
308 }
309
310 if (state.getAttachedFragmentShader())
311 {
312 blockLinker->addShaderBlocks(
313 GL_FRAGMENT_SHADER,
314 &state.getAttachedFragmentShader()->getShaderStorageBlocks(context));
315 }
316
317 if (state.getAttachedComputeShader())
318 {
319 blockLinker->addShaderBlocks(
320 GL_COMPUTE_SHADER, &state.getAttachedComputeShader()->getShaderStorageBlocks(context));
321 }
322}
323
Jamie Madill62d31cb2015-09-11 13:25:51 -0400324} // anonymous namespace
325
Jamie Madill4a3c2342015-10-08 12:58:45 -0400326const char *const g_fakepath = "C:\\fakepath";
327
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400328InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000329{
330}
331
332InfoLog::~InfoLog()
333{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000334}
335
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400336size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000337{
Jamie Madill23176ce2017-07-31 14:14:33 -0400338 if (!mLazyStream)
339 {
340 return 0;
341 }
342
343 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400344 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000345}
346
Geoff Lange1a27752015-10-05 13:16:04 -0400347void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000348{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400349 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000350
351 if (bufSize > 0)
352 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400353 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400354
Jamie Madill23176ce2017-07-31 14:14:33 -0400355 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000356 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400357 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
358 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000359 }
360
361 infoLog[index] = '\0';
362 }
363
364 if (length)
365 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400366 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000367 }
368}
369
370// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300371// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000372// messages, so lets remove all occurrences of this fake file path from the log.
373void InfoLog::appendSanitized(const char *message)
374{
Jamie Madill23176ce2017-07-31 14:14:33 -0400375 ensureInitialized();
376
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000377 std::string msg(message);
378
379 size_t found;
380 do
381 {
382 found = msg.find(g_fakepath);
383 if (found != std::string::npos)
384 {
385 msg.erase(found, strlen(g_fakepath));
386 }
387 }
388 while (found != std::string::npos);
389
Jamie Madill23176ce2017-07-31 14:14:33 -0400390 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000391}
392
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000393void InfoLog::reset()
394{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000395}
396
Olli Etuaho1734e172017-10-27 15:30:27 +0300397VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000398{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500399}
400
Olli Etuahoc8538042017-09-27 11:20:15 +0300401VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300402 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500403{
Olli Etuahoc8538042017-09-27 11:20:15 +0300404 ASSERT(arrayIndex != GL_INVALID_INDEX);
405}
406
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500407SamplerBinding::SamplerBinding(GLenum textureTypeIn, size_t elementCount, bool unreferenced)
408 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
409{
410}
411
412SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
413
414SamplerBinding::~SamplerBinding() = default;
415
416Program::Bindings::Bindings()
417{
418}
419
420Program::Bindings::~Bindings()
421{
422}
423
Geoff Langd8605522016-04-13 10:19:12 -0400424void Program::Bindings::bindLocation(GLuint index, const std::string &name)
425{
426 mBindings[name] = index;
427}
428
429int Program::Bindings::getBinding(const std::string &name) const
430{
431 auto iter = mBindings.find(name);
432 return (iter != mBindings.end()) ? iter->second : -1;
433}
434
435Program::Bindings::const_iterator Program::Bindings::begin() const
436{
437 return mBindings.begin();
438}
439
440Program::Bindings::const_iterator Program::Bindings::end() const
441{
442 return mBindings.end();
443}
444
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500445ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
446{
447}
448ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
449{
450 for (size_t index = 0; index < count; ++index)
451 {
452 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
453 }
454}
455
456ImageBinding::ImageBinding(const ImageBinding &other) = default;
457
458ImageBinding::~ImageBinding() = default;
459
Jamie Madill48ef11b2016-04-27 15:21:52 -0400460ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500461 : mLabel(),
462 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400463 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300464 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800465 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500466 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400467 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500468 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800469 mImageUniformRange(0, 0),
470 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300471 mBinaryRetrieveableHint(false),
472 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400473{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300474 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400475}
476
Jamie Madill48ef11b2016-04-27 15:21:52 -0400477ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400478{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800479 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
480 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400481}
482
Jamie Madill48ef11b2016-04-27 15:21:52 -0400483const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500484{
485 return mLabel;
486}
487
Jamie Madille7d84322017-01-10 18:21:59 -0500488GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400489{
jchen1015015f72017-03-16 13:54:21 +0800490 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400491}
492
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800493GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
494{
495 return GetResourceIndexFromName(mBufferVariables, name);
496}
497
Jamie Madille7d84322017-01-10 18:21:59 -0500498GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
499{
500 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
501 return mUniformLocations[location].index;
502}
503
504Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
505{
506 GLuint index = getUniformIndexFromLocation(location);
507 if (!isSamplerUniformIndex(index))
508 {
509 return Optional<GLuint>::Invalid();
510 }
511
512 return getSamplerIndexFromUniformIndex(index);
513}
514
515bool ProgramState::isSamplerUniformIndex(GLuint index) const
516{
Jamie Madill982f6e02017-06-07 14:33:04 -0400517 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500518}
519
520GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
521{
522 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400523 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500524}
525
Jamie Madill34ca4f52017-06-13 11:49:39 -0400526GLuint ProgramState::getAttributeLocation(const std::string &name) const
527{
528 for (const sh::Attribute &attribute : mAttributes)
529 {
530 if (attribute.name == name)
531 {
532 return attribute.location;
533 }
534 }
535
536 return static_cast<GLuint>(-1);
537}
538
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500539Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400540 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400541 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542 mLinked(false),
543 mDeleteStatus(false),
544 mRefCount(0),
545 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500546 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500547{
548 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000549
Geoff Lang7dd2e102014-11-10 15:19:26 -0500550 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551}
552
553Program::~Program()
554{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400555 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556}
557
Jamie Madill4928b7c2017-06-20 12:57:39 -0400558void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500559{
560 if (mState.mAttachedVertexShader != nullptr)
561 {
562 mState.mAttachedVertexShader->release(context);
563 mState.mAttachedVertexShader = nullptr;
564 }
565
566 if (mState.mAttachedFragmentShader != nullptr)
567 {
568 mState.mAttachedFragmentShader->release(context);
569 mState.mAttachedFragmentShader = nullptr;
570 }
571
572 if (mState.mAttachedComputeShader != nullptr)
573 {
574 mState.mAttachedComputeShader->release(context);
575 mState.mAttachedComputeShader = nullptr;
576 }
577
Jiawei Shao89be29a2017-11-06 14:36:45 +0800578 if (mState.mAttachedGeometryShader != nullptr)
579 {
580 mState.mAttachedGeometryShader->release(context);
581 mState.mAttachedGeometryShader = nullptr;
582 }
583
Jamie Madillc564c072017-06-01 12:45:42 -0400584 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400585
586 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800587 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400588 SafeDelete(mProgram);
589
590 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500591}
592
Geoff Lang70d0f492015-12-10 17:45:46 -0500593void Program::setLabel(const std::string &label)
594{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400595 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500596}
597
598const std::string &Program::getLabel() const
599{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400600 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500601}
602
Jamie Madillef300b12016-10-07 15:12:09 -0400603void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300605 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300607 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608 {
Jamie Madillef300b12016-10-07 15:12:09 -0400609 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300610 mState.mAttachedVertexShader = shader;
611 mState.mAttachedVertexShader->addRef();
612 break;
613 }
614 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000615 {
Jamie Madillef300b12016-10-07 15:12:09 -0400616 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300617 mState.mAttachedFragmentShader = shader;
618 mState.mAttachedFragmentShader->addRef();
619 break;
620 }
621 case GL_COMPUTE_SHADER:
622 {
Jamie Madillef300b12016-10-07 15:12:09 -0400623 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300624 mState.mAttachedComputeShader = shader;
625 mState.mAttachedComputeShader->addRef();
626 break;
627 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800628 case GL_GEOMETRY_SHADER_EXT:
629 {
630 ASSERT(!mState.mAttachedGeometryShader);
631 mState.mAttachedGeometryShader = shader;
632 mState.mAttachedGeometryShader->addRef();
633 break;
634 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300635 default:
636 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638}
639
Jamie Madillc1d770e2017-04-13 17:31:24 -0400640void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300642 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300644 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400646 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500647 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648 mState.mAttachedVertexShader = nullptr;
649 break;
650 }
651 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400653 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500654 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300655 mState.mAttachedFragmentShader = nullptr;
656 break;
657 }
658 case GL_COMPUTE_SHADER:
659 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400660 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500661 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662 mState.mAttachedComputeShader = nullptr;
663 break;
664 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800665 case GL_GEOMETRY_SHADER_EXT:
666 {
667 ASSERT(mState.mAttachedGeometryShader == shader);
668 shader->release(context);
669 mState.mAttachedGeometryShader = nullptr;
670 break;
671 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300672 default:
673 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675}
676
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000677int Program::getAttachedShadersCount() const
678{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800680 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000681}
682
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683void Program::bindAttributeLocation(GLuint index, const char *name)
684{
Geoff Langd8605522016-04-13 10:19:12 -0400685 mAttributeBindings.bindLocation(index, name);
686}
687
688void Program::bindUniformLocation(GLuint index, const char *name)
689{
Olli Etuahod2551232017-10-26 20:03:33 +0300690 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691}
692
Sami Väisänen46eaa942016-06-29 10:26:37 +0300693void Program::bindFragmentInputLocation(GLint index, const char *name)
694{
695 mFragmentInputBindings.bindLocation(index, name);
696}
697
Jamie Madillbd044ed2017-06-05 12:59:21 -0400698BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300699{
700 BindingInfo ret;
701 ret.type = GL_NONE;
702 ret.valid = false;
703
Jamie Madillbd044ed2017-06-05 12:59:21 -0400704 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300705 ASSERT(fragmentShader);
706
707 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800708 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300709
710 for (const auto &binding : mFragmentInputBindings)
711 {
712 if (binding.second != static_cast<GLuint>(index))
713 continue;
714
715 ret.valid = true;
716
Olli Etuahod2551232017-10-26 20:03:33 +0300717 size_t nameLengthWithoutArrayIndex;
718 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300719
720 for (const auto &in : inputs)
721 {
Olli Etuahod2551232017-10-26 20:03:33 +0300722 if (in.name.length() == nameLengthWithoutArrayIndex &&
723 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300724 {
725 if (in.isArray())
726 {
727 // The client wants to bind either "name" or "name[0]".
728 // GL ES 3.1 spec refers to active array names with language such as:
729 // "if the string identifies the base name of an active array, where the
730 // string would exactly match the name of the variable if the suffix "[0]"
731 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400732 if (arrayIndex == GL_INVALID_INDEX)
733 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300734
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400735 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300736 }
737 else
738 {
739 ret.name = in.mappedName;
740 }
741 ret.type = in.type;
742 return ret;
743 }
744 }
745 }
746
747 return ret;
748}
749
Jamie Madillbd044ed2017-06-05 12:59:21 -0400750void Program::pathFragmentInputGen(const Context *context,
751 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300752 GLenum genMode,
753 GLint components,
754 const GLfloat *coeffs)
755{
756 // If the location is -1 then the command is silently ignored
757 if (index == -1)
758 return;
759
Jamie Madillbd044ed2017-06-05 12:59:21 -0400760 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300761
762 // If the input doesn't exist then then the command is silently ignored
763 // This could happen through optimization for example, the shader translator
764 // decides that a variable is not actually being used and optimizes it away.
765 if (binding.name.empty())
766 return;
767
768 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
769}
770
Martin Radev4c4c8e72016-08-04 12:25:34 +0300771// The attached shaders are checked for linking errors by matching up their variables.
772// Uniform, input and output variables get collected.
773// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500774Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000775{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500776 const auto &data = context->getContextState();
777
Jamie Madill6c58b062017-08-01 13:44:25 -0400778 auto *platform = ANGLEPlatformCurrent();
779 double startTime = platform->currentTime(platform);
780
Jamie Madill6c1f6712017-02-14 19:08:04 -0500781 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000782
Jamie Madill32447362017-06-28 14:53:52 -0400783 ProgramHash programHash;
784 auto *cache = context->getMemoryProgramCache();
785 if (cache)
786 {
787 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400788 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400789 }
790
791 if (mLinked)
792 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400793 double delta = platform->currentTime(platform) - startTime;
794 int us = static_cast<int>(delta * 1000000.0);
795 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400796 return NoError();
797 }
798
799 // Cache load failed, fall through to normal linking.
800 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000801 mInfoLog.reset();
802
Martin Radev4c4c8e72016-08-04 12:25:34 +0300803 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500804
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500805 Shader *vertexShader = mState.mAttachedVertexShader;
806 Shader *fragmentShader = mState.mAttachedFragmentShader;
807 Shader *computeShader = mState.mAttachedComputeShader;
Jamie Madill192745a2016-12-22 15:58:21 -0500808
809 bool isComputeShaderAttached = (computeShader != nullptr);
810 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300811 // Check whether we both have a compute and non-compute shaders attached.
812 // If there are of both types attached, then linking should fail.
813 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
814 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500815 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300816 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
817 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400818 }
819
Jamie Madill192745a2016-12-22 15:58:21 -0500820 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500821 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400822 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300823 {
824 mInfoLog << "Attached compute shader is not compiled.";
825 return NoError();
826 }
Jamie Madill192745a2016-12-22 15:58:21 -0500827 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300828
Jamie Madillbd044ed2017-06-05 12:59:21 -0400829 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300830
831 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
832 // If the work group size is not specified, a link time error should occur.
833 if (!mState.mComputeShaderLocalSize.isDeclared())
834 {
835 mInfoLog << "Work group size is not specified.";
836 return NoError();
837 }
838
Jamie Madillbd044ed2017-06-05 12:59:21 -0400839 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300840 {
841 return NoError();
842 }
843
Jiajia Qin729b2c62017-08-14 09:36:11 +0800844 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300845 {
846 return NoError();
847 }
848
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800849 ProgramLinkedResources resources = {
850 {0, PackMode::ANGLE_RELAXED},
851 {&mState.mUniformBlocks, &mState.mUniforms},
852 {&mState.mShaderStorageBlocks, &mState.mBufferVariables}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500853
854 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
855 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
856
857 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500858 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300859 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500860 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300861 }
862 }
863 else
864 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400865 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300866 {
867 return NoError();
868 }
Jamie Madill192745a2016-12-22 15:58:21 -0500869 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300870
Jamie Madillbd044ed2017-06-05 12:59:21 -0400871 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300872 {
873 return NoError();
874 }
Jamie Madill192745a2016-12-22 15:58:21 -0500875 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300876
Jamie Madillbd044ed2017-06-05 12:59:21 -0400877 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300878 {
879 mInfoLog << "Fragment shader version does not match vertex shader version.";
880 return NoError();
881 }
882
Jamie Madillbd044ed2017-06-05 12:59:21 -0400883 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300884 {
885 return NoError();
886 }
887
Jamie Madillbd044ed2017-06-05 12:59:21 -0400888 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300889 {
890 return NoError();
891 }
892
Jamie Madillbd044ed2017-06-05 12:59:21 -0400893 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300894 {
895 return NoError();
896 }
897
Jiajia Qin729b2c62017-08-14 09:36:11 +0800898 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300899 {
900 return NoError();
901 }
902
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400903 if (!linkValidateGlobalNames(context, mInfoLog))
904 {
905 return NoError();
906 }
907
Jamie Madillbd044ed2017-06-05 12:59:21 -0400908 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300909
Martin Radev7cf61662017-07-26 17:10:53 +0300910 mState.mNumViews = vertexShader->getNumViews(context);
911
Jamie Madillbd044ed2017-06-05 12:59:21 -0400912 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300913
Jamie Madill192745a2016-12-22 15:58:21 -0500914 // Map the varyings to the register file
915 // In WebGL, we use a slightly different handling for packing variables.
916 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
917 : PackMode::ANGLE_RELAXED;
Jamie Madillc9727f32017-11-07 12:37:07 -0500918
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800919 ProgramLinkedResources resources = {
920 {data.getCaps().maxVaryingVectors, packMode},
921 {&mState.mUniformBlocks, &mState.mUniforms},
922 {&mState.mShaderStorageBlocks, &mState.mBufferVariables}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500923
924 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
925 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
926
jchen1085c93c42017-11-12 15:36:47 +0800927 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Jamie Madill192745a2016-12-22 15:58:21 -0500928 {
929 return NoError();
930 }
931
jchen1085c93c42017-11-12 15:36:47 +0800932 if (!resources.varyingPacking.collectAndPackUserVaryings(
933 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +0300934 {
935 return NoError();
936 }
937
Jamie Madillc9727f32017-11-07 12:37:07 -0500938 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500939 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300940 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500941 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300942 }
943
944 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500945 }
946
jchen10eaef1e52017-06-13 10:44:11 +0800947 gatherAtomicCounterBuffers();
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500948 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400949
jchen10eaef1e52017-06-13 10:44:11 +0800950 setUniformValuesFromBindingQualifiers();
951
Jamie Madill54164b02017-08-28 15:17:37 -0400952 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400953 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400954
Jamie Madill32447362017-06-28 14:53:52 -0400955 // Save to the program cache.
956 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
957 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
958 {
959 cache->putProgram(programHash, context, this);
960 }
961
Jamie Madill6c58b062017-08-01 13:44:25 -0400962 double delta = platform->currentTime(platform) - startTime;
963 int us = static_cast<int>(delta * 1000000.0);
964 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
965
Martin Radev4c4c8e72016-08-04 12:25:34 +0300966 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000967}
968
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000969// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500970void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400972 mState.mAttributes.clear();
973 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400974 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800975 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400976 mState.mUniforms.clear();
977 mState.mUniformLocations.clear();
978 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800979 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800980 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400981 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800982 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400983 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400984 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300985 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500986 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800987 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300988 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500989
Geoff Lang7dd2e102014-11-10 15:19:26 -0500990 mValidated = false;
991
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000992 mLinked = false;
993}
994
Geoff Lange1a27752015-10-05 13:16:04 -0400995bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000996{
997 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
Jamie Madilla2c74982016-12-12 11:20:42 -05001000Error Program::loadBinary(const Context *context,
1001 GLenum binaryFormat,
1002 const void *binary,
1003 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001004{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001005 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001006
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001008 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001009#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001010 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1011 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001012 {
Jamie Madillf6113162015-05-07 11:49:21 -04001013 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001014 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001015 }
1016
Jamie Madill4f86d052017-06-05 12:59:26 -04001017 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1018 ANGLE_TRY_RESULT(
1019 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001020
1021 // Currently we require the full shader text to compute the program hash.
1022 // TODO(jmadill): Store the binary in the internal program cache.
1023
Jamie Madillb0a838b2016-11-13 20:02:12 -05001024 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001025#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001026}
1027
Jamie Madilla2c74982016-12-12 11:20:42 -05001028Error Program::saveBinary(const Context *context,
1029 GLenum *binaryFormat,
1030 void *binary,
1031 GLsizei bufSize,
1032 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001033{
1034 if (binaryFormat)
1035 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001036 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001037 }
1038
Jamie Madill4f86d052017-06-05 12:59:26 -04001039 angle::MemoryBuffer memoryBuf;
1040 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001041
Jamie Madill4f86d052017-06-05 12:59:26 -04001042 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1043 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001044
1045 if (streamLength > bufSize)
1046 {
1047 if (length)
1048 {
1049 *length = 0;
1050 }
1051
1052 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1053 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1054 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001055 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001056 }
1057
1058 if (binary)
1059 {
1060 char *ptr = reinterpret_cast<char*>(binary);
1061
Jamie Madill48ef11b2016-04-27 15:21:52 -04001062 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001063 ptr += streamLength;
1064
1065 ASSERT(ptr - streamLength == binary);
1066 }
1067
1068 if (length)
1069 {
1070 *length = streamLength;
1071 }
1072
He Yunchaoacd18982017-01-04 10:46:42 +08001073 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001074}
1075
Jamie Madillffe00c02017-06-27 16:26:55 -04001076GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001077{
1078 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001079 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001080 if (error.isError())
1081 {
1082 return 0;
1083 }
1084
1085 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001086}
1087
Geoff Langc5629752015-12-07 16:29:04 -05001088void Program::setBinaryRetrievableHint(bool retrievable)
1089{
1090 // TODO(jmadill) : replace with dirty bits
1091 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001092 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001093}
1094
1095bool Program::getBinaryRetrievableHint() const
1096{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001097 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001098}
1099
Yunchao He61afff12017-03-14 15:34:03 +08001100void Program::setSeparable(bool separable)
1101{
1102 // TODO(yunchao) : replace with dirty bits
1103 if (mState.mSeparable != separable)
1104 {
1105 mProgram->setSeparable(separable);
1106 mState.mSeparable = separable;
1107 }
1108}
1109
1110bool Program::isSeparable() const
1111{
1112 return mState.mSeparable;
1113}
1114
Jamie Madill6c1f6712017-02-14 19:08:04 -05001115void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001116{
1117 mRefCount--;
1118
1119 if (mRefCount == 0 && mDeleteStatus)
1120 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001121 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001122 }
1123}
1124
1125void Program::addRef()
1126{
1127 mRefCount++;
1128}
1129
1130unsigned int Program::getRefCount() const
1131{
1132 return mRefCount;
1133}
1134
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001135int Program::getInfoLogLength() const
1136{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001137 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001138}
1139
Geoff Lange1a27752015-10-05 13:16:04 -04001140void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001141{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001142 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001143}
1144
Geoff Lange1a27752015-10-05 13:16:04 -04001145void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001146{
1147 int total = 0;
1148
Martin Radev4c4c8e72016-08-04 12:25:34 +03001149 if (mState.mAttachedComputeShader)
1150 {
1151 if (total < maxCount)
1152 {
1153 shaders[total] = mState.mAttachedComputeShader->getHandle();
1154 total++;
1155 }
1156 }
1157
Jamie Madill48ef11b2016-04-27 15:21:52 -04001158 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001159 {
1160 if (total < maxCount)
1161 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001162 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001163 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001164 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001165 }
1166
Jamie Madill48ef11b2016-04-27 15:21:52 -04001167 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001168 {
1169 if (total < maxCount)
1170 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001171 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001172 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001173 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001174 }
1175
Jiawei Shao89be29a2017-11-06 14:36:45 +08001176 if (mState.mAttachedGeometryShader)
1177 {
1178 if (total < maxCount)
1179 {
1180 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1181 total++;
1182 }
1183 }
1184
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001185 if (count)
1186 {
1187 *count = total;
1188 }
1189}
1190
Geoff Lange1a27752015-10-05 13:16:04 -04001191GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001192{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001193 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001194}
1195
Jamie Madill63805b42015-08-25 13:17:39 -04001196bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001197{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001198 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1199 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001200}
1201
jchen10fd7c3b52017-03-21 15:36:03 +08001202void Program::getActiveAttribute(GLuint index,
1203 GLsizei bufsize,
1204 GLsizei *length,
1205 GLint *size,
1206 GLenum *type,
1207 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001208{
Jamie Madillc349ec02015-08-21 16:53:12 -04001209 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001210 {
1211 if (bufsize > 0)
1212 {
1213 name[0] = '\0';
1214 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001215
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001216 if (length)
1217 {
1218 *length = 0;
1219 }
1220
1221 *type = GL_NONE;
1222 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001223 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001224 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001225
jchen1036e120e2017-03-14 14:53:58 +08001226 ASSERT(index < mState.mAttributes.size());
1227 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001228
1229 if (bufsize > 0)
1230 {
jchen10fd7c3b52017-03-21 15:36:03 +08001231 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001232 }
1233
1234 // Always a single 'type' instance
1235 *size = 1;
1236 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001237}
1238
Geoff Lange1a27752015-10-05 13:16:04 -04001239GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001240{
Jamie Madillc349ec02015-08-21 16:53:12 -04001241 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001242 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001243 return 0;
1244 }
1245
jchen1036e120e2017-03-14 14:53:58 +08001246 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001247}
1248
Geoff Lange1a27752015-10-05 13:16:04 -04001249GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001250{
Jamie Madillc349ec02015-08-21 16:53:12 -04001251 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001252 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001253 return 0;
1254 }
1255
1256 size_t maxLength = 0;
1257
Jamie Madill48ef11b2016-04-27 15:21:52 -04001258 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001259 {
jchen1036e120e2017-03-14 14:53:58 +08001260 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001261 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262
Jamie Madillc349ec02015-08-21 16:53:12 -04001263 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264}
1265
jchen1015015f72017-03-16 13:54:21 +08001266GLuint Program::getInputResourceIndex(const GLchar *name) const
1267{
Olli Etuahod2551232017-10-26 20:03:33 +03001268 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001269}
1270
1271GLuint Program::getOutputResourceIndex(const GLchar *name) const
1272{
1273 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1274}
1275
jchen10fd7c3b52017-03-21 15:36:03 +08001276size_t Program::getOutputResourceCount() const
1277{
1278 return (mLinked ? mState.mOutputVariables.size() : 0);
1279}
1280
jchen10baf5d942017-08-28 20:45:48 +08001281template <typename T>
1282void Program::getResourceName(GLuint index,
1283 const std::vector<T> &resources,
1284 GLsizei bufSize,
1285 GLsizei *length,
1286 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001287{
1288 if (length)
1289 {
1290 *length = 0;
1291 }
1292
1293 if (!mLinked)
1294 {
1295 if (bufSize > 0)
1296 {
1297 name[0] = '\0';
1298 }
1299 return;
1300 }
jchen10baf5d942017-08-28 20:45:48 +08001301 ASSERT(index < resources.size());
1302 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001303
1304 if (bufSize > 0)
1305 {
Olli Etuahod2551232017-10-26 20:03:33 +03001306 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001307 }
1308}
1309
jchen10baf5d942017-08-28 20:45:48 +08001310void Program::getInputResourceName(GLuint index,
1311 GLsizei bufSize,
1312 GLsizei *length,
1313 GLchar *name) const
1314{
1315 getResourceName(index, mState.mAttributes, bufSize, length, name);
1316}
1317
1318void Program::getOutputResourceName(GLuint index,
1319 GLsizei bufSize,
1320 GLsizei *length,
1321 GLchar *name) const
1322{
1323 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1324}
1325
1326void Program::getUniformResourceName(GLuint index,
1327 GLsizei bufSize,
1328 GLsizei *length,
1329 GLchar *name) const
1330{
1331 getResourceName(index, mState.mUniforms, bufSize, length, name);
1332}
1333
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001334void Program::getBufferVariableResourceName(GLuint index,
1335 GLsizei bufSize,
1336 GLsizei *length,
1337 GLchar *name) const
1338{
1339 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1340}
1341
jchen10880683b2017-04-12 16:21:55 +08001342const sh::Attribute &Program::getInputResource(GLuint index) const
1343{
1344 ASSERT(index < mState.mAttributes.size());
1345 return mState.mAttributes[index];
1346}
1347
1348const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1349{
1350 ASSERT(index < mState.mOutputVariables.size());
1351 return mState.mOutputVariables[index];
1352}
1353
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354GLint Program::getFragDataLocation(const std::string &name) const
1355{
Olli Etuahod2551232017-10-26 20:03:33 +03001356 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001357}
1358
Geoff Lange1a27752015-10-05 13:16:04 -04001359void Program::getActiveUniform(GLuint index,
1360 GLsizei bufsize,
1361 GLsizei *length,
1362 GLint *size,
1363 GLenum *type,
1364 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001365{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001366 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001367 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001368 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001369 ASSERT(index < mState.mUniforms.size());
1370 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371
1372 if (bufsize > 0)
1373 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001374 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001375 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376 }
1377
Jamie Madill62d31cb2015-09-11 13:25:51 -04001378 *size = uniform.elementCount();
1379 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001380 }
1381 else
1382 {
1383 if (bufsize > 0)
1384 {
1385 name[0] = '\0';
1386 }
1387
1388 if (length)
1389 {
1390 *length = 0;
1391 }
1392
1393 *size = 0;
1394 *type = GL_NONE;
1395 }
1396}
1397
Geoff Lange1a27752015-10-05 13:16:04 -04001398GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001399{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001401 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001402 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001403 }
1404 else
1405 {
1406 return 0;
1407 }
1408}
1409
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001410size_t Program::getActiveBufferVariableCount() const
1411{
1412 return mLinked ? mState.mBufferVariables.size() : 0;
1413}
1414
Geoff Lange1a27752015-10-05 13:16:04 -04001415GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001416{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418
1419 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001420 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001421 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001423 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001425 size_t length = uniform.name.length() + 1u;
1426 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427 {
1428 length += 3; // Counting in "[0]".
1429 }
1430 maxLength = std::max(length, maxLength);
1431 }
1432 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001433 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434
Jamie Madill62d31cb2015-09-11 13:25:51 -04001435 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436}
1437
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438bool Program::isValidUniformLocation(GLint location) const
1439{
Jamie Madille2e406c2016-06-02 13:04:10 -04001440 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001441 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001442 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001443}
1444
Jamie Madill62d31cb2015-09-11 13:25:51 -04001445const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001447 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001448 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449}
1450
Jamie Madillac4e9c32017-01-13 14:07:12 -05001451const VariableLocation &Program::getUniformLocation(GLint location) const
1452{
1453 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1454 return mState.mUniformLocations[location];
1455}
1456
1457const std::vector<VariableLocation> &Program::getUniformLocations() const
1458{
1459 return mState.mUniformLocations;
1460}
1461
1462const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1463{
1464 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1465 return mState.mUniforms[index];
1466}
1467
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001468const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1469{
1470 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1471 return mState.mBufferVariables[index];
1472}
1473
Jamie Madill62d31cb2015-09-11 13:25:51 -04001474GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475{
Olli Etuahod2551232017-10-26 20:03:33 +03001476 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477}
1478
Jamie Madill62d31cb2015-09-11 13:25:51 -04001479GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480{
Jamie Madille7d84322017-01-10 18:21:59 -05001481 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001482}
1483
1484void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1485{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001486 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1487 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001488 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489}
1490
1491void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1492{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001493 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1494 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001495 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496}
1497
1498void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1499{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001500 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1501 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001502 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001503}
1504
1505void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1506{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001507 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1508 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001509 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510}
1511
Jamie Madill81c2e252017-09-09 23:32:46 -04001512Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001514 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1515 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1516
Jamie Madill81c2e252017-09-09 23:32:46 -04001517 mProgram->setUniform1iv(location, clampedCount, v);
1518
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001519 if (mState.isSamplerUniformIndex(locationInfo.index))
1520 {
1521 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001522 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001523 }
1524
Jamie Madill81c2e252017-09-09 23:32:46 -04001525 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001526}
1527
1528void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1529{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001530 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1531 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001532 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001533}
1534
1535void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1536{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001537 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1538 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001539 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540}
1541
1542void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1543{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001544 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1545 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001546 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001547}
1548
1549void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1550{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001551 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1552 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001553 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001554}
1555
1556void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1557{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001558 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1559 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001560 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001561}
1562
1563void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1564{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001565 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1566 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001567 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568}
1569
1570void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1571{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001572 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1573 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001574 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001575}
1576
1577void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1578{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001579 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001580 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001581}
1582
1583void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1584{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001585 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001586 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587}
1588
1589void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1590{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001591 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001592 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593}
1594
1595void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1596{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001597 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001598 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001599}
1600
1601void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1602{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001603 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001604 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001605}
1606
1607void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1608{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001609 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001610 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611}
1612
1613void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1614{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001615 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001616 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617}
1618
1619void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1620{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001621 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001622 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623}
1624
1625void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1626{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001627 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001628 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629}
1630
Jamie Madill54164b02017-08-28 15:17:37 -04001631void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632{
Jamie Madill54164b02017-08-28 15:17:37 -04001633 const auto &uniformLocation = mState.getUniformLocations()[location];
1634 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1635
1636 GLenum nativeType = gl::VariableComponentType(uniform.type);
1637 if (nativeType == GL_FLOAT)
1638 {
1639 mProgram->getUniformfv(context, location, v);
1640 }
1641 else
1642 {
1643 getUniformInternal(context, v, location, nativeType,
1644 gl::VariableComponentCount(uniform.type));
1645 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646}
1647
Jamie Madill54164b02017-08-28 15:17:37 -04001648void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001649{
Jamie Madill54164b02017-08-28 15:17:37 -04001650 const auto &uniformLocation = mState.getUniformLocations()[location];
1651 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1652
1653 GLenum nativeType = gl::VariableComponentType(uniform.type);
1654 if (nativeType == GL_INT || nativeType == GL_BOOL)
1655 {
1656 mProgram->getUniformiv(context, location, v);
1657 }
1658 else
1659 {
1660 getUniformInternal(context, v, location, nativeType,
1661 gl::VariableComponentCount(uniform.type));
1662 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663}
1664
Jamie Madill54164b02017-08-28 15:17:37 -04001665void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666{
Jamie Madill54164b02017-08-28 15:17:37 -04001667 const auto &uniformLocation = mState.getUniformLocations()[location];
1668 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1669
1670 GLenum nativeType = gl::VariableComponentType(uniform.type);
1671 if (nativeType == GL_UNSIGNED_INT)
1672 {
1673 mProgram->getUniformuiv(context, location, v);
1674 }
1675 else
1676 {
1677 getUniformInternal(context, v, location, nativeType,
1678 gl::VariableComponentCount(uniform.type));
1679 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680}
1681
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682void Program::flagForDeletion()
1683{
1684 mDeleteStatus = true;
1685}
1686
1687bool Program::isFlaggedForDeletion() const
1688{
1689 return mDeleteStatus;
1690}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001691
Brandon Jones43a53e22014-08-28 16:23:22 -07001692void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001693{
1694 mInfoLog.reset();
1695
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001697 {
Geoff Lang92019432017-11-20 13:09:34 -05001698 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001699 }
1700 else
1701 {
Jamie Madillf6113162015-05-07 11:49:21 -04001702 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001703 }
1704}
1705
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1707{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001708 // Skip cache if we're using an infolog, so we get the full error.
1709 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1710 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1711 {
1712 return mCachedValidateSamplersResult.value();
1713 }
1714
1715 if (mTextureUnitTypesCache.empty())
1716 {
1717 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1718 }
1719 else
1720 {
1721 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1722 }
1723
1724 // if any two active samplers in a program are of different types, but refer to the same
1725 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1726 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001727 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001728 {
Jamie Madill54164b02017-08-28 15:17:37 -04001729 if (samplerBinding.unreferenced)
1730 continue;
1731
Jamie Madille7d84322017-01-10 18:21:59 -05001732 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001733
Jamie Madille7d84322017-01-10 18:21:59 -05001734 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001735 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001736 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1737 {
1738 if (infoLog)
1739 {
1740 (*infoLog) << "Sampler uniform (" << textureUnit
1741 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1742 << caps.maxCombinedTextureImageUnits << ")";
1743 }
1744
1745 mCachedValidateSamplersResult = false;
1746 return false;
1747 }
1748
1749 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1750 {
1751 if (textureType != mTextureUnitTypesCache[textureUnit])
1752 {
1753 if (infoLog)
1754 {
1755 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1756 "image unit ("
1757 << textureUnit << ").";
1758 }
1759
1760 mCachedValidateSamplersResult = false;
1761 return false;
1762 }
1763 }
1764 else
1765 {
1766 mTextureUnitTypesCache[textureUnit] = textureType;
1767 }
1768 }
1769 }
1770
1771 mCachedValidateSamplersResult = true;
1772 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773}
1774
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001775bool Program::isValidated() const
1776{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777 return mValidated;
1778}
1779
Geoff Lange1a27752015-10-05 13:16:04 -04001780GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001782 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001783}
1784
jchen1058f67be2017-10-27 08:59:27 +08001785GLuint Program::getActiveAtomicCounterBufferCount() const
1786{
1787 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1788}
1789
Jiajia Qin729b2c62017-08-14 09:36:11 +08001790GLuint Program::getActiveShaderStorageBlockCount() const
1791{
1792 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1793}
1794
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001795void Program::getActiveUniformBlockName(const GLuint blockIndex,
1796 GLsizei bufSize,
1797 GLsizei *length,
1798 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001800 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
1801}
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001803void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
1804 GLsizei bufSize,
1805 GLsizei *length,
1806 GLchar *blockName) const
1807{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001809 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001810}
1811
Geoff Lange1a27752015-10-05 13:16:04 -04001812GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001813{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814 int maxLength = 0;
1815
1816 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001817 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001818 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001819 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1820 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001821 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 if (!uniformBlock.name.empty())
1823 {
jchen10af713a22017-04-19 09:10:56 +08001824 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1825 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826 }
1827 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001828 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829
1830 return maxLength;
1831}
1832
Geoff Lange1a27752015-10-05 13:16:04 -04001833GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001835 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
1836}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001837
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001838GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
1839{
1840 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001841}
1842
Jiajia Qin729b2c62017-08-14 09:36:11 +08001843const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001844{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001845 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1846 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001847}
1848
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001849const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
1850{
1851 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
1852 return mState.mShaderStorageBlocks[index];
1853}
1854
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001855void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1856{
jchen107a20b972017-06-13 14:25:26 +08001857 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001858 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001859 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001860}
1861
1862GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1863{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001864 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001865}
1866
Jiajia Qin729b2c62017-08-14 09:36:11 +08001867GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1868{
1869 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1870}
1871
Geoff Lang48dcae72014-02-05 16:28:24 -05001872void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1873{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001874 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001875 for (GLsizei i = 0; i < count; i++)
1876 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001877 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001878 }
1879
Jamie Madill48ef11b2016-04-27 15:21:52 -04001880 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001881}
1882
1883void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1884{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001886 {
jchen10a9042d32017-03-17 08:50:45 +08001887 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1888 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1889 std::string varName = var.nameWithArrayIndex();
1890 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001891 if (length)
1892 {
1893 *length = lastNameIdx;
1894 }
1895 if (size)
1896 {
jchen10a9042d32017-03-17 08:50:45 +08001897 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001898 }
1899 if (type)
1900 {
jchen10a9042d32017-03-17 08:50:45 +08001901 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001902 }
1903 if (name)
1904 {
jchen10a9042d32017-03-17 08:50:45 +08001905 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001906 name[lastNameIdx] = '\0';
1907 }
1908 }
1909}
1910
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001911GLsizei Program::getTransformFeedbackVaryingCount() const
1912{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001914 {
jchen10a9042d32017-03-17 08:50:45 +08001915 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001916 }
1917 else
1918 {
1919 return 0;
1920 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001921}
1922
1923GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1924{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001925 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001926 {
1927 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001928 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001929 {
jchen10a9042d32017-03-17 08:50:45 +08001930 maxSize =
1931 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001932 }
1933
1934 return maxSize;
1935 }
1936 else
1937 {
1938 return 0;
1939 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001940}
1941
1942GLenum Program::getTransformFeedbackBufferMode() const
1943{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001944 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945}
1946
Jamie Madillbd044ed2017-06-05 12:59:21 -04001947bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001949 Shader *vertexShader = mState.mAttachedVertexShader;
1950 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001951
Jamie Madillbd044ed2017-06-05 12:59:21 -04001952 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001953
Jiawei Shao3d404882017-10-16 13:30:48 +08001954 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
1955 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001956
Sami Väisänen46eaa942016-06-29 10:26:37 +03001957 std::map<GLuint, std::string> staticFragmentInputLocations;
1958
Jamie Madill4cff2472015-08-21 16:53:18 -04001959 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001961 bool matched = false;
1962
1963 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001964 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965 {
1966 continue;
1967 }
1968
Jamie Madill4cff2472015-08-21 16:53:18 -04001969 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001971 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001973 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001974 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001975 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001976 {
1977 return false;
1978 }
1979
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980 matched = true;
1981 break;
1982 }
1983 }
1984
1985 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001986 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001988 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001989 return false;
1990 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001991
1992 // Check for aliased path rendering input bindings (if any).
1993 // If more than one binding refer statically to the same
1994 // location the link must fail.
1995
1996 if (!output.staticUse)
1997 continue;
1998
1999 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
2000 if (inputBinding == -1)
2001 continue;
2002
2003 const auto it = staticFragmentInputLocations.find(inputBinding);
2004 if (it == std::end(staticFragmentInputLocations))
2005 {
2006 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
2007 }
2008 else
2009 {
2010 infoLog << "Binding for fragment input " << output.name << " conflicts with "
2011 << it->second;
2012 return false;
2013 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 }
2015
Jamie Madillbd044ed2017-06-05 12:59:21 -04002016 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05002017 {
2018 return false;
2019 }
2020
Jamie Madillada9ecc2015-08-17 12:53:37 -04002021 // TODO(jmadill): verify no unmatched vertex varyings?
2022
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023 return true;
2024}
2025
Jamie Madillbd044ed2017-06-05 12:59:21 -04002026bool Program::linkUniforms(const Context *context,
2027 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002028 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002029{
Olli Etuahob78707c2017-03-09 15:03:11 +00002030 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002031 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002032 {
2033 return false;
2034 }
2035
Olli Etuahob78707c2017-03-09 15:03:11 +00002036 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002037
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002038 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002039
jchen10eaef1e52017-06-13 10:44:11 +08002040 if (!linkAtomicCounterBuffers())
2041 {
2042 return false;
2043 }
2044
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002045 return true;
2046}
2047
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002048void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002049{
Jamie Madill982f6e02017-06-07 14:33:04 -04002050 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2051 unsigned int low = high;
2052
jchen10eaef1e52017-06-13 10:44:11 +08002053 for (auto counterIter = mState.mUniforms.rbegin();
2054 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2055 {
2056 --low;
2057 }
2058
2059 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2060
2061 high = low;
2062
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002063 for (auto imageIter = mState.mUniforms.rbegin();
2064 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2065 {
2066 --low;
2067 }
2068
2069 mState.mImageUniformRange = RangeUI(low, high);
2070
2071 // If uniform is a image type, insert it into the mImageBindings array.
2072 for (unsigned int imageIndex : mState.mImageUniformRange)
2073 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002074 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2075 // cannot load values into a uniform defined as an image. if declare without a
2076 // binding qualifier, any uniform image variable (include all elements of
2077 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002078 auto &imageUniform = mState.mUniforms[imageIndex];
2079 if (imageUniform.binding == -1)
2080 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002081 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002082 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002083 else
2084 {
2085 mState.mImageBindings.emplace_back(
2086 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
2087 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002088 }
2089
2090 high = low;
2091
2092 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002093 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002094 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002095 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002096 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002097
2098 mState.mSamplerUniformRange = RangeUI(low, high);
2099
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002100 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002101 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002102 {
2103 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2104 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2105 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04002106 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002107 }
2108}
2109
jchen10eaef1e52017-06-13 10:44:11 +08002110bool Program::linkAtomicCounterBuffers()
2111{
2112 for (unsigned int index : mState.mAtomicCounterUniformRange)
2113 {
2114 auto &uniform = mState.mUniforms[index];
2115 bool found = false;
2116 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2117 ++bufferIndex)
2118 {
2119 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2120 if (buffer.binding == uniform.binding)
2121 {
2122 buffer.memberIndexes.push_back(index);
2123 uniform.bufferIndex = bufferIndex;
2124 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002125 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002126 break;
2127 }
2128 }
2129 if (!found)
2130 {
2131 AtomicCounterBuffer atomicCounterBuffer;
2132 atomicCounterBuffer.binding = uniform.binding;
2133 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002134 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002135 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2136 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2137 }
2138 }
2139 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2140 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2141
2142 return true;
2143}
2144
Martin Radev4c4c8e72016-08-04 12:25:34 +03002145bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2146 const std::string &uniformName,
2147 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002148 const sh::InterfaceBlockField &fragmentUniform,
2149 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002151 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2152 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2153 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 {
2155 return false;
2156 }
2157
2158 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2159 {
Jamie Madillf6113162015-05-07 11:49:21 -04002160 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 return false;
2162 }
2163
2164 return true;
2165}
2166
Jamie Madilleb979bf2016-11-15 12:28:46 -05002167// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002168bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002170 const ContextState &data = context->getContextState();
2171 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002172
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002174 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002175 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002176
2177 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002178 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002179 {
Jamie Madillf6113162015-05-07 11:49:21 -04002180 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002181 return false;
2182 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002184 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002185
Jamie Madillc349ec02015-08-21 16:53:12 -04002186 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002187 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002188 {
Olli Etuahod2551232017-10-26 20:03:33 +03002189 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2190 // structures, so we don't need to worry about adjusting their names or generating entries
2191 // for each member/element (unlike uniforms for example).
2192 ASSERT(!attribute.isArray() && !attribute.isStruct());
2193
Jamie Madilleb979bf2016-11-15 12:28:46 -05002194 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002195 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002196 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002197 attribute.location = bindingLocation;
2198 }
2199
2200 if (attribute.location != -1)
2201 {
2202 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002203 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002204
Jamie Madill63805b42015-08-25 13:17:39 -04002205 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002206 {
Jamie Madillf6113162015-05-07 11:49:21 -04002207 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002208 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209
2210 return false;
2211 }
2212
Jamie Madill63805b42015-08-25 13:17:39 -04002213 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214 {
Jamie Madill63805b42015-08-25 13:17:39 -04002215 const int regLocation = attribute.location + reg;
2216 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002217
2218 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002219 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002220 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002222 // TODO(jmadill): fix aliasing on ES2
2223 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002225 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002226 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002227 return false;
2228 }
2229 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002230 else
2231 {
Jamie Madill63805b42015-08-25 13:17:39 -04002232 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002233 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234
Jamie Madill63805b42015-08-25 13:17:39 -04002235 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 }
2237 }
2238 }
2239
2240 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002241 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002242 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002243 // Not set by glBindAttribLocation or by location layout qualifier
2244 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245 {
Jamie Madill63805b42015-08-25 13:17:39 -04002246 int regs = VariableRegisterCount(attribute.type);
2247 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248
Jamie Madill63805b42015-08-25 13:17:39 -04002249 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002250 {
Jamie Madillf6113162015-05-07 11:49:21 -04002251 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002252 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002253 }
2254
Jamie Madillc349ec02015-08-21 16:53:12 -04002255 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002256 }
2257 }
2258
Jamie Madill48ef11b2016-04-27 15:21:52 -04002259 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260 {
Jamie Madill63805b42015-08-25 13:17:39 -04002261 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002262 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002263
Jamie Madillbd159f02017-10-09 19:39:06 -04002264 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002266 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2267 mState.mActiveAttribLocationsMask.set(location);
2268 mState.mMaxActiveAttribLocation =
2269 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002270 }
2271 }
2272
Geoff Lang7dd2e102014-11-10 15:19:26 -05002273 return true;
2274}
2275
Martin Radev4c4c8e72016-08-04 12:25:34 +03002276bool Program::validateVertexAndFragmentInterfaceBlocks(
2277 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2278 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002279 InfoLog &infoLog,
2280 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002281{
2282 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002283 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2284 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002285
2286 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2287 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002288 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002289 }
2290
Jamie Madille473dee2015-08-18 14:49:01 -04002291 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002292 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002293 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2294 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002295 {
2296 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002297 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2298 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002299 {
2300 return false;
2301 }
2302 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002303 // TODO(jiajia.qin@intel.com): Add
2304 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002305 }
2306 return true;
2307}
Jamie Madille473dee2015-08-18 14:49:01 -04002308
Jiajia Qin729b2c62017-08-14 09:36:11 +08002309bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002310{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002311 const auto &caps = context->getCaps();
2312
Martin Radev4c4c8e72016-08-04 12:25:34 +03002313 if (mState.mAttachedComputeShader)
2314 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002315 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002316 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002317
Jiajia Qin729b2c62017-08-14 09:36:11 +08002318 if (!validateInterfaceBlocksCount(
2319 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002320 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2321 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002323 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002324 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002325
2326 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2327 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2328 computeShaderStorageBlocks,
2329 "Compute shader shader storage block count exceeds "
2330 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2331 infoLog))
2332 {
2333 return false;
2334 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002335 return true;
2336 }
2337
Jamie Madillbd044ed2017-06-05 12:59:21 -04002338 Shader &vertexShader = *mState.mAttachedVertexShader;
2339 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002340
Jiajia Qin729b2c62017-08-14 09:36:11 +08002341 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2342 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002343
Jiajia Qin729b2c62017-08-14 09:36:11 +08002344 if (!validateInterfaceBlocksCount(
2345 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002346 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2347 {
2348 return false;
2349 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002350 if (!validateInterfaceBlocksCount(
2351 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002352 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2353 infoLog))
2354 {
2355
2356 return false;
2357 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002358
2359 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002360 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002361 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002362 {
2363 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 }
Jamie Madille473dee2015-08-18 14:49:01 -04002365
Jiajia Qin729b2c62017-08-14 09:36:11 +08002366 if (context->getClientVersion() >= Version(3, 1))
2367 {
2368 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2369 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2370
2371 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2372 vertexShaderStorageBlocks,
2373 "Vertex shader shader storage block count exceeds "
2374 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2375 infoLog))
2376 {
2377 return false;
2378 }
2379 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2380 fragmentShaderStorageBlocks,
2381 "Fragment shader shader storage block count exceeds "
2382 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2383 infoLog))
2384 {
2385
2386 return false;
2387 }
2388
2389 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2390 fragmentShaderStorageBlocks, infoLog,
2391 webglCompatibility))
2392 {
2393 return false;
2394 }
2395 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396 return true;
2397}
2398
Jamie Madilla2c74982016-12-12 11:20:42 -05002399bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002400 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002401 const sh::InterfaceBlock &fragmentInterfaceBlock,
2402 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403{
2404 const char* blockName = vertexInterfaceBlock.name.c_str();
2405 // validate blocks for the same member types
2406 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2407 {
Jamie Madillf6113162015-05-07 11:49:21 -04002408 infoLog << "Types for interface block '" << blockName
2409 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002410 return false;
2411 }
2412 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2413 {
Jamie Madillf6113162015-05-07 11:49:21 -04002414 infoLog << "Array sizes differ for interface block '" << blockName
2415 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002416 return false;
2417 }
jchen10af713a22017-04-19 09:10:56 +08002418 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2419 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2420 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421 {
Jamie Madillf6113162015-05-07 11:49:21 -04002422 infoLog << "Layout qualifiers differ for interface block '" << blockName
2423 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002424 return false;
2425 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002426 const unsigned int numBlockMembers =
2427 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002428 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2429 {
2430 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2431 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2432 if (vertexMember.name != fragmentMember.name)
2433 {
Jamie Madillf6113162015-05-07 11:49:21 -04002434 infoLog << "Name mismatch for field " << blockMemberIndex
2435 << " of interface block '" << blockName
2436 << "': (in vertex: '" << vertexMember.name
2437 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002438 return false;
2439 }
2440 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002441 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2442 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002443 {
2444 return false;
2445 }
2446 }
2447 return true;
2448}
2449
2450bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2451 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2452{
2453 if (vertexVariable.type != fragmentVariable.type)
2454 {
Jamie Madillf6113162015-05-07 11:49:21 -04002455 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002456 return false;
2457 }
2458 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2459 {
Jamie Madillf6113162015-05-07 11:49:21 -04002460 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002461 return false;
2462 }
2463 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2464 {
Jamie Madillf6113162015-05-07 11:49:21 -04002465 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002466 return false;
2467 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002468 if (vertexVariable.structName != fragmentVariable.structName)
2469 {
2470 infoLog << "Structure names for " << variableName
2471 << " differ between vertex and fragment shaders";
2472 return false;
2473 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002474
2475 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2476 {
Jamie Madillf6113162015-05-07 11:49:21 -04002477 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002478 return false;
2479 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002480 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002481 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2482 {
2483 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2484 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2485
2486 if (vertexMember.name != fragmentMember.name)
2487 {
Jamie Madillf6113162015-05-07 11:49:21 -04002488 infoLog << "Name mismatch for field '" << memberIndex
2489 << "' of " << variableName
2490 << ": (in vertex: '" << vertexMember.name
2491 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002492 return false;
2493 }
2494
2495 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2496 vertexMember.name + "'";
2497
2498 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2499 {
2500 return false;
2501 }
2502 }
2503
2504 return true;
2505}
2506
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002507bool Program::linkValidateVaryings(InfoLog &infoLog,
2508 const std::string &varyingName,
2509 const sh::Varying &vertexVarying,
2510 const sh::Varying &fragmentVarying,
2511 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002512{
2513 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2514 {
2515 return false;
2516 }
2517
Jamie Madille9cc4692015-02-19 16:00:13 -05002518 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002519 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002520 infoLog << "Interpolation types for " << varyingName
2521 << " differ between vertex and fragment shaders.";
2522 return false;
2523 }
2524
2525 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2526 {
2527 infoLog << "Invariance for " << varyingName
2528 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002529 return false;
2530 }
2531
2532 return true;
2533}
2534
Jamie Madillbd044ed2017-06-05 12:59:21 -04002535bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002536{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002537 Shader *vertexShader = mState.mAttachedVertexShader;
2538 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002539 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2540 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002541 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002542
2543 if (shaderVersion != 100)
2544 {
2545 // Only ESSL 1.0 has restrictions on matching input and output invariance
2546 return true;
2547 }
2548
2549 bool glPositionIsInvariant = false;
2550 bool glPointSizeIsInvariant = false;
2551 bool glFragCoordIsInvariant = false;
2552 bool glPointCoordIsInvariant = false;
2553
2554 for (const sh::Varying &varying : vertexVaryings)
2555 {
2556 if (!varying.isBuiltIn())
2557 {
2558 continue;
2559 }
2560 if (varying.name.compare("gl_Position") == 0)
2561 {
2562 glPositionIsInvariant = varying.isInvariant;
2563 }
2564 else if (varying.name.compare("gl_PointSize") == 0)
2565 {
2566 glPointSizeIsInvariant = varying.isInvariant;
2567 }
2568 }
2569
2570 for (const sh::Varying &varying : fragmentVaryings)
2571 {
2572 if (!varying.isBuiltIn())
2573 {
2574 continue;
2575 }
2576 if (varying.name.compare("gl_FragCoord") == 0)
2577 {
2578 glFragCoordIsInvariant = varying.isInvariant;
2579 }
2580 else if (varying.name.compare("gl_PointCoord") == 0)
2581 {
2582 glPointCoordIsInvariant = varying.isInvariant;
2583 }
2584 }
2585
2586 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2587 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2588 // Not requiring invariance to match is supported by:
2589 // dEQP, WebGL CTS, Nexus 5X GLES
2590 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2591 {
2592 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2593 "declared invariant.";
2594 return false;
2595 }
2596 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2597 {
2598 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2599 "declared invariant.";
2600 return false;
2601 }
2602
2603 return true;
2604}
2605
jchen10a9042d32017-03-17 08:50:45 +08002606bool Program::linkValidateTransformFeedback(const gl::Context *context,
2607 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002608 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002609 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002610{
2611 size_t totalComponents = 0;
2612
Jamie Madillccdf74b2015-08-18 10:46:12 -04002613 std::set<std::string> uniqueNames;
2614
Jamie Madill48ef11b2016-04-27 15:21:52 -04002615 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002616 {
2617 bool found = false;
Olli Etuahoc8538042017-09-27 11:20:15 +03002618 std::vector<unsigned int> subscripts;
2619 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002620
Jamie Madill192745a2016-12-22 15:58:21 -05002621 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002622 {
Jamie Madill192745a2016-12-22 15:58:21 -05002623 const sh::Varying *varying = ref.second.get();
2624
jchen10a9042d32017-03-17 08:50:45 +08002625 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002626 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002627 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002628 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002629 infoLog << "Two transform feedback varyings specify the same output variable ("
2630 << tfVaryingName << ").";
2631 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002632 }
jchen10a9042d32017-03-17 08:50:45 +08002633 if (context->getClientVersion() >= Version(3, 1))
2634 {
2635 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2636 {
2637 infoLog
2638 << "Two transform feedback varyings include the same array element ("
2639 << tfVaryingName << ").";
2640 return false;
2641 }
2642 }
2643 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002644 {
2645 infoLog << "Capture of arrays is undefined and not supported.";
2646 return false;
2647 }
2648
jchen10a9042d32017-03-17 08:50:45 +08002649 uniqueNames.insert(tfVaryingName);
2650
Jamie Madillccdf74b2015-08-18 10:46:12 -04002651 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002652 size_t elementCount =
Olli Etuahoc8538042017-09-27 11:20:15 +03002653 ((varying->isArray() && subscripts.empty()) ? varying->elementCount() : 1);
jchen10a9042d32017-03-17 08:50:45 +08002654 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002655 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002656 componentCount > caps.maxTransformFeedbackSeparateComponents)
2657 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002658 infoLog << "Transform feedback varying's " << varying->name << " components ("
2659 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002660 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002661 return false;
2662 }
2663
2664 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002665 found = true;
2666 break;
2667 }
2668 }
jchen10a9042d32017-03-17 08:50:45 +08002669 if (context->getClientVersion() < Version(3, 1) &&
2670 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002671 {
Geoff Lang1a683462015-09-29 15:09:59 -04002672 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002673 return false;
2674 }
jchen1085c93c42017-11-12 15:36:47 +08002675 if (!found)
2676 {
2677 infoLog << "Transform feedback varying " << tfVaryingName
2678 << " does not exist in the vertex shader.";
2679 return false;
2680 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002681 }
2682
Jamie Madill48ef11b2016-04-27 15:21:52 -04002683 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002684 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002685 {
Jamie Madillf6113162015-05-07 11:49:21 -04002686 infoLog << "Transform feedback varying total components (" << totalComponents
2687 << ") exceed the maximum interleaved components ("
2688 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002689 return false;
2690 }
2691
2692 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002693}
2694
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002695bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2696{
2697 const std::vector<sh::Uniform> &vertexUniforms =
2698 mState.mAttachedVertexShader->getUniforms(context);
2699 const std::vector<sh::Uniform> &fragmentUniforms =
2700 mState.mAttachedFragmentShader->getUniforms(context);
2701 const std::vector<sh::Attribute> &attributes =
2702 mState.mAttachedVertexShader->getActiveAttributes(context);
2703 for (const auto &attrib : attributes)
2704 {
2705 for (const auto &uniform : vertexUniforms)
2706 {
2707 if (uniform.name == attrib.name)
2708 {
2709 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2710 return false;
2711 }
2712 }
2713 for (const auto &uniform : fragmentUniforms)
2714 {
2715 if (uniform.name == attrib.name)
2716 {
2717 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2718 return false;
2719 }
2720 }
2721 }
2722 return true;
2723}
2724
Jamie Madill192745a2016-12-22 15:58:21 -05002725void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002726{
2727 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002728 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002729 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002730 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002731 std::vector<unsigned int> subscripts;
2732 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002733 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002734 if (!subscripts.empty())
2735 {
2736 subscript = subscripts.back();
2737 }
Jamie Madill192745a2016-12-22 15:58:21 -05002738 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002739 {
Jamie Madill192745a2016-12-22 15:58:21 -05002740 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002741 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002742 {
jchen10a9042d32017-03-17 08:50:45 +08002743 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2744 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002745 break;
2746 }
2747 }
2748 }
2749}
2750
Jamie Madillbd044ed2017-06-05 12:59:21 -04002751Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002752{
Jamie Madill192745a2016-12-22 15:58:21 -05002753 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002754
Jiawei Shao3d404882017-10-16 13:30:48 +08002755 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002756 {
Jamie Madill192745a2016-12-22 15:58:21 -05002757 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002758 }
2759
Jiawei Shao3d404882017-10-16 13:30:48 +08002760 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002761 {
Jamie Madill192745a2016-12-22 15:58:21 -05002762 merged[varying.name].fragment = &varying;
2763 }
2764
2765 return merged;
2766}
2767
Jamie Madill80a6fc02015-08-21 16:53:16 -04002768
Jamie Madillbd044ed2017-06-05 12:59:21 -04002769void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002770{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002771 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002772 ASSERT(fragmentShader != nullptr);
2773
Geoff Lange0cff192017-05-30 13:04:56 -04002774 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002775 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002776
2777 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002778 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002779 {
2780 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2781 outputVariable.name != "gl_FragData")
2782 {
2783 continue;
2784 }
2785
2786 unsigned int baseLocation =
2787 (outputVariable.location == -1 ? 0u
2788 : static_cast<unsigned int>(outputVariable.location));
2789 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2790 elementIndex++)
2791 {
2792 const unsigned int location = baseLocation + elementIndex;
2793 if (location >= mState.mOutputVariableTypes.size())
2794 {
2795 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2796 }
Corentin Walleze7557742017-06-01 13:09:57 -04002797 ASSERT(location < mState.mActiveOutputVariables.size());
2798 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002799 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2800 }
2801 }
2802
Jamie Madill80a6fc02015-08-21 16:53:16 -04002803 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002804 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002805 return;
2806
Jamie Madillbd044ed2017-06-05 12:59:21 -04002807 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002808 // TODO(jmadill): any caps validation here?
2809
jchen1015015f72017-03-16 13:54:21 +08002810 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002811 outputVariableIndex++)
2812 {
jchen1015015f72017-03-16 13:54:21 +08002813 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002814
Olli Etuahod2551232017-10-26 20:03:33 +03002815 if (outputVariable.isArray())
2816 {
2817 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
2818 // Resources and including [0] at the end of array variable names.
2819 mState.mOutputVariables[outputVariableIndex].name += "[0]";
2820 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
2821 }
2822
Jamie Madill80a6fc02015-08-21 16:53:16 -04002823 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2824 if (outputVariable.isBuiltIn())
2825 continue;
2826
2827 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03002828 unsigned int baseLocation =
2829 (outputVariable.location == -1 ? 0u
2830 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04002831
Jamie Madill80a6fc02015-08-21 16:53:16 -04002832 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2833 elementIndex++)
2834 {
Olli Etuahod2551232017-10-26 20:03:33 +03002835 const unsigned int location = baseLocation + elementIndex;
2836 if (location >= mState.mOutputLocations.size())
2837 {
2838 mState.mOutputLocations.resize(location + 1);
2839 }
2840 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03002841 if (outputVariable.isArray())
2842 {
2843 mState.mOutputLocations[location] =
2844 VariableLocation(elementIndex, outputVariableIndex);
2845 }
2846 else
2847 {
2848 VariableLocation locationInfo;
2849 locationInfo.index = outputVariableIndex;
2850 mState.mOutputLocations[location] = locationInfo;
2851 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002852 }
2853 }
2854}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002855
Olli Etuaho48fed632017-03-16 12:05:30 +00002856void Program::setUniformValuesFromBindingQualifiers()
2857{
Jamie Madill982f6e02017-06-07 14:33:04 -04002858 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002859 {
2860 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2861 if (samplerUniform.binding != -1)
2862 {
Olli Etuahod2551232017-10-26 20:03:33 +03002863 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00002864 ASSERT(location != -1);
2865 std::vector<GLint> boundTextureUnits;
2866 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2867 ++elementIndex)
2868 {
2869 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2870 }
2871 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2872 boundTextureUnits.data());
2873 }
2874 }
2875}
2876
jchen10eaef1e52017-06-13 10:44:11 +08002877void Program::gatherAtomicCounterBuffers()
2878{
jchen10baf5d942017-08-28 20:45:48 +08002879 for (unsigned int index : mState.mAtomicCounterUniformRange)
2880 {
2881 auto &uniform = mState.mUniforms[index];
2882 uniform.blockInfo.offset = uniform.offset;
2883 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2884 uniform.blockInfo.matrixStride = 0;
2885 uniform.blockInfo.isRowMajorMatrix = false;
2886 }
2887
jchen10eaef1e52017-06-13 10:44:11 +08002888 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2889}
2890
Jamie Madill6db1c2e2017-11-08 09:17:40 -05002891void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04002892{
jchen10af713a22017-04-19 09:10:56 +08002893 // Set initial bindings from shader.
2894 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2895 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002896 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002897 bindUniformBlock(blockIndex, uniformBlock.binding);
2898 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002899}
2900
Jamie Madille7d84322017-01-10 18:21:59 -05002901void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002902 GLsizei clampedCount,
2903 const GLint *v)
2904{
Jamie Madill81c2e252017-09-09 23:32:46 -04002905 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
2906 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2907 std::vector<GLuint> *boundTextureUnits =
2908 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05002909
Olli Etuaho1734e172017-10-27 15:30:27 +03002910 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04002911
2912 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04002913 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05002914}
2915
2916template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002917GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2918 GLsizei count,
2919 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002920 const T *v)
2921{
Jamie Madill134f93d2017-08-31 17:11:00 -04002922 if (count == 1)
2923 return 1;
2924
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002925 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926
Corentin Wallez15ac5342016-11-03 17:06:39 -04002927 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2928 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho1734e172017-10-27 15:30:27 +03002929 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002930 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002931 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002932
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002933 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002934 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002935 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002936 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002937
2938 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002939}
2940
2941template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002942GLsizei Program::clampMatrixUniformCount(GLint location,
2943 GLsizei count,
2944 GLboolean transpose,
2945 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002946{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002947 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2948
Jamie Madill62d31cb2015-09-11 13:25:51 -04002949 if (!transpose)
2950 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002951 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002952 }
2953
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002954 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04002955
2956 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2957 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho1734e172017-10-27 15:30:27 +03002958 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002959 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960}
2961
Jamie Madill54164b02017-08-28 15:17:37 -04002962// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
2963// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002964template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04002965void Program::getUniformInternal(const Context *context,
2966 DestT *dataOut,
2967 GLint location,
2968 GLenum nativeType,
2969 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04002970{
Jamie Madill54164b02017-08-28 15:17:37 -04002971 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002972 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002973 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04002974 {
2975 GLint tempValue[16] = {0};
2976 mProgram->getUniformiv(context, location, tempValue);
2977 UniformStateQueryCastLoop<GLboolean>(
2978 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002979 break;
Jamie Madill54164b02017-08-28 15:17:37 -04002980 }
2981 case GL_INT:
2982 {
2983 GLint tempValue[16] = {0};
2984 mProgram->getUniformiv(context, location, tempValue);
2985 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
2986 components);
2987 break;
2988 }
2989 case GL_UNSIGNED_INT:
2990 {
2991 GLuint tempValue[16] = {0};
2992 mProgram->getUniformuiv(context, location, tempValue);
2993 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
2994 components);
2995 break;
2996 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002997 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04002998 {
2999 GLfloat tempValue[16] = {0};
3000 mProgram->getUniformfv(context, location, tempValue);
3001 UniformStateQueryCastLoop<GLfloat>(
3002 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003003 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003004 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003005 default:
3006 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003007 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003008 }
3009}
Jamie Madilla4595b82017-01-11 17:36:34 -05003010
3011bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3012{
3013 // Must be called after samplers are validated.
3014 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3015
3016 for (const auto &binding : mState.mSamplerBindings)
3017 {
3018 GLenum textureType = binding.textureType;
3019 for (const auto &unit : binding.boundTextureUnits)
3020 {
3021 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3022 if (programTextureID == textureID)
3023 {
3024 // TODO(jmadill): Check for appropriate overlap.
3025 return true;
3026 }
3027 }
3028 }
3029
3030 return false;
3031}
3032
Jamie Madilla2c74982016-12-12 11:20:42 -05003033} // namespace gl