blob: 535e09306c80501845affa174e503b7925a96537 [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
jchen108225e732017-11-14 16:29:03 +0800323// Find the matching varying or field by name.
324const sh::ShaderVariable *FindVaryingOrField(const ProgramMergedVaryings &varyings,
325 const std::string &name)
326{
327 const sh::ShaderVariable *var = nullptr;
328 for (const auto &ref : varyings)
329 {
330 const sh::Varying *varying = ref.second.get();
331 if (varying->name == name)
332 {
333 var = varying;
334 break;
335 }
336 var = FindShaderVarField(*varying, name);
337 if (var != nullptr)
338 {
339 break;
340 }
341 }
342 return var;
343}
344
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345} // anonymous namespace
346
Jamie Madill4a3c2342015-10-08 12:58:45 -0400347const char *const g_fakepath = "C:\\fakepath";
348
Jamie Madill3c1da042017-11-27 18:33:40 -0500349// InfoLog implementation.
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400350InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000351{
352}
353
354InfoLog::~InfoLog()
355{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000356}
357
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400358size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000359{
Jamie Madill23176ce2017-07-31 14:14:33 -0400360 if (!mLazyStream)
361 {
362 return 0;
363 }
364
365 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400366 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000367}
368
Geoff Lange1a27752015-10-05 13:16:04 -0400369void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000370{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400371 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000372
373 if (bufSize > 0)
374 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400375 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400376
Jamie Madill23176ce2017-07-31 14:14:33 -0400377 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000378 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400379 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
380 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000381 }
382
383 infoLog[index] = '\0';
384 }
385
386 if (length)
387 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400388 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000389 }
390}
391
392// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300393// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000394// messages, so lets remove all occurrences of this fake file path from the log.
395void InfoLog::appendSanitized(const char *message)
396{
Jamie Madill23176ce2017-07-31 14:14:33 -0400397 ensureInitialized();
398
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000399 std::string msg(message);
400
401 size_t found;
402 do
403 {
404 found = msg.find(g_fakepath);
405 if (found != std::string::npos)
406 {
407 msg.erase(found, strlen(g_fakepath));
408 }
409 }
410 while (found != std::string::npos);
411
Jamie Madill23176ce2017-07-31 14:14:33 -0400412 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000413}
414
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000415void InfoLog::reset()
416{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000417}
418
Jamie Madill3c1da042017-11-27 18:33:40 -0500419// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300420VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000421{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500422}
423
Olli Etuahoc8538042017-09-27 11:20:15 +0300424VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300425 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500426{
Olli Etuahoc8538042017-09-27 11:20:15 +0300427 ASSERT(arrayIndex != GL_INVALID_INDEX);
428}
429
Jamie Madill3c1da042017-11-27 18:33:40 -0500430// SamplerBindings implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500431SamplerBinding::SamplerBinding(GLenum textureTypeIn, size_t elementCount, bool unreferenced)
432 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
433{
434}
435
436SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
437
438SamplerBinding::~SamplerBinding() = default;
439
Jamie Madill3c1da042017-11-27 18:33:40 -0500440// ProgramBindings implementation.
441ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500442{
443}
444
Jamie Madill3c1da042017-11-27 18:33:40 -0500445ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500446{
447}
448
Jamie Madill3c1da042017-11-27 18:33:40 -0500449void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400450{
451 mBindings[name] = index;
452}
453
Jamie Madill3c1da042017-11-27 18:33:40 -0500454int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400455{
456 auto iter = mBindings.find(name);
457 return (iter != mBindings.end()) ? iter->second : -1;
458}
459
Jamie Madill3c1da042017-11-27 18:33:40 -0500460ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400461{
462 return mBindings.begin();
463}
464
Jamie Madill3c1da042017-11-27 18:33:40 -0500465ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400466{
467 return mBindings.end();
468}
469
Jamie Madill3c1da042017-11-27 18:33:40 -0500470// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500471ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
472{
473}
474ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
475{
476 for (size_t index = 0; index < count; ++index)
477 {
478 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
479 }
480}
481
482ImageBinding::ImageBinding(const ImageBinding &other) = default;
483
484ImageBinding::~ImageBinding() = default;
485
Jamie Madill3c1da042017-11-27 18:33:40 -0500486// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400487ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500488 : mLabel(),
489 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400490 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300491 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800492 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500493 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400494 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500495 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800496 mImageUniformRange(0, 0),
497 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300498 mBinaryRetrieveableHint(false),
499 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400500{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400502}
503
Jamie Madill48ef11b2016-04-27 15:21:52 -0400504ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400505{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800506 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
507 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400508}
509
Jamie Madill48ef11b2016-04-27 15:21:52 -0400510const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500511{
512 return mLabel;
513}
514
Jamie Madille7d84322017-01-10 18:21:59 -0500515GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400516{
jchen1015015f72017-03-16 13:54:21 +0800517 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400518}
519
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800520GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
521{
522 return GetResourceIndexFromName(mBufferVariables, name);
523}
524
Jamie Madille7d84322017-01-10 18:21:59 -0500525GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
526{
527 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
528 return mUniformLocations[location].index;
529}
530
531Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
532{
533 GLuint index = getUniformIndexFromLocation(location);
534 if (!isSamplerUniformIndex(index))
535 {
536 return Optional<GLuint>::Invalid();
537 }
538
539 return getSamplerIndexFromUniformIndex(index);
540}
541
542bool ProgramState::isSamplerUniformIndex(GLuint index) const
543{
Jamie Madill982f6e02017-06-07 14:33:04 -0400544 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500545}
546
547GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
548{
549 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400550 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500551}
552
Jamie Madill34ca4f52017-06-13 11:49:39 -0400553GLuint ProgramState::getAttributeLocation(const std::string &name) const
554{
555 for (const sh::Attribute &attribute : mAttributes)
556 {
557 if (attribute.name == name)
558 {
559 return attribute.location;
560 }
561 }
562
563 return static_cast<GLuint>(-1);
564}
565
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500566Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400567 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400568 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500569 mLinked(false),
570 mDeleteStatus(false),
571 mRefCount(0),
572 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500573 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500574{
575 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000576
Geoff Lang7dd2e102014-11-10 15:19:26 -0500577 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578}
579
580Program::~Program()
581{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400582 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583}
584
Jamie Madill4928b7c2017-06-20 12:57:39 -0400585void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500586{
587 if (mState.mAttachedVertexShader != nullptr)
588 {
589 mState.mAttachedVertexShader->release(context);
590 mState.mAttachedVertexShader = nullptr;
591 }
592
593 if (mState.mAttachedFragmentShader != nullptr)
594 {
595 mState.mAttachedFragmentShader->release(context);
596 mState.mAttachedFragmentShader = nullptr;
597 }
598
599 if (mState.mAttachedComputeShader != nullptr)
600 {
601 mState.mAttachedComputeShader->release(context);
602 mState.mAttachedComputeShader = nullptr;
603 }
604
Jiawei Shao89be29a2017-11-06 14:36:45 +0800605 if (mState.mAttachedGeometryShader != nullptr)
606 {
607 mState.mAttachedGeometryShader->release(context);
608 mState.mAttachedGeometryShader = nullptr;
609 }
610
Jamie Madillc564c072017-06-01 12:45:42 -0400611 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400612
613 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800614 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400615 SafeDelete(mProgram);
616
617 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500618}
619
Geoff Lang70d0f492015-12-10 17:45:46 -0500620void Program::setLabel(const std::string &label)
621{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400622 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500623}
624
625const std::string &Program::getLabel() const
626{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400627 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500628}
629
Jamie Madillef300b12016-10-07 15:12:09 -0400630void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300632 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300634 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635 {
Jamie Madillef300b12016-10-07 15:12:09 -0400636 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300637 mState.mAttachedVertexShader = shader;
638 mState.mAttachedVertexShader->addRef();
639 break;
640 }
641 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642 {
Jamie Madillef300b12016-10-07 15:12:09 -0400643 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300644 mState.mAttachedFragmentShader = shader;
645 mState.mAttachedFragmentShader->addRef();
646 break;
647 }
648 case GL_COMPUTE_SHADER:
649 {
Jamie Madillef300b12016-10-07 15:12:09 -0400650 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300651 mState.mAttachedComputeShader = shader;
652 mState.mAttachedComputeShader->addRef();
653 break;
654 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800655 case GL_GEOMETRY_SHADER_EXT:
656 {
657 ASSERT(!mState.mAttachedGeometryShader);
658 mState.mAttachedGeometryShader = shader;
659 mState.mAttachedGeometryShader->addRef();
660 break;
661 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662 default:
663 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
Jamie Madillc1d770e2017-04-13 17:31:24 -0400667void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300669 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300671 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400673 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500674 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300675 mState.mAttachedVertexShader = nullptr;
676 break;
677 }
678 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400680 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500681 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300682 mState.mAttachedFragmentShader = nullptr;
683 break;
684 }
685 case GL_COMPUTE_SHADER:
686 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400687 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500688 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 mState.mAttachedComputeShader = nullptr;
690 break;
691 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800692 case GL_GEOMETRY_SHADER_EXT:
693 {
694 ASSERT(mState.mAttachedGeometryShader == shader);
695 shader->release(context);
696 mState.mAttachedGeometryShader = nullptr;
697 break;
698 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300699 default:
700 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702}
703
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000704int Program::getAttachedShadersCount() const
705{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300706 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800707 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000708}
709
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710void Program::bindAttributeLocation(GLuint index, const char *name)
711{
Geoff Langd8605522016-04-13 10:19:12 -0400712 mAttributeBindings.bindLocation(index, name);
713}
714
715void Program::bindUniformLocation(GLuint index, const char *name)
716{
Olli Etuahod2551232017-10-26 20:03:33 +0300717 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718}
719
Sami Väisänen46eaa942016-06-29 10:26:37 +0300720void Program::bindFragmentInputLocation(GLint index, const char *name)
721{
722 mFragmentInputBindings.bindLocation(index, name);
723}
724
Jamie Madillbd044ed2017-06-05 12:59:21 -0400725BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300726{
727 BindingInfo ret;
728 ret.type = GL_NONE;
729 ret.valid = false;
730
Jamie Madillbd044ed2017-06-05 12:59:21 -0400731 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300732 ASSERT(fragmentShader);
733
734 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800735 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300736
737 for (const auto &binding : mFragmentInputBindings)
738 {
739 if (binding.second != static_cast<GLuint>(index))
740 continue;
741
742 ret.valid = true;
743
Olli Etuahod2551232017-10-26 20:03:33 +0300744 size_t nameLengthWithoutArrayIndex;
745 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300746
747 for (const auto &in : inputs)
748 {
Olli Etuahod2551232017-10-26 20:03:33 +0300749 if (in.name.length() == nameLengthWithoutArrayIndex &&
750 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300751 {
752 if (in.isArray())
753 {
754 // The client wants to bind either "name" or "name[0]".
755 // GL ES 3.1 spec refers to active array names with language such as:
756 // "if the string identifies the base name of an active array, where the
757 // string would exactly match the name of the variable if the suffix "[0]"
758 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400759 if (arrayIndex == GL_INVALID_INDEX)
760 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300761
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400762 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300763 }
764 else
765 {
766 ret.name = in.mappedName;
767 }
768 ret.type = in.type;
769 return ret;
770 }
771 }
772 }
773
774 return ret;
775}
776
Jamie Madillbd044ed2017-06-05 12:59:21 -0400777void Program::pathFragmentInputGen(const Context *context,
778 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300779 GLenum genMode,
780 GLint components,
781 const GLfloat *coeffs)
782{
783 // If the location is -1 then the command is silently ignored
784 if (index == -1)
785 return;
786
Jamie Madillbd044ed2017-06-05 12:59:21 -0400787 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300788
789 // If the input doesn't exist then then the command is silently ignored
790 // This could happen through optimization for example, the shader translator
791 // decides that a variable is not actually being used and optimizes it away.
792 if (binding.name.empty())
793 return;
794
795 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
796}
797
Martin Radev4c4c8e72016-08-04 12:25:34 +0300798// The attached shaders are checked for linking errors by matching up their variables.
799// Uniform, input and output variables get collected.
800// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500801Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000802{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500803 const auto &data = context->getContextState();
804
Jamie Madill6c58b062017-08-01 13:44:25 -0400805 auto *platform = ANGLEPlatformCurrent();
806 double startTime = platform->currentTime(platform);
807
Jamie Madill6c1f6712017-02-14 19:08:04 -0500808 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000809
Jamie Madill32447362017-06-28 14:53:52 -0400810 ProgramHash programHash;
811 auto *cache = context->getMemoryProgramCache();
812 if (cache)
813 {
814 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400815 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400816 }
817
818 if (mLinked)
819 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400820 double delta = platform->currentTime(platform) - startTime;
821 int us = static_cast<int>(delta * 1000000.0);
822 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400823 return NoError();
824 }
825
826 // Cache load failed, fall through to normal linking.
827 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000828 mInfoLog.reset();
829
Jiawei Shao73618602017-12-20 15:47:15 +0800830 if (!linkValidateShaders(context, mInfoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -0500831 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300832 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400833 }
834
Jiawei Shao73618602017-12-20 15:47:15 +0800835 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500836 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400837 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300838 {
839 return NoError();
840 }
841
Jiajia Qin729b2c62017-08-14 09:36:11 +0800842 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300843 {
844 return NoError();
845 }
846
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800847 ProgramLinkedResources resources = {
848 {0, PackMode::ANGLE_RELAXED},
849 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +0800850 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
851 {&mState.mAtomicCounterBuffers}};
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 (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300865 {
866 return NoError();
867 }
868
Jamie Madillbd044ed2017-06-05 12:59:21 -0400869 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300870 {
871 return NoError();
872 }
873
Jamie Madillbd044ed2017-06-05 12:59:21 -0400874 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300875 {
876 return NoError();
877 }
878
Jiajia Qin729b2c62017-08-14 09:36:11 +0800879 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300880 {
881 return NoError();
882 }
883
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400884 if (!linkValidateGlobalNames(context, mInfoLog))
885 {
886 return NoError();
887 }
888
Jamie Madillbd044ed2017-06-05 12:59:21 -0400889 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300890
Jiawei Shao73618602017-12-20 15:47:15 +0800891 ASSERT(mState.mAttachedVertexShader);
892 mState.mNumViews = mState.mAttachedVertexShader->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +0300893
Jamie Madillbd044ed2017-06-05 12:59:21 -0400894 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300895
Jamie Madill192745a2016-12-22 15:58:21 -0500896 // Map the varyings to the register file
897 // In WebGL, we use a slightly different handling for packing variables.
898 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
899 : PackMode::ANGLE_RELAXED;
Jamie Madillc9727f32017-11-07 12:37:07 -0500900
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800901 ProgramLinkedResources resources = {
902 {data.getCaps().maxVaryingVectors, packMode},
903 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +0800904 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
905 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500906
907 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
908 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
909
Jiawei Shao73618602017-12-20 15:47:15 +0800910 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -0500911 {
912 return NoError();
913 }
914
jchen1085c93c42017-11-12 15:36:47 +0800915 if (!resources.varyingPacking.collectAndPackUserVaryings(
916 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +0300917 {
918 return NoError();
919 }
920
Jamie Madillc9727f32017-11-07 12:37:07 -0500921 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500922 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300923 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500924 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300925 }
926
927 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500928 }
929
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500930 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400931
jchen10eaef1e52017-06-13 10:44:11 +0800932 setUniformValuesFromBindingQualifiers();
933
Yunchao Heece12532017-11-21 15:50:21 +0800934 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
935 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +0800936 ASSERT(mLinked);
937 updateLinkedShaderStages();
938
Jamie Madill54164b02017-08-28 15:17:37 -0400939 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400940 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400941
Jamie Madill32447362017-06-28 14:53:52 -0400942 // Save to the program cache.
943 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
944 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
945 {
946 cache->putProgram(programHash, context, this);
947 }
948
Jamie Madill6c58b062017-08-01 13:44:25 -0400949 double delta = platform->currentTime(platform) - startTime;
950 int us = static_cast<int>(delta * 1000000.0);
951 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
952
Martin Radev4c4c8e72016-08-04 12:25:34 +0300953 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000954}
955
Yunchao He85072e82017-11-14 15:43:28 +0800956void Program::updateLinkedShaderStages()
957{
Yunchao Heece12532017-11-21 15:50:21 +0800958 mState.mLinkedShaderStages.reset();
959
Yunchao He85072e82017-11-14 15:43:28 +0800960 if (mState.mAttachedVertexShader)
961 {
962 mState.mLinkedShaderStages.set(SHADER_VERTEX);
963 }
964
965 if (mState.mAttachedFragmentShader)
966 {
967 mState.mLinkedShaderStages.set(SHADER_FRAGMENT);
968 }
969
970 if (mState.mAttachedComputeShader)
971 {
972 mState.mLinkedShaderStages.set(SHADER_COMPUTE);
973 }
974}
975
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000976// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500977void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400979 mState.mAttributes.clear();
980 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400981 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800982 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400983 mState.mUniforms.clear();
984 mState.mUniformLocations.clear();
985 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800986 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800987 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400988 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800989 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400990 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -0800991 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -0400992 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300993 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500994 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800995 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300996 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997
Geoff Lang7dd2e102014-11-10 15:19:26 -0500998 mValidated = false;
999
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001000 mLinked = false;
1001}
1002
Geoff Lange1a27752015-10-05 13:16:04 -04001003bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001004{
1005 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
Jamie Madilla2c74982016-12-12 11:20:42 -05001008Error Program::loadBinary(const Context *context,
1009 GLenum binaryFormat,
1010 const void *binary,
1011 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001012{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001013 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001014
Geoff Lang7dd2e102014-11-10 15:19:26 -05001015#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001016 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001018 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1019 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001020 {
Jamie Madillf6113162015-05-07 11:49:21 -04001021 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001022 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023 }
1024
Jamie Madill4f86d052017-06-05 12:59:26 -04001025 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1026 ANGLE_TRY_RESULT(
1027 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001028
1029 // Currently we require the full shader text to compute the program hash.
1030 // TODO(jmadill): Store the binary in the internal program cache.
1031
Jamie Madillb0a838b2016-11-13 20:02:12 -05001032 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001033#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001034}
1035
Jamie Madilla2c74982016-12-12 11:20:42 -05001036Error Program::saveBinary(const Context *context,
1037 GLenum *binaryFormat,
1038 void *binary,
1039 GLsizei bufSize,
1040 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001041{
1042 if (binaryFormat)
1043 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001044 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001045 }
1046
Jamie Madill4f86d052017-06-05 12:59:26 -04001047 angle::MemoryBuffer memoryBuf;
1048 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001049
Jamie Madill4f86d052017-06-05 12:59:26 -04001050 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1051 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001052
1053 if (streamLength > bufSize)
1054 {
1055 if (length)
1056 {
1057 *length = 0;
1058 }
1059
1060 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1061 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1062 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001063 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 }
1065
1066 if (binary)
1067 {
1068 char *ptr = reinterpret_cast<char*>(binary);
1069
Jamie Madill48ef11b2016-04-27 15:21:52 -04001070 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001071 ptr += streamLength;
1072
1073 ASSERT(ptr - streamLength == binary);
1074 }
1075
1076 if (length)
1077 {
1078 *length = streamLength;
1079 }
1080
He Yunchaoacd18982017-01-04 10:46:42 +08001081 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001082}
1083
Jamie Madillffe00c02017-06-27 16:26:55 -04001084GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085{
1086 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001087 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088 if (error.isError())
1089 {
1090 return 0;
1091 }
1092
1093 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001094}
1095
Geoff Langc5629752015-12-07 16:29:04 -05001096void Program::setBinaryRetrievableHint(bool retrievable)
1097{
1098 // TODO(jmadill) : replace with dirty bits
1099 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001100 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001101}
1102
1103bool Program::getBinaryRetrievableHint() const
1104{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001105 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001106}
1107
Yunchao He61afff12017-03-14 15:34:03 +08001108void Program::setSeparable(bool separable)
1109{
1110 // TODO(yunchao) : replace with dirty bits
1111 if (mState.mSeparable != separable)
1112 {
1113 mProgram->setSeparable(separable);
1114 mState.mSeparable = separable;
1115 }
1116}
1117
1118bool Program::isSeparable() const
1119{
1120 return mState.mSeparable;
1121}
1122
Jamie Madill6c1f6712017-02-14 19:08:04 -05001123void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001124{
1125 mRefCount--;
1126
1127 if (mRefCount == 0 && mDeleteStatus)
1128 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001129 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001130 }
1131}
1132
1133void Program::addRef()
1134{
1135 mRefCount++;
1136}
1137
1138unsigned int Program::getRefCount() const
1139{
1140 return mRefCount;
1141}
1142
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001143int Program::getInfoLogLength() const
1144{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001145 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001146}
1147
Geoff Lange1a27752015-10-05 13:16:04 -04001148void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001149{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001150 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001151}
1152
Geoff Lange1a27752015-10-05 13:16:04 -04001153void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001154{
1155 int total = 0;
1156
Martin Radev4c4c8e72016-08-04 12:25:34 +03001157 if (mState.mAttachedComputeShader)
1158 {
1159 if (total < maxCount)
1160 {
1161 shaders[total] = mState.mAttachedComputeShader->getHandle();
1162 total++;
1163 }
1164 }
1165
Jamie Madill48ef11b2016-04-27 15:21:52 -04001166 if (mState.mAttachedVertexShader)
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.mAttachedVertexShader->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
Jamie Madill48ef11b2016-04-27 15:21:52 -04001175 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001176 {
1177 if (total < maxCount)
1178 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001179 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001180 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001181 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001182 }
1183
Jiawei Shao89be29a2017-11-06 14:36:45 +08001184 if (mState.mAttachedGeometryShader)
1185 {
1186 if (total < maxCount)
1187 {
1188 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1189 total++;
1190 }
1191 }
1192
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001193 if (count)
1194 {
1195 *count = total;
1196 }
1197}
1198
Geoff Lange1a27752015-10-05 13:16:04 -04001199GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001200{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001201 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202}
1203
Jamie Madill63805b42015-08-25 13:17:39 -04001204bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001205{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001206 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1207 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001208}
1209
jchen10fd7c3b52017-03-21 15:36:03 +08001210void Program::getActiveAttribute(GLuint index,
1211 GLsizei bufsize,
1212 GLsizei *length,
1213 GLint *size,
1214 GLenum *type,
1215 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001216{
Jamie Madillc349ec02015-08-21 16:53:12 -04001217 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001218 {
1219 if (bufsize > 0)
1220 {
1221 name[0] = '\0';
1222 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001224 if (length)
1225 {
1226 *length = 0;
1227 }
1228
1229 *type = GL_NONE;
1230 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001231 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001232 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001233
jchen1036e120e2017-03-14 14:53:58 +08001234 ASSERT(index < mState.mAttributes.size());
1235 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001236
1237 if (bufsize > 0)
1238 {
jchen10fd7c3b52017-03-21 15:36:03 +08001239 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001240 }
1241
1242 // Always a single 'type' instance
1243 *size = 1;
1244 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001245}
1246
Geoff Lange1a27752015-10-05 13:16:04 -04001247GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001248{
Jamie Madillc349ec02015-08-21 16:53:12 -04001249 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001250 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001251 return 0;
1252 }
1253
jchen1036e120e2017-03-14 14:53:58 +08001254 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001255}
1256
Geoff Lange1a27752015-10-05 13:16:04 -04001257GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001258{
Jamie Madillc349ec02015-08-21 16:53:12 -04001259 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001260 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001261 return 0;
1262 }
1263
1264 size_t maxLength = 0;
1265
Jamie Madill48ef11b2016-04-27 15:21:52 -04001266 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001267 {
jchen1036e120e2017-03-14 14:53:58 +08001268 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001269 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270
Jamie Madillc349ec02015-08-21 16:53:12 -04001271 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272}
1273
jchen1015015f72017-03-16 13:54:21 +08001274GLuint Program::getInputResourceIndex(const GLchar *name) const
1275{
Olli Etuahod2551232017-10-26 20:03:33 +03001276 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001277}
1278
1279GLuint Program::getOutputResourceIndex(const GLchar *name) const
1280{
1281 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1282}
1283
jchen10fd7c3b52017-03-21 15:36:03 +08001284size_t Program::getOutputResourceCount() const
1285{
1286 return (mLinked ? mState.mOutputVariables.size() : 0);
1287}
1288
jchen10baf5d942017-08-28 20:45:48 +08001289template <typename T>
1290void Program::getResourceName(GLuint index,
1291 const std::vector<T> &resources,
1292 GLsizei bufSize,
1293 GLsizei *length,
1294 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001295{
1296 if (length)
1297 {
1298 *length = 0;
1299 }
1300
1301 if (!mLinked)
1302 {
1303 if (bufSize > 0)
1304 {
1305 name[0] = '\0';
1306 }
1307 return;
1308 }
jchen10baf5d942017-08-28 20:45:48 +08001309 ASSERT(index < resources.size());
1310 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001311
1312 if (bufSize > 0)
1313 {
Olli Etuahod2551232017-10-26 20:03:33 +03001314 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001315 }
1316}
1317
jchen10baf5d942017-08-28 20:45:48 +08001318void Program::getInputResourceName(GLuint index,
1319 GLsizei bufSize,
1320 GLsizei *length,
1321 GLchar *name) const
1322{
1323 getResourceName(index, mState.mAttributes, bufSize, length, name);
1324}
1325
1326void Program::getOutputResourceName(GLuint index,
1327 GLsizei bufSize,
1328 GLsizei *length,
1329 GLchar *name) const
1330{
1331 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1332}
1333
1334void Program::getUniformResourceName(GLuint index,
1335 GLsizei bufSize,
1336 GLsizei *length,
1337 GLchar *name) const
1338{
1339 getResourceName(index, mState.mUniforms, bufSize, length, name);
1340}
1341
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001342void Program::getBufferVariableResourceName(GLuint index,
1343 GLsizei bufSize,
1344 GLsizei *length,
1345 GLchar *name) const
1346{
1347 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1348}
1349
jchen10880683b2017-04-12 16:21:55 +08001350const sh::Attribute &Program::getInputResource(GLuint index) const
1351{
1352 ASSERT(index < mState.mAttributes.size());
1353 return mState.mAttributes[index];
1354}
1355
1356const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1357{
1358 ASSERT(index < mState.mOutputVariables.size());
1359 return mState.mOutputVariables[index];
1360}
1361
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362GLint Program::getFragDataLocation(const std::string &name) const
1363{
Olli Etuahod2551232017-10-26 20:03:33 +03001364 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001365}
1366
Geoff Lange1a27752015-10-05 13:16:04 -04001367void Program::getActiveUniform(GLuint index,
1368 GLsizei bufsize,
1369 GLsizei *length,
1370 GLint *size,
1371 GLenum *type,
1372 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001373{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001375 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001376 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001377 ASSERT(index < mState.mUniforms.size());
1378 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379
1380 if (bufsize > 0)
1381 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001382 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001383 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384 }
1385
Olli Etuaho465835d2017-09-26 13:34:10 +03001386 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001387 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001388 }
1389 else
1390 {
1391 if (bufsize > 0)
1392 {
1393 name[0] = '\0';
1394 }
1395
1396 if (length)
1397 {
1398 *length = 0;
1399 }
1400
1401 *size = 0;
1402 *type = GL_NONE;
1403 }
1404}
1405
Geoff Lange1a27752015-10-05 13:16:04 -04001406GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001407{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001409 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001410 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001411 }
1412 else
1413 {
1414 return 0;
1415 }
1416}
1417
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001418size_t Program::getActiveBufferVariableCount() const
1419{
1420 return mLinked ? mState.mBufferVariables.size() : 0;
1421}
1422
Geoff Lange1a27752015-10-05 13:16:04 -04001423GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001424{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001425 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426
1427 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001428 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001429 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001431 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001433 size_t length = uniform.name.length() + 1u;
1434 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435 {
1436 length += 3; // Counting in "[0]".
1437 }
1438 maxLength = std::max(length, maxLength);
1439 }
1440 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001441 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442
Jamie Madill62d31cb2015-09-11 13:25:51 -04001443 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444}
1445
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446bool Program::isValidUniformLocation(GLint location) const
1447{
Jamie Madille2e406c2016-06-02 13:04:10 -04001448 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001449 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001450 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001451}
1452
Jamie Madill62d31cb2015-09-11 13:25:51 -04001453const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001455 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001456 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001457}
1458
Jamie Madillac4e9c32017-01-13 14:07:12 -05001459const VariableLocation &Program::getUniformLocation(GLint location) const
1460{
1461 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1462 return mState.mUniformLocations[location];
1463}
1464
1465const std::vector<VariableLocation> &Program::getUniformLocations() const
1466{
1467 return mState.mUniformLocations;
1468}
1469
1470const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1471{
1472 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1473 return mState.mUniforms[index];
1474}
1475
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001476const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1477{
1478 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1479 return mState.mBufferVariables[index];
1480}
1481
Jamie Madill62d31cb2015-09-11 13:25:51 -04001482GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483{
Olli Etuahod2551232017-10-26 20:03:33 +03001484 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001485}
1486
Jamie Madill62d31cb2015-09-11 13:25:51 -04001487GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001488{
Jamie Madille7d84322017-01-10 18:21:59 -05001489 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001490}
1491
1492void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1493{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001494 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1495 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001496 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497}
1498
1499void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1500{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001501 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1502 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001503 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
1506void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1507{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001508 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1509 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001510 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511}
1512
1513void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1514{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001515 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1516 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001517 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518}
1519
Jamie Madill81c2e252017-09-09 23:32:46 -04001520Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001522 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1523 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1524
Jamie Madill81c2e252017-09-09 23:32:46 -04001525 mProgram->setUniform1iv(location, clampedCount, v);
1526
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001527 if (mState.isSamplerUniformIndex(locationInfo.index))
1528 {
1529 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001530 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001531 }
1532
Jamie Madill81c2e252017-09-09 23:32:46 -04001533 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001534}
1535
1536void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1537{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001538 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1539 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001540 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001541}
1542
1543void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1544{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001545 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1546 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001547 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548}
1549
1550void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1551{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001552 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1553 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001554 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555}
1556
1557void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1558{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001559 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1560 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001561 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562}
1563
1564void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1565{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001566 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1567 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001568 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001569}
1570
1571void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1572{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001573 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1574 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001575 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576}
1577
1578void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1579{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001580 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1581 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001582 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
1585void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1586{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001587 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001588 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589}
1590
1591void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1592{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001593 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001594 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001595}
1596
1597void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1598{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001599 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001600 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601}
1602
1603void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1604{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001605 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001606 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001607}
1608
1609void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1610{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001611 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001612 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001613}
1614
1615void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1616{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001617 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001618 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619}
1620
1621void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1622{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001623 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001624 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625}
1626
1627void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1628{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001629 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001630 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631}
1632
1633void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1634{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001635 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001636 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637}
1638
Jamie Madill54164b02017-08-28 15:17:37 -04001639void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640{
Jamie Madill54164b02017-08-28 15:17:37 -04001641 const auto &uniformLocation = mState.getUniformLocations()[location];
1642 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1643
1644 GLenum nativeType = gl::VariableComponentType(uniform.type);
1645 if (nativeType == GL_FLOAT)
1646 {
1647 mProgram->getUniformfv(context, location, v);
1648 }
1649 else
1650 {
1651 getUniformInternal(context, v, location, nativeType,
1652 gl::VariableComponentCount(uniform.type));
1653 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654}
1655
Jamie Madill54164b02017-08-28 15:17:37 -04001656void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001657{
Jamie Madill54164b02017-08-28 15:17:37 -04001658 const auto &uniformLocation = mState.getUniformLocations()[location];
1659 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1660
1661 GLenum nativeType = gl::VariableComponentType(uniform.type);
1662 if (nativeType == GL_INT || nativeType == GL_BOOL)
1663 {
1664 mProgram->getUniformiv(context, location, v);
1665 }
1666 else
1667 {
1668 getUniformInternal(context, v, location, nativeType,
1669 gl::VariableComponentCount(uniform.type));
1670 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671}
1672
Jamie Madill54164b02017-08-28 15:17:37 -04001673void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674{
Jamie Madill54164b02017-08-28 15:17:37 -04001675 const auto &uniformLocation = mState.getUniformLocations()[location];
1676 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1677
1678 GLenum nativeType = gl::VariableComponentType(uniform.type);
1679 if (nativeType == GL_UNSIGNED_INT)
1680 {
1681 mProgram->getUniformuiv(context, location, v);
1682 }
1683 else
1684 {
1685 getUniformInternal(context, v, location, nativeType,
1686 gl::VariableComponentCount(uniform.type));
1687 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688}
1689
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690void Program::flagForDeletion()
1691{
1692 mDeleteStatus = true;
1693}
1694
1695bool Program::isFlaggedForDeletion() const
1696{
1697 return mDeleteStatus;
1698}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001699
Brandon Jones43a53e22014-08-28 16:23:22 -07001700void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001701{
1702 mInfoLog.reset();
1703
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001705 {
Geoff Lang92019432017-11-20 13:09:34 -05001706 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001707 }
1708 else
1709 {
Jamie Madillf6113162015-05-07 11:49:21 -04001710 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001711 }
1712}
1713
Geoff Lang7dd2e102014-11-10 15:19:26 -05001714bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1715{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001716 // Skip cache if we're using an infolog, so we get the full error.
1717 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1718 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1719 {
1720 return mCachedValidateSamplersResult.value();
1721 }
1722
1723 if (mTextureUnitTypesCache.empty())
1724 {
1725 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1726 }
1727 else
1728 {
1729 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1730 }
1731
1732 // if any two active samplers in a program are of different types, but refer to the same
1733 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1734 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001735 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001736 {
Jamie Madill54164b02017-08-28 15:17:37 -04001737 if (samplerBinding.unreferenced)
1738 continue;
1739
Jamie Madille7d84322017-01-10 18:21:59 -05001740 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001741
Jamie Madille7d84322017-01-10 18:21:59 -05001742 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001743 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001744 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1745 {
1746 if (infoLog)
1747 {
1748 (*infoLog) << "Sampler uniform (" << textureUnit
1749 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1750 << caps.maxCombinedTextureImageUnits << ")";
1751 }
1752
1753 mCachedValidateSamplersResult = false;
1754 return false;
1755 }
1756
1757 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1758 {
1759 if (textureType != mTextureUnitTypesCache[textureUnit])
1760 {
1761 if (infoLog)
1762 {
1763 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1764 "image unit ("
1765 << textureUnit << ").";
1766 }
1767
1768 mCachedValidateSamplersResult = false;
1769 return false;
1770 }
1771 }
1772 else
1773 {
1774 mTextureUnitTypesCache[textureUnit] = textureType;
1775 }
1776 }
1777 }
1778
1779 mCachedValidateSamplersResult = true;
1780 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781}
1782
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001783bool Program::isValidated() const
1784{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785 return mValidated;
1786}
1787
Geoff Lange1a27752015-10-05 13:16:04 -04001788GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001790 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791}
1792
jchen1058f67be2017-10-27 08:59:27 +08001793GLuint Program::getActiveAtomicCounterBufferCount() const
1794{
1795 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1796}
1797
Jiajia Qin729b2c62017-08-14 09:36:11 +08001798GLuint Program::getActiveShaderStorageBlockCount() const
1799{
1800 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1801}
1802
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001803void Program::getActiveUniformBlockName(const GLuint blockIndex,
1804 GLsizei bufSize,
1805 GLsizei *length,
1806 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001808 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
1809}
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001811void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
1812 GLsizei bufSize,
1813 GLsizei *length,
1814 GLchar *blockName) const
1815{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001817 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001818}
1819
Geoff Lange1a27752015-10-05 13:16:04 -04001820GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001821{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 int maxLength = 0;
1823
1824 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001825 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001826 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1828 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001829 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 if (!uniformBlock.name.empty())
1831 {
jchen10af713a22017-04-19 09:10:56 +08001832 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1833 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 }
1835 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001836 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837
1838 return maxLength;
1839}
1840
Geoff Lange1a27752015-10-05 13:16:04 -04001841GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001843 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
1844}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001845
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001846GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
1847{
1848 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001849}
1850
Jiajia Qin729b2c62017-08-14 09:36:11 +08001851const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001852{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001853 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1854 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001855}
1856
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001857const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
1858{
1859 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
1860 return mState.mShaderStorageBlocks[index];
1861}
1862
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001863void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1864{
jchen107a20b972017-06-13 14:25:26 +08001865 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001866 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001867 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001868}
1869
1870GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1871{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001872 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001873}
1874
Jiajia Qin729b2c62017-08-14 09:36:11 +08001875GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1876{
1877 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1878}
1879
Geoff Lang48dcae72014-02-05 16:28:24 -05001880void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1881{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001882 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001883 for (GLsizei i = 0; i < count; i++)
1884 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001885 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001886 }
1887
Jamie Madill48ef11b2016-04-27 15:21:52 -04001888 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001889}
1890
1891void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1892{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001894 {
jchen10a9042d32017-03-17 08:50:45 +08001895 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1896 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1897 std::string varName = var.nameWithArrayIndex();
1898 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001899 if (length)
1900 {
1901 *length = lastNameIdx;
1902 }
1903 if (size)
1904 {
jchen10a9042d32017-03-17 08:50:45 +08001905 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001906 }
1907 if (type)
1908 {
jchen10a9042d32017-03-17 08:50:45 +08001909 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001910 }
1911 if (name)
1912 {
jchen10a9042d32017-03-17 08:50:45 +08001913 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001914 name[lastNameIdx] = '\0';
1915 }
1916 }
1917}
1918
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001919GLsizei Program::getTransformFeedbackVaryingCount() const
1920{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001921 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001922 {
jchen10a9042d32017-03-17 08:50:45 +08001923 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001924 }
1925 else
1926 {
1927 return 0;
1928 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001929}
1930
1931GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1932{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001933 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001934 {
1935 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001936 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001937 {
jchen10a9042d32017-03-17 08:50:45 +08001938 maxSize =
1939 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001940 }
1941
1942 return maxSize;
1943 }
1944 else
1945 {
1946 return 0;
1947 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001948}
1949
1950GLenum Program::getTransformFeedbackBufferMode() const
1951{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001952 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001953}
1954
Jiawei Shao73618602017-12-20 15:47:15 +08001955bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
1956{
1957 Shader *vertexShader = mState.mAttachedVertexShader;
1958 Shader *fragmentShader = mState.mAttachedFragmentShader;
1959 Shader *computeShader = mState.mAttachedComputeShader;
1960
1961 bool isComputeShaderAttached = (computeShader != nullptr);
1962 bool isGraphicsShaderAttached = (vertexShader != nullptr || fragmentShader != nullptr);
1963 // Check whether we both have a compute and non-compute shaders attached.
1964 // If there are of both types attached, then linking should fail.
1965 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
1966 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
1967 {
1968 infoLog << "Both compute and graphics shaders are attached to the same program.";
1969 return false;
1970 }
1971
1972 if (computeShader)
1973 {
1974 if (!computeShader->isCompiled(context))
1975 {
1976 infoLog << "Attached compute shader is not compiled.";
1977 return false;
1978 }
1979 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
1980
1981 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
1982
1983 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
1984 // If the work group size is not specified, a link time error should occur.
1985 if (!mState.mComputeShaderLocalSize.isDeclared())
1986 {
1987 infoLog << "Work group size is not specified.";
1988 return false;
1989 }
1990 }
1991 else
1992 {
1993 if (!fragmentShader || !fragmentShader->isCompiled(context))
1994 {
1995 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
1996 return false;
1997 }
1998 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
1999
2000 if (!vertexShader || !vertexShader->isCompiled(context))
2001 {
2002 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2003 return false;
2004 }
2005 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
2006
2007 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
2008 {
2009 infoLog << "Fragment shader version does not match vertex shader version.";
2010 return false;
2011 }
2012 }
2013
2014 return true;
2015}
2016
jchen10910a3da2017-11-15 09:40:11 +08002017GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2018{
2019 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2020 {
2021 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2022 if (tf.nameWithArrayIndex() == name)
2023 {
2024 return tfIndex;
2025 }
2026 }
2027 return GL_INVALID_INDEX;
2028}
2029
2030const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2031{
2032 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2033 return mState.mLinkedTransformFeedbackVaryings[index];
2034}
2035
Jamie Madillbd044ed2017-06-05 12:59:21 -04002036bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002038 Shader *vertexShader = mState.mAttachedVertexShader;
2039 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05002040
Jamie Madillbd044ed2017-06-05 12:59:21 -04002041 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002042
Jiawei Shao3d404882017-10-16 13:30:48 +08002043 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
2044 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045
Sami Väisänen46eaa942016-06-29 10:26:37 +03002046 std::map<GLuint, std::string> staticFragmentInputLocations;
2047
Jamie Madill4cff2472015-08-21 16:53:18 -04002048 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 bool matched = false;
2051
2052 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04002053 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054 {
2055 continue;
2056 }
2057
Jamie Madill4cff2472015-08-21 16:53:18 -04002058 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04002060 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04002062 ASSERT(!input.isBuiltIn());
Jiawei Shao73618602017-12-20 15:47:15 +08002063 if (!LinkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002064 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065 {
2066 return false;
2067 }
2068
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069 matched = true;
2070 break;
2071 }
2072 }
2073
2074 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04002075 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04002077 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 return false;
2079 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002080
2081 // Check for aliased path rendering input bindings (if any).
2082 // If more than one binding refer statically to the same
2083 // location the link must fail.
2084
2085 if (!output.staticUse)
2086 continue;
2087
2088 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
2089 if (inputBinding == -1)
2090 continue;
2091
2092 const auto it = staticFragmentInputLocations.find(inputBinding);
2093 if (it == std::end(staticFragmentInputLocations))
2094 {
2095 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
2096 }
2097 else
2098 {
2099 infoLog << "Binding for fragment input " << output.name << " conflicts with "
2100 << it->second;
2101 return false;
2102 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 }
2104
Jamie Madillbd044ed2017-06-05 12:59:21 -04002105 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05002106 {
2107 return false;
2108 }
2109
Jamie Madillada9ecc2015-08-17 12:53:37 -04002110 // TODO(jmadill): verify no unmatched vertex varyings?
2111
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 return true;
2113}
2114
Jamie Madillbd044ed2017-06-05 12:59:21 -04002115bool Program::linkUniforms(const Context *context,
2116 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002117 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002118{
Olli Etuahob78707c2017-03-09 15:03:11 +00002119 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002120 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002121 {
2122 return false;
2123 }
2124
Olli Etuahob78707c2017-03-09 15:03:11 +00002125 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002126
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002127 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002128
jchen10eaef1e52017-06-13 10:44:11 +08002129 if (!linkAtomicCounterBuffers())
2130 {
2131 return false;
2132 }
2133
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002134 return true;
2135}
2136
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002137void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002138{
Jamie Madill982f6e02017-06-07 14:33:04 -04002139 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2140 unsigned int low = high;
2141
jchen10eaef1e52017-06-13 10:44:11 +08002142 for (auto counterIter = mState.mUniforms.rbegin();
2143 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2144 {
2145 --low;
2146 }
2147
2148 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2149
2150 high = low;
2151
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002152 for (auto imageIter = mState.mUniforms.rbegin();
2153 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2154 {
2155 --low;
2156 }
2157
2158 mState.mImageUniformRange = RangeUI(low, high);
2159
2160 // If uniform is a image type, insert it into the mImageBindings array.
2161 for (unsigned int imageIndex : mState.mImageUniformRange)
2162 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002163 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2164 // cannot load values into a uniform defined as an image. if declare without a
2165 // binding qualifier, any uniform image variable (include all elements of
2166 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002167 auto &imageUniform = mState.mUniforms[imageIndex];
2168 if (imageUniform.binding == -1)
2169 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002170 mState.mImageBindings.emplace_back(
2171 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002172 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002173 else
2174 {
2175 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002176 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002177 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002178 }
2179
2180 high = low;
2181
2182 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002183 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002184 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002185 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002186 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002187
2188 mState.mSamplerUniformRange = RangeUI(low, high);
2189
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002190 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002191 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002192 {
2193 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2194 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2195 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002196 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002197 }
2198}
2199
jchen10eaef1e52017-06-13 10:44:11 +08002200bool Program::linkAtomicCounterBuffers()
2201{
2202 for (unsigned int index : mState.mAtomicCounterUniformRange)
2203 {
2204 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002205 uniform.blockInfo.offset = uniform.offset;
2206 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2207 uniform.blockInfo.matrixStride = 0;
2208 uniform.blockInfo.isRowMajorMatrix = false;
2209
jchen10eaef1e52017-06-13 10:44:11 +08002210 bool found = false;
2211 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2212 ++bufferIndex)
2213 {
2214 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2215 if (buffer.binding == uniform.binding)
2216 {
2217 buffer.memberIndexes.push_back(index);
2218 uniform.bufferIndex = bufferIndex;
2219 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002220 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002221 break;
2222 }
2223 }
2224 if (!found)
2225 {
2226 AtomicCounterBuffer atomicCounterBuffer;
2227 atomicCounterBuffer.binding = uniform.binding;
2228 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002229 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002230 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2231 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2232 }
2233 }
2234 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2235 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2236
2237 return true;
2238}
2239
Jiawei Shao73618602017-12-20 15:47:15 +08002240bool Program::LinkValidateInterfaceBlockFields(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002241 const std::string &uniformName,
2242 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002243 const sh::InterfaceBlockField &fragmentUniform,
2244 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002246 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
Jiawei Shao73618602017-12-20 15:47:15 +08002247 if (!LinkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002248 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249 {
2250 return false;
2251 }
2252
2253 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2254 {
Jamie Madillf6113162015-05-07 11:49:21 -04002255 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002256 return false;
2257 }
2258
2259 return true;
2260}
2261
Jamie Madilleb979bf2016-11-15 12:28:46 -05002262// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002263bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002264{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002265 const ContextState &data = context->getContextState();
2266 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002267
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002269 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002270 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002271
2272 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002273 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002274 {
Jamie Madillf6113162015-05-07 11:49:21 -04002275 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002276 return false;
2277 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002279 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002280
Jamie Madillc349ec02015-08-21 16:53:12 -04002281 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002282 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002283 {
Olli Etuahod2551232017-10-26 20:03:33 +03002284 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2285 // structures, so we don't need to worry about adjusting their names or generating entries
2286 // for each member/element (unlike uniforms for example).
2287 ASSERT(!attribute.isArray() && !attribute.isStruct());
2288
Jamie Madilleb979bf2016-11-15 12:28:46 -05002289 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002290 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002291 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002292 attribute.location = bindingLocation;
2293 }
2294
2295 if (attribute.location != -1)
2296 {
2297 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002298 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002299
Jamie Madill63805b42015-08-25 13:17:39 -04002300 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002301 {
Jamie Madillf6113162015-05-07 11:49:21 -04002302 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002303 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304
2305 return false;
2306 }
2307
Jamie Madill63805b42015-08-25 13:17:39 -04002308 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002309 {
Jamie Madill63805b42015-08-25 13:17:39 -04002310 const int regLocation = attribute.location + reg;
2311 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002312
2313 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002314 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002315 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002316 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002317 // TODO(jmadill): fix aliasing on ES2
2318 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002319 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002320 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002321 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 return false;
2323 }
2324 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002325 else
2326 {
Jamie Madill63805b42015-08-25 13:17:39 -04002327 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002328 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002329
Jamie Madill63805b42015-08-25 13:17:39 -04002330 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002331 }
2332 }
2333 }
2334
2335 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002336 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002337 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002338 // Not set by glBindAttribLocation or by location layout qualifier
2339 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002340 {
Jamie Madill63805b42015-08-25 13:17:39 -04002341 int regs = VariableRegisterCount(attribute.type);
2342 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343
Jamie Madill63805b42015-08-25 13:17:39 -04002344 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345 {
Jamie Madillf6113162015-05-07 11:49:21 -04002346 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002347 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002348 }
2349
Jamie Madillc349ec02015-08-21 16:53:12 -04002350 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351 }
2352 }
2353
Jamie Madill48ef11b2016-04-27 15:21:52 -04002354 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002355 {
Jamie Madill63805b42015-08-25 13:17:39 -04002356 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002357 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002358
Jamie Madillbd159f02017-10-09 19:39:06 -04002359 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002360 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002361 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2362 mState.mActiveAttribLocationsMask.set(location);
2363 mState.mMaxActiveAttribLocation =
2364 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365 }
2366 }
2367
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368 return true;
2369}
2370
Jiawei Shao73618602017-12-20 15:47:15 +08002371bool Program::ValidateGraphicsInterfaceBlocks(
Martin Radev4c4c8e72016-08-04 12:25:34 +03002372 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2373 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002374 InfoLog &infoLog,
Jiawei Shao73618602017-12-20 15:47:15 +08002375 bool webglCompatibility)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002376{
2377 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002378 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2379 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002380
2381 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2382 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002383 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002384 }
2385
Jamie Madille473dee2015-08-18 14:49:01 -04002386 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002388 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2389 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390 {
2391 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Jiawei Shao73618602017-12-20 15:47:15 +08002392 if (!AreMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002393 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002394 {
2395 return false;
2396 }
2397 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002398 // TODO(jiajia.qin@intel.com): Add
2399 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002400 }
2401 return true;
2402}
Jamie Madille473dee2015-08-18 14:49:01 -04002403
Jiajia Qin729b2c62017-08-14 09:36:11 +08002404bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002405{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002406 const auto &caps = context->getCaps();
2407
Martin Radev4c4c8e72016-08-04 12:25:34 +03002408 if (mState.mAttachedComputeShader)
2409 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002410 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002411 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002412
Jiajia Qin729b2c62017-08-14 09:36:11 +08002413 if (!validateInterfaceBlocksCount(
2414 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002415 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2416 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002417 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002418 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002419 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002420
2421 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2422 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2423 computeShaderStorageBlocks,
2424 "Compute shader shader storage block count exceeds "
2425 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2426 infoLog))
2427 {
2428 return false;
2429 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002430 return true;
2431 }
2432
Jamie Madillbd044ed2017-06-05 12:59:21 -04002433 Shader &vertexShader = *mState.mAttachedVertexShader;
2434 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002435
Jiajia Qin729b2c62017-08-14 09:36:11 +08002436 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2437 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002438
Jiajia Qin729b2c62017-08-14 09:36:11 +08002439 if (!validateInterfaceBlocksCount(
2440 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002441 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2442 {
2443 return false;
2444 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002445 if (!validateInterfaceBlocksCount(
2446 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002447 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2448 infoLog))
2449 {
2450
2451 return false;
2452 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002453
2454 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao73618602017-12-20 15:47:15 +08002455 if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog,
2456 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002457 {
2458 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002459 }
Jamie Madille473dee2015-08-18 14:49:01 -04002460
Jiajia Qin729b2c62017-08-14 09:36:11 +08002461 if (context->getClientVersion() >= Version(3, 1))
2462 {
2463 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2464 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2465
2466 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2467 vertexShaderStorageBlocks,
2468 "Vertex shader shader storage block count exceeds "
2469 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2470 infoLog))
2471 {
2472 return false;
2473 }
2474 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2475 fragmentShaderStorageBlocks,
2476 "Fragment shader shader storage block count exceeds "
2477 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2478 infoLog))
2479 {
2480
2481 return false;
2482 }
2483
Jiawei Shao73618602017-12-20 15:47:15 +08002484 if (!ValidateGraphicsInterfaceBlocks(vertexShaderStorageBlocks, fragmentShaderStorageBlocks,
2485 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002486 {
2487 return false;
2488 }
2489 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 return true;
2491}
2492
Jiawei Shao73618602017-12-20 15:47:15 +08002493bool Program::AreMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002494 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002495 const sh::InterfaceBlock &fragmentInterfaceBlock,
Jiawei Shao73618602017-12-20 15:47:15 +08002496 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002497{
Jiawei Shao73618602017-12-20 15:47:15 +08002498 const char *blockName = vertexInterfaceBlock.name.c_str();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002499 // validate blocks for the same member types
2500 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2501 {
Jamie Madillf6113162015-05-07 11:49:21 -04002502 infoLog << "Types for interface block '" << blockName
2503 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002504 return false;
2505 }
2506 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2507 {
Jamie Madillf6113162015-05-07 11:49:21 -04002508 infoLog << "Array sizes differ for interface block '" << blockName
2509 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002510 return false;
2511 }
jchen10af713a22017-04-19 09:10:56 +08002512 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
jchen10af713a22017-04-19 09:10:56 +08002513 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002514 {
Jamie Madillf6113162015-05-07 11:49:21 -04002515 infoLog << "Layout qualifiers differ for interface block '" << blockName
2516 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002517 return false;
2518 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002519 const unsigned int numBlockMembers =
2520 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002521 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2522 {
2523 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2524 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2525 if (vertexMember.name != fragmentMember.name)
2526 {
Jamie Madillf6113162015-05-07 11:49:21 -04002527 infoLog << "Name mismatch for field " << blockMemberIndex
2528 << " of interface block '" << blockName
2529 << "': (in vertex: '" << vertexMember.name
2530 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002531 return false;
2532 }
2533 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Jiawei Shao73618602017-12-20 15:47:15 +08002534 if (!LinkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002535 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002536 {
2537 return false;
2538 }
2539 }
2540 return true;
2541}
2542
Jiawei Shao73618602017-12-20 15:47:15 +08002543bool Program::LinkValidateVariablesBase(InfoLog &infoLog,
2544 const std::string &variableName,
2545 const sh::ShaderVariable &vertexVariable,
2546 const sh::ShaderVariable &fragmentVariable,
2547 bool validatePrecision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002548{
2549 if (vertexVariable.type != fragmentVariable.type)
2550 {
Jamie Madillf6113162015-05-07 11:49:21 -04002551 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002552 return false;
2553 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002554 if (vertexVariable.arraySizes != fragmentVariable.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002555 {
Jamie Madillf6113162015-05-07 11:49:21 -04002556 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002557 return false;
2558 }
2559 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2560 {
Jamie Madillf6113162015-05-07 11:49:21 -04002561 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002562 return false;
2563 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002564 if (vertexVariable.structName != fragmentVariable.structName)
2565 {
2566 infoLog << "Structure names for " << variableName
2567 << " differ between vertex and fragment shaders";
2568 return false;
2569 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002570
2571 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2572 {
Jamie Madillf6113162015-05-07 11:49:21 -04002573 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002574 return false;
2575 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002576 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002577 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2578 {
2579 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2580 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2581
2582 if (vertexMember.name != fragmentMember.name)
2583 {
Jamie Madillf6113162015-05-07 11:49:21 -04002584 infoLog << "Name mismatch for field '" << memberIndex
2585 << "' of " << variableName
2586 << ": (in vertex: '" << vertexMember.name
2587 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002588 return false;
2589 }
2590
2591 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2592 vertexMember.name + "'";
2593
Jiawei Shao73618602017-12-20 15:47:15 +08002594 if (!LinkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember,
2595 validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002596 {
2597 return false;
2598 }
2599 }
2600
2601 return true;
2602}
2603
Jiawei Shao73618602017-12-20 15:47:15 +08002604bool Program::LinkValidateVaryings(InfoLog &infoLog,
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002605 const std::string &varyingName,
2606 const sh::Varying &vertexVarying,
2607 const sh::Varying &fragmentVarying,
2608 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002609{
Jiawei Shao73618602017-12-20 15:47:15 +08002610 if (!LinkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002611 {
2612 return false;
2613 }
2614
Jamie Madille9cc4692015-02-19 16:00:13 -05002615 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002616 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002617 infoLog << "Interpolation types for " << varyingName
2618 << " differ between vertex and fragment shaders.";
2619 return false;
2620 }
2621
2622 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2623 {
2624 infoLog << "Invariance for " << varyingName
2625 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002626 return false;
2627 }
2628
2629 return true;
2630}
2631
Jamie Madillbd044ed2017-06-05 12:59:21 -04002632bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002633{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002634 Shader *vertexShader = mState.mAttachedVertexShader;
2635 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002636 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2637 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002638 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002639
2640 if (shaderVersion != 100)
2641 {
2642 // Only ESSL 1.0 has restrictions on matching input and output invariance
2643 return true;
2644 }
2645
2646 bool glPositionIsInvariant = false;
2647 bool glPointSizeIsInvariant = false;
2648 bool glFragCoordIsInvariant = false;
2649 bool glPointCoordIsInvariant = false;
2650
2651 for (const sh::Varying &varying : vertexVaryings)
2652 {
2653 if (!varying.isBuiltIn())
2654 {
2655 continue;
2656 }
2657 if (varying.name.compare("gl_Position") == 0)
2658 {
2659 glPositionIsInvariant = varying.isInvariant;
2660 }
2661 else if (varying.name.compare("gl_PointSize") == 0)
2662 {
2663 glPointSizeIsInvariant = varying.isInvariant;
2664 }
2665 }
2666
2667 for (const sh::Varying &varying : fragmentVaryings)
2668 {
2669 if (!varying.isBuiltIn())
2670 {
2671 continue;
2672 }
2673 if (varying.name.compare("gl_FragCoord") == 0)
2674 {
2675 glFragCoordIsInvariant = varying.isInvariant;
2676 }
2677 else if (varying.name.compare("gl_PointCoord") == 0)
2678 {
2679 glPointCoordIsInvariant = varying.isInvariant;
2680 }
2681 }
2682
2683 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2684 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2685 // Not requiring invariance to match is supported by:
2686 // dEQP, WebGL CTS, Nexus 5X GLES
2687 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2688 {
2689 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2690 "declared invariant.";
2691 return false;
2692 }
2693 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2694 {
2695 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2696 "declared invariant.";
2697 return false;
2698 }
2699
2700 return true;
2701}
2702
jchen10a9042d32017-03-17 08:50:45 +08002703bool Program::linkValidateTransformFeedback(const gl::Context *context,
2704 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002705 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002706 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002707{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002708
jchen108225e732017-11-14 16:29:03 +08002709 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04002710 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002711 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002712 {
jchen10a9042d32017-03-17 08:50:45 +08002713 if (context->getClientVersion() < Version(3, 1) &&
2714 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002715 {
Geoff Lang1a683462015-09-29 15:09:59 -04002716 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002717 return false;
2718 }
jchen108225e732017-11-14 16:29:03 +08002719 if (context->getClientVersion() >= Version(3, 1))
2720 {
2721 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2722 {
2723 infoLog << "Two transform feedback varyings include the same array element ("
2724 << tfVaryingName << ").";
2725 return false;
2726 }
2727 }
2728 else
2729 {
2730 if (uniqueNames.count(tfVaryingName) > 0)
2731 {
2732 infoLog << "Two transform feedback varyings specify the same output variable ("
2733 << tfVaryingName << ").";
2734 return false;
2735 }
2736 }
2737 uniqueNames.insert(tfVaryingName);
2738 }
2739
2740 // Validate against program varyings.
2741 size_t totalComponents = 0;
2742 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
2743 {
2744 std::vector<unsigned int> subscripts;
2745 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
2746
2747 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
2748 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08002749 {
2750 infoLog << "Transform feedback varying " << tfVaryingName
2751 << " does not exist in the vertex shader.";
2752 return false;
2753 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002754
jchen108225e732017-11-14 16:29:03 +08002755 // Validate the matching variable.
2756 if (var->isStruct())
2757 {
2758 infoLog << "Struct cannot be captured directly (" << baseName << ").";
2759 return false;
2760 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002761
jchen108225e732017-11-14 16:29:03 +08002762 size_t elementCount = 0;
2763 size_t componentCount = 0;
2764
2765 if (var->isArray())
2766 {
2767 if (context->getClientVersion() < Version(3, 1))
2768 {
2769 infoLog << "Capture of arrays is undefined and not supported.";
2770 return false;
2771 }
2772
2773 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
2774 ASSERT(!var->isArrayOfArrays());
2775
2776 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
2777 {
2778 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
2779 return false;
2780 }
2781 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
2782 }
2783 else
2784 {
2785 if (!subscripts.empty())
2786 {
2787 infoLog << "Varying '" << baseName
2788 << "' is not an array to be captured by element.";
2789 return false;
2790 }
2791 elementCount = 1;
2792 }
2793
2794 // TODO(jmadill): Investigate implementation limits on D3D11
2795 componentCount = VariableComponentCount(var->type) * elementCount;
2796 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
2797 componentCount > caps.maxTransformFeedbackSeparateComponents)
2798 {
2799 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
2800 << componentCount << ") exceed the maximum separate components ("
2801 << caps.maxTransformFeedbackSeparateComponents << ").";
2802 return false;
2803 }
2804
2805 totalComponents += componentCount;
2806 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
2807 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
2808 {
2809 infoLog << "Transform feedback varying total components (" << totalComponents
2810 << ") exceed the maximum interleaved components ("
2811 << caps.maxTransformFeedbackInterleavedComponents << ").";
2812 return false;
2813 }
2814 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002815 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002816}
2817
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002818bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2819{
2820 const std::vector<sh::Uniform> &vertexUniforms =
2821 mState.mAttachedVertexShader->getUniforms(context);
2822 const std::vector<sh::Uniform> &fragmentUniforms =
2823 mState.mAttachedFragmentShader->getUniforms(context);
2824 const std::vector<sh::Attribute> &attributes =
2825 mState.mAttachedVertexShader->getActiveAttributes(context);
2826 for (const auto &attrib : attributes)
2827 {
2828 for (const auto &uniform : vertexUniforms)
2829 {
2830 if (uniform.name == attrib.name)
2831 {
2832 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2833 return false;
2834 }
2835 }
2836 for (const auto &uniform : fragmentUniforms)
2837 {
2838 if (uniform.name == attrib.name)
2839 {
2840 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2841 return false;
2842 }
2843 }
2844 }
2845 return true;
2846}
2847
Jamie Madill3c1da042017-11-27 18:33:40 -05002848void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002849{
2850 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002851 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002852 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002853 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002854 std::vector<unsigned int> subscripts;
2855 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002856 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002857 if (!subscripts.empty())
2858 {
2859 subscript = subscripts.back();
2860 }
Jamie Madill192745a2016-12-22 15:58:21 -05002861 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002862 {
Jamie Madill192745a2016-12-22 15:58:21 -05002863 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002864 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002865 {
jchen10a9042d32017-03-17 08:50:45 +08002866 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2867 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002868 break;
2869 }
jchen108225e732017-11-14 16:29:03 +08002870 else if (varying->isStruct())
2871 {
2872 const auto *field = FindShaderVarField(*varying, tfVaryingName);
2873 if (field != nullptr)
2874 {
2875 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
2876 break;
2877 }
2878 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002879 }
2880 }
2881}
2882
Jamie Madill3c1da042017-11-27 18:33:40 -05002883ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002884{
Jamie Madill3c1da042017-11-27 18:33:40 -05002885 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002886
Jiawei Shao3d404882017-10-16 13:30:48 +08002887 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002888 {
Jamie Madill192745a2016-12-22 15:58:21 -05002889 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002890 }
2891
Jiawei Shao3d404882017-10-16 13:30:48 +08002892 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002893 {
Jamie Madill192745a2016-12-22 15:58:21 -05002894 merged[varying.name].fragment = &varying;
2895 }
2896
2897 return merged;
2898}
2899
Jamie Madillbd044ed2017-06-05 12:59:21 -04002900void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002901{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002902 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002903 ASSERT(fragmentShader != nullptr);
2904
Geoff Lange0cff192017-05-30 13:04:56 -04002905 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002906 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08002907 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002908
2909 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002910 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002911 {
2912 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2913 outputVariable.name != "gl_FragData")
2914 {
2915 continue;
2916 }
2917
2918 unsigned int baseLocation =
2919 (outputVariable.location == -1 ? 0u
2920 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03002921
2922 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
2923 // structures, so we may use getBasicTypeElementCount().
2924 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
2925 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04002926 {
2927 const unsigned int location = baseLocation + elementIndex;
2928 if (location >= mState.mOutputVariableTypes.size())
2929 {
2930 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2931 }
Corentin Walleze7557742017-06-01 13:09:57 -04002932 ASSERT(location < mState.mActiveOutputVariables.size());
2933 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002934 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08002935 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04002936 }
2937 }
2938
Jamie Madill80a6fc02015-08-21 16:53:16 -04002939 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002940 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002941 return;
2942
Jamie Madillbd044ed2017-06-05 12:59:21 -04002943 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002944 // TODO(jmadill): any caps validation here?
2945
jchen1015015f72017-03-16 13:54:21 +08002946 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002947 outputVariableIndex++)
2948 {
jchen1015015f72017-03-16 13:54:21 +08002949 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002950
Olli Etuahod2551232017-10-26 20:03:33 +03002951 if (outputVariable.isArray())
2952 {
2953 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
2954 // Resources and including [0] at the end of array variable names.
2955 mState.mOutputVariables[outputVariableIndex].name += "[0]";
2956 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
2957 }
2958
Jamie Madill80a6fc02015-08-21 16:53:16 -04002959 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2960 if (outputVariable.isBuiltIn())
2961 continue;
2962
2963 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03002964 unsigned int baseLocation =
2965 (outputVariable.location == -1 ? 0u
2966 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04002967
Olli Etuaho465835d2017-09-26 13:34:10 +03002968 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
2969 // structures, so we may use getBasicTypeElementCount().
2970 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
2971 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002972 {
Olli Etuahod2551232017-10-26 20:03:33 +03002973 const unsigned int location = baseLocation + elementIndex;
2974 if (location >= mState.mOutputLocations.size())
2975 {
2976 mState.mOutputLocations.resize(location + 1);
2977 }
2978 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03002979 if (outputVariable.isArray())
2980 {
2981 mState.mOutputLocations[location] =
2982 VariableLocation(elementIndex, outputVariableIndex);
2983 }
2984 else
2985 {
2986 VariableLocation locationInfo;
2987 locationInfo.index = outputVariableIndex;
2988 mState.mOutputLocations[location] = locationInfo;
2989 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04002990 }
2991 }
2992}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002993
Olli Etuaho48fed632017-03-16 12:05:30 +00002994void Program::setUniformValuesFromBindingQualifiers()
2995{
Jamie Madill982f6e02017-06-07 14:33:04 -04002996 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002997 {
2998 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2999 if (samplerUniform.binding != -1)
3000 {
Olli Etuahod2551232017-10-26 20:03:33 +03003001 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003002 ASSERT(location != -1);
3003 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003004 for (unsigned int elementIndex = 0;
3005 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003006 {
3007 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3008 }
3009 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3010 boundTextureUnits.data());
3011 }
3012 }
3013}
3014
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003015void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003016{
jchen10af713a22017-04-19 09:10:56 +08003017 // Set initial bindings from shader.
3018 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3019 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003020 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003021 bindUniformBlock(blockIndex, uniformBlock.binding);
3022 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003023}
3024
Jamie Madille7d84322017-01-10 18:21:59 -05003025void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003026 GLsizei clampedCount,
3027 const GLint *v)
3028{
Jamie Madill81c2e252017-09-09 23:32:46 -04003029 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3030 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3031 std::vector<GLuint> *boundTextureUnits =
3032 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003033
Olli Etuaho1734e172017-10-27 15:30:27 +03003034 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003035
3036 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003037 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003038}
3039
3040template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003041GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3042 GLsizei count,
3043 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003044 const T *v)
3045{
Jamie Madill134f93d2017-08-31 17:11:00 -04003046 if (count == 1)
3047 return 1;
3048
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003049 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003050
Corentin Wallez15ac5342016-11-03 17:06:39 -04003051 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3052 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003053 unsigned int remainingElements =
3054 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003055 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003056 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003057
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003058 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003059 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003060 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003061 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003062
3063 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003064}
3065
3066template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003067GLsizei Program::clampMatrixUniformCount(GLint location,
3068 GLsizei count,
3069 GLboolean transpose,
3070 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003071{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003072 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3073
Jamie Madill62d31cb2015-09-11 13:25:51 -04003074 if (!transpose)
3075 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003076 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003077 }
3078
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003079 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003080
3081 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3082 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003083 unsigned int remainingElements =
3084 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003085 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003086}
3087
Jamie Madill54164b02017-08-28 15:17:37 -04003088// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3089// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003090template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003091void Program::getUniformInternal(const Context *context,
3092 DestT *dataOut,
3093 GLint location,
3094 GLenum nativeType,
3095 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003096{
Jamie Madill54164b02017-08-28 15:17:37 -04003097 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003098 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003099 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003100 {
3101 GLint tempValue[16] = {0};
3102 mProgram->getUniformiv(context, location, tempValue);
3103 UniformStateQueryCastLoop<GLboolean>(
3104 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003105 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003106 }
3107 case GL_INT:
3108 {
3109 GLint tempValue[16] = {0};
3110 mProgram->getUniformiv(context, location, tempValue);
3111 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3112 components);
3113 break;
3114 }
3115 case GL_UNSIGNED_INT:
3116 {
3117 GLuint tempValue[16] = {0};
3118 mProgram->getUniformuiv(context, location, tempValue);
3119 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3120 components);
3121 break;
3122 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003123 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003124 {
3125 GLfloat tempValue[16] = {0};
3126 mProgram->getUniformfv(context, location, tempValue);
3127 UniformStateQueryCastLoop<GLfloat>(
3128 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003129 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003130 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003131 default:
3132 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003133 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003134 }
3135}
Jamie Madilla4595b82017-01-11 17:36:34 -05003136
3137bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3138{
3139 // Must be called after samplers are validated.
3140 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3141
3142 for (const auto &binding : mState.mSamplerBindings)
3143 {
3144 GLenum textureType = binding.textureType;
3145 for (const auto &unit : binding.boundTextureUnits)
3146 {
3147 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3148 if (programTextureID == textureID)
3149 {
3150 // TODO(jmadill): Check for appropriate overlap.
3151 return true;
3152 }
3153 }
3154 }
3155
3156 return false;
3157}
3158
Jamie Madilla2c74982016-12-12 11:20:42 -05003159} // namespace gl