blob: 7ccd47090bed293ef496379ae24b089abdc07f41 [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{
Jiawei Shao02f15232017-12-27 10:10:28 +0800417 if (mLazyStream)
418 {
419 mLazyStream.reset(nullptr);
420 }
421}
422
423bool InfoLog::empty() const
424{
425 if (!mLazyStream)
426 {
427 return true;
428 }
429
430 return mLazyStream->rdbuf()->in_avail() == 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000431}
432
Jamie Madill3c1da042017-11-27 18:33:40 -0500433// VariableLocation implementation.
Olli Etuaho1734e172017-10-27 15:30:27 +0300434VariableLocation::VariableLocation() : arrayIndex(0), index(kUnused), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000435{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500436}
437
Olli Etuahoc8538042017-09-27 11:20:15 +0300438VariableLocation::VariableLocation(unsigned int arrayIndex, unsigned int index)
Olli Etuaho1734e172017-10-27 15:30:27 +0300439 : arrayIndex(arrayIndex), index(index), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500440{
Olli Etuahoc8538042017-09-27 11:20:15 +0300441 ASSERT(arrayIndex != GL_INVALID_INDEX);
442}
443
Jamie Madill3c1da042017-11-27 18:33:40 -0500444// SamplerBindings implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500445SamplerBinding::SamplerBinding(GLenum textureTypeIn, size_t elementCount, bool unreferenced)
446 : textureType(textureTypeIn), boundTextureUnits(elementCount, 0), unreferenced(unreferenced)
447{
448}
449
450SamplerBinding::SamplerBinding(const SamplerBinding &other) = default;
451
452SamplerBinding::~SamplerBinding() = default;
453
Jamie Madill3c1da042017-11-27 18:33:40 -0500454// ProgramBindings implementation.
455ProgramBindings::ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500456{
457}
458
Jamie Madill3c1da042017-11-27 18:33:40 -0500459ProgramBindings::~ProgramBindings()
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500460{
461}
462
Jamie Madill3c1da042017-11-27 18:33:40 -0500463void ProgramBindings::bindLocation(GLuint index, const std::string &name)
Geoff Langd8605522016-04-13 10:19:12 -0400464{
465 mBindings[name] = index;
466}
467
Jamie Madill3c1da042017-11-27 18:33:40 -0500468int ProgramBindings::getBinding(const std::string &name) const
Geoff Langd8605522016-04-13 10:19:12 -0400469{
470 auto iter = mBindings.find(name);
471 return (iter != mBindings.end()) ? iter->second : -1;
472}
473
Jamie Madill3c1da042017-11-27 18:33:40 -0500474ProgramBindings::const_iterator ProgramBindings::begin() const
Geoff Langd8605522016-04-13 10:19:12 -0400475{
476 return mBindings.begin();
477}
478
Jamie Madill3c1da042017-11-27 18:33:40 -0500479ProgramBindings::const_iterator ProgramBindings::end() const
Geoff Langd8605522016-04-13 10:19:12 -0400480{
481 return mBindings.end();
482}
483
Jamie Madill3c1da042017-11-27 18:33:40 -0500484// ImageBinding implementation.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500485ImageBinding::ImageBinding(size_t count) : boundImageUnits(count, 0)
486{
487}
488ImageBinding::ImageBinding(GLuint imageUnit, size_t count)
489{
490 for (size_t index = 0; index < count; ++index)
491 {
492 boundImageUnits.push_back(imageUnit + static_cast<GLuint>(index));
493 }
494}
495
496ImageBinding::ImageBinding(const ImageBinding &other) = default;
497
498ImageBinding::~ImageBinding() = default;
499
Jamie Madill3c1da042017-11-27 18:33:40 -0500500// ProgramState implementation.
Jamie Madill48ef11b2016-04-27 15:21:52 -0400501ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500502 : mLabel(),
503 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400504 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300505 mAttachedComputeShader(nullptr),
Jiawei Shao89be29a2017-11-06 14:36:45 +0800506 mAttachedGeometryShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500507 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madillbd159f02017-10-09 19:39:06 -0400508 mMaxActiveAttribLocation(0),
Jamie Madille7d84322017-01-10 18:21:59 -0500509 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800510 mImageUniformRange(0, 0),
511 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300512 mBinaryRetrieveableHint(false),
513 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400514{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300515 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400516}
517
Jamie Madill48ef11b2016-04-27 15:21:52 -0400518ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400519{
Jiawei Shao89be29a2017-11-06 14:36:45 +0800520 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader &&
521 !mAttachedGeometryShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400522}
523
Jamie Madill48ef11b2016-04-27 15:21:52 -0400524const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500525{
526 return mLabel;
527}
528
Jamie Madille7d84322017-01-10 18:21:59 -0500529GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400530{
jchen1015015f72017-03-16 13:54:21 +0800531 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400532}
533
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800534GLuint ProgramState::getBufferVariableIndexFromName(const std::string &name) const
535{
536 return GetResourceIndexFromName(mBufferVariables, name);
537}
538
Jamie Madille7d84322017-01-10 18:21:59 -0500539GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
540{
541 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
542 return mUniformLocations[location].index;
543}
544
545Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
546{
547 GLuint index = getUniformIndexFromLocation(location);
548 if (!isSamplerUniformIndex(index))
549 {
550 return Optional<GLuint>::Invalid();
551 }
552
553 return getSamplerIndexFromUniformIndex(index);
554}
555
556bool ProgramState::isSamplerUniformIndex(GLuint index) const
557{
Jamie Madill982f6e02017-06-07 14:33:04 -0400558 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500559}
560
561GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
562{
563 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400564 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500565}
566
Jamie Madill34ca4f52017-06-13 11:49:39 -0400567GLuint ProgramState::getAttributeLocation(const std::string &name) const
568{
569 for (const sh::Attribute &attribute : mAttributes)
570 {
571 if (attribute.name == name)
572 {
573 return attribute.location;
574 }
575 }
576
577 return static_cast<GLuint>(-1);
578}
579
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500580Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400581 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400582 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500583 mLinked(false),
584 mDeleteStatus(false),
585 mRefCount(0),
586 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500587 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500588{
589 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000590
Geoff Lang7dd2e102014-11-10 15:19:26 -0500591 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592}
593
594Program::~Program()
595{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400596 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
Jamie Madill4928b7c2017-06-20 12:57:39 -0400599void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500600{
601 if (mState.mAttachedVertexShader != nullptr)
602 {
603 mState.mAttachedVertexShader->release(context);
604 mState.mAttachedVertexShader = nullptr;
605 }
606
607 if (mState.mAttachedFragmentShader != nullptr)
608 {
609 mState.mAttachedFragmentShader->release(context);
610 mState.mAttachedFragmentShader = nullptr;
611 }
612
613 if (mState.mAttachedComputeShader != nullptr)
614 {
615 mState.mAttachedComputeShader->release(context);
616 mState.mAttachedComputeShader = nullptr;
617 }
618
Jiawei Shao89be29a2017-11-06 14:36:45 +0800619 if (mState.mAttachedGeometryShader != nullptr)
620 {
621 mState.mAttachedGeometryShader->release(context);
622 mState.mAttachedGeometryShader = nullptr;
623 }
624
Jamie Madillc564c072017-06-01 12:45:42 -0400625 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400626
627 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
Jiawei Shao89be29a2017-11-06 14:36:45 +0800628 !mState.mAttachedComputeShader && !mState.mAttachedGeometryShader);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400629 SafeDelete(mProgram);
630
631 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500632}
633
Geoff Lang70d0f492015-12-10 17:45:46 -0500634void Program::setLabel(const std::string &label)
635{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400636 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500637}
638
639const std::string &Program::getLabel() const
640{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400641 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500642}
643
Jamie Madillef300b12016-10-07 15:12:09 -0400644void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300646 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649 {
Jamie Madillef300b12016-10-07 15:12:09 -0400650 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300651 mState.mAttachedVertexShader = shader;
652 mState.mAttachedVertexShader->addRef();
653 break;
654 }
655 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656 {
Jamie Madillef300b12016-10-07 15:12:09 -0400657 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658 mState.mAttachedFragmentShader = shader;
659 mState.mAttachedFragmentShader->addRef();
660 break;
661 }
662 case GL_COMPUTE_SHADER:
663 {
Jamie Madillef300b12016-10-07 15:12:09 -0400664 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665 mState.mAttachedComputeShader = shader;
666 mState.mAttachedComputeShader->addRef();
667 break;
668 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800669 case GL_GEOMETRY_SHADER_EXT:
670 {
671 ASSERT(!mState.mAttachedGeometryShader);
672 mState.mAttachedGeometryShader = shader;
673 mState.mAttachedGeometryShader->addRef();
674 break;
675 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300676 default:
677 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679}
680
Jamie Madillc1d770e2017-04-13 17:31:24 -0400681void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300683 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300685 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400687 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500688 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 mState.mAttachedVertexShader = nullptr;
690 break;
691 }
692 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400694 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500695 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300696 mState.mAttachedFragmentShader = nullptr;
697 break;
698 }
699 case GL_COMPUTE_SHADER:
700 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400701 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500702 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 mState.mAttachedComputeShader = nullptr;
704 break;
705 }
Jiawei Shao89be29a2017-11-06 14:36:45 +0800706 case GL_GEOMETRY_SHADER_EXT:
707 {
708 ASSERT(mState.mAttachedGeometryShader == shader);
709 shader->release(context);
710 mState.mAttachedGeometryShader = nullptr;
711 break;
712 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300713 default:
714 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000716}
717
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000718int Program::getAttachedShadersCount() const
719{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
Jiawei Shao89be29a2017-11-06 14:36:45 +0800721 (mState.mAttachedComputeShader ? 1 : 0) + (mState.mAttachedGeometryShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000722}
723
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724void Program::bindAttributeLocation(GLuint index, const char *name)
725{
Geoff Langd8605522016-04-13 10:19:12 -0400726 mAttributeBindings.bindLocation(index, name);
727}
728
729void Program::bindUniformLocation(GLuint index, const char *name)
730{
Olli Etuahod2551232017-10-26 20:03:33 +0300731 mUniformLocationBindings.bindLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Sami Väisänen46eaa942016-06-29 10:26:37 +0300734void Program::bindFragmentInputLocation(GLint index, const char *name)
735{
736 mFragmentInputBindings.bindLocation(index, name);
737}
738
Jamie Madillbd044ed2017-06-05 12:59:21 -0400739BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300740{
741 BindingInfo ret;
742 ret.type = GL_NONE;
743 ret.valid = false;
744
Jamie Madillbd044ed2017-06-05 12:59:21 -0400745 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300746 ASSERT(fragmentShader);
747
748 // Find the actual fragment shader varying we're interested in
Jiawei Shao3d404882017-10-16 13:30:48 +0800749 const std::vector<sh::Varying> &inputs = fragmentShader->getInputVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300750
751 for (const auto &binding : mFragmentInputBindings)
752 {
753 if (binding.second != static_cast<GLuint>(index))
754 continue;
755
756 ret.valid = true;
757
Olli Etuahod2551232017-10-26 20:03:33 +0300758 size_t nameLengthWithoutArrayIndex;
759 unsigned int arrayIndex = ParseArrayIndex(binding.first, &nameLengthWithoutArrayIndex);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300760
761 for (const auto &in : inputs)
762 {
Olli Etuahod2551232017-10-26 20:03:33 +0300763 if (in.name.length() == nameLengthWithoutArrayIndex &&
764 angle::BeginsWith(in.name, binding.first, nameLengthWithoutArrayIndex))
Sami Väisänen46eaa942016-06-29 10:26:37 +0300765 {
766 if (in.isArray())
767 {
768 // The client wants to bind either "name" or "name[0]".
769 // GL ES 3.1 spec refers to active array names with language such as:
770 // "if the string identifies the base name of an active array, where the
771 // string would exactly match the name of the variable if the suffix "[0]"
772 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400773 if (arrayIndex == GL_INVALID_INDEX)
774 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300775
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400776 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300777 }
778 else
779 {
780 ret.name = in.mappedName;
781 }
782 ret.type = in.type;
783 return ret;
784 }
785 }
786 }
787
788 return ret;
789}
790
Jamie Madillbd044ed2017-06-05 12:59:21 -0400791void Program::pathFragmentInputGen(const Context *context,
792 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300793 GLenum genMode,
794 GLint components,
795 const GLfloat *coeffs)
796{
797 // If the location is -1 then the command is silently ignored
798 if (index == -1)
799 return;
800
Jamie Madillbd044ed2017-06-05 12:59:21 -0400801 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300802
803 // If the input doesn't exist then then the command is silently ignored
804 // This could happen through optimization for example, the shader translator
805 // decides that a variable is not actually being used and optimizes it away.
806 if (binding.name.empty())
807 return;
808
809 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
810}
811
Martin Radev4c4c8e72016-08-04 12:25:34 +0300812// The attached shaders are checked for linking errors by matching up their variables.
813// Uniform, input and output variables get collected.
814// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500815Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000816{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500817 const auto &data = context->getContextState();
818
Jamie Madill6c58b062017-08-01 13:44:25 -0400819 auto *platform = ANGLEPlatformCurrent();
820 double startTime = platform->currentTime(platform);
821
Jamie Madill6c1f6712017-02-14 19:08:04 -0500822 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000823
Jamie Madill32447362017-06-28 14:53:52 -0400824 ProgramHash programHash;
825 auto *cache = context->getMemoryProgramCache();
826 if (cache)
827 {
828 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400829 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400830 }
831
832 if (mLinked)
833 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400834 double delta = platform->currentTime(platform) - startTime;
835 int us = static_cast<int>(delta * 1000000.0);
836 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400837 return NoError();
838 }
839
840 // Cache load failed, fall through to normal linking.
841 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000842 mInfoLog.reset();
843
Jiawei Shao73618602017-12-20 15:47:15 +0800844 if (!linkValidateShaders(context, mInfoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -0500845 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300846 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400847 }
848
Jiawei Shao73618602017-12-20 15:47:15 +0800849 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500850 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400851 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300852 {
853 return NoError();
854 }
855
Jiajia Qin729b2c62017-08-14 09:36:11 +0800856 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300857 {
858 return NoError();
859 }
860
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800861 ProgramLinkedResources resources = {
862 {0, PackMode::ANGLE_RELAXED},
863 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +0800864 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
865 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500866
867 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
868 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
869
870 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500871 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300872 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500873 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300874 }
875 }
876 else
877 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400878 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300879 {
880 return NoError();
881 }
882
Jamie Madillbd044ed2017-06-05 12:59:21 -0400883 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300884 {
885 return NoError();
886 }
887
Jamie Madillbd044ed2017-06-05 12:59:21 -0400888 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300889 {
890 return NoError();
891 }
892
Jiajia Qin729b2c62017-08-14 09:36:11 +0800893 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300894 {
895 return NoError();
896 }
897
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400898 if (!linkValidateGlobalNames(context, mInfoLog))
899 {
900 return NoError();
901 }
902
Jamie Madillbd044ed2017-06-05 12:59:21 -0400903 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300904
Jiawei Shao73618602017-12-20 15:47:15 +0800905 ASSERT(mState.mAttachedVertexShader);
906 mState.mNumViews = mState.mAttachedVertexShader->getNumViews(context);
Martin Radev7cf61662017-07-26 17:10:53 +0300907
Jamie Madillbd044ed2017-06-05 12:59:21 -0400908 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300909
Jamie Madill192745a2016-12-22 15:58:21 -0500910 // Map the varyings to the register file
911 // In WebGL, we use a slightly different handling for packing variables.
912 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
913 : PackMode::ANGLE_RELAXED;
Jamie Madillc9727f32017-11-07 12:37:07 -0500914
Jiajia Qin3a9090f2017-09-27 14:37:04 +0800915 ProgramLinkedResources resources = {
916 {data.getCaps().maxVaryingVectors, packMode},
917 {&mState.mUniformBlocks, &mState.mUniforms},
Jiajia Qin94f1e892017-11-20 12:14:32 +0800918 {&mState.mShaderStorageBlocks, &mState.mBufferVariables},
919 {&mState.mAtomicCounterBuffers}};
Jamie Madillc9727f32017-11-07 12:37:07 -0500920
921 InitUniformBlockLinker(context, mState, &resources.uniformBlockLinker);
922 InitShaderStorageBlockLinker(context, mState, &resources.shaderStorageBlockLinker);
923
Jiawei Shao73618602017-12-20 15:47:15 +0800924 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, context->getCaps()))
Jamie Madill192745a2016-12-22 15:58:21 -0500925 {
926 return NoError();
927 }
928
jchen1085c93c42017-11-12 15:36:47 +0800929 if (!resources.varyingPacking.collectAndPackUserVaryings(
930 mInfoLog, mergedVaryings, mState.getTransformFeedbackVaryingNames()))
Olli Etuaho39e78122017-08-29 14:34:22 +0300931 {
932 return NoError();
933 }
934
Jamie Madillc9727f32017-11-07 12:37:07 -0500935 ANGLE_TRY_RESULT(mProgram->link(context, resources, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500936 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300937 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500938 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300939 }
940
941 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500942 }
943
Jamie Madill6db1c2e2017-11-08 09:17:40 -0500944 initInterfaceBlockBindings();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400945
jchen10eaef1e52017-06-13 10:44:11 +0800946 setUniformValuesFromBindingQualifiers();
947
Yunchao Heece12532017-11-21 15:50:21 +0800948 // According to GLES 3.0/3.1 spec for LinkProgram and UseProgram,
949 // Only successfully linked program can replace the executables.
Yunchao He85072e82017-11-14 15:43:28 +0800950 ASSERT(mLinked);
951 updateLinkedShaderStages();
952
Jamie Madill54164b02017-08-28 15:17:37 -0400953 // Mark implementation-specific unreferenced uniforms as ignored.
Jamie Madillfb997ec2017-09-20 15:44:27 -0400954 mProgram->markUnusedUniformLocations(&mState.mUniformLocations, &mState.mSamplerBindings);
Jamie Madill54164b02017-08-28 15:17:37 -0400955
Jamie Madill32447362017-06-28 14:53:52 -0400956 // Save to the program cache.
957 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
958 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
959 {
960 cache->putProgram(programHash, context, this);
961 }
962
Jamie Madill6c58b062017-08-01 13:44:25 -0400963 double delta = platform->currentTime(platform) - startTime;
964 int us = static_cast<int>(delta * 1000000.0);
965 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
966
Martin Radev4c4c8e72016-08-04 12:25:34 +0300967 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000968}
969
Yunchao He85072e82017-11-14 15:43:28 +0800970void Program::updateLinkedShaderStages()
971{
Yunchao Heece12532017-11-21 15:50:21 +0800972 mState.mLinkedShaderStages.reset();
973
Yunchao He85072e82017-11-14 15:43:28 +0800974 if (mState.mAttachedVertexShader)
975 {
976 mState.mLinkedShaderStages.set(SHADER_VERTEX);
977 }
978
979 if (mState.mAttachedFragmentShader)
980 {
981 mState.mLinkedShaderStages.set(SHADER_FRAGMENT);
982 }
983
984 if (mState.mAttachedComputeShader)
985 {
986 mState.mLinkedShaderStages.set(SHADER_COMPUTE);
987 }
988}
989
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000990// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500991void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400993 mState.mAttributes.clear();
994 mState.mActiveAttribLocationsMask.reset();
Jamie Madillbd159f02017-10-09 19:39:06 -0400995 mState.mMaxActiveAttribLocation = 0;
jchen10a9042d32017-03-17 08:50:45 +0800996 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400997 mState.mUniforms.clear();
998 mState.mUniformLocations.clear();
999 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +08001000 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +08001001 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04001002 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +08001003 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -04001004 mState.mOutputVariableTypes.clear();
Brandon Jones76746f92017-11-22 11:44:41 -08001005 mState.mDrawBufferTypeMask.reset();
Corentin Walleze7557742017-06-01 13:09:57 -04001006 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +03001007 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -05001008 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +08001009 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +03001010 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001011
Geoff Lang7dd2e102014-11-10 15:19:26 -05001012 mValidated = false;
1013
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001014 mLinked = false;
1015}
1016
Geoff Lange1a27752015-10-05 13:16:04 -04001017bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +00001018{
1019 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020}
1021
Jamie Madilla2c74982016-12-12 11:20:42 -05001022Error Program::loadBinary(const Context *context,
1023 GLenum binaryFormat,
1024 const void *binary,
1025 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001026{
Jamie Madill6c1f6712017-02-14 19:08:04 -05001027 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001028
Geoff Lang7dd2e102014-11-10 15:19:26 -05001029#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +08001030 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001031#else
Geoff Langc46cc2f2015-10-01 17:16:20 -04001032 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
1033 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001034 {
Jamie Madillf6113162015-05-07 11:49:21 -04001035 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +08001036 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001037 }
1038
Jamie Madill4f86d052017-06-05 12:59:26 -04001039 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
1040 ANGLE_TRY_RESULT(
1041 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -04001042
1043 // Currently we require the full shader text to compute the program hash.
1044 // TODO(jmadill): Store the binary in the internal program cache.
1045
Jamie Madillb0a838b2016-11-13 20:02:12 -05001046 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -05001047#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -05001048}
1049
Jamie Madilla2c74982016-12-12 11:20:42 -05001050Error Program::saveBinary(const Context *context,
1051 GLenum *binaryFormat,
1052 void *binary,
1053 GLsizei bufSize,
1054 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055{
1056 if (binaryFormat)
1057 {
Geoff Langc46cc2f2015-10-01 17:16:20 -04001058 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001059 }
1060
Jamie Madill4f86d052017-06-05 12:59:26 -04001061 angle::MemoryBuffer memoryBuf;
1062 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001063
Jamie Madill4f86d052017-06-05 12:59:26 -04001064 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
1065 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001066
1067 if (streamLength > bufSize)
1068 {
1069 if (length)
1070 {
1071 *length = 0;
1072 }
1073
1074 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1075 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1076 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -05001077 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001078 }
1079
1080 if (binary)
1081 {
1082 char *ptr = reinterpret_cast<char*>(binary);
1083
Jamie Madill48ef11b2016-04-27 15:21:52 -04001084 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 ptr += streamLength;
1086
1087 ASSERT(ptr - streamLength == binary);
1088 }
1089
1090 if (length)
1091 {
1092 *length = streamLength;
1093 }
1094
He Yunchaoacd18982017-01-04 10:46:42 +08001095 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001096}
1097
Jamie Madillffe00c02017-06-27 16:26:55 -04001098GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099{
1100 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -04001101 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001102 if (error.isError())
1103 {
1104 return 0;
1105 }
1106
1107 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001108}
1109
Geoff Langc5629752015-12-07 16:29:04 -05001110void Program::setBinaryRetrievableHint(bool retrievable)
1111{
1112 // TODO(jmadill) : replace with dirty bits
1113 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001114 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001115}
1116
1117bool Program::getBinaryRetrievableHint() const
1118{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001119 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001120}
1121
Yunchao He61afff12017-03-14 15:34:03 +08001122void Program::setSeparable(bool separable)
1123{
1124 // TODO(yunchao) : replace with dirty bits
1125 if (mState.mSeparable != separable)
1126 {
1127 mProgram->setSeparable(separable);
1128 mState.mSeparable = separable;
1129 }
1130}
1131
1132bool Program::isSeparable() const
1133{
1134 return mState.mSeparable;
1135}
1136
Jamie Madill6c1f6712017-02-14 19:08:04 -05001137void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001138{
1139 mRefCount--;
1140
1141 if (mRefCount == 0 && mDeleteStatus)
1142 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001143 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001144 }
1145}
1146
1147void Program::addRef()
1148{
1149 mRefCount++;
1150}
1151
1152unsigned int Program::getRefCount() const
1153{
1154 return mRefCount;
1155}
1156
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001157int Program::getInfoLogLength() const
1158{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001159 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001160}
1161
Geoff Lange1a27752015-10-05 13:16:04 -04001162void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001163{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001164 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001165}
1166
Geoff Lange1a27752015-10-05 13:16:04 -04001167void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001168{
1169 int total = 0;
1170
Martin Radev4c4c8e72016-08-04 12:25:34 +03001171 if (mState.mAttachedComputeShader)
1172 {
1173 if (total < maxCount)
1174 {
1175 shaders[total] = mState.mAttachedComputeShader->getHandle();
1176 total++;
1177 }
1178 }
1179
Jamie Madill48ef11b2016-04-27 15:21:52 -04001180 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001181 {
1182 if (total < maxCount)
1183 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001184 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001185 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001186 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001187 }
1188
Jamie Madill48ef11b2016-04-27 15:21:52 -04001189 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001190 {
1191 if (total < maxCount)
1192 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001193 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001194 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001195 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001196 }
1197
Jiawei Shao89be29a2017-11-06 14:36:45 +08001198 if (mState.mAttachedGeometryShader)
1199 {
1200 if (total < maxCount)
1201 {
1202 shaders[total] = mState.mAttachedGeometryShader->getHandle();
1203 total++;
1204 }
1205 }
1206
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001207 if (count)
1208 {
1209 *count = total;
1210 }
1211}
1212
Geoff Lange1a27752015-10-05 13:16:04 -04001213GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001215 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216}
1217
Jamie Madill63805b42015-08-25 13:17:39 -04001218bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001219{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001220 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1221 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222}
1223
jchen10fd7c3b52017-03-21 15:36:03 +08001224void Program::getActiveAttribute(GLuint index,
1225 GLsizei bufsize,
1226 GLsizei *length,
1227 GLint *size,
1228 GLenum *type,
1229 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001230{
Jamie Madillc349ec02015-08-21 16:53:12 -04001231 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001232 {
1233 if (bufsize > 0)
1234 {
1235 name[0] = '\0';
1236 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001238 if (length)
1239 {
1240 *length = 0;
1241 }
1242
1243 *type = GL_NONE;
1244 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001245 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001246 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001247
jchen1036e120e2017-03-14 14:53:58 +08001248 ASSERT(index < mState.mAttributes.size());
1249 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001250
1251 if (bufsize > 0)
1252 {
jchen10fd7c3b52017-03-21 15:36:03 +08001253 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001254 }
1255
1256 // Always a single 'type' instance
1257 *size = 1;
1258 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001259}
1260
Geoff Lange1a27752015-10-05 13:16:04 -04001261GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001262{
Jamie Madillc349ec02015-08-21 16:53:12 -04001263 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001264 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001265 return 0;
1266 }
1267
jchen1036e120e2017-03-14 14:53:58 +08001268 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001269}
1270
Geoff Lange1a27752015-10-05 13:16:04 -04001271GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001272{
Jamie Madillc349ec02015-08-21 16:53:12 -04001273 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001274 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001275 return 0;
1276 }
1277
1278 size_t maxLength = 0;
1279
Jamie Madill48ef11b2016-04-27 15:21:52 -04001280 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001281 {
jchen1036e120e2017-03-14 14:53:58 +08001282 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001283 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284
Jamie Madillc349ec02015-08-21 16:53:12 -04001285 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001286}
1287
jchen1015015f72017-03-16 13:54:21 +08001288GLuint Program::getInputResourceIndex(const GLchar *name) const
1289{
Olli Etuahod2551232017-10-26 20:03:33 +03001290 return GetResourceIndexFromName(mState.mAttributes, std::string(name));
jchen1015015f72017-03-16 13:54:21 +08001291}
1292
1293GLuint Program::getOutputResourceIndex(const GLchar *name) const
1294{
1295 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1296}
1297
jchen10fd7c3b52017-03-21 15:36:03 +08001298size_t Program::getOutputResourceCount() const
1299{
1300 return (mLinked ? mState.mOutputVariables.size() : 0);
1301}
1302
jchen10baf5d942017-08-28 20:45:48 +08001303template <typename T>
1304void Program::getResourceName(GLuint index,
1305 const std::vector<T> &resources,
1306 GLsizei bufSize,
1307 GLsizei *length,
1308 GLchar *name) const
jchen10fd7c3b52017-03-21 15:36:03 +08001309{
1310 if (length)
1311 {
1312 *length = 0;
1313 }
1314
1315 if (!mLinked)
1316 {
1317 if (bufSize > 0)
1318 {
1319 name[0] = '\0';
1320 }
1321 return;
1322 }
jchen10baf5d942017-08-28 20:45:48 +08001323 ASSERT(index < resources.size());
1324 const auto &resource = resources[index];
jchen10fd7c3b52017-03-21 15:36:03 +08001325
1326 if (bufSize > 0)
1327 {
Olli Etuahod2551232017-10-26 20:03:33 +03001328 CopyStringToBuffer(name, resource.name, bufSize, length);
jchen10fd7c3b52017-03-21 15:36:03 +08001329 }
1330}
1331
jchen10baf5d942017-08-28 20:45:48 +08001332void Program::getInputResourceName(GLuint index,
1333 GLsizei bufSize,
1334 GLsizei *length,
1335 GLchar *name) const
1336{
1337 getResourceName(index, mState.mAttributes, bufSize, length, name);
1338}
1339
1340void Program::getOutputResourceName(GLuint index,
1341 GLsizei bufSize,
1342 GLsizei *length,
1343 GLchar *name) const
1344{
1345 getResourceName(index, mState.mOutputVariables, bufSize, length, name);
1346}
1347
1348void Program::getUniformResourceName(GLuint index,
1349 GLsizei bufSize,
1350 GLsizei *length,
1351 GLchar *name) const
1352{
1353 getResourceName(index, mState.mUniforms, bufSize, length, name);
1354}
1355
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001356void Program::getBufferVariableResourceName(GLuint index,
1357 GLsizei bufSize,
1358 GLsizei *length,
1359 GLchar *name) const
1360{
1361 getResourceName(index, mState.mBufferVariables, bufSize, length, name);
1362}
1363
jchen10880683b2017-04-12 16:21:55 +08001364const sh::Attribute &Program::getInputResource(GLuint index) const
1365{
1366 ASSERT(index < mState.mAttributes.size());
1367 return mState.mAttributes[index];
1368}
1369
1370const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1371{
1372 ASSERT(index < mState.mOutputVariables.size());
1373 return mState.mOutputVariables[index];
1374}
1375
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376GLint Program::getFragDataLocation(const std::string &name) const
1377{
Olli Etuahod2551232017-10-26 20:03:33 +03001378 return GetVariableLocation(mState.mOutputVariables, mState.mOutputLocations, name);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001379}
1380
Geoff Lange1a27752015-10-05 13:16:04 -04001381void Program::getActiveUniform(GLuint index,
1382 GLsizei bufsize,
1383 GLsizei *length,
1384 GLint *size,
1385 GLenum *type,
1386 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001387{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001389 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001390 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001391 ASSERT(index < mState.mUniforms.size());
1392 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001393
1394 if (bufsize > 0)
1395 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001396 std::string string = uniform.name;
jchen10fd7c3b52017-03-21 15:36:03 +08001397 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398 }
1399
Olli Etuaho465835d2017-09-26 13:34:10 +03001400 *size = clampCast<GLint>(uniform.getBasicTypeElementCount());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001402 }
1403 else
1404 {
1405 if (bufsize > 0)
1406 {
1407 name[0] = '\0';
1408 }
1409
1410 if (length)
1411 {
1412 *length = 0;
1413 }
1414
1415 *size = 0;
1416 *type = GL_NONE;
1417 }
1418}
1419
Geoff Lange1a27752015-10-05 13:16:04 -04001420GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001421{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001423 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001424 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001425 }
1426 else
1427 {
1428 return 0;
1429 }
1430}
1431
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001432size_t Program::getActiveBufferVariableCount() const
1433{
1434 return mLinked ? mState.mBufferVariables.size() : 0;
1435}
1436
Geoff Lange1a27752015-10-05 13:16:04 -04001437GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001438{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001439 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440
1441 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001442 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001443 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001445 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001447 size_t length = uniform.name.length() + 1u;
1448 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 {
1450 length += 3; // Counting in "[0]".
1451 }
1452 maxLength = std::max(length, maxLength);
1453 }
1454 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001455 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456
Jamie Madill62d31cb2015-09-11 13:25:51 -04001457 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458}
1459
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460bool Program::isValidUniformLocation(GLint location) const
1461{
Jamie Madille2e406c2016-06-02 13:04:10 -04001462 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001463 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
Jamie Madillfb997ec2017-09-20 15:44:27 -04001464 mState.mUniformLocations[static_cast<size_t>(location)].used());
Geoff Langd8605522016-04-13 10:19:12 -04001465}
1466
Jamie Madill62d31cb2015-09-11 13:25:51 -04001467const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001469 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001470 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471}
1472
Jamie Madillac4e9c32017-01-13 14:07:12 -05001473const VariableLocation &Program::getUniformLocation(GLint location) const
1474{
1475 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1476 return mState.mUniformLocations[location];
1477}
1478
1479const std::vector<VariableLocation> &Program::getUniformLocations() const
1480{
1481 return mState.mUniformLocations;
1482}
1483
1484const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1485{
1486 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1487 return mState.mUniforms[index];
1488}
1489
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001490const BufferVariable &Program::getBufferVariableByIndex(GLuint index) const
1491{
1492 ASSERT(index < static_cast<size_t>(mState.mBufferVariables.size()));
1493 return mState.mBufferVariables[index];
1494}
1495
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497{
Olli Etuahod2551232017-10-26 20:03:33 +03001498 return GetVariableLocation(mState.mUniforms, mState.mUniformLocations, name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499}
1500
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001502{
Jamie Madille7d84322017-01-10 18:21:59 -05001503 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
1506void Program::setUniform1fv(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, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001510 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511}
1512
1513void Program::setUniform2fv(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, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001517 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518}
1519
1520void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1521{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001522 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1523 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001524 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525}
1526
1527void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1528{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001529 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1530 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001531 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001532}
1533
Jamie Madill81c2e252017-09-09 23:32:46 -04001534Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001536 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1537 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1538
Jamie Madill81c2e252017-09-09 23:32:46 -04001539 mProgram->setUniform1iv(location, clampedCount, v);
1540
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001541 if (mState.isSamplerUniformIndex(locationInfo.index))
1542 {
1543 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001544 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001545 }
1546
Jamie Madill81c2e252017-09-09 23:32:46 -04001547 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548}
1549
1550void Program::setUniform2iv(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, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001554 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555}
1556
1557void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1558{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001559 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1560 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001561 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562}
1563
1564void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1565{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001566 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1567 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001568 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001569}
1570
1571void Program::setUniform1uiv(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, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001575 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576}
1577
1578void Program::setUniform2uiv(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, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001582 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
1585void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1586{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001587 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1588 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001589 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590}
1591
1592void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1593{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001594 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1595 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001596 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001597}
1598
1599void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1600{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001601 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001602 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603}
1604
1605void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1606{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001607 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001608 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001609}
1610
1611void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1612{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001613 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001614 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001615}
1616
1617void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1618{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001619 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001620 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621}
1622
1623void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1624{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001625 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001626 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627}
1628
1629void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1630{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001631 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001632 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001633}
1634
1635void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1636{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001637 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001638 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639}
1640
1641void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1642{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001643 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001644 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645}
1646
1647void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1648{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001649 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001650 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651}
1652
Jamie Madill54164b02017-08-28 15:17:37 -04001653void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654{
Jamie Madill54164b02017-08-28 15:17:37 -04001655 const auto &uniformLocation = mState.getUniformLocations()[location];
1656 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1657
1658 GLenum nativeType = gl::VariableComponentType(uniform.type);
1659 if (nativeType == GL_FLOAT)
1660 {
1661 mProgram->getUniformfv(context, location, v);
1662 }
1663 else
1664 {
1665 getUniformInternal(context, v, location, nativeType,
1666 gl::VariableComponentCount(uniform.type));
1667 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001668}
1669
Jamie Madill54164b02017-08-28 15:17:37 -04001670void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671{
Jamie Madill54164b02017-08-28 15:17:37 -04001672 const auto &uniformLocation = mState.getUniformLocations()[location];
1673 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1674
1675 GLenum nativeType = gl::VariableComponentType(uniform.type);
1676 if (nativeType == GL_INT || nativeType == GL_BOOL)
1677 {
1678 mProgram->getUniformiv(context, location, v);
1679 }
1680 else
1681 {
1682 getUniformInternal(context, v, location, nativeType,
1683 gl::VariableComponentCount(uniform.type));
1684 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001685}
1686
Jamie Madill54164b02017-08-28 15:17:37 -04001687void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688{
Jamie Madill54164b02017-08-28 15:17:37 -04001689 const auto &uniformLocation = mState.getUniformLocations()[location];
1690 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1691
1692 GLenum nativeType = gl::VariableComponentType(uniform.type);
1693 if (nativeType == GL_UNSIGNED_INT)
1694 {
1695 mProgram->getUniformuiv(context, location, v);
1696 }
1697 else
1698 {
1699 getUniformInternal(context, v, location, nativeType,
1700 gl::VariableComponentCount(uniform.type));
1701 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702}
1703
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704void Program::flagForDeletion()
1705{
1706 mDeleteStatus = true;
1707}
1708
1709bool Program::isFlaggedForDeletion() const
1710{
1711 return mDeleteStatus;
1712}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001713
Brandon Jones43a53e22014-08-28 16:23:22 -07001714void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001715{
1716 mInfoLog.reset();
1717
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001719 {
Geoff Lang92019432017-11-20 13:09:34 -05001720 mValidated = ConvertToBool(mProgram->validate(caps, &mInfoLog));
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001721 }
1722 else
1723 {
Jamie Madillf6113162015-05-07 11:49:21 -04001724 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001725 }
1726}
1727
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1729{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001730 // Skip cache if we're using an infolog, so we get the full error.
1731 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1732 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1733 {
1734 return mCachedValidateSamplersResult.value();
1735 }
1736
1737 if (mTextureUnitTypesCache.empty())
1738 {
1739 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1740 }
1741 else
1742 {
1743 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1744 }
1745
1746 // if any two active samplers in a program are of different types, but refer to the same
1747 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1748 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001749 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001750 {
Jamie Madill54164b02017-08-28 15:17:37 -04001751 if (samplerBinding.unreferenced)
1752 continue;
1753
Jamie Madille7d84322017-01-10 18:21:59 -05001754 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001755
Jamie Madille7d84322017-01-10 18:21:59 -05001756 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001757 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001758 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1759 {
1760 if (infoLog)
1761 {
1762 (*infoLog) << "Sampler uniform (" << textureUnit
1763 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1764 << caps.maxCombinedTextureImageUnits << ")";
1765 }
1766
1767 mCachedValidateSamplersResult = false;
1768 return false;
1769 }
1770
1771 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1772 {
1773 if (textureType != mTextureUnitTypesCache[textureUnit])
1774 {
1775 if (infoLog)
1776 {
1777 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1778 "image unit ("
1779 << textureUnit << ").";
1780 }
1781
1782 mCachedValidateSamplersResult = false;
1783 return false;
1784 }
1785 }
1786 else
1787 {
1788 mTextureUnitTypesCache[textureUnit] = textureType;
1789 }
1790 }
1791 }
1792
1793 mCachedValidateSamplersResult = true;
1794 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795}
1796
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001797bool Program::isValidated() const
1798{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799 return mValidated;
1800}
1801
Geoff Lange1a27752015-10-05 13:16:04 -04001802GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001804 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805}
1806
jchen1058f67be2017-10-27 08:59:27 +08001807GLuint Program::getActiveAtomicCounterBufferCount() const
1808{
1809 return static_cast<GLuint>(mState.mAtomicCounterBuffers.size());
1810}
1811
Jiajia Qin729b2c62017-08-14 09:36:11 +08001812GLuint Program::getActiveShaderStorageBlockCount() const
1813{
1814 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1815}
1816
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001817void Program::getActiveUniformBlockName(const GLuint blockIndex,
1818 GLsizei bufSize,
1819 GLsizei *length,
1820 GLchar *blockName) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001822 GetInterfaceBlockName(blockIndex, mState.mUniformBlocks, bufSize, length, blockName);
1823}
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001825void Program::getActiveShaderStorageBlockName(const GLuint blockIndex,
1826 GLsizei bufSize,
1827 GLsizei *length,
1828 GLchar *blockName) const
1829{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001831 GetInterfaceBlockName(blockIndex, mState.mShaderStorageBlocks, bufSize, length, blockName);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001832}
1833
Geoff Lange1a27752015-10-05 13:16:04 -04001834GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001835{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001836 int maxLength = 0;
1837
1838 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001839 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001840 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1842 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001843 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 if (!uniformBlock.name.empty())
1845 {
jchen10af713a22017-04-19 09:10:56 +08001846 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1847 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 }
1849 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001850 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851
1852 return maxLength;
1853}
1854
Geoff Lange1a27752015-10-05 13:16:04 -04001855GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856{
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001857 return GetInterfaceBlockIndex(mState.mUniformBlocks, name);
1858}
Jamie Madill62d31cb2015-09-11 13:25:51 -04001859
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001860GLuint Program::getShaderStorageBlockIndex(const std::string &name) const
1861{
1862 return GetInterfaceBlockIndex(mState.mShaderStorageBlocks, name);
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001863}
1864
Jiajia Qin729b2c62017-08-14 09:36:11 +08001865const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001866{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001867 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1868 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001869}
1870
Jiajia Qin3a9090f2017-09-27 14:37:04 +08001871const InterfaceBlock &Program::getShaderStorageBlockByIndex(GLuint index) const
1872{
1873 ASSERT(index < static_cast<GLuint>(mState.mShaderStorageBlocks.size()));
1874 return mState.mShaderStorageBlocks[index];
1875}
1876
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001877void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1878{
jchen107a20b972017-06-13 14:25:26 +08001879 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001880 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001881 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001882}
1883
1884GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1885{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001886 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001887}
1888
Jiajia Qin729b2c62017-08-14 09:36:11 +08001889GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1890{
1891 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1892}
1893
Geoff Lang48dcae72014-02-05 16:28:24 -05001894void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1895{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001896 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001897 for (GLsizei i = 0; i < count; i++)
1898 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001899 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001900 }
1901
Jamie Madill48ef11b2016-04-27 15:21:52 -04001902 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001903}
1904
1905void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1906{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001908 {
jchen10a9042d32017-03-17 08:50:45 +08001909 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1910 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1911 std::string varName = var.nameWithArrayIndex();
1912 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001913 if (length)
1914 {
1915 *length = lastNameIdx;
1916 }
1917 if (size)
1918 {
jchen10a9042d32017-03-17 08:50:45 +08001919 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001920 }
1921 if (type)
1922 {
jchen10a9042d32017-03-17 08:50:45 +08001923 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001924 }
1925 if (name)
1926 {
jchen10a9042d32017-03-17 08:50:45 +08001927 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001928 name[lastNameIdx] = '\0';
1929 }
1930 }
1931}
1932
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001933GLsizei Program::getTransformFeedbackVaryingCount() const
1934{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001935 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001936 {
jchen10a9042d32017-03-17 08:50:45 +08001937 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001938 }
1939 else
1940 {
1941 return 0;
1942 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001943}
1944
1945GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1946{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001948 {
1949 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001950 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001951 {
jchen10a9042d32017-03-17 08:50:45 +08001952 maxSize =
1953 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001954 }
1955
1956 return maxSize;
1957 }
1958 else
1959 {
1960 return 0;
1961 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001962}
1963
1964GLenum Program::getTransformFeedbackBufferMode() const
1965{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001966 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967}
1968
Jiawei Shao73618602017-12-20 15:47:15 +08001969bool Program::linkValidateShaders(const Context *context, InfoLog &infoLog)
1970{
1971 Shader *vertexShader = mState.mAttachedVertexShader;
1972 Shader *fragmentShader = mState.mAttachedFragmentShader;
1973 Shader *computeShader = mState.mAttachedComputeShader;
1974
1975 bool isComputeShaderAttached = (computeShader != nullptr);
1976 bool isGraphicsShaderAttached = (vertexShader != nullptr || fragmentShader != nullptr);
1977 // Check whether we both have a compute and non-compute shaders attached.
1978 // If there are of both types attached, then linking should fail.
1979 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
1980 if (isComputeShaderAttached == true && isGraphicsShaderAttached == true)
1981 {
1982 infoLog << "Both compute and graphics shaders are attached to the same program.";
1983 return false;
1984 }
1985
1986 if (computeShader)
1987 {
1988 if (!computeShader->isCompiled(context))
1989 {
1990 infoLog << "Attached compute shader is not compiled.";
1991 return false;
1992 }
1993 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
1994
1995 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
1996
1997 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
1998 // If the work group size is not specified, a link time error should occur.
1999 if (!mState.mComputeShaderLocalSize.isDeclared())
2000 {
2001 infoLog << "Work group size is not specified.";
2002 return false;
2003 }
2004 }
2005 else
2006 {
2007 if (!fragmentShader || !fragmentShader->isCompiled(context))
2008 {
2009 infoLog << "No compiled fragment shader when at least one graphics shader is attached.";
2010 return false;
2011 }
2012 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
2013
2014 if (!vertexShader || !vertexShader->isCompiled(context))
2015 {
2016 infoLog << "No compiled vertex shader when at least one graphics shader is attached.";
2017 return false;
2018 }
2019 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
2020
2021 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
2022 {
2023 infoLog << "Fragment shader version does not match vertex shader version.";
2024 return false;
2025 }
2026 }
2027
2028 return true;
2029}
2030
jchen10910a3da2017-11-15 09:40:11 +08002031GLuint Program::getTransformFeedbackVaryingResourceIndex(const GLchar *name) const
2032{
2033 for (GLuint tfIndex = 0; tfIndex < mState.mLinkedTransformFeedbackVaryings.size(); ++tfIndex)
2034 {
2035 const auto &tf = mState.mLinkedTransformFeedbackVaryings[tfIndex];
2036 if (tf.nameWithArrayIndex() == name)
2037 {
2038 return tfIndex;
2039 }
2040 }
2041 return GL_INVALID_INDEX;
2042}
2043
2044const TransformFeedbackVarying &Program::getTransformFeedbackVaryingResource(GLuint index) const
2045{
2046 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
2047 return mState.mLinkedTransformFeedbackVaryings[index];
2048}
2049
Jamie Madillbd044ed2017-06-05 12:59:21 -04002050bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002052 Shader *vertexShader = mState.mAttachedVertexShader;
2053 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05002054
Jamie Madillbd044ed2017-06-05 12:59:21 -04002055 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002056
Jiawei Shao3d404882017-10-16 13:30:48 +08002057 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getOutputVaryings(context);
2058 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getInputVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059
Sami Väisänen46eaa942016-06-29 10:26:37 +03002060 std::map<GLuint, std::string> staticFragmentInputLocations;
2061
Jamie Madill4cff2472015-08-21 16:53:18 -04002062 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 bool matched = false;
2065
2066 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04002067 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 {
2069 continue;
2070 }
2071
Jamie Madill4cff2472015-08-21 16:53:18 -04002072 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04002074 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04002076 ASSERT(!input.isBuiltIn());
Jiawei Shao73618602017-12-20 15:47:15 +08002077 if (!LinkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04002078 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 {
2080 return false;
2081 }
2082
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 matched = true;
2084 break;
2085 }
2086 }
2087
2088 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04002089 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04002091 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 return false;
2093 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03002094
2095 // Check for aliased path rendering input bindings (if any).
2096 // If more than one binding refer statically to the same
2097 // location the link must fail.
2098
2099 if (!output.staticUse)
2100 continue;
2101
2102 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
2103 if (inputBinding == -1)
2104 continue;
2105
2106 const auto it = staticFragmentInputLocations.find(inputBinding);
2107 if (it == std::end(staticFragmentInputLocations))
2108 {
2109 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
2110 }
2111 else
2112 {
2113 infoLog << "Binding for fragment input " << output.name << " conflicts with "
2114 << it->second;
2115 return false;
2116 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 }
2118
Jamie Madillbd044ed2017-06-05 12:59:21 -04002119 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05002120 {
2121 return false;
2122 }
2123
Jamie Madillada9ecc2015-08-17 12:53:37 -04002124 // TODO(jmadill): verify no unmatched vertex varyings?
2125
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 return true;
2127}
2128
Jamie Madillbd044ed2017-06-05 12:59:21 -04002129bool Program::linkUniforms(const Context *context,
2130 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002131 const ProgramBindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002132{
Olli Etuahob78707c2017-03-09 15:03:11 +00002133 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002134 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002135 {
2136 return false;
2137 }
2138
Olli Etuahob78707c2017-03-09 15:03:11 +00002139 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002140
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002141 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002142
jchen10eaef1e52017-06-13 10:44:11 +08002143 if (!linkAtomicCounterBuffers())
2144 {
2145 return false;
2146 }
2147
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002148 return true;
2149}
2150
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002151void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002152{
Jamie Madill982f6e02017-06-07 14:33:04 -04002153 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
2154 unsigned int low = high;
2155
jchen10eaef1e52017-06-13 10:44:11 +08002156 for (auto counterIter = mState.mUniforms.rbegin();
2157 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
2158 {
2159 --low;
2160 }
2161
2162 mState.mAtomicCounterUniformRange = RangeUI(low, high);
2163
2164 high = low;
2165
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002166 for (auto imageIter = mState.mUniforms.rbegin();
2167 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
2168 {
2169 --low;
2170 }
2171
2172 mState.mImageUniformRange = RangeUI(low, high);
2173
2174 // If uniform is a image type, insert it into the mImageBindings array.
2175 for (unsigned int imageIndex : mState.mImageUniformRange)
2176 {
Xinghua Cao0328b572017-06-26 15:51:36 +08002177 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
2178 // cannot load values into a uniform defined as an image. if declare without a
2179 // binding qualifier, any uniform image variable (include all elements of
2180 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002181 auto &imageUniform = mState.mUniforms[imageIndex];
2182 if (imageUniform.binding == -1)
2183 {
Olli Etuaho465835d2017-09-26 13:34:10 +03002184 mState.mImageBindings.emplace_back(
2185 ImageBinding(imageUniform.getBasicTypeElementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002186 }
Xinghua Cao0328b572017-06-26 15:51:36 +08002187 else
2188 {
2189 mState.mImageBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002190 ImageBinding(imageUniform.binding, imageUniform.getBasicTypeElementCount()));
Xinghua Cao0328b572017-06-26 15:51:36 +08002191 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002192 }
2193
2194 high = low;
2195
2196 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04002197 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002198 {
Jamie Madill982f6e02017-06-07 14:33:04 -04002199 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002200 }
Jamie Madill982f6e02017-06-07 14:33:04 -04002201
2202 mState.mSamplerUniformRange = RangeUI(low, high);
2203
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002204 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04002205 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002206 {
2207 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2208 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2209 mState.mSamplerBindings.emplace_back(
Olli Etuaho465835d2017-09-26 13:34:10 +03002210 SamplerBinding(textureType, samplerUniform.getBasicTypeElementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002211 }
2212}
2213
jchen10eaef1e52017-06-13 10:44:11 +08002214bool Program::linkAtomicCounterBuffers()
2215{
2216 for (unsigned int index : mState.mAtomicCounterUniformRange)
2217 {
2218 auto &uniform = mState.mUniforms[index];
Jiajia Qin94f1e892017-11-20 12:14:32 +08002219 uniform.blockInfo.offset = uniform.offset;
2220 uniform.blockInfo.arrayStride = (uniform.isArray() ? 4 : 0);
2221 uniform.blockInfo.matrixStride = 0;
2222 uniform.blockInfo.isRowMajorMatrix = false;
2223
jchen10eaef1e52017-06-13 10:44:11 +08002224 bool found = false;
2225 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
2226 ++bufferIndex)
2227 {
2228 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
2229 if (buffer.binding == uniform.binding)
2230 {
2231 buffer.memberIndexes.push_back(index);
2232 uniform.bufferIndex = bufferIndex;
2233 found = true;
jchen1058f67be2017-10-27 08:59:27 +08002234 buffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002235 break;
2236 }
2237 }
2238 if (!found)
2239 {
2240 AtomicCounterBuffer atomicCounterBuffer;
2241 atomicCounterBuffer.binding = uniform.binding;
2242 atomicCounterBuffer.memberIndexes.push_back(index);
jchen1058f67be2017-10-27 08:59:27 +08002243 atomicCounterBuffer.unionReferencesWith(uniform);
jchen10eaef1e52017-06-13 10:44:11 +08002244 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2245 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2246 }
2247 }
2248 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2249 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2250
2251 return true;
2252}
2253
Jiawei Shao73618602017-12-20 15:47:15 +08002254bool Program::LinkValidateInterfaceBlockFields(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002255 const std::string &uniformName,
2256 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002257 const sh::InterfaceBlockField &fragmentUniform,
2258 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002260 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
Jiawei Shao73618602017-12-20 15:47:15 +08002261 if (!LinkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002262 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002263 {
2264 return false;
2265 }
2266
2267 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2268 {
Jamie Madillf6113162015-05-07 11:49:21 -04002269 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002270 return false;
2271 }
2272
2273 return true;
2274}
2275
Jamie Madilleb979bf2016-11-15 12:28:46 -05002276// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002277bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002279 const ContextState &data = context->getContextState();
2280 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002281
Geoff Lang7dd2e102014-11-10 15:19:26 -05002282 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002283 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002284 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002285
2286 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002287 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002288 {
Jamie Madillf6113162015-05-07 11:49:21 -04002289 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002290 return false;
2291 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002292
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002293 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002294
Jamie Madillc349ec02015-08-21 16:53:12 -04002295 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002296 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002297 {
Olli Etuahod2551232017-10-26 20:03:33 +03002298 // GLSL ES 3.10 January 2016 section 4.3.4: Vertex shader inputs can't be arrays or
2299 // structures, so we don't need to worry about adjusting their names or generating entries
2300 // for each member/element (unlike uniforms for example).
2301 ASSERT(!attribute.isArray() && !attribute.isStruct());
2302
Jamie Madilleb979bf2016-11-15 12:28:46 -05002303 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002304 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002305 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002306 attribute.location = bindingLocation;
2307 }
2308
2309 if (attribute.location != -1)
2310 {
2311 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002312 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002313
Jamie Madill63805b42015-08-25 13:17:39 -04002314 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315 {
Jamie Madillf6113162015-05-07 11:49:21 -04002316 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002317 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002318
2319 return false;
2320 }
2321
Jamie Madill63805b42015-08-25 13:17:39 -04002322 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323 {
Jamie Madill63805b42015-08-25 13:17:39 -04002324 const int regLocation = attribute.location + reg;
2325 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002326
2327 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002328 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002329 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002330 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002331 // TODO(jmadill): fix aliasing on ES2
2332 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002333 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002334 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002335 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002336 return false;
2337 }
2338 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002339 else
2340 {
Jamie Madill63805b42015-08-25 13:17:39 -04002341 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002342 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343
Jamie Madill63805b42015-08-25 13:17:39 -04002344 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345 }
2346 }
2347 }
2348
2349 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002350 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002352 // Not set by glBindAttribLocation or by location layout qualifier
2353 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 {
Jamie Madill63805b42015-08-25 13:17:39 -04002355 int regs = VariableRegisterCount(attribute.type);
2356 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002357
Jamie Madill63805b42015-08-25 13:17:39 -04002358 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 {
Jamie Madillf6113162015-05-07 11:49:21 -04002360 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002361 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002362 }
2363
Jamie Madillc349ec02015-08-21 16:53:12 -04002364 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365 }
2366 }
2367
Jamie Madill48ef11b2016-04-27 15:21:52 -04002368 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369 {
Jamie Madill63805b42015-08-25 13:17:39 -04002370 ASSERT(attribute.location != -1);
Jamie Madillbd159f02017-10-09 19:39:06 -04002371 unsigned int regs = static_cast<unsigned int>(VariableRegisterCount(attribute.type));
Jamie Madillc349ec02015-08-21 16:53:12 -04002372
Jamie Madillbd159f02017-10-09 19:39:06 -04002373 for (unsigned int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374 {
Jamie Madillbd159f02017-10-09 19:39:06 -04002375 unsigned int location = static_cast<unsigned int>(attribute.location) + r;
2376 mState.mActiveAttribLocationsMask.set(location);
2377 mState.mMaxActiveAttribLocation =
2378 std::max(mState.mMaxActiveAttribLocation, location + 1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379 }
2380 }
2381
Geoff Lang7dd2e102014-11-10 15:19:26 -05002382 return true;
2383}
2384
Jiawei Shao73618602017-12-20 15:47:15 +08002385bool Program::ValidateGraphicsInterfaceBlocks(
Martin Radev4c4c8e72016-08-04 12:25:34 +03002386 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2387 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002388 InfoLog &infoLog,
Jiawei Shao73618602017-12-20 15:47:15 +08002389 bool webglCompatibility)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002390{
2391 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002392 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2393 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002394
2395 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2396 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002397 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002398 }
2399
Jamie Madille473dee2015-08-18 14:49:01 -04002400 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002401 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002402 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2403 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002404 {
2405 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Jiawei Shao73618602017-12-20 15:47:15 +08002406 if (!AreMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002407 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002408 {
2409 return false;
2410 }
2411 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002412 // TODO(jiajia.qin@intel.com): Add
2413 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002414 }
2415 return true;
2416}
Jamie Madille473dee2015-08-18 14:49:01 -04002417
Jiajia Qin729b2c62017-08-14 09:36:11 +08002418bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002419{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002420 const auto &caps = context->getCaps();
2421
Martin Radev4c4c8e72016-08-04 12:25:34 +03002422 if (mState.mAttachedComputeShader)
2423 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002424 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002425 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002426
Jiajia Qin729b2c62017-08-14 09:36:11 +08002427 if (!validateInterfaceBlocksCount(
2428 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002429 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2430 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002431 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002432 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002433 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002434
2435 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2436 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2437 computeShaderStorageBlocks,
2438 "Compute shader shader storage block count exceeds "
2439 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2440 infoLog))
2441 {
2442 return false;
2443 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002444 return true;
2445 }
2446
Jamie Madillbd044ed2017-06-05 12:59:21 -04002447 Shader &vertexShader = *mState.mAttachedVertexShader;
2448 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002449
Jiajia Qin729b2c62017-08-14 09:36:11 +08002450 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2451 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002452
Jiajia Qin729b2c62017-08-14 09:36:11 +08002453 if (!validateInterfaceBlocksCount(
2454 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002455 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2456 {
2457 return false;
2458 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002459 if (!validateInterfaceBlocksCount(
2460 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002461 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2462 infoLog))
2463 {
2464
2465 return false;
2466 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002467
2468 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiawei Shao73618602017-12-20 15:47:15 +08002469 if (!ValidateGraphicsInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks, infoLog,
2470 webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002471 {
2472 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 }
Jamie Madille473dee2015-08-18 14:49:01 -04002474
Jiajia Qin729b2c62017-08-14 09:36:11 +08002475 if (context->getClientVersion() >= Version(3, 1))
2476 {
2477 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2478 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2479
2480 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2481 vertexShaderStorageBlocks,
2482 "Vertex shader shader storage block count exceeds "
2483 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2484 infoLog))
2485 {
2486 return false;
2487 }
2488 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2489 fragmentShaderStorageBlocks,
2490 "Fragment shader shader storage block count exceeds "
2491 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2492 infoLog))
2493 {
2494
2495 return false;
2496 }
2497
Jiawei Shao73618602017-12-20 15:47:15 +08002498 if (!ValidateGraphicsInterfaceBlocks(vertexShaderStorageBlocks, fragmentShaderStorageBlocks,
2499 infoLog, webglCompatibility))
Jiajia Qin729b2c62017-08-14 09:36:11 +08002500 {
2501 return false;
2502 }
2503 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002504 return true;
2505}
2506
Jiawei Shao73618602017-12-20 15:47:15 +08002507bool Program::AreMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002508 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002509 const sh::InterfaceBlock &fragmentInterfaceBlock,
Jiawei Shao73618602017-12-20 15:47:15 +08002510 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002511{
Jiawei Shao73618602017-12-20 15:47:15 +08002512 const char *blockName = vertexInterfaceBlock.name.c_str();
Geoff Lang7dd2e102014-11-10 15:19:26 -05002513 // validate blocks for the same member types
2514 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2515 {
Jamie Madillf6113162015-05-07 11:49:21 -04002516 infoLog << "Types for interface block '" << blockName
2517 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002518 return false;
2519 }
2520 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2521 {
Jamie Madillf6113162015-05-07 11:49:21 -04002522 infoLog << "Array sizes differ for interface block '" << blockName
2523 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002524 return false;
2525 }
jchen10af713a22017-04-19 09:10:56 +08002526 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
jchen10af713a22017-04-19 09:10:56 +08002527 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002528 {
Jamie Madillf6113162015-05-07 11:49:21 -04002529 infoLog << "Layout qualifiers differ for interface block '" << blockName
2530 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002531 return false;
2532 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002533 const unsigned int numBlockMembers =
2534 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002535 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2536 {
2537 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2538 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2539 if (vertexMember.name != fragmentMember.name)
2540 {
Jamie Madillf6113162015-05-07 11:49:21 -04002541 infoLog << "Name mismatch for field " << blockMemberIndex
2542 << " of interface block '" << blockName
2543 << "': (in vertex: '" << vertexMember.name
2544 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002545 return false;
2546 }
2547 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Jiawei Shao73618602017-12-20 15:47:15 +08002548 if (!LinkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002549 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002550 {
2551 return false;
2552 }
2553 }
2554 return true;
2555}
2556
Jiawei Shao73618602017-12-20 15:47:15 +08002557bool Program::LinkValidateVariablesBase(InfoLog &infoLog,
2558 const std::string &variableName,
2559 const sh::ShaderVariable &vertexVariable,
2560 const sh::ShaderVariable &fragmentVariable,
2561 bool validatePrecision)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002562{
2563 if (vertexVariable.type != fragmentVariable.type)
2564 {
Jamie Madillf6113162015-05-07 11:49:21 -04002565 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002566 return false;
2567 }
Olli Etuaho465835d2017-09-26 13:34:10 +03002568 if (vertexVariable.arraySizes != fragmentVariable.arraySizes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002569 {
Jamie Madillf6113162015-05-07 11:49:21 -04002570 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002571 return false;
2572 }
2573 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2574 {
Jamie Madillf6113162015-05-07 11:49:21 -04002575 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002576 return false;
2577 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002578 if (vertexVariable.structName != fragmentVariable.structName)
2579 {
2580 infoLog << "Structure names for " << variableName
2581 << " differ between vertex and fragment shaders";
2582 return false;
2583 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002584
2585 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2586 {
Jamie Madillf6113162015-05-07 11:49:21 -04002587 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002588 return false;
2589 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002590 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002591 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2592 {
2593 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2594 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2595
2596 if (vertexMember.name != fragmentMember.name)
2597 {
Jamie Madillf6113162015-05-07 11:49:21 -04002598 infoLog << "Name mismatch for field '" << memberIndex
2599 << "' of " << variableName
2600 << ": (in vertex: '" << vertexMember.name
2601 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002602 return false;
2603 }
2604
2605 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2606 vertexMember.name + "'";
2607
Jiawei Shao73618602017-12-20 15:47:15 +08002608 if (!LinkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember,
2609 validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002610 {
2611 return false;
2612 }
2613 }
2614
2615 return true;
2616}
2617
Jiawei Shao73618602017-12-20 15:47:15 +08002618bool Program::LinkValidateVaryings(InfoLog &infoLog,
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002619 const std::string &varyingName,
2620 const sh::Varying &vertexVarying,
2621 const sh::Varying &fragmentVarying,
2622 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002623{
Jiawei Shao73618602017-12-20 15:47:15 +08002624 if (!LinkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002625 {
2626 return false;
2627 }
2628
Jamie Madille9cc4692015-02-19 16:00:13 -05002629 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002630 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002631 infoLog << "Interpolation types for " << varyingName
2632 << " differ between vertex and fragment shaders.";
2633 return false;
2634 }
2635
2636 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2637 {
2638 infoLog << "Invariance for " << varyingName
2639 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002640 return false;
2641 }
2642
2643 return true;
2644}
2645
Jamie Madillbd044ed2017-06-05 12:59:21 -04002646bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002647{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002648 Shader *vertexShader = mState.mAttachedVertexShader;
2649 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jiawei Shao3d404882017-10-16 13:30:48 +08002650 const auto &vertexVaryings = vertexShader->getOutputVaryings(context);
2651 const auto &fragmentVaryings = fragmentShader->getInputVaryings(context);
Jamie Madillbd044ed2017-06-05 12:59:21 -04002652 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002653
2654 if (shaderVersion != 100)
2655 {
2656 // Only ESSL 1.0 has restrictions on matching input and output invariance
2657 return true;
2658 }
2659
2660 bool glPositionIsInvariant = false;
2661 bool glPointSizeIsInvariant = false;
2662 bool glFragCoordIsInvariant = false;
2663 bool glPointCoordIsInvariant = false;
2664
2665 for (const sh::Varying &varying : vertexVaryings)
2666 {
2667 if (!varying.isBuiltIn())
2668 {
2669 continue;
2670 }
2671 if (varying.name.compare("gl_Position") == 0)
2672 {
2673 glPositionIsInvariant = varying.isInvariant;
2674 }
2675 else if (varying.name.compare("gl_PointSize") == 0)
2676 {
2677 glPointSizeIsInvariant = varying.isInvariant;
2678 }
2679 }
2680
2681 for (const sh::Varying &varying : fragmentVaryings)
2682 {
2683 if (!varying.isBuiltIn())
2684 {
2685 continue;
2686 }
2687 if (varying.name.compare("gl_FragCoord") == 0)
2688 {
2689 glFragCoordIsInvariant = varying.isInvariant;
2690 }
2691 else if (varying.name.compare("gl_PointCoord") == 0)
2692 {
2693 glPointCoordIsInvariant = varying.isInvariant;
2694 }
2695 }
2696
2697 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2698 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2699 // Not requiring invariance to match is supported by:
2700 // dEQP, WebGL CTS, Nexus 5X GLES
2701 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2702 {
2703 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2704 "declared invariant.";
2705 return false;
2706 }
2707 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2708 {
2709 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2710 "declared invariant.";
2711 return false;
2712 }
2713
2714 return true;
2715}
2716
jchen10a9042d32017-03-17 08:50:45 +08002717bool Program::linkValidateTransformFeedback(const gl::Context *context,
2718 InfoLog &infoLog,
Jamie Madill3c1da042017-11-27 18:33:40 -05002719 const ProgramMergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002720 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002721{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002722
jchen108225e732017-11-14 16:29:03 +08002723 // Validate the tf names regardless of the actual program varyings.
Jamie Madillccdf74b2015-08-18 10:46:12 -04002724 std::set<std::string> uniqueNames;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002725 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002726 {
jchen10a9042d32017-03-17 08:50:45 +08002727 if (context->getClientVersion() < Version(3, 1) &&
2728 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002729 {
Geoff Lang1a683462015-09-29 15:09:59 -04002730 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002731 return false;
2732 }
jchen108225e732017-11-14 16:29:03 +08002733 if (context->getClientVersion() >= Version(3, 1))
2734 {
2735 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2736 {
2737 infoLog << "Two transform feedback varyings include the same array element ("
2738 << tfVaryingName << ").";
2739 return false;
2740 }
2741 }
2742 else
2743 {
2744 if (uniqueNames.count(tfVaryingName) > 0)
2745 {
2746 infoLog << "Two transform feedback varyings specify the same output variable ("
2747 << tfVaryingName << ").";
2748 return false;
2749 }
2750 }
2751 uniqueNames.insert(tfVaryingName);
2752 }
2753
2754 // Validate against program varyings.
2755 size_t totalComponents = 0;
2756 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
2757 {
2758 std::vector<unsigned int> subscripts;
2759 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
2760
2761 const sh::ShaderVariable *var = FindVaryingOrField(varyings, baseName);
2762 if (var == nullptr)
jchen1085c93c42017-11-12 15:36:47 +08002763 {
2764 infoLog << "Transform feedback varying " << tfVaryingName
2765 << " does not exist in the vertex shader.";
2766 return false;
2767 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002768
jchen108225e732017-11-14 16:29:03 +08002769 // Validate the matching variable.
2770 if (var->isStruct())
2771 {
2772 infoLog << "Struct cannot be captured directly (" << baseName << ").";
2773 return false;
2774 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002775
jchen108225e732017-11-14 16:29:03 +08002776 size_t elementCount = 0;
2777 size_t componentCount = 0;
2778
2779 if (var->isArray())
2780 {
2781 if (context->getClientVersion() < Version(3, 1))
2782 {
2783 infoLog << "Capture of arrays is undefined and not supported.";
2784 return false;
2785 }
2786
2787 // GLSL ES 3.10 section 4.3.6: A vertex output can't be an array of arrays.
2788 ASSERT(!var->isArrayOfArrays());
2789
2790 if (!subscripts.empty() && subscripts[0] >= var->getOutermostArraySize())
2791 {
2792 infoLog << "Cannot capture outbound array element '" << tfVaryingName << "'.";
2793 return false;
2794 }
2795 elementCount = (subscripts.empty() ? var->getOutermostArraySize() : 1);
2796 }
2797 else
2798 {
2799 if (!subscripts.empty())
2800 {
2801 infoLog << "Varying '" << baseName
2802 << "' is not an array to be captured by element.";
2803 return false;
2804 }
2805 elementCount = 1;
2806 }
2807
2808 // TODO(jmadill): Investigate implementation limits on D3D11
2809 componentCount = VariableComponentCount(var->type) * elementCount;
2810 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
2811 componentCount > caps.maxTransformFeedbackSeparateComponents)
2812 {
2813 infoLog << "Transform feedback varying " << tfVaryingName << " components ("
2814 << componentCount << ") exceed the maximum separate components ("
2815 << caps.maxTransformFeedbackSeparateComponents << ").";
2816 return false;
2817 }
2818
2819 totalComponents += componentCount;
2820 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
2821 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
2822 {
2823 infoLog << "Transform feedback varying total components (" << totalComponents
2824 << ") exceed the maximum interleaved components ("
2825 << caps.maxTransformFeedbackInterleavedComponents << ").";
2826 return false;
2827 }
2828 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002829 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002830}
2831
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002832bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2833{
2834 const std::vector<sh::Uniform> &vertexUniforms =
2835 mState.mAttachedVertexShader->getUniforms(context);
2836 const std::vector<sh::Uniform> &fragmentUniforms =
2837 mState.mAttachedFragmentShader->getUniforms(context);
2838 const std::vector<sh::Attribute> &attributes =
2839 mState.mAttachedVertexShader->getActiveAttributes(context);
2840 for (const auto &attrib : attributes)
2841 {
2842 for (const auto &uniform : vertexUniforms)
2843 {
2844 if (uniform.name == attrib.name)
2845 {
2846 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2847 return false;
2848 }
2849 }
2850 for (const auto &uniform : fragmentUniforms)
2851 {
2852 if (uniform.name == attrib.name)
2853 {
2854 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2855 return false;
2856 }
2857 }
2858 }
2859 return true;
2860}
2861
Jamie Madill3c1da042017-11-27 18:33:40 -05002862void Program::gatherTransformFeedbackVaryings(const ProgramMergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002863{
2864 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002865 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002866 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002867 {
Olli Etuahoc8538042017-09-27 11:20:15 +03002868 std::vector<unsigned int> subscripts;
2869 std::string baseName = ParseResourceName(tfVaryingName, &subscripts);
jchen10a9042d32017-03-17 08:50:45 +08002870 size_t subscript = GL_INVALID_INDEX;
Olli Etuahoc8538042017-09-27 11:20:15 +03002871 if (!subscripts.empty())
2872 {
2873 subscript = subscripts.back();
2874 }
Jamie Madill192745a2016-12-22 15:58:21 -05002875 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002876 {
Jamie Madill192745a2016-12-22 15:58:21 -05002877 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002878 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002879 {
jchen10a9042d32017-03-17 08:50:45 +08002880 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2881 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002882 break;
2883 }
jchen108225e732017-11-14 16:29:03 +08002884 else if (varying->isStruct())
2885 {
2886 const auto *field = FindShaderVarField(*varying, tfVaryingName);
2887 if (field != nullptr)
2888 {
2889 mState.mLinkedTransformFeedbackVaryings.emplace_back(*field, *varying);
2890 break;
2891 }
2892 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002893 }
2894 }
2895}
2896
Jamie Madill3c1da042017-11-27 18:33:40 -05002897ProgramMergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002898{
Jamie Madill3c1da042017-11-27 18:33:40 -05002899 ProgramMergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002900
Jiawei Shao3d404882017-10-16 13:30:48 +08002901 for (const sh::Varying &varying : mState.mAttachedVertexShader->getOutputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002902 {
Jamie Madill192745a2016-12-22 15:58:21 -05002903 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002904 }
2905
Jiawei Shao3d404882017-10-16 13:30:48 +08002906 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getInputVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002907 {
Jamie Madill192745a2016-12-22 15:58:21 -05002908 merged[varying.name].fragment = &varying;
2909 }
2910
2911 return merged;
2912}
2913
Jamie Madillbd044ed2017-06-05 12:59:21 -04002914void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002915{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002916 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002917 ASSERT(fragmentShader != nullptr);
2918
Geoff Lange0cff192017-05-30 13:04:56 -04002919 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002920 ASSERT(mState.mActiveOutputVariables.none());
Brandon Jones76746f92017-11-22 11:44:41 -08002921 ASSERT(mState.mDrawBufferTypeMask.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002922
2923 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002924 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002925 {
2926 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2927 outputVariable.name != "gl_FragData")
2928 {
2929 continue;
2930 }
2931
2932 unsigned int baseLocation =
2933 (outputVariable.location == -1 ? 0u
2934 : static_cast<unsigned int>(outputVariable.location));
Olli Etuaho465835d2017-09-26 13:34:10 +03002935
2936 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
2937 // structures, so we may use getBasicTypeElementCount().
2938 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
2939 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Geoff Lange0cff192017-05-30 13:04:56 -04002940 {
2941 const unsigned int location = baseLocation + elementIndex;
2942 if (location >= mState.mOutputVariableTypes.size())
2943 {
2944 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2945 }
Corentin Walleze7557742017-06-01 13:09:57 -04002946 ASSERT(location < mState.mActiveOutputVariables.size());
2947 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002948 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
Brandon Jones76746f92017-11-22 11:44:41 -08002949 mState.mDrawBufferTypeMask.setIndex(mState.mOutputVariableTypes[location], location);
Geoff Lange0cff192017-05-30 13:04:56 -04002950 }
2951 }
2952
Jamie Madill80a6fc02015-08-21 16:53:16 -04002953 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002954 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002955 return;
2956
Jamie Madillbd044ed2017-06-05 12:59:21 -04002957 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002958 // TODO(jmadill): any caps validation here?
2959
jchen1015015f72017-03-16 13:54:21 +08002960 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002961 outputVariableIndex++)
2962 {
jchen1015015f72017-03-16 13:54:21 +08002963 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002964
Olli Etuahod2551232017-10-26 20:03:33 +03002965 if (outputVariable.isArray())
2966 {
2967 // We're following the GLES 3.1 November 2016 spec section 7.3.1.1 Naming Active
2968 // Resources and including [0] at the end of array variable names.
2969 mState.mOutputVariables[outputVariableIndex].name += "[0]";
2970 mState.mOutputVariables[outputVariableIndex].mappedName += "[0]";
2971 }
2972
Jamie Madill80a6fc02015-08-21 16:53:16 -04002973 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2974 if (outputVariable.isBuiltIn())
2975 continue;
2976
2977 // Since multiple output locations must be specified, use 0 for non-specified locations.
Olli Etuahod2551232017-10-26 20:03:33 +03002978 unsigned int baseLocation =
2979 (outputVariable.location == -1 ? 0u
2980 : static_cast<unsigned int>(outputVariable.location));
Jamie Madill80a6fc02015-08-21 16:53:16 -04002981
Olli Etuaho465835d2017-09-26 13:34:10 +03002982 // GLSL ES 3.10 section 4.3.6: Output variables cannot be arrays of arrays or arrays of
2983 // structures, so we may use getBasicTypeElementCount().
2984 unsigned int elementCount = outputVariable.getBasicTypeElementCount();
2985 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002986 {
Olli Etuahod2551232017-10-26 20:03:33 +03002987 const unsigned int location = baseLocation + elementIndex;
2988 if (location >= mState.mOutputLocations.size())
2989 {
2990 mState.mOutputLocations.resize(location + 1);
2991 }
2992 ASSERT(!mState.mOutputLocations.at(location).used());
Olli Etuahoc8538042017-09-27 11:20:15 +03002993 if (outputVariable.isArray())
2994 {
2995 mState.mOutputLocations[location] =
2996 VariableLocation(elementIndex, outputVariableIndex);
2997 }
2998 else
2999 {
3000 VariableLocation locationInfo;
3001 locationInfo.index = outputVariableIndex;
3002 mState.mOutputLocations[location] = locationInfo;
3003 }
Jamie Madill80a6fc02015-08-21 16:53:16 -04003004 }
3005 }
3006}
Jamie Madill62d31cb2015-09-11 13:25:51 -04003007
Olli Etuaho48fed632017-03-16 12:05:30 +00003008void Program::setUniformValuesFromBindingQualifiers()
3009{
Jamie Madill982f6e02017-06-07 14:33:04 -04003010 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00003011 {
3012 const auto &samplerUniform = mState.mUniforms[samplerIndex];
3013 if (samplerUniform.binding != -1)
3014 {
Olli Etuahod2551232017-10-26 20:03:33 +03003015 GLint location = getUniformLocation(samplerUniform.name);
Olli Etuaho48fed632017-03-16 12:05:30 +00003016 ASSERT(location != -1);
3017 std::vector<GLint> boundTextureUnits;
Olli Etuaho465835d2017-09-26 13:34:10 +03003018 for (unsigned int elementIndex = 0;
3019 elementIndex < samplerUniform.getBasicTypeElementCount(); ++elementIndex)
Olli Etuaho48fed632017-03-16 12:05:30 +00003020 {
3021 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
3022 }
3023 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
3024 boundTextureUnits.data());
3025 }
3026 }
3027}
3028
Jamie Madill6db1c2e2017-11-08 09:17:40 -05003029void Program::initInterfaceBlockBindings()
Jamie Madill62d31cb2015-09-11 13:25:51 -04003030{
jchen10af713a22017-04-19 09:10:56 +08003031 // Set initial bindings from shader.
3032 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
3033 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003034 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08003035 bindUniformBlock(blockIndex, uniformBlock.binding);
3036 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003037}
3038
Jamie Madille7d84322017-01-10 18:21:59 -05003039void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003040 GLsizei clampedCount,
3041 const GLint *v)
3042{
Jamie Madill81c2e252017-09-09 23:32:46 -04003043 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3044 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3045 std::vector<GLuint> *boundTextureUnits =
3046 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003047
Olli Etuaho1734e172017-10-27 15:30:27 +03003048 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.arrayIndex);
Jamie Madilld68248b2017-09-11 14:34:14 -04003049
3050 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003051 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003052}
3053
3054template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003055GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3056 GLsizei count,
3057 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003058 const T *v)
3059{
Jamie Madill134f93d2017-08-31 17:11:00 -04003060 if (count == 1)
3061 return 1;
3062
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003063 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003064
Corentin Wallez15ac5342016-11-03 17:06:39 -04003065 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3066 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003067 unsigned int remainingElements =
3068 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003069 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003070 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003071
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003072 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003073 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003074 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003075 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003076
3077 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003078}
3079
3080template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003081GLsizei Program::clampMatrixUniformCount(GLint location,
3082 GLsizei count,
3083 GLboolean transpose,
3084 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003085{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003086 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3087
Jamie Madill62d31cb2015-09-11 13:25:51 -04003088 if (!transpose)
3089 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003090 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003091 }
3092
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003093 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003094
3095 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3096 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Olli Etuaho465835d2017-09-26 13:34:10 +03003097 unsigned int remainingElements =
3098 linkedUniform.getBasicTypeElementCount() - locationInfo.arrayIndex;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003099 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003100}
3101
Jamie Madill54164b02017-08-28 15:17:37 -04003102// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3103// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003104template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003105void Program::getUniformInternal(const Context *context,
3106 DestT *dataOut,
3107 GLint location,
3108 GLenum nativeType,
3109 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003110{
Jamie Madill54164b02017-08-28 15:17:37 -04003111 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003112 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003113 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003114 {
3115 GLint tempValue[16] = {0};
3116 mProgram->getUniformiv(context, location, tempValue);
3117 UniformStateQueryCastLoop<GLboolean>(
3118 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003119 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003120 }
3121 case GL_INT:
3122 {
3123 GLint tempValue[16] = {0};
3124 mProgram->getUniformiv(context, location, tempValue);
3125 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3126 components);
3127 break;
3128 }
3129 case GL_UNSIGNED_INT:
3130 {
3131 GLuint tempValue[16] = {0};
3132 mProgram->getUniformuiv(context, location, tempValue);
3133 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3134 components);
3135 break;
3136 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003137 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003138 {
3139 GLfloat tempValue[16] = {0};
3140 mProgram->getUniformfv(context, location, tempValue);
3141 UniformStateQueryCastLoop<GLfloat>(
3142 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003143 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003144 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003145 default:
3146 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003147 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003148 }
3149}
Jamie Madilla4595b82017-01-11 17:36:34 -05003150
3151bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3152{
3153 // Must be called after samplers are validated.
3154 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3155
3156 for (const auto &binding : mState.mSamplerBindings)
3157 {
3158 GLenum textureType = binding.textureType;
3159 for (const auto &unit : binding.boundTextureUnits)
3160 {
3161 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3162 if (programTextureID == textureID)
3163 {
3164 // TODO(jmadill): Check for appropriate overlap.
3165 return true;
3166 }
3167 }
3168 }
3169
3170 return false;
3171}
3172
Jamie Madilla2c74982016-12-12 11:20:42 -05003173} // namespace gl