blob: af64838224a6c51f11e3b3c4301470ddf67bb085 [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
Jamie Madillffe00c02017-06-27 16:26:55 -0400852GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500853{
854 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400855 Error error = saveBinary(context, 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
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001739 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001740
1741 return true;
1742}
1743
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001744void Program::linkSamplerAndImageBindings()
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
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001749 for (auto imageIter = mState.mUniforms.rbegin();
1750 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1751 {
1752 --low;
1753 }
1754
1755 mState.mImageUniformRange = RangeUI(low, high);
1756
1757 // If uniform is a image type, insert it into the mImageBindings array.
1758 for (unsigned int imageIndex : mState.mImageUniformRange)
1759 {
1760 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v}
1761 // commands cannot load values into a uniform defined as an image,
1762 // if declare without a binding qualifier, the image variable is
1763 // initially bound to unit zero.
1764 auto &imageUniform = mState.mUniforms[imageIndex];
1765 if (imageUniform.binding == -1)
1766 {
1767 imageUniform.binding = 0;
1768 }
1769 mState.mImageBindings.emplace_back(
1770 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1771 }
1772
1773 high = low;
1774
1775 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001776 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001777 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001778 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001779 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001780
1781 mState.mSamplerUniformRange = RangeUI(low, high);
1782
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001783 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001784 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001785 {
1786 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1787 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1788 mState.mSamplerBindings.emplace_back(
1789 SamplerBinding(textureType, samplerUniform.elementCount()));
1790 }
1791}
1792
Martin Radev4c4c8e72016-08-04 12:25:34 +03001793bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1794 const std::string &uniformName,
1795 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001796 const sh::InterfaceBlockField &fragmentUniform,
1797 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001799 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1800 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1801 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 {
1803 return false;
1804 }
1805
1806 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1807 {
Jamie Madillf6113162015-05-07 11:49:21 -04001808 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809 return false;
1810 }
1811
1812 return true;
1813}
1814
Jamie Madilleb979bf2016-11-15 12:28:46 -05001815// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001816bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001818 const ContextState &data = context->getContextState();
1819 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001820
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001822 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001823 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001824
1825 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001826 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001827 {
Jamie Madillf6113162015-05-07 11:49:21 -04001828 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001829 return false;
1830 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001832 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001833
Jamie Madillc349ec02015-08-21 16:53:12 -04001834 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001835 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001836 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001837 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001838 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001839 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001840 attribute.location = bindingLocation;
1841 }
1842
1843 if (attribute.location != -1)
1844 {
1845 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001846 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847
Jamie Madill63805b42015-08-25 13:17:39 -04001848 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849 {
Jamie Madillf6113162015-05-07 11:49:21 -04001850 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001851 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852
1853 return false;
1854 }
1855
Jamie Madill63805b42015-08-25 13:17:39 -04001856 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 {
Jamie Madill63805b42015-08-25 13:17:39 -04001858 const int regLocation = attribute.location + reg;
1859 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001860
1861 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001862 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001863 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001865 // TODO(jmadill): fix aliasing on ES2
1866 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001868 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001869 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 return false;
1871 }
1872 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001873 else
1874 {
Jamie Madill63805b42015-08-25 13:17:39 -04001875 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001876 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877
Jamie Madill63805b42015-08-25 13:17:39 -04001878 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879 }
1880 }
1881 }
1882
1883 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001884 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001886 // Not set by glBindAttribLocation or by location layout qualifier
1887 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 {
Jamie Madill63805b42015-08-25 13:17:39 -04001889 int regs = VariableRegisterCount(attribute.type);
1890 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891
Jamie Madill63805b42015-08-25 13:17:39 -04001892 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 {
Jamie Madillf6113162015-05-07 11:49:21 -04001894 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001895 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896 }
1897
Jamie Madillc349ec02015-08-21 16:53:12 -04001898 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001899 }
1900 }
1901
Jamie Madill48ef11b2016-04-27 15:21:52 -04001902 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001903 {
Jamie Madill63805b42015-08-25 13:17:39 -04001904 ASSERT(attribute.location != -1);
1905 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001906
Jamie Madill63805b42015-08-25 13:17:39 -04001907 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001909 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001910 }
1911 }
1912
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 return true;
1914}
1915
Martin Radev4c4c8e72016-08-04 12:25:34 +03001916bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1917 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1918 const std::string &errorMessage,
1919 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001920{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001921 GLuint blockCount = 0;
1922 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001924 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04001925 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001926 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04001927 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001928 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04001929 return false;
1930 }
1931 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001933 return true;
1934}
Jamie Madille473dee2015-08-18 14:49:01 -04001935
Martin Radev4c4c8e72016-08-04 12:25:34 +03001936bool Program::validateVertexAndFragmentInterfaceBlocks(
1937 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
1938 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001939 InfoLog &infoLog,
1940 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03001941{
1942 // Check that interface blocks defined in the vertex and fragment shaders are identical
1943 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
1944 UniformBlockMap linkedUniformBlocks;
1945
1946 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
1947 {
1948 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
1949 }
1950
Jamie Madille473dee2015-08-18 14:49:01 -04001951 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001952 {
Jamie Madille473dee2015-08-18 14:49:01 -04001953 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 if (entry != linkedUniformBlocks.end())
1955 {
1956 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04001957 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
1958 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 {
1960 return false;
1961 }
1962 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001963 }
1964 return true;
1965}
Jamie Madille473dee2015-08-18 14:49:01 -04001966
Jamie Madillbd044ed2017-06-05 12:59:21 -04001967bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001968{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001969 const auto &caps = context->getCaps();
1970
Martin Radev4c4c8e72016-08-04 12:25:34 +03001971 if (mState.mAttachedComputeShader)
1972 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001973 Shader &computeShader = *mState.mAttachedComputeShader;
1974 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001975
1976 if (!validateUniformBlocksCount(
1977 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
1978 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
1979 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001981 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001982 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001983 return true;
1984 }
1985
Jamie Madillbd044ed2017-06-05 12:59:21 -04001986 Shader &vertexShader = *mState.mAttachedVertexShader;
1987 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001988
Jamie Madillbd044ed2017-06-05 12:59:21 -04001989 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
1990 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001991
1992 if (!validateUniformBlocksCount(
1993 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
1994 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
1995 {
1996 return false;
1997 }
1998 if (!validateUniformBlocksCount(
1999 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2000 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2001 infoLog))
2002 {
2003
2004 return false;
2005 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002006
2007 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002008 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002009 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002010 {
2011 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012 }
Jamie Madille473dee2015-08-18 14:49:01 -04002013
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 return true;
2015}
2016
Jamie Madilla2c74982016-12-12 11:20:42 -05002017bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002018 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002019 const sh::InterfaceBlock &fragmentInterfaceBlock,
2020 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021{
2022 const char* blockName = vertexInterfaceBlock.name.c_str();
2023 // validate blocks for the same member types
2024 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2025 {
Jamie Madillf6113162015-05-07 11:49:21 -04002026 infoLog << "Types for interface block '" << blockName
2027 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028 return false;
2029 }
2030 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2031 {
Jamie Madillf6113162015-05-07 11:49:21 -04002032 infoLog << "Array sizes differ for interface block '" << blockName
2033 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034 return false;
2035 }
jchen10af713a22017-04-19 09:10:56 +08002036 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2037 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2038 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 {
Jamie Madillf6113162015-05-07 11:49:21 -04002040 infoLog << "Layout qualifiers differ for interface block '" << blockName
2041 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 return false;
2043 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002044 const unsigned int numBlockMembers =
2045 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2047 {
2048 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2049 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2050 if (vertexMember.name != fragmentMember.name)
2051 {
Jamie Madillf6113162015-05-07 11:49:21 -04002052 infoLog << "Name mismatch for field " << blockMemberIndex
2053 << " of interface block '" << blockName
2054 << "': (in vertex: '" << vertexMember.name
2055 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056 return false;
2057 }
2058 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002059 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2060 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 {
2062 return false;
2063 }
2064 }
2065 return true;
2066}
2067
2068bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2069 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2070{
2071 if (vertexVariable.type != fragmentVariable.type)
2072 {
Jamie Madillf6113162015-05-07 11:49:21 -04002073 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 return false;
2075 }
2076 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2077 {
Jamie Madillf6113162015-05-07 11:49:21 -04002078 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 return false;
2080 }
2081 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2082 {
Jamie Madillf6113162015-05-07 11:49:21 -04002083 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084 return false;
2085 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002086 if (vertexVariable.structName != fragmentVariable.structName)
2087 {
2088 infoLog << "Structure names for " << variableName
2089 << " differ between vertex and fragment shaders";
2090 return false;
2091 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092
2093 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2094 {
Jamie Madillf6113162015-05-07 11:49:21 -04002095 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 return false;
2097 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002098 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2100 {
2101 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2102 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2103
2104 if (vertexMember.name != fragmentMember.name)
2105 {
Jamie Madillf6113162015-05-07 11:49:21 -04002106 infoLog << "Name mismatch for field '" << memberIndex
2107 << "' of " << variableName
2108 << ": (in vertex: '" << vertexMember.name
2109 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 return false;
2111 }
2112
2113 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2114 vertexMember.name + "'";
2115
2116 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2117 {
2118 return false;
2119 }
2120 }
2121
2122 return true;
2123}
2124
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002125bool Program::linkValidateVaryings(InfoLog &infoLog,
2126 const std::string &varyingName,
2127 const sh::Varying &vertexVarying,
2128 const sh::Varying &fragmentVarying,
2129 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130{
2131 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2132 {
2133 return false;
2134 }
2135
Jamie Madille9cc4692015-02-19 16:00:13 -05002136 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002138 infoLog << "Interpolation types for " << varyingName
2139 << " differ between vertex and fragment shaders.";
2140 return false;
2141 }
2142
2143 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2144 {
2145 infoLog << "Invariance for " << varyingName
2146 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147 return false;
2148 }
2149
2150 return true;
2151}
2152
Jamie Madillbd044ed2017-06-05 12:59:21 -04002153bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002154{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002155 Shader *vertexShader = mState.mAttachedVertexShader;
2156 Shader *fragmentShader = mState.mAttachedFragmentShader;
2157 const auto &vertexVaryings = vertexShader->getVaryings(context);
2158 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2159 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002160
2161 if (shaderVersion != 100)
2162 {
2163 // Only ESSL 1.0 has restrictions on matching input and output invariance
2164 return true;
2165 }
2166
2167 bool glPositionIsInvariant = false;
2168 bool glPointSizeIsInvariant = false;
2169 bool glFragCoordIsInvariant = false;
2170 bool glPointCoordIsInvariant = false;
2171
2172 for (const sh::Varying &varying : vertexVaryings)
2173 {
2174 if (!varying.isBuiltIn())
2175 {
2176 continue;
2177 }
2178 if (varying.name.compare("gl_Position") == 0)
2179 {
2180 glPositionIsInvariant = varying.isInvariant;
2181 }
2182 else if (varying.name.compare("gl_PointSize") == 0)
2183 {
2184 glPointSizeIsInvariant = varying.isInvariant;
2185 }
2186 }
2187
2188 for (const sh::Varying &varying : fragmentVaryings)
2189 {
2190 if (!varying.isBuiltIn())
2191 {
2192 continue;
2193 }
2194 if (varying.name.compare("gl_FragCoord") == 0)
2195 {
2196 glFragCoordIsInvariant = varying.isInvariant;
2197 }
2198 else if (varying.name.compare("gl_PointCoord") == 0)
2199 {
2200 glPointCoordIsInvariant = varying.isInvariant;
2201 }
2202 }
2203
2204 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2205 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2206 // Not requiring invariance to match is supported by:
2207 // dEQP, WebGL CTS, Nexus 5X GLES
2208 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2209 {
2210 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2211 "declared invariant.";
2212 return false;
2213 }
2214 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2215 {
2216 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2217 "declared invariant.";
2218 return false;
2219 }
2220
2221 return true;
2222}
2223
jchen10a9042d32017-03-17 08:50:45 +08002224bool Program::linkValidateTransformFeedback(const gl::Context *context,
2225 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002226 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002227 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228{
2229 size_t totalComponents = 0;
2230
Jamie Madillccdf74b2015-08-18 10:46:12 -04002231 std::set<std::string> uniqueNames;
2232
Jamie Madill48ef11b2016-04-27 15:21:52 -04002233 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234 {
2235 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002236 size_t subscript = GL_INVALID_INDEX;
2237 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2238
Jamie Madill192745a2016-12-22 15:58:21 -05002239 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002240 {
Jamie Madill192745a2016-12-22 15:58:21 -05002241 const sh::Varying *varying = ref.second.get();
2242
jchen10a9042d32017-03-17 08:50:45 +08002243 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002244 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002245 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002246 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002247 infoLog << "Two transform feedback varyings specify the same output variable ("
2248 << tfVaryingName << ").";
2249 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002250 }
jchen10a9042d32017-03-17 08:50:45 +08002251 if (context->getClientVersion() >= Version(3, 1))
2252 {
2253 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2254 {
2255 infoLog
2256 << "Two transform feedback varyings include the same array element ("
2257 << tfVaryingName << ").";
2258 return false;
2259 }
2260 }
2261 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002262 {
2263 infoLog << "Capture of arrays is undefined and not supported.";
2264 return false;
2265 }
2266
jchen10a9042d32017-03-17 08:50:45 +08002267 uniqueNames.insert(tfVaryingName);
2268
Jamie Madillccdf74b2015-08-18 10:46:12 -04002269 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002270 size_t elementCount =
2271 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2272 : 1);
2273 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002274 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002275 componentCount > caps.maxTransformFeedbackSeparateComponents)
2276 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002277 infoLog << "Transform feedback varying's " << varying->name << " components ("
2278 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002279 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280 return false;
2281 }
2282
2283 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002284 found = true;
2285 break;
2286 }
2287 }
jchen10a9042d32017-03-17 08:50:45 +08002288 if (context->getClientVersion() < Version(3, 1) &&
2289 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002290 {
Geoff Lang1a683462015-09-29 15:09:59 -04002291 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002292 return false;
2293 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2295 ASSERT(found);
2296 }
2297
Jamie Madill48ef11b2016-04-27 15:21:52 -04002298 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002299 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002300 {
Jamie Madillf6113162015-05-07 11:49:21 -04002301 infoLog << "Transform feedback varying total components (" << totalComponents
2302 << ") exceed the maximum interleaved components ("
2303 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304 return false;
2305 }
2306
2307 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002308}
2309
Jamie Madill192745a2016-12-22 15:58:21 -05002310void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002311{
2312 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002313 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002314 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002315 {
jchen10a9042d32017-03-17 08:50:45 +08002316 size_t subscript = GL_INVALID_INDEX;
2317 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002318 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002319 {
Jamie Madill192745a2016-12-22 15:58:21 -05002320 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002321 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002322 {
jchen10a9042d32017-03-17 08:50:45 +08002323 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2324 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002325 break;
2326 }
2327 }
2328 }
2329}
2330
Jamie Madillbd044ed2017-06-05 12:59:21 -04002331Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002332{
Jamie Madill192745a2016-12-22 15:58:21 -05002333 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002334
Jamie Madillbd044ed2017-06-05 12:59:21 -04002335 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002336 {
Jamie Madill192745a2016-12-22 15:58:21 -05002337 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002338 }
2339
Jamie Madillbd044ed2017-06-05 12:59:21 -04002340 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002341 {
Jamie Madill192745a2016-12-22 15:58:21 -05002342 merged[varying.name].fragment = &varying;
2343 }
2344
2345 return merged;
2346}
2347
2348std::vector<PackedVarying> Program::getPackedVaryings(
2349 const Program::MergedVaryings &mergedVaryings) const
2350{
2351 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2352 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002353 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002354
2355 for (const auto &ref : mergedVaryings)
2356 {
2357 const sh::Varying *input = ref.second.vertex;
2358 const sh::Varying *output = ref.second.fragment;
2359
2360 // Only pack varyings that have a matched input or output, plus special builtins.
2361 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002362 {
Jamie Madill192745a2016-12-22 15:58:21 -05002363 // Will get the vertex shader interpolation by default.
2364 auto interpolation = ref.second.get()->interpolation;
2365
2366 // Interpolation qualifiers must match.
2367 if (output->isStruct())
2368 {
2369 ASSERT(!output->isArray());
2370 for (const auto &field : output->fields)
2371 {
2372 ASSERT(!field.isStruct() && !field.isArray());
2373 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2374 }
2375 }
2376 else
2377 {
2378 packedVaryings.push_back(PackedVarying(*output, interpolation));
2379 }
2380 continue;
2381 }
2382
2383 // Keep Transform FB varyings in the merged list always.
2384 if (!input)
2385 {
2386 continue;
2387 }
2388
2389 for (const std::string &tfVarying : tfVaryings)
2390 {
jchen10a9042d32017-03-17 08:50:45 +08002391 size_t subscript = GL_INVALID_INDEX;
2392 std::string baseName = ParseResourceName(tfVarying, &subscript);
2393 if (uniqueFullNames.count(tfVarying) > 0)
2394 {
2395 continue;
2396 }
2397 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002398 {
2399 // Transform feedback for varying structs is underspecified.
2400 // See Khronos bug 9856.
2401 // TODO(jmadill): Figure out how to be spec-compliant here.
2402 if (!input->isStruct())
2403 {
2404 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2405 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002406 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2407 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002408 }
jchen10a9042d32017-03-17 08:50:45 +08002409 if (subscript == GL_INVALID_INDEX)
2410 {
2411 break;
2412 }
Jamie Madill192745a2016-12-22 15:58:21 -05002413 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002414 }
2415 }
2416
Jamie Madill192745a2016-12-22 15:58:21 -05002417 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2418
2419 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002420}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002421
Jamie Madillbd044ed2017-06-05 12:59:21 -04002422void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002423{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002424 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002425 ASSERT(fragmentShader != nullptr);
2426
Geoff Lange0cff192017-05-30 13:04:56 -04002427 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002428 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002429
2430 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002431 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002432 {
2433 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2434 outputVariable.name != "gl_FragData")
2435 {
2436 continue;
2437 }
2438
2439 unsigned int baseLocation =
2440 (outputVariable.location == -1 ? 0u
2441 : static_cast<unsigned int>(outputVariable.location));
2442 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2443 elementIndex++)
2444 {
2445 const unsigned int location = baseLocation + elementIndex;
2446 if (location >= mState.mOutputVariableTypes.size())
2447 {
2448 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2449 }
Corentin Walleze7557742017-06-01 13:09:57 -04002450 ASSERT(location < mState.mActiveOutputVariables.size());
2451 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002452 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2453 }
2454 }
2455
Jamie Madill80a6fc02015-08-21 16:53:16 -04002456 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002457 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002458 return;
2459
Jamie Madillbd044ed2017-06-05 12:59:21 -04002460 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002461 // TODO(jmadill): any caps validation here?
2462
jchen1015015f72017-03-16 13:54:21 +08002463 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002464 outputVariableIndex++)
2465 {
jchen1015015f72017-03-16 13:54:21 +08002466 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002467
2468 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2469 if (outputVariable.isBuiltIn())
2470 continue;
2471
2472 // Since multiple output locations must be specified, use 0 for non-specified locations.
2473 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2474
Jamie Madill80a6fc02015-08-21 16:53:16 -04002475 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2476 elementIndex++)
2477 {
2478 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002479 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002480 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002481 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002482 VariableLocation(outputVariable.name, element, outputVariableIndex);
2483 }
2484 }
2485}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002486
Olli Etuaho48fed632017-03-16 12:05:30 +00002487void Program::setUniformValuesFromBindingQualifiers()
2488{
Jamie Madill982f6e02017-06-07 14:33:04 -04002489 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002490 {
2491 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2492 if (samplerUniform.binding != -1)
2493 {
2494 GLint location = mState.getUniformLocation(samplerUniform.name);
2495 ASSERT(location != -1);
2496 std::vector<GLint> boundTextureUnits;
2497 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2498 ++elementIndex)
2499 {
2500 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2501 }
2502 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2503 boundTextureUnits.data());
2504 }
2505 }
2506}
2507
Jamie Madillbd044ed2017-06-05 12:59:21 -04002508void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002509{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002510 ASSERT(mState.mUniformBlocks.empty());
2511
2512 if (mState.mAttachedComputeShader)
2513 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002514 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002515
Jamie Madillbd044ed2017-06-05 12:59:21 -04002516 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002517 {
2518
2519 // Only 'packed' blocks are allowed to be considered inactive.
2520 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2521 continue;
2522
Jamie Madilla2c74982016-12-12 11:20:42 -05002523 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002524 {
2525 if (block.name == computeBlock.name)
2526 {
2527 block.computeStaticUse = computeBlock.staticUse;
2528 }
2529 }
2530
2531 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2532 }
2533 return;
2534 }
2535
Jamie Madill62d31cb2015-09-11 13:25:51 -04002536 std::set<std::string> visitedList;
2537
Jamie Madillbd044ed2017-06-05 12:59:21 -04002538 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002539
Jamie Madillbd044ed2017-06-05 12:59:21 -04002540 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002541 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002542 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002543 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2544 continue;
2545
2546 if (visitedList.count(vertexBlock.name) > 0)
2547 continue;
2548
2549 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2550 visitedList.insert(vertexBlock.name);
2551 }
2552
Jamie Madillbd044ed2017-06-05 12:59:21 -04002553 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002554
Jamie Madillbd044ed2017-06-05 12:59:21 -04002555 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002556 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002557 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002558 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2559 continue;
2560
2561 if (visitedList.count(fragmentBlock.name) > 0)
2562 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002563 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002564 {
2565 if (block.name == fragmentBlock.name)
2566 {
2567 block.fragmentStaticUse = fragmentBlock.staticUse;
2568 }
2569 }
2570
2571 continue;
2572 }
2573
2574 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2575 visitedList.insert(fragmentBlock.name);
2576 }
jchen10af713a22017-04-19 09:10:56 +08002577 // Set initial bindings from shader.
2578 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2579 {
2580 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2581 bindUniformBlock(blockIndex, uniformBlock.binding);
2582 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002583}
2584
Jamie Madill4a3c2342015-10-08 12:58:45 -04002585template <typename VarT>
2586void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2587 const std::string &prefix,
2588 int blockIndex)
2589{
2590 for (const VarT &field : fields)
2591 {
2592 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2593
2594 if (field.isStruct())
2595 {
2596 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2597 {
2598 const std::string uniformElementName =
2599 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2600 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2601 }
2602 }
2603 else
2604 {
2605 // If getBlockMemberInfo returns false, the uniform is optimized out.
2606 sh::BlockMemberInfo memberInfo;
2607 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2608 {
2609 continue;
2610 }
2611
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002612 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002613 blockIndex, memberInfo);
2614
2615 // Since block uniforms have no location, we don't need to store them in the uniform
2616 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002617 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002618 }
2619 }
2620}
2621
Jamie Madill62d31cb2015-09-11 13:25:51 -04002622void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2623{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002624 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002625 size_t blockSize = 0;
2626
Jamie Madill4a3c2342015-10-08 12:58:45 -04002627 // Track the first and last uniform index to determine the range of active uniforms in the
2628 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002629 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002630 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002631 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002632
2633 std::vector<unsigned int> blockUniformIndexes;
2634 for (size_t blockUniformIndex = firstBlockUniformIndex;
2635 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2636 {
2637 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2638 }
jchen10af713a22017-04-19 09:10:56 +08002639 // ESSL 3.10 section 4.4.4 page 58:
2640 // Any uniform or shader storage block declared without a binding qualifier is initially
2641 // assigned to block binding point zero.
2642 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002643 if (interfaceBlock.arraySize > 0)
2644 {
2645 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2646 {
jchen10af713a22017-04-19 09:10:56 +08002647 // Don't define this block at all if it's not active in the implementation.
2648 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2649 &blockSize))
2650 {
2651 continue;
2652 }
2653 UniformBlock block(interfaceBlock.name, true, arrayElement,
2654 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002655 block.memberUniformIndexes = blockUniformIndexes;
2656
Martin Radev4c4c8e72016-08-04 12:25:34 +03002657 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002658 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002659 case GL_VERTEX_SHADER:
2660 {
2661 block.vertexStaticUse = interfaceBlock.staticUse;
2662 break;
2663 }
2664 case GL_FRAGMENT_SHADER:
2665 {
2666 block.fragmentStaticUse = interfaceBlock.staticUse;
2667 break;
2668 }
2669 case GL_COMPUTE_SHADER:
2670 {
2671 block.computeStaticUse = interfaceBlock.staticUse;
2672 break;
2673 }
2674 default:
2675 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002676 }
2677
Qin Jiajia0350a642016-11-01 17:01:51 +08002678 // Since all block elements in an array share the same active uniforms, they will all be
2679 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2680 // here we will add every block element in the array.
2681 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002682 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002683 }
2684 }
2685 else
2686 {
jchen10af713a22017-04-19 09:10:56 +08002687 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2688 {
2689 return;
2690 }
2691 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002692 block.memberUniformIndexes = blockUniformIndexes;
2693
Martin Radev4c4c8e72016-08-04 12:25:34 +03002694 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002695 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002696 case GL_VERTEX_SHADER:
2697 {
2698 block.vertexStaticUse = interfaceBlock.staticUse;
2699 break;
2700 }
2701 case GL_FRAGMENT_SHADER:
2702 {
2703 block.fragmentStaticUse = interfaceBlock.staticUse;
2704 break;
2705 }
2706 case GL_COMPUTE_SHADER:
2707 {
2708 block.computeStaticUse = interfaceBlock.staticUse;
2709 break;
2710 }
2711 default:
2712 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002713 }
2714
Jamie Madill4a3c2342015-10-08 12:58:45 -04002715 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002716 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002717 }
2718}
2719
Jamie Madille7d84322017-01-10 18:21:59 -05002720template <>
2721void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2722 const uint8_t *destPointer,
2723 GLsizei clampedCount,
2724 const GLint *v)
2725{
2726 // Invalidate the validation cache only if we modify the sampler data.
2727 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2728 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2729 {
2730 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2731 std::vector<GLuint> *boundTextureUnits =
2732 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2733
2734 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2735 mCachedValidateSamplersResult.reset();
2736 }
2737}
2738
2739template <typename T>
2740void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2741 const uint8_t *destPointer,
2742 GLsizei clampedCount,
2743 const T *v)
2744{
2745}
2746
Jamie Madill62d31cb2015-09-11 13:25:51 -04002747template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002748GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002749{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002750 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2751 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002752 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2753
Corentin Wallez15ac5342016-11-03 17:06:39 -04002754 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2755 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2756 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002757 GLsizei maxElementCount =
2758 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2759
2760 GLsizei count = countIn;
2761 GLsizei clampedCount = count * vectorSize;
2762 if (clampedCount > maxElementCount)
2763 {
2764 clampedCount = maxElementCount;
2765 count = maxElementCount / vectorSize;
2766 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002767
Jamie Madill62d31cb2015-09-11 13:25:51 -04002768 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2769 {
2770 // Do a cast conversion for boolean types. From the spec:
2771 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2772 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002773 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002774 {
2775 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2776 }
2777 }
2778 else
2779 {
Jamie Madille7d84322017-01-10 18:21:59 -05002780 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002781 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002782 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002783
2784 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002785}
2786
2787template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002788GLsizei Program::setMatrixUniformInternal(GLint location,
2789 GLsizei count,
2790 GLboolean transpose,
2791 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002792{
2793 if (!transpose)
2794 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002795 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002796 }
2797
2798 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002799 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2800 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002801 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002802
2803 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2804 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2805 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2806 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2807
2808 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002809 {
2810 size_t elementOffset = element * rows * cols;
2811
2812 for (size_t row = 0; row < rows; ++row)
2813 {
2814 for (size_t col = 0; col < cols; ++col)
2815 {
2816 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2817 }
2818 }
2819 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002820
2821 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822}
2823
2824template <typename DestT>
2825void Program::getUniformInternal(GLint location, DestT *dataOut) const
2826{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002827 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2828 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829
2830 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2831
2832 GLenum componentType = VariableComponentType(uniform.type);
2833 if (componentType == GLTypeToGLenum<DestT>::value)
2834 {
2835 memcpy(dataOut, srcPointer, uniform.getElementSize());
2836 return;
2837 }
2838
Corentin Wallez6596c462016-03-17 17:26:58 -04002839 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002840
2841 switch (componentType)
2842 {
2843 case GL_INT:
2844 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2845 break;
2846 case GL_UNSIGNED_INT:
2847 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2848 break;
2849 case GL_BOOL:
2850 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2851 break;
2852 case GL_FLOAT:
2853 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2854 break;
2855 default:
2856 UNREACHABLE();
2857 }
2858}
Jamie Madilla4595b82017-01-11 17:36:34 -05002859
2860bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2861{
2862 // Must be called after samplers are validated.
2863 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2864
2865 for (const auto &binding : mState.mSamplerBindings)
2866 {
2867 GLenum textureType = binding.textureType;
2868 for (const auto &unit : binding.boundTextureUnits)
2869 {
2870 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2871 if (programTextureID == textureID)
2872 {
2873 // TODO(jmadill): Check for appropriate overlap.
2874 return true;
2875 }
2876 }
2877 }
2878
2879 return false;
2880}
2881
Jamie Madilla2c74982016-12-12 11:20:42 -05002882} // namespace gl