blob: e966c600637d15676b66d5a3ba3b22a586d38087 [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
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000619 mInfoLog.reset();
620
Martin Radev4c4c8e72016-08-04 12:25:34 +0300621 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500622
Jamie Madill192745a2016-12-22 15:58:21 -0500623 auto vertexShader = mState.mAttachedVertexShader;
624 auto fragmentShader = mState.mAttachedFragmentShader;
625 auto computeShader = mState.mAttachedComputeShader;
626
627 bool isComputeShaderAttached = (computeShader != nullptr);
628 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300629 // Check whether we both have a compute and non-compute shaders attached.
630 // If there are of both types attached, then linking should fail.
631 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
632 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500633 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300634 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
635 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400636 }
637
Jamie Madill192745a2016-12-22 15:58:21 -0500638 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500639 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400640 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641 {
642 mInfoLog << "Attached compute shader is not compiled.";
643 return NoError();
644 }
Jamie Madill192745a2016-12-22 15:58:21 -0500645 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300646
Jamie Madillbd044ed2017-06-05 12:59:21 -0400647 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648
649 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
650 // If the work group size is not specified, a link time error should occur.
651 if (!mState.mComputeShaderLocalSize.isDeclared())
652 {
653 mInfoLog << "Work group size is not specified.";
654 return NoError();
655 }
656
Jamie Madillbd044ed2017-06-05 12:59:21 -0400657 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658 {
659 return NoError();
660 }
661
Jamie Madillbd044ed2017-06-05 12:59:21 -0400662 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300663 {
664 return NoError();
665 }
666
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500667 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400668 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500669 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300670 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500671 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300672 }
673 }
674 else
675 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400676 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300677 {
678 return NoError();
679 }
Jamie Madill192745a2016-12-22 15:58:21 -0500680 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300681
Jamie Madillbd044ed2017-06-05 12:59:21 -0400682 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300683 {
684 return NoError();
685 }
Jamie Madill192745a2016-12-22 15:58:21 -0500686 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300687
Jamie Madillbd044ed2017-06-05 12:59:21 -0400688 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 {
690 mInfoLog << "Fragment shader version does not match vertex shader version.";
691 return NoError();
692 }
693
Jamie Madillbd044ed2017-06-05 12:59:21 -0400694 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300695 {
696 return NoError();
697 }
698
Jamie Madillbd044ed2017-06-05 12:59:21 -0400699 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300700 {
701 return NoError();
702 }
703
Jamie Madillbd044ed2017-06-05 12:59:21 -0400704 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 {
706 return NoError();
707 }
708
Jamie Madillbd044ed2017-06-05 12:59:21 -0400709 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300710 {
711 return NoError();
712 }
713
Jamie Madillbd044ed2017-06-05 12:59:21 -0400714 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300715
jchen10a9042d32017-03-17 08:50:45 +0800716 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717 {
718 return NoError();
719 }
720
Jamie Madillbd044ed2017-06-05 12:59:21 -0400721 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300722
Jamie Madill192745a2016-12-22 15:58:21 -0500723 // Validate we can pack the varyings.
724 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
725
726 // Map the varyings to the register file
727 // In WebGL, we use a slightly different handling for packing variables.
728 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
729 : PackMode::ANGLE_RELAXED;
730 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
731 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
732 mState.getTransformFeedbackVaryingNames()))
733 {
734 return NoError();
735 }
736
Jamie Madillc564c072017-06-01 12:45:42 -0400737 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500738 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300739 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500740 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300741 }
742
743 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500744 }
745
Olli Etuaho48fed632017-03-16 12:05:30 +0000746 setUniformValuesFromBindingQualifiers();
747
Jamie Madillbd044ed2017-06-05 12:59:21 -0400748 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400749
Martin Radev4c4c8e72016-08-04 12:25:34 +0300750 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000751}
752
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000753// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500754void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400756 mState.mAttributes.clear();
757 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800758 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400759 mState.mUniforms.clear();
760 mState.mUniformLocations.clear();
761 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800762 mState.mActiveUniformBlockBindings.reset();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400763 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800764 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400765 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400766 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300767 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500768 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500769
Geoff Lang7dd2e102014-11-10 15:19:26 -0500770 mValidated = false;
771
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000772 mLinked = false;
773}
774
Geoff Lange1a27752015-10-05 13:16:04 -0400775bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000776{
777 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
Jamie Madilla2c74982016-12-12 11:20:42 -0500780Error Program::loadBinary(const Context *context,
781 GLenum binaryFormat,
782 const void *binary,
783 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000784{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500785 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000786
Geoff Lang7dd2e102014-11-10 15:19:26 -0500787#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800788 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500789#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400790 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
791 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000792 {
Jamie Madillf6113162015-05-07 11:49:21 -0400793 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800794 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500795 }
796
Jamie Madill4f86d052017-06-05 12:59:26 -0400797 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
798 ANGLE_TRY_RESULT(
799 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500800 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500801#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500802}
803
Jamie Madilla2c74982016-12-12 11:20:42 -0500804Error Program::saveBinary(const Context *context,
805 GLenum *binaryFormat,
806 void *binary,
807 GLsizei bufSize,
808 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500809{
810 if (binaryFormat)
811 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400812 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500813 }
814
Jamie Madill4f86d052017-06-05 12:59:26 -0400815 angle::MemoryBuffer memoryBuf;
816 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500817
Jamie Madill4f86d052017-06-05 12:59:26 -0400818 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
819 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500820
821 if (streamLength > bufSize)
822 {
823 if (length)
824 {
825 *length = 0;
826 }
827
828 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
829 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
830 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500831 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500832 }
833
834 if (binary)
835 {
836 char *ptr = reinterpret_cast<char*>(binary);
837
Jamie Madill48ef11b2016-04-27 15:21:52 -0400838 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500839 ptr += streamLength;
840
841 ASSERT(ptr - streamLength == binary);
842 }
843
844 if (length)
845 {
846 *length = streamLength;
847 }
848
He Yunchaoacd18982017-01-04 10:46:42 +0800849 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500850}
851
852GLint Program::getBinaryLength() const
853{
854 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -0500855 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500856 if (error.isError())
857 {
858 return 0;
859 }
860
861 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000862}
863
Geoff Langc5629752015-12-07 16:29:04 -0500864void Program::setBinaryRetrievableHint(bool retrievable)
865{
866 // TODO(jmadill) : replace with dirty bits
867 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400868 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500869}
870
871bool Program::getBinaryRetrievableHint() const
872{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400873 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500874}
875
Yunchao He61afff12017-03-14 15:34:03 +0800876void Program::setSeparable(bool separable)
877{
878 // TODO(yunchao) : replace with dirty bits
879 if (mState.mSeparable != separable)
880 {
881 mProgram->setSeparable(separable);
882 mState.mSeparable = separable;
883 }
884}
885
886bool Program::isSeparable() const
887{
888 return mState.mSeparable;
889}
890
Jamie Madill6c1f6712017-02-14 19:08:04 -0500891void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000892{
893 mRefCount--;
894
895 if (mRefCount == 0 && mDeleteStatus)
896 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500897 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000898 }
899}
900
901void Program::addRef()
902{
903 mRefCount++;
904}
905
906unsigned int Program::getRefCount() const
907{
908 return mRefCount;
909}
910
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000911int Program::getInfoLogLength() const
912{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400913 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000914}
915
Geoff Lange1a27752015-10-05 13:16:04 -0400916void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000917{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000918 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000919}
920
Geoff Lange1a27752015-10-05 13:16:04 -0400921void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000922{
923 int total = 0;
924
Martin Radev4c4c8e72016-08-04 12:25:34 +0300925 if (mState.mAttachedComputeShader)
926 {
927 if (total < maxCount)
928 {
929 shaders[total] = mState.mAttachedComputeShader->getHandle();
930 total++;
931 }
932 }
933
Jamie Madill48ef11b2016-04-27 15:21:52 -0400934 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000935 {
936 if (total < maxCount)
937 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400938 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200939 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000940 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000941 }
942
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000944 {
945 if (total < maxCount)
946 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400947 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200948 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000949 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000950 }
951
952 if (count)
953 {
954 *count = total;
955 }
956}
957
Geoff Lange1a27752015-10-05 13:16:04 -0400958GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500959{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400960 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500961}
962
Jamie Madill63805b42015-08-25 13:17:39 -0400963bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400964{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400965 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
966 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500967}
968
jchen10fd7c3b52017-03-21 15:36:03 +0800969void Program::getActiveAttribute(GLuint index,
970 GLsizei bufsize,
971 GLsizei *length,
972 GLint *size,
973 GLenum *type,
974 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000975{
Jamie Madillc349ec02015-08-21 16:53:12 -0400976 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000977 {
978 if (bufsize > 0)
979 {
980 name[0] = '\0';
981 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500982
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000983 if (length)
984 {
985 *length = 0;
986 }
987
988 *type = GL_NONE;
989 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400990 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000991 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400992
jchen1036e120e2017-03-14 14:53:58 +0800993 ASSERT(index < mState.mAttributes.size());
994 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -0400995
996 if (bufsize > 0)
997 {
jchen10fd7c3b52017-03-21 15:36:03 +0800998 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -0400999 }
1000
1001 // Always a single 'type' instance
1002 *size = 1;
1003 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001004}
1005
Geoff Lange1a27752015-10-05 13:16:04 -04001006GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001007{
Jamie Madillc349ec02015-08-21 16:53:12 -04001008 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001009 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001010 return 0;
1011 }
1012
jchen1036e120e2017-03-14 14:53:58 +08001013 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001014}
1015
Geoff Lange1a27752015-10-05 13:16:04 -04001016GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001017{
Jamie Madillc349ec02015-08-21 16:53:12 -04001018 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001019 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001020 return 0;
1021 }
1022
1023 size_t maxLength = 0;
1024
Jamie Madill48ef11b2016-04-27 15:21:52 -04001025 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001026 {
jchen1036e120e2017-03-14 14:53:58 +08001027 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001028 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001029
Jamie Madillc349ec02015-08-21 16:53:12 -04001030 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001031}
1032
jchen1015015f72017-03-16 13:54:21 +08001033GLuint Program::getInputResourceIndex(const GLchar *name) const
1034{
1035 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1036 {
1037 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1038 if (attribute.name == name)
1039 {
1040 return attributeIndex;
1041 }
1042 }
1043 return GL_INVALID_INDEX;
1044}
1045
1046GLuint Program::getOutputResourceIndex(const GLchar *name) const
1047{
1048 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1049}
1050
jchen10fd7c3b52017-03-21 15:36:03 +08001051size_t Program::getOutputResourceCount() const
1052{
1053 return (mLinked ? mState.mOutputVariables.size() : 0);
1054}
1055
1056void Program::getInputResourceName(GLuint index,
1057 GLsizei bufSize,
1058 GLsizei *length,
1059 GLchar *name) const
1060{
1061 GLint size;
1062 GLenum type;
1063 getActiveAttribute(index, bufSize, length, &size, &type, name);
1064}
1065
1066void Program::getOutputResourceName(GLuint index,
1067 GLsizei bufSize,
1068 GLsizei *length,
1069 GLchar *name) const
1070{
1071 if (length)
1072 {
1073 *length = 0;
1074 }
1075
1076 if (!mLinked)
1077 {
1078 if (bufSize > 0)
1079 {
1080 name[0] = '\0';
1081 }
1082 return;
1083 }
1084 ASSERT(index < mState.mOutputVariables.size());
1085 const auto &output = mState.mOutputVariables[index];
1086
1087 if (bufSize > 0)
1088 {
1089 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1090
1091 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1092 }
1093}
1094
Geoff Lang7dd2e102014-11-10 15:19:26 -05001095GLint Program::getFragDataLocation(const std::string &name) const
1096{
1097 std::string baseName(name);
1098 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001099 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001100 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001101 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001102 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1103 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001104 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001105 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001106 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108}
1109
Geoff Lange1a27752015-10-05 13:16:04 -04001110void Program::getActiveUniform(GLuint index,
1111 GLsizei bufsize,
1112 GLsizei *length,
1113 GLint *size,
1114 GLenum *type,
1115 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001118 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001119 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001120 ASSERT(index < mState.mUniforms.size());
1121 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001122
1123 if (bufsize > 0)
1124 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001125 std::string string = uniform.name;
1126 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001127 {
1128 string += "[0]";
1129 }
jchen10fd7c3b52017-03-21 15:36:03 +08001130 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001131 }
1132
Jamie Madill62d31cb2015-09-11 13:25:51 -04001133 *size = uniform.elementCount();
1134 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001135 }
1136 else
1137 {
1138 if (bufsize > 0)
1139 {
1140 name[0] = '\0';
1141 }
1142
1143 if (length)
1144 {
1145 *length = 0;
1146 }
1147
1148 *size = 0;
1149 *type = GL_NONE;
1150 }
1151}
1152
Geoff Lange1a27752015-10-05 13:16:04 -04001153GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001154{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001155 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001156 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001157 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001158 }
1159 else
1160 {
1161 return 0;
1162 }
1163}
1164
Geoff Lange1a27752015-10-05 13:16:04 -04001165GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001166{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001167 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001168
1169 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001170 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001171 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001172 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001173 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001174 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001175 size_t length = uniform.name.length() + 1u;
1176 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001177 {
1178 length += 3; // Counting in "[0]".
1179 }
1180 maxLength = std::max(length, maxLength);
1181 }
1182 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001183 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001184
Jamie Madill62d31cb2015-09-11 13:25:51 -04001185 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186}
1187
1188GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1189{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001190 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001191 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001192 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001193 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001194 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1195 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1196 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1197 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1198 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1199 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1200 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1201 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1202 default:
1203 UNREACHABLE();
1204 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001205 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001206 return 0;
1207}
1208
1209bool Program::isValidUniformLocation(GLint location) const
1210{
Jamie Madille2e406c2016-06-02 13:04:10 -04001211 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001212 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1213 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001214}
1215
Jamie Madill62d31cb2015-09-11 13:25:51 -04001216const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001218 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001219 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001220}
1221
Jamie Madillac4e9c32017-01-13 14:07:12 -05001222const VariableLocation &Program::getUniformLocation(GLint location) const
1223{
1224 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1225 return mState.mUniformLocations[location];
1226}
1227
1228const std::vector<VariableLocation> &Program::getUniformLocations() const
1229{
1230 return mState.mUniformLocations;
1231}
1232
1233const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1234{
1235 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1236 return mState.mUniforms[index];
1237}
1238
Jamie Madill62d31cb2015-09-11 13:25:51 -04001239GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001241 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242}
1243
Jamie Madill62d31cb2015-09-11 13:25:51 -04001244GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245{
Jamie Madille7d84322017-01-10 18:21:59 -05001246 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247}
1248
1249void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1250{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001251 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1252 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253}
1254
1255void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1256{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001257 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1258 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001259}
1260
1261void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1262{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001263 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1264 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265}
1266
1267void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1268{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001269 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1270 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271}
1272
1273void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1274{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001275 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1276 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277}
1278
1279void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1280{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001281 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1282 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283}
1284
1285void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1286{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001287 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1288 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289}
1290
1291void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1292{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001293 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1294 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295}
1296
1297void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1298{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001299 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1300 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001301}
1302
1303void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1304{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001305 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1306 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307}
1308
1309void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1310{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001311 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1312 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313}
1314
1315void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1316{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001317 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1318 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
1321void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1322{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001323 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1324 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325}
1326
1327void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1328{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001329 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1330 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331}
1332
1333void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1334{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001335 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1336 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
1339void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1340{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001341 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1342 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343}
1344
1345void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1346{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001347 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1348 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349}
1350
1351void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1352{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001353 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1354 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355}
1356
1357void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1358{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001359 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1360 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361}
1362
1363void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1364{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001365 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1366 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001367}
1368
1369void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1370{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001371 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1372 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
Geoff Lange1a27752015-10-05 13:16:04 -04001375void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001377 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378}
1379
Geoff Lange1a27752015-10-05 13:16:04 -04001380void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001382 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383}
1384
Geoff Lange1a27752015-10-05 13:16:04 -04001385void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001387 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388}
1389
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001390void Program::flagForDeletion()
1391{
1392 mDeleteStatus = true;
1393}
1394
1395bool Program::isFlaggedForDeletion() const
1396{
1397 return mDeleteStatus;
1398}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001399
Brandon Jones43a53e22014-08-28 16:23:22 -07001400void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001401{
1402 mInfoLog.reset();
1403
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001405 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001406 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001407 }
1408 else
1409 {
Jamie Madillf6113162015-05-07 11:49:21 -04001410 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001411 }
1412}
1413
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1415{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001416 // Skip cache if we're using an infolog, so we get the full error.
1417 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1418 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1419 {
1420 return mCachedValidateSamplersResult.value();
1421 }
1422
1423 if (mTextureUnitTypesCache.empty())
1424 {
1425 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1426 }
1427 else
1428 {
1429 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1430 }
1431
1432 // if any two active samplers in a program are of different types, but refer to the same
1433 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1434 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001435 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001436 {
Jamie Madille7d84322017-01-10 18:21:59 -05001437 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001438
Jamie Madille7d84322017-01-10 18:21:59 -05001439 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001440 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001441 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1442 {
1443 if (infoLog)
1444 {
1445 (*infoLog) << "Sampler uniform (" << textureUnit
1446 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1447 << caps.maxCombinedTextureImageUnits << ")";
1448 }
1449
1450 mCachedValidateSamplersResult = false;
1451 return false;
1452 }
1453
1454 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1455 {
1456 if (textureType != mTextureUnitTypesCache[textureUnit])
1457 {
1458 if (infoLog)
1459 {
1460 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1461 "image unit ("
1462 << textureUnit << ").";
1463 }
1464
1465 mCachedValidateSamplersResult = false;
1466 return false;
1467 }
1468 }
1469 else
1470 {
1471 mTextureUnitTypesCache[textureUnit] = textureType;
1472 }
1473 }
1474 }
1475
1476 mCachedValidateSamplersResult = true;
1477 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478}
1479
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001480bool Program::isValidated() const
1481{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001482 return mValidated;
1483}
1484
Geoff Lange1a27752015-10-05 13:16:04 -04001485GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001487 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001488}
1489
1490void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1491{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001492 ASSERT(
1493 uniformBlockIndex <
1494 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495
Jamie Madill48ef11b2016-04-27 15:21:52 -04001496 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497
1498 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001499 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001500 std::string string = uniformBlock.name;
1501
Jamie Madill62d31cb2015-09-11 13:25:51 -04001502 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001503 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001504 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001505 }
jchen10fd7c3b52017-03-21 15:36:03 +08001506 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001507 }
1508}
1509
Geoff Lange1a27752015-10-05 13:16:04 -04001510GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001511{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001512 int maxLength = 0;
1513
1514 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001515 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001516 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1518 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001519 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520 if (!uniformBlock.name.empty())
1521 {
jchen10af713a22017-04-19 09:10:56 +08001522 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1523 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524 }
1525 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001526 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001527
1528 return maxLength;
1529}
1530
Geoff Lange1a27752015-10-05 13:16:04 -04001531GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001532{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001533 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001534 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001535
Jamie Madill48ef11b2016-04-27 15:21:52 -04001536 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1538 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001539 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001540 if (uniformBlock.name == baseName)
1541 {
1542 const bool arrayElementZero =
1543 (subscript == GL_INVALID_INDEX &&
1544 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1545 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1546 {
1547 return blockIndex;
1548 }
1549 }
1550 }
1551
1552 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001553}
1554
Jamie Madill62d31cb2015-09-11 13:25:51 -04001555const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001556{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001557 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1558 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001559}
1560
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001561void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1562{
jchen107a20b972017-06-13 14:25:26 +08001563 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001564 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001565 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001566}
1567
1568GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1569{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001570 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001571}
1572
Geoff Lang48dcae72014-02-05 16:28:24 -05001573void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1574{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001575 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001576 for (GLsizei i = 0; i < count; i++)
1577 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001578 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001579 }
1580
Jamie Madill48ef11b2016-04-27 15:21:52 -04001581 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001582}
1583
1584void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1585{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001587 {
jchen10a9042d32017-03-17 08:50:45 +08001588 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1589 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1590 std::string varName = var.nameWithArrayIndex();
1591 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001592 if (length)
1593 {
1594 *length = lastNameIdx;
1595 }
1596 if (size)
1597 {
jchen10a9042d32017-03-17 08:50:45 +08001598 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001599 }
1600 if (type)
1601 {
jchen10a9042d32017-03-17 08:50:45 +08001602 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001603 }
1604 if (name)
1605 {
jchen10a9042d32017-03-17 08:50:45 +08001606 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001607 name[lastNameIdx] = '\0';
1608 }
1609 }
1610}
1611
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001612GLsizei Program::getTransformFeedbackVaryingCount() const
1613{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001615 {
jchen10a9042d32017-03-17 08:50:45 +08001616 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001617 }
1618 else
1619 {
1620 return 0;
1621 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001622}
1623
1624GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1625{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001626 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001627 {
1628 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001629 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001630 {
jchen10a9042d32017-03-17 08:50:45 +08001631 maxSize =
1632 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001633 }
1634
1635 return maxSize;
1636 }
1637 else
1638 {
1639 return 0;
1640 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001641}
1642
1643GLenum Program::getTransformFeedbackBufferMode() const
1644{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001645 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646}
1647
Jamie Madillbd044ed2017-06-05 12:59:21 -04001648bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001649{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001650 Shader *vertexShader = mState.mAttachedVertexShader;
1651 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001652
Jamie Madillbd044ed2017-06-05 12:59:21 -04001653 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001654
Jamie Madillbd044ed2017-06-05 12:59:21 -04001655 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1656 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001657
Sami Väisänen46eaa942016-06-29 10:26:37 +03001658 std::map<GLuint, std::string> staticFragmentInputLocations;
1659
Jamie Madill4cff2472015-08-21 16:53:18 -04001660 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662 bool matched = false;
1663
1664 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001665 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 {
1667 continue;
1668 }
1669
Jamie Madill4cff2472015-08-21 16:53:18 -04001670 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001672 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001674 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001675 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001676 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677 {
1678 return false;
1679 }
1680
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 matched = true;
1682 break;
1683 }
1684 }
1685
1686 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001687 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001689 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001690 return false;
1691 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001692
1693 // Check for aliased path rendering input bindings (if any).
1694 // If more than one binding refer statically to the same
1695 // location the link must fail.
1696
1697 if (!output.staticUse)
1698 continue;
1699
1700 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1701 if (inputBinding == -1)
1702 continue;
1703
1704 const auto it = staticFragmentInputLocations.find(inputBinding);
1705 if (it == std::end(staticFragmentInputLocations))
1706 {
1707 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1708 }
1709 else
1710 {
1711 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1712 << it->second;
1713 return false;
1714 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001715 }
1716
Jamie Madillbd044ed2017-06-05 12:59:21 -04001717 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001718 {
1719 return false;
1720 }
1721
Jamie Madillada9ecc2015-08-17 12:53:37 -04001722 // TODO(jmadill): verify no unmatched vertex varyings?
1723
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 return true;
1725}
1726
Jamie Madillbd044ed2017-06-05 12:59:21 -04001727bool Program::linkUniforms(const Context *context,
1728 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001729 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001730{
Olli Etuahob78707c2017-03-09 15:03:11 +00001731 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001732 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001733 {
1734 return false;
1735 }
1736
Olli Etuahob78707c2017-03-09 15:03:11 +00001737 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001738
Olli Etuaho48fed632017-03-16 12:05:30 +00001739 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001740
1741 return true;
1742}
1743
Olli Etuaho48fed632017-03-16 12:05:30 +00001744void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001745{
Jamie Madill982f6e02017-06-07 14:33:04 -04001746 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1747 unsigned int low = high;
1748
1749 for (auto samplerIter = mState.mUniforms.rbegin();
1750 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001751 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001752 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001753 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001754
1755 mState.mSamplerUniformRange = RangeUI(low, high);
1756
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001757 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001758 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001759 {
1760 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1761 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1762 mState.mSamplerBindings.emplace_back(
1763 SamplerBinding(textureType, samplerUniform.elementCount()));
1764 }
1765}
1766
Martin Radev4c4c8e72016-08-04 12:25:34 +03001767bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1768 const std::string &uniformName,
1769 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001770 const sh::InterfaceBlockField &fragmentUniform,
1771 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001773 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1774 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1775 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 {
1777 return false;
1778 }
1779
1780 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1781 {
Jamie Madillf6113162015-05-07 11:49:21 -04001782 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001783 return false;
1784 }
1785
1786 return true;
1787}
1788
Jamie Madilleb979bf2016-11-15 12:28:46 -05001789// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001790bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001792 const ContextState &data = context->getContextState();
1793 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001794
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001796 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001797 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001798
1799 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001800 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001801 {
Jamie Madillf6113162015-05-07 11:49:21 -04001802 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001803 return false;
1804 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001806 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001807
Jamie Madillc349ec02015-08-21 16:53:12 -04001808 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001809 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001810 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001811 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001812 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001813 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001814 attribute.location = bindingLocation;
1815 }
1816
1817 if (attribute.location != -1)
1818 {
1819 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001820 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821
Jamie Madill63805b42015-08-25 13:17:39 -04001822 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 {
Jamie Madillf6113162015-05-07 11:49:21 -04001824 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001825 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826
1827 return false;
1828 }
1829
Jamie Madill63805b42015-08-25 13:17:39 -04001830 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 {
Jamie Madill63805b42015-08-25 13:17:39 -04001832 const int regLocation = attribute.location + reg;
1833 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834
1835 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001836 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001837 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001839 // TODO(jmadill): fix aliasing on ES2
1840 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001842 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001843 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 return false;
1845 }
1846 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001847 else
1848 {
Jamie Madill63805b42015-08-25 13:17:39 -04001849 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001850 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851
Jamie Madill63805b42015-08-25 13:17:39 -04001852 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853 }
1854 }
1855 }
1856
1857 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001858 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001860 // Not set by glBindAttribLocation or by location layout qualifier
1861 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001862 {
Jamie Madill63805b42015-08-25 13:17:39 -04001863 int regs = VariableRegisterCount(attribute.type);
1864 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865
Jamie Madill63805b42015-08-25 13:17:39 -04001866 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867 {
Jamie Madillf6113162015-05-07 11:49:21 -04001868 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001869 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 }
1871
Jamie Madillc349ec02015-08-21 16:53:12 -04001872 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873 }
1874 }
1875
Jamie Madill48ef11b2016-04-27 15:21:52 -04001876 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877 {
Jamie Madill63805b42015-08-25 13:17:39 -04001878 ASSERT(attribute.location != -1);
1879 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001880
Jamie Madill63805b42015-08-25 13:17:39 -04001881 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001882 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001883 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 }
1885 }
1886
Geoff Lang7dd2e102014-11-10 15:19:26 -05001887 return true;
1888}
1889
Martin Radev4c4c8e72016-08-04 12:25:34 +03001890bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1891 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1892 const std::string &errorMessage,
1893 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001894{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001895 GLuint blockCount = 0;
1896 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001898 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04001899 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001900 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04001901 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001902 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04001903 return false;
1904 }
1905 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001906 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001907 return true;
1908}
Jamie Madille473dee2015-08-18 14:49:01 -04001909
Martin Radev4c4c8e72016-08-04 12:25:34 +03001910bool Program::validateVertexAndFragmentInterfaceBlocks(
1911 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
1912 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001913 InfoLog &infoLog,
1914 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03001915{
1916 // Check that interface blocks defined in the vertex and fragment shaders are identical
1917 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
1918 UniformBlockMap linkedUniformBlocks;
1919
1920 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
1921 {
1922 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
1923 }
1924
Jamie Madille473dee2015-08-18 14:49:01 -04001925 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001926 {
Jamie Madille473dee2015-08-18 14:49:01 -04001927 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928 if (entry != linkedUniformBlocks.end())
1929 {
1930 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04001931 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
1932 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001933 {
1934 return false;
1935 }
1936 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001937 }
1938 return true;
1939}
Jamie Madille473dee2015-08-18 14:49:01 -04001940
Jamie Madillbd044ed2017-06-05 12:59:21 -04001941bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001942{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001943 const auto &caps = context->getCaps();
1944
Martin Radev4c4c8e72016-08-04 12:25:34 +03001945 if (mState.mAttachedComputeShader)
1946 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001947 Shader &computeShader = *mState.mAttachedComputeShader;
1948 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001949
1950 if (!validateUniformBlocksCount(
1951 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
1952 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
1953 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001955 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001956 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001957 return true;
1958 }
1959
Jamie Madillbd044ed2017-06-05 12:59:21 -04001960 Shader &vertexShader = *mState.mAttachedVertexShader;
1961 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001962
Jamie Madillbd044ed2017-06-05 12:59:21 -04001963 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
1964 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001965
1966 if (!validateUniformBlocksCount(
1967 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
1968 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
1969 {
1970 return false;
1971 }
1972 if (!validateUniformBlocksCount(
1973 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
1974 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
1975 infoLog))
1976 {
1977
1978 return false;
1979 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001980
1981 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001982 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001983 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001984 {
1985 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 }
Jamie Madille473dee2015-08-18 14:49:01 -04001987
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 return true;
1989}
1990
Jamie Madilla2c74982016-12-12 11:20:42 -05001991bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03001992 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001993 const sh::InterfaceBlock &fragmentInterfaceBlock,
1994 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001995{
1996 const char* blockName = vertexInterfaceBlock.name.c_str();
1997 // validate blocks for the same member types
1998 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1999 {
Jamie Madillf6113162015-05-07 11:49:21 -04002000 infoLog << "Types for interface block '" << blockName
2001 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002 return false;
2003 }
2004 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2005 {
Jamie Madillf6113162015-05-07 11:49:21 -04002006 infoLog << "Array sizes differ for interface block '" << blockName
2007 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 return false;
2009 }
jchen10af713a22017-04-19 09:10:56 +08002010 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2011 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2012 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002013 {
Jamie Madillf6113162015-05-07 11:49:21 -04002014 infoLog << "Layout qualifiers differ for interface block '" << blockName
2015 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002016 return false;
2017 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002018 const unsigned int numBlockMembers =
2019 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2021 {
2022 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2023 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2024 if (vertexMember.name != fragmentMember.name)
2025 {
Jamie Madillf6113162015-05-07 11:49:21 -04002026 infoLog << "Name mismatch for field " << blockMemberIndex
2027 << " of interface block '" << blockName
2028 << "': (in vertex: '" << vertexMember.name
2029 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 return false;
2031 }
2032 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002033 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2034 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035 {
2036 return false;
2037 }
2038 }
2039 return true;
2040}
2041
2042bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2043 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2044{
2045 if (vertexVariable.type != fragmentVariable.type)
2046 {
Jamie Madillf6113162015-05-07 11:49:21 -04002047 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048 return false;
2049 }
2050 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2051 {
Jamie Madillf6113162015-05-07 11:49:21 -04002052 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 return false;
2054 }
2055 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2056 {
Jamie Madillf6113162015-05-07 11:49:21 -04002057 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 return false;
2059 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002060 if (vertexVariable.structName != fragmentVariable.structName)
2061 {
2062 infoLog << "Structure names for " << variableName
2063 << " differ between vertex and fragment shaders";
2064 return false;
2065 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066
2067 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2068 {
Jamie Madillf6113162015-05-07 11:49:21 -04002069 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070 return false;
2071 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002072 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2074 {
2075 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2076 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2077
2078 if (vertexMember.name != fragmentMember.name)
2079 {
Jamie Madillf6113162015-05-07 11:49:21 -04002080 infoLog << "Name mismatch for field '" << memberIndex
2081 << "' of " << variableName
2082 << ": (in vertex: '" << vertexMember.name
2083 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084 return false;
2085 }
2086
2087 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2088 vertexMember.name + "'";
2089
2090 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2091 {
2092 return false;
2093 }
2094 }
2095
2096 return true;
2097}
2098
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002099bool Program::linkValidateVaryings(InfoLog &infoLog,
2100 const std::string &varyingName,
2101 const sh::Varying &vertexVarying,
2102 const sh::Varying &fragmentVarying,
2103 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104{
2105 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2106 {
2107 return false;
2108 }
2109
Jamie Madille9cc4692015-02-19 16:00:13 -05002110 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002112 infoLog << "Interpolation types for " << varyingName
2113 << " differ between vertex and fragment shaders.";
2114 return false;
2115 }
2116
2117 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2118 {
2119 infoLog << "Invariance for " << varyingName
2120 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 return false;
2122 }
2123
2124 return true;
2125}
2126
Jamie Madillbd044ed2017-06-05 12:59:21 -04002127bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002128{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002129 Shader *vertexShader = mState.mAttachedVertexShader;
2130 Shader *fragmentShader = mState.mAttachedFragmentShader;
2131 const auto &vertexVaryings = vertexShader->getVaryings(context);
2132 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2133 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002134
2135 if (shaderVersion != 100)
2136 {
2137 // Only ESSL 1.0 has restrictions on matching input and output invariance
2138 return true;
2139 }
2140
2141 bool glPositionIsInvariant = false;
2142 bool glPointSizeIsInvariant = false;
2143 bool glFragCoordIsInvariant = false;
2144 bool glPointCoordIsInvariant = false;
2145
2146 for (const sh::Varying &varying : vertexVaryings)
2147 {
2148 if (!varying.isBuiltIn())
2149 {
2150 continue;
2151 }
2152 if (varying.name.compare("gl_Position") == 0)
2153 {
2154 glPositionIsInvariant = varying.isInvariant;
2155 }
2156 else if (varying.name.compare("gl_PointSize") == 0)
2157 {
2158 glPointSizeIsInvariant = varying.isInvariant;
2159 }
2160 }
2161
2162 for (const sh::Varying &varying : fragmentVaryings)
2163 {
2164 if (!varying.isBuiltIn())
2165 {
2166 continue;
2167 }
2168 if (varying.name.compare("gl_FragCoord") == 0)
2169 {
2170 glFragCoordIsInvariant = varying.isInvariant;
2171 }
2172 else if (varying.name.compare("gl_PointCoord") == 0)
2173 {
2174 glPointCoordIsInvariant = varying.isInvariant;
2175 }
2176 }
2177
2178 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2179 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2180 // Not requiring invariance to match is supported by:
2181 // dEQP, WebGL CTS, Nexus 5X GLES
2182 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2183 {
2184 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2185 "declared invariant.";
2186 return false;
2187 }
2188 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2189 {
2190 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2191 "declared invariant.";
2192 return false;
2193 }
2194
2195 return true;
2196}
2197
jchen10a9042d32017-03-17 08:50:45 +08002198bool Program::linkValidateTransformFeedback(const gl::Context *context,
2199 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002200 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002201 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202{
2203 size_t totalComponents = 0;
2204
Jamie Madillccdf74b2015-08-18 10:46:12 -04002205 std::set<std::string> uniqueNames;
2206
Jamie Madill48ef11b2016-04-27 15:21:52 -04002207 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 {
2209 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002210 size_t subscript = GL_INVALID_INDEX;
2211 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2212
Jamie Madill192745a2016-12-22 15:58:21 -05002213 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214 {
Jamie Madill192745a2016-12-22 15:58:21 -05002215 const sh::Varying *varying = ref.second.get();
2216
jchen10a9042d32017-03-17 08:50:45 +08002217 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002219 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002220 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002221 infoLog << "Two transform feedback varyings specify the same output variable ("
2222 << tfVaryingName << ").";
2223 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 }
jchen10a9042d32017-03-17 08:50:45 +08002225 if (context->getClientVersion() >= Version(3, 1))
2226 {
2227 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2228 {
2229 infoLog
2230 << "Two transform feedback varyings include the same array element ("
2231 << tfVaryingName << ").";
2232 return false;
2233 }
2234 }
2235 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002236 {
2237 infoLog << "Capture of arrays is undefined and not supported.";
2238 return false;
2239 }
2240
jchen10a9042d32017-03-17 08:50:45 +08002241 uniqueNames.insert(tfVaryingName);
2242
Jamie Madillccdf74b2015-08-18 10:46:12 -04002243 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002244 size_t elementCount =
2245 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2246 : 1);
2247 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002248 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249 componentCount > caps.maxTransformFeedbackSeparateComponents)
2250 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002251 infoLog << "Transform feedback varying's " << varying->name << " components ("
2252 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002253 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002254 return false;
2255 }
2256
2257 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002258 found = true;
2259 break;
2260 }
2261 }
jchen10a9042d32017-03-17 08:50:45 +08002262 if (context->getClientVersion() < Version(3, 1) &&
2263 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002264 {
Geoff Lang1a683462015-09-29 15:09:59 -04002265 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002266 return false;
2267 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2269 ASSERT(found);
2270 }
2271
Jamie Madill48ef11b2016-04-27 15:21:52 -04002272 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002273 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002274 {
Jamie Madillf6113162015-05-07 11:49:21 -04002275 infoLog << "Transform feedback varying total components (" << totalComponents
2276 << ") exceed the maximum interleaved components ("
2277 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 return false;
2279 }
2280
2281 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002282}
2283
Jamie Madill192745a2016-12-22 15:58:21 -05002284void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002285{
2286 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002287 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002288 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002289 {
jchen10a9042d32017-03-17 08:50:45 +08002290 size_t subscript = GL_INVALID_INDEX;
2291 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002292 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002293 {
Jamie Madill192745a2016-12-22 15:58:21 -05002294 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002295 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002296 {
jchen10a9042d32017-03-17 08:50:45 +08002297 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2298 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002299 break;
2300 }
2301 }
2302 }
2303}
2304
Jamie Madillbd044ed2017-06-05 12:59:21 -04002305Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002306{
Jamie Madill192745a2016-12-22 15:58:21 -05002307 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002308
Jamie Madillbd044ed2017-06-05 12:59:21 -04002309 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002310 {
Jamie Madill192745a2016-12-22 15:58:21 -05002311 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002312 }
2313
Jamie Madillbd044ed2017-06-05 12:59:21 -04002314 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002315 {
Jamie Madill192745a2016-12-22 15:58:21 -05002316 merged[varying.name].fragment = &varying;
2317 }
2318
2319 return merged;
2320}
2321
2322std::vector<PackedVarying> Program::getPackedVaryings(
2323 const Program::MergedVaryings &mergedVaryings) const
2324{
2325 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2326 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002327 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002328
2329 for (const auto &ref : mergedVaryings)
2330 {
2331 const sh::Varying *input = ref.second.vertex;
2332 const sh::Varying *output = ref.second.fragment;
2333
2334 // Only pack varyings that have a matched input or output, plus special builtins.
2335 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002336 {
Jamie Madill192745a2016-12-22 15:58:21 -05002337 // Will get the vertex shader interpolation by default.
2338 auto interpolation = ref.second.get()->interpolation;
2339
2340 // Interpolation qualifiers must match.
2341 if (output->isStruct())
2342 {
2343 ASSERT(!output->isArray());
2344 for (const auto &field : output->fields)
2345 {
2346 ASSERT(!field.isStruct() && !field.isArray());
2347 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2348 }
2349 }
2350 else
2351 {
2352 packedVaryings.push_back(PackedVarying(*output, interpolation));
2353 }
2354 continue;
2355 }
2356
2357 // Keep Transform FB varyings in the merged list always.
2358 if (!input)
2359 {
2360 continue;
2361 }
2362
2363 for (const std::string &tfVarying : tfVaryings)
2364 {
jchen10a9042d32017-03-17 08:50:45 +08002365 size_t subscript = GL_INVALID_INDEX;
2366 std::string baseName = ParseResourceName(tfVarying, &subscript);
2367 if (uniqueFullNames.count(tfVarying) > 0)
2368 {
2369 continue;
2370 }
2371 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002372 {
2373 // Transform feedback for varying structs is underspecified.
2374 // See Khronos bug 9856.
2375 // TODO(jmadill): Figure out how to be spec-compliant here.
2376 if (!input->isStruct())
2377 {
2378 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2379 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002380 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2381 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002382 }
jchen10a9042d32017-03-17 08:50:45 +08002383 if (subscript == GL_INVALID_INDEX)
2384 {
2385 break;
2386 }
Jamie Madill192745a2016-12-22 15:58:21 -05002387 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002388 }
2389 }
2390
Jamie Madill192745a2016-12-22 15:58:21 -05002391 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2392
2393 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002394}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002395
Jamie Madillbd044ed2017-06-05 12:59:21 -04002396void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002397{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002398 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002399 ASSERT(fragmentShader != nullptr);
2400
Geoff Lange0cff192017-05-30 13:04:56 -04002401 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002402 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002403
2404 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002405 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002406 {
2407 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2408 outputVariable.name != "gl_FragData")
2409 {
2410 continue;
2411 }
2412
2413 unsigned int baseLocation =
2414 (outputVariable.location == -1 ? 0u
2415 : static_cast<unsigned int>(outputVariable.location));
2416 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2417 elementIndex++)
2418 {
2419 const unsigned int location = baseLocation + elementIndex;
2420 if (location >= mState.mOutputVariableTypes.size())
2421 {
2422 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2423 }
Corentin Walleze7557742017-06-01 13:09:57 -04002424 ASSERT(location < mState.mActiveOutputVariables.size());
2425 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002426 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2427 }
2428 }
2429
Jamie Madill80a6fc02015-08-21 16:53:16 -04002430 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002431 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002432 return;
2433
Jamie Madillbd044ed2017-06-05 12:59:21 -04002434 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002435 // TODO(jmadill): any caps validation here?
2436
jchen1015015f72017-03-16 13:54:21 +08002437 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002438 outputVariableIndex++)
2439 {
jchen1015015f72017-03-16 13:54:21 +08002440 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002441
2442 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2443 if (outputVariable.isBuiltIn())
2444 continue;
2445
2446 // Since multiple output locations must be specified, use 0 for non-specified locations.
2447 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2448
Jamie Madill80a6fc02015-08-21 16:53:16 -04002449 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2450 elementIndex++)
2451 {
2452 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002453 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002454 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002455 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002456 VariableLocation(outputVariable.name, element, outputVariableIndex);
2457 }
2458 }
2459}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002460
Olli Etuaho48fed632017-03-16 12:05:30 +00002461void Program::setUniformValuesFromBindingQualifiers()
2462{
Jamie Madill982f6e02017-06-07 14:33:04 -04002463 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002464 {
2465 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2466 if (samplerUniform.binding != -1)
2467 {
2468 GLint location = mState.getUniformLocation(samplerUniform.name);
2469 ASSERT(location != -1);
2470 std::vector<GLint> boundTextureUnits;
2471 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2472 ++elementIndex)
2473 {
2474 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2475 }
2476 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2477 boundTextureUnits.data());
2478 }
2479 }
2480}
2481
Jamie Madillbd044ed2017-06-05 12:59:21 -04002482void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002483{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002484 ASSERT(mState.mUniformBlocks.empty());
2485
2486 if (mState.mAttachedComputeShader)
2487 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002488 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002489
Jamie Madillbd044ed2017-06-05 12:59:21 -04002490 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002491 {
2492
2493 // Only 'packed' blocks are allowed to be considered inactive.
2494 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2495 continue;
2496
Jamie Madilla2c74982016-12-12 11:20:42 -05002497 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002498 {
2499 if (block.name == computeBlock.name)
2500 {
2501 block.computeStaticUse = computeBlock.staticUse;
2502 }
2503 }
2504
2505 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2506 }
2507 return;
2508 }
2509
Jamie Madill62d31cb2015-09-11 13:25:51 -04002510 std::set<std::string> visitedList;
2511
Jamie Madillbd044ed2017-06-05 12:59:21 -04002512 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002513
Jamie Madillbd044ed2017-06-05 12:59:21 -04002514 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002515 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002516 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002517 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2518 continue;
2519
2520 if (visitedList.count(vertexBlock.name) > 0)
2521 continue;
2522
2523 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2524 visitedList.insert(vertexBlock.name);
2525 }
2526
Jamie Madillbd044ed2017-06-05 12:59:21 -04002527 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002528
Jamie Madillbd044ed2017-06-05 12:59:21 -04002529 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002530 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002531 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002532 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2533 continue;
2534
2535 if (visitedList.count(fragmentBlock.name) > 0)
2536 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002537 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002538 {
2539 if (block.name == fragmentBlock.name)
2540 {
2541 block.fragmentStaticUse = fragmentBlock.staticUse;
2542 }
2543 }
2544
2545 continue;
2546 }
2547
2548 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2549 visitedList.insert(fragmentBlock.name);
2550 }
jchen10af713a22017-04-19 09:10:56 +08002551 // Set initial bindings from shader.
2552 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2553 {
2554 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2555 bindUniformBlock(blockIndex, uniformBlock.binding);
2556 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002557}
2558
Jamie Madill4a3c2342015-10-08 12:58:45 -04002559template <typename VarT>
2560void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2561 const std::string &prefix,
2562 int blockIndex)
2563{
2564 for (const VarT &field : fields)
2565 {
2566 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2567
2568 if (field.isStruct())
2569 {
2570 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2571 {
2572 const std::string uniformElementName =
2573 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2574 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2575 }
2576 }
2577 else
2578 {
2579 // If getBlockMemberInfo returns false, the uniform is optimized out.
2580 sh::BlockMemberInfo memberInfo;
2581 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2582 {
2583 continue;
2584 }
2585
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002586 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002587 blockIndex, memberInfo);
2588
2589 // Since block uniforms have no location, we don't need to store them in the uniform
2590 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002591 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002592 }
2593 }
2594}
2595
Jamie Madill62d31cb2015-09-11 13:25:51 -04002596void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2597{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002598 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002599 size_t blockSize = 0;
2600
Jamie Madill4a3c2342015-10-08 12:58:45 -04002601 // Track the first and last uniform index to determine the range of active uniforms in the
2602 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002603 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002604 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002605 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002606
2607 std::vector<unsigned int> blockUniformIndexes;
2608 for (size_t blockUniformIndex = firstBlockUniformIndex;
2609 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2610 {
2611 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2612 }
jchen10af713a22017-04-19 09:10:56 +08002613 // ESSL 3.10 section 4.4.4 page 58:
2614 // Any uniform or shader storage block declared without a binding qualifier is initially
2615 // assigned to block binding point zero.
2616 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002617 if (interfaceBlock.arraySize > 0)
2618 {
2619 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2620 {
jchen10af713a22017-04-19 09:10:56 +08002621 // Don't define this block at all if it's not active in the implementation.
2622 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2623 &blockSize))
2624 {
2625 continue;
2626 }
2627 UniformBlock block(interfaceBlock.name, true, arrayElement,
2628 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002629 block.memberUniformIndexes = blockUniformIndexes;
2630
Martin Radev4c4c8e72016-08-04 12:25:34 +03002631 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002632 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002633 case GL_VERTEX_SHADER:
2634 {
2635 block.vertexStaticUse = interfaceBlock.staticUse;
2636 break;
2637 }
2638 case GL_FRAGMENT_SHADER:
2639 {
2640 block.fragmentStaticUse = interfaceBlock.staticUse;
2641 break;
2642 }
2643 case GL_COMPUTE_SHADER:
2644 {
2645 block.computeStaticUse = interfaceBlock.staticUse;
2646 break;
2647 }
2648 default:
2649 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002650 }
2651
Qin Jiajia0350a642016-11-01 17:01:51 +08002652 // Since all block elements in an array share the same active uniforms, they will all be
2653 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2654 // here we will add every block element in the array.
2655 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002656 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002657 }
2658 }
2659 else
2660 {
jchen10af713a22017-04-19 09:10:56 +08002661 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2662 {
2663 return;
2664 }
2665 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002666 block.memberUniformIndexes = blockUniformIndexes;
2667
Martin Radev4c4c8e72016-08-04 12:25:34 +03002668 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002669 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002670 case GL_VERTEX_SHADER:
2671 {
2672 block.vertexStaticUse = interfaceBlock.staticUse;
2673 break;
2674 }
2675 case GL_FRAGMENT_SHADER:
2676 {
2677 block.fragmentStaticUse = interfaceBlock.staticUse;
2678 break;
2679 }
2680 case GL_COMPUTE_SHADER:
2681 {
2682 block.computeStaticUse = interfaceBlock.staticUse;
2683 break;
2684 }
2685 default:
2686 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002687 }
2688
Jamie Madill4a3c2342015-10-08 12:58:45 -04002689 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002690 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002691 }
2692}
2693
Jamie Madille7d84322017-01-10 18:21:59 -05002694template <>
2695void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2696 const uint8_t *destPointer,
2697 GLsizei clampedCount,
2698 const GLint *v)
2699{
2700 // Invalidate the validation cache only if we modify the sampler data.
2701 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2702 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2703 {
2704 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2705 std::vector<GLuint> *boundTextureUnits =
2706 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2707
2708 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2709 mCachedValidateSamplersResult.reset();
2710 }
2711}
2712
2713template <typename T>
2714void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2715 const uint8_t *destPointer,
2716 GLsizei clampedCount,
2717 const T *v)
2718{
2719}
2720
Jamie Madill62d31cb2015-09-11 13:25:51 -04002721template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002722GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002723{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002724 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2725 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002726 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2727
Corentin Wallez15ac5342016-11-03 17:06:39 -04002728 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2729 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2730 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002731 GLsizei maxElementCount =
2732 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2733
2734 GLsizei count = countIn;
2735 GLsizei clampedCount = count * vectorSize;
2736 if (clampedCount > maxElementCount)
2737 {
2738 clampedCount = maxElementCount;
2739 count = maxElementCount / vectorSize;
2740 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002741
Jamie Madill62d31cb2015-09-11 13:25:51 -04002742 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2743 {
2744 // Do a cast conversion for boolean types. From the spec:
2745 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2746 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002747 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002748 {
2749 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2750 }
2751 }
2752 else
2753 {
Jamie Madille7d84322017-01-10 18:21:59 -05002754 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002755 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002756 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002757
2758 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002759}
2760
2761template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002762GLsizei Program::setMatrixUniformInternal(GLint location,
2763 GLsizei count,
2764 GLboolean transpose,
2765 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002766{
2767 if (!transpose)
2768 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002769 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002770 }
2771
2772 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002773 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2774 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002775 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002776
2777 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2778 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2779 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2780 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2781
2782 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002783 {
2784 size_t elementOffset = element * rows * cols;
2785
2786 for (size_t row = 0; row < rows; ++row)
2787 {
2788 for (size_t col = 0; col < cols; ++col)
2789 {
2790 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2791 }
2792 }
2793 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002794
2795 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002796}
2797
2798template <typename DestT>
2799void Program::getUniformInternal(GLint location, DestT *dataOut) const
2800{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002801 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2802 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002803
2804 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2805
2806 GLenum componentType = VariableComponentType(uniform.type);
2807 if (componentType == GLTypeToGLenum<DestT>::value)
2808 {
2809 memcpy(dataOut, srcPointer, uniform.getElementSize());
2810 return;
2811 }
2812
Corentin Wallez6596c462016-03-17 17:26:58 -04002813 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002814
2815 switch (componentType)
2816 {
2817 case GL_INT:
2818 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2819 break;
2820 case GL_UNSIGNED_INT:
2821 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2822 break;
2823 case GL_BOOL:
2824 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2825 break;
2826 case GL_FLOAT:
2827 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2828 break;
2829 default:
2830 UNREACHABLE();
2831 }
2832}
Jamie Madilla4595b82017-01-11 17:36:34 -05002833
2834bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2835{
2836 // Must be called after samplers are validated.
2837 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2838
2839 for (const auto &binding : mState.mSamplerBindings)
2840 {
2841 GLenum textureType = binding.textureType;
2842 for (const auto &unit : binding.boundTextureUnits)
2843 {
2844 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2845 if (programTextureID == textureID)
2846 {
2847 // TODO(jmadill): Check for appropriate overlap.
2848 return true;
2849 }
2850 }
2851 }
2852
2853 return false;
2854}
2855
Jamie Madilla2c74982016-12-12 11:20:42 -05002856} // namespace gl