blob: 52b8720b794c237cb5b71b86465e97be53ac7087 [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"
17#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050019#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040020#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040022#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000023#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040024#include "libANGLE/VaryingPacking.h"
25#include "libANGLE/features.h"
26#include "libANGLE/queryconversions.h"
27#include "libANGLE/renderer/GLImplFactory.h"
28#include "libANGLE/renderer/ProgramImpl.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000032
Geoff Lang7dd2e102014-11-10 15:19:26 -050033namespace
34{
35
Jamie Madill62d31cb2015-09-11 13:25:51 -040036// This simplified cast function doesn't need to worry about advanced concepts like
37// depth range values, or casting to bool.
38template <typename DestT, typename SrcT>
39DestT UniformStateQueryCast(SrcT value);
40
41// From-Float-To-Integer Casts
42template <>
43GLint UniformStateQueryCast(GLfloat value)
44{
45 return clampCast<GLint>(roundf(value));
46}
47
48template <>
49GLuint UniformStateQueryCast(GLfloat value)
50{
51 return clampCast<GLuint>(roundf(value));
52}
53
54// From-Integer-to-Integer Casts
55template <>
56GLint UniformStateQueryCast(GLuint value)
57{
58 return clampCast<GLint>(value);
59}
60
61template <>
62GLuint UniformStateQueryCast(GLint value)
63{
64 return clampCast<GLuint>(value);
65}
66
67// From-Boolean-to-Anything Casts
68template <>
69GLfloat UniformStateQueryCast(GLboolean value)
70{
71 return (value == GL_TRUE ? 1.0f : 0.0f);
72}
73
74template <>
75GLint UniformStateQueryCast(GLboolean value)
76{
77 return (value == GL_TRUE ? 1 : 0);
78}
79
80template <>
81GLuint UniformStateQueryCast(GLboolean value)
82{
83 return (value == GL_TRUE ? 1u : 0u);
84}
85
86// Default to static_cast
87template <typename DestT, typename SrcT>
88DestT UniformStateQueryCast(SrcT value)
89{
90 return static_cast<DestT>(value);
91}
92
93template <typename SrcT, typename DestT>
94void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
95{
96 for (int comp = 0; comp < components; ++comp)
97 {
98 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
99 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
100 size_t offset = comp * 4;
101 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
102 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
103 }
104}
105
Jamie Madill192745a2016-12-22 15:58:21 -0500106// true if varying x has a higher priority in packing than y
107bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
108{
jchen10a9042d32017-03-17 08:50:45 +0800109 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
110 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
111 sh::ShaderVariable vx, vy;
112 const sh::ShaderVariable *px, *py;
113 if (x.isArrayElement())
114 {
115 vx = *x.varying;
116 vx.arraySize = 0;
117 px = &vx;
118 }
119 else
120 {
121 px = x.varying;
122 }
123
124 if (y.isArrayElement())
125 {
126 vy = *y.varying;
127 vy.arraySize = 0;
128 py = &vy;
129 }
130 else
131 {
132 py = y.varying;
133 }
134
135 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500136}
137
jchen1015015f72017-03-16 13:54:21 +0800138template <typename VarT>
139GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
140{
141 size_t subscript = GL_INVALID_INDEX;
142 std::string baseName = ParseResourceName(name, &subscript);
143
144 // The app is not allowed to specify array indices other than 0 for arrays of basic types
145 if (subscript != 0 && subscript != GL_INVALID_INDEX)
146 {
147 return GL_INVALID_INDEX;
148 }
149
150 for (size_t index = 0; index < list.size(); index++)
151 {
152 const VarT &resource = list[index];
153 if (resource.name == baseName)
154 {
155 if (resource.isArray() || subscript == GL_INVALID_INDEX)
156 {
157 return static_cast<GLuint>(index);
158 }
159 }
160 }
161
162 return GL_INVALID_INDEX;
163}
164
jchen10fd7c3b52017-03-21 15:36:03 +0800165void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
166{
167 ASSERT(bufSize > 0);
168 strncpy(buffer, string.c_str(), bufSize);
169 buffer[bufSize - 1] = '\0';
170
171 if (length)
172 {
173 *length = static_cast<GLsizei>(strlen(buffer));
174 }
175}
176
jchen10a9042d32017-03-17 08:50:45 +0800177bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
178{
179 size_t subscript = GL_INVALID_INDEX;
180 std::string baseName = ParseResourceName(name, &subscript);
181 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
182 {
183 size_t arrayIndex = GL_INVALID_INDEX;
184 std::string arrayName = ParseResourceName(*it, &arrayIndex);
185 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
186 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
187 {
188 return true;
189 }
190 }
191 return false;
192}
193
Jamie Madill62d31cb2015-09-11 13:25:51 -0400194} // anonymous namespace
195
Jamie Madill4a3c2342015-10-08 12:58:45 -0400196const char *const g_fakepath = "C:\\fakepath";
197
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400198InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199{
200}
201
202InfoLog::~InfoLog()
203{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204}
205
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400206size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208 const std::string &logString = mStream.str();
209 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000210}
211
Geoff Lange1a27752015-10-05 13:16:04 -0400212void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000213{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400214 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000215
216 if (bufSize > 0)
217 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400218 const std::string str(mStream.str());
219
220 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000221 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400222 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
223 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000224 }
225
226 infoLog[index] = '\0';
227 }
228
229 if (length)
230 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400231 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000232 }
233}
234
235// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300236// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000237// messages, so lets remove all occurrences of this fake file path from the log.
238void InfoLog::appendSanitized(const char *message)
239{
240 std::string msg(message);
241
242 size_t found;
243 do
244 {
245 found = msg.find(g_fakepath);
246 if (found != std::string::npos)
247 {
248 msg.erase(found, strlen(g_fakepath));
249 }
250 }
251 while (found != std::string::npos);
252
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400253 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000254}
255
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000256void InfoLog::reset()
257{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000258}
259
Geoff Langd8605522016-04-13 10:19:12 -0400260VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000261{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500262}
263
Geoff Langd8605522016-04-13 10:19:12 -0400264VariableLocation::VariableLocation(const std::string &name,
265 unsigned int element,
266 unsigned int index)
267 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500268{
269}
270
Geoff Langd8605522016-04-13 10:19:12 -0400271void Program::Bindings::bindLocation(GLuint index, const std::string &name)
272{
273 mBindings[name] = index;
274}
275
276int Program::Bindings::getBinding(const std::string &name) const
277{
278 auto iter = mBindings.find(name);
279 return (iter != mBindings.end()) ? iter->second : -1;
280}
281
282Program::Bindings::const_iterator Program::Bindings::begin() const
283{
284 return mBindings.begin();
285}
286
287Program::Bindings::const_iterator Program::Bindings::end() const
288{
289 return mBindings.end();
290}
291
Jamie Madill48ef11b2016-04-27 15:21:52 -0400292ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500293 : mLabel(),
294 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400295 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300296 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500297 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500298 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500299 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400300{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300301 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400302}
303
Jamie Madill48ef11b2016-04-27 15:21:52 -0400304ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400305{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500306 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400307}
308
Jamie Madill48ef11b2016-04-27 15:21:52 -0400309const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500310{
311 return mLabel;
312}
313
Jamie Madill48ef11b2016-04-27 15:21:52 -0400314GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400315{
316 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800317 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400318
319 for (size_t location = 0; location < mUniformLocations.size(); ++location)
320 {
321 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400322 if (!uniformLocation.used)
323 {
324 continue;
325 }
326
327 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400328
329 if (uniform.name == baseName)
330 {
Geoff Langd8605522016-04-13 10:19:12 -0400331 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400332 {
Geoff Langd8605522016-04-13 10:19:12 -0400333 if (uniformLocation.element == subscript ||
334 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
335 {
336 return static_cast<GLint>(location);
337 }
338 }
339 else
340 {
341 if (subscript == GL_INVALID_INDEX)
342 {
343 return static_cast<GLint>(location);
344 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345 }
346 }
347 }
348
349 return -1;
350}
351
Jamie Madille7d84322017-01-10 18:21:59 -0500352GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400353{
jchen1015015f72017-03-16 13:54:21 +0800354 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400355}
356
Jamie Madille7d84322017-01-10 18:21:59 -0500357GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
358{
359 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
360 return mUniformLocations[location].index;
361}
362
363Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
364{
365 GLuint index = getUniformIndexFromLocation(location);
366 if (!isSamplerUniformIndex(index))
367 {
368 return Optional<GLuint>::Invalid();
369 }
370
371 return getSamplerIndexFromUniformIndex(index);
372}
373
374bool ProgramState::isSamplerUniformIndex(GLuint index) const
375{
Jamie Madill982f6e02017-06-07 14:33:04 -0400376 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500377}
378
379GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
380{
381 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400382 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500383}
384
Jamie Madill34ca4f52017-06-13 11:49:39 -0400385GLuint ProgramState::getAttributeLocation(const std::string &name) const
386{
387 for (const sh::Attribute &attribute : mAttributes)
388 {
389 if (attribute.name == name)
390 {
391 return attribute.location;
392 }
393 }
394
395 return static_cast<GLuint>(-1);
396}
397
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500398Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400399 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400400 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500401 mLinked(false),
402 mDeleteStatus(false),
403 mRefCount(0),
404 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500405 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500406{
407 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000408
Geoff Lang7dd2e102014-11-10 15:19:26 -0500409 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410}
411
412Program::~Program()
413{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400414 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
Jamie Madill4928b7c2017-06-20 12:57:39 -0400417void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500418{
419 if (mState.mAttachedVertexShader != nullptr)
420 {
421 mState.mAttachedVertexShader->release(context);
422 mState.mAttachedVertexShader = nullptr;
423 }
424
425 if (mState.mAttachedFragmentShader != nullptr)
426 {
427 mState.mAttachedFragmentShader->release(context);
428 mState.mAttachedFragmentShader = nullptr;
429 }
430
431 if (mState.mAttachedComputeShader != nullptr)
432 {
433 mState.mAttachedComputeShader->release(context);
434 mState.mAttachedComputeShader = nullptr;
435 }
436
Jamie Madillc564c072017-06-01 12:45:42 -0400437 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400438
439 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
440 !mState.mAttachedComputeShader);
441 SafeDelete(mProgram);
442
443 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500444}
445
Geoff Lang70d0f492015-12-10 17:45:46 -0500446void Program::setLabel(const std::string &label)
447{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400448 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500449}
450
451const std::string &Program::getLabel() const
452{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400453 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500454}
455
Jamie Madillef300b12016-10-07 15:12:09 -0400456void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300458 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300460 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461 {
Jamie Madillef300b12016-10-07 15:12:09 -0400462 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300463 mState.mAttachedVertexShader = shader;
464 mState.mAttachedVertexShader->addRef();
465 break;
466 }
467 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468 {
Jamie Madillef300b12016-10-07 15:12:09 -0400469 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300470 mState.mAttachedFragmentShader = shader;
471 mState.mAttachedFragmentShader->addRef();
472 break;
473 }
474 case GL_COMPUTE_SHADER:
475 {
Jamie Madillef300b12016-10-07 15:12:09 -0400476 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300477 mState.mAttachedComputeShader = shader;
478 mState.mAttachedComputeShader->addRef();
479 break;
480 }
481 default:
482 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484}
485
Jamie Madillc1d770e2017-04-13 17:31:24 -0400486void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300488 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300490 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400492 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500493 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300494 mState.mAttachedVertexShader = nullptr;
495 break;
496 }
497 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400499 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500500 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 mState.mAttachedFragmentShader = nullptr;
502 break;
503 }
504 case GL_COMPUTE_SHADER:
505 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400506 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500507 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300508 mState.mAttachedComputeShader = nullptr;
509 break;
510 }
511 default:
512 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514}
515
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000516int Program::getAttachedShadersCount() const
517{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300518 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
519 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000520}
521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522void Program::bindAttributeLocation(GLuint index, const char *name)
523{
Geoff Langd8605522016-04-13 10:19:12 -0400524 mAttributeBindings.bindLocation(index, name);
525}
526
527void Program::bindUniformLocation(GLuint index, const char *name)
528{
529 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800530 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531}
532
Sami Väisänen46eaa942016-06-29 10:26:37 +0300533void Program::bindFragmentInputLocation(GLint index, const char *name)
534{
535 mFragmentInputBindings.bindLocation(index, name);
536}
537
Jamie Madillbd044ed2017-06-05 12:59:21 -0400538BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300539{
540 BindingInfo ret;
541 ret.type = GL_NONE;
542 ret.valid = false;
543
Jamie Madillbd044ed2017-06-05 12:59:21 -0400544 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300545 ASSERT(fragmentShader);
546
547 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400548 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300549
550 for (const auto &binding : mFragmentInputBindings)
551 {
552 if (binding.second != static_cast<GLuint>(index))
553 continue;
554
555 ret.valid = true;
556
557 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400558 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300559
560 for (const auto &in : inputs)
561 {
562 if (in.name == originalName)
563 {
564 if (in.isArray())
565 {
566 // The client wants to bind either "name" or "name[0]".
567 // GL ES 3.1 spec refers to active array names with language such as:
568 // "if the string identifies the base name of an active array, where the
569 // string would exactly match the name of the variable if the suffix "[0]"
570 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400571 if (arrayIndex == GL_INVALID_INDEX)
572 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300573
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400574 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300575 }
576 else
577 {
578 ret.name = in.mappedName;
579 }
580 ret.type = in.type;
581 return ret;
582 }
583 }
584 }
585
586 return ret;
587}
588
Jamie Madillbd044ed2017-06-05 12:59:21 -0400589void Program::pathFragmentInputGen(const Context *context,
590 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300591 GLenum genMode,
592 GLint components,
593 const GLfloat *coeffs)
594{
595 // If the location is -1 then the command is silently ignored
596 if (index == -1)
597 return;
598
Jamie Madillbd044ed2017-06-05 12:59:21 -0400599 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300600
601 // If the input doesn't exist then then the command is silently ignored
602 // This could happen through optimization for example, the shader translator
603 // decides that a variable is not actually being used and optimizes it away.
604 if (binding.name.empty())
605 return;
606
607 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
608}
609
Martin Radev4c4c8e72016-08-04 12:25:34 +0300610// The attached shaders are checked for linking errors by matching up their variables.
611// Uniform, input and output variables get collected.
612// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500613Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000614{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500615 const auto &data = context->getContextState();
616
Jamie Madill6c1f6712017-02-14 19:08:04 -0500617 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000618
Jamie Madill32447362017-06-28 14:53:52 -0400619 ProgramHash programHash;
620 auto *cache = context->getMemoryProgramCache();
621 if (cache)
622 {
623 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
624 }
625
626 if (mLinked)
627 {
628 return NoError();
629 }
630
631 // Cache load failed, fall through to normal linking.
632 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000633 mInfoLog.reset();
634
Martin Radev4c4c8e72016-08-04 12:25:34 +0300635 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500636
Jamie Madill192745a2016-12-22 15:58:21 -0500637 auto vertexShader = mState.mAttachedVertexShader;
638 auto fragmentShader = mState.mAttachedFragmentShader;
639 auto computeShader = mState.mAttachedComputeShader;
640
641 bool isComputeShaderAttached = (computeShader != nullptr);
642 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300643 // Check whether we both have a compute and non-compute shaders attached.
644 // If there are of both types attached, then linking should fail.
645 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
646 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500647 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
649 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400650 }
651
Jamie Madill192745a2016-12-22 15:58:21 -0500652 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500653 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400654 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300655 {
656 mInfoLog << "Attached compute shader is not compiled.";
657 return NoError();
658 }
Jamie Madill192745a2016-12-22 15:58:21 -0500659 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660
Jamie Madillbd044ed2017-06-05 12:59:21 -0400661 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662
663 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
664 // If the work group size is not specified, a link time error should occur.
665 if (!mState.mComputeShaderLocalSize.isDeclared())
666 {
667 mInfoLog << "Work group size is not specified.";
668 return NoError();
669 }
670
Jamie Madillbd044ed2017-06-05 12:59:21 -0400671 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300672 {
673 return NoError();
674 }
675
Jamie Madillbd044ed2017-06-05 12:59:21 -0400676 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300677 {
678 return NoError();
679 }
680
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500681 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400682 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500683 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300684 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500685 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300686 }
687 }
688 else
689 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400690 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300691 {
692 return NoError();
693 }
Jamie Madill192745a2016-12-22 15:58:21 -0500694 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300695
Jamie Madillbd044ed2017-06-05 12:59:21 -0400696 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300697 {
698 return NoError();
699 }
Jamie Madill192745a2016-12-22 15:58:21 -0500700 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701
Jamie Madillbd044ed2017-06-05 12:59:21 -0400702 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 {
704 mInfoLog << "Fragment shader version does not match vertex shader version.";
705 return NoError();
706 }
707
Jamie Madillbd044ed2017-06-05 12:59:21 -0400708 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300709 {
710 return NoError();
711 }
712
Jamie Madillbd044ed2017-06-05 12:59:21 -0400713 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300714 {
715 return NoError();
716 }
717
Jamie Madillbd044ed2017-06-05 12:59:21 -0400718 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300719 {
720 return NoError();
721 }
722
Jamie Madillbd044ed2017-06-05 12:59:21 -0400723 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 {
725 return NoError();
726 }
727
Jamie Madillbd044ed2017-06-05 12:59:21 -0400728 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300729
jchen10a9042d32017-03-17 08:50:45 +0800730 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300731 {
732 return NoError();
733 }
734
Jamie Madillbd044ed2017-06-05 12:59:21 -0400735 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300736
Jamie Madill192745a2016-12-22 15:58:21 -0500737 // Validate we can pack the varyings.
738 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
739
740 // Map the varyings to the register file
741 // In WebGL, we use a slightly different handling for packing variables.
742 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
743 : PackMode::ANGLE_RELAXED;
744 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
745 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
746 mState.getTransformFeedbackVaryingNames()))
747 {
748 return NoError();
749 }
750
Jamie Madillc564c072017-06-01 12:45:42 -0400751 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500752 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500754 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300755 }
756
757 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500758 }
759
Olli Etuaho48fed632017-03-16 12:05:30 +0000760 setUniformValuesFromBindingQualifiers();
761
Jamie Madillbd044ed2017-06-05 12:59:21 -0400762 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400763
Jamie Madill32447362017-06-28 14:53:52 -0400764 // Save to the program cache.
765 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
766 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
767 {
768 cache->putProgram(programHash, context, this);
769 }
770
Martin Radev4c4c8e72016-08-04 12:25:34 +0300771 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000772}
773
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000774// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500775void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400777 mState.mAttributes.clear();
778 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800779 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 mState.mUniforms.clear();
781 mState.mUniformLocations.clear();
782 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800783 mState.mActiveUniformBlockBindings.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400784 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800785 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400786 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400787 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300788 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500789 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500790
Geoff Lang7dd2e102014-11-10 15:19:26 -0500791 mValidated = false;
792
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000793 mLinked = false;
794}
795
Geoff Lange1a27752015-10-05 13:16:04 -0400796bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000797{
798 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799}
800
Jamie Madilla2c74982016-12-12 11:20:42 -0500801Error Program::loadBinary(const Context *context,
802 GLenum binaryFormat,
803 const void *binary,
804 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000805{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500806 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000807
Geoff Lang7dd2e102014-11-10 15:19:26 -0500808#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800809 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500810#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400811 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
812 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000813 {
Jamie Madillf6113162015-05-07 11:49:21 -0400814 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800815 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500816 }
817
Jamie Madill4f86d052017-06-05 12:59:26 -0400818 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
819 ANGLE_TRY_RESULT(
820 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400821
822 // Currently we require the full shader text to compute the program hash.
823 // TODO(jmadill): Store the binary in the internal program cache.
824
Jamie Madillb0a838b2016-11-13 20:02:12 -0500825 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500826#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500827}
828
Jamie Madilla2c74982016-12-12 11:20:42 -0500829Error Program::saveBinary(const Context *context,
830 GLenum *binaryFormat,
831 void *binary,
832 GLsizei bufSize,
833 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500834{
835 if (binaryFormat)
836 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400837 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500838 }
839
Jamie Madill4f86d052017-06-05 12:59:26 -0400840 angle::MemoryBuffer memoryBuf;
841 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500842
Jamie Madill4f86d052017-06-05 12:59:26 -0400843 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
844 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500845
846 if (streamLength > bufSize)
847 {
848 if (length)
849 {
850 *length = 0;
851 }
852
853 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
854 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
855 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500856 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500857 }
858
859 if (binary)
860 {
861 char *ptr = reinterpret_cast<char*>(binary);
862
Jamie Madill48ef11b2016-04-27 15:21:52 -0400863 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500864 ptr += streamLength;
865
866 ASSERT(ptr - streamLength == binary);
867 }
868
869 if (length)
870 {
871 *length = streamLength;
872 }
873
He Yunchaoacd18982017-01-04 10:46:42 +0800874 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500875}
876
Jamie Madillffe00c02017-06-27 16:26:55 -0400877GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878{
879 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400880 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881 if (error.isError())
882 {
883 return 0;
884 }
885
886 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000887}
888
Geoff Langc5629752015-12-07 16:29:04 -0500889void Program::setBinaryRetrievableHint(bool retrievable)
890{
891 // TODO(jmadill) : replace with dirty bits
892 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400893 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500894}
895
896bool Program::getBinaryRetrievableHint() const
897{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400898 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500899}
900
Yunchao He61afff12017-03-14 15:34:03 +0800901void Program::setSeparable(bool separable)
902{
903 // TODO(yunchao) : replace with dirty bits
904 if (mState.mSeparable != separable)
905 {
906 mProgram->setSeparable(separable);
907 mState.mSeparable = separable;
908 }
909}
910
911bool Program::isSeparable() const
912{
913 return mState.mSeparable;
914}
915
Jamie Madill6c1f6712017-02-14 19:08:04 -0500916void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917{
918 mRefCount--;
919
920 if (mRefCount == 0 && mDeleteStatus)
921 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500922 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923 }
924}
925
926void Program::addRef()
927{
928 mRefCount++;
929}
930
931unsigned int Program::getRefCount() const
932{
933 return mRefCount;
934}
935
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000936int Program::getInfoLogLength() const
937{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400938 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000939}
940
Geoff Lange1a27752015-10-05 13:16:04 -0400941void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000942{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000943 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000944}
945
Geoff Lange1a27752015-10-05 13:16:04 -0400946void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000947{
948 int total = 0;
949
Martin Radev4c4c8e72016-08-04 12:25:34 +0300950 if (mState.mAttachedComputeShader)
951 {
952 if (total < maxCount)
953 {
954 shaders[total] = mState.mAttachedComputeShader->getHandle();
955 total++;
956 }
957 }
958
Jamie Madill48ef11b2016-04-27 15:21:52 -0400959 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000960 {
961 if (total < maxCount)
962 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400963 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200964 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000965 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000966 }
967
Jamie Madill48ef11b2016-04-27 15:21:52 -0400968 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000969 {
970 if (total < maxCount)
971 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400972 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200973 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000974 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000975 }
976
977 if (count)
978 {
979 *count = total;
980 }
981}
982
Geoff Lange1a27752015-10-05 13:16:04 -0400983GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500984{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400985 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500986}
987
Jamie Madill63805b42015-08-25 13:17:39 -0400988bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400989{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400990 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
991 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500992}
993
jchen10fd7c3b52017-03-21 15:36:03 +0800994void Program::getActiveAttribute(GLuint index,
995 GLsizei bufsize,
996 GLsizei *length,
997 GLint *size,
998 GLenum *type,
999 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001000{
Jamie Madillc349ec02015-08-21 16:53:12 -04001001 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001002 {
1003 if (bufsize > 0)
1004 {
1005 name[0] = '\0';
1006 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001008 if (length)
1009 {
1010 *length = 0;
1011 }
1012
1013 *type = GL_NONE;
1014 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001015 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001016 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001017
jchen1036e120e2017-03-14 14:53:58 +08001018 ASSERT(index < mState.mAttributes.size());
1019 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001020
1021 if (bufsize > 0)
1022 {
jchen10fd7c3b52017-03-21 15:36:03 +08001023 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001024 }
1025
1026 // Always a single 'type' instance
1027 *size = 1;
1028 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001029}
1030
Geoff Lange1a27752015-10-05 13:16:04 -04001031GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001032{
Jamie Madillc349ec02015-08-21 16:53:12 -04001033 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001034 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001035 return 0;
1036 }
1037
jchen1036e120e2017-03-14 14:53:58 +08001038 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001039}
1040
Geoff Lange1a27752015-10-05 13:16:04 -04001041GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001042{
Jamie Madillc349ec02015-08-21 16:53:12 -04001043 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001044 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001045 return 0;
1046 }
1047
1048 size_t maxLength = 0;
1049
Jamie Madill48ef11b2016-04-27 15:21:52 -04001050 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001051 {
jchen1036e120e2017-03-14 14:53:58 +08001052 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001053 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001054
Jamie Madillc349ec02015-08-21 16:53:12 -04001055 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001056}
1057
jchen1015015f72017-03-16 13:54:21 +08001058GLuint Program::getInputResourceIndex(const GLchar *name) const
1059{
1060 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1061 {
1062 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1063 if (attribute.name == name)
1064 {
1065 return attributeIndex;
1066 }
1067 }
1068 return GL_INVALID_INDEX;
1069}
1070
1071GLuint Program::getOutputResourceIndex(const GLchar *name) const
1072{
1073 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1074}
1075
jchen10fd7c3b52017-03-21 15:36:03 +08001076size_t Program::getOutputResourceCount() const
1077{
1078 return (mLinked ? mState.mOutputVariables.size() : 0);
1079}
1080
1081void Program::getInputResourceName(GLuint index,
1082 GLsizei bufSize,
1083 GLsizei *length,
1084 GLchar *name) const
1085{
1086 GLint size;
1087 GLenum type;
1088 getActiveAttribute(index, bufSize, length, &size, &type, name);
1089}
1090
1091void Program::getOutputResourceName(GLuint index,
1092 GLsizei bufSize,
1093 GLsizei *length,
1094 GLchar *name) const
1095{
1096 if (length)
1097 {
1098 *length = 0;
1099 }
1100
1101 if (!mLinked)
1102 {
1103 if (bufSize > 0)
1104 {
1105 name[0] = '\0';
1106 }
1107 return;
1108 }
1109 ASSERT(index < mState.mOutputVariables.size());
1110 const auto &output = mState.mOutputVariables[index];
1111
1112 if (bufSize > 0)
1113 {
1114 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1115
1116 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1117 }
1118}
1119
Geoff Lang7dd2e102014-11-10 15:19:26 -05001120GLint Program::getFragDataLocation(const std::string &name) const
1121{
1122 std::string baseName(name);
1123 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001124 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001125 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001126 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001127 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1128 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001129 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001131 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001132 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001133}
1134
Geoff Lange1a27752015-10-05 13:16:04 -04001135void Program::getActiveUniform(GLuint index,
1136 GLsizei bufsize,
1137 GLsizei *length,
1138 GLint *size,
1139 GLenum *type,
1140 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001141{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001143 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001144 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001145 ASSERT(index < mState.mUniforms.size());
1146 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147
1148 if (bufsize > 0)
1149 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001150 std::string string = uniform.name;
1151 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152 {
1153 string += "[0]";
1154 }
jchen10fd7c3b52017-03-21 15:36:03 +08001155 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001156 }
1157
Jamie Madill62d31cb2015-09-11 13:25:51 -04001158 *size = uniform.elementCount();
1159 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001160 }
1161 else
1162 {
1163 if (bufsize > 0)
1164 {
1165 name[0] = '\0';
1166 }
1167
1168 if (length)
1169 {
1170 *length = 0;
1171 }
1172
1173 *size = 0;
1174 *type = GL_NONE;
1175 }
1176}
1177
Geoff Lange1a27752015-10-05 13:16:04 -04001178GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001179{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001181 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001182 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001183 }
1184 else
1185 {
1186 return 0;
1187 }
1188}
1189
Geoff Lange1a27752015-10-05 13:16:04 -04001190GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001191{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001192 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193
1194 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001195 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001196 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001197 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001198 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001200 size_t length = uniform.name.length() + 1u;
1201 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 {
1203 length += 3; // Counting in "[0]".
1204 }
1205 maxLength = std::max(length, maxLength);
1206 }
1207 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001208 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001209
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211}
1212
1213GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1214{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001215 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001216 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001218 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001219 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1220 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1221 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1222 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1223 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1224 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1225 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1226 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1227 default:
1228 UNREACHABLE();
1229 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001230 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001231 return 0;
1232}
1233
1234bool Program::isValidUniformLocation(GLint location) const
1235{
Jamie Madille2e406c2016-06-02 13:04:10 -04001236 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001237 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1238 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001239}
1240
Jamie Madill62d31cb2015-09-11 13:25:51 -04001241const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001243 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001244 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245}
1246
Jamie Madillac4e9c32017-01-13 14:07:12 -05001247const VariableLocation &Program::getUniformLocation(GLint location) const
1248{
1249 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1250 return mState.mUniformLocations[location];
1251}
1252
1253const std::vector<VariableLocation> &Program::getUniformLocations() const
1254{
1255 return mState.mUniformLocations;
1256}
1257
1258const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1259{
1260 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1261 return mState.mUniforms[index];
1262}
1263
Jamie Madill62d31cb2015-09-11 13:25:51 -04001264GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001266 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267}
1268
Jamie Madill62d31cb2015-09-11 13:25:51 -04001269GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270{
Jamie Madille7d84322017-01-10 18:21:59 -05001271 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272}
1273
1274void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1275{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001276 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1277 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278}
1279
1280void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1281{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001282 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1283 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284}
1285
1286void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1287{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001288 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1289 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290}
1291
1292void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1293{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001294 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1295 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001296}
1297
1298void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1299{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001300 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1301 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302}
1303
1304void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1305{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001306 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1307 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308}
1309
1310void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1311{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001312 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1313 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001314}
1315
1316void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1317{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001318 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1319 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001320}
1321
1322void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1323{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001324 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1325 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326}
1327
1328void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1329{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001330 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1331 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
1334void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1335{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001336 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1337 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338}
1339
1340void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1341{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001342 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1343 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344}
1345
1346void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1347{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001348 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1349 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350}
1351
1352void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1353{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001354 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1355 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356}
1357
1358void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1359{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001360 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1361 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362}
1363
1364void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1365{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001366 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1367 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368}
1369
1370void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1371{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001372 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1373 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
1376void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1377{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001378 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1379 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380}
1381
1382void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1383{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001384 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1385 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386}
1387
1388void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1389{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001390 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1391 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392}
1393
1394void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1395{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001396 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1397 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398}
1399
Geoff Lange1a27752015-10-05 13:16:04 -04001400void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001402 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
Geoff Lange1a27752015-10-05 13:16:04 -04001405void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001407 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408}
1409
Geoff Lange1a27752015-10-05 13:16:04 -04001410void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001412 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413}
1414
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415void Program::flagForDeletion()
1416{
1417 mDeleteStatus = true;
1418}
1419
1420bool Program::isFlaggedForDeletion() const
1421{
1422 return mDeleteStatus;
1423}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001424
Brandon Jones43a53e22014-08-28 16:23:22 -07001425void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001426{
1427 mInfoLog.reset();
1428
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001430 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001431 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001432 }
1433 else
1434 {
Jamie Madillf6113162015-05-07 11:49:21 -04001435 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001436 }
1437}
1438
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1440{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001441 // Skip cache if we're using an infolog, so we get the full error.
1442 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1443 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1444 {
1445 return mCachedValidateSamplersResult.value();
1446 }
1447
1448 if (mTextureUnitTypesCache.empty())
1449 {
1450 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1451 }
1452 else
1453 {
1454 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1455 }
1456
1457 // if any two active samplers in a program are of different types, but refer to the same
1458 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1459 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001460 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001461 {
Jamie Madille7d84322017-01-10 18:21:59 -05001462 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001463
Jamie Madille7d84322017-01-10 18:21:59 -05001464 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001465 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001466 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1467 {
1468 if (infoLog)
1469 {
1470 (*infoLog) << "Sampler uniform (" << textureUnit
1471 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1472 << caps.maxCombinedTextureImageUnits << ")";
1473 }
1474
1475 mCachedValidateSamplersResult = false;
1476 return false;
1477 }
1478
1479 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1480 {
1481 if (textureType != mTextureUnitTypesCache[textureUnit])
1482 {
1483 if (infoLog)
1484 {
1485 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1486 "image unit ("
1487 << textureUnit << ").";
1488 }
1489
1490 mCachedValidateSamplersResult = false;
1491 return false;
1492 }
1493 }
1494 else
1495 {
1496 mTextureUnitTypesCache[textureUnit] = textureType;
1497 }
1498 }
1499 }
1500
1501 mCachedValidateSamplersResult = true;
1502 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001503}
1504
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001505bool Program::isValidated() const
1506{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001507 return mValidated;
1508}
1509
Geoff Lange1a27752015-10-05 13:16:04 -04001510GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001512 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513}
1514
1515void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1516{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001517 ASSERT(
1518 uniformBlockIndex <
1519 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520
Jamie Madill48ef11b2016-04-27 15:21:52 -04001521 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522
1523 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001524 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525 std::string string = uniformBlock.name;
1526
Jamie Madill62d31cb2015-09-11 13:25:51 -04001527 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001529 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530 }
jchen10fd7c3b52017-03-21 15:36:03 +08001531 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001532 }
1533}
1534
Geoff Lange1a27752015-10-05 13:16:04 -04001535GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001536{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001537 int maxLength = 0;
1538
1539 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001540 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001541 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001542 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1543 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001544 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001545 if (!uniformBlock.name.empty())
1546 {
jchen10af713a22017-04-19 09:10:56 +08001547 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1548 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001549 }
1550 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001551 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552
1553 return maxLength;
1554}
1555
Geoff Lange1a27752015-10-05 13:16:04 -04001556GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001558 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001559 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001560
Jamie Madill48ef11b2016-04-27 15:21:52 -04001561 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1563 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001564 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001565 if (uniformBlock.name == baseName)
1566 {
1567 const bool arrayElementZero =
1568 (subscript == GL_INVALID_INDEX &&
1569 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1570 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1571 {
1572 return blockIndex;
1573 }
1574 }
1575 }
1576
1577 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001578}
1579
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001581{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001582 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1583 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001584}
1585
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001586void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1587{
jchen107a20b972017-06-13 14:25:26 +08001588 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001589 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001590 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001591}
1592
1593GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1594{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001595 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001596}
1597
Geoff Lang48dcae72014-02-05 16:28:24 -05001598void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1599{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001600 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001601 for (GLsizei i = 0; i < count; i++)
1602 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001603 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001604 }
1605
Jamie Madill48ef11b2016-04-27 15:21:52 -04001606 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001607}
1608
1609void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1610{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001612 {
jchen10a9042d32017-03-17 08:50:45 +08001613 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1614 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1615 std::string varName = var.nameWithArrayIndex();
1616 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001617 if (length)
1618 {
1619 *length = lastNameIdx;
1620 }
1621 if (size)
1622 {
jchen10a9042d32017-03-17 08:50:45 +08001623 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001624 }
1625 if (type)
1626 {
jchen10a9042d32017-03-17 08:50:45 +08001627 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001628 }
1629 if (name)
1630 {
jchen10a9042d32017-03-17 08:50:45 +08001631 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001632 name[lastNameIdx] = '\0';
1633 }
1634 }
1635}
1636
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001637GLsizei Program::getTransformFeedbackVaryingCount() const
1638{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001640 {
jchen10a9042d32017-03-17 08:50:45 +08001641 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001642 }
1643 else
1644 {
1645 return 0;
1646 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001647}
1648
1649GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1650{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001652 {
1653 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001654 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001655 {
jchen10a9042d32017-03-17 08:50:45 +08001656 maxSize =
1657 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001658 }
1659
1660 return maxSize;
1661 }
1662 else
1663 {
1664 return 0;
1665 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001666}
1667
1668GLenum Program::getTransformFeedbackBufferMode() const
1669{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001670 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671}
1672
Jamie Madillbd044ed2017-06-05 12:59:21 -04001673bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001675 Shader *vertexShader = mState.mAttachedVertexShader;
1676 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001677
Jamie Madillbd044ed2017-06-05 12:59:21 -04001678 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001679
Jamie Madillbd044ed2017-06-05 12:59:21 -04001680 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1681 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682
Sami Väisänen46eaa942016-06-29 10:26:37 +03001683 std::map<GLuint, std::string> staticFragmentInputLocations;
1684
Jamie Madill4cff2472015-08-21 16:53:18 -04001685 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687 bool matched = false;
1688
1689 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001690 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001691 {
1692 continue;
1693 }
1694
Jamie Madill4cff2472015-08-21 16:53:18 -04001695 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001697 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001699 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001700 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001701 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702 {
1703 return false;
1704 }
1705
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706 matched = true;
1707 break;
1708 }
1709 }
1710
1711 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001712 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001714 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001715 return false;
1716 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001717
1718 // Check for aliased path rendering input bindings (if any).
1719 // If more than one binding refer statically to the same
1720 // location the link must fail.
1721
1722 if (!output.staticUse)
1723 continue;
1724
1725 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1726 if (inputBinding == -1)
1727 continue;
1728
1729 const auto it = staticFragmentInputLocations.find(inputBinding);
1730 if (it == std::end(staticFragmentInputLocations))
1731 {
1732 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1733 }
1734 else
1735 {
1736 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1737 << it->second;
1738 return false;
1739 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001740 }
1741
Jamie Madillbd044ed2017-06-05 12:59:21 -04001742 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001743 {
1744 return false;
1745 }
1746
Jamie Madillada9ecc2015-08-17 12:53:37 -04001747 // TODO(jmadill): verify no unmatched vertex varyings?
1748
Geoff Lang7dd2e102014-11-10 15:19:26 -05001749 return true;
1750}
1751
Jamie Madillbd044ed2017-06-05 12:59:21 -04001752bool Program::linkUniforms(const Context *context,
1753 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001754 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001755{
Olli Etuahob78707c2017-03-09 15:03:11 +00001756 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001757 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001758 {
1759 return false;
1760 }
1761
Olli Etuahob78707c2017-03-09 15:03:11 +00001762 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001763
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001764 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001765
1766 return true;
1767}
1768
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001769void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001770{
Jamie Madill982f6e02017-06-07 14:33:04 -04001771 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1772 unsigned int low = high;
1773
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001774 for (auto imageIter = mState.mUniforms.rbegin();
1775 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1776 {
1777 --low;
1778 }
1779
1780 mState.mImageUniformRange = RangeUI(low, high);
1781
1782 // If uniform is a image type, insert it into the mImageBindings array.
1783 for (unsigned int imageIndex : mState.mImageUniformRange)
1784 {
1785 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v}
1786 // commands cannot load values into a uniform defined as an image,
1787 // if declare without a binding qualifier, the image variable is
1788 // initially bound to unit zero.
1789 auto &imageUniform = mState.mUniforms[imageIndex];
1790 if (imageUniform.binding == -1)
1791 {
1792 imageUniform.binding = 0;
1793 }
1794 mState.mImageBindings.emplace_back(
1795 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1796 }
1797
1798 high = low;
1799
1800 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001801 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001802 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001803 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001804 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001805
1806 mState.mSamplerUniformRange = RangeUI(low, high);
1807
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001808 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001809 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001810 {
1811 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1812 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1813 mState.mSamplerBindings.emplace_back(
1814 SamplerBinding(textureType, samplerUniform.elementCount()));
1815 }
1816}
1817
Martin Radev4c4c8e72016-08-04 12:25:34 +03001818bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1819 const std::string &uniformName,
1820 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001821 const sh::InterfaceBlockField &fragmentUniform,
1822 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001824 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1825 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1826 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827 {
1828 return false;
1829 }
1830
1831 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1832 {
Jamie Madillf6113162015-05-07 11:49:21 -04001833 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 return false;
1835 }
1836
1837 return true;
1838}
1839
Jamie Madilleb979bf2016-11-15 12:28:46 -05001840// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001841bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001843 const ContextState &data = context->getContextState();
1844 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001845
Geoff Lang7dd2e102014-11-10 15:19:26 -05001846 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001847 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001848 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001849
1850 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001851 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001852 {
Jamie Madillf6113162015-05-07 11:49:21 -04001853 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001854 return false;
1855 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001857 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001858
Jamie Madillc349ec02015-08-21 16:53:12 -04001859 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001860 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001861 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001862 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001863 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001864 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001865 attribute.location = bindingLocation;
1866 }
1867
1868 if (attribute.location != -1)
1869 {
1870 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001871 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001872
Jamie Madill63805b42015-08-25 13:17:39 -04001873 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 {
Jamie Madillf6113162015-05-07 11:49:21 -04001875 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001876 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877
1878 return false;
1879 }
1880
Jamie Madill63805b42015-08-25 13:17:39 -04001881 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001882 {
Jamie Madill63805b42015-08-25 13:17:39 -04001883 const int regLocation = attribute.location + reg;
1884 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885
1886 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001887 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001888 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001889 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001890 // TODO(jmadill): fix aliasing on ES2
1891 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001893 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001894 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895 return false;
1896 }
1897 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001898 else
1899 {
Jamie Madill63805b42015-08-25 13:17:39 -04001900 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001901 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902
Jamie Madill63805b42015-08-25 13:17:39 -04001903 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904 }
1905 }
1906 }
1907
1908 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001909 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001910 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001911 // Not set by glBindAttribLocation or by location layout qualifier
1912 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 {
Jamie Madill63805b42015-08-25 13:17:39 -04001914 int regs = VariableRegisterCount(attribute.type);
1915 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001916
Jamie Madill63805b42015-08-25 13:17:39 -04001917 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 {
Jamie Madillf6113162015-05-07 11:49:21 -04001919 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001920 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001921 }
1922
Jamie Madillc349ec02015-08-21 16:53:12 -04001923 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924 }
1925 }
1926
Jamie Madill48ef11b2016-04-27 15:21:52 -04001927 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928 {
Jamie Madill63805b42015-08-25 13:17:39 -04001929 ASSERT(attribute.location != -1);
1930 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001931
Jamie Madill63805b42015-08-25 13:17:39 -04001932 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001933 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001934 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001935 }
1936 }
1937
Geoff Lang7dd2e102014-11-10 15:19:26 -05001938 return true;
1939}
1940
Martin Radev4c4c8e72016-08-04 12:25:34 +03001941bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1942 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1943 const std::string &errorMessage,
1944 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001946 GLuint blockCount = 0;
1947 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001949 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04001950 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001951 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04001952 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001953 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04001954 return false;
1955 }
1956 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001958 return true;
1959}
Jamie Madille473dee2015-08-18 14:49:01 -04001960
Martin Radev4c4c8e72016-08-04 12:25:34 +03001961bool Program::validateVertexAndFragmentInterfaceBlocks(
1962 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
1963 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001964 InfoLog &infoLog,
1965 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03001966{
1967 // Check that interface blocks defined in the vertex and fragment shaders are identical
1968 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
1969 UniformBlockMap linkedUniformBlocks;
1970
1971 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
1972 {
1973 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
1974 }
1975
Jamie Madille473dee2015-08-18 14:49:01 -04001976 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 {
Jamie Madille473dee2015-08-18 14:49:01 -04001978 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 if (entry != linkedUniformBlocks.end())
1980 {
1981 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04001982 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
1983 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984 {
1985 return false;
1986 }
1987 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001988 }
1989 return true;
1990}
Jamie Madille473dee2015-08-18 14:49:01 -04001991
Jamie Madillbd044ed2017-06-05 12:59:21 -04001992bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001993{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001994 const auto &caps = context->getCaps();
1995
Martin Radev4c4c8e72016-08-04 12:25:34 +03001996 if (mState.mAttachedComputeShader)
1997 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001998 Shader &computeShader = *mState.mAttachedComputeShader;
1999 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002000
2001 if (!validateUniformBlocksCount(
2002 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2003 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2004 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002005 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002006 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002008 return true;
2009 }
2010
Jamie Madillbd044ed2017-06-05 12:59:21 -04002011 Shader &vertexShader = *mState.mAttachedVertexShader;
2012 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002013
Jamie Madillbd044ed2017-06-05 12:59:21 -04002014 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2015 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002016
2017 if (!validateUniformBlocksCount(
2018 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2019 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2020 {
2021 return false;
2022 }
2023 if (!validateUniformBlocksCount(
2024 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2025 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2026 infoLog))
2027 {
2028
2029 return false;
2030 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002031
2032 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002033 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002034 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002035 {
2036 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037 }
Jamie Madille473dee2015-08-18 14:49:01 -04002038
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 return true;
2040}
2041
Jamie Madilla2c74982016-12-12 11:20:42 -05002042bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002043 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002044 const sh::InterfaceBlock &fragmentInterfaceBlock,
2045 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046{
2047 const char* blockName = vertexInterfaceBlock.name.c_str();
2048 // validate blocks for the same member types
2049 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2050 {
Jamie Madillf6113162015-05-07 11:49:21 -04002051 infoLog << "Types for interface block '" << blockName
2052 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 return false;
2054 }
2055 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2056 {
Jamie Madillf6113162015-05-07 11:49:21 -04002057 infoLog << "Array sizes differ for interface block '" << blockName
2058 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059 return false;
2060 }
jchen10af713a22017-04-19 09:10:56 +08002061 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2062 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2063 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 {
Jamie Madillf6113162015-05-07 11:49:21 -04002065 infoLog << "Layout qualifiers differ for interface block '" << blockName
2066 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 return false;
2068 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002069 const unsigned int numBlockMembers =
2070 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2072 {
2073 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2074 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2075 if (vertexMember.name != fragmentMember.name)
2076 {
Jamie Madillf6113162015-05-07 11:49:21 -04002077 infoLog << "Name mismatch for field " << blockMemberIndex
2078 << " of interface block '" << blockName
2079 << "': (in vertex: '" << vertexMember.name
2080 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 return false;
2082 }
2083 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002084 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2085 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086 {
2087 return false;
2088 }
2089 }
2090 return true;
2091}
2092
2093bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2094 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2095{
2096 if (vertexVariable.type != fragmentVariable.type)
2097 {
Jamie Madillf6113162015-05-07 11:49:21 -04002098 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 return false;
2100 }
2101 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2102 {
Jamie Madillf6113162015-05-07 11:49:21 -04002103 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 return false;
2105 }
2106 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2107 {
Jamie Madillf6113162015-05-07 11:49:21 -04002108 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109 return false;
2110 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002111 if (vertexVariable.structName != fragmentVariable.structName)
2112 {
2113 infoLog << "Structure names for " << variableName
2114 << " differ between vertex and fragment shaders";
2115 return false;
2116 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117
2118 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2119 {
Jamie Madillf6113162015-05-07 11:49:21 -04002120 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 return false;
2122 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002123 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2125 {
2126 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2127 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2128
2129 if (vertexMember.name != fragmentMember.name)
2130 {
Jamie Madillf6113162015-05-07 11:49:21 -04002131 infoLog << "Name mismatch for field '" << memberIndex
2132 << "' of " << variableName
2133 << ": (in vertex: '" << vertexMember.name
2134 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 return false;
2136 }
2137
2138 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2139 vertexMember.name + "'";
2140
2141 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2142 {
2143 return false;
2144 }
2145 }
2146
2147 return true;
2148}
2149
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002150bool Program::linkValidateVaryings(InfoLog &infoLog,
2151 const std::string &varyingName,
2152 const sh::Varying &vertexVarying,
2153 const sh::Varying &fragmentVarying,
2154 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155{
2156 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2157 {
2158 return false;
2159 }
2160
Jamie Madille9cc4692015-02-19 16:00:13 -05002161 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002163 infoLog << "Interpolation types for " << varyingName
2164 << " differ between vertex and fragment shaders.";
2165 return false;
2166 }
2167
2168 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2169 {
2170 infoLog << "Invariance for " << varyingName
2171 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 return false;
2173 }
2174
2175 return true;
2176}
2177
Jamie Madillbd044ed2017-06-05 12:59:21 -04002178bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002179{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002180 Shader *vertexShader = mState.mAttachedVertexShader;
2181 Shader *fragmentShader = mState.mAttachedFragmentShader;
2182 const auto &vertexVaryings = vertexShader->getVaryings(context);
2183 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2184 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002185
2186 if (shaderVersion != 100)
2187 {
2188 // Only ESSL 1.0 has restrictions on matching input and output invariance
2189 return true;
2190 }
2191
2192 bool glPositionIsInvariant = false;
2193 bool glPointSizeIsInvariant = false;
2194 bool glFragCoordIsInvariant = false;
2195 bool glPointCoordIsInvariant = false;
2196
2197 for (const sh::Varying &varying : vertexVaryings)
2198 {
2199 if (!varying.isBuiltIn())
2200 {
2201 continue;
2202 }
2203 if (varying.name.compare("gl_Position") == 0)
2204 {
2205 glPositionIsInvariant = varying.isInvariant;
2206 }
2207 else if (varying.name.compare("gl_PointSize") == 0)
2208 {
2209 glPointSizeIsInvariant = varying.isInvariant;
2210 }
2211 }
2212
2213 for (const sh::Varying &varying : fragmentVaryings)
2214 {
2215 if (!varying.isBuiltIn())
2216 {
2217 continue;
2218 }
2219 if (varying.name.compare("gl_FragCoord") == 0)
2220 {
2221 glFragCoordIsInvariant = varying.isInvariant;
2222 }
2223 else if (varying.name.compare("gl_PointCoord") == 0)
2224 {
2225 glPointCoordIsInvariant = varying.isInvariant;
2226 }
2227 }
2228
2229 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2230 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2231 // Not requiring invariance to match is supported by:
2232 // dEQP, WebGL CTS, Nexus 5X GLES
2233 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2234 {
2235 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2236 "declared invariant.";
2237 return false;
2238 }
2239 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2240 {
2241 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2242 "declared invariant.";
2243 return false;
2244 }
2245
2246 return true;
2247}
2248
jchen10a9042d32017-03-17 08:50:45 +08002249bool Program::linkValidateTransformFeedback(const gl::Context *context,
2250 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002251 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002252 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002253{
2254 size_t totalComponents = 0;
2255
Jamie Madillccdf74b2015-08-18 10:46:12 -04002256 std::set<std::string> uniqueNames;
2257
Jamie Madill48ef11b2016-04-27 15:21:52 -04002258 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 {
2260 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002261 size_t subscript = GL_INVALID_INDEX;
2262 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2263
Jamie Madill192745a2016-12-22 15:58:21 -05002264 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265 {
Jamie Madill192745a2016-12-22 15:58:21 -05002266 const sh::Varying *varying = ref.second.get();
2267
jchen10a9042d32017-03-17 08:50:45 +08002268 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002270 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002272 infoLog << "Two transform feedback varyings specify the same output variable ("
2273 << tfVaryingName << ").";
2274 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002275 }
jchen10a9042d32017-03-17 08:50:45 +08002276 if (context->getClientVersion() >= Version(3, 1))
2277 {
2278 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2279 {
2280 infoLog
2281 << "Two transform feedback varyings include the same array element ("
2282 << tfVaryingName << ").";
2283 return false;
2284 }
2285 }
2286 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002287 {
2288 infoLog << "Capture of arrays is undefined and not supported.";
2289 return false;
2290 }
2291
jchen10a9042d32017-03-17 08:50:45 +08002292 uniqueNames.insert(tfVaryingName);
2293
Jamie Madillccdf74b2015-08-18 10:46:12 -04002294 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002295 size_t elementCount =
2296 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2297 : 1);
2298 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002299 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300 componentCount > caps.maxTransformFeedbackSeparateComponents)
2301 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002302 infoLog << "Transform feedback varying's " << varying->name << " components ("
2303 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002304 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002305 return false;
2306 }
2307
2308 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002309 found = true;
2310 break;
2311 }
2312 }
jchen10a9042d32017-03-17 08:50:45 +08002313 if (context->getClientVersion() < Version(3, 1) &&
2314 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002315 {
Geoff Lang1a683462015-09-29 15:09:59 -04002316 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002317 return false;
2318 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002319 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2320 ASSERT(found);
2321 }
2322
Jamie Madill48ef11b2016-04-27 15:21:52 -04002323 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002324 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325 {
Jamie Madillf6113162015-05-07 11:49:21 -04002326 infoLog << "Transform feedback varying total components (" << totalComponents
2327 << ") exceed the maximum interleaved components ("
2328 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002329 return false;
2330 }
2331
2332 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002333}
2334
Jamie Madill192745a2016-12-22 15:58:21 -05002335void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002336{
2337 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002338 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002339 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002340 {
jchen10a9042d32017-03-17 08:50:45 +08002341 size_t subscript = GL_INVALID_INDEX;
2342 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002343 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002344 {
Jamie Madill192745a2016-12-22 15:58:21 -05002345 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002346 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002347 {
jchen10a9042d32017-03-17 08:50:45 +08002348 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2349 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002350 break;
2351 }
2352 }
2353 }
2354}
2355
Jamie Madillbd044ed2017-06-05 12:59:21 -04002356Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002357{
Jamie Madill192745a2016-12-22 15:58:21 -05002358 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002359
Jamie Madillbd044ed2017-06-05 12:59:21 -04002360 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002361 {
Jamie Madill192745a2016-12-22 15:58:21 -05002362 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002363 }
2364
Jamie Madillbd044ed2017-06-05 12:59:21 -04002365 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002366 {
Jamie Madill192745a2016-12-22 15:58:21 -05002367 merged[varying.name].fragment = &varying;
2368 }
2369
2370 return merged;
2371}
2372
2373std::vector<PackedVarying> Program::getPackedVaryings(
2374 const Program::MergedVaryings &mergedVaryings) const
2375{
2376 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2377 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002378 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002379
2380 for (const auto &ref : mergedVaryings)
2381 {
2382 const sh::Varying *input = ref.second.vertex;
2383 const sh::Varying *output = ref.second.fragment;
2384
2385 // Only pack varyings that have a matched input or output, plus special builtins.
2386 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002387 {
Jamie Madill192745a2016-12-22 15:58:21 -05002388 // Will get the vertex shader interpolation by default.
2389 auto interpolation = ref.second.get()->interpolation;
2390
2391 // Interpolation qualifiers must match.
2392 if (output->isStruct())
2393 {
2394 ASSERT(!output->isArray());
2395 for (const auto &field : output->fields)
2396 {
2397 ASSERT(!field.isStruct() && !field.isArray());
2398 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2399 }
2400 }
2401 else
2402 {
2403 packedVaryings.push_back(PackedVarying(*output, interpolation));
2404 }
2405 continue;
2406 }
2407
2408 // Keep Transform FB varyings in the merged list always.
2409 if (!input)
2410 {
2411 continue;
2412 }
2413
2414 for (const std::string &tfVarying : tfVaryings)
2415 {
jchen10a9042d32017-03-17 08:50:45 +08002416 size_t subscript = GL_INVALID_INDEX;
2417 std::string baseName = ParseResourceName(tfVarying, &subscript);
2418 if (uniqueFullNames.count(tfVarying) > 0)
2419 {
2420 continue;
2421 }
2422 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002423 {
2424 // Transform feedback for varying structs is underspecified.
2425 // See Khronos bug 9856.
2426 // TODO(jmadill): Figure out how to be spec-compliant here.
2427 if (!input->isStruct())
2428 {
2429 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2430 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002431 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2432 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002433 }
jchen10a9042d32017-03-17 08:50:45 +08002434 if (subscript == GL_INVALID_INDEX)
2435 {
2436 break;
2437 }
Jamie Madill192745a2016-12-22 15:58:21 -05002438 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002439 }
2440 }
2441
Jamie Madill192745a2016-12-22 15:58:21 -05002442 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2443
2444 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002445}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002446
Jamie Madillbd044ed2017-06-05 12:59:21 -04002447void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002448{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002449 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002450 ASSERT(fragmentShader != nullptr);
2451
Geoff Lange0cff192017-05-30 13:04:56 -04002452 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002453 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002454
2455 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002456 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002457 {
2458 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2459 outputVariable.name != "gl_FragData")
2460 {
2461 continue;
2462 }
2463
2464 unsigned int baseLocation =
2465 (outputVariable.location == -1 ? 0u
2466 : static_cast<unsigned int>(outputVariable.location));
2467 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2468 elementIndex++)
2469 {
2470 const unsigned int location = baseLocation + elementIndex;
2471 if (location >= mState.mOutputVariableTypes.size())
2472 {
2473 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2474 }
Corentin Walleze7557742017-06-01 13:09:57 -04002475 ASSERT(location < mState.mActiveOutputVariables.size());
2476 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002477 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2478 }
2479 }
2480
Jamie Madill80a6fc02015-08-21 16:53:16 -04002481 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002482 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002483 return;
2484
Jamie Madillbd044ed2017-06-05 12:59:21 -04002485 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002486 // TODO(jmadill): any caps validation here?
2487
jchen1015015f72017-03-16 13:54:21 +08002488 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002489 outputVariableIndex++)
2490 {
jchen1015015f72017-03-16 13:54:21 +08002491 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002492
2493 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2494 if (outputVariable.isBuiltIn())
2495 continue;
2496
2497 // Since multiple output locations must be specified, use 0 for non-specified locations.
2498 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2499
Jamie Madill80a6fc02015-08-21 16:53:16 -04002500 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2501 elementIndex++)
2502 {
2503 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002504 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002505 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002506 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002507 VariableLocation(outputVariable.name, element, outputVariableIndex);
2508 }
2509 }
2510}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002511
Olli Etuaho48fed632017-03-16 12:05:30 +00002512void Program::setUniformValuesFromBindingQualifiers()
2513{
Jamie Madill982f6e02017-06-07 14:33:04 -04002514 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002515 {
2516 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2517 if (samplerUniform.binding != -1)
2518 {
2519 GLint location = mState.getUniformLocation(samplerUniform.name);
2520 ASSERT(location != -1);
2521 std::vector<GLint> boundTextureUnits;
2522 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2523 ++elementIndex)
2524 {
2525 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2526 }
2527 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2528 boundTextureUnits.data());
2529 }
2530 }
2531}
2532
Jamie Madillbd044ed2017-06-05 12:59:21 -04002533void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002534{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002535 ASSERT(mState.mUniformBlocks.empty());
2536
2537 if (mState.mAttachedComputeShader)
2538 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002539 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002540
Jamie Madillbd044ed2017-06-05 12:59:21 -04002541 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002542 {
2543
2544 // Only 'packed' blocks are allowed to be considered inactive.
2545 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2546 continue;
2547
Jamie Madilla2c74982016-12-12 11:20:42 -05002548 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002549 {
2550 if (block.name == computeBlock.name)
2551 {
2552 block.computeStaticUse = computeBlock.staticUse;
2553 }
2554 }
2555
2556 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2557 }
2558 return;
2559 }
2560
Jamie Madill62d31cb2015-09-11 13:25:51 -04002561 std::set<std::string> visitedList;
2562
Jamie Madillbd044ed2017-06-05 12:59:21 -04002563 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002564
Jamie Madillbd044ed2017-06-05 12:59:21 -04002565 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002566 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002567 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002568 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2569 continue;
2570
2571 if (visitedList.count(vertexBlock.name) > 0)
2572 continue;
2573
2574 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2575 visitedList.insert(vertexBlock.name);
2576 }
2577
Jamie Madillbd044ed2017-06-05 12:59:21 -04002578 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002579
Jamie Madillbd044ed2017-06-05 12:59:21 -04002580 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002581 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002582 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002583 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2584 continue;
2585
2586 if (visitedList.count(fragmentBlock.name) > 0)
2587 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002588 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002589 {
2590 if (block.name == fragmentBlock.name)
2591 {
2592 block.fragmentStaticUse = fragmentBlock.staticUse;
2593 }
2594 }
2595
2596 continue;
2597 }
2598
2599 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2600 visitedList.insert(fragmentBlock.name);
2601 }
jchen10af713a22017-04-19 09:10:56 +08002602 // Set initial bindings from shader.
2603 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2604 {
2605 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2606 bindUniformBlock(blockIndex, uniformBlock.binding);
2607 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002608}
2609
Jamie Madill4a3c2342015-10-08 12:58:45 -04002610template <typename VarT>
2611void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2612 const std::string &prefix,
2613 int blockIndex)
2614{
2615 for (const VarT &field : fields)
2616 {
2617 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2618
2619 if (field.isStruct())
2620 {
2621 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2622 {
2623 const std::string uniformElementName =
2624 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2625 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2626 }
2627 }
2628 else
2629 {
2630 // If getBlockMemberInfo returns false, the uniform is optimized out.
2631 sh::BlockMemberInfo memberInfo;
2632 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2633 {
2634 continue;
2635 }
2636
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002637 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002638 blockIndex, memberInfo);
2639
2640 // Since block uniforms have no location, we don't need to store them in the uniform
2641 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002642 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002643 }
2644 }
2645}
2646
Jamie Madill62d31cb2015-09-11 13:25:51 -04002647void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2648{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002649 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002650 size_t blockSize = 0;
2651
Jamie Madill4a3c2342015-10-08 12:58:45 -04002652 // Track the first and last uniform index to determine the range of active uniforms in the
2653 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002654 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002655 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002656 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002657
2658 std::vector<unsigned int> blockUniformIndexes;
2659 for (size_t blockUniformIndex = firstBlockUniformIndex;
2660 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2661 {
2662 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2663 }
jchen10af713a22017-04-19 09:10:56 +08002664 // ESSL 3.10 section 4.4.4 page 58:
2665 // Any uniform or shader storage block declared without a binding qualifier is initially
2666 // assigned to block binding point zero.
2667 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002668 if (interfaceBlock.arraySize > 0)
2669 {
2670 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2671 {
jchen10af713a22017-04-19 09:10:56 +08002672 // Don't define this block at all if it's not active in the implementation.
2673 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2674 &blockSize))
2675 {
2676 continue;
2677 }
2678 UniformBlock block(interfaceBlock.name, true, arrayElement,
2679 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002680 block.memberUniformIndexes = blockUniformIndexes;
2681
Martin Radev4c4c8e72016-08-04 12:25:34 +03002682 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002683 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002684 case GL_VERTEX_SHADER:
2685 {
2686 block.vertexStaticUse = interfaceBlock.staticUse;
2687 break;
2688 }
2689 case GL_FRAGMENT_SHADER:
2690 {
2691 block.fragmentStaticUse = interfaceBlock.staticUse;
2692 break;
2693 }
2694 case GL_COMPUTE_SHADER:
2695 {
2696 block.computeStaticUse = interfaceBlock.staticUse;
2697 break;
2698 }
2699 default:
2700 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002701 }
2702
Qin Jiajia0350a642016-11-01 17:01:51 +08002703 // Since all block elements in an array share the same active uniforms, they will all be
2704 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2705 // here we will add every block element in the array.
2706 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002707 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002708 }
2709 }
2710 else
2711 {
jchen10af713a22017-04-19 09:10:56 +08002712 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2713 {
2714 return;
2715 }
2716 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002717 block.memberUniformIndexes = blockUniformIndexes;
2718
Martin Radev4c4c8e72016-08-04 12:25:34 +03002719 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002720 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002721 case GL_VERTEX_SHADER:
2722 {
2723 block.vertexStaticUse = interfaceBlock.staticUse;
2724 break;
2725 }
2726 case GL_FRAGMENT_SHADER:
2727 {
2728 block.fragmentStaticUse = interfaceBlock.staticUse;
2729 break;
2730 }
2731 case GL_COMPUTE_SHADER:
2732 {
2733 block.computeStaticUse = interfaceBlock.staticUse;
2734 break;
2735 }
2736 default:
2737 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002738 }
2739
Jamie Madill4a3c2342015-10-08 12:58:45 -04002740 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002741 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002742 }
2743}
2744
Jamie Madille7d84322017-01-10 18:21:59 -05002745template <>
2746void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2747 const uint8_t *destPointer,
2748 GLsizei clampedCount,
2749 const GLint *v)
2750{
2751 // Invalidate the validation cache only if we modify the sampler data.
2752 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2753 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2754 {
2755 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2756 std::vector<GLuint> *boundTextureUnits =
2757 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2758
2759 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2760 mCachedValidateSamplersResult.reset();
2761 }
2762}
2763
2764template <typename T>
2765void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2766 const uint8_t *destPointer,
2767 GLsizei clampedCount,
2768 const T *v)
2769{
2770}
2771
Jamie Madill62d31cb2015-09-11 13:25:51 -04002772template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002773GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002774{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002775 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2776 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002777 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2778
Corentin Wallez15ac5342016-11-03 17:06:39 -04002779 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2780 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2781 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002782 GLsizei maxElementCount =
2783 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2784
2785 GLsizei count = countIn;
2786 GLsizei clampedCount = count * vectorSize;
2787 if (clampedCount > maxElementCount)
2788 {
2789 clampedCount = maxElementCount;
2790 count = maxElementCount / vectorSize;
2791 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002792
Jamie Madill62d31cb2015-09-11 13:25:51 -04002793 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2794 {
2795 // Do a cast conversion for boolean types. From the spec:
2796 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2797 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002798 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002799 {
2800 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2801 }
2802 }
2803 else
2804 {
Jamie Madille7d84322017-01-10 18:21:59 -05002805 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002806 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002807 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002808
2809 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002810}
2811
2812template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002813GLsizei Program::setMatrixUniformInternal(GLint location,
2814 GLsizei count,
2815 GLboolean transpose,
2816 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817{
2818 if (!transpose)
2819 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002820 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002821 }
2822
2823 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002824 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2825 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002827
2828 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2829 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2830 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2831 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2832
2833 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002834 {
2835 size_t elementOffset = element * rows * cols;
2836
2837 for (size_t row = 0; row < rows; ++row)
2838 {
2839 for (size_t col = 0; col < cols; ++col)
2840 {
2841 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2842 }
2843 }
2844 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002845
2846 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002847}
2848
2849template <typename DestT>
2850void Program::getUniformInternal(GLint location, DestT *dataOut) const
2851{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002852 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2853 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002854
2855 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2856
2857 GLenum componentType = VariableComponentType(uniform.type);
2858 if (componentType == GLTypeToGLenum<DestT>::value)
2859 {
2860 memcpy(dataOut, srcPointer, uniform.getElementSize());
2861 return;
2862 }
2863
Corentin Wallez6596c462016-03-17 17:26:58 -04002864 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002865
2866 switch (componentType)
2867 {
2868 case GL_INT:
2869 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2870 break;
2871 case GL_UNSIGNED_INT:
2872 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2873 break;
2874 case GL_BOOL:
2875 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2876 break;
2877 case GL_FLOAT:
2878 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2879 break;
2880 default:
2881 UNREACHABLE();
2882 }
2883}
Jamie Madilla4595b82017-01-11 17:36:34 -05002884
2885bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2886{
2887 // Must be called after samplers are validated.
2888 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2889
2890 for (const auto &binding : mState.mSamplerBindings)
2891 {
2892 GLenum textureType = binding.textureType;
2893 for (const auto &unit : binding.boundTextureUnits)
2894 {
2895 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2896 if (programTextureID == textureID)
2897 {
2898 // TODO(jmadill): Check for appropriate overlap.
2899 return true;
2900 }
2901 }
2902 }
2903
2904 return false;
2905}
2906
Jamie Madilla2c74982016-12-12 11:20:42 -05002907} // namespace gl