blob: fdd31640b53097bbad16f25bf9642d065ba14fc1 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050019#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040020#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040022#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000023#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040024#include "libANGLE/VaryingPacking.h"
25#include "libANGLE/features.h"
26#include "libANGLE/queryconversions.h"
27#include "libANGLE/renderer/GLImplFactory.h"
28#include "libANGLE/renderer/ProgramImpl.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000032
Geoff Lang7dd2e102014-11-10 15:19:26 -050033namespace
34{
35
Jamie Madill62d31cb2015-09-11 13:25:51 -040036// This simplified cast function doesn't need to worry about advanced concepts like
37// depth range values, or casting to bool.
38template <typename DestT, typename SrcT>
39DestT UniformStateQueryCast(SrcT value);
40
41// From-Float-To-Integer Casts
42template <>
43GLint UniformStateQueryCast(GLfloat value)
44{
45 return clampCast<GLint>(roundf(value));
46}
47
48template <>
49GLuint UniformStateQueryCast(GLfloat value)
50{
51 return clampCast<GLuint>(roundf(value));
52}
53
54// From-Integer-to-Integer Casts
55template <>
56GLint UniformStateQueryCast(GLuint value)
57{
58 return clampCast<GLint>(value);
59}
60
61template <>
62GLuint UniformStateQueryCast(GLint value)
63{
64 return clampCast<GLuint>(value);
65}
66
67// From-Boolean-to-Anything Casts
68template <>
69GLfloat UniformStateQueryCast(GLboolean value)
70{
71 return (value == GL_TRUE ? 1.0f : 0.0f);
72}
73
74template <>
75GLint UniformStateQueryCast(GLboolean value)
76{
77 return (value == GL_TRUE ? 1 : 0);
78}
79
80template <>
81GLuint UniformStateQueryCast(GLboolean value)
82{
83 return (value == GL_TRUE ? 1u : 0u);
84}
85
86// Default to static_cast
87template <typename DestT, typename SrcT>
88DestT UniformStateQueryCast(SrcT value)
89{
90 return static_cast<DestT>(value);
91}
92
93template <typename SrcT, typename DestT>
94void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
95{
96 for (int comp = 0; comp < components; ++comp)
97 {
98 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
99 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
100 size_t offset = comp * 4;
101 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
102 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
103 }
104}
105
Jamie Madill192745a2016-12-22 15:58:21 -0500106// true if varying x has a higher priority in packing than y
107bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
108{
jchen10a9042d32017-03-17 08:50:45 +0800109 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
110 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
111 sh::ShaderVariable vx, vy;
112 const sh::ShaderVariable *px, *py;
113 if (x.isArrayElement())
114 {
115 vx = *x.varying;
116 vx.arraySize = 0;
117 px = &vx;
118 }
119 else
120 {
121 px = x.varying;
122 }
123
124 if (y.isArrayElement())
125 {
126 vy = *y.varying;
127 vy.arraySize = 0;
128 py = &vy;
129 }
130 else
131 {
132 py = y.varying;
133 }
134
135 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500136}
137
jchen1015015f72017-03-16 13:54:21 +0800138template <typename VarT>
139GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
140{
141 size_t subscript = GL_INVALID_INDEX;
142 std::string baseName = ParseResourceName(name, &subscript);
143
144 // The app is not allowed to specify array indices other than 0 for arrays of basic types
145 if (subscript != 0 && subscript != GL_INVALID_INDEX)
146 {
147 return GL_INVALID_INDEX;
148 }
149
150 for (size_t index = 0; index < list.size(); index++)
151 {
152 const VarT &resource = list[index];
153 if (resource.name == baseName)
154 {
155 if (resource.isArray() || subscript == GL_INVALID_INDEX)
156 {
157 return static_cast<GLuint>(index);
158 }
159 }
160 }
161
162 return GL_INVALID_INDEX;
163}
164
jchen10fd7c3b52017-03-21 15:36:03 +0800165void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
166{
167 ASSERT(bufSize > 0);
168 strncpy(buffer, string.c_str(), bufSize);
169 buffer[bufSize - 1] = '\0';
170
171 if (length)
172 {
173 *length = static_cast<GLsizei>(strlen(buffer));
174 }
175}
176
jchen10a9042d32017-03-17 08:50:45 +0800177bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
178{
179 size_t subscript = GL_INVALID_INDEX;
180 std::string baseName = ParseResourceName(name, &subscript);
181 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
182 {
183 size_t arrayIndex = GL_INVALID_INDEX;
184 std::string arrayName = ParseResourceName(*it, &arrayIndex);
185 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
186 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
187 {
188 return true;
189 }
190 }
191 return false;
192}
193
Jamie Madill62d31cb2015-09-11 13:25:51 -0400194} // anonymous namespace
195
Jamie Madill4a3c2342015-10-08 12:58:45 -0400196const char *const g_fakepath = "C:\\fakepath";
197
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400198InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199{
200}
201
202InfoLog::~InfoLog()
203{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204}
205
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400206size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208 const std::string &logString = mStream.str();
209 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000210}
211
Geoff Lange1a27752015-10-05 13:16:04 -0400212void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000213{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400214 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000215
216 if (bufSize > 0)
217 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400218 const std::string str(mStream.str());
219
220 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000221 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400222 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
223 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000224 }
225
226 infoLog[index] = '\0';
227 }
228
229 if (length)
230 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400231 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000232 }
233}
234
235// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300236// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000237// messages, so lets remove all occurrences of this fake file path from the log.
238void InfoLog::appendSanitized(const char *message)
239{
240 std::string msg(message);
241
242 size_t found;
243 do
244 {
245 found = msg.find(g_fakepath);
246 if (found != std::string::npos)
247 {
248 msg.erase(found, strlen(g_fakepath));
249 }
250 }
251 while (found != std::string::npos);
252
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400253 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000254}
255
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000256void InfoLog::reset()
257{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000258}
259
Geoff Langd8605522016-04-13 10:19:12 -0400260VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000261{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500262}
263
Geoff Langd8605522016-04-13 10:19:12 -0400264VariableLocation::VariableLocation(const std::string &name,
265 unsigned int element,
266 unsigned int index)
267 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500268{
269}
270
Geoff Langd8605522016-04-13 10:19:12 -0400271void Program::Bindings::bindLocation(GLuint index, const std::string &name)
272{
273 mBindings[name] = index;
274}
275
276int Program::Bindings::getBinding(const std::string &name) const
277{
278 auto iter = mBindings.find(name);
279 return (iter != mBindings.end()) ? iter->second : -1;
280}
281
282Program::Bindings::const_iterator Program::Bindings::begin() const
283{
284 return mBindings.begin();
285}
286
287Program::Bindings::const_iterator Program::Bindings::end() const
288{
289 return mBindings.end();
290}
291
Jamie Madill48ef11b2016-04-27 15:21:52 -0400292ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500293 : mLabel(),
294 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400295 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300296 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500297 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500298 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500299 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400300{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300301 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400302}
303
Jamie Madill48ef11b2016-04-27 15:21:52 -0400304ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400305{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500306 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400307}
308
Jamie Madill48ef11b2016-04-27 15:21:52 -0400309const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500310{
311 return mLabel;
312}
313
Jamie Madill48ef11b2016-04-27 15:21:52 -0400314GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400315{
316 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800317 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400318
319 for (size_t location = 0; location < mUniformLocations.size(); ++location)
320 {
321 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400322 if (!uniformLocation.used)
323 {
324 continue;
325 }
326
327 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400328
329 if (uniform.name == baseName)
330 {
Geoff Langd8605522016-04-13 10:19:12 -0400331 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400332 {
Geoff Langd8605522016-04-13 10:19:12 -0400333 if (uniformLocation.element == subscript ||
334 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
335 {
336 return static_cast<GLint>(location);
337 }
338 }
339 else
340 {
341 if (subscript == GL_INVALID_INDEX)
342 {
343 return static_cast<GLint>(location);
344 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345 }
346 }
347 }
348
349 return -1;
350}
351
Jamie Madille7d84322017-01-10 18:21:59 -0500352GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400353{
jchen1015015f72017-03-16 13:54:21 +0800354 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400355}
356
Jamie Madille7d84322017-01-10 18:21:59 -0500357GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
358{
359 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
360 return mUniformLocations[location].index;
361}
362
363Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
364{
365 GLuint index = getUniformIndexFromLocation(location);
366 if (!isSamplerUniformIndex(index))
367 {
368 return Optional<GLuint>::Invalid();
369 }
370
371 return getSamplerIndexFromUniformIndex(index);
372}
373
374bool ProgramState::isSamplerUniformIndex(GLuint index) const
375{
Jamie Madill982f6e02017-06-07 14:33:04 -0400376 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500377}
378
379GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
380{
381 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400382 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500383}
384
Jamie Madill34ca4f52017-06-13 11:49:39 -0400385GLuint ProgramState::getAttributeLocation(const std::string &name) const
386{
387 for (const sh::Attribute &attribute : mAttributes)
388 {
389 if (attribute.name == name)
390 {
391 return attribute.location;
392 }
393 }
394
395 return static_cast<GLuint>(-1);
396}
397
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500398Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400399 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400400 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500401 mLinked(false),
402 mDeleteStatus(false),
403 mRefCount(0),
404 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500405 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500406{
407 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000408
409 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500410 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411}
412
413Program::~Program()
414{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500415 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
416 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500417 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418}
419
Jamie Madill6c1f6712017-02-14 19:08:04 -0500420void Program::destroy(const Context *context)
421{
422 if (mState.mAttachedVertexShader != nullptr)
423 {
424 mState.mAttachedVertexShader->release(context);
425 mState.mAttachedVertexShader = nullptr;
426 }
427
428 if (mState.mAttachedFragmentShader != nullptr)
429 {
430 mState.mAttachedFragmentShader->release(context);
431 mState.mAttachedFragmentShader = nullptr;
432 }
433
434 if (mState.mAttachedComputeShader != nullptr)
435 {
436 mState.mAttachedComputeShader->release(context);
437 mState.mAttachedComputeShader = nullptr;
438 }
439
Jamie Madillc564c072017-06-01 12:45:42 -0400440 mProgram->destroy(context);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500441}
442
Geoff Lang70d0f492015-12-10 17:45:46 -0500443void Program::setLabel(const std::string &label)
444{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400445 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500446}
447
448const std::string &Program::getLabel() const
449{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400450 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500451}
452
Jamie Madillef300b12016-10-07 15:12:09 -0400453void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300455 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300457 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458 {
Jamie Madillef300b12016-10-07 15:12:09 -0400459 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300460 mState.mAttachedVertexShader = shader;
461 mState.mAttachedVertexShader->addRef();
462 break;
463 }
464 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465 {
Jamie Madillef300b12016-10-07 15:12:09 -0400466 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300467 mState.mAttachedFragmentShader = shader;
468 mState.mAttachedFragmentShader->addRef();
469 break;
470 }
471 case GL_COMPUTE_SHADER:
472 {
Jamie Madillef300b12016-10-07 15:12:09 -0400473 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300474 mState.mAttachedComputeShader = shader;
475 mState.mAttachedComputeShader->addRef();
476 break;
477 }
478 default:
479 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481}
482
Jamie Madillc1d770e2017-04-13 17:31:24 -0400483void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300485 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300487 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400489 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500490 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300491 mState.mAttachedVertexShader = nullptr;
492 break;
493 }
494 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400496 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500497 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300498 mState.mAttachedFragmentShader = nullptr;
499 break;
500 }
501 case GL_COMPUTE_SHADER:
502 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400503 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500504 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300505 mState.mAttachedComputeShader = nullptr;
506 break;
507 }
508 default:
509 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511}
512
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000513int Program::getAttachedShadersCount() const
514{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300515 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
516 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000517}
518
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519void Program::bindAttributeLocation(GLuint index, const char *name)
520{
Geoff Langd8605522016-04-13 10:19:12 -0400521 mAttributeBindings.bindLocation(index, name);
522}
523
524void Program::bindUniformLocation(GLuint index, const char *name)
525{
526 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800527 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528}
529
Sami Väisänen46eaa942016-06-29 10:26:37 +0300530void Program::bindFragmentInputLocation(GLint index, const char *name)
531{
532 mFragmentInputBindings.bindLocation(index, name);
533}
534
Jamie Madillbd044ed2017-06-05 12:59:21 -0400535BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300536{
537 BindingInfo ret;
538 ret.type = GL_NONE;
539 ret.valid = false;
540
Jamie Madillbd044ed2017-06-05 12:59:21 -0400541 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300542 ASSERT(fragmentShader);
543
544 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400545 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300546
547 for (const auto &binding : mFragmentInputBindings)
548 {
549 if (binding.second != static_cast<GLuint>(index))
550 continue;
551
552 ret.valid = true;
553
554 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400555 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300556
557 for (const auto &in : inputs)
558 {
559 if (in.name == originalName)
560 {
561 if (in.isArray())
562 {
563 // The client wants to bind either "name" or "name[0]".
564 // GL ES 3.1 spec refers to active array names with language such as:
565 // "if the string identifies the base name of an active array, where the
566 // string would exactly match the name of the variable if the suffix "[0]"
567 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400568 if (arrayIndex == GL_INVALID_INDEX)
569 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300570
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400571 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300572 }
573 else
574 {
575 ret.name = in.mappedName;
576 }
577 ret.type = in.type;
578 return ret;
579 }
580 }
581 }
582
583 return ret;
584}
585
Jamie Madillbd044ed2017-06-05 12:59:21 -0400586void Program::pathFragmentInputGen(const Context *context,
587 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300588 GLenum genMode,
589 GLint components,
590 const GLfloat *coeffs)
591{
592 // If the location is -1 then the command is silently ignored
593 if (index == -1)
594 return;
595
Jamie Madillbd044ed2017-06-05 12:59:21 -0400596 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300597
598 // If the input doesn't exist then then the command is silently ignored
599 // This could happen through optimization for example, the shader translator
600 // decides that a variable is not actually being used and optimizes it away.
601 if (binding.name.empty())
602 return;
603
604 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
605}
606
Martin Radev4c4c8e72016-08-04 12:25:34 +0300607// The attached shaders are checked for linking errors by matching up their variables.
608// Uniform, input and output variables get collected.
609// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500610Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000611{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500612 const auto &data = context->getContextState();
613
Jamie Madill6c1f6712017-02-14 19:08:04 -0500614 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000615
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000616 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000617 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000618
Martin Radev4c4c8e72016-08-04 12:25:34 +0300619 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500620
Jamie Madill192745a2016-12-22 15:58:21 -0500621 auto vertexShader = mState.mAttachedVertexShader;
622 auto fragmentShader = mState.mAttachedFragmentShader;
623 auto computeShader = mState.mAttachedComputeShader;
624
625 bool isComputeShaderAttached = (computeShader != nullptr);
626 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300627 // Check whether we both have a compute and non-compute shaders attached.
628 // If there are of both types attached, then linking should fail.
629 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
630 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500631 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300632 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
633 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400634 }
635
Jamie Madill192745a2016-12-22 15:58:21 -0500636 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500637 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400638 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300639 {
640 mInfoLog << "Attached compute shader is not compiled.";
641 return NoError();
642 }
Jamie Madill192745a2016-12-22 15:58:21 -0500643 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300644
Jamie Madillbd044ed2017-06-05 12:59:21 -0400645 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300646
647 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
648 // If the work group size is not specified, a link time error should occur.
649 if (!mState.mComputeShaderLocalSize.isDeclared())
650 {
651 mInfoLog << "Work group size is not specified.";
652 return NoError();
653 }
654
Jamie Madillbd044ed2017-06-05 12:59:21 -0400655 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300656 {
657 return NoError();
658 }
659
Jamie Madillbd044ed2017-06-05 12:59:21 -0400660 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300661 {
662 return NoError();
663 }
664
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500665 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400666 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500667 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300668 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500669 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300670 }
671 }
672 else
673 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400674 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300675 {
676 return NoError();
677 }
Jamie Madill192745a2016-12-22 15:58:21 -0500678 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679
Jamie Madillbd044ed2017-06-05 12:59:21 -0400680 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300681 {
682 return NoError();
683 }
Jamie Madill192745a2016-12-22 15:58:21 -0500684 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300685
Jamie Madillbd044ed2017-06-05 12:59:21 -0400686 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300687 {
688 mInfoLog << "Fragment shader version does not match vertex shader version.";
689 return NoError();
690 }
691
Jamie Madillbd044ed2017-06-05 12:59:21 -0400692 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300693 {
694 return NoError();
695 }
696
Jamie Madillbd044ed2017-06-05 12:59:21 -0400697 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300698 {
699 return NoError();
700 }
701
Jamie Madillbd044ed2017-06-05 12:59:21 -0400702 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 {
704 return NoError();
705 }
706
Jamie Madillbd044ed2017-06-05 12:59:21 -0400707 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300708 {
709 return NoError();
710 }
711
Jamie Madillbd044ed2017-06-05 12:59:21 -0400712 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300713
jchen10a9042d32017-03-17 08:50:45 +0800714 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300715 {
716 return NoError();
717 }
718
Jamie Madillbd044ed2017-06-05 12:59:21 -0400719 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300720
Jamie Madill192745a2016-12-22 15:58:21 -0500721 // Validate we can pack the varyings.
722 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
723
724 // Map the varyings to the register file
725 // In WebGL, we use a slightly different handling for packing variables.
726 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
727 : PackMode::ANGLE_RELAXED;
728 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
729 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
730 mState.getTransformFeedbackVaryingNames()))
731 {
732 return NoError();
733 }
734
Jamie Madillc564c072017-06-01 12:45:42 -0400735 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500736 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300737 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500738 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300739 }
740
741 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500742 }
743
Olli Etuaho48fed632017-03-16 12:05:30 +0000744 setUniformValuesFromBindingQualifiers();
745
Jamie Madillbd044ed2017-06-05 12:59:21 -0400746 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400747
Martin Radev4c4c8e72016-08-04 12:25:34 +0300748 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000749}
750
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000751// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500752void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400754 mState.mAttributes.clear();
755 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800756 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400757 mState.mUniforms.clear();
758 mState.mUniformLocations.clear();
759 mState.mUniformBlocks.clear();
760 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800761 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400762 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400763 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300764 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500765 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500766
Geoff Lang7dd2e102014-11-10 15:19:26 -0500767 mValidated = false;
768
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000769 mLinked = false;
770}
771
Geoff Lange1a27752015-10-05 13:16:04 -0400772bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000773{
774 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775}
776
Jamie Madilla2c74982016-12-12 11:20:42 -0500777Error Program::loadBinary(const Context *context,
778 GLenum binaryFormat,
779 const void *binary,
780 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000781{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500782 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000783
Geoff Lang7dd2e102014-11-10 15:19:26 -0500784#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800785 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500786#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400787 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
788 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000789 {
Jamie Madillf6113162015-05-07 11:49:21 -0400790 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800791 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500792 }
793
Jamie Madill4f86d052017-06-05 12:59:26 -0400794 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
795 ANGLE_TRY_RESULT(
796 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500797 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500798#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500799}
800
Jamie Madilla2c74982016-12-12 11:20:42 -0500801Error Program::saveBinary(const Context *context,
802 GLenum *binaryFormat,
803 void *binary,
804 GLsizei bufSize,
805 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500806{
807 if (binaryFormat)
808 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400809 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500810 }
811
Jamie Madill4f86d052017-06-05 12:59:26 -0400812 angle::MemoryBuffer memoryBuf;
813 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500814
Jamie Madill4f86d052017-06-05 12:59:26 -0400815 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
816 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500817
818 if (streamLength > bufSize)
819 {
820 if (length)
821 {
822 *length = 0;
823 }
824
825 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
826 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
827 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500828 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500829 }
830
831 if (binary)
832 {
833 char *ptr = reinterpret_cast<char*>(binary);
834
Jamie Madill48ef11b2016-04-27 15:21:52 -0400835 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500836 ptr += streamLength;
837
838 ASSERT(ptr - streamLength == binary);
839 }
840
841 if (length)
842 {
843 *length = streamLength;
844 }
845
He Yunchaoacd18982017-01-04 10:46:42 +0800846 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500847}
848
849GLint Program::getBinaryLength() const
850{
851 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -0500852 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500853 if (error.isError())
854 {
855 return 0;
856 }
857
858 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000859}
860
Geoff Langc5629752015-12-07 16:29:04 -0500861void Program::setBinaryRetrievableHint(bool retrievable)
862{
863 // TODO(jmadill) : replace with dirty bits
864 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400865 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500866}
867
868bool Program::getBinaryRetrievableHint() const
869{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400870 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500871}
872
Yunchao He61afff12017-03-14 15:34:03 +0800873void Program::setSeparable(bool separable)
874{
875 // TODO(yunchao) : replace with dirty bits
876 if (mState.mSeparable != separable)
877 {
878 mProgram->setSeparable(separable);
879 mState.mSeparable = separable;
880 }
881}
882
883bool Program::isSeparable() const
884{
885 return mState.mSeparable;
886}
887
Jamie Madill6c1f6712017-02-14 19:08:04 -0500888void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000889{
890 mRefCount--;
891
892 if (mRefCount == 0 && mDeleteStatus)
893 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500894 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000895 }
896}
897
898void Program::addRef()
899{
900 mRefCount++;
901}
902
903unsigned int Program::getRefCount() const
904{
905 return mRefCount;
906}
907
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000908int Program::getInfoLogLength() const
909{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400910 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000911}
912
Geoff Lange1a27752015-10-05 13:16:04 -0400913void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000914{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000915 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000916}
917
Geoff Lange1a27752015-10-05 13:16:04 -0400918void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000919{
920 int total = 0;
921
Martin Radev4c4c8e72016-08-04 12:25:34 +0300922 if (mState.mAttachedComputeShader)
923 {
924 if (total < maxCount)
925 {
926 shaders[total] = mState.mAttachedComputeShader->getHandle();
927 total++;
928 }
929 }
930
Jamie Madill48ef11b2016-04-27 15:21:52 -0400931 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000932 {
933 if (total < maxCount)
934 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400935 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200936 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000937 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000938 }
939
Jamie Madill48ef11b2016-04-27 15:21:52 -0400940 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000941 {
942 if (total < maxCount)
943 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400944 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200945 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000946 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000947 }
948
949 if (count)
950 {
951 *count = total;
952 }
953}
954
Geoff Lange1a27752015-10-05 13:16:04 -0400955GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500956{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400957 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500958}
959
Jamie Madill63805b42015-08-25 13:17:39 -0400960bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400961{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400962 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
963 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500964}
965
jchen10fd7c3b52017-03-21 15:36:03 +0800966void Program::getActiveAttribute(GLuint index,
967 GLsizei bufsize,
968 GLsizei *length,
969 GLint *size,
970 GLenum *type,
971 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000972{
Jamie Madillc349ec02015-08-21 16:53:12 -0400973 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000974 {
975 if (bufsize > 0)
976 {
977 name[0] = '\0';
978 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500979
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000980 if (length)
981 {
982 *length = 0;
983 }
984
985 *type = GL_NONE;
986 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400987 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000988 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400989
jchen1036e120e2017-03-14 14:53:58 +0800990 ASSERT(index < mState.mAttributes.size());
991 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -0400992
993 if (bufsize > 0)
994 {
jchen10fd7c3b52017-03-21 15:36:03 +0800995 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -0400996 }
997
998 // Always a single 'type' instance
999 *size = 1;
1000 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001001}
1002
Geoff Lange1a27752015-10-05 13:16:04 -04001003GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001004{
Jamie Madillc349ec02015-08-21 16:53:12 -04001005 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001006 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001007 return 0;
1008 }
1009
jchen1036e120e2017-03-14 14:53:58 +08001010 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001011}
1012
Geoff Lange1a27752015-10-05 13:16:04 -04001013GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001014{
Jamie Madillc349ec02015-08-21 16:53:12 -04001015 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001016 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001017 return 0;
1018 }
1019
1020 size_t maxLength = 0;
1021
Jamie Madill48ef11b2016-04-27 15:21:52 -04001022 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001023 {
jchen1036e120e2017-03-14 14:53:58 +08001024 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001025 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001026
Jamie Madillc349ec02015-08-21 16:53:12 -04001027 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001028}
1029
jchen1015015f72017-03-16 13:54:21 +08001030GLuint Program::getInputResourceIndex(const GLchar *name) const
1031{
1032 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1033 {
1034 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1035 if (attribute.name == name)
1036 {
1037 return attributeIndex;
1038 }
1039 }
1040 return GL_INVALID_INDEX;
1041}
1042
1043GLuint Program::getOutputResourceIndex(const GLchar *name) const
1044{
1045 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1046}
1047
jchen10fd7c3b52017-03-21 15:36:03 +08001048size_t Program::getOutputResourceCount() const
1049{
1050 return (mLinked ? mState.mOutputVariables.size() : 0);
1051}
1052
1053void Program::getInputResourceName(GLuint index,
1054 GLsizei bufSize,
1055 GLsizei *length,
1056 GLchar *name) const
1057{
1058 GLint size;
1059 GLenum type;
1060 getActiveAttribute(index, bufSize, length, &size, &type, name);
1061}
1062
1063void Program::getOutputResourceName(GLuint index,
1064 GLsizei bufSize,
1065 GLsizei *length,
1066 GLchar *name) const
1067{
1068 if (length)
1069 {
1070 *length = 0;
1071 }
1072
1073 if (!mLinked)
1074 {
1075 if (bufSize > 0)
1076 {
1077 name[0] = '\0';
1078 }
1079 return;
1080 }
1081 ASSERT(index < mState.mOutputVariables.size());
1082 const auto &output = mState.mOutputVariables[index];
1083
1084 if (bufSize > 0)
1085 {
1086 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1087
1088 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1089 }
1090}
1091
Geoff Lang7dd2e102014-11-10 15:19:26 -05001092GLint Program::getFragDataLocation(const std::string &name) const
1093{
1094 std::string baseName(name);
1095 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001096 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001097 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001098 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1100 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001101 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001102 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001103 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001105}
1106
Geoff Lange1a27752015-10-05 13:16:04 -04001107void Program::getActiveUniform(GLuint index,
1108 GLsizei bufsize,
1109 GLsizei *length,
1110 GLint *size,
1111 GLenum *type,
1112 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001113{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001115 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001116 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001117 ASSERT(index < mState.mUniforms.size());
1118 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001119
1120 if (bufsize > 0)
1121 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001122 std::string string = uniform.name;
1123 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001124 {
1125 string += "[0]";
1126 }
jchen10fd7c3b52017-03-21 15:36:03 +08001127 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128 }
1129
Jamie Madill62d31cb2015-09-11 13:25:51 -04001130 *size = uniform.elementCount();
1131 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001132 }
1133 else
1134 {
1135 if (bufsize > 0)
1136 {
1137 name[0] = '\0';
1138 }
1139
1140 if (length)
1141 {
1142 *length = 0;
1143 }
1144
1145 *size = 0;
1146 *type = GL_NONE;
1147 }
1148}
1149
Geoff Lange1a27752015-10-05 13:16:04 -04001150GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001151{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001153 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001154 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001155 }
1156 else
1157 {
1158 return 0;
1159 }
1160}
1161
Geoff Lange1a27752015-10-05 13:16:04 -04001162GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001163{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001164 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001165
1166 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001167 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001168 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001170 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001171 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001172 size_t length = uniform.name.length() + 1u;
1173 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001174 {
1175 length += 3; // Counting in "[0]".
1176 }
1177 maxLength = std::max(length, maxLength);
1178 }
1179 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001180 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181
Jamie Madill62d31cb2015-09-11 13:25:51 -04001182 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001183}
1184
1185GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1186{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001187 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001188 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001190 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1192 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1193 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1194 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1195 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1196 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1197 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1198 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1199 default:
1200 UNREACHABLE();
1201 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001202 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203 return 0;
1204}
1205
1206bool Program::isValidUniformLocation(GLint location) const
1207{
Jamie Madille2e406c2016-06-02 13:04:10 -04001208 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001209 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1210 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001211}
1212
Jamie Madill62d31cb2015-09-11 13:25:51 -04001213const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001215 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001216 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217}
1218
Jamie Madillac4e9c32017-01-13 14:07:12 -05001219const VariableLocation &Program::getUniformLocation(GLint location) const
1220{
1221 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1222 return mState.mUniformLocations[location];
1223}
1224
1225const std::vector<VariableLocation> &Program::getUniformLocations() const
1226{
1227 return mState.mUniformLocations;
1228}
1229
1230const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1231{
1232 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1233 return mState.mUniforms[index];
1234}
1235
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001238 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001239}
1240
Jamie Madill62d31cb2015-09-11 13:25:51 -04001241GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242{
Jamie Madille7d84322017-01-10 18:21:59 -05001243 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001244}
1245
1246void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1247{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001248 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1249 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001250}
1251
1252void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1253{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001254 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1255 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001256}
1257
1258void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1259{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001260 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1261 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262}
1263
1264void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1265{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001266 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1267 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001268}
1269
1270void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1271{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001272 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1273 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274}
1275
1276void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1277{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001278 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1279 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280}
1281
1282void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1283{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001284 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1285 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001286}
1287
1288void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1289{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001290 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1291 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292}
1293
1294void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1295{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001296 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1297 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001298}
1299
1300void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1301{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001302 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1303 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001304}
1305
1306void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1307{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001308 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1309 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310}
1311
1312void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1313{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001314 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1315 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001316}
1317
1318void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1319{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001320 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1321 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001322}
1323
1324void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1325{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001326 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1327 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001328}
1329
1330void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1331{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001332 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1333 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001334}
1335
1336void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1337{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001338 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1339 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340}
1341
1342void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1343{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001344 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1345 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346}
1347
1348void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1349{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001350 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1351 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001352}
1353
1354void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1355{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001356 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1357 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001358}
1359
1360void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1361{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001362 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1363 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364}
1365
1366void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1367{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001368 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1369 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001370}
1371
Geoff Lange1a27752015-10-05 13:16:04 -04001372void Program::getUniformfv(GLint location, GLfloat *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::getUniformiv(GLint location, GLint *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
Geoff Lange1a27752015-10-05 13:16:04 -04001382void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001384 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385}
1386
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001387void Program::flagForDeletion()
1388{
1389 mDeleteStatus = true;
1390}
1391
1392bool Program::isFlaggedForDeletion() const
1393{
1394 return mDeleteStatus;
1395}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001396
Brandon Jones43a53e22014-08-28 16:23:22 -07001397void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001398{
1399 mInfoLog.reset();
1400
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001402 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001403 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001404 }
1405 else
1406 {
Jamie Madillf6113162015-05-07 11:49:21 -04001407 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001408 }
1409}
1410
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1412{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001413 // Skip cache if we're using an infolog, so we get the full error.
1414 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1415 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1416 {
1417 return mCachedValidateSamplersResult.value();
1418 }
1419
1420 if (mTextureUnitTypesCache.empty())
1421 {
1422 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1423 }
1424 else
1425 {
1426 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1427 }
1428
1429 // if any two active samplers in a program are of different types, but refer to the same
1430 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1431 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001432 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001433 {
Jamie Madille7d84322017-01-10 18:21:59 -05001434 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001435
Jamie Madille7d84322017-01-10 18:21:59 -05001436 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001437 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001438 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1439 {
1440 if (infoLog)
1441 {
1442 (*infoLog) << "Sampler uniform (" << textureUnit
1443 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1444 << caps.maxCombinedTextureImageUnits << ")";
1445 }
1446
1447 mCachedValidateSamplersResult = false;
1448 return false;
1449 }
1450
1451 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1452 {
1453 if (textureType != mTextureUnitTypesCache[textureUnit])
1454 {
1455 if (infoLog)
1456 {
1457 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1458 "image unit ("
1459 << textureUnit << ").";
1460 }
1461
1462 mCachedValidateSamplersResult = false;
1463 return false;
1464 }
1465 }
1466 else
1467 {
1468 mTextureUnitTypesCache[textureUnit] = textureType;
1469 }
1470 }
1471 }
1472
1473 mCachedValidateSamplersResult = true;
1474 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475}
1476
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001477bool Program::isValidated() const
1478{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479 return mValidated;
1480}
1481
Geoff Lange1a27752015-10-05 13:16:04 -04001482GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001484 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001485}
1486
1487void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1488{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001489 ASSERT(
1490 uniformBlockIndex <
1491 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492
Jamie Madill48ef11b2016-04-27 15:21:52 -04001493 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494
1495 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001496 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497 std::string string = uniformBlock.name;
1498
Jamie Madill62d31cb2015-09-11 13:25:51 -04001499 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001500 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001502 }
jchen10fd7c3b52017-03-21 15:36:03 +08001503 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001504 }
1505}
1506
Geoff Lange1a27752015-10-05 13:16:04 -04001507GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001508{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509 int maxLength = 0;
1510
1511 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001512 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001513 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001514 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1515 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001516 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517 if (!uniformBlock.name.empty())
1518 {
jchen10af713a22017-04-19 09:10:56 +08001519 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1520 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521 }
1522 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001523 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524
1525 return maxLength;
1526}
1527
Geoff Lange1a27752015-10-05 13:16:04 -04001528GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001529{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001530 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001531 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001532
Jamie Madill48ef11b2016-04-27 15:21:52 -04001533 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001534 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1535 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001536 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001537 if (uniformBlock.name == baseName)
1538 {
1539 const bool arrayElementZero =
1540 (subscript == GL_INVALID_INDEX &&
1541 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1542 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1543 {
1544 return blockIndex;
1545 }
1546 }
1547 }
1548
1549 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001550}
1551
Jamie Madill62d31cb2015-09-11 13:25:51 -04001552const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001553{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001554 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1555 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001556}
1557
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001558void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1559{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001560 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001561 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001562 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001563}
1564
1565GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1566{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001567 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001568}
1569
1570void Program::resetUniformBlockBindings()
1571{
1572 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1573 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001574 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001575 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001576 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001577}
1578
Geoff Lang48dcae72014-02-05 16:28:24 -05001579void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1580{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001581 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001582 for (GLsizei i = 0; i < count; i++)
1583 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001584 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001585 }
1586
Jamie Madill48ef11b2016-04-27 15:21:52 -04001587 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001588}
1589
1590void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1591{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001592 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001593 {
jchen10a9042d32017-03-17 08:50:45 +08001594 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1595 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1596 std::string varName = var.nameWithArrayIndex();
1597 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001598 if (length)
1599 {
1600 *length = lastNameIdx;
1601 }
1602 if (size)
1603 {
jchen10a9042d32017-03-17 08:50:45 +08001604 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001605 }
1606 if (type)
1607 {
jchen10a9042d32017-03-17 08:50:45 +08001608 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001609 }
1610 if (name)
1611 {
jchen10a9042d32017-03-17 08:50:45 +08001612 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001613 name[lastNameIdx] = '\0';
1614 }
1615 }
1616}
1617
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001618GLsizei Program::getTransformFeedbackVaryingCount() const
1619{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001621 {
jchen10a9042d32017-03-17 08:50:45 +08001622 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001623 }
1624 else
1625 {
1626 return 0;
1627 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001628}
1629
1630GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1631{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001633 {
1634 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001635 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001636 {
jchen10a9042d32017-03-17 08:50:45 +08001637 maxSize =
1638 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001639 }
1640
1641 return maxSize;
1642 }
1643 else
1644 {
1645 return 0;
1646 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001647}
1648
1649GLenum Program::getTransformFeedbackBufferMode() const
1650{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001651 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652}
1653
Jamie Madillbd044ed2017-06-05 12:59:21 -04001654bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001656 Shader *vertexShader = mState.mAttachedVertexShader;
1657 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001658
Jamie Madillbd044ed2017-06-05 12:59:21 -04001659 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001660
Jamie Madillbd044ed2017-06-05 12:59:21 -04001661 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1662 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663
Sami Väisänen46eaa942016-06-29 10:26:37 +03001664 std::map<GLuint, std::string> staticFragmentInputLocations;
1665
Jamie Madill4cff2472015-08-21 16:53:18 -04001666 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001668 bool matched = false;
1669
1670 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001671 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672 {
1673 continue;
1674 }
1675
Jamie Madill4cff2472015-08-21 16:53:18 -04001676 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001678 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001680 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001681 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001682 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001683 {
1684 return false;
1685 }
1686
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687 matched = true;
1688 break;
1689 }
1690 }
1691
1692 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001693 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001694 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001695 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696 return false;
1697 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001698
1699 // Check for aliased path rendering input bindings (if any).
1700 // If more than one binding refer statically to the same
1701 // location the link must fail.
1702
1703 if (!output.staticUse)
1704 continue;
1705
1706 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1707 if (inputBinding == -1)
1708 continue;
1709
1710 const auto it = staticFragmentInputLocations.find(inputBinding);
1711 if (it == std::end(staticFragmentInputLocations))
1712 {
1713 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1714 }
1715 else
1716 {
1717 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1718 << it->second;
1719 return false;
1720 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001721 }
1722
Jamie Madillbd044ed2017-06-05 12:59:21 -04001723 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001724 {
1725 return false;
1726 }
1727
Jamie Madillada9ecc2015-08-17 12:53:37 -04001728 // TODO(jmadill): verify no unmatched vertex varyings?
1729
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730 return true;
1731}
1732
Jamie Madillbd044ed2017-06-05 12:59:21 -04001733bool Program::linkUniforms(const Context *context,
1734 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001735 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001736{
Olli Etuahob78707c2017-03-09 15:03:11 +00001737 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001738 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001739 {
1740 return false;
1741 }
1742
Olli Etuahob78707c2017-03-09 15:03:11 +00001743 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001744
Olli Etuaho48fed632017-03-16 12:05:30 +00001745 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001746
1747 return true;
1748}
1749
Olli Etuaho48fed632017-03-16 12:05:30 +00001750void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001751{
Jamie Madill982f6e02017-06-07 14:33:04 -04001752 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1753 unsigned int low = high;
1754
1755 for (auto samplerIter = mState.mUniforms.rbegin();
1756 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001757 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001758 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001759 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001760
1761 mState.mSamplerUniformRange = RangeUI(low, high);
1762
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001763 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001764 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001765 {
1766 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1767 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1768 mState.mSamplerBindings.emplace_back(
1769 SamplerBinding(textureType, samplerUniform.elementCount()));
1770 }
1771}
1772
Martin Radev4c4c8e72016-08-04 12:25:34 +03001773bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1774 const std::string &uniformName,
1775 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001776 const sh::InterfaceBlockField &fragmentUniform,
1777 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001779 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1780 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1781 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782 {
1783 return false;
1784 }
1785
1786 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1787 {
Jamie Madillf6113162015-05-07 11:49:21 -04001788 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 return false;
1790 }
1791
1792 return true;
1793}
1794
Jamie Madilleb979bf2016-11-15 12:28:46 -05001795// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001796bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001798 const ContextState &data = context->getContextState();
1799 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001800
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001802 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001803 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001804
1805 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001806 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001807 {
Jamie Madillf6113162015-05-07 11:49:21 -04001808 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001809 return false;
1810 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001812 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001813
Jamie Madillc349ec02015-08-21 16:53:12 -04001814 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001815 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001816 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001817 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001818 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001819 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001820 attribute.location = bindingLocation;
1821 }
1822
1823 if (attribute.location != -1)
1824 {
1825 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001826 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827
Jamie Madill63805b42015-08-25 13:17:39 -04001828 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 {
Jamie Madillf6113162015-05-07 11:49:21 -04001830 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001831 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832
1833 return false;
1834 }
1835
Jamie Madill63805b42015-08-25 13:17:39 -04001836 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837 {
Jamie Madill63805b42015-08-25 13:17:39 -04001838 const int regLocation = attribute.location + reg;
1839 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840
1841 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001842 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001843 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001845 // TODO(jmadill): fix aliasing on ES2
1846 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001848 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001849 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 return false;
1851 }
1852 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001853 else
1854 {
Jamie Madill63805b42015-08-25 13:17:39 -04001855 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001856 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857
Jamie Madill63805b42015-08-25 13:17:39 -04001858 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859 }
1860 }
1861 }
1862
1863 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001864 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001866 // Not set by glBindAttribLocation or by location layout qualifier
1867 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 {
Jamie Madill63805b42015-08-25 13:17:39 -04001869 int regs = VariableRegisterCount(attribute.type);
1870 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871
Jamie Madill63805b42015-08-25 13:17:39 -04001872 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873 {
Jamie Madillf6113162015-05-07 11:49:21 -04001874 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001875 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001876 }
1877
Jamie Madillc349ec02015-08-21 16:53:12 -04001878 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879 }
1880 }
1881
Jamie Madill48ef11b2016-04-27 15:21:52 -04001882 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 {
Jamie Madill63805b42015-08-25 13:17:39 -04001884 ASSERT(attribute.location != -1);
1885 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001886
Jamie Madill63805b42015-08-25 13:17:39 -04001887 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001889 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890 }
1891 }
1892
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 return true;
1894}
1895
Martin Radev4c4c8e72016-08-04 12:25:34 +03001896bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1897 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1898 const std::string &errorMessage,
1899 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001901 GLuint blockCount = 0;
1902 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001903 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001904 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04001905 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001906 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04001907 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001908 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04001909 return false;
1910 }
1911 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001913 return true;
1914}
Jamie Madille473dee2015-08-18 14:49:01 -04001915
Martin Radev4c4c8e72016-08-04 12:25:34 +03001916bool Program::validateVertexAndFragmentInterfaceBlocks(
1917 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
1918 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001919 InfoLog &infoLog,
1920 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03001921{
1922 // Check that interface blocks defined in the vertex and fragment shaders are identical
1923 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
1924 UniformBlockMap linkedUniformBlocks;
1925
1926 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
1927 {
1928 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
1929 }
1930
Jamie Madille473dee2015-08-18 14:49:01 -04001931 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932 {
Jamie Madille473dee2015-08-18 14:49:01 -04001933 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 if (entry != linkedUniformBlocks.end())
1935 {
1936 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04001937 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
1938 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001939 {
1940 return false;
1941 }
1942 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001943 }
1944 return true;
1945}
Jamie Madille473dee2015-08-18 14:49:01 -04001946
Jamie Madillbd044ed2017-06-05 12:59:21 -04001947bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001948{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001949 const auto &caps = context->getCaps();
1950
Martin Radev4c4c8e72016-08-04 12:25:34 +03001951 if (mState.mAttachedComputeShader)
1952 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04001953 Shader &computeShader = *mState.mAttachedComputeShader;
1954 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001955
1956 if (!validateUniformBlocksCount(
1957 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
1958 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
1959 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001961 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001962 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001963 return true;
1964 }
1965
Jamie Madillbd044ed2017-06-05 12:59:21 -04001966 Shader &vertexShader = *mState.mAttachedVertexShader;
1967 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001968
Jamie Madillbd044ed2017-06-05 12:59:21 -04001969 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
1970 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03001971
1972 if (!validateUniformBlocksCount(
1973 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
1974 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
1975 {
1976 return false;
1977 }
1978 if (!validateUniformBlocksCount(
1979 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
1980 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
1981 infoLog))
1982 {
1983
1984 return false;
1985 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04001986
1987 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03001988 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001989 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03001990 {
1991 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992 }
Jamie Madille473dee2015-08-18 14:49:01 -04001993
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 return true;
1995}
1996
Jamie Madilla2c74982016-12-12 11:20:42 -05001997bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03001998 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001999 const sh::InterfaceBlock &fragmentInterfaceBlock,
2000 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001{
2002 const char* blockName = vertexInterfaceBlock.name.c_str();
2003 // validate blocks for the same member types
2004 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2005 {
Jamie Madillf6113162015-05-07 11:49:21 -04002006 infoLog << "Types for interface block '" << blockName
2007 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 return false;
2009 }
2010 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2011 {
Jamie Madillf6113162015-05-07 11:49:21 -04002012 infoLog << "Array sizes differ for interface block '" << blockName
2013 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 return false;
2015 }
jchen10af713a22017-04-19 09:10:56 +08002016 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2017 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2018 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 {
Jamie Madillf6113162015-05-07 11:49:21 -04002020 infoLog << "Layout qualifiers differ for interface block '" << blockName
2021 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022 return false;
2023 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002024 const unsigned int numBlockMembers =
2025 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2027 {
2028 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2029 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2030 if (vertexMember.name != fragmentMember.name)
2031 {
Jamie Madillf6113162015-05-07 11:49:21 -04002032 infoLog << "Name mismatch for field " << blockMemberIndex
2033 << " of interface block '" << blockName
2034 << "': (in vertex: '" << vertexMember.name
2035 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 return false;
2037 }
2038 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002039 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2040 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041 {
2042 return false;
2043 }
2044 }
2045 return true;
2046}
2047
2048bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2049 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2050{
2051 if (vertexVariable.type != fragmentVariable.type)
2052 {
Jamie Madillf6113162015-05-07 11:49:21 -04002053 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054 return false;
2055 }
2056 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2057 {
Jamie Madillf6113162015-05-07 11:49:21 -04002058 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059 return false;
2060 }
2061 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2062 {
Jamie Madillf6113162015-05-07 11:49:21 -04002063 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 return false;
2065 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002066 if (vertexVariable.structName != fragmentVariable.structName)
2067 {
2068 infoLog << "Structure names for " << variableName
2069 << " differ between vertex and fragment shaders";
2070 return false;
2071 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002072
2073 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2074 {
Jamie Madillf6113162015-05-07 11:49:21 -04002075 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076 return false;
2077 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002078 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2080 {
2081 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2082 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2083
2084 if (vertexMember.name != fragmentMember.name)
2085 {
Jamie Madillf6113162015-05-07 11:49:21 -04002086 infoLog << "Name mismatch for field '" << memberIndex
2087 << "' of " << variableName
2088 << ": (in vertex: '" << vertexMember.name
2089 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 return false;
2091 }
2092
2093 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2094 vertexMember.name + "'";
2095
2096 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2097 {
2098 return false;
2099 }
2100 }
2101
2102 return true;
2103}
2104
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002105bool Program::linkValidateVaryings(InfoLog &infoLog,
2106 const std::string &varyingName,
2107 const sh::Varying &vertexVarying,
2108 const sh::Varying &fragmentVarying,
2109 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110{
2111 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2112 {
2113 return false;
2114 }
2115
Jamie Madille9cc4692015-02-19 16:00:13 -05002116 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002118 infoLog << "Interpolation types for " << varyingName
2119 << " differ between vertex and fragment shaders.";
2120 return false;
2121 }
2122
2123 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2124 {
2125 infoLog << "Invariance for " << varyingName
2126 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 return false;
2128 }
2129
2130 return true;
2131}
2132
Jamie Madillbd044ed2017-06-05 12:59:21 -04002133bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002134{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002135 Shader *vertexShader = mState.mAttachedVertexShader;
2136 Shader *fragmentShader = mState.mAttachedFragmentShader;
2137 const auto &vertexVaryings = vertexShader->getVaryings(context);
2138 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2139 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002140
2141 if (shaderVersion != 100)
2142 {
2143 // Only ESSL 1.0 has restrictions on matching input and output invariance
2144 return true;
2145 }
2146
2147 bool glPositionIsInvariant = false;
2148 bool glPointSizeIsInvariant = false;
2149 bool glFragCoordIsInvariant = false;
2150 bool glPointCoordIsInvariant = false;
2151
2152 for (const sh::Varying &varying : vertexVaryings)
2153 {
2154 if (!varying.isBuiltIn())
2155 {
2156 continue;
2157 }
2158 if (varying.name.compare("gl_Position") == 0)
2159 {
2160 glPositionIsInvariant = varying.isInvariant;
2161 }
2162 else if (varying.name.compare("gl_PointSize") == 0)
2163 {
2164 glPointSizeIsInvariant = varying.isInvariant;
2165 }
2166 }
2167
2168 for (const sh::Varying &varying : fragmentVaryings)
2169 {
2170 if (!varying.isBuiltIn())
2171 {
2172 continue;
2173 }
2174 if (varying.name.compare("gl_FragCoord") == 0)
2175 {
2176 glFragCoordIsInvariant = varying.isInvariant;
2177 }
2178 else if (varying.name.compare("gl_PointCoord") == 0)
2179 {
2180 glPointCoordIsInvariant = varying.isInvariant;
2181 }
2182 }
2183
2184 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2185 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2186 // Not requiring invariance to match is supported by:
2187 // dEQP, WebGL CTS, Nexus 5X GLES
2188 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2189 {
2190 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2191 "declared invariant.";
2192 return false;
2193 }
2194 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2195 {
2196 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2197 "declared invariant.";
2198 return false;
2199 }
2200
2201 return true;
2202}
2203
jchen10a9042d32017-03-17 08:50:45 +08002204bool Program::linkValidateTransformFeedback(const gl::Context *context,
2205 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002206 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002207 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208{
2209 size_t totalComponents = 0;
2210
Jamie Madillccdf74b2015-08-18 10:46:12 -04002211 std::set<std::string> uniqueNames;
2212
Jamie Madill48ef11b2016-04-27 15:21:52 -04002213 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214 {
2215 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002216 size_t subscript = GL_INVALID_INDEX;
2217 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2218
Jamie Madill192745a2016-12-22 15:58:21 -05002219 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002220 {
Jamie Madill192745a2016-12-22 15:58:21 -05002221 const sh::Varying *varying = ref.second.get();
2222
jchen10a9042d32017-03-17 08:50:45 +08002223 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002225 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002226 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002227 infoLog << "Two transform feedback varyings specify the same output variable ("
2228 << tfVaryingName << ").";
2229 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002230 }
jchen10a9042d32017-03-17 08:50:45 +08002231 if (context->getClientVersion() >= Version(3, 1))
2232 {
2233 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2234 {
2235 infoLog
2236 << "Two transform feedback varyings include the same array element ("
2237 << tfVaryingName << ").";
2238 return false;
2239 }
2240 }
2241 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002242 {
2243 infoLog << "Capture of arrays is undefined and not supported.";
2244 return false;
2245 }
2246
jchen10a9042d32017-03-17 08:50:45 +08002247 uniqueNames.insert(tfVaryingName);
2248
Jamie Madillccdf74b2015-08-18 10:46:12 -04002249 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002250 size_t elementCount =
2251 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2252 : 1);
2253 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002254 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255 componentCount > caps.maxTransformFeedbackSeparateComponents)
2256 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002257 infoLog << "Transform feedback varying's " << varying->name << " components ("
2258 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002259 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260 return false;
2261 }
2262
2263 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002264 found = true;
2265 break;
2266 }
2267 }
jchen10a9042d32017-03-17 08:50:45 +08002268 if (context->getClientVersion() < Version(3, 1) &&
2269 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002270 {
Geoff Lang1a683462015-09-29 15:09:59 -04002271 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002272 return false;
2273 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002274 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2275 ASSERT(found);
2276 }
2277
Jamie Madill48ef11b2016-04-27 15:21:52 -04002278 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002279 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280 {
Jamie Madillf6113162015-05-07 11:49:21 -04002281 infoLog << "Transform feedback varying total components (" << totalComponents
2282 << ") exceed the maximum interleaved components ("
2283 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002284 return false;
2285 }
2286
2287 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002288}
2289
Jamie Madill192745a2016-12-22 15:58:21 -05002290void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002291{
2292 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002293 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002294 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002295 {
jchen10a9042d32017-03-17 08:50:45 +08002296 size_t subscript = GL_INVALID_INDEX;
2297 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002298 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002299 {
Jamie Madill192745a2016-12-22 15:58:21 -05002300 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002301 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002302 {
jchen10a9042d32017-03-17 08:50:45 +08002303 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2304 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002305 break;
2306 }
2307 }
2308 }
2309}
2310
Jamie Madillbd044ed2017-06-05 12:59:21 -04002311Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002312{
Jamie Madill192745a2016-12-22 15:58:21 -05002313 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002314
Jamie Madillbd044ed2017-06-05 12:59:21 -04002315 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002316 {
Jamie Madill192745a2016-12-22 15:58:21 -05002317 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002318 }
2319
Jamie Madillbd044ed2017-06-05 12:59:21 -04002320 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002321 {
Jamie Madill192745a2016-12-22 15:58:21 -05002322 merged[varying.name].fragment = &varying;
2323 }
2324
2325 return merged;
2326}
2327
2328std::vector<PackedVarying> Program::getPackedVaryings(
2329 const Program::MergedVaryings &mergedVaryings) const
2330{
2331 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2332 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002333 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002334
2335 for (const auto &ref : mergedVaryings)
2336 {
2337 const sh::Varying *input = ref.second.vertex;
2338 const sh::Varying *output = ref.second.fragment;
2339
2340 // Only pack varyings that have a matched input or output, plus special builtins.
2341 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002342 {
Jamie Madill192745a2016-12-22 15:58:21 -05002343 // Will get the vertex shader interpolation by default.
2344 auto interpolation = ref.second.get()->interpolation;
2345
2346 // Interpolation qualifiers must match.
2347 if (output->isStruct())
2348 {
2349 ASSERT(!output->isArray());
2350 for (const auto &field : output->fields)
2351 {
2352 ASSERT(!field.isStruct() && !field.isArray());
2353 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2354 }
2355 }
2356 else
2357 {
2358 packedVaryings.push_back(PackedVarying(*output, interpolation));
2359 }
2360 continue;
2361 }
2362
2363 // Keep Transform FB varyings in the merged list always.
2364 if (!input)
2365 {
2366 continue;
2367 }
2368
2369 for (const std::string &tfVarying : tfVaryings)
2370 {
jchen10a9042d32017-03-17 08:50:45 +08002371 size_t subscript = GL_INVALID_INDEX;
2372 std::string baseName = ParseResourceName(tfVarying, &subscript);
2373 if (uniqueFullNames.count(tfVarying) > 0)
2374 {
2375 continue;
2376 }
2377 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002378 {
2379 // Transform feedback for varying structs is underspecified.
2380 // See Khronos bug 9856.
2381 // TODO(jmadill): Figure out how to be spec-compliant here.
2382 if (!input->isStruct())
2383 {
2384 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2385 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002386 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2387 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002388 }
jchen10a9042d32017-03-17 08:50:45 +08002389 if (subscript == GL_INVALID_INDEX)
2390 {
2391 break;
2392 }
Jamie Madill192745a2016-12-22 15:58:21 -05002393 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002394 }
2395 }
2396
Jamie Madill192745a2016-12-22 15:58:21 -05002397 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2398
2399 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002400}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002401
Jamie Madillbd044ed2017-06-05 12:59:21 -04002402void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002403{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002404 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002405 ASSERT(fragmentShader != nullptr);
2406
Geoff Lange0cff192017-05-30 13:04:56 -04002407 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002408 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002409
2410 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002411 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002412 {
2413 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2414 outputVariable.name != "gl_FragData")
2415 {
2416 continue;
2417 }
2418
2419 unsigned int baseLocation =
2420 (outputVariable.location == -1 ? 0u
2421 : static_cast<unsigned int>(outputVariable.location));
2422 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2423 elementIndex++)
2424 {
2425 const unsigned int location = baseLocation + elementIndex;
2426 if (location >= mState.mOutputVariableTypes.size())
2427 {
2428 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2429 }
Corentin Walleze7557742017-06-01 13:09:57 -04002430 ASSERT(location < mState.mActiveOutputVariables.size());
2431 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002432 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2433 }
2434 }
2435
Jamie Madill80a6fc02015-08-21 16:53:16 -04002436 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002437 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002438 return;
2439
Jamie Madillbd044ed2017-06-05 12:59:21 -04002440 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002441 // TODO(jmadill): any caps validation here?
2442
jchen1015015f72017-03-16 13:54:21 +08002443 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002444 outputVariableIndex++)
2445 {
jchen1015015f72017-03-16 13:54:21 +08002446 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002447
2448 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2449 if (outputVariable.isBuiltIn())
2450 continue;
2451
2452 // Since multiple output locations must be specified, use 0 for non-specified locations.
2453 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2454
Jamie Madill80a6fc02015-08-21 16:53:16 -04002455 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2456 elementIndex++)
2457 {
2458 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002459 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002460 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002461 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002462 VariableLocation(outputVariable.name, element, outputVariableIndex);
2463 }
2464 }
2465}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002466
Olli Etuaho48fed632017-03-16 12:05:30 +00002467void Program::setUniformValuesFromBindingQualifiers()
2468{
Jamie Madill982f6e02017-06-07 14:33:04 -04002469 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002470 {
2471 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2472 if (samplerUniform.binding != -1)
2473 {
2474 GLint location = mState.getUniformLocation(samplerUniform.name);
2475 ASSERT(location != -1);
2476 std::vector<GLint> boundTextureUnits;
2477 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2478 ++elementIndex)
2479 {
2480 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2481 }
2482 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2483 boundTextureUnits.data());
2484 }
2485 }
2486}
2487
Jamie Madillbd044ed2017-06-05 12:59:21 -04002488void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002489{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002490 ASSERT(mState.mUniformBlocks.empty());
2491
2492 if (mState.mAttachedComputeShader)
2493 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002494 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002495
Jamie Madillbd044ed2017-06-05 12:59:21 -04002496 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002497 {
2498
2499 // Only 'packed' blocks are allowed to be considered inactive.
2500 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2501 continue;
2502
Jamie Madilla2c74982016-12-12 11:20:42 -05002503 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002504 {
2505 if (block.name == computeBlock.name)
2506 {
2507 block.computeStaticUse = computeBlock.staticUse;
2508 }
2509 }
2510
2511 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2512 }
2513 return;
2514 }
2515
Jamie Madill62d31cb2015-09-11 13:25:51 -04002516 std::set<std::string> visitedList;
2517
Jamie Madillbd044ed2017-06-05 12:59:21 -04002518 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002519
Jamie Madillbd044ed2017-06-05 12:59:21 -04002520 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002521 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002522 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002523 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2524 continue;
2525
2526 if (visitedList.count(vertexBlock.name) > 0)
2527 continue;
2528
2529 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2530 visitedList.insert(vertexBlock.name);
2531 }
2532
Jamie Madillbd044ed2017-06-05 12:59:21 -04002533 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002534
Jamie Madillbd044ed2017-06-05 12:59:21 -04002535 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002536 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002537 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002538 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2539 continue;
2540
2541 if (visitedList.count(fragmentBlock.name) > 0)
2542 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002543 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002544 {
2545 if (block.name == fragmentBlock.name)
2546 {
2547 block.fragmentStaticUse = fragmentBlock.staticUse;
2548 }
2549 }
2550
2551 continue;
2552 }
2553
2554 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2555 visitedList.insert(fragmentBlock.name);
2556 }
jchen10af713a22017-04-19 09:10:56 +08002557 // Set initial bindings from shader.
2558 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2559 {
2560 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2561 bindUniformBlock(blockIndex, uniformBlock.binding);
2562 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002563}
2564
Jamie Madill4a3c2342015-10-08 12:58:45 -04002565template <typename VarT>
2566void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2567 const std::string &prefix,
2568 int blockIndex)
2569{
2570 for (const VarT &field : fields)
2571 {
2572 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2573
2574 if (field.isStruct())
2575 {
2576 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2577 {
2578 const std::string uniformElementName =
2579 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2580 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2581 }
2582 }
2583 else
2584 {
2585 // If getBlockMemberInfo returns false, the uniform is optimized out.
2586 sh::BlockMemberInfo memberInfo;
2587 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2588 {
2589 continue;
2590 }
2591
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002592 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002593 blockIndex, memberInfo);
2594
2595 // Since block uniforms have no location, we don't need to store them in the uniform
2596 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002597 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002598 }
2599 }
2600}
2601
Jamie Madill62d31cb2015-09-11 13:25:51 -04002602void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2603{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002604 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002605 size_t blockSize = 0;
2606
Jamie Madill4a3c2342015-10-08 12:58:45 -04002607 // Track the first and last uniform index to determine the range of active uniforms in the
2608 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002609 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002610 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002611 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002612
2613 std::vector<unsigned int> blockUniformIndexes;
2614 for (size_t blockUniformIndex = firstBlockUniformIndex;
2615 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2616 {
2617 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2618 }
jchen10af713a22017-04-19 09:10:56 +08002619 // ESSL 3.10 section 4.4.4 page 58:
2620 // Any uniform or shader storage block declared without a binding qualifier is initially
2621 // assigned to block binding point zero.
2622 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002623 if (interfaceBlock.arraySize > 0)
2624 {
2625 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2626 {
jchen10af713a22017-04-19 09:10:56 +08002627 // Don't define this block at all if it's not active in the implementation.
2628 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2629 &blockSize))
2630 {
2631 continue;
2632 }
2633 UniformBlock block(interfaceBlock.name, true, arrayElement,
2634 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002635 block.memberUniformIndexes = blockUniformIndexes;
2636
Martin Radev4c4c8e72016-08-04 12:25:34 +03002637 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002638 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002639 case GL_VERTEX_SHADER:
2640 {
2641 block.vertexStaticUse = interfaceBlock.staticUse;
2642 break;
2643 }
2644 case GL_FRAGMENT_SHADER:
2645 {
2646 block.fragmentStaticUse = interfaceBlock.staticUse;
2647 break;
2648 }
2649 case GL_COMPUTE_SHADER:
2650 {
2651 block.computeStaticUse = interfaceBlock.staticUse;
2652 break;
2653 }
2654 default:
2655 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002656 }
2657
Qin Jiajia0350a642016-11-01 17:01:51 +08002658 // Since all block elements in an array share the same active uniforms, they will all be
2659 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2660 // here we will add every block element in the array.
2661 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002662 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002663 }
2664 }
2665 else
2666 {
jchen10af713a22017-04-19 09:10:56 +08002667 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2668 {
2669 return;
2670 }
2671 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002672 block.memberUniformIndexes = blockUniformIndexes;
2673
Martin Radev4c4c8e72016-08-04 12:25:34 +03002674 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002675 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002676 case GL_VERTEX_SHADER:
2677 {
2678 block.vertexStaticUse = interfaceBlock.staticUse;
2679 break;
2680 }
2681 case GL_FRAGMENT_SHADER:
2682 {
2683 block.fragmentStaticUse = interfaceBlock.staticUse;
2684 break;
2685 }
2686 case GL_COMPUTE_SHADER:
2687 {
2688 block.computeStaticUse = interfaceBlock.staticUse;
2689 break;
2690 }
2691 default:
2692 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002693 }
2694
Jamie Madill4a3c2342015-10-08 12:58:45 -04002695 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002696 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002697 }
2698}
2699
Jamie Madille7d84322017-01-10 18:21:59 -05002700template <>
2701void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2702 const uint8_t *destPointer,
2703 GLsizei clampedCount,
2704 const GLint *v)
2705{
2706 // Invalidate the validation cache only if we modify the sampler data.
2707 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2708 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2709 {
2710 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2711 std::vector<GLuint> *boundTextureUnits =
2712 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2713
2714 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2715 mCachedValidateSamplersResult.reset();
2716 }
2717}
2718
2719template <typename T>
2720void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2721 const uint8_t *destPointer,
2722 GLsizei clampedCount,
2723 const T *v)
2724{
2725}
2726
Jamie Madill62d31cb2015-09-11 13:25:51 -04002727template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002728GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002729{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002730 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2731 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002732 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2733
Corentin Wallez15ac5342016-11-03 17:06:39 -04002734 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2735 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2736 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002737 GLsizei maxElementCount =
2738 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2739
2740 GLsizei count = countIn;
2741 GLsizei clampedCount = count * vectorSize;
2742 if (clampedCount > maxElementCount)
2743 {
2744 clampedCount = maxElementCount;
2745 count = maxElementCount / vectorSize;
2746 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002747
Jamie Madill62d31cb2015-09-11 13:25:51 -04002748 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2749 {
2750 // Do a cast conversion for boolean types. From the spec:
2751 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2752 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002753 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002754 {
2755 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2756 }
2757 }
2758 else
2759 {
Jamie Madille7d84322017-01-10 18:21:59 -05002760 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002761 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002762 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002763
2764 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002765}
2766
2767template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002768GLsizei Program::setMatrixUniformInternal(GLint location,
2769 GLsizei count,
2770 GLboolean transpose,
2771 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002772{
2773 if (!transpose)
2774 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002775 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002776 }
2777
2778 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002779 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2780 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002781 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002782
2783 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2784 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2785 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2786 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2787
2788 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002789 {
2790 size_t elementOffset = element * rows * cols;
2791
2792 for (size_t row = 0; row < rows; ++row)
2793 {
2794 for (size_t col = 0; col < cols; ++col)
2795 {
2796 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2797 }
2798 }
2799 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002800
2801 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002802}
2803
2804template <typename DestT>
2805void Program::getUniformInternal(GLint location, DestT *dataOut) const
2806{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002807 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2808 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002809
2810 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2811
2812 GLenum componentType = VariableComponentType(uniform.type);
2813 if (componentType == GLTypeToGLenum<DestT>::value)
2814 {
2815 memcpy(dataOut, srcPointer, uniform.getElementSize());
2816 return;
2817 }
2818
Corentin Wallez6596c462016-03-17 17:26:58 -04002819 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002820
2821 switch (componentType)
2822 {
2823 case GL_INT:
2824 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2825 break;
2826 case GL_UNSIGNED_INT:
2827 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2828 break;
2829 case GL_BOOL:
2830 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2831 break;
2832 case GL_FLOAT:
2833 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2834 break;
2835 default:
2836 UNREACHABLE();
2837 }
2838}
Jamie Madilla4595b82017-01-11 17:36:34 -05002839
2840bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2841{
2842 // Must be called after samplers are validated.
2843 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2844
2845 for (const auto &binding : mState.mSamplerBindings)
2846 {
2847 GLenum textureType = binding.textureType;
2848 for (const auto &unit : binding.boundTextureUnits)
2849 {
2850 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2851 if (programTextureID == textureID)
2852 {
2853 // TODO(jmadill): Check for appropriate overlap.
2854 return true;
2855 }
2856 }
2857 }
2858
2859 return false;
2860}
2861
Jamie Madilla2c74982016-12-12 11:20:42 -05002862} // namespace gl