blob: e59ad77ab4fdb37b09a488886fa22ec8bb3b608d [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),
jchen10eaef1e52017-06-13 10:44:11 +0800299 mImageUniformRange(0, 0),
300 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300301 mBinaryRetrieveableHint(false),
302 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400303{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300304 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400305}
306
Jamie Madill48ef11b2016-04-27 15:21:52 -0400307ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400308{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500309 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400310}
311
Jamie Madill48ef11b2016-04-27 15:21:52 -0400312const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500313{
314 return mLabel;
315}
316
Jamie Madill48ef11b2016-04-27 15:21:52 -0400317GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400318{
319 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800320 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400321
322 for (size_t location = 0; location < mUniformLocations.size(); ++location)
323 {
324 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400325 if (!uniformLocation.used)
326 {
327 continue;
328 }
329
330 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400331
332 if (uniform.name == baseName)
333 {
Geoff Langd8605522016-04-13 10:19:12 -0400334 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400335 {
Geoff Langd8605522016-04-13 10:19:12 -0400336 if (uniformLocation.element == subscript ||
337 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
338 {
339 return static_cast<GLint>(location);
340 }
341 }
342 else
343 {
344 if (subscript == GL_INVALID_INDEX)
345 {
346 return static_cast<GLint>(location);
347 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400348 }
349 }
350 }
351
352 return -1;
353}
354
Jamie Madille7d84322017-01-10 18:21:59 -0500355GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400356{
jchen1015015f72017-03-16 13:54:21 +0800357 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400358}
359
Jamie Madille7d84322017-01-10 18:21:59 -0500360GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
361{
362 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
363 return mUniformLocations[location].index;
364}
365
366Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
367{
368 GLuint index = getUniformIndexFromLocation(location);
369 if (!isSamplerUniformIndex(index))
370 {
371 return Optional<GLuint>::Invalid();
372 }
373
374 return getSamplerIndexFromUniformIndex(index);
375}
376
377bool ProgramState::isSamplerUniformIndex(GLuint index) const
378{
Jamie Madill982f6e02017-06-07 14:33:04 -0400379 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500380}
381
382GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
383{
384 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400385 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500386}
387
Jamie Madill34ca4f52017-06-13 11:49:39 -0400388GLuint ProgramState::getAttributeLocation(const std::string &name) const
389{
390 for (const sh::Attribute &attribute : mAttributes)
391 {
392 if (attribute.name == name)
393 {
394 return attribute.location;
395 }
396 }
397
398 return static_cast<GLuint>(-1);
399}
400
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500401Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400402 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400403 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500404 mLinked(false),
405 mDeleteStatus(false),
406 mRefCount(0),
407 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500408 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500409{
410 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000411
Geoff Lang7dd2e102014-11-10 15:19:26 -0500412 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413}
414
415Program::~Program()
416{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400417 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418}
419
Jamie Madill4928b7c2017-06-20 12:57:39 -0400420void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500421{
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 Madill4928b7c2017-06-20 12:57:39 -0400441
442 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
443 !mState.mAttachedComputeShader);
444 SafeDelete(mProgram);
445
446 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500447}
448
Geoff Lang70d0f492015-12-10 17:45:46 -0500449void Program::setLabel(const std::string &label)
450{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400451 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500452}
453
454const std::string &Program::getLabel() const
455{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400456 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500457}
458
Jamie Madillef300b12016-10-07 15:12:09 -0400459void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300461 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300463 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464 {
Jamie Madillef300b12016-10-07 15:12:09 -0400465 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300466 mState.mAttachedVertexShader = shader;
467 mState.mAttachedVertexShader->addRef();
468 break;
469 }
470 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
Jamie Madillef300b12016-10-07 15:12:09 -0400472 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300473 mState.mAttachedFragmentShader = shader;
474 mState.mAttachedFragmentShader->addRef();
475 break;
476 }
477 case GL_COMPUTE_SHADER:
478 {
Jamie Madillef300b12016-10-07 15:12:09 -0400479 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300480 mState.mAttachedComputeShader = shader;
481 mState.mAttachedComputeShader->addRef();
482 break;
483 }
484 default:
485 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487}
488
Jamie Madillc1d770e2017-04-13 17:31:24 -0400489void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300491 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300493 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400495 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500496 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300497 mState.mAttachedVertexShader = nullptr;
498 break;
499 }
500 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400502 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500503 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300504 mState.mAttachedFragmentShader = nullptr;
505 break;
506 }
507 case GL_COMPUTE_SHADER:
508 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400509 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500510 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300511 mState.mAttachedComputeShader = nullptr;
512 break;
513 }
514 default:
515 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517}
518
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000519int Program::getAttachedShadersCount() const
520{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300521 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
522 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000523}
524
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525void Program::bindAttributeLocation(GLuint index, const char *name)
526{
Geoff Langd8605522016-04-13 10:19:12 -0400527 mAttributeBindings.bindLocation(index, name);
528}
529
530void Program::bindUniformLocation(GLuint index, const char *name)
531{
532 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800533 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534}
535
Sami Väisänen46eaa942016-06-29 10:26:37 +0300536void Program::bindFragmentInputLocation(GLint index, const char *name)
537{
538 mFragmentInputBindings.bindLocation(index, name);
539}
540
Jamie Madillbd044ed2017-06-05 12:59:21 -0400541BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300542{
543 BindingInfo ret;
544 ret.type = GL_NONE;
545 ret.valid = false;
546
Jamie Madillbd044ed2017-06-05 12:59:21 -0400547 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300548 ASSERT(fragmentShader);
549
550 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400551 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300552
553 for (const auto &binding : mFragmentInputBindings)
554 {
555 if (binding.second != static_cast<GLuint>(index))
556 continue;
557
558 ret.valid = true;
559
560 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400561 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300562
563 for (const auto &in : inputs)
564 {
565 if (in.name == originalName)
566 {
567 if (in.isArray())
568 {
569 // The client wants to bind either "name" or "name[0]".
570 // GL ES 3.1 spec refers to active array names with language such as:
571 // "if the string identifies the base name of an active array, where the
572 // string would exactly match the name of the variable if the suffix "[0]"
573 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400574 if (arrayIndex == GL_INVALID_INDEX)
575 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300576
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400577 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300578 }
579 else
580 {
581 ret.name = in.mappedName;
582 }
583 ret.type = in.type;
584 return ret;
585 }
586 }
587 }
588
589 return ret;
590}
591
Jamie Madillbd044ed2017-06-05 12:59:21 -0400592void Program::pathFragmentInputGen(const Context *context,
593 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300594 GLenum genMode,
595 GLint components,
596 const GLfloat *coeffs)
597{
598 // If the location is -1 then the command is silently ignored
599 if (index == -1)
600 return;
601
Jamie Madillbd044ed2017-06-05 12:59:21 -0400602 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300603
604 // If the input doesn't exist then then the command is silently ignored
605 // This could happen through optimization for example, the shader translator
606 // decides that a variable is not actually being used and optimizes it away.
607 if (binding.name.empty())
608 return;
609
610 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
611}
612
Martin Radev4c4c8e72016-08-04 12:25:34 +0300613// The attached shaders are checked for linking errors by matching up their variables.
614// Uniform, input and output variables get collected.
615// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500616Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000617{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500618 const auto &data = context->getContextState();
619
Jamie Madill6c1f6712017-02-14 19:08:04 -0500620 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000621
Jamie Madill32447362017-06-28 14:53:52 -0400622 ProgramHash programHash;
623 auto *cache = context->getMemoryProgramCache();
624 if (cache)
625 {
626 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
627 }
628
629 if (mLinked)
630 {
631 return NoError();
632 }
633
634 // Cache load failed, fall through to normal linking.
635 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000636 mInfoLog.reset();
637
Martin Radev4c4c8e72016-08-04 12:25:34 +0300638 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500639
Jamie Madill192745a2016-12-22 15:58:21 -0500640 auto vertexShader = mState.mAttachedVertexShader;
641 auto fragmentShader = mState.mAttachedFragmentShader;
642 auto computeShader = mState.mAttachedComputeShader;
643
644 bool isComputeShaderAttached = (computeShader != nullptr);
645 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300646 // Check whether we both have a compute and non-compute shaders attached.
647 // If there are of both types attached, then linking should fail.
648 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
649 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500650 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300651 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
652 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400653 }
654
Jamie Madill192745a2016-12-22 15:58:21 -0500655 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500656 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400657 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658 {
659 mInfoLog << "Attached compute shader is not compiled.";
660 return NoError();
661 }
Jamie Madill192745a2016-12-22 15:58:21 -0500662 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300663
Jamie Madillbd044ed2017-06-05 12:59:21 -0400664 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665
666 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
667 // If the work group size is not specified, a link time error should occur.
668 if (!mState.mComputeShaderLocalSize.isDeclared())
669 {
670 mInfoLog << "Work group size is not specified.";
671 return NoError();
672 }
673
Jamie Madillbd044ed2017-06-05 12:59:21 -0400674 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300675 {
676 return NoError();
677 }
678
Jamie Madillbd044ed2017-06-05 12:59:21 -0400679 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300680 {
681 return NoError();
682 }
683
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500684 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400685 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500686 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300687 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500688 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 }
690 }
691 else
692 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400693 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300694 {
695 return NoError();
696 }
Jamie Madill192745a2016-12-22 15:58:21 -0500697 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300698
Jamie Madillbd044ed2017-06-05 12:59:21 -0400699 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300700 {
701 return NoError();
702 }
Jamie Madill192745a2016-12-22 15:58:21 -0500703 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300704
Jamie Madillbd044ed2017-06-05 12:59:21 -0400705 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300706 {
707 mInfoLog << "Fragment shader version does not match vertex shader version.";
708 return NoError();
709 }
710
Jamie Madillbd044ed2017-06-05 12:59:21 -0400711 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300712 {
713 return NoError();
714 }
715
Jamie Madillbd044ed2017-06-05 12:59:21 -0400716 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717 {
718 return NoError();
719 }
720
Jamie Madillbd044ed2017-06-05 12:59:21 -0400721 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300722 {
723 return NoError();
724 }
725
Jamie Madillbd044ed2017-06-05 12:59:21 -0400726 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300727 {
728 return NoError();
729 }
730
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400731 if (!linkValidateGlobalNames(context, mInfoLog))
732 {
733 return NoError();
734 }
735
Jamie Madillbd044ed2017-06-05 12:59:21 -0400736 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300737
jchen10a9042d32017-03-17 08:50:45 +0800738 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300739 {
740 return NoError();
741 }
742
Martin Radev7cf61662017-07-26 17:10:53 +0300743 mState.mNumViews = vertexShader->getNumViews(context);
744
Jamie Madillbd044ed2017-06-05 12:59:21 -0400745 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300746
Jamie Madill192745a2016-12-22 15:58:21 -0500747 // Validate we can pack the varyings.
748 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
749
750 // Map the varyings to the register file
751 // In WebGL, we use a slightly different handling for packing variables.
752 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
753 : PackMode::ANGLE_RELAXED;
754 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
755 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
756 mState.getTransformFeedbackVaryingNames()))
757 {
758 return NoError();
759 }
760
Jamie Madillc564c072017-06-01 12:45:42 -0400761 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500762 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300763 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500764 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300765 }
766
767 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500768 }
769
jchen10eaef1e52017-06-13 10:44:11 +0800770 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400771 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400772
jchen10eaef1e52017-06-13 10:44:11 +0800773 setUniformValuesFromBindingQualifiers();
774
Jamie Madill32447362017-06-28 14:53:52 -0400775 // Save to the program cache.
776 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
777 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
778 {
779 cache->putProgram(programHash, context, this);
780 }
781
Martin Radev4c4c8e72016-08-04 12:25:34 +0300782 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000783}
784
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000785// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500786void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400788 mState.mAttributes.clear();
789 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800790 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400791 mState.mUniforms.clear();
792 mState.mUniformLocations.clear();
793 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800794 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800795 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400796 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800797 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400798 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400799 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300800 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500801 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800802 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300803 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500804
Geoff Lang7dd2e102014-11-10 15:19:26 -0500805 mValidated = false;
806
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000807 mLinked = false;
808}
809
Geoff Lange1a27752015-10-05 13:16:04 -0400810bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000811{
812 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
Jamie Madilla2c74982016-12-12 11:20:42 -0500815Error Program::loadBinary(const Context *context,
816 GLenum binaryFormat,
817 const void *binary,
818 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000819{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500820 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000821
Geoff Lang7dd2e102014-11-10 15:19:26 -0500822#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800823 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500824#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400825 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
826 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000827 {
Jamie Madillf6113162015-05-07 11:49:21 -0400828 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800829 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500830 }
831
Jamie Madill4f86d052017-06-05 12:59:26 -0400832 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
833 ANGLE_TRY_RESULT(
834 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400835
836 // Currently we require the full shader text to compute the program hash.
837 // TODO(jmadill): Store the binary in the internal program cache.
838
Jamie Madillb0a838b2016-11-13 20:02:12 -0500839 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500840#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500841}
842
Jamie Madilla2c74982016-12-12 11:20:42 -0500843Error Program::saveBinary(const Context *context,
844 GLenum *binaryFormat,
845 void *binary,
846 GLsizei bufSize,
847 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500848{
849 if (binaryFormat)
850 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400851 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500852 }
853
Jamie Madill4f86d052017-06-05 12:59:26 -0400854 angle::MemoryBuffer memoryBuf;
855 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500856
Jamie Madill4f86d052017-06-05 12:59:26 -0400857 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
858 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500859
860 if (streamLength > bufSize)
861 {
862 if (length)
863 {
864 *length = 0;
865 }
866
867 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
868 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
869 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500870 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500871 }
872
873 if (binary)
874 {
875 char *ptr = reinterpret_cast<char*>(binary);
876
Jamie Madill48ef11b2016-04-27 15:21:52 -0400877 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878 ptr += streamLength;
879
880 ASSERT(ptr - streamLength == binary);
881 }
882
883 if (length)
884 {
885 *length = streamLength;
886 }
887
He Yunchaoacd18982017-01-04 10:46:42 +0800888 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500889}
890
Jamie Madillffe00c02017-06-27 16:26:55 -0400891GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892{
893 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400894 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500895 if (error.isError())
896 {
897 return 0;
898 }
899
900 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000901}
902
Geoff Langc5629752015-12-07 16:29:04 -0500903void Program::setBinaryRetrievableHint(bool retrievable)
904{
905 // TODO(jmadill) : replace with dirty bits
906 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400907 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500908}
909
910bool Program::getBinaryRetrievableHint() const
911{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400912 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500913}
914
Yunchao He61afff12017-03-14 15:34:03 +0800915void Program::setSeparable(bool separable)
916{
917 // TODO(yunchao) : replace with dirty bits
918 if (mState.mSeparable != separable)
919 {
920 mProgram->setSeparable(separable);
921 mState.mSeparable = separable;
922 }
923}
924
925bool Program::isSeparable() const
926{
927 return mState.mSeparable;
928}
929
Jamie Madill6c1f6712017-02-14 19:08:04 -0500930void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000931{
932 mRefCount--;
933
934 if (mRefCount == 0 && mDeleteStatus)
935 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500936 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000937 }
938}
939
940void Program::addRef()
941{
942 mRefCount++;
943}
944
945unsigned int Program::getRefCount() const
946{
947 return mRefCount;
948}
949
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000950int Program::getInfoLogLength() const
951{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400952 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000953}
954
Geoff Lange1a27752015-10-05 13:16:04 -0400955void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000956{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000957 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000958}
959
Geoff Lange1a27752015-10-05 13:16:04 -0400960void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000961{
962 int total = 0;
963
Martin Radev4c4c8e72016-08-04 12:25:34 +0300964 if (mState.mAttachedComputeShader)
965 {
966 if (total < maxCount)
967 {
968 shaders[total] = mState.mAttachedComputeShader->getHandle();
969 total++;
970 }
971 }
972
Jamie Madill48ef11b2016-04-27 15:21:52 -0400973 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000974 {
975 if (total < maxCount)
976 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400977 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200978 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000979 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000980 }
981
Jamie Madill48ef11b2016-04-27 15:21:52 -0400982 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000983 {
984 if (total < maxCount)
985 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400986 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200987 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000988 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000989 }
990
991 if (count)
992 {
993 *count = total;
994 }
995}
996
Geoff Lange1a27752015-10-05 13:16:04 -0400997GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500998{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400999 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001000}
1001
Jamie Madill63805b42015-08-25 13:17:39 -04001002bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001003{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001004 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1005 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001006}
1007
jchen10fd7c3b52017-03-21 15:36:03 +08001008void Program::getActiveAttribute(GLuint index,
1009 GLsizei bufsize,
1010 GLsizei *length,
1011 GLint *size,
1012 GLenum *type,
1013 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001014{
Jamie Madillc349ec02015-08-21 16:53:12 -04001015 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001016 {
1017 if (bufsize > 0)
1018 {
1019 name[0] = '\0';
1020 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001021
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001022 if (length)
1023 {
1024 *length = 0;
1025 }
1026
1027 *type = GL_NONE;
1028 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001029 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001030 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001031
jchen1036e120e2017-03-14 14:53:58 +08001032 ASSERT(index < mState.mAttributes.size());
1033 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001034
1035 if (bufsize > 0)
1036 {
jchen10fd7c3b52017-03-21 15:36:03 +08001037 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001038 }
1039
1040 // Always a single 'type' instance
1041 *size = 1;
1042 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001043}
1044
Geoff Lange1a27752015-10-05 13:16:04 -04001045GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001046{
Jamie Madillc349ec02015-08-21 16:53:12 -04001047 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001048 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001049 return 0;
1050 }
1051
jchen1036e120e2017-03-14 14:53:58 +08001052 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001053}
1054
Geoff Lange1a27752015-10-05 13:16:04 -04001055GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001056{
Jamie Madillc349ec02015-08-21 16:53:12 -04001057 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001058 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001059 return 0;
1060 }
1061
1062 size_t maxLength = 0;
1063
Jamie Madill48ef11b2016-04-27 15:21:52 -04001064 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001065 {
jchen1036e120e2017-03-14 14:53:58 +08001066 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001067 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001068
Jamie Madillc349ec02015-08-21 16:53:12 -04001069 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001070}
1071
jchen1015015f72017-03-16 13:54:21 +08001072GLuint Program::getInputResourceIndex(const GLchar *name) const
1073{
1074 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1075 {
1076 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1077 if (attribute.name == name)
1078 {
1079 return attributeIndex;
1080 }
1081 }
1082 return GL_INVALID_INDEX;
1083}
1084
1085GLuint Program::getOutputResourceIndex(const GLchar *name) const
1086{
1087 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1088}
1089
jchen10fd7c3b52017-03-21 15:36:03 +08001090size_t Program::getOutputResourceCount() const
1091{
1092 return (mLinked ? mState.mOutputVariables.size() : 0);
1093}
1094
1095void Program::getInputResourceName(GLuint index,
1096 GLsizei bufSize,
1097 GLsizei *length,
1098 GLchar *name) const
1099{
1100 GLint size;
1101 GLenum type;
1102 getActiveAttribute(index, bufSize, length, &size, &type, name);
1103}
1104
1105void Program::getOutputResourceName(GLuint index,
1106 GLsizei bufSize,
1107 GLsizei *length,
1108 GLchar *name) const
1109{
1110 if (length)
1111 {
1112 *length = 0;
1113 }
1114
1115 if (!mLinked)
1116 {
1117 if (bufSize > 0)
1118 {
1119 name[0] = '\0';
1120 }
1121 return;
1122 }
1123 ASSERT(index < mState.mOutputVariables.size());
1124 const auto &output = mState.mOutputVariables[index];
1125
1126 if (bufSize > 0)
1127 {
1128 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1129
1130 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1131 }
1132}
1133
Geoff Lang7dd2e102014-11-10 15:19:26 -05001134GLint Program::getFragDataLocation(const std::string &name) const
1135{
1136 std::string baseName(name);
1137 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001138 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001139 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001140 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1142 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001143 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001145 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001146 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001147}
1148
Geoff Lange1a27752015-10-05 13:16:04 -04001149void Program::getActiveUniform(GLuint index,
1150 GLsizei bufsize,
1151 GLsizei *length,
1152 GLint *size,
1153 GLenum *type,
1154 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001155{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001156 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001157 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001158 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001159 ASSERT(index < mState.mUniforms.size());
1160 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001161
1162 if (bufsize > 0)
1163 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001164 std::string string = uniform.name;
1165 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166 {
1167 string += "[0]";
1168 }
jchen10fd7c3b52017-03-21 15:36:03 +08001169 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001170 }
1171
Jamie Madill62d31cb2015-09-11 13:25:51 -04001172 *size = uniform.elementCount();
1173 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001174 }
1175 else
1176 {
1177 if (bufsize > 0)
1178 {
1179 name[0] = '\0';
1180 }
1181
1182 if (length)
1183 {
1184 *length = 0;
1185 }
1186
1187 *size = 0;
1188 *type = GL_NONE;
1189 }
1190}
1191
Geoff Lange1a27752015-10-05 13:16:04 -04001192GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001193{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001194 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001195 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001196 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001197 }
1198 else
1199 {
1200 return 0;
1201 }
1202}
1203
Geoff Lange1a27752015-10-05 13:16:04 -04001204GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001205{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001206 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207
1208 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001209 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001210 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001212 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001213 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001214 size_t length = uniform.name.length() + 1u;
1215 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 {
1217 length += 3; // Counting in "[0]".
1218 }
1219 maxLength = std::max(length, maxLength);
1220 }
1221 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001222 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223
Jamie Madill62d31cb2015-09-11 13:25:51 -04001224 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001225}
1226
1227GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1228{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001229 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001230 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001231 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001232 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1234 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1235 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001236 case GL_UNIFORM_BLOCK_INDEX:
1237 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1239 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1240 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1241 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1242 default:
1243 UNREACHABLE();
1244 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001245 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001246 return 0;
1247}
1248
1249bool Program::isValidUniformLocation(GLint location) const
1250{
Jamie Madille2e406c2016-06-02 13:04:10 -04001251 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001252 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1253 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001254}
1255
Jamie Madill62d31cb2015-09-11 13:25:51 -04001256const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001258 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001259 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001260}
1261
Jamie Madillac4e9c32017-01-13 14:07:12 -05001262const VariableLocation &Program::getUniformLocation(GLint location) const
1263{
1264 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1265 return mState.mUniformLocations[location];
1266}
1267
1268const std::vector<VariableLocation> &Program::getUniformLocations() const
1269{
1270 return mState.mUniformLocations;
1271}
1272
1273const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1274{
1275 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1276 return mState.mUniforms[index];
1277}
1278
Jamie Madill62d31cb2015-09-11 13:25:51 -04001279GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001281 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001282}
1283
Jamie Madill62d31cb2015-09-11 13:25:51 -04001284GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285{
Jamie Madille7d84322017-01-10 18:21:59 -05001286 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287}
1288
1289void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1290{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001291 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1292 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001293}
1294
1295void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1296{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001297 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1298 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001299}
1300
1301void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1302{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001303 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1304 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305}
1306
1307void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1308{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001309 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1310 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001311}
1312
1313void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1314{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001315 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1316 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001317}
1318
1319void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1320{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001321 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1322 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001323}
1324
1325void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1326{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001327 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1328 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329}
1330
1331void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1332{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001333 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1334 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335}
1336
1337void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1338{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001339 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1340 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001341}
1342
1343void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1344{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001345 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1346 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347}
1348
1349void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1350{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001351 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1352 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
1355void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1356{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001357 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1358 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359}
1360
1361void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1362{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001363 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1364 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365}
1366
1367void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1368{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001369 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1370 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371}
1372
1373void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1374{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001375 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1376 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377}
1378
1379void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1380{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001381 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1382 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383}
1384
1385void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1386{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001387 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1388 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389}
1390
1391void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1392{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001393 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1394 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395}
1396
1397void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1398{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001399 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1400 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401}
1402
1403void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1404{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001405 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1406 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407}
1408
1409void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1410{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001411 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1412 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413}
1414
Geoff Lange1a27752015-10-05 13:16:04 -04001415void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418}
1419
Geoff Lange1a27752015-10-05 13:16:04 -04001420void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001422 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423}
1424
Geoff Lange1a27752015-10-05 13:16:04 -04001425void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001427 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428}
1429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001430void Program::flagForDeletion()
1431{
1432 mDeleteStatus = true;
1433}
1434
1435bool Program::isFlaggedForDeletion() const
1436{
1437 return mDeleteStatus;
1438}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001439
Brandon Jones43a53e22014-08-28 16:23:22 -07001440void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001441{
1442 mInfoLog.reset();
1443
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001445 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001446 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001447 }
1448 else
1449 {
Jamie Madillf6113162015-05-07 11:49:21 -04001450 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001451 }
1452}
1453
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1455{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001456 // Skip cache if we're using an infolog, so we get the full error.
1457 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1458 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1459 {
1460 return mCachedValidateSamplersResult.value();
1461 }
1462
1463 if (mTextureUnitTypesCache.empty())
1464 {
1465 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1466 }
1467 else
1468 {
1469 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1470 }
1471
1472 // if any two active samplers in a program are of different types, but refer to the same
1473 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1474 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001475 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001476 {
Jamie Madille7d84322017-01-10 18:21:59 -05001477 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001478
Jamie Madille7d84322017-01-10 18:21:59 -05001479 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001480 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001481 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1482 {
1483 if (infoLog)
1484 {
1485 (*infoLog) << "Sampler uniform (" << textureUnit
1486 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1487 << caps.maxCombinedTextureImageUnits << ")";
1488 }
1489
1490 mCachedValidateSamplersResult = false;
1491 return false;
1492 }
1493
1494 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1495 {
1496 if (textureType != mTextureUnitTypesCache[textureUnit])
1497 {
1498 if (infoLog)
1499 {
1500 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1501 "image unit ("
1502 << textureUnit << ").";
1503 }
1504
1505 mCachedValidateSamplersResult = false;
1506 return false;
1507 }
1508 }
1509 else
1510 {
1511 mTextureUnitTypesCache[textureUnit] = textureType;
1512 }
1513 }
1514 }
1515
1516 mCachedValidateSamplersResult = true;
1517 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518}
1519
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001520bool Program::isValidated() const
1521{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522 return mValidated;
1523}
1524
Geoff Lange1a27752015-10-05 13:16:04 -04001525GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001526{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001527 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528}
1529
1530void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1531{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001532 ASSERT(
1533 uniformBlockIndex <
1534 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535
Jamie Madill48ef11b2016-04-27 15:21:52 -04001536 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001537
1538 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001539 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540 std::string string = uniformBlock.name;
1541
Jamie Madill62d31cb2015-09-11 13:25:51 -04001542 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001544 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001545 }
jchen10fd7c3b52017-03-21 15:36:03 +08001546 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001547 }
1548}
1549
Geoff Lange1a27752015-10-05 13:16:04 -04001550GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001551{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552 int maxLength = 0;
1553
1554 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001555 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001556 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1558 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001559 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560 if (!uniformBlock.name.empty())
1561 {
jchen10af713a22017-04-19 09:10:56 +08001562 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1563 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564 }
1565 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001566 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567
1568 return maxLength;
1569}
1570
Geoff Lange1a27752015-10-05 13:16:04 -04001571GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001572{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001573 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001574 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001575
Jamie Madill48ef11b2016-04-27 15:21:52 -04001576 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001577 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1578 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001579 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580 if (uniformBlock.name == baseName)
1581 {
1582 const bool arrayElementZero =
1583 (subscript == GL_INVALID_INDEX &&
1584 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1585 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1586 {
1587 return blockIndex;
1588 }
1589 }
1590 }
1591
1592 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001593}
1594
Jamie Madill62d31cb2015-09-11 13:25:51 -04001595const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001596{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001597 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1598 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001599}
1600
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001601void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1602{
jchen107a20b972017-06-13 14:25:26 +08001603 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001604 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001605 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001606}
1607
1608GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1609{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001610 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001611}
1612
Geoff Lang48dcae72014-02-05 16:28:24 -05001613void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1614{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001615 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001616 for (GLsizei i = 0; i < count; i++)
1617 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001618 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001619 }
1620
Jamie Madill48ef11b2016-04-27 15:21:52 -04001621 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001622}
1623
1624void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1625{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001626 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001627 {
jchen10a9042d32017-03-17 08:50:45 +08001628 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1629 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1630 std::string varName = var.nameWithArrayIndex();
1631 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001632 if (length)
1633 {
1634 *length = lastNameIdx;
1635 }
1636 if (size)
1637 {
jchen10a9042d32017-03-17 08:50:45 +08001638 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001639 }
1640 if (type)
1641 {
jchen10a9042d32017-03-17 08:50:45 +08001642 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001643 }
1644 if (name)
1645 {
jchen10a9042d32017-03-17 08:50:45 +08001646 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001647 name[lastNameIdx] = '\0';
1648 }
1649 }
1650}
1651
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001652GLsizei Program::getTransformFeedbackVaryingCount() const
1653{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001655 {
jchen10a9042d32017-03-17 08:50:45 +08001656 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001657 }
1658 else
1659 {
1660 return 0;
1661 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001662}
1663
1664GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1665{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001667 {
1668 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001669 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001670 {
jchen10a9042d32017-03-17 08:50:45 +08001671 maxSize =
1672 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001673 }
1674
1675 return maxSize;
1676 }
1677 else
1678 {
1679 return 0;
1680 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001681}
1682
1683GLenum Program::getTransformFeedbackBufferMode() const
1684{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001685 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686}
1687
Jamie Madillbd044ed2017-06-05 12:59:21 -04001688bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001690 Shader *vertexShader = mState.mAttachedVertexShader;
1691 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001692
Jamie Madillbd044ed2017-06-05 12:59:21 -04001693 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001694
Jamie Madillbd044ed2017-06-05 12:59:21 -04001695 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1696 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697
Sami Väisänen46eaa942016-06-29 10:26:37 +03001698 std::map<GLuint, std::string> staticFragmentInputLocations;
1699
Jamie Madill4cff2472015-08-21 16:53:18 -04001700 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001701 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702 bool matched = false;
1703
1704 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001705 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706 {
1707 continue;
1708 }
1709
Jamie Madill4cff2472015-08-21 16:53:18 -04001710 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001711 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001712 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001714 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001715 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001716 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001717 {
1718 return false;
1719 }
1720
Geoff Lang7dd2e102014-11-10 15:19:26 -05001721 matched = true;
1722 break;
1723 }
1724 }
1725
1726 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001727 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001729 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730 return false;
1731 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001732
1733 // Check for aliased path rendering input bindings (if any).
1734 // If more than one binding refer statically to the same
1735 // location the link must fail.
1736
1737 if (!output.staticUse)
1738 continue;
1739
1740 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1741 if (inputBinding == -1)
1742 continue;
1743
1744 const auto it = staticFragmentInputLocations.find(inputBinding);
1745 if (it == std::end(staticFragmentInputLocations))
1746 {
1747 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1748 }
1749 else
1750 {
1751 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1752 << it->second;
1753 return false;
1754 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755 }
1756
Jamie Madillbd044ed2017-06-05 12:59:21 -04001757 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001758 {
1759 return false;
1760 }
1761
Jamie Madillada9ecc2015-08-17 12:53:37 -04001762 // TODO(jmadill): verify no unmatched vertex varyings?
1763
Geoff Lang7dd2e102014-11-10 15:19:26 -05001764 return true;
1765}
1766
Jamie Madillbd044ed2017-06-05 12:59:21 -04001767bool Program::linkUniforms(const Context *context,
1768 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001769 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001770{
Olli Etuahob78707c2017-03-09 15:03:11 +00001771 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001772 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001773 {
1774 return false;
1775 }
1776
Olli Etuahob78707c2017-03-09 15:03:11 +00001777 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001778
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001779 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001780
jchen10eaef1e52017-06-13 10:44:11 +08001781 if (!linkAtomicCounterBuffers())
1782 {
1783 return false;
1784 }
1785
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001786 return true;
1787}
1788
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001789void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001790{
Jamie Madill982f6e02017-06-07 14:33:04 -04001791 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1792 unsigned int low = high;
1793
jchen10eaef1e52017-06-13 10:44:11 +08001794 for (auto counterIter = mState.mUniforms.rbegin();
1795 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1796 {
1797 --low;
1798 }
1799
1800 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1801
1802 high = low;
1803
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001804 for (auto imageIter = mState.mUniforms.rbegin();
1805 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1806 {
1807 --low;
1808 }
1809
1810 mState.mImageUniformRange = RangeUI(low, high);
1811
1812 // If uniform is a image type, insert it into the mImageBindings array.
1813 for (unsigned int imageIndex : mState.mImageUniformRange)
1814 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001815 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1816 // cannot load values into a uniform defined as an image. if declare without a
1817 // binding qualifier, any uniform image variable (include all elements of
1818 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001819 auto &imageUniform = mState.mUniforms[imageIndex];
1820 if (imageUniform.binding == -1)
1821 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001822 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001823 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001824 else
1825 {
1826 mState.mImageBindings.emplace_back(
1827 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1828 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001829 }
1830
1831 high = low;
1832
1833 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001834 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001835 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001836 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001837 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001838
1839 mState.mSamplerUniformRange = RangeUI(low, high);
1840
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001841 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001842 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001843 {
1844 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1845 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1846 mState.mSamplerBindings.emplace_back(
1847 SamplerBinding(textureType, samplerUniform.elementCount()));
1848 }
1849}
1850
jchen10eaef1e52017-06-13 10:44:11 +08001851bool Program::linkAtomicCounterBuffers()
1852{
1853 for (unsigned int index : mState.mAtomicCounterUniformRange)
1854 {
1855 auto &uniform = mState.mUniforms[index];
1856 bool found = false;
1857 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1858 ++bufferIndex)
1859 {
1860 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1861 if (buffer.binding == uniform.binding)
1862 {
1863 buffer.memberIndexes.push_back(index);
1864 uniform.bufferIndex = bufferIndex;
1865 found = true;
1866 break;
1867 }
1868 }
1869 if (!found)
1870 {
1871 AtomicCounterBuffer atomicCounterBuffer;
1872 atomicCounterBuffer.binding = uniform.binding;
1873 atomicCounterBuffer.memberIndexes.push_back(index);
1874 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1875 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1876 }
1877 }
1878 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1879 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1880
1881 return true;
1882}
1883
Martin Radev4c4c8e72016-08-04 12:25:34 +03001884bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1885 const std::string &uniformName,
1886 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001887 const sh::InterfaceBlockField &fragmentUniform,
1888 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001889{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001890 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1891 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1892 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 {
1894 return false;
1895 }
1896
1897 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1898 {
Jamie Madillf6113162015-05-07 11:49:21 -04001899 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 return false;
1901 }
1902
1903 return true;
1904}
1905
Jamie Madilleb979bf2016-11-15 12:28:46 -05001906// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001907bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001909 const ContextState &data = context->getContextState();
1910 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001911
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001913 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001914 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001915
1916 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001917 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001918 {
Jamie Madillf6113162015-05-07 11:49:21 -04001919 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001920 return false;
1921 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001922
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001923 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001924
Jamie Madillc349ec02015-08-21 16:53:12 -04001925 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001926 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001927 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001928 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001929 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001930 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001931 attribute.location = bindingLocation;
1932 }
1933
1934 if (attribute.location != -1)
1935 {
1936 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001937 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001938
Jamie Madill63805b42015-08-25 13:17:39 -04001939 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001940 {
Jamie Madillf6113162015-05-07 11:49:21 -04001941 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001942 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943
1944 return false;
1945 }
1946
Jamie Madill63805b42015-08-25 13:17:39 -04001947 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948 {
Jamie Madill63805b42015-08-25 13:17:39 -04001949 const int regLocation = attribute.location + reg;
1950 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951
1952 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001953 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001954 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001956 // TODO(jmadill): fix aliasing on ES2
1957 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001958 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001959 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001960 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001961 return false;
1962 }
1963 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001964 else
1965 {
Jamie Madill63805b42015-08-25 13:17:39 -04001966 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001967 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968
Jamie Madill63805b42015-08-25 13:17:39 -04001969 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970 }
1971 }
1972 }
1973
1974 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001975 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001976 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001977 // Not set by glBindAttribLocation or by location layout qualifier
1978 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 {
Jamie Madill63805b42015-08-25 13:17:39 -04001980 int regs = VariableRegisterCount(attribute.type);
1981 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001982
Jamie Madill63805b42015-08-25 13:17:39 -04001983 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984 {
Jamie Madillf6113162015-05-07 11:49:21 -04001985 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001986 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 }
1988
Jamie Madillc349ec02015-08-21 16:53:12 -04001989 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990 }
1991 }
1992
Jamie Madill48ef11b2016-04-27 15:21:52 -04001993 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 {
Jamie Madill63805b42015-08-25 13:17:39 -04001995 ASSERT(attribute.location != -1);
1996 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001997
Jamie Madill63805b42015-08-25 13:17:39 -04001998 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002000 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001 }
2002 }
2003
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004 return true;
2005}
2006
Martin Radev4c4c8e72016-08-04 12:25:34 +03002007bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2008 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2009 const std::string &errorMessage,
2010 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002012 GLuint blockCount = 0;
2013 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002015 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002016 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002017 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002018 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002019 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002020 return false;
2021 }
2022 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002024 return true;
2025}
Jamie Madille473dee2015-08-18 14:49:01 -04002026
Martin Radev4c4c8e72016-08-04 12:25:34 +03002027bool Program::validateVertexAndFragmentInterfaceBlocks(
2028 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2029 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002030 InfoLog &infoLog,
2031 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002032{
2033 // Check that interface blocks defined in the vertex and fragment shaders are identical
2034 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2035 UniformBlockMap linkedUniformBlocks;
2036
2037 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2038 {
2039 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2040 }
2041
Jamie Madille473dee2015-08-18 14:49:01 -04002042 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
Jamie Madille473dee2015-08-18 14:49:01 -04002044 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045 if (entry != linkedUniformBlocks.end())
2046 {
2047 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002048 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2049 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 {
2051 return false;
2052 }
2053 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002054 }
2055 return true;
2056}
Jamie Madille473dee2015-08-18 14:49:01 -04002057
Jamie Madillbd044ed2017-06-05 12:59:21 -04002058bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002059{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002060 const auto &caps = context->getCaps();
2061
Martin Radev4c4c8e72016-08-04 12:25:34 +03002062 if (mState.mAttachedComputeShader)
2063 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002064 Shader &computeShader = *mState.mAttachedComputeShader;
2065 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002066
2067 if (!validateUniformBlocksCount(
2068 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2069 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2070 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002072 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002074 return true;
2075 }
2076
Jamie Madillbd044ed2017-06-05 12:59:21 -04002077 Shader &vertexShader = *mState.mAttachedVertexShader;
2078 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002079
Jamie Madillbd044ed2017-06-05 12:59:21 -04002080 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2081 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002082
2083 if (!validateUniformBlocksCount(
2084 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2085 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2086 {
2087 return false;
2088 }
2089 if (!validateUniformBlocksCount(
2090 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2091 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2092 infoLog))
2093 {
2094
2095 return false;
2096 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002097
2098 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002099 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002100 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002101 {
2102 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 }
Jamie Madille473dee2015-08-18 14:49:01 -04002104
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 return true;
2106}
2107
Jamie Madilla2c74982016-12-12 11:20:42 -05002108bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002109 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002110 const sh::InterfaceBlock &fragmentInterfaceBlock,
2111 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112{
2113 const char* blockName = vertexInterfaceBlock.name.c_str();
2114 // validate blocks for the same member types
2115 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2116 {
Jamie Madillf6113162015-05-07 11:49:21 -04002117 infoLog << "Types for interface block '" << blockName
2118 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119 return false;
2120 }
2121 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2122 {
Jamie Madillf6113162015-05-07 11:49:21 -04002123 infoLog << "Array sizes differ for interface block '" << blockName
2124 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 return false;
2126 }
jchen10af713a22017-04-19 09:10:56 +08002127 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2128 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2129 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 {
Jamie Madillf6113162015-05-07 11:49:21 -04002131 infoLog << "Layout qualifiers differ for interface block '" << blockName
2132 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133 return false;
2134 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002135 const unsigned int numBlockMembers =
2136 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2138 {
2139 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2140 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2141 if (vertexMember.name != fragmentMember.name)
2142 {
Jamie Madillf6113162015-05-07 11:49:21 -04002143 infoLog << "Name mismatch for field " << blockMemberIndex
2144 << " of interface block '" << blockName
2145 << "': (in vertex: '" << vertexMember.name
2146 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147 return false;
2148 }
2149 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002150 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2151 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 {
2153 return false;
2154 }
2155 }
2156 return true;
2157}
2158
2159bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2160 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2161{
2162 if (vertexVariable.type != fragmentVariable.type)
2163 {
Jamie Madillf6113162015-05-07 11:49:21 -04002164 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002165 return false;
2166 }
2167 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2168 {
Jamie Madillf6113162015-05-07 11:49:21 -04002169 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170 return false;
2171 }
2172 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2173 {
Jamie Madillf6113162015-05-07 11:49:21 -04002174 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 return false;
2176 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002177 if (vertexVariable.structName != fragmentVariable.structName)
2178 {
2179 infoLog << "Structure names for " << variableName
2180 << " differ between vertex and fragment shaders";
2181 return false;
2182 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183
2184 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2185 {
Jamie Madillf6113162015-05-07 11:49:21 -04002186 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187 return false;
2188 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002189 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2191 {
2192 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2193 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2194
2195 if (vertexMember.name != fragmentMember.name)
2196 {
Jamie Madillf6113162015-05-07 11:49:21 -04002197 infoLog << "Name mismatch for field '" << memberIndex
2198 << "' of " << variableName
2199 << ": (in vertex: '" << vertexMember.name
2200 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002201 return false;
2202 }
2203
2204 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2205 vertexMember.name + "'";
2206
2207 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2208 {
2209 return false;
2210 }
2211 }
2212
2213 return true;
2214}
2215
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002216bool Program::linkValidateVaryings(InfoLog &infoLog,
2217 const std::string &varyingName,
2218 const sh::Varying &vertexVarying,
2219 const sh::Varying &fragmentVarying,
2220 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221{
2222 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2223 {
2224 return false;
2225 }
2226
Jamie Madille9cc4692015-02-19 16:00:13 -05002227 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002229 infoLog << "Interpolation types for " << varyingName
2230 << " differ between vertex and fragment shaders.";
2231 return false;
2232 }
2233
2234 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2235 {
2236 infoLog << "Invariance for " << varyingName
2237 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002238 return false;
2239 }
2240
2241 return true;
2242}
2243
Jamie Madillbd044ed2017-06-05 12:59:21 -04002244bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002245{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002246 Shader *vertexShader = mState.mAttachedVertexShader;
2247 Shader *fragmentShader = mState.mAttachedFragmentShader;
2248 const auto &vertexVaryings = vertexShader->getVaryings(context);
2249 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2250 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002251
2252 if (shaderVersion != 100)
2253 {
2254 // Only ESSL 1.0 has restrictions on matching input and output invariance
2255 return true;
2256 }
2257
2258 bool glPositionIsInvariant = false;
2259 bool glPointSizeIsInvariant = false;
2260 bool glFragCoordIsInvariant = false;
2261 bool glPointCoordIsInvariant = false;
2262
2263 for (const sh::Varying &varying : vertexVaryings)
2264 {
2265 if (!varying.isBuiltIn())
2266 {
2267 continue;
2268 }
2269 if (varying.name.compare("gl_Position") == 0)
2270 {
2271 glPositionIsInvariant = varying.isInvariant;
2272 }
2273 else if (varying.name.compare("gl_PointSize") == 0)
2274 {
2275 glPointSizeIsInvariant = varying.isInvariant;
2276 }
2277 }
2278
2279 for (const sh::Varying &varying : fragmentVaryings)
2280 {
2281 if (!varying.isBuiltIn())
2282 {
2283 continue;
2284 }
2285 if (varying.name.compare("gl_FragCoord") == 0)
2286 {
2287 glFragCoordIsInvariant = varying.isInvariant;
2288 }
2289 else if (varying.name.compare("gl_PointCoord") == 0)
2290 {
2291 glPointCoordIsInvariant = varying.isInvariant;
2292 }
2293 }
2294
2295 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2296 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2297 // Not requiring invariance to match is supported by:
2298 // dEQP, WebGL CTS, Nexus 5X GLES
2299 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2300 {
2301 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2302 "declared invariant.";
2303 return false;
2304 }
2305 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2306 {
2307 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2308 "declared invariant.";
2309 return false;
2310 }
2311
2312 return true;
2313}
2314
jchen10a9042d32017-03-17 08:50:45 +08002315bool Program::linkValidateTransformFeedback(const gl::Context *context,
2316 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002317 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002318 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002319{
2320 size_t totalComponents = 0;
2321
Jamie Madillccdf74b2015-08-18 10:46:12 -04002322 std::set<std::string> uniqueNames;
2323
Jamie Madill48ef11b2016-04-27 15:21:52 -04002324 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325 {
2326 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002327 size_t subscript = GL_INVALID_INDEX;
2328 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2329
Jamie Madill192745a2016-12-22 15:58:21 -05002330 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002331 {
Jamie Madill192745a2016-12-22 15:58:21 -05002332 const sh::Varying *varying = ref.second.get();
2333
jchen10a9042d32017-03-17 08:50:45 +08002334 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002335 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002336 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002337 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002338 infoLog << "Two transform feedback varyings specify the same output variable ("
2339 << tfVaryingName << ").";
2340 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002341 }
jchen10a9042d32017-03-17 08:50:45 +08002342 if (context->getClientVersion() >= Version(3, 1))
2343 {
2344 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2345 {
2346 infoLog
2347 << "Two transform feedback varyings include the same array element ("
2348 << tfVaryingName << ").";
2349 return false;
2350 }
2351 }
2352 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002353 {
2354 infoLog << "Capture of arrays is undefined and not supported.";
2355 return false;
2356 }
2357
jchen10a9042d32017-03-17 08:50:45 +08002358 uniqueNames.insert(tfVaryingName);
2359
Jamie Madillccdf74b2015-08-18 10:46:12 -04002360 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002361 size_t elementCount =
2362 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2363 : 1);
2364 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002365 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002366 componentCount > caps.maxTransformFeedbackSeparateComponents)
2367 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002368 infoLog << "Transform feedback varying's " << varying->name << " components ("
2369 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002370 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371 return false;
2372 }
2373
2374 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 found = true;
2376 break;
2377 }
2378 }
jchen10a9042d32017-03-17 08:50:45 +08002379 if (context->getClientVersion() < Version(3, 1) &&
2380 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002381 {
Geoff Lang1a683462015-09-29 15:09:59 -04002382 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002383 return false;
2384 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002385 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2386 ASSERT(found);
2387 }
2388
Jamie Madill48ef11b2016-04-27 15:21:52 -04002389 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002390 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 {
Jamie Madillf6113162015-05-07 11:49:21 -04002392 infoLog << "Transform feedback varying total components (" << totalComponents
2393 << ") exceed the maximum interleaved components ("
2394 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002395 return false;
2396 }
2397
2398 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002399}
2400
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002401bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2402{
2403 const std::vector<sh::Uniform> &vertexUniforms =
2404 mState.mAttachedVertexShader->getUniforms(context);
2405 const std::vector<sh::Uniform> &fragmentUniforms =
2406 mState.mAttachedFragmentShader->getUniforms(context);
2407 const std::vector<sh::Attribute> &attributes =
2408 mState.mAttachedVertexShader->getActiveAttributes(context);
2409 for (const auto &attrib : attributes)
2410 {
2411 for (const auto &uniform : vertexUniforms)
2412 {
2413 if (uniform.name == attrib.name)
2414 {
2415 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2416 return false;
2417 }
2418 }
2419 for (const auto &uniform : fragmentUniforms)
2420 {
2421 if (uniform.name == attrib.name)
2422 {
2423 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2424 return false;
2425 }
2426 }
2427 }
2428 return true;
2429}
2430
Jamie Madill192745a2016-12-22 15:58:21 -05002431void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002432{
2433 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002434 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002435 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436 {
jchen10a9042d32017-03-17 08:50:45 +08002437 size_t subscript = GL_INVALID_INDEX;
2438 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002439 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002440 {
Jamie Madill192745a2016-12-22 15:58:21 -05002441 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002442 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002443 {
jchen10a9042d32017-03-17 08:50:45 +08002444 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2445 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002446 break;
2447 }
2448 }
2449 }
2450}
2451
Jamie Madillbd044ed2017-06-05 12:59:21 -04002452Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453{
Jamie Madill192745a2016-12-22 15:58:21 -05002454 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002455
Jamie Madillbd044ed2017-06-05 12:59:21 -04002456 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002457 {
Jamie Madill192745a2016-12-22 15:58:21 -05002458 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002459 }
2460
Jamie Madillbd044ed2017-06-05 12:59:21 -04002461 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002462 {
Jamie Madill192745a2016-12-22 15:58:21 -05002463 merged[varying.name].fragment = &varying;
2464 }
2465
2466 return merged;
2467}
2468
2469std::vector<PackedVarying> Program::getPackedVaryings(
2470 const Program::MergedVaryings &mergedVaryings) const
2471{
2472 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2473 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002474 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002475
2476 for (const auto &ref : mergedVaryings)
2477 {
2478 const sh::Varying *input = ref.second.vertex;
2479 const sh::Varying *output = ref.second.fragment;
2480
2481 // Only pack varyings that have a matched input or output, plus special builtins.
2482 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002483 {
Jamie Madill192745a2016-12-22 15:58:21 -05002484 // Will get the vertex shader interpolation by default.
2485 auto interpolation = ref.second.get()->interpolation;
2486
Olli Etuaho06a06f52017-07-12 12:22:15 +03002487 // Note that we lose the vertex shader static use information here. The data for the
2488 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002489 if (output->isStruct())
2490 {
2491 ASSERT(!output->isArray());
2492 for (const auto &field : output->fields)
2493 {
2494 ASSERT(!field.isStruct() && !field.isArray());
2495 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2496 }
2497 }
2498 else
2499 {
2500 packedVaryings.push_back(PackedVarying(*output, interpolation));
2501 }
2502 continue;
2503 }
2504
2505 // Keep Transform FB varyings in the merged list always.
2506 if (!input)
2507 {
2508 continue;
2509 }
2510
2511 for (const std::string &tfVarying : tfVaryings)
2512 {
jchen10a9042d32017-03-17 08:50:45 +08002513 size_t subscript = GL_INVALID_INDEX;
2514 std::string baseName = ParseResourceName(tfVarying, &subscript);
2515 if (uniqueFullNames.count(tfVarying) > 0)
2516 {
2517 continue;
2518 }
2519 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002520 {
2521 // Transform feedback for varying structs is underspecified.
2522 // See Khronos bug 9856.
2523 // TODO(jmadill): Figure out how to be spec-compliant here.
2524 if (!input->isStruct())
2525 {
2526 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2527 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002528 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2529 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002530 }
jchen10a9042d32017-03-17 08:50:45 +08002531 if (subscript == GL_INVALID_INDEX)
2532 {
2533 break;
2534 }
Jamie Madill192745a2016-12-22 15:58:21 -05002535 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002536 }
2537 }
2538
Jamie Madill192745a2016-12-22 15:58:21 -05002539 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2540
2541 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002542}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002543
Jamie Madillbd044ed2017-06-05 12:59:21 -04002544void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002545{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002546 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002547 ASSERT(fragmentShader != nullptr);
2548
Geoff Lange0cff192017-05-30 13:04:56 -04002549 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002550 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002551
2552 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002553 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002554 {
2555 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2556 outputVariable.name != "gl_FragData")
2557 {
2558 continue;
2559 }
2560
2561 unsigned int baseLocation =
2562 (outputVariable.location == -1 ? 0u
2563 : static_cast<unsigned int>(outputVariable.location));
2564 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2565 elementIndex++)
2566 {
2567 const unsigned int location = baseLocation + elementIndex;
2568 if (location >= mState.mOutputVariableTypes.size())
2569 {
2570 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2571 }
Corentin Walleze7557742017-06-01 13:09:57 -04002572 ASSERT(location < mState.mActiveOutputVariables.size());
2573 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002574 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2575 }
2576 }
2577
Jamie Madill80a6fc02015-08-21 16:53:16 -04002578 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002579 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002580 return;
2581
Jamie Madillbd044ed2017-06-05 12:59:21 -04002582 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002583 // TODO(jmadill): any caps validation here?
2584
jchen1015015f72017-03-16 13:54:21 +08002585 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002586 outputVariableIndex++)
2587 {
jchen1015015f72017-03-16 13:54:21 +08002588 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002589
2590 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2591 if (outputVariable.isBuiltIn())
2592 continue;
2593
2594 // Since multiple output locations must be specified, use 0 for non-specified locations.
2595 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2596
Jamie Madill80a6fc02015-08-21 16:53:16 -04002597 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2598 elementIndex++)
2599 {
2600 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002601 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002602 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002603 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002604 VariableLocation(outputVariable.name, element, outputVariableIndex);
2605 }
2606 }
2607}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002608
Olli Etuaho48fed632017-03-16 12:05:30 +00002609void Program::setUniformValuesFromBindingQualifiers()
2610{
Jamie Madill982f6e02017-06-07 14:33:04 -04002611 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002612 {
2613 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2614 if (samplerUniform.binding != -1)
2615 {
2616 GLint location = mState.getUniformLocation(samplerUniform.name);
2617 ASSERT(location != -1);
2618 std::vector<GLint> boundTextureUnits;
2619 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2620 ++elementIndex)
2621 {
2622 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2623 }
2624 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2625 boundTextureUnits.data());
2626 }
2627 }
2628}
2629
jchen10eaef1e52017-06-13 10:44:11 +08002630void Program::gatherAtomicCounterBuffers()
2631{
2632 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2633 // counter.
2634 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2635}
2636
Jamie Madillbd044ed2017-06-05 12:59:21 -04002637void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002638{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002639 ASSERT(mState.mUniformBlocks.empty());
2640
2641 if (mState.mAttachedComputeShader)
2642 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002643 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002644
Jamie Madillbd044ed2017-06-05 12:59:21 -04002645 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002646 {
2647
2648 // Only 'packed' blocks are allowed to be considered inactive.
2649 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2650 continue;
2651
Jamie Madilla2c74982016-12-12 11:20:42 -05002652 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002653 {
2654 if (block.name == computeBlock.name)
2655 {
2656 block.computeStaticUse = computeBlock.staticUse;
2657 }
2658 }
2659
2660 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2661 }
2662 return;
2663 }
2664
Jamie Madill62d31cb2015-09-11 13:25:51 -04002665 std::set<std::string> visitedList;
2666
Jamie Madillbd044ed2017-06-05 12:59:21 -04002667 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002668
Jamie Madillbd044ed2017-06-05 12:59:21 -04002669 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002670 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002671 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002672 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2673 continue;
2674
2675 if (visitedList.count(vertexBlock.name) > 0)
2676 continue;
2677
2678 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2679 visitedList.insert(vertexBlock.name);
2680 }
2681
Jamie Madillbd044ed2017-06-05 12:59:21 -04002682 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002683
Jamie Madillbd044ed2017-06-05 12:59:21 -04002684 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002685 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002686 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002687 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2688 continue;
2689
2690 if (visitedList.count(fragmentBlock.name) > 0)
2691 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002692 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002693 {
2694 if (block.name == fragmentBlock.name)
2695 {
2696 block.fragmentStaticUse = fragmentBlock.staticUse;
2697 }
2698 }
2699
2700 continue;
2701 }
2702
2703 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2704 visitedList.insert(fragmentBlock.name);
2705 }
jchen10af713a22017-04-19 09:10:56 +08002706 // Set initial bindings from shader.
2707 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2708 {
2709 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2710 bindUniformBlock(blockIndex, uniformBlock.binding);
2711 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002712}
2713
Jamie Madill4a3c2342015-10-08 12:58:45 -04002714template <typename VarT>
2715void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2716 const std::string &prefix,
2717 int blockIndex)
2718{
2719 for (const VarT &field : fields)
2720 {
2721 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2722
2723 if (field.isStruct())
2724 {
2725 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2726 {
2727 const std::string uniformElementName =
2728 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2729 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2730 }
2731 }
2732 else
2733 {
2734 // If getBlockMemberInfo returns false, the uniform is optimized out.
2735 sh::BlockMemberInfo memberInfo;
2736 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2737 {
2738 continue;
2739 }
2740
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002741 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002742 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002743
2744 // Since block uniforms have no location, we don't need to store them in the uniform
2745 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002746 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002747 }
2748 }
2749}
2750
Jamie Madill62d31cb2015-09-11 13:25:51 -04002751void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2752{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002753 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002754 size_t blockSize = 0;
2755
Jamie Madill4a3c2342015-10-08 12:58:45 -04002756 // Track the first and last uniform index to determine the range of active uniforms in the
2757 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002758 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002759 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002760 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002761
2762 std::vector<unsigned int> blockUniformIndexes;
2763 for (size_t blockUniformIndex = firstBlockUniformIndex;
2764 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2765 {
2766 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2767 }
jchen10af713a22017-04-19 09:10:56 +08002768 // ESSL 3.10 section 4.4.4 page 58:
2769 // Any uniform or shader storage block declared without a binding qualifier is initially
2770 // assigned to block binding point zero.
2771 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002772 if (interfaceBlock.arraySize > 0)
2773 {
2774 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2775 {
jchen10af713a22017-04-19 09:10:56 +08002776 // Don't define this block at all if it's not active in the implementation.
2777 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2778 &blockSize))
2779 {
2780 continue;
2781 }
2782 UniformBlock block(interfaceBlock.name, true, arrayElement,
2783 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002784 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002785
Martin Radev4c4c8e72016-08-04 12:25:34 +03002786 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002787 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002788 case GL_VERTEX_SHADER:
2789 {
2790 block.vertexStaticUse = interfaceBlock.staticUse;
2791 break;
2792 }
2793 case GL_FRAGMENT_SHADER:
2794 {
2795 block.fragmentStaticUse = interfaceBlock.staticUse;
2796 break;
2797 }
2798 case GL_COMPUTE_SHADER:
2799 {
2800 block.computeStaticUse = interfaceBlock.staticUse;
2801 break;
2802 }
2803 default:
2804 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002805 }
2806
Qin Jiajia0350a642016-11-01 17:01:51 +08002807 // Since all block elements in an array share the same active uniforms, they will all be
2808 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2809 // here we will add every block element in the array.
2810 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002811 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002812 }
2813 }
2814 else
2815 {
jchen10af713a22017-04-19 09:10:56 +08002816 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2817 {
2818 return;
2819 }
2820 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002821 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822
Martin Radev4c4c8e72016-08-04 12:25:34 +03002823 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002824 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002825 case GL_VERTEX_SHADER:
2826 {
2827 block.vertexStaticUse = interfaceBlock.staticUse;
2828 break;
2829 }
2830 case GL_FRAGMENT_SHADER:
2831 {
2832 block.fragmentStaticUse = interfaceBlock.staticUse;
2833 break;
2834 }
2835 case GL_COMPUTE_SHADER:
2836 {
2837 block.computeStaticUse = interfaceBlock.staticUse;
2838 break;
2839 }
2840 default:
2841 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842 }
2843
Jamie Madill4a3c2342015-10-08 12:58:45 -04002844 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002845 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002846 }
2847}
2848
Jamie Madille7d84322017-01-10 18:21:59 -05002849template <>
2850void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2851 const uint8_t *destPointer,
2852 GLsizei clampedCount,
2853 const GLint *v)
2854{
2855 // Invalidate the validation cache only if we modify the sampler data.
2856 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2857 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2858 {
2859 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2860 std::vector<GLuint> *boundTextureUnits =
2861 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2862
2863 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2864 mCachedValidateSamplersResult.reset();
2865 }
2866}
2867
2868template <typename T>
2869void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2870 const uint8_t *destPointer,
2871 GLsizei clampedCount,
2872 const T *v)
2873{
2874}
2875
Jamie Madill62d31cb2015-09-11 13:25:51 -04002876template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002877GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002878{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002879 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2880 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002881 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2882
Corentin Wallez15ac5342016-11-03 17:06:39 -04002883 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2884 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2885 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002886 GLsizei maxElementCount =
2887 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2888
2889 GLsizei count = countIn;
2890 GLsizei clampedCount = count * vectorSize;
2891 if (clampedCount > maxElementCount)
2892 {
2893 clampedCount = maxElementCount;
2894 count = maxElementCount / vectorSize;
2895 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002896
Jamie Madill62d31cb2015-09-11 13:25:51 -04002897 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2898 {
2899 // Do a cast conversion for boolean types. From the spec:
2900 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2901 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002902 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002903 {
2904 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2905 }
2906 }
2907 else
2908 {
Jamie Madille7d84322017-01-10 18:21:59 -05002909 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002910 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002911 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002912
2913 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002914}
2915
2916template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002917GLsizei Program::setMatrixUniformInternal(GLint location,
2918 GLsizei count,
2919 GLboolean transpose,
2920 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002921{
2922 if (!transpose)
2923 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002924 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002925 }
2926
2927 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002928 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2929 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002930 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002931
2932 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2933 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2934 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2935 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2936
2937 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002938 {
2939 size_t elementOffset = element * rows * cols;
2940
2941 for (size_t row = 0; row < rows; ++row)
2942 {
2943 for (size_t col = 0; col < cols; ++col)
2944 {
2945 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2946 }
2947 }
2948 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002949
2950 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002951}
2952
2953template <typename DestT>
2954void Program::getUniformInternal(GLint location, DestT *dataOut) const
2955{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002956 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2957 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002958
2959 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2960
2961 GLenum componentType = VariableComponentType(uniform.type);
2962 if (componentType == GLTypeToGLenum<DestT>::value)
2963 {
2964 memcpy(dataOut, srcPointer, uniform.getElementSize());
2965 return;
2966 }
2967
Corentin Wallez6596c462016-03-17 17:26:58 -04002968 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002969
2970 switch (componentType)
2971 {
2972 case GL_INT:
2973 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2974 break;
2975 case GL_UNSIGNED_INT:
2976 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2977 break;
2978 case GL_BOOL:
2979 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2980 break;
2981 case GL_FLOAT:
2982 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2983 break;
2984 default:
2985 UNREACHABLE();
2986 }
2987}
Jamie Madilla4595b82017-01-11 17:36:34 -05002988
2989bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2990{
2991 // Must be called after samplers are validated.
2992 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2993
2994 for (const auto &binding : mState.mSamplerBindings)
2995 {
2996 GLenum textureType = binding.textureType;
2997 for (const auto &unit : binding.boundTextureUnits)
2998 {
2999 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3000 if (programTextureID == textureID)
3001 {
3002 // TODO(jmadill): Check for appropriate overlap.
3003 return true;
3004 }
3005 }
3006 }
3007
3008 return false;
3009}
3010
Jamie Madilla2c74982016-12-12 11:20:42 -05003011} // namespace gl