blob: df2137bf3b177d89f6f0b58696e0bde527681092 [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
409 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500410 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411}
412
413Program::~Program()
414{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400415 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416}
417
Jamie Madill4928b7c2017-06-20 12:57:39 -0400418void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500419{
420 if (mState.mAttachedVertexShader != nullptr)
421 {
422 mState.mAttachedVertexShader->release(context);
423 mState.mAttachedVertexShader = nullptr;
424 }
425
426 if (mState.mAttachedFragmentShader != nullptr)
427 {
428 mState.mAttachedFragmentShader->release(context);
429 mState.mAttachedFragmentShader = nullptr;
430 }
431
432 if (mState.mAttachedComputeShader != nullptr)
433 {
434 mState.mAttachedComputeShader->release(context);
435 mState.mAttachedComputeShader = nullptr;
436 }
437
Jamie Madillc564c072017-06-01 12:45:42 -0400438 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400439
440 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
441 !mState.mAttachedComputeShader);
442 SafeDelete(mProgram);
443
444 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500445}
446
Geoff Lang70d0f492015-12-10 17:45:46 -0500447void Program::setLabel(const std::string &label)
448{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400449 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500450}
451
452const std::string &Program::getLabel() const
453{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400454 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500455}
456
Jamie Madillef300b12016-10-07 15:12:09 -0400457void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300459 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300461 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462 {
Jamie Madillef300b12016-10-07 15:12:09 -0400463 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300464 mState.mAttachedVertexShader = shader;
465 mState.mAttachedVertexShader->addRef();
466 break;
467 }
468 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469 {
Jamie Madillef300b12016-10-07 15:12:09 -0400470 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300471 mState.mAttachedFragmentShader = shader;
472 mState.mAttachedFragmentShader->addRef();
473 break;
474 }
475 case GL_COMPUTE_SHADER:
476 {
Jamie Madillef300b12016-10-07 15:12:09 -0400477 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300478 mState.mAttachedComputeShader = shader;
479 mState.mAttachedComputeShader->addRef();
480 break;
481 }
482 default:
483 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485}
486
Jamie Madillc1d770e2017-04-13 17:31:24 -0400487void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300489 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300491 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400493 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500494 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300495 mState.mAttachedVertexShader = nullptr;
496 break;
497 }
498 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400500 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500501 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300502 mState.mAttachedFragmentShader = nullptr;
503 break;
504 }
505 case GL_COMPUTE_SHADER:
506 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400507 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500508 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300509 mState.mAttachedComputeShader = nullptr;
510 break;
511 }
512 default:
513 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515}
516
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000517int Program::getAttachedShadersCount() const
518{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300519 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
520 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000521}
522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523void Program::bindAttributeLocation(GLuint index, const char *name)
524{
Geoff Langd8605522016-04-13 10:19:12 -0400525 mAttributeBindings.bindLocation(index, name);
526}
527
528void Program::bindUniformLocation(GLuint index, const char *name)
529{
530 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800531 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532}
533
Sami Väisänen46eaa942016-06-29 10:26:37 +0300534void Program::bindFragmentInputLocation(GLint index, const char *name)
535{
536 mFragmentInputBindings.bindLocation(index, name);
537}
538
Jamie Madillbd044ed2017-06-05 12:59:21 -0400539BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300540{
541 BindingInfo ret;
542 ret.type = GL_NONE;
543 ret.valid = false;
544
Jamie Madillbd044ed2017-06-05 12:59:21 -0400545 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300546 ASSERT(fragmentShader);
547
548 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400549 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300550
551 for (const auto &binding : mFragmentInputBindings)
552 {
553 if (binding.second != static_cast<GLuint>(index))
554 continue;
555
556 ret.valid = true;
557
558 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400559 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300560
561 for (const auto &in : inputs)
562 {
563 if (in.name == originalName)
564 {
565 if (in.isArray())
566 {
567 // The client wants to bind either "name" or "name[0]".
568 // GL ES 3.1 spec refers to active array names with language such as:
569 // "if the string identifies the base name of an active array, where the
570 // string would exactly match the name of the variable if the suffix "[0]"
571 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400572 if (arrayIndex == GL_INVALID_INDEX)
573 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300574
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400575 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300576 }
577 else
578 {
579 ret.name = in.mappedName;
580 }
581 ret.type = in.type;
582 return ret;
583 }
584 }
585 }
586
587 return ret;
588}
589
Jamie Madillbd044ed2017-06-05 12:59:21 -0400590void Program::pathFragmentInputGen(const Context *context,
591 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300592 GLenum genMode,
593 GLint components,
594 const GLfloat *coeffs)
595{
596 // If the location is -1 then the command is silently ignored
597 if (index == -1)
598 return;
599
Jamie Madillbd044ed2017-06-05 12:59:21 -0400600 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300601
602 // If the input doesn't exist then then the command is silently ignored
603 // This could happen through optimization for example, the shader translator
604 // decides that a variable is not actually being used and optimizes it away.
605 if (binding.name.empty())
606 return;
607
608 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
609}
610
Martin Radev4c4c8e72016-08-04 12:25:34 +0300611// The attached shaders are checked for linking errors by matching up their variables.
612// Uniform, input and output variables get collected.
613// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500614Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000615{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500616 const auto &data = context->getContextState();
617
Jamie Madill6c1f6712017-02-14 19:08:04 -0500618 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000619
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000620 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000621 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000622
Martin Radev4c4c8e72016-08-04 12:25:34 +0300623 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500624
Jamie Madill192745a2016-12-22 15:58:21 -0500625 auto vertexShader = mState.mAttachedVertexShader;
626 auto fragmentShader = mState.mAttachedFragmentShader;
627 auto computeShader = mState.mAttachedComputeShader;
628
629 bool isComputeShaderAttached = (computeShader != nullptr);
630 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300631 // Check whether we both have a compute and non-compute shaders attached.
632 // If there are of both types attached, then linking should fail.
633 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
634 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500635 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300636 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
637 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400638 }
639
Jamie Madill192745a2016-12-22 15:58:21 -0500640 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500641 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400642 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300643 {
644 mInfoLog << "Attached compute shader is not compiled.";
645 return NoError();
646 }
Jamie Madill192745a2016-12-22 15:58:21 -0500647 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648
Jamie Madillbd044ed2017-06-05 12:59:21 -0400649 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300650
651 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
652 // If the work group size is not specified, a link time error should occur.
653 if (!mState.mComputeShaderLocalSize.isDeclared())
654 {
655 mInfoLog << "Work group size is not specified.";
656 return NoError();
657 }
658
Jamie Madillbd044ed2017-06-05 12:59:21 -0400659 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660 {
661 return NoError();
662 }
663
Jamie Madillbd044ed2017-06-05 12:59:21 -0400664 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665 {
666 return NoError();
667 }
668
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500669 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400670 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500671 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300672 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500673 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300674 }
675 }
676 else
677 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400678 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679 {
680 return NoError();
681 }
Jamie Madill192745a2016-12-22 15:58:21 -0500682 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300683
Jamie Madillbd044ed2017-06-05 12:59:21 -0400684 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300685 {
686 return NoError();
687 }
Jamie Madill192745a2016-12-22 15:58:21 -0500688 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689
Jamie Madillbd044ed2017-06-05 12:59:21 -0400690 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300691 {
692 mInfoLog << "Fragment shader version does not match vertex shader version.";
693 return NoError();
694 }
695
Jamie Madillbd044ed2017-06-05 12:59:21 -0400696 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300697 {
698 return NoError();
699 }
700
Jamie Madillbd044ed2017-06-05 12:59:21 -0400701 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300702 {
703 return NoError();
704 }
705
Jamie Madillbd044ed2017-06-05 12:59:21 -0400706 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300707 {
708 return NoError();
709 }
710
Jamie Madillbd044ed2017-06-05 12:59:21 -0400711 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300712 {
713 return NoError();
714 }
715
Jamie Madillbd044ed2017-06-05 12:59:21 -0400716 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717
jchen10a9042d32017-03-17 08:50:45 +0800718 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300719 {
720 return NoError();
721 }
722
Jamie Madillbd044ed2017-06-05 12:59:21 -0400723 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724
Jamie Madill192745a2016-12-22 15:58:21 -0500725 // Validate we can pack the varyings.
726 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
727
728 // Map the varyings to the register file
729 // In WebGL, we use a slightly different handling for packing variables.
730 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
731 : PackMode::ANGLE_RELAXED;
732 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
733 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
734 mState.getTransformFeedbackVaryingNames()))
735 {
736 return NoError();
737 }
738
Jamie Madillc564c072017-06-01 12:45:42 -0400739 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500740 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300741 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500742 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300743 }
744
745 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500746 }
747
Olli Etuaho48fed632017-03-16 12:05:30 +0000748 setUniformValuesFromBindingQualifiers();
749
Jamie Madillbd044ed2017-06-05 12:59:21 -0400750 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400751
Martin Radev4c4c8e72016-08-04 12:25:34 +0300752 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000753}
754
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000755// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500756void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400758 mState.mAttributes.clear();
759 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800760 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400761 mState.mUniforms.clear();
762 mState.mUniformLocations.clear();
763 mState.mUniformBlocks.clear();
764 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800765 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400766 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400767 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300768 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500769 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500770
Geoff Lang7dd2e102014-11-10 15:19:26 -0500771 mValidated = false;
772
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000773 mLinked = false;
774}
775
Geoff Lange1a27752015-10-05 13:16:04 -0400776bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000777{
778 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779}
780
Jamie Madilla2c74982016-12-12 11:20:42 -0500781Error Program::loadBinary(const Context *context,
782 GLenum binaryFormat,
783 const void *binary,
784 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000785{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500786 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000787
Geoff Lang7dd2e102014-11-10 15:19:26 -0500788#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800789 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500790#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400791 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
792 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000793 {
Jamie Madillf6113162015-05-07 11:49:21 -0400794 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800795 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500796 }
797
Jamie Madill4f86d052017-06-05 12:59:26 -0400798 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
799 ANGLE_TRY_RESULT(
800 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500801 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500802#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500803}
804
Jamie Madilla2c74982016-12-12 11:20:42 -0500805Error Program::saveBinary(const Context *context,
806 GLenum *binaryFormat,
807 void *binary,
808 GLsizei bufSize,
809 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500810{
811 if (binaryFormat)
812 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400813 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500814 }
815
Jamie Madill4f86d052017-06-05 12:59:26 -0400816 angle::MemoryBuffer memoryBuf;
817 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500818
Jamie Madill4f86d052017-06-05 12:59:26 -0400819 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
820 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500821
822 if (streamLength > bufSize)
823 {
824 if (length)
825 {
826 *length = 0;
827 }
828
829 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
830 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
831 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500832 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500833 }
834
835 if (binary)
836 {
837 char *ptr = reinterpret_cast<char*>(binary);
838
Jamie Madill48ef11b2016-04-27 15:21:52 -0400839 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500840 ptr += streamLength;
841
842 ASSERT(ptr - streamLength == binary);
843 }
844
845 if (length)
846 {
847 *length = streamLength;
848 }
849
He Yunchaoacd18982017-01-04 10:46:42 +0800850 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500851}
852
853GLint Program::getBinaryLength() const
854{
855 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -0500856 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500857 if (error.isError())
858 {
859 return 0;
860 }
861
862 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000863}
864
Geoff Langc5629752015-12-07 16:29:04 -0500865void Program::setBinaryRetrievableHint(bool retrievable)
866{
867 // TODO(jmadill) : replace with dirty bits
868 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400869 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500870}
871
872bool Program::getBinaryRetrievableHint() const
873{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400874 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500875}
876
Yunchao He61afff12017-03-14 15:34:03 +0800877void Program::setSeparable(bool separable)
878{
879 // TODO(yunchao) : replace with dirty bits
880 if (mState.mSeparable != separable)
881 {
882 mProgram->setSeparable(separable);
883 mState.mSeparable = separable;
884 }
885}
886
887bool Program::isSeparable() const
888{
889 return mState.mSeparable;
890}
891
Jamie Madill6c1f6712017-02-14 19:08:04 -0500892void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000893{
894 mRefCount--;
895
896 if (mRefCount == 0 && mDeleteStatus)
897 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500898 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000899 }
900}
901
902void Program::addRef()
903{
904 mRefCount++;
905}
906
907unsigned int Program::getRefCount() const
908{
909 return mRefCount;
910}
911
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000912int Program::getInfoLogLength() const
913{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400914 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000915}
916
Geoff Lange1a27752015-10-05 13:16:04 -0400917void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000918{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000919 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000920}
921
Geoff Lange1a27752015-10-05 13:16:04 -0400922void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000923{
924 int total = 0;
925
Martin Radev4c4c8e72016-08-04 12:25:34 +0300926 if (mState.mAttachedComputeShader)
927 {
928 if (total < maxCount)
929 {
930 shaders[total] = mState.mAttachedComputeShader->getHandle();
931 total++;
932 }
933 }
934
Jamie Madill48ef11b2016-04-27 15:21:52 -0400935 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000936 {
937 if (total < maxCount)
938 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400939 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200940 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000941 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000942 }
943
Jamie Madill48ef11b2016-04-27 15:21:52 -0400944 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000945 {
946 if (total < maxCount)
947 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400948 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200949 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000950 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000951 }
952
953 if (count)
954 {
955 *count = total;
956 }
957}
958
Geoff Lange1a27752015-10-05 13:16:04 -0400959GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500960{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400961 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500962}
963
Jamie Madill63805b42015-08-25 13:17:39 -0400964bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400965{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400966 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
967 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500968}
969
jchen10fd7c3b52017-03-21 15:36:03 +0800970void Program::getActiveAttribute(GLuint index,
971 GLsizei bufsize,
972 GLsizei *length,
973 GLint *size,
974 GLenum *type,
975 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976{
Jamie Madillc349ec02015-08-21 16:53:12 -0400977 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000978 {
979 if (bufsize > 0)
980 {
981 name[0] = '\0';
982 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500983
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000984 if (length)
985 {
986 *length = 0;
987 }
988
989 *type = GL_NONE;
990 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400991 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000992 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400993
jchen1036e120e2017-03-14 14:53:58 +0800994 ASSERT(index < mState.mAttributes.size());
995 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -0400996
997 if (bufsize > 0)
998 {
jchen10fd7c3b52017-03-21 15:36:03 +0800999 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001000 }
1001
1002 // Always a single 'type' instance
1003 *size = 1;
1004 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001005}
1006
Geoff Lange1a27752015-10-05 13:16:04 -04001007GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001008{
Jamie Madillc349ec02015-08-21 16:53:12 -04001009 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001010 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001011 return 0;
1012 }
1013
jchen1036e120e2017-03-14 14:53:58 +08001014 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001015}
1016
Geoff Lange1a27752015-10-05 13:16:04 -04001017GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001018{
Jamie Madillc349ec02015-08-21 16:53:12 -04001019 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001020 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001021 return 0;
1022 }
1023
1024 size_t maxLength = 0;
1025
Jamie Madill48ef11b2016-04-27 15:21:52 -04001026 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001027 {
jchen1036e120e2017-03-14 14:53:58 +08001028 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001029 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001030
Jamie Madillc349ec02015-08-21 16:53:12 -04001031 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001032}
1033
jchen1015015f72017-03-16 13:54:21 +08001034GLuint Program::getInputResourceIndex(const GLchar *name) const
1035{
1036 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1037 {
1038 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1039 if (attribute.name == name)
1040 {
1041 return attributeIndex;
1042 }
1043 }
1044 return GL_INVALID_INDEX;
1045}
1046
1047GLuint Program::getOutputResourceIndex(const GLchar *name) const
1048{
1049 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1050}
1051
jchen10fd7c3b52017-03-21 15:36:03 +08001052size_t Program::getOutputResourceCount() const
1053{
1054 return (mLinked ? mState.mOutputVariables.size() : 0);
1055}
1056
1057void Program::getInputResourceName(GLuint index,
1058 GLsizei bufSize,
1059 GLsizei *length,
1060 GLchar *name) const
1061{
1062 GLint size;
1063 GLenum type;
1064 getActiveAttribute(index, bufSize, length, &size, &type, name);
1065}
1066
1067void Program::getOutputResourceName(GLuint index,
1068 GLsizei bufSize,
1069 GLsizei *length,
1070 GLchar *name) const
1071{
1072 if (length)
1073 {
1074 *length = 0;
1075 }
1076
1077 if (!mLinked)
1078 {
1079 if (bufSize > 0)
1080 {
1081 name[0] = '\0';
1082 }
1083 return;
1084 }
1085 ASSERT(index < mState.mOutputVariables.size());
1086 const auto &output = mState.mOutputVariables[index];
1087
1088 if (bufSize > 0)
1089 {
1090 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1091
1092 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1093 }
1094}
1095
Geoff Lang7dd2e102014-11-10 15:19:26 -05001096GLint Program::getFragDataLocation(const std::string &name) const
1097{
1098 std::string baseName(name);
1099 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001100 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001101 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001102 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001103 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1104 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001105 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001107 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001108 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001109}
1110
Geoff Lange1a27752015-10-05 13:16:04 -04001111void Program::getActiveUniform(GLuint index,
1112 GLsizei bufsize,
1113 GLsizei *length,
1114 GLint *size,
1115 GLenum *type,
1116 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001117{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001118 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001119 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001120 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001121 ASSERT(index < mState.mUniforms.size());
1122 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123
1124 if (bufsize > 0)
1125 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001126 std::string string = uniform.name;
1127 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128 {
1129 string += "[0]";
1130 }
jchen10fd7c3b52017-03-21 15:36:03 +08001131 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001132 }
1133
Jamie Madill62d31cb2015-09-11 13:25:51 -04001134 *size = uniform.elementCount();
1135 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001136 }
1137 else
1138 {
1139 if (bufsize > 0)
1140 {
1141 name[0] = '\0';
1142 }
1143
1144 if (length)
1145 {
1146 *length = 0;
1147 }
1148
1149 *size = 0;
1150 *type = GL_NONE;
1151 }
1152}
1153
Geoff Lange1a27752015-10-05 13:16:04 -04001154GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001155{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001156 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001157 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001158 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001159 }
1160 else
1161 {
1162 return 0;
1163 }
1164}
1165
Geoff Lange1a27752015-10-05 13:16:04 -04001166GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001167{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001168 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169
1170 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001171 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001172 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001173 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001174 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001175 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001176 size_t length = uniform.name.length() + 1u;
1177 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178 {
1179 length += 3; // Counting in "[0]".
1180 }
1181 maxLength = std::max(length, maxLength);
1182 }
1183 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001185
Jamie Madill62d31cb2015-09-11 13:25:51 -04001186 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187}
1188
1189GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1190{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001191 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001192 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001194 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001195 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1196 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1197 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1198 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1199 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1200 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1201 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1202 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1203 default:
1204 UNREACHABLE();
1205 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001206 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207 return 0;
1208}
1209
1210bool Program::isValidUniformLocation(GLint location) const
1211{
Jamie Madille2e406c2016-06-02 13:04:10 -04001212 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001213 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1214 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001215}
1216
Jamie Madill62d31cb2015-09-11 13:25:51 -04001217const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001218{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001219 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001220 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001221}
1222
Jamie Madillac4e9c32017-01-13 14:07:12 -05001223const VariableLocation &Program::getUniformLocation(GLint location) const
1224{
1225 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1226 return mState.mUniformLocations[location];
1227}
1228
1229const std::vector<VariableLocation> &Program::getUniformLocations() const
1230{
1231 return mState.mUniformLocations;
1232}
1233
1234const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1235{
1236 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1237 return mState.mUniforms[index];
1238}
1239
Jamie Madill62d31cb2015-09-11 13:25:51 -04001240GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001241{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001242 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001243}
1244
Jamie Madill62d31cb2015-09-11 13:25:51 -04001245GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001246{
Jamie Madille7d84322017-01-10 18:21:59 -05001247 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001248}
1249
1250void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1251{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001252 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1253 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001254}
1255
1256void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1257{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001258 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1259 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001260}
1261
1262void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1263{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001264 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1265 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001266}
1267
1268void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1269{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001270 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1271 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272}
1273
1274void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1275{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001276 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1277 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278}
1279
1280void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1281{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001282 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1283 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284}
1285
1286void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1287{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001288 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1289 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290}
1291
1292void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1293{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001294 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1295 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001296}
1297
1298void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1299{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001300 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1301 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302}
1303
1304void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1305{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001306 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1307 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308}
1309
1310void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1311{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001312 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1313 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001314}
1315
1316void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1317{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001318 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1319 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001320}
1321
1322void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1323{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001324 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1325 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326}
1327
1328void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1329{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001330 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1331 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
1334void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1335{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001336 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1337 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338}
1339
1340void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1341{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001342 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1343 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344}
1345
1346void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1347{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001348 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1349 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350}
1351
1352void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1353{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001354 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1355 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356}
1357
1358void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1359{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001360 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1361 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362}
1363
1364void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1365{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001366 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1367 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368}
1369
1370void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1371{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001372 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1373 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
Geoff Lange1a27752015-10-05 13:16:04 -04001376void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001378 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
Geoff Lange1a27752015-10-05 13:16:04 -04001381void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001383 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384}
1385
Geoff Lange1a27752015-10-05 13:16:04 -04001386void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001388 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389}
1390
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391void Program::flagForDeletion()
1392{
1393 mDeleteStatus = true;
1394}
1395
1396bool Program::isFlaggedForDeletion() const
1397{
1398 return mDeleteStatus;
1399}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001400
Brandon Jones43a53e22014-08-28 16:23:22 -07001401void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001402{
1403 mInfoLog.reset();
1404
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001406 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001407 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001408 }
1409 else
1410 {
Jamie Madillf6113162015-05-07 11:49:21 -04001411 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001412 }
1413}
1414
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1416{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001417 // Skip cache if we're using an infolog, so we get the full error.
1418 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1419 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1420 {
1421 return mCachedValidateSamplersResult.value();
1422 }
1423
1424 if (mTextureUnitTypesCache.empty())
1425 {
1426 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1427 }
1428 else
1429 {
1430 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1431 }
1432
1433 // if any two active samplers in a program are of different types, but refer to the same
1434 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1435 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001436 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001437 {
Jamie Madille7d84322017-01-10 18:21:59 -05001438 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001439
Jamie Madille7d84322017-01-10 18:21:59 -05001440 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001441 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001442 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1443 {
1444 if (infoLog)
1445 {
1446 (*infoLog) << "Sampler uniform (" << textureUnit
1447 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1448 << caps.maxCombinedTextureImageUnits << ")";
1449 }
1450
1451 mCachedValidateSamplersResult = false;
1452 return false;
1453 }
1454
1455 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1456 {
1457 if (textureType != mTextureUnitTypesCache[textureUnit])
1458 {
1459 if (infoLog)
1460 {
1461 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1462 "image unit ("
1463 << textureUnit << ").";
1464 }
1465
1466 mCachedValidateSamplersResult = false;
1467 return false;
1468 }
1469 }
1470 else
1471 {
1472 mTextureUnitTypesCache[textureUnit] = textureType;
1473 }
1474 }
1475 }
1476
1477 mCachedValidateSamplersResult = true;
1478 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479}
1480
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001481bool Program::isValidated() const
1482{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483 return mValidated;
1484}
1485
Geoff Lange1a27752015-10-05 13:16:04 -04001486GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001488 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489}
1490
1491void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1492{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001493 ASSERT(
1494 uniformBlockIndex <
1495 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496
Jamie Madill48ef11b2016-04-27 15:21:52 -04001497 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498
1499 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001500 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501 std::string string = uniformBlock.name;
1502
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001505 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001506 }
jchen10fd7c3b52017-03-21 15:36:03 +08001507 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001508 }
1509}
1510
Geoff Lange1a27752015-10-05 13:16:04 -04001511GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001512{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513 int maxLength = 0;
1514
1515 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001516 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001517 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1519 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001520 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521 if (!uniformBlock.name.empty())
1522 {
jchen10af713a22017-04-19 09:10:56 +08001523 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1524 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525 }
1526 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001527 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528
1529 return maxLength;
1530}
1531
Geoff Lange1a27752015-10-05 13:16:04 -04001532GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001533{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001534 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001535 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001536
Jamie Madill48ef11b2016-04-27 15:21:52 -04001537 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001538 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1539 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001540 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001541 if (uniformBlock.name == baseName)
1542 {
1543 const bool arrayElementZero =
1544 (subscript == GL_INVALID_INDEX &&
1545 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1546 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1547 {
1548 return blockIndex;
1549 }
1550 }
1551 }
1552
1553 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001554}
1555
Jamie Madill62d31cb2015-09-11 13:25:51 -04001556const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001557{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001558 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1559 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001560}
1561
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001562void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1563{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001564 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001565 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001566 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001567}
1568
1569GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1570{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001571 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001572}
1573
1574void Program::resetUniformBlockBindings()
1575{
1576 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1577 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001578 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001579 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001580 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001581}
1582
Geoff Lang48dcae72014-02-05 16:28:24 -05001583void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1584{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001585 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001586 for (GLsizei i = 0; i < count; i++)
1587 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001588 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001589 }
1590
Jamie Madill48ef11b2016-04-27 15:21:52 -04001591 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001592}
1593
1594void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1595{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001596 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001597 {
jchen10a9042d32017-03-17 08:50:45 +08001598 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1599 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1600 std::string varName = var.nameWithArrayIndex();
1601 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001602 if (length)
1603 {
1604 *length = lastNameIdx;
1605 }
1606 if (size)
1607 {
jchen10a9042d32017-03-17 08:50:45 +08001608 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001609 }
1610 if (type)
1611 {
jchen10a9042d32017-03-17 08:50:45 +08001612 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001613 }
1614 if (name)
1615 {
jchen10a9042d32017-03-17 08:50:45 +08001616 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001617 name[lastNameIdx] = '\0';
1618 }
1619 }
1620}
1621
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001622GLsizei Program::getTransformFeedbackVaryingCount() const
1623{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001625 {
jchen10a9042d32017-03-17 08:50:45 +08001626 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001627 }
1628 else
1629 {
1630 return 0;
1631 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001632}
1633
1634GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1635{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001637 {
1638 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001639 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001640 {
jchen10a9042d32017-03-17 08:50:45 +08001641 maxSize =
1642 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001643 }
1644
1645 return maxSize;
1646 }
1647 else
1648 {
1649 return 0;
1650 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001651}
1652
1653GLenum Program::getTransformFeedbackBufferMode() const
1654{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001655 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656}
1657
Jamie Madillbd044ed2017-06-05 12:59:21 -04001658bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001659{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001660 Shader *vertexShader = mState.mAttachedVertexShader;
1661 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001662
Jamie Madillbd044ed2017-06-05 12:59:21 -04001663 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001664
Jamie Madillbd044ed2017-06-05 12:59:21 -04001665 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1666 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667
Sami Väisänen46eaa942016-06-29 10:26:37 +03001668 std::map<GLuint, std::string> staticFragmentInputLocations;
1669
Jamie Madill4cff2472015-08-21 16:53:18 -04001670 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672 bool matched = false;
1673
1674 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001675 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676 {
1677 continue;
1678 }
1679
Jamie Madill4cff2472015-08-21 16:53:18 -04001680 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001682 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001683 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001684 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001685 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001686 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687 {
1688 return false;
1689 }
1690
Geoff Lang7dd2e102014-11-10 15:19:26 -05001691 matched = true;
1692 break;
1693 }
1694 }
1695
1696 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001697 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001699 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700 return false;
1701 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001702
1703 // Check for aliased path rendering input bindings (if any).
1704 // If more than one binding refer statically to the same
1705 // location the link must fail.
1706
1707 if (!output.staticUse)
1708 continue;
1709
1710 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1711 if (inputBinding == -1)
1712 continue;
1713
1714 const auto it = staticFragmentInputLocations.find(inputBinding);
1715 if (it == std::end(staticFragmentInputLocations))
1716 {
1717 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1718 }
1719 else
1720 {
1721 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1722 << it->second;
1723 return false;
1724 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001725 }
1726
Jamie Madillbd044ed2017-06-05 12:59:21 -04001727 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001728 {
1729 return false;
1730 }
1731
Jamie Madillada9ecc2015-08-17 12:53:37 -04001732 // TODO(jmadill): verify no unmatched vertex varyings?
1733
Geoff Lang7dd2e102014-11-10 15:19:26 -05001734 return true;
1735}
1736
Jamie Madillbd044ed2017-06-05 12:59:21 -04001737bool Program::linkUniforms(const Context *context,
1738 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001739 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001740{
Olli Etuahob78707c2017-03-09 15:03:11 +00001741 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001742 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001743 {
1744 return false;
1745 }
1746
Olli Etuahob78707c2017-03-09 15:03:11 +00001747 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001748
Olli Etuaho48fed632017-03-16 12:05:30 +00001749 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001750
1751 return true;
1752}
1753
Olli Etuaho48fed632017-03-16 12:05:30 +00001754void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001755{
Jamie Madill982f6e02017-06-07 14:33:04 -04001756 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1757 unsigned int low = high;
1758
1759 for (auto samplerIter = mState.mUniforms.rbegin();
1760 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001761 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001762 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001763 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001764
1765 mState.mSamplerUniformRange = RangeUI(low, high);
1766
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001767 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001768 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001769 {
1770 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1771 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1772 mState.mSamplerBindings.emplace_back(
1773 SamplerBinding(textureType, samplerUniform.elementCount()));
1774 }
1775}
1776
Martin Radev4c4c8e72016-08-04 12:25:34 +03001777bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1778 const std::string &uniformName,
1779 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001780 const sh::InterfaceBlockField &fragmentUniform,
1781 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001783 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1784 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1785 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001786 {
1787 return false;
1788 }
1789
1790 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1791 {
Jamie Madillf6113162015-05-07 11:49:21 -04001792 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001793 return false;
1794 }
1795
1796 return true;
1797}
1798
Jamie Madilleb979bf2016-11-15 12:28:46 -05001799// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001800bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001802 const ContextState &data = context->getContextState();
1803 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001804
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001806 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001807 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001808
1809 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001810 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001811 {
Jamie Madillf6113162015-05-07 11:49:21 -04001812 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001813 return false;
1814 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001816 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001817
Jamie Madillc349ec02015-08-21 16:53:12 -04001818 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001819 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001820 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001821 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001822 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001823 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001824 attribute.location = bindingLocation;
1825 }
1826
1827 if (attribute.location != -1)
1828 {
1829 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001830 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831
Jamie Madill63805b42015-08-25 13:17:39 -04001832 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 {
Jamie Madillf6113162015-05-07 11:49:21 -04001834 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001835 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001836
1837 return false;
1838 }
1839
Jamie Madill63805b42015-08-25 13:17:39 -04001840 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 {
Jamie Madill63805b42015-08-25 13:17:39 -04001842 const int regLocation = attribute.location + reg;
1843 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844
1845 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001846 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001847 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001849 // TODO(jmadill): fix aliasing on ES2
1850 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001852 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001853 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 return false;
1855 }
1856 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001857 else
1858 {
Jamie Madill63805b42015-08-25 13:17:39 -04001859 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001860 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861
Jamie Madill63805b42015-08-25 13:17:39 -04001862 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001863 }
1864 }
1865 }
1866
1867 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001868 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001870 // Not set by glBindAttribLocation or by location layout qualifier
1871 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001872 {
Jamie Madill63805b42015-08-25 13:17:39 -04001873 int regs = VariableRegisterCount(attribute.type);
1874 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875
Jamie Madill63805b42015-08-25 13:17:39 -04001876 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877 {
Jamie Madillf6113162015-05-07 11:49:21 -04001878 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001879 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 }
1881
Jamie Madillc349ec02015-08-21 16:53:12 -04001882 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 }
1884 }
1885
Jamie Madill48ef11b2016-04-27 15:21:52 -04001886 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001887 {
Jamie Madill63805b42015-08-25 13:17:39 -04001888 ASSERT(attribute.location != -1);
1889 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001890
Jamie Madill63805b42015-08-25 13:17:39 -04001891 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001893 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001894 }
1895 }
1896
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897 return true;
1898}
1899
Martin Radev4c4c8e72016-08-04 12:25:34 +03001900bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1901 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1902 const std::string &errorMessage,
1903 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001905 GLuint blockCount = 0;
1906 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001908 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04001909 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001910 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04001911 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001912 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04001913 return false;
1914 }
1915 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001916 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001917 return true;
1918}
Jamie Madille473dee2015-08-18 14:49:01 -04001919
Martin Radev4c4c8e72016-08-04 12:25:34 +03001920bool Program::validateVertexAndFragmentInterfaceBlocks(
1921 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
1922 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001923 InfoLog &infoLog,
1924 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03001925{
1926 // Check that interface blocks defined in the vertex and fragment shaders are identical
1927 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
1928 UniformBlockMap linkedUniformBlocks;
1929
1930 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
1931 {
1932 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
1933 }
1934
Jamie Madille473dee2015-08-18 14:49:01 -04001935 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001936 {
Jamie Madille473dee2015-08-18 14:49:01 -04001937 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001938 if (entry != linkedUniformBlocks.end())
1939 {
1940 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04001941 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
1942 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943 {
1944 return false;
1945 }
1946 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001947 }
1948 return true;
1949}
Jamie Madille473dee2015-08-18 14:49:01 -04001950
Jamie Madillbd044ed2017-06-05 12:59:21 -04001951bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001952{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001953 const auto &caps = context->getCaps();
1954
Martin Radev4c4c8e72016-08-04 12:25:34 +03001955 if (mState.mAttachedComputeShader)
1956 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001957 Shader &computeShader = *mState.mAttachedComputeShader;
1958 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001959
1960 if (!validateUniformBlocksCount(
1961 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
1962 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
1963 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001965 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001967 return true;
1968 }
1969
Jamie Madillbd044ed2017-06-05 12:59:21 -04001970 Shader &vertexShader = *mState.mAttachedVertexShader;
1971 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001972
Jamie Madillbd044ed2017-06-05 12:59:21 -04001973 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
1974 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001975
1976 if (!validateUniformBlocksCount(
1977 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
1978 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
1979 {
1980 return false;
1981 }
1982 if (!validateUniformBlocksCount(
1983 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
1984 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
1985 infoLog))
1986 {
1987
1988 return false;
1989 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001990
1991 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001992 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001993 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001994 {
1995 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996 }
Jamie Madille473dee2015-08-18 14:49:01 -04001997
Geoff Lang7dd2e102014-11-10 15:19:26 -05001998 return true;
1999}
2000
Jamie Madilla2c74982016-12-12 11:20:42 -05002001bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002002 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002003 const sh::InterfaceBlock &fragmentInterfaceBlock,
2004 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002005{
2006 const char* blockName = vertexInterfaceBlock.name.c_str();
2007 // validate blocks for the same member types
2008 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2009 {
Jamie Madillf6113162015-05-07 11:49:21 -04002010 infoLog << "Types for interface block '" << blockName
2011 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012 return false;
2013 }
2014 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2015 {
Jamie Madillf6113162015-05-07 11:49:21 -04002016 infoLog << "Array sizes differ for interface block '" << blockName
2017 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018 return false;
2019 }
jchen10af713a22017-04-19 09:10:56 +08002020 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2021 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2022 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023 {
Jamie Madillf6113162015-05-07 11:49:21 -04002024 infoLog << "Layout qualifiers differ for interface block '" << blockName
2025 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 return false;
2027 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002028 const unsigned int numBlockMembers =
2029 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2031 {
2032 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2033 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2034 if (vertexMember.name != fragmentMember.name)
2035 {
Jamie Madillf6113162015-05-07 11:49:21 -04002036 infoLog << "Name mismatch for field " << blockMemberIndex
2037 << " of interface block '" << blockName
2038 << "': (in vertex: '" << vertexMember.name
2039 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040 return false;
2041 }
2042 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002043 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2044 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045 {
2046 return false;
2047 }
2048 }
2049 return true;
2050}
2051
2052bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2053 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2054{
2055 if (vertexVariable.type != fragmentVariable.type)
2056 {
Jamie Madillf6113162015-05-07 11:49:21 -04002057 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 return false;
2059 }
2060 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2061 {
Jamie Madillf6113162015-05-07 11:49:21 -04002062 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 return false;
2064 }
2065 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2066 {
Jamie Madillf6113162015-05-07 11:49:21 -04002067 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 return false;
2069 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002070 if (vertexVariable.structName != fragmentVariable.structName)
2071 {
2072 infoLog << "Structure names for " << variableName
2073 << " differ between vertex and fragment shaders";
2074 return false;
2075 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076
2077 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2078 {
Jamie Madillf6113162015-05-07 11:49:21 -04002079 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080 return false;
2081 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002082 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2084 {
2085 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2086 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2087
2088 if (vertexMember.name != fragmentMember.name)
2089 {
Jamie Madillf6113162015-05-07 11:49:21 -04002090 infoLog << "Name mismatch for field '" << memberIndex
2091 << "' of " << variableName
2092 << ": (in vertex: '" << vertexMember.name
2093 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094 return false;
2095 }
2096
2097 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2098 vertexMember.name + "'";
2099
2100 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2101 {
2102 return false;
2103 }
2104 }
2105
2106 return true;
2107}
2108
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002109bool Program::linkValidateVaryings(InfoLog &infoLog,
2110 const std::string &varyingName,
2111 const sh::Varying &vertexVarying,
2112 const sh::Varying &fragmentVarying,
2113 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114{
2115 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2116 {
2117 return false;
2118 }
2119
Jamie Madille9cc4692015-02-19 16:00:13 -05002120 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002122 infoLog << "Interpolation types for " << varyingName
2123 << " differ between vertex and fragment shaders.";
2124 return false;
2125 }
2126
2127 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2128 {
2129 infoLog << "Invariance for " << varyingName
2130 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 return false;
2132 }
2133
2134 return true;
2135}
2136
Jamie Madillbd044ed2017-06-05 12:59:21 -04002137bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002138{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002139 Shader *vertexShader = mState.mAttachedVertexShader;
2140 Shader *fragmentShader = mState.mAttachedFragmentShader;
2141 const auto &vertexVaryings = vertexShader->getVaryings(context);
2142 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2143 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002144
2145 if (shaderVersion != 100)
2146 {
2147 // Only ESSL 1.0 has restrictions on matching input and output invariance
2148 return true;
2149 }
2150
2151 bool glPositionIsInvariant = false;
2152 bool glPointSizeIsInvariant = false;
2153 bool glFragCoordIsInvariant = false;
2154 bool glPointCoordIsInvariant = false;
2155
2156 for (const sh::Varying &varying : vertexVaryings)
2157 {
2158 if (!varying.isBuiltIn())
2159 {
2160 continue;
2161 }
2162 if (varying.name.compare("gl_Position") == 0)
2163 {
2164 glPositionIsInvariant = varying.isInvariant;
2165 }
2166 else if (varying.name.compare("gl_PointSize") == 0)
2167 {
2168 glPointSizeIsInvariant = varying.isInvariant;
2169 }
2170 }
2171
2172 for (const sh::Varying &varying : fragmentVaryings)
2173 {
2174 if (!varying.isBuiltIn())
2175 {
2176 continue;
2177 }
2178 if (varying.name.compare("gl_FragCoord") == 0)
2179 {
2180 glFragCoordIsInvariant = varying.isInvariant;
2181 }
2182 else if (varying.name.compare("gl_PointCoord") == 0)
2183 {
2184 glPointCoordIsInvariant = varying.isInvariant;
2185 }
2186 }
2187
2188 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2189 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2190 // Not requiring invariance to match is supported by:
2191 // dEQP, WebGL CTS, Nexus 5X GLES
2192 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2193 {
2194 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2195 "declared invariant.";
2196 return false;
2197 }
2198 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2199 {
2200 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2201 "declared invariant.";
2202 return false;
2203 }
2204
2205 return true;
2206}
2207
jchen10a9042d32017-03-17 08:50:45 +08002208bool Program::linkValidateTransformFeedback(const gl::Context *context,
2209 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002210 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002211 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002212{
2213 size_t totalComponents = 0;
2214
Jamie Madillccdf74b2015-08-18 10:46:12 -04002215 std::set<std::string> uniqueNames;
2216
Jamie Madill48ef11b2016-04-27 15:21:52 -04002217 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218 {
2219 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002220 size_t subscript = GL_INVALID_INDEX;
2221 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2222
Jamie Madill192745a2016-12-22 15:58:21 -05002223 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 {
Jamie Madill192745a2016-12-22 15:58:21 -05002225 const sh::Varying *varying = ref.second.get();
2226
jchen10a9042d32017-03-17 08:50:45 +08002227 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002229 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002230 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002231 infoLog << "Two transform feedback varyings specify the same output variable ("
2232 << tfVaryingName << ").";
2233 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234 }
jchen10a9042d32017-03-17 08:50:45 +08002235 if (context->getClientVersion() >= Version(3, 1))
2236 {
2237 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2238 {
2239 infoLog
2240 << "Two transform feedback varyings include the same array element ("
2241 << tfVaryingName << ").";
2242 return false;
2243 }
2244 }
2245 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002246 {
2247 infoLog << "Capture of arrays is undefined and not supported.";
2248 return false;
2249 }
2250
jchen10a9042d32017-03-17 08:50:45 +08002251 uniqueNames.insert(tfVaryingName);
2252
Jamie Madillccdf74b2015-08-18 10:46:12 -04002253 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002254 size_t elementCount =
2255 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2256 : 1);
2257 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002258 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 componentCount > caps.maxTransformFeedbackSeparateComponents)
2260 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002261 infoLog << "Transform feedback varying's " << varying->name << " components ("
2262 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002263 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002264 return false;
2265 }
2266
2267 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 found = true;
2269 break;
2270 }
2271 }
jchen10a9042d32017-03-17 08:50:45 +08002272 if (context->getClientVersion() < Version(3, 1) &&
2273 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002274 {
Geoff Lang1a683462015-09-29 15:09:59 -04002275 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002276 return false;
2277 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2279 ASSERT(found);
2280 }
2281
Jamie Madill48ef11b2016-04-27 15:21:52 -04002282 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002283 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002284 {
Jamie Madillf6113162015-05-07 11:49:21 -04002285 infoLog << "Transform feedback varying total components (" << totalComponents
2286 << ") exceed the maximum interleaved components ("
2287 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002288 return false;
2289 }
2290
2291 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002292}
2293
Jamie Madill192745a2016-12-22 15:58:21 -05002294void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002295{
2296 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002297 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002298 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002299 {
jchen10a9042d32017-03-17 08:50:45 +08002300 size_t subscript = GL_INVALID_INDEX;
2301 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002302 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002303 {
Jamie Madill192745a2016-12-22 15:58:21 -05002304 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002305 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002306 {
jchen10a9042d32017-03-17 08:50:45 +08002307 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2308 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002309 break;
2310 }
2311 }
2312 }
2313}
2314
Jamie Madillbd044ed2017-06-05 12:59:21 -04002315Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002316{
Jamie Madill192745a2016-12-22 15:58:21 -05002317 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002318
Jamie Madillbd044ed2017-06-05 12:59:21 -04002319 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002320 {
Jamie Madill192745a2016-12-22 15:58:21 -05002321 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002322 }
2323
Jamie Madillbd044ed2017-06-05 12:59:21 -04002324 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002325 {
Jamie Madill192745a2016-12-22 15:58:21 -05002326 merged[varying.name].fragment = &varying;
2327 }
2328
2329 return merged;
2330}
2331
2332std::vector<PackedVarying> Program::getPackedVaryings(
2333 const Program::MergedVaryings &mergedVaryings) const
2334{
2335 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2336 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002337 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002338
2339 for (const auto &ref : mergedVaryings)
2340 {
2341 const sh::Varying *input = ref.second.vertex;
2342 const sh::Varying *output = ref.second.fragment;
2343
2344 // Only pack varyings that have a matched input or output, plus special builtins.
2345 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002346 {
Jamie Madill192745a2016-12-22 15:58:21 -05002347 // Will get the vertex shader interpolation by default.
2348 auto interpolation = ref.second.get()->interpolation;
2349
2350 // Interpolation qualifiers must match.
2351 if (output->isStruct())
2352 {
2353 ASSERT(!output->isArray());
2354 for (const auto &field : output->fields)
2355 {
2356 ASSERT(!field.isStruct() && !field.isArray());
2357 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2358 }
2359 }
2360 else
2361 {
2362 packedVaryings.push_back(PackedVarying(*output, interpolation));
2363 }
2364 continue;
2365 }
2366
2367 // Keep Transform FB varyings in the merged list always.
2368 if (!input)
2369 {
2370 continue;
2371 }
2372
2373 for (const std::string &tfVarying : tfVaryings)
2374 {
jchen10a9042d32017-03-17 08:50:45 +08002375 size_t subscript = GL_INVALID_INDEX;
2376 std::string baseName = ParseResourceName(tfVarying, &subscript);
2377 if (uniqueFullNames.count(tfVarying) > 0)
2378 {
2379 continue;
2380 }
2381 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002382 {
2383 // Transform feedback for varying structs is underspecified.
2384 // See Khronos bug 9856.
2385 // TODO(jmadill): Figure out how to be spec-compliant here.
2386 if (!input->isStruct())
2387 {
2388 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2389 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002390 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2391 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002392 }
jchen10a9042d32017-03-17 08:50:45 +08002393 if (subscript == GL_INVALID_INDEX)
2394 {
2395 break;
2396 }
Jamie Madill192745a2016-12-22 15:58:21 -05002397 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002398 }
2399 }
2400
Jamie Madill192745a2016-12-22 15:58:21 -05002401 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2402
2403 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002404}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002405
Jamie Madillbd044ed2017-06-05 12:59:21 -04002406void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002407{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002408 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002409 ASSERT(fragmentShader != nullptr);
2410
Geoff Lange0cff192017-05-30 13:04:56 -04002411 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002412 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002413
2414 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002415 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002416 {
2417 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2418 outputVariable.name != "gl_FragData")
2419 {
2420 continue;
2421 }
2422
2423 unsigned int baseLocation =
2424 (outputVariable.location == -1 ? 0u
2425 : static_cast<unsigned int>(outputVariable.location));
2426 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2427 elementIndex++)
2428 {
2429 const unsigned int location = baseLocation + elementIndex;
2430 if (location >= mState.mOutputVariableTypes.size())
2431 {
2432 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2433 }
Corentin Walleze7557742017-06-01 13:09:57 -04002434 ASSERT(location < mState.mActiveOutputVariables.size());
2435 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002436 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2437 }
2438 }
2439
Jamie Madill80a6fc02015-08-21 16:53:16 -04002440 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002441 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002442 return;
2443
Jamie Madillbd044ed2017-06-05 12:59:21 -04002444 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002445 // TODO(jmadill): any caps validation here?
2446
jchen1015015f72017-03-16 13:54:21 +08002447 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002448 outputVariableIndex++)
2449 {
jchen1015015f72017-03-16 13:54:21 +08002450 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002451
2452 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2453 if (outputVariable.isBuiltIn())
2454 continue;
2455
2456 // Since multiple output locations must be specified, use 0 for non-specified locations.
2457 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2458
Jamie Madill80a6fc02015-08-21 16:53:16 -04002459 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2460 elementIndex++)
2461 {
2462 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002463 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002464 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002465 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002466 VariableLocation(outputVariable.name, element, outputVariableIndex);
2467 }
2468 }
2469}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002470
Olli Etuaho48fed632017-03-16 12:05:30 +00002471void Program::setUniformValuesFromBindingQualifiers()
2472{
Jamie Madill982f6e02017-06-07 14:33:04 -04002473 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002474 {
2475 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2476 if (samplerUniform.binding != -1)
2477 {
2478 GLint location = mState.getUniformLocation(samplerUniform.name);
2479 ASSERT(location != -1);
2480 std::vector<GLint> boundTextureUnits;
2481 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2482 ++elementIndex)
2483 {
2484 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2485 }
2486 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2487 boundTextureUnits.data());
2488 }
2489 }
2490}
2491
Jamie Madillbd044ed2017-06-05 12:59:21 -04002492void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002493{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002494 ASSERT(mState.mUniformBlocks.empty());
2495
2496 if (mState.mAttachedComputeShader)
2497 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002498 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002499
Jamie Madillbd044ed2017-06-05 12:59:21 -04002500 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002501 {
2502
2503 // Only 'packed' blocks are allowed to be considered inactive.
2504 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2505 continue;
2506
Jamie Madilla2c74982016-12-12 11:20:42 -05002507 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002508 {
2509 if (block.name == computeBlock.name)
2510 {
2511 block.computeStaticUse = computeBlock.staticUse;
2512 }
2513 }
2514
2515 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2516 }
2517 return;
2518 }
2519
Jamie Madill62d31cb2015-09-11 13:25:51 -04002520 std::set<std::string> visitedList;
2521
Jamie Madillbd044ed2017-06-05 12:59:21 -04002522 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002523
Jamie Madillbd044ed2017-06-05 12:59:21 -04002524 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002525 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002526 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002527 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2528 continue;
2529
2530 if (visitedList.count(vertexBlock.name) > 0)
2531 continue;
2532
2533 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2534 visitedList.insert(vertexBlock.name);
2535 }
2536
Jamie Madillbd044ed2017-06-05 12:59:21 -04002537 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002538
Jamie Madillbd044ed2017-06-05 12:59:21 -04002539 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002540 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002541 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002542 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2543 continue;
2544
2545 if (visitedList.count(fragmentBlock.name) > 0)
2546 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002547 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002548 {
2549 if (block.name == fragmentBlock.name)
2550 {
2551 block.fragmentStaticUse = fragmentBlock.staticUse;
2552 }
2553 }
2554
2555 continue;
2556 }
2557
2558 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2559 visitedList.insert(fragmentBlock.name);
2560 }
jchen10af713a22017-04-19 09:10:56 +08002561 // Set initial bindings from shader.
2562 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2563 {
2564 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2565 bindUniformBlock(blockIndex, uniformBlock.binding);
2566 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002567}
2568
Jamie Madill4a3c2342015-10-08 12:58:45 -04002569template <typename VarT>
2570void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2571 const std::string &prefix,
2572 int blockIndex)
2573{
2574 for (const VarT &field : fields)
2575 {
2576 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2577
2578 if (field.isStruct())
2579 {
2580 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2581 {
2582 const std::string uniformElementName =
2583 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2584 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2585 }
2586 }
2587 else
2588 {
2589 // If getBlockMemberInfo returns false, the uniform is optimized out.
2590 sh::BlockMemberInfo memberInfo;
2591 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2592 {
2593 continue;
2594 }
2595
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002596 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002597 blockIndex, memberInfo);
2598
2599 // Since block uniforms have no location, we don't need to store them in the uniform
2600 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002601 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002602 }
2603 }
2604}
2605
Jamie Madill62d31cb2015-09-11 13:25:51 -04002606void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2607{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002608 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002609 size_t blockSize = 0;
2610
Jamie Madill4a3c2342015-10-08 12:58:45 -04002611 // Track the first and last uniform index to determine the range of active uniforms in the
2612 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002613 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002614 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002615 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002616
2617 std::vector<unsigned int> blockUniformIndexes;
2618 for (size_t blockUniformIndex = firstBlockUniformIndex;
2619 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2620 {
2621 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2622 }
jchen10af713a22017-04-19 09:10:56 +08002623 // ESSL 3.10 section 4.4.4 page 58:
2624 // Any uniform or shader storage block declared without a binding qualifier is initially
2625 // assigned to block binding point zero.
2626 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002627 if (interfaceBlock.arraySize > 0)
2628 {
2629 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2630 {
jchen10af713a22017-04-19 09:10:56 +08002631 // Don't define this block at all if it's not active in the implementation.
2632 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2633 &blockSize))
2634 {
2635 continue;
2636 }
2637 UniformBlock block(interfaceBlock.name, true, arrayElement,
2638 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002639 block.memberUniformIndexes = blockUniformIndexes;
2640
Martin Radev4c4c8e72016-08-04 12:25:34 +03002641 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002642 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002643 case GL_VERTEX_SHADER:
2644 {
2645 block.vertexStaticUse = interfaceBlock.staticUse;
2646 break;
2647 }
2648 case GL_FRAGMENT_SHADER:
2649 {
2650 block.fragmentStaticUse = interfaceBlock.staticUse;
2651 break;
2652 }
2653 case GL_COMPUTE_SHADER:
2654 {
2655 block.computeStaticUse = interfaceBlock.staticUse;
2656 break;
2657 }
2658 default:
2659 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002660 }
2661
Qin Jiajia0350a642016-11-01 17:01:51 +08002662 // Since all block elements in an array share the same active uniforms, they will all be
2663 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2664 // here we will add every block element in the array.
2665 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002666 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002667 }
2668 }
2669 else
2670 {
jchen10af713a22017-04-19 09:10:56 +08002671 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2672 {
2673 return;
2674 }
2675 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002676 block.memberUniformIndexes = blockUniformIndexes;
2677
Martin Radev4c4c8e72016-08-04 12:25:34 +03002678 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002679 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002680 case GL_VERTEX_SHADER:
2681 {
2682 block.vertexStaticUse = interfaceBlock.staticUse;
2683 break;
2684 }
2685 case GL_FRAGMENT_SHADER:
2686 {
2687 block.fragmentStaticUse = interfaceBlock.staticUse;
2688 break;
2689 }
2690 case GL_COMPUTE_SHADER:
2691 {
2692 block.computeStaticUse = interfaceBlock.staticUse;
2693 break;
2694 }
2695 default:
2696 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002697 }
2698
Jamie Madill4a3c2342015-10-08 12:58:45 -04002699 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002700 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002701 }
2702}
2703
Jamie Madille7d84322017-01-10 18:21:59 -05002704template <>
2705void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2706 const uint8_t *destPointer,
2707 GLsizei clampedCount,
2708 const GLint *v)
2709{
2710 // Invalidate the validation cache only if we modify the sampler data.
2711 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2712 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2713 {
2714 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2715 std::vector<GLuint> *boundTextureUnits =
2716 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2717
2718 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2719 mCachedValidateSamplersResult.reset();
2720 }
2721}
2722
2723template <typename T>
2724void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2725 const uint8_t *destPointer,
2726 GLsizei clampedCount,
2727 const T *v)
2728{
2729}
2730
Jamie Madill62d31cb2015-09-11 13:25:51 -04002731template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002732GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002733{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002734 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2735 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002736 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2737
Corentin Wallez15ac5342016-11-03 17:06:39 -04002738 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2739 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2740 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002741 GLsizei maxElementCount =
2742 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2743
2744 GLsizei count = countIn;
2745 GLsizei clampedCount = count * vectorSize;
2746 if (clampedCount > maxElementCount)
2747 {
2748 clampedCount = maxElementCount;
2749 count = maxElementCount / vectorSize;
2750 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002751
Jamie Madill62d31cb2015-09-11 13:25:51 -04002752 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2753 {
2754 // Do a cast conversion for boolean types. From the spec:
2755 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2756 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002757 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002758 {
2759 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2760 }
2761 }
2762 else
2763 {
Jamie Madille7d84322017-01-10 18:21:59 -05002764 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002765 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002766 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002767
2768 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002769}
2770
2771template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002772GLsizei Program::setMatrixUniformInternal(GLint location,
2773 GLsizei count,
2774 GLboolean transpose,
2775 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002776{
2777 if (!transpose)
2778 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002779 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002780 }
2781
2782 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002783 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2784 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002785 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002786
2787 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2788 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2789 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2790 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2791
2792 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002793 {
2794 size_t elementOffset = element * rows * cols;
2795
2796 for (size_t row = 0; row < rows; ++row)
2797 {
2798 for (size_t col = 0; col < cols; ++col)
2799 {
2800 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2801 }
2802 }
2803 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002804
2805 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002806}
2807
2808template <typename DestT>
2809void Program::getUniformInternal(GLint location, DestT *dataOut) const
2810{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002811 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2812 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002813
2814 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2815
2816 GLenum componentType = VariableComponentType(uniform.type);
2817 if (componentType == GLTypeToGLenum<DestT>::value)
2818 {
2819 memcpy(dataOut, srcPointer, uniform.getElementSize());
2820 return;
2821 }
2822
Corentin Wallez6596c462016-03-17 17:26:58 -04002823 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002824
2825 switch (componentType)
2826 {
2827 case GL_INT:
2828 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2829 break;
2830 case GL_UNSIGNED_INT:
2831 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2832 break;
2833 case GL_BOOL:
2834 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2835 break;
2836 case GL_FLOAT:
2837 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2838 break;
2839 default:
2840 UNREACHABLE();
2841 }
2842}
Jamie Madilla4595b82017-01-11 17:36:34 -05002843
2844bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2845{
2846 // Must be called after samplers are validated.
2847 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2848
2849 for (const auto &binding : mState.mSamplerBindings)
2850 {
2851 GLenum textureType = binding.textureType;
2852 for (const auto &unit : binding.boundTextureUnits)
2853 {
2854 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2855 if (programTextureID == textureID)
2856 {
2857 // TODO(jmadill): Check for appropriate overlap.
2858 return true;
2859 }
2860 }
2861 }
2862
2863 return false;
2864}
2865
Jamie Madilla2c74982016-12-12 11:20:42 -05002866} // namespace gl