blob: 9531c1fb97cc4ff543919ddf51625d181340302a [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
jchen1015015f72017-03-16 13:54:21 +0800109template <typename VarT>
110GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
111{
Olli Etuahod2551232017-10-26 20:03:33 +0300112 std::string nameAsArrayName = name + "[0]";
jchen1015015f72017-03-16 13:54:21 +0800113 for (size_t index = 0; index < list.size(); index++)
114 {
115 const VarT &resource = list[index];
Olli Etuahod2551232017-10-26 20:03:33 +0300116 if (resource.name == name || (resource.isArray() && resource.name == nameAsArrayName))
jchen1015015f72017-03-16 13:54:21 +0800117 {
Olli Etuahod2551232017-10-26 20:03:33 +0300118 return static_cast<GLuint>(index);
jchen1015015f72017-03-16 13:54:21 +0800119 }
120 }
121
122 return GL_INVALID_INDEX;
123}
124
Olli Etuahod2551232017-10-26 20:03:33 +0300125template <typename VarT>
126GLint GetVariableLocation(const std::vector<VarT> &list,
127 const std::vector<VariableLocation> &locationList,
128 const std::string &name)
129{
130 size_t nameLengthWithoutArrayIndex;
131 unsigned int arrayIndex = ParseArrayIndex(name, &nameLengthWithoutArrayIndex);
132
133 for (size_t location = 0u; location < locationList.size(); ++location)
134 {
135 const VariableLocation &variableLocation = locationList[location];
136 if (!variableLocation.used())
137 {
138 continue;
139 }
140
141 const VarT &variable = list[variableLocation.index];
142
143 if (angle::BeginsWith(variable.name, name))
144 {
145 if (name.length() == variable.name.length())
146 {
147 ASSERT(name == variable.name);
148 // GLES 3.1 November 2016 page 87.
149 // The string exactly matches the name of the active variable.
150 return static_cast<GLint>(location);
151 }
152 if (name.length() + 3u == variable.name.length() && variable.isArray())
153 {
154 ASSERT(name + "[0]" == variable.name);
155 // The string identifies the base name of an active array, where the string would
156 // exactly match the name of the variable if the suffix "[0]" were appended to the
157 // string.
158 return static_cast<GLint>(location);
159 }
160 }
Olli Etuaho1734e172017-10-27 15:30:27 +0300161 if (variable.isArray() && variableLocation.arrayIndex == arrayIndex &&
Olli Etuahod2551232017-10-26 20:03:33 +0300162 nameLengthWithoutArrayIndex + 3u == variable.name.length() &&
163 angle::BeginsWith(variable.name, name, nameLengthWithoutArrayIndex))
164 {
165 ASSERT(name.substr(0u, nameLengthWithoutArrayIndex) + "[0]" == variable.name);
166 // The string identifies an active element of the array, where the string ends with the
167 // concatenation of the "[" character, an integer (with no "+" sign, extra leading
168 // zeroes, or whitespace) identifying an array element, and the "]" character, the
169 // integer is less than the number of active elements of the array variable, and where
170 // the string would exactly match the enumerated name of the array if the decimal
171 // integer were replaced with zero.
172 return static_cast<GLint>(location);
173 }
174 }
175
176 return -1;
177}
178
jchen10fd7c3b52017-03-21 15:36:03 +0800179void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
180{
181 ASSERT(bufSize > 0);
182 strncpy(buffer, string.c_str(), bufSize);
183 buffer[bufSize - 1] = '\0';
184
185 if (length)
186 {
187 *length = static_cast<GLsizei>(strlen(buffer));
188 }
189}
190
jchen10a9042d32017-03-17 08:50:45 +0800191bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
192{
Olli Etuahoc8538042017-09-27 11:20:15 +0300193 std::vector<unsigned int> subscripts;
194 std::string baseName = ParseResourceName(name, &subscripts);
195 for (auto nameInSet : nameSet)
jchen10a9042d32017-03-17 08:50:45 +0800196 {
Olli Etuahoc8538042017-09-27 11:20:15 +0300197 std::vector<unsigned int> arrayIndices;
198 std::string arrayName = ParseResourceName(nameInSet, &arrayIndices);
199 if (baseName == arrayName &&
200 (subscripts.empty() || arrayIndices.empty() || subscripts == arrayIndices))
jchen10a9042d32017-03-17 08:50:45 +0800201 {
202 return true;
203 }
204 }
205 return false;
206}
207
Jiajia Qin729b2c62017-08-14 09:36:11 +0800208bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
209 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
210 const std::string &errorMessage,
211 InfoLog &infoLog)
212{
213 GLuint blockCount = 0;
214 for (const sh::InterfaceBlock &block : interfaceBlocks)
215 {
216 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
217 {
218 blockCount += (block.arraySize ? block.arraySize : 1);
219 if (blockCount > maxInterfaceBlocks)
220 {
221 infoLog << errorMessage << maxInterfaceBlocks << ")";
222 return false;
223 }
224 }
225 }
226 return true;
227}
228
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800229GLuint GetInterfaceBlockIndex(const std::vector<InterfaceBlock> &list, const std::string &name)
230{
231 std::vector<unsigned int> subscripts;
232 std::string baseName = ParseResourceName(name, &subscripts);
233
234 unsigned int numBlocks = static_cast<unsigned int>(list.size());
235 for (unsigned int blockIndex = 0; blockIndex < numBlocks; blockIndex++)
236 {
237 const auto &block = list[blockIndex];
238 if (block.name == baseName)
239 {
240 const bool arrayElementZero =
241 (subscripts.empty() && (!block.isArray || block.arrayElement == 0));
242 const bool arrayElementMatches =
243 (subscripts.size() == 1 && subscripts[0] == block.arrayElement);
244 if (arrayElementMatches || arrayElementZero)
245 {
246 return blockIndex;
247 }
248 }
249 }
250
251 return GL_INVALID_INDEX;
252}
253
254void GetInterfaceBlockName(const GLuint index,
255 const std::vector<InterfaceBlock> &list,
256 GLsizei bufSize,
257 GLsizei *length,
258 GLchar *name)
259{
260 ASSERT(index < list.size());
261
262 const auto &block = list[index];
263
264 if (bufSize > 0)
265 {
266 std::string blockName = block.name;
267
268 if (block.isArray)
269 {
270 blockName += ArrayString(block.arrayElement);
271 }
272 CopyStringToBuffer(name, blockName, bufSize, length);
273 }
274}
275
Jamie Madillc9727f32017-11-07 12:37:07 -0500276void InitUniformBlockLinker(const gl::Context *context,
277 const ProgramState &state,
278 UniformBlockLinker *blockLinker)
279{
280 if (state.getAttachedVertexShader())
281 {
282 blockLinker->addShaderBlocks(GL_VERTEX_SHADER,
283 &state.getAttachedVertexShader()->getUniformBlocks(context));
284 }
285
286 if (state.getAttachedFragmentShader())
287 {
288 blockLinker->addShaderBlocks(GL_FRAGMENT_SHADER,
289 &state.getAttachedFragmentShader()->getUniformBlocks(context));
290 }
291
292 if (state.getAttachedComputeShader())
293 {
294 blockLinker->addShaderBlocks(GL_COMPUTE_SHADER,
295 &state.getAttachedComputeShader()->getUniformBlocks(context));
296 }
297}
298
299void InitShaderStorageBlockLinker(const gl::Context *context,
300 const ProgramState &state,
301 ShaderStorageBlockLinker *blockLinker)
302{
303 if (state.getAttachedVertexShader())
304 {
305 blockLinker->addShaderBlocks(
306 GL_VERTEX_SHADER, &state.getAttachedVertexShader()->getShaderStorageBlocks(context));
307 }
308
309 if (state.getAttachedFragmentShader())
310 {
311 blockLinker->addShaderBlocks(
312 GL_FRAGMENT_SHADER,
313 &state.getAttachedFragmentShader()->getShaderStorageBlocks(context));
314 }
315
316 if (state.getAttachedComputeShader())
317 {
318 blockLinker->addShaderBlocks(
319 GL_COMPUTE_SHADER, &state.getAttachedComputeShader()->getShaderStorageBlocks(context));
320 }
321}
322
Jamie Madill62d31cb2015-09-11 13:25:51 -0400323} // anonymous namespace
324
Jamie Madill4a3c2342015-10-08 12:58:45 -0400325const char *const g_fakepath = "C:\\fakepath";
326
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400327InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000328{
329}
330
331InfoLog::~InfoLog()
332{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000333}
334
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400335size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000336{
Jamie Madill23176ce2017-07-31 14:14:33 -0400337 if (!mLazyStream)
338 {
339 return 0;
340 }
341
342 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400343 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000344}
345
Geoff Lange1a27752015-10-05 13:16:04 -0400346void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000347{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400348 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000349
350 if (bufSize > 0)
351 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400352 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400353
Jamie Madill23176ce2017-07-31 14:14:33 -0400354 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000355 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400356 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
357 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000358 }
359
360 infoLog[index] = '\0';
361 }
362
363 if (length)
364 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400365 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000366 }
367}
368
369// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300370// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000371// messages, so lets remove all occurrences of this fake file path from the log.
372void InfoLog::appendSanitized(const char *message)
373{
Jamie Madill23176ce2017-07-31 14:14:33 -0400374 ensureInitialized();
375
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000376 std::string msg(message);
377
378 size_t found;
379 do
380 {
381 found = msg.find(g_fakepath);
382 if (found != std::string::npos)
383 {
384 msg.erase(found, strlen(g_fakepath));
385 }
386 }
387 while (found != std::string::npos);
388
Jamie Madill23176ce2017-07-31 14:14:33 -0400389 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000390}
391
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000392void InfoLog::reset()
393{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000394}
395
Olli Etuaho1734e172017-10-27 15:30:27 +0300396VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000397{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500398}
399
Olli Etuahoc8538042017-09-27 11:20:15 +0300400VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300401 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500402{
Olli Etuahoc8538042017-09-27 11:20:15 +0300403 ASSERT(arrayIndex != GL_INVALID_INDEX);
404}
405
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500406SamplerBinding::SamplerBinding(GLenum textureTypeIn, size_t elementCount, bool unreferenced)
407 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
408{
409}
410
411SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
412
413SamplerBinding::~SamplerBinding() = default;
414
415Program::Bindings::Bindings()
416{
417}
418
419Program::Bindings::~Bindings()
420{
421}
422
Geoff Langd8605522016-04-13 10:19:12 -0400423void Program::Bindings::bindLocation(GLuint index, const std::string &name)
424{
425 mBindings[name] = index;
426}
427
428int Program::Bindings::getBinding(const std::string &name) const
429{
430 auto iter = mBindings.find(name);
431 return (iter != mBindings.end()) ? iter->second : -1;
432}
433
434Program::Bindings::const_iterator Program::Bindings::begin() const
435{
436 return mBindings.begin();
437}
438
439Program::Bindings::const_iterator Program::Bindings::end() const
440{
441 return mBindings.end();
442}
443
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500444ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
445{
446}
447ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
448{
449 for (size_t index = 0; index < count; ++index)
450 {
451 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
452 }
453}
454
455ImageBinding::ImageBinding(const ImageBinding &other) = default;
456
457ImageBinding::~ImageBinding() = default;
458
Jamie Madill48ef11b2016-04-27 15:21:52 -0400459ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500460 : mLabel(),
461 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400462 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300463 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800464 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500465 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400466 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500467 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800468 mImageUniformRange(0, 0),
469 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300470 mBinaryRetrieveableHint(false),
471 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400472{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300473 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400474}
475
Jamie Madill48ef11b2016-04-27 15:21:52 -0400476ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400477{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800478 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
479 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400480}
481
Jamie Madill48ef11b2016-04-27 15:21:52 -0400482const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500483{
484 return mLabel;
485}
486
Jamie Madille7d84322017-01-10 18:21:59 -0500487GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400488{
jchen1015015f72017-03-16 13:54:21 +0800489 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400490}
491
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800492GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
493{
494 return GetResourceIndexFromName(mBufferVariables, name);
495}
496
Jamie Madille7d84322017-01-10 18:21:59 -0500497GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
498{
499 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
500 return mUniformLocations[location].index;
501}
502
503Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
504{
505 GLuint index = getUniformIndexFromLocation(location);
506 if (!isSamplerUniformIndex(index))
507 {
508 return Optional<GLuint>::Invalid();
509 }
510
511 return getSamplerIndexFromUniformIndex(index);
512}
513
514bool ProgramState::isSamplerUniformIndex(GLuint index) const
515{
Jamie Madill982f6e02017-06-07 14:33:04 -0400516 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500517}
518
519GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
520{
521 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400522 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500523}
524
Jamie Madill34ca4f52017-06-13 11:49:39 -0400525GLuint ProgramState::getAttributeLocation(const std::string &name) const
526{
527 for (const sh::Attribute &attribute : mAttributes)
528 {
529 if (attribute.name == name)
530 {
531 return attribute.location;
532 }
533 }
534
535 return static_cast<GLuint>(-1);
536}
537
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500538Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400539 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400540 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500541 mLinked(false),
542 mDeleteStatus(false),
543 mRefCount(0),
544 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500545 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500546{
547 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000548
Geoff Lang7dd2e102014-11-10 15:19:26 -0500549 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550}
551
552Program::~Program()
553{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400554 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555}
556
Jamie Madill4928b7c2017-06-20 12:57:39 -0400557void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500558{
559 if (mState.mAttachedVertexShader != nullptr)
560 {
561 mState.mAttachedVertexShader->release(context);
562 mState.mAttachedVertexShader = nullptr;
563 }
564
565 if (mState.mAttachedFragmentShader != nullptr)
566 {
567 mState.mAttachedFragmentShader->release(context);
568 mState.mAttachedFragmentShader = nullptr;
569 }
570
571 if (mState.mAttachedComputeShader != nullptr)
572 {
573 mState.mAttachedComputeShader->release(context);
574 mState.mAttachedComputeShader = nullptr;
575 }
576
Jiawei Shao89be29a2017-11-06 14:36:45 +0800577 if (mState.mAttachedGeometryShader != nullptr)
578 {
579 mState.mAttachedGeometryShader->release(context);
580 mState.mAttachedGeometryShader = nullptr;
581 }
582
Jamie Madillc564c072017-06-01 12:45:42 -0400583 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400584
585 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800586 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400587 SafeDelete(mProgram);
588
589 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500590}
591
Geoff Lang70d0f492015-12-10 17:45:46 -0500592void Program::setLabel(const std::string &label)
593{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400594 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500595}
596
597const std::string &Program::getLabel() const
598{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400599 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500600}
601
Jamie Madillef300b12016-10-07 15:12:09 -0400602void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300604 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300606 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607 {
Jamie Madillef300b12016-10-07 15:12:09 -0400608 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300609 mState.mAttachedVertexShader = shader;
610 mState.mAttachedVertexShader->addRef();
611 break;
612 }
613 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614 {
Jamie Madillef300b12016-10-07 15:12:09 -0400615 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300616 mState.mAttachedFragmentShader = shader;
617 mState.mAttachedFragmentShader->addRef();
618 break;
619 }
620 case GL_COMPUTE_SHADER:
621 {
Jamie Madillef300b12016-10-07 15:12:09 -0400622 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300623 mState.mAttachedComputeShader = shader;
624 mState.mAttachedComputeShader->addRef();
625 break;
626 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800627 case GL_GEOMETRY_SHADER_EXT:
628 {
629 ASSERT(!mState.mAttachedGeometryShader);
630 mState.mAttachedGeometryShader = shader;
631 mState.mAttachedGeometryShader->addRef();
632 break;
633 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300634 default:
635 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
Jamie Madillc1d770e2017-04-13 17:31:24 -0400639void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300643 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400645 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500646 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300647 mState.mAttachedVertexShader = nullptr;
648 break;
649 }
650 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400652 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500653 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300654 mState.mAttachedFragmentShader = nullptr;
655 break;
656 }
657 case GL_COMPUTE_SHADER:
658 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400659 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500660 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300661 mState.mAttachedComputeShader = nullptr;
662 break;
663 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800664 case GL_GEOMETRY_SHADER_EXT:
665 {
666 ASSERT(mState.mAttachedGeometryShader == shader);
667 shader->release(context);
668 mState.mAttachedGeometryShader = nullptr;
669 break;
670 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300671 default:
672 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674}
675
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000676int Program::getAttachedShadersCount() const
677{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300678 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800679 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000680}
681
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682void Program::bindAttributeLocation(GLuint index, const char *name)
683{
Geoff Langd8605522016-04-13 10:19:12 -0400684 mAttributeBindings.bindLocation(index, name);
685}
686
687void Program::bindUniformLocation(GLuint index, const char *name)
688{
Olli Etuahod2551232017-10-26 20:03:33 +0300689 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690}
691
Sami Väisänen46eaa942016-06-29 10:26:37 +0300692void Program::bindFragmentInputLocation(GLint index, const char *name)
693{
694 mFragmentInputBindings.bindLocation(index, name);
695}
696
Jamie Madillbd044ed2017-06-05 12:59:21 -0400697BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300698{
699 BindingInfo ret;
700 ret.type = GL_NONE;
701 ret.valid = false;
702
Jamie Madillbd044ed2017-06-05 12:59:21 -0400703 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300704 ASSERT(fragmentShader);
705
706 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800707 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300708
709 for (const auto &binding : mFragmentInputBindings)
710 {
711 if (binding.second != static_cast<GLuint>(index))
712 continue;
713
714 ret.valid = true;
715
Olli Etuahod2551232017-10-26 20:03:33 +0300716 size_t nameLengthWithoutArrayIndex;
717 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300718
719 for (const auto &in : inputs)
720 {
Olli Etuahod2551232017-10-26 20:03:33 +0300721 if (in.name.length() == nameLengthWithoutArrayIndex &&
722 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300723 {
724 if (in.isArray())
725 {
726 // The client wants to bind either "name" or "name[0]".
727 // GL ES 3.1 spec refers to active array names with language such as:
728 // "if the string identifies the base name of an active array, where the
729 // string would exactly match the name of the variable if the suffix "[0]"
730 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400731 if (arrayIndex == GL_INVALID_INDEX)
732 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300733
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400734 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300735 }
736 else
737 {
738 ret.name = in.mappedName;
739 }
740 ret.type = in.type;
741 return ret;
742 }
743 }
744 }
745
746 return ret;
747}
748
Jamie Madillbd044ed2017-06-05 12:59:21 -0400749void Program::pathFragmentInputGen(const Context *context,
750 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300751 GLenum genMode,
752 GLint components,
753 const GLfloat *coeffs)
754{
755 // If the location is -1 then the command is silently ignored
756 if (index == -1)
757 return;
758
Jamie Madillbd044ed2017-06-05 12:59:21 -0400759 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300760
761 // If the input doesn't exist then then the command is silently ignored
762 // This could happen through optimization for example, the shader translator
763 // decides that a variable is not actually being used and optimizes it away.
764 if (binding.name.empty())
765 return;
766
767 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
768}
769
Martin Radev4c4c8e72016-08-04 12:25:34 +0300770// The attached shaders are checked for linking errors by matching up their variables.
771// Uniform, input and output variables get collected.
772// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500773Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000774{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500775 const auto &data = context->getContextState();
776
Jamie Madill6c58b062017-08-01 13:44:25 -0400777 auto *platform = ANGLEPlatformCurrent();
778 double startTime = platform->currentTime(platform);
779
Jamie Madill6c1f6712017-02-14 19:08:04 -0500780 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000781
Jamie Madill32447362017-06-28 14:53:52 -0400782 ProgramHash programHash;
783 auto *cache = context->getMemoryProgramCache();
784 if (cache)
785 {
786 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400787 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400788 }
789
790 if (mLinked)
791 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400792 double delta = platform->currentTime(platform) - startTime;
793 int us = static_cast<int>(delta * 1000000.0);
794 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400795 return NoError();
796 }
797
798 // Cache load failed, fall through to normal linking.
799 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000800 mInfoLog.reset();
801
Martin Radev4c4c8e72016-08-04 12:25:34 +0300802 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500803
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500804 Shader *vertexShader = mState.mAttachedVertexShader;
805 Shader *fragmentShader = mState.mAttachedFragmentShader;
806 Shader *computeShader = mState.mAttachedComputeShader;
Jamie Madill192745a2016-12-22 15:58:21 -0500807
808 bool isComputeShaderAttached = (computeShader != nullptr);
809 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300810 // Check whether we both have a compute and non-compute shaders attached.
811 // If there are of both types attached, then linking should fail.
812 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
813 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500814 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300815 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
816 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400817 }
818
Jamie Madill192745a2016-12-22 15:58:21 -0500819 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500820 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400821 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300822 {
823 mInfoLog << "Attached compute shader is not compiled.";
824 return NoError();
825 }
Jamie Madill192745a2016-12-22 15:58:21 -0500826 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300827
Jamie Madillbd044ed2017-06-05 12:59:21 -0400828 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300829
830 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
831 // If the work group size is not specified, a link time error should occur.
832 if (!mState.mComputeShaderLocalSize.isDeclared())
833 {
834 mInfoLog << "Work group size is not specified.";
835 return NoError();
836 }
837
Jamie Madillbd044ed2017-06-05 12:59:21 -0400838 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300839 {
840 return NoError();
841 }
842
Jiajia Qin729b2c62017-08-14 09:36:11 +0800843 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300844 {
845 return NoError();
846 }
847
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800848 ProgramLinkedResources resources = {
849 {0, PackMode::ANGLE_RELAXED},
850 {&mState.mUniformBlocks, &mState.mUniforms},
851 {&mState.mShaderStorageBlocks, &mState.mBufferVariables}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500852
853 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
854 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
855
856 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500857 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300858 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500859 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300860 }
861 }
862 else
863 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400864 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300865 {
866 return NoError();
867 }
Jamie Madill192745a2016-12-22 15:58:21 -0500868 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300869
Jamie Madillbd044ed2017-06-05 12:59:21 -0400870 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300871 {
872 return NoError();
873 }
Jamie Madill192745a2016-12-22 15:58:21 -0500874 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300875
Jamie Madillbd044ed2017-06-05 12:59:21 -0400876 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300877 {
878 mInfoLog << "Fragment shader version does not match vertex shader version.";
879 return NoError();
880 }
881
Jamie Madillbd044ed2017-06-05 12:59:21 -0400882 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300883 {
884 return NoError();
885 }
886
Jamie Madillbd044ed2017-06-05 12:59:21 -0400887 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300888 {
889 return NoError();
890 }
891
Jamie Madillbd044ed2017-06-05 12:59:21 -0400892 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300893 {
894 return NoError();
895 }
896
Jiajia Qin729b2c62017-08-14 09:36:11 +0800897 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300898 {
899 return NoError();
900 }
901
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400902 if (!linkValidateGlobalNames(context, mInfoLog))
903 {
904 return NoError();
905 }
906
Jamie Madillbd044ed2017-06-05 12:59:21 -0400907 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300908
Martin Radev7cf61662017-07-26 17:10:53 +0300909 mState.mNumViews = vertexShader->getNumViews(context);
910
Jamie Madillbd044ed2017-06-05 12:59:21 -0400911 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300912
Jamie Madill192745a2016-12-22 15:58:21 -0500913 // Map the varyings to the register file
914 // In WebGL, we use a slightly different handling for packing variables.
915 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
916 : PackMode::ANGLE_RELAXED;
Jamie Madillc9727f32017-11-07 12:37:07 -0500917
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800918 ProgramLinkedResources resources = {
919 {data.getCaps().maxVaryingVectors, packMode},
920 {&mState.mUniformBlocks, &mState.mUniforms},
921 {&mState.mShaderStorageBlocks, &mState.mBufferVariables}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500922
923 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
924 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
925
jchen1085c93c42017-11-12 15:36:47 +0800926 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Jamie Madill192745a2016-12-22 15:58:21 -0500927 {
928 return NoError();
929 }
930
jchen1085c93c42017-11-12 15:36:47 +0800931 if (!resources.varyingPacking.collectAndPackUserVaryings(
932 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +0300933 {
934 return NoError();
935 }
936
Jamie Madillc9727f32017-11-07 12:37:07 -0500937 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500938 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300939 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500940 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300941 }
942
943 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500944 }
945
jchen10eaef1e52017-06-13 10:44:11 +0800946 gatherAtomicCounterBuffers();
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500947 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400948
jchen10eaef1e52017-06-13 10:44:11 +0800949 setUniformValuesFromBindingQualifiers();
950
Jamie Madill54164b02017-08-28 15:17:37 -0400951 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400952 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400953
Jamie Madill32447362017-06-28 14:53:52 -0400954 // Save to the program cache.
955 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
956 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
957 {
958 cache->putProgram(programHash, context, this);
959 }
960
Jamie Madill6c58b062017-08-01 13:44:25 -0400961 double delta = platform->currentTime(platform) - startTime;
962 int us = static_cast<int>(delta * 1000000.0);
963 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
964
Martin Radev4c4c8e72016-08-04 12:25:34 +0300965 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000966}
967
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000968// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500969void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400971 mState.mAttributes.clear();
972 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400973 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800974 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400975 mState.mUniforms.clear();
976 mState.mUniformLocations.clear();
977 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800978 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800979 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400980 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800981 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400982 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400983 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300984 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500985 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800986 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300987 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500988
Geoff Lang7dd2e102014-11-10 15:19:26 -0500989 mValidated = false;
990
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000991 mLinked = false;
992}
993
Geoff Lange1a27752015-10-05 13:16:04 -0400994bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000995{
996 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Jamie Madilla2c74982016-12-12 11:20:42 -0500999Error Program::loadBinary(const Context *context,
1000 GLenum binaryFormat,
1001 const void *binary,
1002 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001003{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001004 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001005
Geoff Lang7dd2e102014-11-10 15:19:26 -05001006#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001007 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001008#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001009 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1010 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001011 {
Jamie Madillf6113162015-05-07 11:49:21 -04001012 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001013 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001014 }
1015
Jamie Madill4f86d052017-06-05 12:59:26 -04001016 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1017 ANGLE_TRY_RESULT(
1018 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001019
1020 // Currently we require the full shader text to compute the program hash.
1021 // TODO(jmadill): Store the binary in the internal program cache.
1022
Jamie Madillb0a838b2016-11-13 20:02:12 -05001023 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001024#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001025}
1026
Jamie Madilla2c74982016-12-12 11:20:42 -05001027Error Program::saveBinary(const Context *context,
1028 GLenum *binaryFormat,
1029 void *binary,
1030 GLsizei bufSize,
1031 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001032{
1033 if (binaryFormat)
1034 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001035 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001036 }
1037
Jamie Madill4f86d052017-06-05 12:59:26 -04001038 angle::MemoryBuffer memoryBuf;
1039 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001040
Jamie Madill4f86d052017-06-05 12:59:26 -04001041 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1042 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001043
1044 if (streamLength > bufSize)
1045 {
1046 if (length)
1047 {
1048 *length = 0;
1049 }
1050
1051 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1052 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1053 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001054 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055 }
1056
1057 if (binary)
1058 {
1059 char *ptr = reinterpret_cast<char*>(binary);
1060
Jamie Madill48ef11b2016-04-27 15:21:52 -04001061 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001062 ptr += streamLength;
1063
1064 ASSERT(ptr - streamLength == binary);
1065 }
1066
1067 if (length)
1068 {
1069 *length = streamLength;
1070 }
1071
He Yunchaoacd18982017-01-04 10:46:42 +08001072 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001073}
1074
Jamie Madillffe00c02017-06-27 16:26:55 -04001075GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076{
1077 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001078 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001079 if (error.isError())
1080 {
1081 return 0;
1082 }
1083
1084 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001085}
1086
Geoff Langc5629752015-12-07 16:29:04 -05001087void Program::setBinaryRetrievableHint(bool retrievable)
1088{
1089 // TODO(jmadill) : replace with dirty bits
1090 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001091 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001092}
1093
1094bool Program::getBinaryRetrievableHint() const
1095{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001096 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001097}
1098
Yunchao He61afff12017-03-14 15:34:03 +08001099void Program::setSeparable(bool separable)
1100{
1101 // TODO(yunchao) : replace with dirty bits
1102 if (mState.mSeparable != separable)
1103 {
1104 mProgram->setSeparable(separable);
1105 mState.mSeparable = separable;
1106 }
1107}
1108
1109bool Program::isSeparable() const
1110{
1111 return mState.mSeparable;
1112}
1113
Jamie Madill6c1f6712017-02-14 19:08:04 -05001114void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001115{
1116 mRefCount--;
1117
1118 if (mRefCount == 0 && mDeleteStatus)
1119 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001120 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001121 }
1122}
1123
1124void Program::addRef()
1125{
1126 mRefCount++;
1127}
1128
1129unsigned int Program::getRefCount() const
1130{
1131 return mRefCount;
1132}
1133
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001134int Program::getInfoLogLength() const
1135{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001136 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001137}
1138
Geoff Lange1a27752015-10-05 13:16:04 -04001139void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001140{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001141 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001142}
1143
Geoff Lange1a27752015-10-05 13:16:04 -04001144void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001145{
1146 int total = 0;
1147
Martin Radev4c4c8e72016-08-04 12:25:34 +03001148 if (mState.mAttachedComputeShader)
1149 {
1150 if (total < maxCount)
1151 {
1152 shaders[total] = mState.mAttachedComputeShader->getHandle();
1153 total++;
1154 }
1155 }
1156
Jamie Madill48ef11b2016-04-27 15:21:52 -04001157 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001158 {
1159 if (total < maxCount)
1160 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001161 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001162 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001163 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001164 }
1165
Jamie Madill48ef11b2016-04-27 15:21:52 -04001166 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001167 {
1168 if (total < maxCount)
1169 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001170 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001171 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001172 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001173 }
1174
Jiawei Shao89be29a2017-11-06 14:36:45 +08001175 if (mState.mAttachedGeometryShader)
1176 {
1177 if (total < maxCount)
1178 {
1179 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1180 total++;
1181 }
1182 }
1183
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001184 if (count)
1185 {
1186 *count = total;
1187 }
1188}
1189
Geoff Lange1a27752015-10-05 13:16:04 -04001190GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001192 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193}
1194
Jamie Madill63805b42015-08-25 13:17:39 -04001195bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001196{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001197 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1198 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199}
1200
jchen10fd7c3b52017-03-21 15:36:03 +08001201void Program::getActiveAttribute(GLuint index,
1202 GLsizei bufsize,
1203 GLsizei *length,
1204 GLint *size,
1205 GLenum *type,
1206 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001207{
Jamie Madillc349ec02015-08-21 16:53:12 -04001208 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001209 {
1210 if (bufsize > 0)
1211 {
1212 name[0] = '\0';
1213 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001215 if (length)
1216 {
1217 *length = 0;
1218 }
1219
1220 *type = GL_NONE;
1221 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001222 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001223 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001224
jchen1036e120e2017-03-14 14:53:58 +08001225 ASSERT(index < mState.mAttributes.size());
1226 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001227
1228 if (bufsize > 0)
1229 {
jchen10fd7c3b52017-03-21 15:36:03 +08001230 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001231 }
1232
1233 // Always a single 'type' instance
1234 *size = 1;
1235 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001236}
1237
Geoff Lange1a27752015-10-05 13:16:04 -04001238GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001239{
Jamie Madillc349ec02015-08-21 16:53:12 -04001240 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001241 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001242 return 0;
1243 }
1244
jchen1036e120e2017-03-14 14:53:58 +08001245 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001246}
1247
Geoff Lange1a27752015-10-05 13:16:04 -04001248GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001249{
Jamie Madillc349ec02015-08-21 16:53:12 -04001250 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001251 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001252 return 0;
1253 }
1254
1255 size_t maxLength = 0;
1256
Jamie Madill48ef11b2016-04-27 15:21:52 -04001257 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001258 {
jchen1036e120e2017-03-14 14:53:58 +08001259 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001260 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001261
Jamie Madillc349ec02015-08-21 16:53:12 -04001262 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001263}
1264
jchen1015015f72017-03-16 13:54:21 +08001265GLuint Program::getInputResourceIndex(const GLchar *name) const
1266{
Olli Etuahod2551232017-10-26 20:03:33 +03001267 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001268}
1269
1270GLuint Program::getOutputResourceIndex(const GLchar *name) const
1271{
1272 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1273}
1274
jchen10fd7c3b52017-03-21 15:36:03 +08001275size_t Program::getOutputResourceCount() const
1276{
1277 return (mLinked ? mState.mOutputVariables.size() : 0);
1278}
1279
jchen10baf5d942017-08-28 20:45:48 +08001280template <typename T>
1281void Program::getResourceName(GLuint index,
1282 const std::vector<T> &resources,
1283 GLsizei bufSize,
1284 GLsizei *length,
1285 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001286{
1287 if (length)
1288 {
1289 *length = 0;
1290 }
1291
1292 if (!mLinked)
1293 {
1294 if (bufSize > 0)
1295 {
1296 name[0] = '\0';
1297 }
1298 return;
1299 }
jchen10baf5d942017-08-28 20:45:48 +08001300 ASSERT(index < resources.size());
1301 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001302
1303 if (bufSize > 0)
1304 {
Olli Etuahod2551232017-10-26 20:03:33 +03001305 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001306 }
1307}
1308
jchen10baf5d942017-08-28 20:45:48 +08001309void Program::getInputResourceName(GLuint index,
1310 GLsizei bufSize,
1311 GLsizei *length,
1312 GLchar *name) const
1313{
1314 getResourceName(index, mState.mAttributes, bufSize, length, name);
1315}
1316
1317void Program::getOutputResourceName(GLuint index,
1318 GLsizei bufSize,
1319 GLsizei *length,
1320 GLchar *name) const
1321{
1322 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1323}
1324
1325void Program::getUniformResourceName(GLuint index,
1326 GLsizei bufSize,
1327 GLsizei *length,
1328 GLchar *name) const
1329{
1330 getResourceName(index, mState.mUniforms, bufSize, length, name);
1331}
1332
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001333void Program::getBufferVariableResourceName(GLuint index,
1334 GLsizei bufSize,
1335 GLsizei *length,
1336 GLchar *name) const
1337{
1338 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1339}
1340
jchen10880683b2017-04-12 16:21:55 +08001341const sh::Attribute &Program::getInputResource(GLuint index) const
1342{
1343 ASSERT(index < mState.mAttributes.size());
1344 return mState.mAttributes[index];
1345}
1346
1347const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1348{
1349 ASSERT(index < mState.mOutputVariables.size());
1350 return mState.mOutputVariables[index];
1351}
1352
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353GLint Program::getFragDataLocation(const std::string &name) const
1354{
Olli Etuahod2551232017-10-26 20:03:33 +03001355 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001356}
1357
Geoff Lange1a27752015-10-05 13:16:04 -04001358void Program::getActiveUniform(GLuint index,
1359 GLsizei bufsize,
1360 GLsizei *length,
1361 GLint *size,
1362 GLenum *type,
1363 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001364{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001366 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001367 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001368 ASSERT(index < mState.mUniforms.size());
1369 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001370
1371 if (bufsize > 0)
1372 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001373 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001374 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375 }
1376
Olli Etuaho465835d2017-09-26 13:34:10 +03001377 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001378 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001379 }
1380 else
1381 {
1382 if (bufsize > 0)
1383 {
1384 name[0] = '\0';
1385 }
1386
1387 if (length)
1388 {
1389 *length = 0;
1390 }
1391
1392 *size = 0;
1393 *type = GL_NONE;
1394 }
1395}
1396
Geoff Lange1a27752015-10-05 13:16:04 -04001397GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001398{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001399 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001400 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001401 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001402 }
1403 else
1404 {
1405 return 0;
1406 }
1407}
1408
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001409size_t Program::getActiveBufferVariableCount() const
1410{
1411 return mLinked ? mState.mBufferVariables.size() : 0;
1412}
1413
Geoff Lange1a27752015-10-05 13:16:04 -04001414GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001415{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001416 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417
1418 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001419 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001420 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001422 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001424 size_t length = uniform.name.length() + 1u;
1425 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426 {
1427 length += 3; // Counting in "[0]".
1428 }
1429 maxLength = std::max(length, maxLength);
1430 }
1431 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001432 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433
Jamie Madill62d31cb2015-09-11 13:25:51 -04001434 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435}
1436
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437bool Program::isValidUniformLocation(GLint location) const
1438{
Jamie Madille2e406c2016-06-02 13:04:10 -04001439 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001440 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001441 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001442}
1443
Jamie Madill62d31cb2015-09-11 13:25:51 -04001444const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001446 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001447 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448}
1449
Jamie Madillac4e9c32017-01-13 14:07:12 -05001450const VariableLocation &Program::getUniformLocation(GLint location) const
1451{
1452 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1453 return mState.mUniformLocations[location];
1454}
1455
1456const std::vector<VariableLocation> &Program::getUniformLocations() const
1457{
1458 return mState.mUniformLocations;
1459}
1460
1461const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1462{
1463 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1464 return mState.mUniforms[index];
1465}
1466
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001467const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1468{
1469 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1470 return mState.mBufferVariables[index];
1471}
1472
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474{
Olli Etuahod2551232017-10-26 20:03:33 +03001475 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476}
1477
Jamie Madill62d31cb2015-09-11 13:25:51 -04001478GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479{
Jamie Madille7d84322017-01-10 18:21:59 -05001480 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481}
1482
1483void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1484{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001485 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1486 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001487 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001488}
1489
1490void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1491{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001492 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1493 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001494 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495}
1496
1497void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1498{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001499 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1500 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001501 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001502}
1503
1504void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1505{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001506 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1507 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001508 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509}
1510
Jamie Madill81c2e252017-09-09 23:32:46 -04001511Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001512{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001513 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1514 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1515
Jamie Madill81c2e252017-09-09 23:32:46 -04001516 mProgram->setUniform1iv(location, clampedCount, v);
1517
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001518 if (mState.isSamplerUniformIndex(locationInfo.index))
1519 {
1520 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001521 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001522 }
1523
Jamie Madill81c2e252017-09-09 23:32:46 -04001524 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525}
1526
1527void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1528{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001529 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1530 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001531 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001532}
1533
1534void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1535{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001536 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1537 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001538 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001539}
1540
1541void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1542{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001543 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1544 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001545 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001546}
1547
1548void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1549{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001550 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1551 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001552 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001553}
1554
1555void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1556{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001557 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1558 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001559 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560}
1561
1562void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1563{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001564 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1565 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001566 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567}
1568
1569void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1570{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001571 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1572 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001573 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001574}
1575
1576void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1577{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001578 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001579 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580}
1581
1582void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1583{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001584 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001585 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586}
1587
1588void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1589{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001590 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001591 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001592}
1593
1594void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1595{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001596 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001597 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001598}
1599
1600void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1601{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001602 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001603 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604}
1605
1606void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1607{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001608 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001609 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001610}
1611
1612void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1613{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001614 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001615 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001616}
1617
1618void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1619{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001620 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001621 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622}
1623
1624void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1625{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001626 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001627 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628}
1629
Jamie Madill54164b02017-08-28 15:17:37 -04001630void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631{
Jamie Madill54164b02017-08-28 15:17:37 -04001632 const auto &uniformLocation = mState.getUniformLocations()[location];
1633 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1634
1635 GLenum nativeType = gl::VariableComponentType(uniform.type);
1636 if (nativeType == GL_FLOAT)
1637 {
1638 mProgram->getUniformfv(context, location, v);
1639 }
1640 else
1641 {
1642 getUniformInternal(context, v, location, nativeType,
1643 gl::VariableComponentCount(uniform.type));
1644 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645}
1646
Jamie Madill54164b02017-08-28 15:17:37 -04001647void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648{
Jamie Madill54164b02017-08-28 15:17:37 -04001649 const auto &uniformLocation = mState.getUniformLocations()[location];
1650 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1651
1652 GLenum nativeType = gl::VariableComponentType(uniform.type);
1653 if (nativeType == GL_INT || nativeType == GL_BOOL)
1654 {
1655 mProgram->getUniformiv(context, location, v);
1656 }
1657 else
1658 {
1659 getUniformInternal(context, v, location, nativeType,
1660 gl::VariableComponentCount(uniform.type));
1661 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662}
1663
Jamie Madill54164b02017-08-28 15:17:37 -04001664void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001665{
Jamie Madill54164b02017-08-28 15:17:37 -04001666 const auto &uniformLocation = mState.getUniformLocations()[location];
1667 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1668
1669 GLenum nativeType = gl::VariableComponentType(uniform.type);
1670 if (nativeType == GL_UNSIGNED_INT)
1671 {
1672 mProgram->getUniformuiv(context, location, v);
1673 }
1674 else
1675 {
1676 getUniformInternal(context, v, location, nativeType,
1677 gl::VariableComponentCount(uniform.type));
1678 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679}
1680
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681void Program::flagForDeletion()
1682{
1683 mDeleteStatus = true;
1684}
1685
1686bool Program::isFlaggedForDeletion() const
1687{
1688 return mDeleteStatus;
1689}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001690
Brandon Jones43a53e22014-08-28 16:23:22 -07001691void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001692{
1693 mInfoLog.reset();
1694
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001696 {
Geoff Lang92019432017-11-20 13:09:34 -05001697 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001698 }
1699 else
1700 {
Jamie Madillf6113162015-05-07 11:49:21 -04001701 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001702 }
1703}
1704
Geoff Lang7dd2e102014-11-10 15:19:26 -05001705bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1706{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001707 // Skip cache if we're using an infolog, so we get the full error.
1708 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1709 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1710 {
1711 return mCachedValidateSamplersResult.value();
1712 }
1713
1714 if (mTextureUnitTypesCache.empty())
1715 {
1716 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1717 }
1718 else
1719 {
1720 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1721 }
1722
1723 // if any two active samplers in a program are of different types, but refer to the same
1724 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1725 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001726 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001727 {
Jamie Madill54164b02017-08-28 15:17:37 -04001728 if (samplerBinding.unreferenced)
1729 continue;
1730
Jamie Madille7d84322017-01-10 18:21:59 -05001731 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001732
Jamie Madille7d84322017-01-10 18:21:59 -05001733 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001734 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001735 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1736 {
1737 if (infoLog)
1738 {
1739 (*infoLog) << "Sampler uniform (" << textureUnit
1740 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1741 << caps.maxCombinedTextureImageUnits << ")";
1742 }
1743
1744 mCachedValidateSamplersResult = false;
1745 return false;
1746 }
1747
1748 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1749 {
1750 if (textureType != mTextureUnitTypesCache[textureUnit])
1751 {
1752 if (infoLog)
1753 {
1754 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1755 "image unit ("
1756 << textureUnit << ").";
1757 }
1758
1759 mCachedValidateSamplersResult = false;
1760 return false;
1761 }
1762 }
1763 else
1764 {
1765 mTextureUnitTypesCache[textureUnit] = textureType;
1766 }
1767 }
1768 }
1769
1770 mCachedValidateSamplersResult = true;
1771 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772}
1773
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001774bool Program::isValidated() const
1775{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 return mValidated;
1777}
1778
Geoff Lange1a27752015-10-05 13:16:04 -04001779GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001781 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782}
1783
jchen1058f67be2017-10-27 08:59:27 +08001784GLuint Program::getActiveAtomicCounterBufferCount() const
1785{
1786 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1787}
1788
Jiajia Qin729b2c62017-08-14 09:36:11 +08001789GLuint Program::getActiveShaderStorageBlockCount() const
1790{
1791 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1792}
1793
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001794void Program::getActiveUniformBlockName(const GLuint blockIndex,
1795 GLsizei bufSize,
1796 GLsizei *length,
1797 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001799 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
1800}
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001802void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
1803 GLsizei bufSize,
1804 GLsizei *length,
1805 GLchar *blockName) const
1806{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001808 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001809}
1810
Geoff Lange1a27752015-10-05 13:16:04 -04001811GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001812{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813 int maxLength = 0;
1814
1815 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001816 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001817 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1819 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001820 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 if (!uniformBlock.name.empty())
1822 {
jchen10af713a22017-04-19 09:10:56 +08001823 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1824 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825 }
1826 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001827 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001828
1829 return maxLength;
1830}
1831
Geoff Lange1a27752015-10-05 13:16:04 -04001832GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001834 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
1835}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001836
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001837GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
1838{
1839 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001840}
1841
Jiajia Qin729b2c62017-08-14 09:36:11 +08001842const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001843{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001844 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1845 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001846}
1847
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001848const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
1849{
1850 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
1851 return mState.mShaderStorageBlocks[index];
1852}
1853
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001854void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1855{
jchen107a20b972017-06-13 14:25:26 +08001856 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001857 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001858 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001859}
1860
1861GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1862{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001863 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001864}
1865
Jiajia Qin729b2c62017-08-14 09:36:11 +08001866GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1867{
1868 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1869}
1870
Geoff Lang48dcae72014-02-05 16:28:24 -05001871void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1872{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001873 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001874 for (GLsizei i = 0; i < count; i++)
1875 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001876 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001877 }
1878
Jamie Madill48ef11b2016-04-27 15:21:52 -04001879 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001880}
1881
1882void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1883{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001885 {
jchen10a9042d32017-03-17 08:50:45 +08001886 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1887 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1888 std::string varName = var.nameWithArrayIndex();
1889 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001890 if (length)
1891 {
1892 *length = lastNameIdx;
1893 }
1894 if (size)
1895 {
jchen10a9042d32017-03-17 08:50:45 +08001896 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001897 }
1898 if (type)
1899 {
jchen10a9042d32017-03-17 08:50:45 +08001900 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001901 }
1902 if (name)
1903 {
jchen10a9042d32017-03-17 08:50:45 +08001904 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001905 name[lastNameIdx] = '\0';
1906 }
1907 }
1908}
1909
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001910GLsizei Program::getTransformFeedbackVaryingCount() const
1911{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001913 {
jchen10a9042d32017-03-17 08:50:45 +08001914 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001915 }
1916 else
1917 {
1918 return 0;
1919 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001920}
1921
1922GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1923{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001925 {
1926 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001927 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001928 {
jchen10a9042d32017-03-17 08:50:45 +08001929 maxSize =
1930 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001931 }
1932
1933 return maxSize;
1934 }
1935 else
1936 {
1937 return 0;
1938 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001939}
1940
1941GLenum Program::getTransformFeedbackBufferMode() const
1942{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001943 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001944}
1945
Jamie Madillbd044ed2017-06-05 12:59:21 -04001946bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001948 Shader *vertexShader = mState.mAttachedVertexShader;
1949 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001950
Jamie Madillbd044ed2017-06-05 12:59:21 -04001951 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001952
Jiawei Shao3d404882017-10-16 13:30:48 +08001953 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
1954 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955
Sami Väisänen46eaa942016-06-29 10:26:37 +03001956 std::map<GLuint, std::string> staticFragmentInputLocations;
1957
Jamie Madill4cff2472015-08-21 16:53:18 -04001958 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 bool matched = false;
1961
1962 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001963 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964 {
1965 continue;
1966 }
1967
Jamie Madill4cff2472015-08-21 16:53:18 -04001968 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001969 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001970 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001972 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001973 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001974 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975 {
1976 return false;
1977 }
1978
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 matched = true;
1980 break;
1981 }
1982 }
1983
1984 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001985 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001987 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 return false;
1989 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001990
1991 // Check for aliased path rendering input bindings (if any).
1992 // If more than one binding refer statically to the same
1993 // location the link must fail.
1994
1995 if (!output.staticUse)
1996 continue;
1997
1998 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1999 if (inputBinding == -1)
2000 continue;
2001
2002 const auto it = staticFragmentInputLocations.find(inputBinding);
2003 if (it == std::end(staticFragmentInputLocations))
2004 {
2005 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
2006 }
2007 else
2008 {
2009 infoLog << "Binding for fragment input " << output.name << " conflicts with "
2010 << it->second;
2011 return false;
2012 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002013 }
2014
Jamie Madillbd044ed2017-06-05 12:59:21 -04002015 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05002016 {
2017 return false;
2018 }
2019
Jamie Madillada9ecc2015-08-17 12:53:37 -04002020 // TODO(jmadill): verify no unmatched vertex varyings?
2021
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022 return true;
2023}
2024
Jamie Madillbd044ed2017-06-05 12:59:21 -04002025bool Program::linkUniforms(const Context *context,
2026 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002027 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002028{
Olli Etuahob78707c2017-03-09 15:03:11 +00002029 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002030 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002031 {
2032 return false;
2033 }
2034
Olli Etuahob78707c2017-03-09 15:03:11 +00002035 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002036
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002037 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002038
jchen10eaef1e52017-06-13 10:44:11 +08002039 if (!linkAtomicCounterBuffers())
2040 {
2041 return false;
2042 }
2043
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002044 return true;
2045}
2046
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002047void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002048{
Jamie Madill982f6e02017-06-07 14:33:04 -04002049 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2050 unsigned int low = high;
2051
jchen10eaef1e52017-06-13 10:44:11 +08002052 for (auto counterIter = mState.mUniforms.rbegin();
2053 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2054 {
2055 --low;
2056 }
2057
2058 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2059
2060 high = low;
2061
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002062 for (auto imageIter = mState.mUniforms.rbegin();
2063 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2064 {
2065 --low;
2066 }
2067
2068 mState.mImageUniformRange = RangeUI(low, high);
2069
2070 // If uniform is a image type, insert it into the mImageBindings array.
2071 for (unsigned int imageIndex : mState.mImageUniformRange)
2072 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002073 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2074 // cannot load values into a uniform defined as an image. if declare without a
2075 // binding qualifier, any uniform image variable (include all elements of
2076 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002077 auto &imageUniform = mState.mUniforms[imageIndex];
2078 if (imageUniform.binding == -1)
2079 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002080 mState.mImageBindings.emplace_back(
2081 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002082 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002083 else
2084 {
2085 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002086 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002087 }
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(
Olli Etuaho465835d2017-09-26 13:34:10 +03002106 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), 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 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002458 if (vertexVariable.arraySizes != fragmentVariable.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002459 {
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
Olli Etuaho465835d2017-09-26 13:34:10 +03002652
2653 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
2654 ASSERT(!varying->isArrayOfArrays());
jchen10a9042d32017-03-17 08:50:45 +08002655 size_t elementCount =
Olli Etuaho465835d2017-09-26 13:34:10 +03002656 ((varying->isArray() && subscripts.empty()) ? varying->getOutermostArraySize()
2657 : 1);
jchen10a9042d32017-03-17 08:50:45 +08002658 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002659 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002660 componentCount > caps.maxTransformFeedbackSeparateComponents)
2661 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002662 infoLog << "Transform feedback varying's " << varying->name << " components ("
2663 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002664 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002665 return false;
2666 }
2667
2668 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002669 found = true;
2670 break;
2671 }
2672 }
jchen10a9042d32017-03-17 08:50:45 +08002673 if (context->getClientVersion() < Version(3, 1) &&
2674 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002675 {
Geoff Lang1a683462015-09-29 15:09:59 -04002676 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002677 return false;
2678 }
jchen1085c93c42017-11-12 15:36:47 +08002679 if (!found)
2680 {
2681 infoLog << "Transform feedback varying " << tfVaryingName
2682 << " does not exist in the vertex shader.";
2683 return false;
2684 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002685 }
2686
Jamie Madill48ef11b2016-04-27 15:21:52 -04002687 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002688 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002689 {
Jamie Madillf6113162015-05-07 11:49:21 -04002690 infoLog << "Transform feedback varying total components (" << totalComponents
2691 << ") exceed the maximum interleaved components ("
2692 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002693 return false;
2694 }
2695
2696 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002697}
2698
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002699bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2700{
2701 const std::vector<sh::Uniform> &vertexUniforms =
2702 mState.mAttachedVertexShader->getUniforms(context);
2703 const std::vector<sh::Uniform> &fragmentUniforms =
2704 mState.mAttachedFragmentShader->getUniforms(context);
2705 const std::vector<sh::Attribute> &attributes =
2706 mState.mAttachedVertexShader->getActiveAttributes(context);
2707 for (const auto &attrib : attributes)
2708 {
2709 for (const auto &uniform : vertexUniforms)
2710 {
2711 if (uniform.name == attrib.name)
2712 {
2713 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2714 return false;
2715 }
2716 }
2717 for (const auto &uniform : fragmentUniforms)
2718 {
2719 if (uniform.name == attrib.name)
2720 {
2721 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2722 return false;
2723 }
2724 }
2725 }
2726 return true;
2727}
2728
Jamie Madill192745a2016-12-22 15:58:21 -05002729void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002730{
2731 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002732 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002733 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002734 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002735 std::vector<unsigned int> subscripts;
2736 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002737 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002738 if (!subscripts.empty())
2739 {
2740 subscript = subscripts.back();
2741 }
Jamie Madill192745a2016-12-22 15:58:21 -05002742 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002743 {
Jamie Madill192745a2016-12-22 15:58:21 -05002744 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002745 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002746 {
jchen10a9042d32017-03-17 08:50:45 +08002747 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2748 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002749 break;
2750 }
2751 }
2752 }
2753}
2754
Jamie Madillbd044ed2017-06-05 12:59:21 -04002755Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002756{
Jamie Madill192745a2016-12-22 15:58:21 -05002757 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002758
Jiawei Shao3d404882017-10-16 13:30:48 +08002759 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002760 {
Jamie Madill192745a2016-12-22 15:58:21 -05002761 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002762 }
2763
Jiawei Shao3d404882017-10-16 13:30:48 +08002764 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002765 {
Jamie Madill192745a2016-12-22 15:58:21 -05002766 merged[varying.name].fragment = &varying;
2767 }
2768
2769 return merged;
2770}
2771
Jamie Madill80a6fc02015-08-21 16:53:16 -04002772
Jamie Madillbd044ed2017-06-05 12:59:21 -04002773void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002774{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002775 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002776 ASSERT(fragmentShader != nullptr);
2777
Geoff Lange0cff192017-05-30 13:04:56 -04002778 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002779 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002780
2781 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002782 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002783 {
2784 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2785 outputVariable.name != "gl_FragData")
2786 {
2787 continue;
2788 }
2789
2790 unsigned int baseLocation =
2791 (outputVariable.location == -1 ? 0u
2792 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03002793
2794 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
2795 // structures, so we may use getBasicTypeElementCount().
2796 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
2797 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04002798 {
2799 const unsigned int location = baseLocation + elementIndex;
2800 if (location >= mState.mOutputVariableTypes.size())
2801 {
2802 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2803 }
Corentin Walleze7557742017-06-01 13:09:57 -04002804 ASSERT(location < mState.mActiveOutputVariables.size());
2805 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002806 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2807 }
2808 }
2809
Jamie Madill80a6fc02015-08-21 16:53:16 -04002810 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002811 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002812 return;
2813
Jamie Madillbd044ed2017-06-05 12:59:21 -04002814 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002815 // TODO(jmadill): any caps validation here?
2816
jchen1015015f72017-03-16 13:54:21 +08002817 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002818 outputVariableIndex++)
2819 {
jchen1015015f72017-03-16 13:54:21 +08002820 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002821
Olli Etuahod2551232017-10-26 20:03:33 +03002822 if (outputVariable.isArray())
2823 {
2824 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
2825 // Resources and including [0] at the end of array variable names.
2826 mState.mOutputVariables[outputVariableIndex].name += "[0]";
2827 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
2828 }
2829
Jamie Madill80a6fc02015-08-21 16:53:16 -04002830 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2831 if (outputVariable.isBuiltIn())
2832 continue;
2833
2834 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03002835 unsigned int baseLocation =
2836 (outputVariable.location == -1 ? 0u
2837 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04002838
Olli Etuaho465835d2017-09-26 13:34:10 +03002839 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
2840 // structures, so we may use getBasicTypeElementCount().
2841 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
2842 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002843 {
Olli Etuahod2551232017-10-26 20:03:33 +03002844 const unsigned int location = baseLocation + elementIndex;
2845 if (location >= mState.mOutputLocations.size())
2846 {
2847 mState.mOutputLocations.resize(location + 1);
2848 }
2849 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03002850 if (outputVariable.isArray())
2851 {
2852 mState.mOutputLocations[location] =
2853 VariableLocation(elementIndex, outputVariableIndex);
2854 }
2855 else
2856 {
2857 VariableLocation locationInfo;
2858 locationInfo.index = outputVariableIndex;
2859 mState.mOutputLocations[location] = locationInfo;
2860 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002861 }
2862 }
2863}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002864
Olli Etuaho48fed632017-03-16 12:05:30 +00002865void Program::setUniformValuesFromBindingQualifiers()
2866{
Jamie Madill982f6e02017-06-07 14:33:04 -04002867 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002868 {
2869 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2870 if (samplerUniform.binding != -1)
2871 {
Olli Etuahod2551232017-10-26 20:03:33 +03002872 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00002873 ASSERT(location != -1);
2874 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03002875 for (unsigned int elementIndex = 0;
2876 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00002877 {
2878 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2879 }
2880 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2881 boundTextureUnits.data());
2882 }
2883 }
2884}
2885
jchen10eaef1e52017-06-13 10:44:11 +08002886void Program::gatherAtomicCounterBuffers()
2887{
jchen10baf5d942017-08-28 20:45:48 +08002888 for (unsigned int index : mState.mAtomicCounterUniformRange)
2889 {
2890 auto &uniform = mState.mUniforms[index];
2891 uniform.blockInfo.offset = uniform.offset;
2892 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2893 uniform.blockInfo.matrixStride = 0;
2894 uniform.blockInfo.isRowMajorMatrix = false;
2895 }
2896
jchen10eaef1e52017-06-13 10:44:11 +08002897 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2898}
2899
Jamie Madill6db1c2e2017-11-08 09:17:40 -05002900void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04002901{
jchen10af713a22017-04-19 09:10:56 +08002902 // Set initial bindings from shader.
2903 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2904 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002905 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002906 bindUniformBlock(blockIndex, uniformBlock.binding);
2907 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908}
2909
Jamie Madille7d84322017-01-10 18:21:59 -05002910void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002911 GLsizei clampedCount,
2912 const GLint *v)
2913{
Jamie Madill81c2e252017-09-09 23:32:46 -04002914 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
2915 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2916 std::vector<GLuint> *boundTextureUnits =
2917 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05002918
Olli Etuaho1734e172017-10-27 15:30:27 +03002919 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04002920
2921 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04002922 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05002923}
2924
2925template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002926GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2927 GLsizei count,
2928 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002929 const T *v)
2930{
Jamie Madill134f93d2017-08-31 17:11:00 -04002931 if (count == 1)
2932 return 1;
2933
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002934 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002935
Corentin Wallez15ac5342016-11-03 17:06:39 -04002936 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2937 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03002938 unsigned int remainingElements =
2939 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002940 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002941 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002942
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002943 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002944 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002945 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002946 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002947
2948 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002949}
2950
2951template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002952GLsizei Program::clampMatrixUniformCount(GLint location,
2953 GLsizei count,
2954 GLboolean transpose,
2955 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002956{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002957 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2958
Jamie Madill62d31cb2015-09-11 13:25:51 -04002959 if (!transpose)
2960 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002961 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002962 }
2963
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002964 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04002965
2966 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2967 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03002968 unsigned int remainingElements =
2969 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002970 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04002971}
2972
Jamie Madill54164b02017-08-28 15:17:37 -04002973// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
2974// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002975template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04002976void Program::getUniformInternal(const Context *context,
2977 DestT *dataOut,
2978 GLint location,
2979 GLenum nativeType,
2980 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04002981{
Jamie Madill54164b02017-08-28 15:17:37 -04002982 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002983 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04002984 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04002985 {
2986 GLint tempValue[16] = {0};
2987 mProgram->getUniformiv(context, location, tempValue);
2988 UniformStateQueryCastLoop<GLboolean>(
2989 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002990 break;
Jamie Madill54164b02017-08-28 15:17:37 -04002991 }
2992 case GL_INT:
2993 {
2994 GLint tempValue[16] = {0};
2995 mProgram->getUniformiv(context, location, tempValue);
2996 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
2997 components);
2998 break;
2999 }
3000 case GL_UNSIGNED_INT:
3001 {
3002 GLuint tempValue[16] = {0};
3003 mProgram->getUniformuiv(context, location, tempValue);
3004 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3005 components);
3006 break;
3007 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003008 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003009 {
3010 GLfloat tempValue[16] = {0};
3011 mProgram->getUniformfv(context, location, tempValue);
3012 UniformStateQueryCastLoop<GLfloat>(
3013 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003014 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003015 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003016 default:
3017 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003018 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003019 }
3020}
Jamie Madilla4595b82017-01-11 17:36:34 -05003021
3022bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3023{
3024 // Must be called after samplers are validated.
3025 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3026
3027 for (const auto &binding : mState.mSamplerBindings)
3028 {
3029 GLenum textureType = binding.textureType;
3030 for (const auto &unit : binding.boundTextureUnits)
3031 {
3032 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3033 if (programTextureID == textureID)
3034 {
3035 // TODO(jmadill): Check for appropriate overlap.
3036 return true;
3037 }
3038 }
3039 }
3040
3041 return false;
3042}
3043
Jamie Madilla2c74982016-12-12 11:20:42 -05003044} // namespace gl