blob: 3ec48d1bc80f67485668e918a709a707410a4df9 [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
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500385Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400386 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400387 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500388 mLinked(false),
389 mDeleteStatus(false),
390 mRefCount(0),
391 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500392 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500393{
394 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000395
396 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500397 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
400Program::~Program()
401{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500402 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
403 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500404 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
Jamie Madill6c1f6712017-02-14 19:08:04 -0500407void Program::destroy(const Context *context)
408{
409 if (mState.mAttachedVertexShader != nullptr)
410 {
411 mState.mAttachedVertexShader->release(context);
412 mState.mAttachedVertexShader = nullptr;
413 }
414
415 if (mState.mAttachedFragmentShader != nullptr)
416 {
417 mState.mAttachedFragmentShader->release(context);
418 mState.mAttachedFragmentShader = nullptr;
419 }
420
421 if (mState.mAttachedComputeShader != nullptr)
422 {
423 mState.mAttachedComputeShader->release(context);
424 mState.mAttachedComputeShader = nullptr;
425 }
426
Jamie Madillc564c072017-06-01 12:45:42 -0400427 mProgram->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500428}
429
Geoff Lang70d0f492015-12-10 17:45:46 -0500430void Program::setLabel(const std::string &label)
431{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400432 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500433}
434
435const std::string &Program::getLabel() const
436{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400437 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500438}
439
Jamie Madillef300b12016-10-07 15:12:09 -0400440void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300442 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300444 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445 {
Jamie Madillef300b12016-10-07 15:12:09 -0400446 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300447 mState.mAttachedVertexShader = shader;
448 mState.mAttachedVertexShader->addRef();
449 break;
450 }
451 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452 {
Jamie Madillef300b12016-10-07 15:12:09 -0400453 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300454 mState.mAttachedFragmentShader = shader;
455 mState.mAttachedFragmentShader->addRef();
456 break;
457 }
458 case GL_COMPUTE_SHADER:
459 {
Jamie Madillef300b12016-10-07 15:12:09 -0400460 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300461 mState.mAttachedComputeShader = shader;
462 mState.mAttachedComputeShader->addRef();
463 break;
464 }
465 default:
466 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468}
469
Jamie Madillc1d770e2017-04-13 17:31:24 -0400470void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300472 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300474 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400476 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500477 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300478 mState.mAttachedVertexShader = nullptr;
479 break;
480 }
481 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400483 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500484 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300485 mState.mAttachedFragmentShader = nullptr;
486 break;
487 }
488 case GL_COMPUTE_SHADER:
489 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400490 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500491 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300492 mState.mAttachedComputeShader = nullptr;
493 break;
494 }
495 default:
496 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498}
499
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000500int Program::getAttachedShadersCount() const
501{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300502 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
503 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000504}
505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506void Program::bindAttributeLocation(GLuint index, const char *name)
507{
Geoff Langd8605522016-04-13 10:19:12 -0400508 mAttributeBindings.bindLocation(index, name);
509}
510
511void Program::bindUniformLocation(GLuint index, const char *name)
512{
513 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800514 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515}
516
Sami Väisänen46eaa942016-06-29 10:26:37 +0300517void Program::bindFragmentInputLocation(GLint index, const char *name)
518{
519 mFragmentInputBindings.bindLocation(index, name);
520}
521
Jamie Madillbd044ed2017-06-05 12:59:21 -0400522BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300523{
524 BindingInfo ret;
525 ret.type = GL_NONE;
526 ret.valid = false;
527
Jamie Madillbd044ed2017-06-05 12:59:21 -0400528 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300529 ASSERT(fragmentShader);
530
531 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400532 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300533
534 for (const auto &binding : mFragmentInputBindings)
535 {
536 if (binding.second != static_cast<GLuint>(index))
537 continue;
538
539 ret.valid = true;
540
541 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400542 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300543
544 for (const auto &in : inputs)
545 {
546 if (in.name == originalName)
547 {
548 if (in.isArray())
549 {
550 // The client wants to bind either "name" or "name[0]".
551 // GL ES 3.1 spec refers to active array names with language such as:
552 // "if the string identifies the base name of an active array, where the
553 // string would exactly match the name of the variable if the suffix "[0]"
554 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400555 if (arrayIndex == GL_INVALID_INDEX)
556 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300557
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400558 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300559 }
560 else
561 {
562 ret.name = in.mappedName;
563 }
564 ret.type = in.type;
565 return ret;
566 }
567 }
568 }
569
570 return ret;
571}
572
Jamie Madillbd044ed2017-06-05 12:59:21 -0400573void Program::pathFragmentInputGen(const Context *context,
574 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300575 GLenum genMode,
576 GLint components,
577 const GLfloat *coeffs)
578{
579 // If the location is -1 then the command is silently ignored
580 if (index == -1)
581 return;
582
Jamie Madillbd044ed2017-06-05 12:59:21 -0400583 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300584
585 // If the input doesn't exist then then the command is silently ignored
586 // This could happen through optimization for example, the shader translator
587 // decides that a variable is not actually being used and optimizes it away.
588 if (binding.name.empty())
589 return;
590
591 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
592}
593
Martin Radev4c4c8e72016-08-04 12:25:34 +0300594// The attached shaders are checked for linking errors by matching up their variables.
595// Uniform, input and output variables get collected.
596// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500597Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000598{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500599 const auto &data = context->getContextState();
600
Jamie Madill6c1f6712017-02-14 19:08:04 -0500601 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000602
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000603 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000604 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000605
Martin Radev4c4c8e72016-08-04 12:25:34 +0300606 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500607
Jamie Madill192745a2016-12-22 15:58:21 -0500608 auto vertexShader = mState.mAttachedVertexShader;
609 auto fragmentShader = mState.mAttachedFragmentShader;
610 auto computeShader = mState.mAttachedComputeShader;
611
612 bool isComputeShaderAttached = (computeShader != nullptr);
613 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300614 // Check whether we both have a compute and non-compute shaders attached.
615 // If there are of both types attached, then linking should fail.
616 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
617 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500618 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300619 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
620 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400621 }
622
Jamie Madill192745a2016-12-22 15:58:21 -0500623 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500624 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400625 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300626 {
627 mInfoLog << "Attached compute shader is not compiled.";
628 return NoError();
629 }
Jamie Madill192745a2016-12-22 15:58:21 -0500630 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300631
Jamie Madillbd044ed2017-06-05 12:59:21 -0400632 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300633
634 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
635 // If the work group size is not specified, a link time error should occur.
636 if (!mState.mComputeShaderLocalSize.isDeclared())
637 {
638 mInfoLog << "Work group size is not specified.";
639 return NoError();
640 }
641
Jamie Madillbd044ed2017-06-05 12:59:21 -0400642 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300643 {
644 return NoError();
645 }
646
Jamie Madillbd044ed2017-06-05 12:59:21 -0400647 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648 {
649 return NoError();
650 }
651
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500652 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400653 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500654 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300655 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500656 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300657 }
658 }
659 else
660 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400661 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662 {
663 return NoError();
664 }
Jamie Madill192745a2016-12-22 15:58:21 -0500665 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300666
Jamie Madillbd044ed2017-06-05 12:59:21 -0400667 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300668 {
669 return NoError();
670 }
Jamie Madill192745a2016-12-22 15:58:21 -0500671 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300672
Jamie Madillbd044ed2017-06-05 12:59:21 -0400673 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300674 {
675 mInfoLog << "Fragment shader version does not match vertex shader version.";
676 return NoError();
677 }
678
Jamie Madillbd044ed2017-06-05 12:59:21 -0400679 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300680 {
681 return NoError();
682 }
683
Jamie Madillbd044ed2017-06-05 12:59:21 -0400684 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300685 {
686 return NoError();
687 }
688
Jamie Madillbd044ed2017-06-05 12:59:21 -0400689 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300690 {
691 return NoError();
692 }
693
Jamie Madillbd044ed2017-06-05 12:59:21 -0400694 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300695 {
696 return NoError();
697 }
698
Jamie Madillbd044ed2017-06-05 12:59:21 -0400699 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300700
jchen10a9042d32017-03-17 08:50:45 +0800701 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300702 {
703 return NoError();
704 }
705
Jamie Madillbd044ed2017-06-05 12:59:21 -0400706 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300707
Jamie Madill192745a2016-12-22 15:58:21 -0500708 // Validate we can pack the varyings.
709 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
710
711 // Map the varyings to the register file
712 // In WebGL, we use a slightly different handling for packing variables.
713 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
714 : PackMode::ANGLE_RELAXED;
715 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
716 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
717 mState.getTransformFeedbackVaryingNames()))
718 {
719 return NoError();
720 }
721
Jamie Madillc564c072017-06-01 12:45:42 -0400722 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500723 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500725 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300726 }
727
728 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500729 }
730
Olli Etuaho48fed632017-03-16 12:05:30 +0000731 setUniformValuesFromBindingQualifiers();
732
Jamie Madillbd044ed2017-06-05 12:59:21 -0400733 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400734
Martin Radev4c4c8e72016-08-04 12:25:34 +0300735 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000736}
737
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000738// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500739void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400741 mState.mAttributes.clear();
742 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800743 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400744 mState.mUniforms.clear();
745 mState.mUniformLocations.clear();
746 mState.mUniformBlocks.clear();
747 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800748 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400749 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400750 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300751 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500752 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500753
Geoff Lang7dd2e102014-11-10 15:19:26 -0500754 mValidated = false;
755
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000756 mLinked = false;
757}
758
Geoff Lange1a27752015-10-05 13:16:04 -0400759bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000760{
761 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
Jamie Madilla2c74982016-12-12 11:20:42 -0500764Error Program::loadBinary(const Context *context,
765 GLenum binaryFormat,
766 const void *binary,
767 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000768{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500769 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000770
Geoff Lang7dd2e102014-11-10 15:19:26 -0500771#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800772 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500773#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400774 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
775 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000776 {
Jamie Madillf6113162015-05-07 11:49:21 -0400777 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800778 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500779 }
780
Jamie Madill4f86d052017-06-05 12:59:26 -0400781 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
782 ANGLE_TRY_RESULT(
783 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500784 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500785#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500786}
787
Jamie Madilla2c74982016-12-12 11:20:42 -0500788Error Program::saveBinary(const Context *context,
789 GLenum *binaryFormat,
790 void *binary,
791 GLsizei bufSize,
792 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500793{
794 if (binaryFormat)
795 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400796 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500797 }
798
Jamie Madill4f86d052017-06-05 12:59:26 -0400799 angle::MemoryBuffer memoryBuf;
800 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500801
Jamie Madill4f86d052017-06-05 12:59:26 -0400802 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
803 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500804
805 if (streamLength > bufSize)
806 {
807 if (length)
808 {
809 *length = 0;
810 }
811
812 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
813 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
814 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500815 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500816 }
817
818 if (binary)
819 {
820 char *ptr = reinterpret_cast<char*>(binary);
821
Jamie Madill48ef11b2016-04-27 15:21:52 -0400822 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500823 ptr += streamLength;
824
825 ASSERT(ptr - streamLength == binary);
826 }
827
828 if (length)
829 {
830 *length = streamLength;
831 }
832
He Yunchaoacd18982017-01-04 10:46:42 +0800833 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500834}
835
836GLint Program::getBinaryLength() const
837{
838 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -0500839 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500840 if (error.isError())
841 {
842 return 0;
843 }
844
845 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000846}
847
Geoff Langc5629752015-12-07 16:29:04 -0500848void Program::setBinaryRetrievableHint(bool retrievable)
849{
850 // TODO(jmadill) : replace with dirty bits
851 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400852 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500853}
854
855bool Program::getBinaryRetrievableHint() const
856{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400857 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500858}
859
Yunchao He61afff12017-03-14 15:34:03 +0800860void Program::setSeparable(bool separable)
861{
862 // TODO(yunchao) : replace with dirty bits
863 if (mState.mSeparable != separable)
864 {
865 mProgram->setSeparable(separable);
866 mState.mSeparable = separable;
867 }
868}
869
870bool Program::isSeparable() const
871{
872 return mState.mSeparable;
873}
874
Jamie Madill6c1f6712017-02-14 19:08:04 -0500875void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000876{
877 mRefCount--;
878
879 if (mRefCount == 0 && mDeleteStatus)
880 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500881 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000882 }
883}
884
885void Program::addRef()
886{
887 mRefCount++;
888}
889
890unsigned int Program::getRefCount() const
891{
892 return mRefCount;
893}
894
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000895int Program::getInfoLogLength() const
896{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400897 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000898}
899
Geoff Lange1a27752015-10-05 13:16:04 -0400900void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000901{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000902 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000903}
904
Geoff Lange1a27752015-10-05 13:16:04 -0400905void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000906{
907 int total = 0;
908
Martin Radev4c4c8e72016-08-04 12:25:34 +0300909 if (mState.mAttachedComputeShader)
910 {
911 if (total < maxCount)
912 {
913 shaders[total] = mState.mAttachedComputeShader->getHandle();
914 total++;
915 }
916 }
917
Jamie Madill48ef11b2016-04-27 15:21:52 -0400918 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000919 {
920 if (total < maxCount)
921 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400922 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200923 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000924 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000925 }
926
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000928 {
929 if (total < maxCount)
930 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400931 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200932 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000933 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000934 }
935
936 if (count)
937 {
938 *count = total;
939 }
940}
941
Geoff Lange1a27752015-10-05 13:16:04 -0400942GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500943{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400944 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500945 {
jchen1036e120e2017-03-14 14:53:58 +0800946 if (attribute.name == name)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400948 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500949 }
950 }
951
Austin Kinrossb8af7232015-03-16 22:33:25 -0700952 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500953}
954
Jamie Madill63805b42015-08-25 13:17:39 -0400955bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400956{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400957 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
958 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500959}
960
jchen10fd7c3b52017-03-21 15:36:03 +0800961void Program::getActiveAttribute(GLuint index,
962 GLsizei bufsize,
963 GLsizei *length,
964 GLint *size,
965 GLenum *type,
966 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000967{
Jamie Madillc349ec02015-08-21 16:53:12 -0400968 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000969 {
970 if (bufsize > 0)
971 {
972 name[0] = '\0';
973 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500974
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000975 if (length)
976 {
977 *length = 0;
978 }
979
980 *type = GL_NONE;
981 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400982 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000983 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400984
jchen1036e120e2017-03-14 14:53:58 +0800985 ASSERT(index < mState.mAttributes.size());
986 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -0400987
988 if (bufsize > 0)
989 {
jchen10fd7c3b52017-03-21 15:36:03 +0800990 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -0400991 }
992
993 // Always a single 'type' instance
994 *size = 1;
995 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000996}
997
Geoff Lange1a27752015-10-05 13:16:04 -0400998GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000999{
Jamie Madillc349ec02015-08-21 16:53:12 -04001000 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001001 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001002 return 0;
1003 }
1004
jchen1036e120e2017-03-14 14:53:58 +08001005 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001006}
1007
Geoff Lange1a27752015-10-05 13:16:04 -04001008GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001009{
Jamie Madillc349ec02015-08-21 16:53:12 -04001010 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001011 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001012 return 0;
1013 }
1014
1015 size_t maxLength = 0;
1016
Jamie Madill48ef11b2016-04-27 15:21:52 -04001017 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001018 {
jchen1036e120e2017-03-14 14:53:58 +08001019 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001020 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001021
Jamie Madillc349ec02015-08-21 16:53:12 -04001022 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023}
1024
jchen1015015f72017-03-16 13:54:21 +08001025GLuint Program::getInputResourceIndex(const GLchar *name) const
1026{
1027 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1028 {
1029 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1030 if (attribute.name == name)
1031 {
1032 return attributeIndex;
1033 }
1034 }
1035 return GL_INVALID_INDEX;
1036}
1037
1038GLuint Program::getOutputResourceIndex(const GLchar *name) const
1039{
1040 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1041}
1042
jchen10fd7c3b52017-03-21 15:36:03 +08001043size_t Program::getOutputResourceCount() const
1044{
1045 return (mLinked ? mState.mOutputVariables.size() : 0);
1046}
1047
1048void Program::getInputResourceName(GLuint index,
1049 GLsizei bufSize,
1050 GLsizei *length,
1051 GLchar *name) const
1052{
1053 GLint size;
1054 GLenum type;
1055 getActiveAttribute(index, bufSize, length, &size, &type, name);
1056}
1057
1058void Program::getOutputResourceName(GLuint index,
1059 GLsizei bufSize,
1060 GLsizei *length,
1061 GLchar *name) const
1062{
1063 if (length)
1064 {
1065 *length = 0;
1066 }
1067
1068 if (!mLinked)
1069 {
1070 if (bufSize > 0)
1071 {
1072 name[0] = '\0';
1073 }
1074 return;
1075 }
1076 ASSERT(index < mState.mOutputVariables.size());
1077 const auto &output = mState.mOutputVariables[index];
1078
1079 if (bufSize > 0)
1080 {
1081 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1082
1083 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1084 }
1085}
1086
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087GLint Program::getFragDataLocation(const std::string &name) const
1088{
1089 std::string baseName(name);
1090 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001091 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001093 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001094 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1095 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001096 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001098 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001100}
1101
Geoff Lange1a27752015-10-05 13:16:04 -04001102void Program::getActiveUniform(GLuint index,
1103 GLsizei bufsize,
1104 GLsizei *length,
1105 GLint *size,
1106 GLenum *type,
1107 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001110 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001111 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001112 ASSERT(index < mState.mUniforms.size());
1113 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114
1115 if (bufsize > 0)
1116 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001117 std::string string = uniform.name;
1118 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001119 {
1120 string += "[0]";
1121 }
jchen10fd7c3b52017-03-21 15:36:03 +08001122 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123 }
1124
Jamie Madill62d31cb2015-09-11 13:25:51 -04001125 *size = uniform.elementCount();
1126 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001127 }
1128 else
1129 {
1130 if (bufsize > 0)
1131 {
1132 name[0] = '\0';
1133 }
1134
1135 if (length)
1136 {
1137 *length = 0;
1138 }
1139
1140 *size = 0;
1141 *type = GL_NONE;
1142 }
1143}
1144
Geoff Lange1a27752015-10-05 13:16:04 -04001145GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001146{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001148 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001149 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001150 }
1151 else
1152 {
1153 return 0;
1154 }
1155}
1156
Geoff Lange1a27752015-10-05 13:16:04 -04001157GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001158{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001159 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001160
1161 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001162 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001163 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001164 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001165 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001167 size_t length = uniform.name.length() + 1u;
1168 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 {
1170 length += 3; // Counting in "[0]".
1171 }
1172 maxLength = std::max(length, maxLength);
1173 }
1174 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001175 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001176
Jamie Madill62d31cb2015-09-11 13:25:51 -04001177 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178}
1179
1180GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1181{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001182 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001183 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001184 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001185 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1187 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1188 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1189 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1190 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1191 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1192 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1193 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1194 default:
1195 UNREACHABLE();
1196 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001197 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 return 0;
1199}
1200
1201bool Program::isValidUniformLocation(GLint location) const
1202{
Jamie Madille2e406c2016-06-02 13:04:10 -04001203 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001204 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1205 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001206}
1207
Jamie Madill62d31cb2015-09-11 13:25:51 -04001208const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001209{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001210 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001211 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001212}
1213
Jamie Madillac4e9c32017-01-13 14:07:12 -05001214const VariableLocation &Program::getUniformLocation(GLint location) const
1215{
1216 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1217 return mState.mUniformLocations[location];
1218}
1219
1220const std::vector<VariableLocation> &Program::getUniformLocations() const
1221{
1222 return mState.mUniformLocations;
1223}
1224
1225const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1226{
1227 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1228 return mState.mUniforms[index];
1229}
1230
Jamie Madill62d31cb2015-09-11 13:25:51 -04001231GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001233 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234}
1235
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237{
Jamie Madille7d84322017-01-10 18:21:59 -05001238 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001239}
1240
1241void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1242{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001243 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1244 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245}
1246
1247void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1248{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001249 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1250 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251}
1252
1253void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1254{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001255 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1256 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257}
1258
1259void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1260{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001261 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1262 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001263}
1264
1265void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1266{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001267 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1268 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269}
1270
1271void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1272{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001273 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1274 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001275}
1276
1277void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1278{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001279 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1280 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001281}
1282
1283void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1284{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001285 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1286 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287}
1288
1289void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1290{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001291 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1292 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001293}
1294
1295void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1296{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001297 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1298 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001299}
1300
1301void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1302{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001303 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1304 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305}
1306
1307void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1308{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001309 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1310 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001311}
1312
1313void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1314{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001315 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1316 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001317}
1318
1319void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1320{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001321 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1322 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001323}
1324
1325void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1326{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001327 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1328 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329}
1330
1331void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1332{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001333 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1334 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335}
1336
1337void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1338{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001339 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1340 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001341}
1342
1343void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1344{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001345 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1346 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347}
1348
1349void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1350{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001351 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1352 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
1355void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1356{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001357 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1358 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359}
1360
1361void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1362{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001363 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1364 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365}
1366
Geoff Lange1a27752015-10-05 13:16:04 -04001367void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001369 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001370}
1371
Geoff Lange1a27752015-10-05 13:16:04 -04001372void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001374 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375}
1376
Geoff Lange1a27752015-10-05 13:16:04 -04001377void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001379 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380}
1381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001382void Program::flagForDeletion()
1383{
1384 mDeleteStatus = true;
1385}
1386
1387bool Program::isFlaggedForDeletion() const
1388{
1389 return mDeleteStatus;
1390}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001391
Brandon Jones43a53e22014-08-28 16:23:22 -07001392void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001393{
1394 mInfoLog.reset();
1395
Geoff Lang7dd2e102014-11-10 15:19:26 -05001396 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001397 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001398 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001399 }
1400 else
1401 {
Jamie Madillf6113162015-05-07 11:49:21 -04001402 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001403 }
1404}
1405
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1407{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001408 // Skip cache if we're using an infolog, so we get the full error.
1409 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1410 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1411 {
1412 return mCachedValidateSamplersResult.value();
1413 }
1414
1415 if (mTextureUnitTypesCache.empty())
1416 {
1417 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1418 }
1419 else
1420 {
1421 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1422 }
1423
1424 // if any two active samplers in a program are of different types, but refer to the same
1425 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1426 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001427 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001428 {
Jamie Madille7d84322017-01-10 18:21:59 -05001429 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001430
Jamie Madille7d84322017-01-10 18:21:59 -05001431 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001432 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001433 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1434 {
1435 if (infoLog)
1436 {
1437 (*infoLog) << "Sampler uniform (" << textureUnit
1438 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1439 << caps.maxCombinedTextureImageUnits << ")";
1440 }
1441
1442 mCachedValidateSamplersResult = false;
1443 return false;
1444 }
1445
1446 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1447 {
1448 if (textureType != mTextureUnitTypesCache[textureUnit])
1449 {
1450 if (infoLog)
1451 {
1452 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1453 "image unit ("
1454 << textureUnit << ").";
1455 }
1456
1457 mCachedValidateSamplersResult = false;
1458 return false;
1459 }
1460 }
1461 else
1462 {
1463 mTextureUnitTypesCache[textureUnit] = textureType;
1464 }
1465 }
1466 }
1467
1468 mCachedValidateSamplersResult = true;
1469 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470}
1471
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001472bool Program::isValidated() const
1473{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474 return mValidated;
1475}
1476
Geoff Lange1a27752015-10-05 13:16:04 -04001477GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001479 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
1482void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1483{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001484 ASSERT(
1485 uniformBlockIndex <
1486 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487
Jamie Madill48ef11b2016-04-27 15:21:52 -04001488 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489
1490 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001491 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 std::string string = uniformBlock.name;
1493
Jamie Madill62d31cb2015-09-11 13:25:51 -04001494 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497 }
jchen10fd7c3b52017-03-21 15:36:03 +08001498 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001499 }
1500}
1501
Geoff Lange1a27752015-10-05 13:16:04 -04001502GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001503{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504 int maxLength = 0;
1505
1506 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001507 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001508 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1510 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001511 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001512 if (!uniformBlock.name.empty())
1513 {
jchen10af713a22017-04-19 09:10:56 +08001514 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1515 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001516 }
1517 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001518 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001519
1520 return maxLength;
1521}
1522
Geoff Lange1a27752015-10-05 13:16:04 -04001523GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001525 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001526 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001527
Jamie Madill48ef11b2016-04-27 15:21:52 -04001528 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001529 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1530 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001531 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532 if (uniformBlock.name == baseName)
1533 {
1534 const bool arrayElementZero =
1535 (subscript == GL_INVALID_INDEX &&
1536 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1537 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1538 {
1539 return blockIndex;
1540 }
1541 }
1542 }
1543
1544 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001545}
1546
Jamie Madill62d31cb2015-09-11 13:25:51 -04001547const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001548{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001549 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1550 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001551}
1552
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001553void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1554{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001555 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001556 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001557 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001558}
1559
1560GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1561{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001562 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001563}
1564
1565void Program::resetUniformBlockBindings()
1566{
1567 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1568 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001569 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001570 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001571 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001572}
1573
Geoff Lang48dcae72014-02-05 16:28:24 -05001574void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1575{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001576 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001577 for (GLsizei i = 0; i < count; i++)
1578 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001579 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001580 }
1581
Jamie Madill48ef11b2016-04-27 15:21:52 -04001582 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001583}
1584
1585void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1586{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001588 {
jchen10a9042d32017-03-17 08:50:45 +08001589 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1590 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1591 std::string varName = var.nameWithArrayIndex();
1592 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001593 if (length)
1594 {
1595 *length = lastNameIdx;
1596 }
1597 if (size)
1598 {
jchen10a9042d32017-03-17 08:50:45 +08001599 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001600 }
1601 if (type)
1602 {
jchen10a9042d32017-03-17 08:50:45 +08001603 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001604 }
1605 if (name)
1606 {
jchen10a9042d32017-03-17 08:50:45 +08001607 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001608 name[lastNameIdx] = '\0';
1609 }
1610 }
1611}
1612
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001613GLsizei Program::getTransformFeedbackVaryingCount() const
1614{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001615 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001616 {
jchen10a9042d32017-03-17 08:50:45 +08001617 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001618 }
1619 else
1620 {
1621 return 0;
1622 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001623}
1624
1625GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1626{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001628 {
1629 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001630 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001631 {
jchen10a9042d32017-03-17 08:50:45 +08001632 maxSize =
1633 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001634 }
1635
1636 return maxSize;
1637 }
1638 else
1639 {
1640 return 0;
1641 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001642}
1643
1644GLenum Program::getTransformFeedbackBufferMode() const
1645{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001646 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001647}
1648
Jamie Madillbd044ed2017-06-05 12:59:21 -04001649bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001651 Shader *vertexShader = mState.mAttachedVertexShader;
1652 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001653
Jamie Madillbd044ed2017-06-05 12:59:21 -04001654 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001655
Jamie Madillbd044ed2017-06-05 12:59:21 -04001656 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1657 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658
Sami Väisänen46eaa942016-06-29 10:26:37 +03001659 std::map<GLuint, std::string> staticFragmentInputLocations;
1660
Jamie Madill4cff2472015-08-21 16:53:18 -04001661 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663 bool matched = false;
1664
1665 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001666 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 {
1668 continue;
1669 }
1670
Jamie Madill4cff2472015-08-21 16:53:18 -04001671 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001673 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001675 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001676 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001677 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678 {
1679 return false;
1680 }
1681
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682 matched = true;
1683 break;
1684 }
1685 }
1686
1687 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001688 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001690 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001691 return false;
1692 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001693
1694 // Check for aliased path rendering input bindings (if any).
1695 // If more than one binding refer statically to the same
1696 // location the link must fail.
1697
1698 if (!output.staticUse)
1699 continue;
1700
1701 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1702 if (inputBinding == -1)
1703 continue;
1704
1705 const auto it = staticFragmentInputLocations.find(inputBinding);
1706 if (it == std::end(staticFragmentInputLocations))
1707 {
1708 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1709 }
1710 else
1711 {
1712 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1713 << it->second;
1714 return false;
1715 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001716 }
1717
Jamie Madillbd044ed2017-06-05 12:59:21 -04001718 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001719 {
1720 return false;
1721 }
1722
Jamie Madillada9ecc2015-08-17 12:53:37 -04001723 // TODO(jmadill): verify no unmatched vertex varyings?
1724
Geoff Lang7dd2e102014-11-10 15:19:26 -05001725 return true;
1726}
1727
Jamie Madillbd044ed2017-06-05 12:59:21 -04001728bool Program::linkUniforms(const Context *context,
1729 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001730 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001731{
Olli Etuahob78707c2017-03-09 15:03:11 +00001732 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001733 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001734 {
1735 return false;
1736 }
1737
Olli Etuahob78707c2017-03-09 15:03:11 +00001738 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001739
Olli Etuaho48fed632017-03-16 12:05:30 +00001740 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001741
1742 return true;
1743}
1744
Olli Etuaho48fed632017-03-16 12:05:30 +00001745void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001746{
Jamie Madill982f6e02017-06-07 14:33:04 -04001747 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1748 unsigned int low = high;
1749
1750 for (auto samplerIter = mState.mUniforms.rbegin();
1751 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001752 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001753 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001754 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001755
1756 mState.mSamplerUniformRange = RangeUI(low, high);
1757
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001758 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001759 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001760 {
1761 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1762 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1763 mState.mSamplerBindings.emplace_back(
1764 SamplerBinding(textureType, samplerUniform.elementCount()));
1765 }
1766}
1767
Martin Radev4c4c8e72016-08-04 12:25:34 +03001768bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1769 const std::string &uniformName,
1770 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001771 const sh::InterfaceBlockField &fragmentUniform,
1772 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001774 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1775 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1776 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777 {
1778 return false;
1779 }
1780
1781 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1782 {
Jamie Madillf6113162015-05-07 11:49:21 -04001783 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784 return false;
1785 }
1786
1787 return true;
1788}
1789
Jamie Madilleb979bf2016-11-15 12:28:46 -05001790// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001791bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001793 const ContextState &data = context->getContextState();
1794 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001795
Geoff Lang7dd2e102014-11-10 15:19:26 -05001796 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001797 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001798 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001799
1800 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001801 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001802 {
Jamie Madillf6113162015-05-07 11:49:21 -04001803 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001804 return false;
1805 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001807 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001808
Jamie Madillc349ec02015-08-21 16:53:12 -04001809 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001810 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001811 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001812 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001813 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001814 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001815 attribute.location = bindingLocation;
1816 }
1817
1818 if (attribute.location != -1)
1819 {
1820 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001821 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822
Jamie Madill63805b42015-08-25 13:17:39 -04001823 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824 {
Jamie Madillf6113162015-05-07 11:49:21 -04001825 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001826 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827
1828 return false;
1829 }
1830
Jamie Madill63805b42015-08-25 13:17:39 -04001831 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 {
Jamie Madill63805b42015-08-25 13:17:39 -04001833 const int regLocation = attribute.location + reg;
1834 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001835
1836 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001837 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001838 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001840 // TODO(jmadill): fix aliasing on ES2
1841 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001843 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001844 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001845 return false;
1846 }
1847 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001848 else
1849 {
Jamie Madill63805b42015-08-25 13:17:39 -04001850 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001851 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852
Jamie Madill63805b42015-08-25 13:17:39 -04001853 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 }
1855 }
1856 }
1857
1858 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001859 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001860 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001861 // Not set by glBindAttribLocation or by location layout qualifier
1862 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001863 {
Jamie Madill63805b42015-08-25 13:17:39 -04001864 int regs = VariableRegisterCount(attribute.type);
1865 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001866
Jamie Madill63805b42015-08-25 13:17:39 -04001867 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 {
Jamie Madillf6113162015-05-07 11:49:21 -04001869 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001870 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 }
1872
Jamie Madillc349ec02015-08-21 16:53:12 -04001873 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 }
1875 }
1876
Jamie Madill48ef11b2016-04-27 15:21:52 -04001877 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 {
Jamie Madill63805b42015-08-25 13:17:39 -04001879 ASSERT(attribute.location != -1);
1880 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001881
Jamie Madill63805b42015-08-25 13:17:39 -04001882 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001884 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885 }
1886 }
1887
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 return true;
1889}
1890
Martin Radev4c4c8e72016-08-04 12:25:34 +03001891bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1892 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1893 const std::string &errorMessage,
1894 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001896 GLuint blockCount = 0;
1897 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001899 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04001900 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001901 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04001902 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001903 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04001904 return false;
1905 }
1906 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001908 return true;
1909}
Jamie Madille473dee2015-08-18 14:49:01 -04001910
Martin Radev4c4c8e72016-08-04 12:25:34 +03001911bool Program::validateVertexAndFragmentInterfaceBlocks(
1912 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
1913 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001914 InfoLog &infoLog,
1915 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03001916{
1917 // Check that interface blocks defined in the vertex and fragment shaders are identical
1918 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
1919 UniformBlockMap linkedUniformBlocks;
1920
1921 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
1922 {
1923 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
1924 }
1925
Jamie Madille473dee2015-08-18 14:49:01 -04001926 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 {
Jamie Madille473dee2015-08-18 14:49:01 -04001928 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929 if (entry != linkedUniformBlocks.end())
1930 {
1931 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04001932 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
1933 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 {
1935 return false;
1936 }
1937 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001938 }
1939 return true;
1940}
Jamie Madille473dee2015-08-18 14:49:01 -04001941
Jamie Madillbd044ed2017-06-05 12:59:21 -04001942bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001943{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001944 const auto &caps = context->getCaps();
1945
Martin Radev4c4c8e72016-08-04 12:25:34 +03001946 if (mState.mAttachedComputeShader)
1947 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001948 Shader &computeShader = *mState.mAttachedComputeShader;
1949 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001950
1951 if (!validateUniformBlocksCount(
1952 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
1953 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
1954 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001956 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001958 return true;
1959 }
1960
Jamie Madillbd044ed2017-06-05 12:59:21 -04001961 Shader &vertexShader = *mState.mAttachedVertexShader;
1962 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001963
Jamie Madillbd044ed2017-06-05 12:59:21 -04001964 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
1965 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001966
1967 if (!validateUniformBlocksCount(
1968 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
1969 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
1970 {
1971 return false;
1972 }
1973 if (!validateUniformBlocksCount(
1974 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
1975 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
1976 infoLog))
1977 {
1978
1979 return false;
1980 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001981
1982 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001983 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001984 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001985 {
1986 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 }
Jamie Madille473dee2015-08-18 14:49:01 -04001988
Geoff Lang7dd2e102014-11-10 15:19:26 -05001989 return true;
1990}
1991
Jamie Madilla2c74982016-12-12 11:20:42 -05001992bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03001993 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001994 const sh::InterfaceBlock &fragmentInterfaceBlock,
1995 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996{
1997 const char* blockName = vertexInterfaceBlock.name.c_str();
1998 // validate blocks for the same member types
1999 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2000 {
Jamie Madillf6113162015-05-07 11:49:21 -04002001 infoLog << "Types for interface block '" << blockName
2002 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 return false;
2004 }
2005 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2006 {
Jamie Madillf6113162015-05-07 11:49:21 -04002007 infoLog << "Array sizes differ for interface block '" << blockName
2008 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009 return false;
2010 }
jchen10af713a22017-04-19 09:10:56 +08002011 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2012 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2013 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 {
Jamie Madillf6113162015-05-07 11:49:21 -04002015 infoLog << "Layout qualifiers differ for interface block '" << blockName
2016 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002017 return false;
2018 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002019 const unsigned int numBlockMembers =
2020 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2022 {
2023 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2024 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2025 if (vertexMember.name != fragmentMember.name)
2026 {
Jamie Madillf6113162015-05-07 11:49:21 -04002027 infoLog << "Name mismatch for field " << blockMemberIndex
2028 << " of interface block '" << blockName
2029 << "': (in vertex: '" << vertexMember.name
2030 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031 return false;
2032 }
2033 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002034 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2035 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 {
2037 return false;
2038 }
2039 }
2040 return true;
2041}
2042
2043bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2044 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2045{
2046 if (vertexVariable.type != fragmentVariable.type)
2047 {
Jamie Madillf6113162015-05-07 11:49:21 -04002048 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049 return false;
2050 }
2051 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2052 {
Jamie Madillf6113162015-05-07 11:49:21 -04002053 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054 return false;
2055 }
2056 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2057 {
Jamie Madillf6113162015-05-07 11:49:21 -04002058 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059 return false;
2060 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002061 if (vertexVariable.structName != fragmentVariable.structName)
2062 {
2063 infoLog << "Structure names for " << variableName
2064 << " differ between vertex and fragment shaders";
2065 return false;
2066 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067
2068 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2069 {
Jamie Madillf6113162015-05-07 11:49:21 -04002070 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 return false;
2072 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002073 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2075 {
2076 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2077 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2078
2079 if (vertexMember.name != fragmentMember.name)
2080 {
Jamie Madillf6113162015-05-07 11:49:21 -04002081 infoLog << "Name mismatch for field '" << memberIndex
2082 << "' of " << variableName
2083 << ": (in vertex: '" << vertexMember.name
2084 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 return false;
2086 }
2087
2088 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2089 vertexMember.name + "'";
2090
2091 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2092 {
2093 return false;
2094 }
2095 }
2096
2097 return true;
2098}
2099
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002100bool Program::linkValidateVaryings(InfoLog &infoLog,
2101 const std::string &varyingName,
2102 const sh::Varying &vertexVarying,
2103 const sh::Varying &fragmentVarying,
2104 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105{
2106 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2107 {
2108 return false;
2109 }
2110
Jamie Madille9cc4692015-02-19 16:00:13 -05002111 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002113 infoLog << "Interpolation types for " << varyingName
2114 << " differ between vertex and fragment shaders.";
2115 return false;
2116 }
2117
2118 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2119 {
2120 infoLog << "Invariance for " << varyingName
2121 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122 return false;
2123 }
2124
2125 return true;
2126}
2127
Jamie Madillbd044ed2017-06-05 12:59:21 -04002128bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002129{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002130 Shader *vertexShader = mState.mAttachedVertexShader;
2131 Shader *fragmentShader = mState.mAttachedFragmentShader;
2132 const auto &vertexVaryings = vertexShader->getVaryings(context);
2133 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2134 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002135
2136 if (shaderVersion != 100)
2137 {
2138 // Only ESSL 1.0 has restrictions on matching input and output invariance
2139 return true;
2140 }
2141
2142 bool glPositionIsInvariant = false;
2143 bool glPointSizeIsInvariant = false;
2144 bool glFragCoordIsInvariant = false;
2145 bool glPointCoordIsInvariant = false;
2146
2147 for (const sh::Varying &varying : vertexVaryings)
2148 {
2149 if (!varying.isBuiltIn())
2150 {
2151 continue;
2152 }
2153 if (varying.name.compare("gl_Position") == 0)
2154 {
2155 glPositionIsInvariant = varying.isInvariant;
2156 }
2157 else if (varying.name.compare("gl_PointSize") == 0)
2158 {
2159 glPointSizeIsInvariant = varying.isInvariant;
2160 }
2161 }
2162
2163 for (const sh::Varying &varying : fragmentVaryings)
2164 {
2165 if (!varying.isBuiltIn())
2166 {
2167 continue;
2168 }
2169 if (varying.name.compare("gl_FragCoord") == 0)
2170 {
2171 glFragCoordIsInvariant = varying.isInvariant;
2172 }
2173 else if (varying.name.compare("gl_PointCoord") == 0)
2174 {
2175 glPointCoordIsInvariant = varying.isInvariant;
2176 }
2177 }
2178
2179 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2180 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2181 // Not requiring invariance to match is supported by:
2182 // dEQP, WebGL CTS, Nexus 5X GLES
2183 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2184 {
2185 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2186 "declared invariant.";
2187 return false;
2188 }
2189 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2190 {
2191 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2192 "declared invariant.";
2193 return false;
2194 }
2195
2196 return true;
2197}
2198
jchen10a9042d32017-03-17 08:50:45 +08002199bool Program::linkValidateTransformFeedback(const gl::Context *context,
2200 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002201 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002202 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203{
2204 size_t totalComponents = 0;
2205
Jamie Madillccdf74b2015-08-18 10:46:12 -04002206 std::set<std::string> uniqueNames;
2207
Jamie Madill48ef11b2016-04-27 15:21:52 -04002208 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209 {
2210 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002211 size_t subscript = GL_INVALID_INDEX;
2212 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2213
Jamie Madill192745a2016-12-22 15:58:21 -05002214 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002215 {
Jamie Madill192745a2016-12-22 15:58:21 -05002216 const sh::Varying *varying = ref.second.get();
2217
jchen10a9042d32017-03-17 08:50:45 +08002218 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002219 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002220 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002222 infoLog << "Two transform feedback varyings specify the same output variable ("
2223 << tfVaryingName << ").";
2224 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002225 }
jchen10a9042d32017-03-17 08:50:45 +08002226 if (context->getClientVersion() >= Version(3, 1))
2227 {
2228 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2229 {
2230 infoLog
2231 << "Two transform feedback varyings include the same array element ("
2232 << tfVaryingName << ").";
2233 return false;
2234 }
2235 }
2236 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002237 {
2238 infoLog << "Capture of arrays is undefined and not supported.";
2239 return false;
2240 }
2241
jchen10a9042d32017-03-17 08:50:45 +08002242 uniqueNames.insert(tfVaryingName);
2243
Jamie Madillccdf74b2015-08-18 10:46:12 -04002244 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002245 size_t elementCount =
2246 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2247 : 1);
2248 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002249 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002250 componentCount > caps.maxTransformFeedbackSeparateComponents)
2251 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002252 infoLog << "Transform feedback varying's " << varying->name << " components ("
2253 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002254 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255 return false;
2256 }
2257
2258 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 found = true;
2260 break;
2261 }
2262 }
jchen10a9042d32017-03-17 08:50:45 +08002263 if (context->getClientVersion() < Version(3, 1) &&
2264 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002265 {
Geoff Lang1a683462015-09-29 15:09:59 -04002266 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002267 return false;
2268 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2270 ASSERT(found);
2271 }
2272
Jamie Madill48ef11b2016-04-27 15:21:52 -04002273 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002274 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002275 {
Jamie Madillf6113162015-05-07 11:49:21 -04002276 infoLog << "Transform feedback varying total components (" << totalComponents
2277 << ") exceed the maximum interleaved components ("
2278 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002279 return false;
2280 }
2281
2282 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002283}
2284
Jamie Madill192745a2016-12-22 15:58:21 -05002285void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002286{
2287 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002288 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002289 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002290 {
jchen10a9042d32017-03-17 08:50:45 +08002291 size_t subscript = GL_INVALID_INDEX;
2292 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002293 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002294 {
Jamie Madill192745a2016-12-22 15:58:21 -05002295 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002296 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002297 {
jchen10a9042d32017-03-17 08:50:45 +08002298 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2299 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002300 break;
2301 }
2302 }
2303 }
2304}
2305
Jamie Madillbd044ed2017-06-05 12:59:21 -04002306Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002307{
Jamie Madill192745a2016-12-22 15:58:21 -05002308 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002309
Jamie Madillbd044ed2017-06-05 12:59:21 -04002310 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002311 {
Jamie Madill192745a2016-12-22 15:58:21 -05002312 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002313 }
2314
Jamie Madillbd044ed2017-06-05 12:59:21 -04002315 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002316 {
Jamie Madill192745a2016-12-22 15:58:21 -05002317 merged[varying.name].fragment = &varying;
2318 }
2319
2320 return merged;
2321}
2322
2323std::vector<PackedVarying> Program::getPackedVaryings(
2324 const Program::MergedVaryings &mergedVaryings) const
2325{
2326 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2327 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002328 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002329
2330 for (const auto &ref : mergedVaryings)
2331 {
2332 const sh::Varying *input = ref.second.vertex;
2333 const sh::Varying *output = ref.second.fragment;
2334
2335 // Only pack varyings that have a matched input or output, plus special builtins.
2336 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002337 {
Jamie Madill192745a2016-12-22 15:58:21 -05002338 // Will get the vertex shader interpolation by default.
2339 auto interpolation = ref.second.get()->interpolation;
2340
2341 // Interpolation qualifiers must match.
2342 if (output->isStruct())
2343 {
2344 ASSERT(!output->isArray());
2345 for (const auto &field : output->fields)
2346 {
2347 ASSERT(!field.isStruct() && !field.isArray());
2348 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2349 }
2350 }
2351 else
2352 {
2353 packedVaryings.push_back(PackedVarying(*output, interpolation));
2354 }
2355 continue;
2356 }
2357
2358 // Keep Transform FB varyings in the merged list always.
2359 if (!input)
2360 {
2361 continue;
2362 }
2363
2364 for (const std::string &tfVarying : tfVaryings)
2365 {
jchen10a9042d32017-03-17 08:50:45 +08002366 size_t subscript = GL_INVALID_INDEX;
2367 std::string baseName = ParseResourceName(tfVarying, &subscript);
2368 if (uniqueFullNames.count(tfVarying) > 0)
2369 {
2370 continue;
2371 }
2372 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002373 {
2374 // Transform feedback for varying structs is underspecified.
2375 // See Khronos bug 9856.
2376 // TODO(jmadill): Figure out how to be spec-compliant here.
2377 if (!input->isStruct())
2378 {
2379 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2380 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002381 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2382 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002383 }
jchen10a9042d32017-03-17 08:50:45 +08002384 if (subscript == GL_INVALID_INDEX)
2385 {
2386 break;
2387 }
Jamie Madill192745a2016-12-22 15:58:21 -05002388 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002389 }
2390 }
2391
Jamie Madill192745a2016-12-22 15:58:21 -05002392 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2393
2394 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002395}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002396
Jamie Madillbd044ed2017-06-05 12:59:21 -04002397void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002398{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002399 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002400 ASSERT(fragmentShader != nullptr);
2401
Geoff Lange0cff192017-05-30 13:04:56 -04002402 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002403 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002404
2405 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002406 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002407 {
2408 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2409 outputVariable.name != "gl_FragData")
2410 {
2411 continue;
2412 }
2413
2414 unsigned int baseLocation =
2415 (outputVariable.location == -1 ? 0u
2416 : static_cast<unsigned int>(outputVariable.location));
2417 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2418 elementIndex++)
2419 {
2420 const unsigned int location = baseLocation + elementIndex;
2421 if (location >= mState.mOutputVariableTypes.size())
2422 {
2423 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2424 }
Corentin Walleze7557742017-06-01 13:09:57 -04002425 ASSERT(location < mState.mActiveOutputVariables.size());
2426 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002427 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2428 }
2429 }
2430
Jamie Madill80a6fc02015-08-21 16:53:16 -04002431 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002432 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002433 return;
2434
Jamie Madillbd044ed2017-06-05 12:59:21 -04002435 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002436 // TODO(jmadill): any caps validation here?
2437
jchen1015015f72017-03-16 13:54:21 +08002438 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002439 outputVariableIndex++)
2440 {
jchen1015015f72017-03-16 13:54:21 +08002441 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002442
2443 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2444 if (outputVariable.isBuiltIn())
2445 continue;
2446
2447 // Since multiple output locations must be specified, use 0 for non-specified locations.
2448 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2449
Jamie Madill80a6fc02015-08-21 16:53:16 -04002450 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2451 elementIndex++)
2452 {
2453 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002454 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002455 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002456 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002457 VariableLocation(outputVariable.name, element, outputVariableIndex);
2458 }
2459 }
2460}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002461
Olli Etuaho48fed632017-03-16 12:05:30 +00002462void Program::setUniformValuesFromBindingQualifiers()
2463{
Jamie Madill982f6e02017-06-07 14:33:04 -04002464 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002465 {
2466 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2467 if (samplerUniform.binding != -1)
2468 {
2469 GLint location = mState.getUniformLocation(samplerUniform.name);
2470 ASSERT(location != -1);
2471 std::vector<GLint> boundTextureUnits;
2472 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2473 ++elementIndex)
2474 {
2475 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2476 }
2477 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2478 boundTextureUnits.data());
2479 }
2480 }
2481}
2482
Jamie Madillbd044ed2017-06-05 12:59:21 -04002483void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002484{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002485 ASSERT(mState.mUniformBlocks.empty());
2486
2487 if (mState.mAttachedComputeShader)
2488 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002489 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002490
Jamie Madillbd044ed2017-06-05 12:59:21 -04002491 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002492 {
2493
2494 // Only 'packed' blocks are allowed to be considered inactive.
2495 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2496 continue;
2497
Jamie Madilla2c74982016-12-12 11:20:42 -05002498 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002499 {
2500 if (block.name == computeBlock.name)
2501 {
2502 block.computeStaticUse = computeBlock.staticUse;
2503 }
2504 }
2505
2506 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2507 }
2508 return;
2509 }
2510
Jamie Madill62d31cb2015-09-11 13:25:51 -04002511 std::set<std::string> visitedList;
2512
Jamie Madillbd044ed2017-06-05 12:59:21 -04002513 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002514
Jamie Madillbd044ed2017-06-05 12:59:21 -04002515 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002516 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002517 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002518 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2519 continue;
2520
2521 if (visitedList.count(vertexBlock.name) > 0)
2522 continue;
2523
2524 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2525 visitedList.insert(vertexBlock.name);
2526 }
2527
Jamie Madillbd044ed2017-06-05 12:59:21 -04002528 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002529
Jamie Madillbd044ed2017-06-05 12:59:21 -04002530 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002531 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002532 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002533 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2534 continue;
2535
2536 if (visitedList.count(fragmentBlock.name) > 0)
2537 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002538 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002539 {
2540 if (block.name == fragmentBlock.name)
2541 {
2542 block.fragmentStaticUse = fragmentBlock.staticUse;
2543 }
2544 }
2545
2546 continue;
2547 }
2548
2549 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2550 visitedList.insert(fragmentBlock.name);
2551 }
jchen10af713a22017-04-19 09:10:56 +08002552 // Set initial bindings from shader.
2553 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2554 {
2555 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2556 bindUniformBlock(blockIndex, uniformBlock.binding);
2557 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002558}
2559
Jamie Madill4a3c2342015-10-08 12:58:45 -04002560template <typename VarT>
2561void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2562 const std::string &prefix,
2563 int blockIndex)
2564{
2565 for (const VarT &field : fields)
2566 {
2567 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2568
2569 if (field.isStruct())
2570 {
2571 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2572 {
2573 const std::string uniformElementName =
2574 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2575 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2576 }
2577 }
2578 else
2579 {
2580 // If getBlockMemberInfo returns false, the uniform is optimized out.
2581 sh::BlockMemberInfo memberInfo;
2582 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2583 {
2584 continue;
2585 }
2586
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002587 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002588 blockIndex, memberInfo);
2589
2590 // Since block uniforms have no location, we don't need to store them in the uniform
2591 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002592 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002593 }
2594 }
2595}
2596
Jamie Madill62d31cb2015-09-11 13:25:51 -04002597void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2598{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002599 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002600 size_t blockSize = 0;
2601
Jamie Madill4a3c2342015-10-08 12:58:45 -04002602 // Track the first and last uniform index to determine the range of active uniforms in the
2603 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002604 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002605 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002606 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002607
2608 std::vector<unsigned int> blockUniformIndexes;
2609 for (size_t blockUniformIndex = firstBlockUniformIndex;
2610 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2611 {
2612 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2613 }
jchen10af713a22017-04-19 09:10:56 +08002614 // ESSL 3.10 section 4.4.4 page 58:
2615 // Any uniform or shader storage block declared without a binding qualifier is initially
2616 // assigned to block binding point zero.
2617 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002618 if (interfaceBlock.arraySize > 0)
2619 {
2620 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2621 {
jchen10af713a22017-04-19 09:10:56 +08002622 // Don't define this block at all if it's not active in the implementation.
2623 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2624 &blockSize))
2625 {
2626 continue;
2627 }
2628 UniformBlock block(interfaceBlock.name, true, arrayElement,
2629 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002630 block.memberUniformIndexes = blockUniformIndexes;
2631
Martin Radev4c4c8e72016-08-04 12:25:34 +03002632 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002633 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002634 case GL_VERTEX_SHADER:
2635 {
2636 block.vertexStaticUse = interfaceBlock.staticUse;
2637 break;
2638 }
2639 case GL_FRAGMENT_SHADER:
2640 {
2641 block.fragmentStaticUse = interfaceBlock.staticUse;
2642 break;
2643 }
2644 case GL_COMPUTE_SHADER:
2645 {
2646 block.computeStaticUse = interfaceBlock.staticUse;
2647 break;
2648 }
2649 default:
2650 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002651 }
2652
Qin Jiajia0350a642016-11-01 17:01:51 +08002653 // Since all block elements in an array share the same active uniforms, they will all be
2654 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2655 // here we will add every block element in the array.
2656 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002657 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002658 }
2659 }
2660 else
2661 {
jchen10af713a22017-04-19 09:10:56 +08002662 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2663 {
2664 return;
2665 }
2666 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002667 block.memberUniformIndexes = blockUniformIndexes;
2668
Martin Radev4c4c8e72016-08-04 12:25:34 +03002669 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002670 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002671 case GL_VERTEX_SHADER:
2672 {
2673 block.vertexStaticUse = interfaceBlock.staticUse;
2674 break;
2675 }
2676 case GL_FRAGMENT_SHADER:
2677 {
2678 block.fragmentStaticUse = interfaceBlock.staticUse;
2679 break;
2680 }
2681 case GL_COMPUTE_SHADER:
2682 {
2683 block.computeStaticUse = interfaceBlock.staticUse;
2684 break;
2685 }
2686 default:
2687 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002688 }
2689
Jamie Madill4a3c2342015-10-08 12:58:45 -04002690 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002691 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002692 }
2693}
2694
Jamie Madille7d84322017-01-10 18:21:59 -05002695template <>
2696void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2697 const uint8_t *destPointer,
2698 GLsizei clampedCount,
2699 const GLint *v)
2700{
2701 // Invalidate the validation cache only if we modify the sampler data.
2702 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2703 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2704 {
2705 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2706 std::vector<GLuint> *boundTextureUnits =
2707 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2708
2709 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2710 mCachedValidateSamplersResult.reset();
2711 }
2712}
2713
2714template <typename T>
2715void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2716 const uint8_t *destPointer,
2717 GLsizei clampedCount,
2718 const T *v)
2719{
2720}
2721
Jamie Madill62d31cb2015-09-11 13:25:51 -04002722template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002723GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002724{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002725 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2726 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002727 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2728
Corentin Wallez15ac5342016-11-03 17:06:39 -04002729 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2730 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2731 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002732 GLsizei maxElementCount =
2733 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2734
2735 GLsizei count = countIn;
2736 GLsizei clampedCount = count * vectorSize;
2737 if (clampedCount > maxElementCount)
2738 {
2739 clampedCount = maxElementCount;
2740 count = maxElementCount / vectorSize;
2741 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002742
Jamie Madill62d31cb2015-09-11 13:25:51 -04002743 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2744 {
2745 // Do a cast conversion for boolean types. From the spec:
2746 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2747 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002748 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002749 {
2750 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2751 }
2752 }
2753 else
2754 {
Jamie Madille7d84322017-01-10 18:21:59 -05002755 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002756 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002757 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002758
2759 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002760}
2761
2762template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002763GLsizei Program::setMatrixUniformInternal(GLint location,
2764 GLsizei count,
2765 GLboolean transpose,
2766 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767{
2768 if (!transpose)
2769 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002770 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002771 }
2772
2773 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002774 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2775 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002776 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002777
2778 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2779 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2780 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2781 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2782
2783 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002784 {
2785 size_t elementOffset = element * rows * cols;
2786
2787 for (size_t row = 0; row < rows; ++row)
2788 {
2789 for (size_t col = 0; col < cols; ++col)
2790 {
2791 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2792 }
2793 }
2794 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002795
2796 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002797}
2798
2799template <typename DestT>
2800void Program::getUniformInternal(GLint location, DestT *dataOut) const
2801{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002802 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2803 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002804
2805 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2806
2807 GLenum componentType = VariableComponentType(uniform.type);
2808 if (componentType == GLTypeToGLenum<DestT>::value)
2809 {
2810 memcpy(dataOut, srcPointer, uniform.getElementSize());
2811 return;
2812 }
2813
Corentin Wallez6596c462016-03-17 17:26:58 -04002814 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002815
2816 switch (componentType)
2817 {
2818 case GL_INT:
2819 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2820 break;
2821 case GL_UNSIGNED_INT:
2822 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2823 break;
2824 case GL_BOOL:
2825 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2826 break;
2827 case GL_FLOAT:
2828 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2829 break;
2830 default:
2831 UNREACHABLE();
2832 }
2833}
Jamie Madilla4595b82017-01-11 17:36:34 -05002834
2835bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2836{
2837 // Must be called after samplers are validated.
2838 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2839
2840 for (const auto &binding : mState.mSamplerBindings)
2841 {
2842 GLenum textureType = binding.textureType;
2843 for (const auto &unit : binding.boundTextureUnits)
2844 {
2845 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2846 if (programTextureID == textureID)
2847 {
2848 // TODO(jmadill): Check for appropriate overlap.
2849 return true;
2850 }
2851 }
2852 }
2853
2854 return false;
2855}
2856
Jamie Madilla2c74982016-12-12 11:20:42 -05002857} // namespace gl