blob: b7c5f1ad13171d84ad4689c27d2fce07a6911c10 [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"
Jamie Madill6c58b062017-08-01 13:44:25 -040026#include "libANGLE/histogram_macros.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040027#include "libANGLE/queryconversions.h"
28#include "libANGLE/renderer/GLImplFactory.h"
29#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill6c58b062017-08-01 13:44:25 -040030#include "platform/Platform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace gl
33{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000034
Geoff Lang7dd2e102014-11-10 15:19:26 -050035namespace
36{
37
Jamie Madill62d31cb2015-09-11 13:25:51 -040038// This simplified cast function doesn't need to worry about advanced concepts like
39// depth range values, or casting to bool.
40template <typename DestT, typename SrcT>
41DestT UniformStateQueryCast(SrcT value);
42
43// From-Float-To-Integer Casts
44template <>
45GLint UniformStateQueryCast(GLfloat value)
46{
47 return clampCast<GLint>(roundf(value));
48}
49
50template <>
51GLuint UniformStateQueryCast(GLfloat value)
52{
53 return clampCast<GLuint>(roundf(value));
54}
55
56// From-Integer-to-Integer Casts
57template <>
58GLint UniformStateQueryCast(GLuint value)
59{
60 return clampCast<GLint>(value);
61}
62
63template <>
64GLuint UniformStateQueryCast(GLint value)
65{
66 return clampCast<GLuint>(value);
67}
68
69// From-Boolean-to-Anything Casts
70template <>
71GLfloat UniformStateQueryCast(GLboolean value)
72{
73 return (value == GL_TRUE ? 1.0f : 0.0f);
74}
75
76template <>
77GLint UniformStateQueryCast(GLboolean value)
78{
79 return (value == GL_TRUE ? 1 : 0);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLboolean value)
84{
85 return (value == GL_TRUE ? 1u : 0u);
86}
87
88// Default to static_cast
89template <typename DestT, typename SrcT>
90DestT UniformStateQueryCast(SrcT value)
91{
92 return static_cast<DestT>(value);
93}
94
95template <typename SrcT, typename DestT>
96void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
97{
98 for (int comp = 0; comp < components; ++comp)
99 {
100 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
101 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
102 size_t offset = comp * 4;
103 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
104 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
105 }
106}
107
Jamie Madill192745a2016-12-22 15:58:21 -0500108// true if varying x has a higher priority in packing than y
109bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
110{
jchen10a9042d32017-03-17 08:50:45 +0800111 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
112 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
113 sh::ShaderVariable vx, vy;
114 const sh::ShaderVariable *px, *py;
115 if (x.isArrayElement())
116 {
117 vx = *x.varying;
118 vx.arraySize = 0;
119 px = &vx;
120 }
121 else
122 {
123 px = x.varying;
124 }
125
126 if (y.isArrayElement())
127 {
128 vy = *y.varying;
129 vy.arraySize = 0;
130 py = &vy;
131 }
132 else
133 {
134 py = y.varying;
135 }
136
137 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500138}
139
jchen1015015f72017-03-16 13:54:21 +0800140template <typename VarT>
141GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
142{
143 size_t subscript = GL_INVALID_INDEX;
144 std::string baseName = ParseResourceName(name, &subscript);
145
146 // The app is not allowed to specify array indices other than 0 for arrays of basic types
147 if (subscript != 0 && subscript != GL_INVALID_INDEX)
148 {
149 return GL_INVALID_INDEX;
150 }
151
152 for (size_t index = 0; index < list.size(); index++)
153 {
154 const VarT &resource = list[index];
155 if (resource.name == baseName)
156 {
157 if (resource.isArray() || subscript == GL_INVALID_INDEX)
158 {
159 return static_cast<GLuint>(index);
160 }
161 }
162 }
163
164 return GL_INVALID_INDEX;
165}
166
jchen10fd7c3b52017-03-21 15:36:03 +0800167void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
168{
169 ASSERT(bufSize > 0);
170 strncpy(buffer, string.c_str(), bufSize);
171 buffer[bufSize - 1] = '\0';
172
173 if (length)
174 {
175 *length = static_cast<GLsizei>(strlen(buffer));
176 }
177}
178
jchen10a9042d32017-03-17 08:50:45 +0800179bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
180{
181 size_t subscript = GL_INVALID_INDEX;
182 std::string baseName = ParseResourceName(name, &subscript);
183 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
184 {
185 size_t arrayIndex = GL_INVALID_INDEX;
186 std::string arrayName = ParseResourceName(*it, &arrayIndex);
187 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
188 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
189 {
190 return true;
191 }
192 }
193 return false;
194}
195
Jiajia Qin729b2c62017-08-14 09:36:11 +0800196bool validateInterfaceBlocksCount(GLuint maxInterfaceBlocks,
197 const std::vector<sh::InterfaceBlock> &interfaceBlocks,
198 const std::string &errorMessage,
199 InfoLog &infoLog)
200{
201 GLuint blockCount = 0;
202 for (const sh::InterfaceBlock &block : interfaceBlocks)
203 {
204 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
205 {
206 blockCount += (block.arraySize ? block.arraySize : 1);
207 if (blockCount > maxInterfaceBlocks)
208 {
209 infoLog << errorMessage << maxInterfaceBlocks << ")";
210 return false;
211 }
212 }
213 }
214 return true;
215}
216
Jamie Madill62d31cb2015-09-11 13:25:51 -0400217} // anonymous namespace
218
Jamie Madill4a3c2342015-10-08 12:58:45 -0400219const char *const g_fakepath = "C:\\fakepath";
220
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400221InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000222{
223}
224
225InfoLog::~InfoLog()
226{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000227}
228
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400229size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000230{
Jamie Madill23176ce2017-07-31 14:14:33 -0400231 if (!mLazyStream)
232 {
233 return 0;
234 }
235
236 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400237 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000238}
239
Geoff Lange1a27752015-10-05 13:16:04 -0400240void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000241{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400242 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000243
244 if (bufSize > 0)
245 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400246 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400247
Jamie Madill23176ce2017-07-31 14:14:33 -0400248 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000249 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400250 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
251 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000252 }
253
254 infoLog[index] = '\0';
255 }
256
257 if (length)
258 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400259 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000260 }
261}
262
263// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300264// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265// messages, so lets remove all occurrences of this fake file path from the log.
266void InfoLog::appendSanitized(const char *message)
267{
Jamie Madill23176ce2017-07-31 14:14:33 -0400268 ensureInitialized();
269
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000270 std::string msg(message);
271
272 size_t found;
273 do
274 {
275 found = msg.find(g_fakepath);
276 if (found != std::string::npos)
277 {
278 msg.erase(found, strlen(g_fakepath));
279 }
280 }
281 while (found != std::string::npos);
282
Jamie Madill23176ce2017-07-31 14:14:33 -0400283 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000284}
285
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000286void InfoLog::reset()
287{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000288}
289
Jamie Madillfe8b5982017-09-07 17:00:18 -0400290VariableLocation::VariableLocation()
291 : name(), element(0), index(GL_INVALID_INDEX), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000292{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500293}
294
Geoff Langd8605522016-04-13 10:19:12 -0400295VariableLocation::VariableLocation(const std::string &name,
296 unsigned int element,
297 unsigned int index)
298 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500299{
300}
301
Geoff Langd8605522016-04-13 10:19:12 -0400302void Program::Bindings::bindLocation(GLuint index, const std::string &name)
303{
304 mBindings[name] = index;
305}
306
307int Program::Bindings::getBinding(const std::string &name) const
308{
309 auto iter = mBindings.find(name);
310 return (iter != mBindings.end()) ? iter->second : -1;
311}
312
313Program::Bindings::const_iterator Program::Bindings::begin() const
314{
315 return mBindings.begin();
316}
317
318Program::Bindings::const_iterator Program::Bindings::end() const
319{
320 return mBindings.end();
321}
322
Jamie Madill48ef11b2016-04-27 15:21:52 -0400323ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500324 : mLabel(),
325 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400326 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300327 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500328 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500329 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800330 mImageUniformRange(0, 0),
331 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300332 mBinaryRetrieveableHint(false),
333 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400334{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300335 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400336}
337
Jamie Madill48ef11b2016-04-27 15:21:52 -0400338ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400339{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500340 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400341}
342
Jamie Madill48ef11b2016-04-27 15:21:52 -0400343const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500344{
345 return mLabel;
346}
347
Jamie Madill48ef11b2016-04-27 15:21:52 -0400348GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400349{
350 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800351 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400352
353 for (size_t location = 0; location < mUniformLocations.size(); ++location)
354 {
355 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400356 if (!uniformLocation.used)
357 {
358 continue;
359 }
360
361 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400362
363 if (uniform.name == baseName)
364 {
Geoff Langd8605522016-04-13 10:19:12 -0400365 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400366 {
Geoff Langd8605522016-04-13 10:19:12 -0400367 if (uniformLocation.element == subscript ||
368 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
369 {
370 return static_cast<GLint>(location);
371 }
372 }
373 else
374 {
375 if (subscript == GL_INVALID_INDEX)
376 {
377 return static_cast<GLint>(location);
378 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400379 }
380 }
381 }
382
383 return -1;
384}
385
Jamie Madille7d84322017-01-10 18:21:59 -0500386GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400387{
jchen1015015f72017-03-16 13:54:21 +0800388 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400389}
390
Jamie Madille7d84322017-01-10 18:21:59 -0500391GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
392{
393 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
394 return mUniformLocations[location].index;
395}
396
397Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
398{
399 GLuint index = getUniformIndexFromLocation(location);
400 if (!isSamplerUniformIndex(index))
401 {
402 return Optional<GLuint>::Invalid();
403 }
404
405 return getSamplerIndexFromUniformIndex(index);
406}
407
408bool ProgramState::isSamplerUniformIndex(GLuint index) const
409{
Jamie Madill982f6e02017-06-07 14:33:04 -0400410 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500411}
412
413GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
414{
415 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400416 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500417}
418
Jamie Madill34ca4f52017-06-13 11:49:39 -0400419GLuint ProgramState::getAttributeLocation(const std::string &name) const
420{
421 for (const sh::Attribute &attribute : mAttributes)
422 {
423 if (attribute.name == name)
424 {
425 return attribute.location;
426 }
427 }
428
429 return static_cast<GLuint>(-1);
430}
431
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500432Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400433 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400434 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500435 mLinked(false),
436 mDeleteStatus(false),
437 mRefCount(0),
438 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500439 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500440{
441 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000442
Geoff Lang7dd2e102014-11-10 15:19:26 -0500443 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444}
445
446Program::~Program()
447{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400448 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449}
450
Jamie Madill4928b7c2017-06-20 12:57:39 -0400451void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500452{
453 if (mState.mAttachedVertexShader != nullptr)
454 {
455 mState.mAttachedVertexShader->release(context);
456 mState.mAttachedVertexShader = nullptr;
457 }
458
459 if (mState.mAttachedFragmentShader != nullptr)
460 {
461 mState.mAttachedFragmentShader->release(context);
462 mState.mAttachedFragmentShader = nullptr;
463 }
464
465 if (mState.mAttachedComputeShader != nullptr)
466 {
467 mState.mAttachedComputeShader->release(context);
468 mState.mAttachedComputeShader = nullptr;
469 }
470
Jamie Madillc564c072017-06-01 12:45:42 -0400471 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400472
473 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
474 !mState.mAttachedComputeShader);
475 SafeDelete(mProgram);
476
477 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500478}
479
Geoff Lang70d0f492015-12-10 17:45:46 -0500480void Program::setLabel(const std::string &label)
481{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400482 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500483}
484
485const std::string &Program::getLabel() const
486{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400487 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500488}
489
Jamie Madillef300b12016-10-07 15:12:09 -0400490void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300492 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300494 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 {
Jamie Madillef300b12016-10-07 15:12:09 -0400496 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300497 mState.mAttachedVertexShader = shader;
498 mState.mAttachedVertexShader->addRef();
499 break;
500 }
501 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502 {
Jamie Madillef300b12016-10-07 15:12:09 -0400503 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300504 mState.mAttachedFragmentShader = shader;
505 mState.mAttachedFragmentShader->addRef();
506 break;
507 }
508 case GL_COMPUTE_SHADER:
509 {
Jamie Madillef300b12016-10-07 15:12:09 -0400510 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300511 mState.mAttachedComputeShader = shader;
512 mState.mAttachedComputeShader->addRef();
513 break;
514 }
515 default:
516 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518}
519
Jamie Madillc1d770e2017-04-13 17:31:24 -0400520void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300522 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300524 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400526 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500527 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300528 mState.mAttachedVertexShader = nullptr;
529 break;
530 }
531 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400533 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500534 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300535 mState.mAttachedFragmentShader = nullptr;
536 break;
537 }
538 case GL_COMPUTE_SHADER:
539 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400540 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500541 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300542 mState.mAttachedComputeShader = nullptr;
543 break;
544 }
545 default:
546 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548}
549
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000550int Program::getAttachedShadersCount() const
551{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300552 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
553 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000554}
555
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556void Program::bindAttributeLocation(GLuint index, const char *name)
557{
Geoff Langd8605522016-04-13 10:19:12 -0400558 mAttributeBindings.bindLocation(index, name);
559}
560
561void Program::bindUniformLocation(GLuint index, const char *name)
562{
563 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800564 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565}
566
Sami Väisänen46eaa942016-06-29 10:26:37 +0300567void Program::bindFragmentInputLocation(GLint index, const char *name)
568{
569 mFragmentInputBindings.bindLocation(index, name);
570}
571
Jamie Madillbd044ed2017-06-05 12:59:21 -0400572BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300573{
574 BindingInfo ret;
575 ret.type = GL_NONE;
576 ret.valid = false;
577
Jamie Madillbd044ed2017-06-05 12:59:21 -0400578 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300579 ASSERT(fragmentShader);
580
581 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400582 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300583
584 for (const auto &binding : mFragmentInputBindings)
585 {
586 if (binding.second != static_cast<GLuint>(index))
587 continue;
588
589 ret.valid = true;
590
591 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400592 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300593
594 for (const auto &in : inputs)
595 {
596 if (in.name == originalName)
597 {
598 if (in.isArray())
599 {
600 // The client wants to bind either "name" or "name[0]".
601 // GL ES 3.1 spec refers to active array names with language such as:
602 // "if the string identifies the base name of an active array, where the
603 // string would exactly match the name of the variable if the suffix "[0]"
604 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400605 if (arrayIndex == GL_INVALID_INDEX)
606 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300607
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400608 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300609 }
610 else
611 {
612 ret.name = in.mappedName;
613 }
614 ret.type = in.type;
615 return ret;
616 }
617 }
618 }
619
620 return ret;
621}
622
Jamie Madillbd044ed2017-06-05 12:59:21 -0400623void Program::pathFragmentInputGen(const Context *context,
624 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300625 GLenum genMode,
626 GLint components,
627 const GLfloat *coeffs)
628{
629 // If the location is -1 then the command is silently ignored
630 if (index == -1)
631 return;
632
Jamie Madillbd044ed2017-06-05 12:59:21 -0400633 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300634
635 // If the input doesn't exist then then the command is silently ignored
636 // This could happen through optimization for example, the shader translator
637 // decides that a variable is not actually being used and optimizes it away.
638 if (binding.name.empty())
639 return;
640
641 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
642}
643
Martin Radev4c4c8e72016-08-04 12:25:34 +0300644// The attached shaders are checked for linking errors by matching up their variables.
645// Uniform, input and output variables get collected.
646// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500647Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000648{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500649 const auto &data = context->getContextState();
650
Jamie Madill6c58b062017-08-01 13:44:25 -0400651 auto *platform = ANGLEPlatformCurrent();
652 double startTime = platform->currentTime(platform);
653
Jamie Madill6c1f6712017-02-14 19:08:04 -0500654 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000655
Jamie Madill32447362017-06-28 14:53:52 -0400656 ProgramHash programHash;
657 auto *cache = context->getMemoryProgramCache();
658 if (cache)
659 {
660 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400661 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400662 }
663
664 if (mLinked)
665 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400666 double delta = platform->currentTime(platform) - startTime;
667 int us = static_cast<int>(delta * 1000000.0);
668 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400669 return NoError();
670 }
671
672 // Cache load failed, fall through to normal linking.
673 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000674 mInfoLog.reset();
675
Martin Radev4c4c8e72016-08-04 12:25:34 +0300676 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677
Jamie Madill192745a2016-12-22 15:58:21 -0500678 auto vertexShader = mState.mAttachedVertexShader;
679 auto fragmentShader = mState.mAttachedFragmentShader;
680 auto computeShader = mState.mAttachedComputeShader;
681
682 bool isComputeShaderAttached = (computeShader != nullptr);
683 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300684 // Check whether we both have a compute and non-compute shaders attached.
685 // If there are of both types attached, then linking should fail.
686 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
687 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500688 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
690 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400691 }
692
Jamie Madill192745a2016-12-22 15:58:21 -0500693 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500694 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400695 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300696 {
697 mInfoLog << "Attached compute shader is not compiled.";
698 return NoError();
699 }
Jamie Madill192745a2016-12-22 15:58:21 -0500700 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701
Jamie Madillbd044ed2017-06-05 12:59:21 -0400702 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703
704 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
705 // If the work group size is not specified, a link time error should occur.
706 if (!mState.mComputeShaderLocalSize.isDeclared())
707 {
708 mInfoLog << "Work group size is not specified.";
709 return NoError();
710 }
711
Jamie Madillbd044ed2017-06-05 12:59:21 -0400712 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300713 {
714 return NoError();
715 }
716
Jiajia Qin729b2c62017-08-14 09:36:11 +0800717 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300718 {
719 return NoError();
720 }
721
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500722 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400723 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500724 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300725 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500726 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300727 }
728 }
729 else
730 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400731 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300732 {
733 return NoError();
734 }
Jamie Madill192745a2016-12-22 15:58:21 -0500735 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300736
Jamie Madillbd044ed2017-06-05 12:59:21 -0400737 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300738 {
739 return NoError();
740 }
Jamie Madill192745a2016-12-22 15:58:21 -0500741 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300742
Jamie Madillbd044ed2017-06-05 12:59:21 -0400743 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300744 {
745 mInfoLog << "Fragment shader version does not match vertex shader version.";
746 return NoError();
747 }
748
Jamie Madillbd044ed2017-06-05 12:59:21 -0400749 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300750 {
751 return NoError();
752 }
753
Jamie Madillbd044ed2017-06-05 12:59:21 -0400754 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300755 {
756 return NoError();
757 }
758
Jamie Madillbd044ed2017-06-05 12:59:21 -0400759 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300760 {
761 return NoError();
762 }
763
Jiajia Qin729b2c62017-08-14 09:36:11 +0800764 if (!linkInterfaceBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300765 {
766 return NoError();
767 }
768
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400769 if (!linkValidateGlobalNames(context, mInfoLog))
770 {
771 return NoError();
772 }
773
Jamie Madillbd044ed2017-06-05 12:59:21 -0400774 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300775
Martin Radev7cf61662017-07-26 17:10:53 +0300776 mState.mNumViews = vertexShader->getNumViews(context);
777
Jamie Madillbd044ed2017-06-05 12:59:21 -0400778 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300779
Jamie Madill192745a2016-12-22 15:58:21 -0500780 // Validate we can pack the varyings.
781 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
782
783 // Map the varyings to the register file
784 // In WebGL, we use a slightly different handling for packing variables.
785 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
786 : PackMode::ANGLE_RELAXED;
787 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
788 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
789 mState.getTransformFeedbackVaryingNames()))
790 {
791 return NoError();
792 }
793
Olli Etuaho39e78122017-08-29 14:34:22 +0300794 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
795 {
796 return NoError();
797 }
798
Jamie Madillc564c072017-06-01 12:45:42 -0400799 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500800 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300801 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500802 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300803 }
804
805 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500806 }
807
jchen10eaef1e52017-06-13 10:44:11 +0800808 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400809 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400810
jchen10eaef1e52017-06-13 10:44:11 +0800811 setUniformValuesFromBindingQualifiers();
812
Jamie Madill54164b02017-08-28 15:17:37 -0400813 // Mark implementation-specific unreferenced uniforms as ignored.
814 mProgram->markUnusedUniformLocations(&mState.mUniformLocations);
815
816 // Update sampler bindings with unreferenced uniforms.
817 for (const auto &location : mState.mUniformLocations)
818 {
819 if (!location.used && mState.isSamplerUniformIndex(location.index))
820 {
821 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(location.index);
822 mState.mSamplerBindings[samplerIndex].unreferenced = true;
823 }
824 }
825
Jamie Madill32447362017-06-28 14:53:52 -0400826 // Save to the program cache.
827 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
828 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
829 {
830 cache->putProgram(programHash, context, this);
831 }
832
Jamie Madill6c58b062017-08-01 13:44:25 -0400833 double delta = platform->currentTime(platform) - startTime;
834 int us = static_cast<int>(delta * 1000000.0);
835 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
836
Martin Radev4c4c8e72016-08-04 12:25:34 +0300837 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000838}
839
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000840// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500841void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400843 mState.mAttributes.clear();
844 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800845 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400846 mState.mUniforms.clear();
847 mState.mUniformLocations.clear();
848 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800849 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800850 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400851 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800852 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400853 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400854 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300855 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500856 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800857 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300858 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500859
Geoff Lang7dd2e102014-11-10 15:19:26 -0500860 mValidated = false;
861
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000862 mLinked = false;
863}
864
Geoff Lange1a27752015-10-05 13:16:04 -0400865bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000866{
867 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
Jamie Madilla2c74982016-12-12 11:20:42 -0500870Error Program::loadBinary(const Context *context,
871 GLenum binaryFormat,
872 const void *binary,
873 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000874{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500875 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000876
Geoff Lang7dd2e102014-11-10 15:19:26 -0500877#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800878 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500879#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400880 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
881 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000882 {
Jamie Madillf6113162015-05-07 11:49:21 -0400883 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800884 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885 }
886
Jamie Madill4f86d052017-06-05 12:59:26 -0400887 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
888 ANGLE_TRY_RESULT(
889 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400890
891 // Currently we require the full shader text to compute the program hash.
892 // TODO(jmadill): Store the binary in the internal program cache.
893
Jamie Madillb0a838b2016-11-13 20:02:12 -0500894 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500895#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500896}
897
Jamie Madilla2c74982016-12-12 11:20:42 -0500898Error Program::saveBinary(const Context *context,
899 GLenum *binaryFormat,
900 void *binary,
901 GLsizei bufSize,
902 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500903{
904 if (binaryFormat)
905 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400906 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500907 }
908
Jamie Madill4f86d052017-06-05 12:59:26 -0400909 angle::MemoryBuffer memoryBuf;
910 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500911
Jamie Madill4f86d052017-06-05 12:59:26 -0400912 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
913 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500914
915 if (streamLength > bufSize)
916 {
917 if (length)
918 {
919 *length = 0;
920 }
921
922 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
923 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
924 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500925 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500926 }
927
928 if (binary)
929 {
930 char *ptr = reinterpret_cast<char*>(binary);
931
Jamie Madill48ef11b2016-04-27 15:21:52 -0400932 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500933 ptr += streamLength;
934
935 ASSERT(ptr - streamLength == binary);
936 }
937
938 if (length)
939 {
940 *length = streamLength;
941 }
942
He Yunchaoacd18982017-01-04 10:46:42 +0800943 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500944}
945
Jamie Madillffe00c02017-06-27 16:26:55 -0400946GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947{
948 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400949 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500950 if (error.isError())
951 {
952 return 0;
953 }
954
955 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000956}
957
Geoff Langc5629752015-12-07 16:29:04 -0500958void Program::setBinaryRetrievableHint(bool retrievable)
959{
960 // TODO(jmadill) : replace with dirty bits
961 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400962 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500963}
964
965bool Program::getBinaryRetrievableHint() const
966{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400967 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500968}
969
Yunchao He61afff12017-03-14 15:34:03 +0800970void Program::setSeparable(bool separable)
971{
972 // TODO(yunchao) : replace with dirty bits
973 if (mState.mSeparable != separable)
974 {
975 mProgram->setSeparable(separable);
976 mState.mSeparable = separable;
977 }
978}
979
980bool Program::isSeparable() const
981{
982 return mState.mSeparable;
983}
984
Jamie Madill6c1f6712017-02-14 19:08:04 -0500985void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000986{
987 mRefCount--;
988
989 if (mRefCount == 0 && mDeleteStatus)
990 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500991 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000992 }
993}
994
995void Program::addRef()
996{
997 mRefCount++;
998}
999
1000unsigned int Program::getRefCount() const
1001{
1002 return mRefCount;
1003}
1004
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001005int Program::getInfoLogLength() const
1006{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001007 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001008}
1009
Geoff Lange1a27752015-10-05 13:16:04 -04001010void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001011{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001012 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001013}
1014
Geoff Lange1a27752015-10-05 13:16:04 -04001015void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001016{
1017 int total = 0;
1018
Martin Radev4c4c8e72016-08-04 12:25:34 +03001019 if (mState.mAttachedComputeShader)
1020 {
1021 if (total < maxCount)
1022 {
1023 shaders[total] = mState.mAttachedComputeShader->getHandle();
1024 total++;
1025 }
1026 }
1027
Jamie Madill48ef11b2016-04-27 15:21:52 -04001028 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001029 {
1030 if (total < maxCount)
1031 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001032 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001033 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001034 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001035 }
1036
Jamie Madill48ef11b2016-04-27 15:21:52 -04001037 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001038 {
1039 if (total < maxCount)
1040 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001041 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001042 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001043 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001044 }
1045
1046 if (count)
1047 {
1048 *count = total;
1049 }
1050}
1051
Geoff Lange1a27752015-10-05 13:16:04 -04001052GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001053{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001054 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055}
1056
Jamie Madill63805b42015-08-25 13:17:39 -04001057bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001058{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001059 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1060 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001061}
1062
jchen10fd7c3b52017-03-21 15:36:03 +08001063void Program::getActiveAttribute(GLuint index,
1064 GLsizei bufsize,
1065 GLsizei *length,
1066 GLint *size,
1067 GLenum *type,
1068 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001069{
Jamie Madillc349ec02015-08-21 16:53:12 -04001070 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001071 {
1072 if (bufsize > 0)
1073 {
1074 name[0] = '\0';
1075 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001077 if (length)
1078 {
1079 *length = 0;
1080 }
1081
1082 *type = GL_NONE;
1083 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001084 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001085 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001086
jchen1036e120e2017-03-14 14:53:58 +08001087 ASSERT(index < mState.mAttributes.size());
1088 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001089
1090 if (bufsize > 0)
1091 {
jchen10fd7c3b52017-03-21 15:36:03 +08001092 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001093 }
1094
1095 // Always a single 'type' instance
1096 *size = 1;
1097 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001098}
1099
Geoff Lange1a27752015-10-05 13:16:04 -04001100GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001101{
Jamie Madillc349ec02015-08-21 16:53:12 -04001102 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001103 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001104 return 0;
1105 }
1106
jchen1036e120e2017-03-14 14:53:58 +08001107 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108}
1109
Geoff Lange1a27752015-10-05 13:16:04 -04001110GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001111{
Jamie Madillc349ec02015-08-21 16:53:12 -04001112 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001113 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001114 return 0;
1115 }
1116
1117 size_t maxLength = 0;
1118
Jamie Madill48ef11b2016-04-27 15:21:52 -04001119 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001120 {
jchen1036e120e2017-03-14 14:53:58 +08001121 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001122 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123
Jamie Madillc349ec02015-08-21 16:53:12 -04001124 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001125}
1126
jchen1015015f72017-03-16 13:54:21 +08001127GLuint Program::getInputResourceIndex(const GLchar *name) const
1128{
1129 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1130 {
1131 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1132 if (attribute.name == name)
1133 {
1134 return attributeIndex;
1135 }
1136 }
1137 return GL_INVALID_INDEX;
1138}
1139
1140GLuint Program::getOutputResourceIndex(const GLchar *name) const
1141{
1142 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1143}
1144
jchen10fd7c3b52017-03-21 15:36:03 +08001145size_t Program::getOutputResourceCount() const
1146{
1147 return (mLinked ? mState.mOutputVariables.size() : 0);
1148}
1149
1150void Program::getInputResourceName(GLuint index,
1151 GLsizei bufSize,
1152 GLsizei *length,
1153 GLchar *name) const
1154{
1155 GLint size;
1156 GLenum type;
1157 getActiveAttribute(index, bufSize, length, &size, &type, name);
1158}
1159
1160void Program::getOutputResourceName(GLuint index,
1161 GLsizei bufSize,
1162 GLsizei *length,
1163 GLchar *name) const
1164{
1165 if (length)
1166 {
1167 *length = 0;
1168 }
1169
1170 if (!mLinked)
1171 {
1172 if (bufSize > 0)
1173 {
1174 name[0] = '\0';
1175 }
1176 return;
1177 }
1178 ASSERT(index < mState.mOutputVariables.size());
1179 const auto &output = mState.mOutputVariables[index];
1180
1181 if (bufSize > 0)
1182 {
1183 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1184
1185 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1186 }
1187}
1188
jchen10880683b2017-04-12 16:21:55 +08001189const sh::Attribute &Program::getInputResource(GLuint index) const
1190{
1191 ASSERT(index < mState.mAttributes.size());
1192 return mState.mAttributes[index];
1193}
1194
1195const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1196{
1197 ASSERT(index < mState.mOutputVariables.size());
1198 return mState.mOutputVariables[index];
1199}
1200
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201GLint Program::getFragDataLocation(const std::string &name) const
1202{
1203 std::string baseName(name);
1204 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001205 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001206 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001207 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001208 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1209 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001210 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001212 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001213 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001214}
1215
Geoff Lange1a27752015-10-05 13:16:04 -04001216void Program::getActiveUniform(GLuint index,
1217 GLsizei bufsize,
1218 GLsizei *length,
1219 GLint *size,
1220 GLenum *type,
1221 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001222{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001224 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001225 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001226 ASSERT(index < mState.mUniforms.size());
1227 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228
1229 if (bufsize > 0)
1230 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001231 std::string string = uniform.name;
1232 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233 {
1234 string += "[0]";
1235 }
jchen10fd7c3b52017-03-21 15:36:03 +08001236 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237 }
1238
Jamie Madill62d31cb2015-09-11 13:25:51 -04001239 *size = uniform.elementCount();
1240 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241 }
1242 else
1243 {
1244 if (bufsize > 0)
1245 {
1246 name[0] = '\0';
1247 }
1248
1249 if (length)
1250 {
1251 *length = 0;
1252 }
1253
1254 *size = 0;
1255 *type = GL_NONE;
1256 }
1257}
1258
Geoff Lange1a27752015-10-05 13:16:04 -04001259GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001260{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001261 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001262 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001263 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001264 }
1265 else
1266 {
1267 return 0;
1268 }
1269}
1270
Geoff Lange1a27752015-10-05 13:16:04 -04001271GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001272{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001273 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274
1275 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001276 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001277 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001279 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001281 size_t length = uniform.name.length() + 1u;
1282 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283 {
1284 length += 3; // Counting in "[0]".
1285 }
1286 maxLength = std::max(length, maxLength);
1287 }
1288 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290
Jamie Madill62d31cb2015-09-11 13:25:51 -04001291 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292}
1293
1294GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1295{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001296 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001297 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001298 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001299 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001300 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1301 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1302 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001303 case GL_UNIFORM_BLOCK_INDEX:
1304 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1306 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1307 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1308 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1309 default:
1310 UNREACHABLE();
1311 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001312 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313 return 0;
1314}
1315
1316bool Program::isValidUniformLocation(GLint location) const
1317{
Jamie Madille2e406c2016-06-02 13:04:10 -04001318 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001319 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1320 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001321}
1322
Jamie Madill62d31cb2015-09-11 13:25:51 -04001323const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001324{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001325 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001326 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001327}
1328
Jamie Madillac4e9c32017-01-13 14:07:12 -05001329const VariableLocation &Program::getUniformLocation(GLint location) const
1330{
1331 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1332 return mState.mUniformLocations[location];
1333}
1334
1335const std::vector<VariableLocation> &Program::getUniformLocations() const
1336{
1337 return mState.mUniformLocations;
1338}
1339
1340const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1341{
1342 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1343 return mState.mUniforms[index];
1344}
1345
Jamie Madill62d31cb2015-09-11 13:25:51 -04001346GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001348 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349}
1350
Jamie Madill62d31cb2015-09-11 13:25:51 -04001351GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001352{
Jamie Madille7d84322017-01-10 18:21:59 -05001353 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354}
1355
1356void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1357{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001358 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1359 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001360 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361}
1362
1363void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1364{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001365 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1366 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001367 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368}
1369
1370void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1371{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001372 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1373 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001374 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375}
1376
1377void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1378{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001379 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1380 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001381 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382}
1383
Jamie Madill81c2e252017-09-09 23:32:46 -04001384Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001386 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1387 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1388
Jamie Madill81c2e252017-09-09 23:32:46 -04001389 mProgram->setUniform1iv(location, clampedCount, v);
1390
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001391 if (mState.isSamplerUniformIndex(locationInfo.index))
1392 {
1393 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001394 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001395 }
1396
Jamie Madill81c2e252017-09-09 23:32:46 -04001397 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398}
1399
1400void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1401{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001402 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1403 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001404 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405}
1406
1407void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1408{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001409 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1410 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001411 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412}
1413
1414void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1415{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001416 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1417 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001418 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419}
1420
1421void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1422{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001423 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1424 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001425 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426}
1427
1428void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1429{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001430 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1431 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001432 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433}
1434
1435void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1436{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001437 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1438 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001439 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440}
1441
1442void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1443{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001444 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1445 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001446 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447}
1448
1449void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1450{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001451 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001452 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001453}
1454
1455void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1456{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001457 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001458 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459}
1460
1461void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1462{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001463 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001464 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465}
1466
1467void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1468{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001469 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001470 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471}
1472
1473void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1474{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001475 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001476 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477}
1478
1479void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1480{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001481 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001482 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483}
1484
1485void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1486{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001487 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001488 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489}
1490
1491void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1492{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001493 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001494 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495}
1496
1497void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1498{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001499 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001500 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501}
1502
Jamie Madill54164b02017-08-28 15:17:37 -04001503void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504{
Jamie Madill54164b02017-08-28 15:17:37 -04001505 const auto &uniformLocation = mState.getUniformLocations()[location];
1506 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1507
1508 GLenum nativeType = gl::VariableComponentType(uniform.type);
1509 if (nativeType == GL_FLOAT)
1510 {
1511 mProgram->getUniformfv(context, location, v);
1512 }
1513 else
1514 {
1515 getUniformInternal(context, v, location, nativeType,
1516 gl::VariableComponentCount(uniform.type));
1517 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518}
1519
Jamie Madill54164b02017-08-28 15:17:37 -04001520void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521{
Jamie Madill54164b02017-08-28 15:17:37 -04001522 const auto &uniformLocation = mState.getUniformLocations()[location];
1523 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1524
1525 GLenum nativeType = gl::VariableComponentType(uniform.type);
1526 if (nativeType == GL_INT || nativeType == GL_BOOL)
1527 {
1528 mProgram->getUniformiv(context, location, v);
1529 }
1530 else
1531 {
1532 getUniformInternal(context, v, location, nativeType,
1533 gl::VariableComponentCount(uniform.type));
1534 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535}
1536
Jamie Madill54164b02017-08-28 15:17:37 -04001537void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001538{
Jamie Madill54164b02017-08-28 15:17:37 -04001539 const auto &uniformLocation = mState.getUniformLocations()[location];
1540 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1541
1542 GLenum nativeType = gl::VariableComponentType(uniform.type);
1543 if (nativeType == GL_UNSIGNED_INT)
1544 {
1545 mProgram->getUniformuiv(context, location, v);
1546 }
1547 else
1548 {
1549 getUniformInternal(context, v, location, nativeType,
1550 gl::VariableComponentCount(uniform.type));
1551 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552}
1553
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001554void Program::flagForDeletion()
1555{
1556 mDeleteStatus = true;
1557}
1558
1559bool Program::isFlaggedForDeletion() const
1560{
1561 return mDeleteStatus;
1562}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001563
Brandon Jones43a53e22014-08-28 16:23:22 -07001564void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001565{
1566 mInfoLog.reset();
1567
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001569 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001570 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001571 }
1572 else
1573 {
Jamie Madillf6113162015-05-07 11:49:21 -04001574 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001575 }
1576}
1577
Geoff Lang7dd2e102014-11-10 15:19:26 -05001578bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1579{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001580 // Skip cache if we're using an infolog, so we get the full error.
1581 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1582 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1583 {
1584 return mCachedValidateSamplersResult.value();
1585 }
1586
1587 if (mTextureUnitTypesCache.empty())
1588 {
1589 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1590 }
1591 else
1592 {
1593 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1594 }
1595
1596 // if any two active samplers in a program are of different types, but refer to the same
1597 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1598 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001599 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001600 {
Jamie Madill54164b02017-08-28 15:17:37 -04001601 if (samplerBinding.unreferenced)
1602 continue;
1603
Jamie Madille7d84322017-01-10 18:21:59 -05001604 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001605
Jamie Madille7d84322017-01-10 18:21:59 -05001606 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001607 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001608 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1609 {
1610 if (infoLog)
1611 {
1612 (*infoLog) << "Sampler uniform (" << textureUnit
1613 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1614 << caps.maxCombinedTextureImageUnits << ")";
1615 }
1616
1617 mCachedValidateSamplersResult = false;
1618 return false;
1619 }
1620
1621 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1622 {
1623 if (textureType != mTextureUnitTypesCache[textureUnit])
1624 {
1625 if (infoLog)
1626 {
1627 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1628 "image unit ("
1629 << textureUnit << ").";
1630 }
1631
1632 mCachedValidateSamplersResult = false;
1633 return false;
1634 }
1635 }
1636 else
1637 {
1638 mTextureUnitTypesCache[textureUnit] = textureType;
1639 }
1640 }
1641 }
1642
1643 mCachedValidateSamplersResult = true;
1644 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645}
1646
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001647bool Program::isValidated() const
1648{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001649 return mValidated;
1650}
1651
Geoff Lange1a27752015-10-05 13:16:04 -04001652GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001654 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655}
1656
Jiajia Qin729b2c62017-08-14 09:36:11 +08001657GLuint Program::getActiveShaderStorageBlockCount() const
1658{
1659 return static_cast<GLuint>(mState.mShaderStorageBlocks.size());
1660}
1661
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1663{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001664 ASSERT(
1665 uniformBlockIndex <
1666 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667
Jiajia Qin729b2c62017-08-14 09:36:11 +08001668 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001669
1670 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001671 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672 std::string string = uniformBlock.name;
1673
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001676 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677 }
jchen10fd7c3b52017-03-21 15:36:03 +08001678 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001679 }
1680}
1681
Geoff Lange1a27752015-10-05 13:16:04 -04001682GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001683{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001684 int maxLength = 0;
1685
1686 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001687 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001688 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1690 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001691 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001692 if (!uniformBlock.name.empty())
1693 {
jchen10af713a22017-04-19 09:10:56 +08001694 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1695 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696 }
1697 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001698 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001699
1700 return maxLength;
1701}
1702
Geoff Lange1a27752015-10-05 13:16:04 -04001703GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001705 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001706 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001707
Jamie Madill48ef11b2016-04-27 15:21:52 -04001708 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001709 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1710 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08001711 const InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001712 if (uniformBlock.name == baseName)
1713 {
1714 const bool arrayElementZero =
1715 (subscript == GL_INVALID_INDEX &&
1716 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1717 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1718 {
1719 return blockIndex;
1720 }
1721 }
1722 }
1723
1724 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001725}
1726
Jiajia Qin729b2c62017-08-14 09:36:11 +08001727const InterfaceBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001728{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001729 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1730 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001731}
1732
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001733void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1734{
jchen107a20b972017-06-13 14:25:26 +08001735 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001736 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001737 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001738}
1739
1740GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1741{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001742 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001743}
1744
Jiajia Qin729b2c62017-08-14 09:36:11 +08001745GLuint Program::getShaderStorageBlockBinding(GLuint shaderStorageBlockIndex) const
1746{
1747 return mState.getShaderStorageBlockBinding(shaderStorageBlockIndex);
1748}
1749
Geoff Lang48dcae72014-02-05 16:28:24 -05001750void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1751{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001752 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001753 for (GLsizei i = 0; i < count; i++)
1754 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001755 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001756 }
1757
Jamie Madill48ef11b2016-04-27 15:21:52 -04001758 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001759}
1760
1761void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1762{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001764 {
jchen10a9042d32017-03-17 08:50:45 +08001765 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1766 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1767 std::string varName = var.nameWithArrayIndex();
1768 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001769 if (length)
1770 {
1771 *length = lastNameIdx;
1772 }
1773 if (size)
1774 {
jchen10a9042d32017-03-17 08:50:45 +08001775 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001776 }
1777 if (type)
1778 {
jchen10a9042d32017-03-17 08:50:45 +08001779 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001780 }
1781 if (name)
1782 {
jchen10a9042d32017-03-17 08:50:45 +08001783 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001784 name[lastNameIdx] = '\0';
1785 }
1786 }
1787}
1788
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001789GLsizei Program::getTransformFeedbackVaryingCount() const
1790{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001792 {
jchen10a9042d32017-03-17 08:50:45 +08001793 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001794 }
1795 else
1796 {
1797 return 0;
1798 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001799}
1800
1801GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1802{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001804 {
1805 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001806 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001807 {
jchen10a9042d32017-03-17 08:50:45 +08001808 maxSize =
1809 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001810 }
1811
1812 return maxSize;
1813 }
1814 else
1815 {
1816 return 0;
1817 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001818}
1819
1820GLenum Program::getTransformFeedbackBufferMode() const
1821{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001822 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823}
1824
Jamie Madillbd044ed2017-06-05 12:59:21 -04001825bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001827 Shader *vertexShader = mState.mAttachedVertexShader;
1828 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001829
Jamie Madillbd044ed2017-06-05 12:59:21 -04001830 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001831
Jamie Madillbd044ed2017-06-05 12:59:21 -04001832 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1833 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834
Sami Väisänen46eaa942016-06-29 10:26:37 +03001835 std::map<GLuint, std::string> staticFragmentInputLocations;
1836
Jamie Madill4cff2472015-08-21 16:53:18 -04001837 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 bool matched = false;
1840
1841 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001842 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001843 {
1844 continue;
1845 }
1846
Jamie Madill4cff2472015-08-21 16:53:18 -04001847 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001849 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001851 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001852 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001853 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 {
1855 return false;
1856 }
1857
Geoff Lang7dd2e102014-11-10 15:19:26 -05001858 matched = true;
1859 break;
1860 }
1861 }
1862
1863 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001864 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001866 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867 return false;
1868 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001869
1870 // Check for aliased path rendering input bindings (if any).
1871 // If more than one binding refer statically to the same
1872 // location the link must fail.
1873
1874 if (!output.staticUse)
1875 continue;
1876
1877 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1878 if (inputBinding == -1)
1879 continue;
1880
1881 const auto it = staticFragmentInputLocations.find(inputBinding);
1882 if (it == std::end(staticFragmentInputLocations))
1883 {
1884 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1885 }
1886 else
1887 {
1888 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1889 << it->second;
1890 return false;
1891 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 }
1893
Jamie Madillbd044ed2017-06-05 12:59:21 -04001894 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001895 {
1896 return false;
1897 }
1898
Jamie Madillada9ecc2015-08-17 12:53:37 -04001899 // TODO(jmadill): verify no unmatched vertex varyings?
1900
Geoff Lang7dd2e102014-11-10 15:19:26 -05001901 return true;
1902}
1903
Jamie Madillbd044ed2017-06-05 12:59:21 -04001904bool Program::linkUniforms(const Context *context,
1905 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001906 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001907{
Olli Etuahob78707c2017-03-09 15:03:11 +00001908 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001909 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001910 {
1911 return false;
1912 }
1913
Olli Etuahob78707c2017-03-09 15:03:11 +00001914 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001915
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001916 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001917
jchen10eaef1e52017-06-13 10:44:11 +08001918 if (!linkAtomicCounterBuffers())
1919 {
1920 return false;
1921 }
1922
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001923 return true;
1924}
1925
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001926void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001927{
Jamie Madill982f6e02017-06-07 14:33:04 -04001928 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1929 unsigned int low = high;
1930
jchen10eaef1e52017-06-13 10:44:11 +08001931 for (auto counterIter = mState.mUniforms.rbegin();
1932 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1933 {
1934 --low;
1935 }
1936
1937 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1938
1939 high = low;
1940
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001941 for (auto imageIter = mState.mUniforms.rbegin();
1942 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1943 {
1944 --low;
1945 }
1946
1947 mState.mImageUniformRange = RangeUI(low, high);
1948
1949 // If uniform is a image type, insert it into the mImageBindings array.
1950 for (unsigned int imageIndex : mState.mImageUniformRange)
1951 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001952 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1953 // cannot load values into a uniform defined as an image. if declare without a
1954 // binding qualifier, any uniform image variable (include all elements of
1955 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001956 auto &imageUniform = mState.mUniforms[imageIndex];
1957 if (imageUniform.binding == -1)
1958 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001959 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001960 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001961 else
1962 {
1963 mState.mImageBindings.emplace_back(
1964 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1965 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001966 }
1967
1968 high = low;
1969
1970 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001971 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001972 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001973 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001974 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001975
1976 mState.mSamplerUniformRange = RangeUI(low, high);
1977
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001978 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001979 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001980 {
1981 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1982 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1983 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001984 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001985 }
1986}
1987
jchen10eaef1e52017-06-13 10:44:11 +08001988bool Program::linkAtomicCounterBuffers()
1989{
1990 for (unsigned int index : mState.mAtomicCounterUniformRange)
1991 {
1992 auto &uniform = mState.mUniforms[index];
1993 bool found = false;
1994 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1995 ++bufferIndex)
1996 {
1997 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1998 if (buffer.binding == uniform.binding)
1999 {
2000 buffer.memberIndexes.push_back(index);
2001 uniform.bufferIndex = bufferIndex;
2002 found = true;
2003 break;
2004 }
2005 }
2006 if (!found)
2007 {
2008 AtomicCounterBuffer atomicCounterBuffer;
2009 atomicCounterBuffer.binding = uniform.binding;
2010 atomicCounterBuffer.memberIndexes.push_back(index);
2011 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
2012 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
2013 }
2014 }
2015 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
2016 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
2017
2018 return true;
2019}
2020
Martin Radev4c4c8e72016-08-04 12:25:34 +03002021bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2022 const std::string &uniformName,
2023 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002024 const sh::InterfaceBlockField &fragmentUniform,
2025 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026{
Frank Henigmanfccbac22017-05-28 17:29:26 -04002027 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
2028 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
2029 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 {
2031 return false;
2032 }
2033
2034 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2035 {
Jamie Madillf6113162015-05-07 11:49:21 -04002036 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037 return false;
2038 }
2039
2040 return true;
2041}
2042
Jamie Madilleb979bf2016-11-15 12:28:46 -05002043// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002044bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002046 const ContextState &data = context->getContextState();
2047 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002048
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002050 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002051 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002052
2053 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002054 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002055 {
Jamie Madillf6113162015-05-07 11:49:21 -04002056 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002057 return false;
2058 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002060 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002061
Jamie Madillc349ec02015-08-21 16:53:12 -04002062 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002063 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002064 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002065 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002066 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002067 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002068 attribute.location = bindingLocation;
2069 }
2070
2071 if (attribute.location != -1)
2072 {
2073 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002074 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075
Jamie Madill63805b42015-08-25 13:17:39 -04002076 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077 {
Jamie Madillf6113162015-05-07 11:49:21 -04002078 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002079 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080
2081 return false;
2082 }
2083
Jamie Madill63805b42015-08-25 13:17:39 -04002084 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 {
Jamie Madill63805b42015-08-25 13:17:39 -04002086 const int regLocation = attribute.location + reg;
2087 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088
2089 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002090 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002093 // TODO(jmadill): fix aliasing on ES2
2094 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002096 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002097 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 return false;
2099 }
2100 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002101 else
2102 {
Jamie Madill63805b42015-08-25 13:17:39 -04002103 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002104 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105
Jamie Madill63805b42015-08-25 13:17:39 -04002106 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 }
2108 }
2109 }
2110
2111 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002112 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002114 // Not set by glBindAttribLocation or by location layout qualifier
2115 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116 {
Jamie Madill63805b42015-08-25 13:17:39 -04002117 int regs = VariableRegisterCount(attribute.type);
2118 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119
Jamie Madill63805b42015-08-25 13:17:39 -04002120 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 {
Jamie Madillf6113162015-05-07 11:49:21 -04002122 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002123 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124 }
2125
Jamie Madillc349ec02015-08-21 16:53:12 -04002126 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 }
2128 }
2129
Jamie Madill48ef11b2016-04-27 15:21:52 -04002130 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 {
Jamie Madill63805b42015-08-25 13:17:39 -04002132 ASSERT(attribute.location != -1);
2133 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002134
Jamie Madill63805b42015-08-25 13:17:39 -04002135 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002136 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002137 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002138 }
2139 }
2140
Geoff Lang7dd2e102014-11-10 15:19:26 -05002141 return true;
2142}
2143
Martin Radev4c4c8e72016-08-04 12:25:34 +03002144bool Program::validateVertexAndFragmentInterfaceBlocks(
2145 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2146 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002147 InfoLog &infoLog,
2148 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002149{
2150 // Check that interface blocks defined in the vertex and fragment shaders are identical
Jiajia Qin729b2c62017-08-14 09:36:11 +08002151 typedef std::map<std::string, const sh::InterfaceBlock *> InterfaceBlockMap;
2152 InterfaceBlockMap linkedInterfaceBlocks;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002153
2154 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2155 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002156 linkedInterfaceBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002157 }
2158
Jamie Madille473dee2015-08-18 14:49:01 -04002159 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002160 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002161 auto entry = linkedInterfaceBlocks.find(fragmentInterfaceBlock.name);
2162 if (entry != linkedInterfaceBlocks.end())
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 {
2164 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002165 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2166 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 {
2168 return false;
2169 }
2170 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002171 // TODO(jiajia.qin@intel.com): Add
2172 // MAX_COMBINED_UNIFORM_BLOCKS/MAX_COMBINED_SHADER_STORAGE_BLOCKS validation.
Martin Radev4c4c8e72016-08-04 12:25:34 +03002173 }
2174 return true;
2175}
Jamie Madille473dee2015-08-18 14:49:01 -04002176
Jiajia Qin729b2c62017-08-14 09:36:11 +08002177bool Program::linkInterfaceBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002178{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002179 const auto &caps = context->getCaps();
2180
Martin Radev4c4c8e72016-08-04 12:25:34 +03002181 if (mState.mAttachedComputeShader)
2182 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002183 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002184 const auto &computeUniformBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185
Jiajia Qin729b2c62017-08-14 09:36:11 +08002186 if (!validateInterfaceBlocksCount(
2187 caps.maxComputeUniformBlocks, computeUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002188 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2189 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002191 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002193
2194 const auto &computeShaderStorageBlocks = computeShader.getShaderStorageBlocks(context);
2195 if (!validateInterfaceBlocksCount(caps.maxComputeShaderStorageBlocks,
2196 computeShaderStorageBlocks,
2197 "Compute shader shader storage block count exceeds "
2198 "GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS (",
2199 infoLog))
2200 {
2201 return false;
2202 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002203 return true;
2204 }
2205
Jamie Madillbd044ed2017-06-05 12:59:21 -04002206 Shader &vertexShader = *mState.mAttachedVertexShader;
2207 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002208
Jiajia Qin729b2c62017-08-14 09:36:11 +08002209 const auto &vertexUniformBlocks = vertexShader.getUniformBlocks(context);
2210 const auto &fragmentUniformBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002211
Jiajia Qin729b2c62017-08-14 09:36:11 +08002212 if (!validateInterfaceBlocksCount(
2213 caps.maxVertexUniformBlocks, vertexUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002214 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2215 {
2216 return false;
2217 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002218 if (!validateInterfaceBlocksCount(
2219 caps.maxFragmentUniformBlocks, fragmentUniformBlocks,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002220 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2221 infoLog))
2222 {
2223
2224 return false;
2225 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002226
2227 bool webglCompatibility = context->getExtensions().webglCompatibility;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002228 if (!validateVertexAndFragmentInterfaceBlocks(vertexUniformBlocks, fragmentUniformBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002229 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002230 {
2231 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002232 }
Jamie Madille473dee2015-08-18 14:49:01 -04002233
Jiajia Qin729b2c62017-08-14 09:36:11 +08002234 if (context->getClientVersion() >= Version(3, 1))
2235 {
2236 const auto &vertexShaderStorageBlocks = vertexShader.getShaderStorageBlocks(context);
2237 const auto &fragmentShaderStorageBlocks = fragmentShader.getShaderStorageBlocks(context);
2238
2239 if (!validateInterfaceBlocksCount(caps.maxVertexShaderStorageBlocks,
2240 vertexShaderStorageBlocks,
2241 "Vertex shader shader storage block count exceeds "
2242 "GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS (",
2243 infoLog))
2244 {
2245 return false;
2246 }
2247 if (!validateInterfaceBlocksCount(caps.maxFragmentShaderStorageBlocks,
2248 fragmentShaderStorageBlocks,
2249 "Fragment shader shader storage block count exceeds "
2250 "GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS (",
2251 infoLog))
2252 {
2253
2254 return false;
2255 }
2256
2257 if (!validateVertexAndFragmentInterfaceBlocks(vertexShaderStorageBlocks,
2258 fragmentShaderStorageBlocks, infoLog,
2259 webglCompatibility))
2260 {
2261 return false;
2262 }
2263 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002264 return true;
2265}
2266
Jamie Madilla2c74982016-12-12 11:20:42 -05002267bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002268 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002269 const sh::InterfaceBlock &fragmentInterfaceBlock,
2270 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271{
2272 const char* blockName = vertexInterfaceBlock.name.c_str();
2273 // validate blocks for the same member types
2274 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2275 {
Jamie Madillf6113162015-05-07 11:49:21 -04002276 infoLog << "Types for interface block '" << blockName
2277 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 return false;
2279 }
2280 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2281 {
Jamie Madillf6113162015-05-07 11:49:21 -04002282 infoLog << "Array sizes differ for interface block '" << blockName
2283 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002284 return false;
2285 }
jchen10af713a22017-04-19 09:10:56 +08002286 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2287 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2288 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289 {
Jamie Madillf6113162015-05-07 11:49:21 -04002290 infoLog << "Layout qualifiers differ for interface block '" << blockName
2291 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002292 return false;
2293 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002294 const unsigned int numBlockMembers =
2295 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2297 {
2298 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2299 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2300 if (vertexMember.name != fragmentMember.name)
2301 {
Jamie Madillf6113162015-05-07 11:49:21 -04002302 infoLog << "Name mismatch for field " << blockMemberIndex
2303 << " of interface block '" << blockName
2304 << "': (in vertex: '" << vertexMember.name
2305 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002306 return false;
2307 }
2308 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002309 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2310 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002311 {
2312 return false;
2313 }
2314 }
2315 return true;
2316}
2317
2318bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2319 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2320{
2321 if (vertexVariable.type != fragmentVariable.type)
2322 {
Jamie Madillf6113162015-05-07 11:49:21 -04002323 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002324 return false;
2325 }
2326 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2327 {
Jamie Madillf6113162015-05-07 11:49:21 -04002328 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002329 return false;
2330 }
2331 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2332 {
Jamie Madillf6113162015-05-07 11:49:21 -04002333 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334 return false;
2335 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002336 if (vertexVariable.structName != fragmentVariable.structName)
2337 {
2338 infoLog << "Structure names for " << variableName
2339 << " differ between vertex and fragment shaders";
2340 return false;
2341 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002342
2343 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2344 {
Jamie Madillf6113162015-05-07 11:49:21 -04002345 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002346 return false;
2347 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002348 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002349 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2350 {
2351 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2352 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2353
2354 if (vertexMember.name != fragmentMember.name)
2355 {
Jamie Madillf6113162015-05-07 11:49:21 -04002356 infoLog << "Name mismatch for field '" << memberIndex
2357 << "' of " << variableName
2358 << ": (in vertex: '" << vertexMember.name
2359 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002360 return false;
2361 }
2362
2363 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2364 vertexMember.name + "'";
2365
2366 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2367 {
2368 return false;
2369 }
2370 }
2371
2372 return true;
2373}
2374
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002375bool Program::linkValidateVaryings(InfoLog &infoLog,
2376 const std::string &varyingName,
2377 const sh::Varying &vertexVarying,
2378 const sh::Varying &fragmentVarying,
2379 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002380{
2381 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2382 {
2383 return false;
2384 }
2385
Jamie Madille9cc4692015-02-19 16:00:13 -05002386 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002388 infoLog << "Interpolation types for " << varyingName
2389 << " differ between vertex and fragment shaders.";
2390 return false;
2391 }
2392
2393 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2394 {
2395 infoLog << "Invariance for " << varyingName
2396 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002397 return false;
2398 }
2399
2400 return true;
2401}
2402
Jamie Madillbd044ed2017-06-05 12:59:21 -04002403bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002404{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002405 Shader *vertexShader = mState.mAttachedVertexShader;
2406 Shader *fragmentShader = mState.mAttachedFragmentShader;
2407 const auto &vertexVaryings = vertexShader->getVaryings(context);
2408 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2409 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002410
2411 if (shaderVersion != 100)
2412 {
2413 // Only ESSL 1.0 has restrictions on matching input and output invariance
2414 return true;
2415 }
2416
2417 bool glPositionIsInvariant = false;
2418 bool glPointSizeIsInvariant = false;
2419 bool glFragCoordIsInvariant = false;
2420 bool glPointCoordIsInvariant = false;
2421
2422 for (const sh::Varying &varying : vertexVaryings)
2423 {
2424 if (!varying.isBuiltIn())
2425 {
2426 continue;
2427 }
2428 if (varying.name.compare("gl_Position") == 0)
2429 {
2430 glPositionIsInvariant = varying.isInvariant;
2431 }
2432 else if (varying.name.compare("gl_PointSize") == 0)
2433 {
2434 glPointSizeIsInvariant = varying.isInvariant;
2435 }
2436 }
2437
2438 for (const sh::Varying &varying : fragmentVaryings)
2439 {
2440 if (!varying.isBuiltIn())
2441 {
2442 continue;
2443 }
2444 if (varying.name.compare("gl_FragCoord") == 0)
2445 {
2446 glFragCoordIsInvariant = varying.isInvariant;
2447 }
2448 else if (varying.name.compare("gl_PointCoord") == 0)
2449 {
2450 glPointCoordIsInvariant = varying.isInvariant;
2451 }
2452 }
2453
2454 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2455 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2456 // Not requiring invariance to match is supported by:
2457 // dEQP, WebGL CTS, Nexus 5X GLES
2458 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2459 {
2460 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2461 "declared invariant.";
2462 return false;
2463 }
2464 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2465 {
2466 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2467 "declared invariant.";
2468 return false;
2469 }
2470
2471 return true;
2472}
2473
jchen10a9042d32017-03-17 08:50:45 +08002474bool Program::linkValidateTransformFeedback(const gl::Context *context,
2475 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002476 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002477 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002478{
2479 size_t totalComponents = 0;
2480
Jamie Madillccdf74b2015-08-18 10:46:12 -04002481 std::set<std::string> uniqueNames;
2482
Jamie Madill48ef11b2016-04-27 15:21:52 -04002483 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002484 {
2485 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002486 size_t subscript = GL_INVALID_INDEX;
2487 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2488
Jamie Madill192745a2016-12-22 15:58:21 -05002489 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 {
Jamie Madill192745a2016-12-22 15:58:21 -05002491 const sh::Varying *varying = ref.second.get();
2492
jchen10a9042d32017-03-17 08:50:45 +08002493 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002495 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002496 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002497 infoLog << "Two transform feedback varyings specify the same output variable ("
2498 << tfVaryingName << ").";
2499 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002500 }
jchen10a9042d32017-03-17 08:50:45 +08002501 if (context->getClientVersion() >= Version(3, 1))
2502 {
2503 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2504 {
2505 infoLog
2506 << "Two transform feedback varyings include the same array element ("
2507 << tfVaryingName << ").";
2508 return false;
2509 }
2510 }
2511 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002512 {
2513 infoLog << "Capture of arrays is undefined and not supported.";
2514 return false;
2515 }
2516
jchen10a9042d32017-03-17 08:50:45 +08002517 uniqueNames.insert(tfVaryingName);
2518
Jamie Madillccdf74b2015-08-18 10:46:12 -04002519 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002520 size_t elementCount =
2521 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2522 : 1);
2523 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002524 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002525 componentCount > caps.maxTransformFeedbackSeparateComponents)
2526 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002527 infoLog << "Transform feedback varying's " << varying->name << " components ("
2528 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002529 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002530 return false;
2531 }
2532
2533 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002534 found = true;
2535 break;
2536 }
2537 }
jchen10a9042d32017-03-17 08:50:45 +08002538 if (context->getClientVersion() < Version(3, 1) &&
2539 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002540 {
Geoff Lang1a683462015-09-29 15:09:59 -04002541 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002542 return false;
2543 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002544 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2545 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002546 ASSERT(found);
2547 }
2548
Jamie Madill48ef11b2016-04-27 15:21:52 -04002549 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002550 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002551 {
Jamie Madillf6113162015-05-07 11:49:21 -04002552 infoLog << "Transform feedback varying total components (" << totalComponents
2553 << ") exceed the maximum interleaved components ("
2554 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002555 return false;
2556 }
2557
2558 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002559}
2560
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002561bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2562{
2563 const std::vector<sh::Uniform> &vertexUniforms =
2564 mState.mAttachedVertexShader->getUniforms(context);
2565 const std::vector<sh::Uniform> &fragmentUniforms =
2566 mState.mAttachedFragmentShader->getUniforms(context);
2567 const std::vector<sh::Attribute> &attributes =
2568 mState.mAttachedVertexShader->getActiveAttributes(context);
2569 for (const auto &attrib : attributes)
2570 {
2571 for (const auto &uniform : vertexUniforms)
2572 {
2573 if (uniform.name == attrib.name)
2574 {
2575 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2576 return false;
2577 }
2578 }
2579 for (const auto &uniform : fragmentUniforms)
2580 {
2581 if (uniform.name == attrib.name)
2582 {
2583 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2584 return false;
2585 }
2586 }
2587 }
2588 return true;
2589}
2590
Jamie Madill192745a2016-12-22 15:58:21 -05002591void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002592{
2593 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002594 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002595 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002596 {
jchen10a9042d32017-03-17 08:50:45 +08002597 size_t subscript = GL_INVALID_INDEX;
2598 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002599 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002600 {
Jamie Madill192745a2016-12-22 15:58:21 -05002601 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002602 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002603 {
jchen10a9042d32017-03-17 08:50:45 +08002604 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2605 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002606 break;
2607 }
2608 }
2609 }
2610}
2611
Jamie Madillbd044ed2017-06-05 12:59:21 -04002612Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002613{
Jamie Madill192745a2016-12-22 15:58:21 -05002614 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002615
Jamie Madillbd044ed2017-06-05 12:59:21 -04002616 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002617 {
Jamie Madill192745a2016-12-22 15:58:21 -05002618 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002619 }
2620
Jamie Madillbd044ed2017-06-05 12:59:21 -04002621 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002622 {
Jamie Madill192745a2016-12-22 15:58:21 -05002623 merged[varying.name].fragment = &varying;
2624 }
2625
2626 return merged;
2627}
2628
2629std::vector<PackedVarying> Program::getPackedVaryings(
2630 const Program::MergedVaryings &mergedVaryings) const
2631{
2632 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2633 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002634 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002635
2636 for (const auto &ref : mergedVaryings)
2637 {
2638 const sh::Varying *input = ref.second.vertex;
2639 const sh::Varying *output = ref.second.fragment;
2640
2641 // Only pack varyings that have a matched input or output, plus special builtins.
2642 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002643 {
Jamie Madill192745a2016-12-22 15:58:21 -05002644 // Will get the vertex shader interpolation by default.
2645 auto interpolation = ref.second.get()->interpolation;
2646
Olli Etuaho06a06f52017-07-12 12:22:15 +03002647 // Note that we lose the vertex shader static use information here. The data for the
2648 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002649 if (output->isStruct())
2650 {
2651 ASSERT(!output->isArray());
2652 for (const auto &field : output->fields)
2653 {
2654 ASSERT(!field.isStruct() && !field.isArray());
2655 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2656 }
2657 }
2658 else
2659 {
2660 packedVaryings.push_back(PackedVarying(*output, interpolation));
2661 }
2662 continue;
2663 }
2664
2665 // Keep Transform FB varyings in the merged list always.
2666 if (!input)
2667 {
2668 continue;
2669 }
2670
2671 for (const std::string &tfVarying : tfVaryings)
2672 {
jchen10a9042d32017-03-17 08:50:45 +08002673 size_t subscript = GL_INVALID_INDEX;
2674 std::string baseName = ParseResourceName(tfVarying, &subscript);
2675 if (uniqueFullNames.count(tfVarying) > 0)
2676 {
2677 continue;
2678 }
2679 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002680 {
2681 // Transform feedback for varying structs is underspecified.
2682 // See Khronos bug 9856.
2683 // TODO(jmadill): Figure out how to be spec-compliant here.
2684 if (!input->isStruct())
2685 {
2686 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2687 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002688 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2689 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002690 }
jchen10a9042d32017-03-17 08:50:45 +08002691 if (subscript == GL_INVALID_INDEX)
2692 {
2693 break;
2694 }
Jamie Madill192745a2016-12-22 15:58:21 -05002695 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002696 }
2697 }
2698
Jamie Madill192745a2016-12-22 15:58:21 -05002699 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2700
2701 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002702}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002703
Jamie Madillbd044ed2017-06-05 12:59:21 -04002704void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002705{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002706 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002707 ASSERT(fragmentShader != nullptr);
2708
Geoff Lange0cff192017-05-30 13:04:56 -04002709 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002710 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002711
2712 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002713 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002714 {
2715 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2716 outputVariable.name != "gl_FragData")
2717 {
2718 continue;
2719 }
2720
2721 unsigned int baseLocation =
2722 (outputVariable.location == -1 ? 0u
2723 : static_cast<unsigned int>(outputVariable.location));
2724 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2725 elementIndex++)
2726 {
2727 const unsigned int location = baseLocation + elementIndex;
2728 if (location >= mState.mOutputVariableTypes.size())
2729 {
2730 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2731 }
Corentin Walleze7557742017-06-01 13:09:57 -04002732 ASSERT(location < mState.mActiveOutputVariables.size());
2733 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002734 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2735 }
2736 }
2737
Jamie Madill80a6fc02015-08-21 16:53:16 -04002738 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002739 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002740 return;
2741
Jamie Madillbd044ed2017-06-05 12:59:21 -04002742 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002743 // TODO(jmadill): any caps validation here?
2744
jchen1015015f72017-03-16 13:54:21 +08002745 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002746 outputVariableIndex++)
2747 {
jchen1015015f72017-03-16 13:54:21 +08002748 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002749
2750 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2751 if (outputVariable.isBuiltIn())
2752 continue;
2753
2754 // Since multiple output locations must be specified, use 0 for non-specified locations.
2755 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2756
Jamie Madill80a6fc02015-08-21 16:53:16 -04002757 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2758 elementIndex++)
2759 {
2760 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002761 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002762 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002763 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002764 VariableLocation(outputVariable.name, element, outputVariableIndex);
2765 }
2766 }
2767}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002768
Olli Etuaho48fed632017-03-16 12:05:30 +00002769void Program::setUniformValuesFromBindingQualifiers()
2770{
Jamie Madill982f6e02017-06-07 14:33:04 -04002771 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002772 {
2773 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2774 if (samplerUniform.binding != -1)
2775 {
2776 GLint location = mState.getUniformLocation(samplerUniform.name);
2777 ASSERT(location != -1);
2778 std::vector<GLint> boundTextureUnits;
2779 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2780 ++elementIndex)
2781 {
2782 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2783 }
2784 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2785 boundTextureUnits.data());
2786 }
2787 }
2788}
2789
jchen10eaef1e52017-06-13 10:44:11 +08002790void Program::gatherAtomicCounterBuffers()
2791{
2792 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2793 // counter.
2794 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2795}
2796
Jiajia Qin729b2c62017-08-14 09:36:11 +08002797void Program::gatherComputeBlockInfo(const std::vector<sh::InterfaceBlock> &computeBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002798{
Jiajia Qin729b2c62017-08-14 09:36:11 +08002799 for (const sh::InterfaceBlock &computeBlock : computeBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002800 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002801
Jiajia Qin729b2c62017-08-14 09:36:11 +08002802 // Only 'packed' blocks are allowed to be considered inactive.
2803 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2804 continue;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002805
Jiajia Qin729b2c62017-08-14 09:36:11 +08002806 defineInterfaceBlock(computeBlock, GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002807 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002808}
Martin Radev4c4c8e72016-08-04 12:25:34 +03002809
Jiajia Qin729b2c62017-08-14 09:36:11 +08002810void Program::gatherVertexAndFragmentBlockInfo(
2811 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2812 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks)
2813{
Jamie Madill62d31cb2015-09-11 13:25:51 -04002814 std::set<std::string> visitedList;
2815
Jiajia Qin729b2c62017-08-14 09:36:11 +08002816 for (const sh::InterfaceBlock &vertexBlock : vertexInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002818 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2820 continue;
2821
Jiajia Qin729b2c62017-08-14 09:36:11 +08002822 defineInterfaceBlock(vertexBlock, GL_VERTEX_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002823 visitedList.insert(vertexBlock.name);
2824 }
2825
Jiajia Qin729b2c62017-08-14 09:36:11 +08002826 for (const sh::InterfaceBlock &fragmentBlock : fragmentInterfaceBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002827 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002828 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2830 continue;
2831
2832 if (visitedList.count(fragmentBlock.name) > 0)
2833 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002834 if (fragmentBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002835 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002836 for (InterfaceBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002837 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002838 if (block.name == fragmentBlock.name)
2839 {
2840 block.fragmentStaticUse = fragmentBlock.staticUse;
2841 }
2842 }
2843 }
2844 else
2845 {
2846 ASSERT(fragmentBlock.blockType == sh::BlockType::BLOCK_BUFFER);
2847 for (InterfaceBlock &block : mState.mShaderStorageBlocks)
2848 {
2849 if (block.name == fragmentBlock.name)
2850 {
2851 block.fragmentStaticUse = fragmentBlock.staticUse;
2852 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002853 }
2854 }
2855
2856 continue;
2857 }
2858
Jiajia Qin729b2c62017-08-14 09:36:11 +08002859 defineInterfaceBlock(fragmentBlock, GL_FRAGMENT_SHADER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002860 visitedList.insert(fragmentBlock.name);
2861 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002862}
2863
2864void Program::gatherInterfaceBlockInfo(const Context *context)
2865{
2866 ASSERT(mState.mUniformBlocks.empty());
2867 ASSERT(mState.mShaderStorageBlocks.empty());
2868
2869 if (mState.mAttachedComputeShader)
2870 {
2871 Shader *computeShader = mState.getAttachedComputeShader();
2872
2873 gatherComputeBlockInfo(computeShader->getUniformBlocks(context));
2874 gatherComputeBlockInfo(computeShader->getShaderStorageBlocks(context));
2875 return;
2876 }
2877
2878 Shader *vertexShader = mState.getAttachedVertexShader();
2879 Shader *fragmentShader = mState.getAttachedFragmentShader();
2880
2881 gatherVertexAndFragmentBlockInfo(vertexShader->getUniformBlocks(context),
2882 fragmentShader->getUniformBlocks(context));
2883 if (context->getClientVersion() >= Version(3, 1))
2884 {
2885 gatherVertexAndFragmentBlockInfo(vertexShader->getShaderStorageBlocks(context),
2886 fragmentShader->getShaderStorageBlocks(context));
2887 }
2888
jchen10af713a22017-04-19 09:10:56 +08002889 // Set initial bindings from shader.
2890 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2891 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002892 InterfaceBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
jchen10af713a22017-04-19 09:10:56 +08002893 bindUniformBlock(blockIndex, uniformBlock.binding);
2894 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002895}
2896
Jamie Madill4a3c2342015-10-08 12:58:45 -04002897template <typename VarT>
2898void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2899 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002900 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002901 int blockIndex)
2902{
2903 for (const VarT &field : fields)
2904 {
2905 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2906
Olli Etuaho855d9642017-05-17 14:05:06 +03002907 const std::string &fullMappedName =
2908 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2909
Jamie Madill4a3c2342015-10-08 12:58:45 -04002910 if (field.isStruct())
2911 {
2912 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2913 {
2914 const std::string uniformElementName =
2915 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002916 const std::string uniformElementMappedName =
2917 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2918 defineUniformBlockMembers(field.fields, uniformElementName,
2919 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002920 }
2921 }
2922 else
2923 {
2924 // If getBlockMemberInfo returns false, the uniform is optimized out.
2925 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002926 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002927 {
2928 continue;
2929 }
2930
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002931 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002932 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002933 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002934
2935 // Since block uniforms have no location, we don't need to store them in the uniform
2936 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002937 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002938 }
2939 }
2940}
2941
Jiajia Qin729b2c62017-08-14 09:36:11 +08002942void Program::defineInterfaceBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002943{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002944 size_t blockSize = 0;
Jiajia Qin729b2c62017-08-14 09:36:11 +08002945 std::vector<unsigned int> blockIndexes;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002946
Jiajia Qin729b2c62017-08-14 09:36:11 +08002947 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002948 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002949 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
2950 // Track the first and last uniform index to determine the range of active uniforms in the
2951 // block.
2952 size_t firstBlockUniformIndex = mState.mUniforms.size();
2953 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2954 interfaceBlock.fieldMappedPrefix(), blockIndex);
2955 size_t lastBlockUniformIndex = mState.mUniforms.size();
2956
2957 for (size_t blockUniformIndex = firstBlockUniformIndex;
2958 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2959 {
2960 blockIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2961 }
2962 }
2963 else
2964 {
2965 // TODO(jiajia.qin@intel.com) : Add buffer variables support and calculate the block index.
2966 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002967 }
jchen10af713a22017-04-19 09:10:56 +08002968 // ESSL 3.10 section 4.4.4 page 58:
2969 // Any uniform or shader storage block declared without a binding qualifier is initially
2970 // assigned to block binding point zero.
2971 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002972 if (interfaceBlock.arraySize > 0)
2973 {
2974 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2975 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002976 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
2977 // of UniformBlock and ShaderStorageBlock.
2978 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08002979 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08002980 // Don't define this block at all if it's not active in the implementation.
2981 if (!mProgram->getUniformBlockSize(
2982 interfaceBlock.name + ArrayString(arrayElement),
2983 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
2984 {
2985 continue;
2986 }
jchen10af713a22017-04-19 09:10:56 +08002987 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08002988
2989 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
2990 blockBinding + arrayElement);
2991 block.memberIndexes = blockIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002992
Martin Radev4c4c8e72016-08-04 12:25:34 +03002993 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002994 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002995 case GL_VERTEX_SHADER:
2996 {
2997 block.vertexStaticUse = interfaceBlock.staticUse;
2998 break;
2999 }
3000 case GL_FRAGMENT_SHADER:
3001 {
3002 block.fragmentStaticUse = interfaceBlock.staticUse;
3003 break;
3004 }
3005 case GL_COMPUTE_SHADER:
3006 {
3007 block.computeStaticUse = interfaceBlock.staticUse;
3008 break;
3009 }
3010 default:
3011 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003012 }
3013
Jiajia Qin729b2c62017-08-14 09:36:11 +08003014 // Since all block elements in an array share the same active interface blocks, they
3015 // will all be active once any block member is used. So, since interfaceBlock.name[0]
3016 // was active, here we will add every block element in the array.
Qin Jiajia0350a642016-11-01 17:01:51 +08003017 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003018 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3019 {
3020 mState.mUniformBlocks.push_back(block);
3021 }
3022 else
3023 {
3024 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3025 mState.mShaderStorageBlocks.push_back(block);
3026 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003027 }
3028 }
3029 else
3030 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003031 // TODO(jiajia.qin@intel.com) : use GetProgramResourceiv to calculate BUFFER_DATA_SIZE
3032 // of UniformBlock and ShaderStorageBlock.
3033 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
jchen10af713a22017-04-19 09:10:56 +08003034 {
Jiajia Qin729b2c62017-08-14 09:36:11 +08003035 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
3036 &blockSize))
3037 {
3038 return;
3039 }
jchen10af713a22017-04-19 09:10:56 +08003040 }
Jiajia Qin729b2c62017-08-14 09:36:11 +08003041
3042 InterfaceBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0,
3043 blockBinding);
3044 block.memberIndexes = blockIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003045
Martin Radev4c4c8e72016-08-04 12:25:34 +03003046 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003047 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003048 case GL_VERTEX_SHADER:
3049 {
3050 block.vertexStaticUse = interfaceBlock.staticUse;
3051 break;
3052 }
3053 case GL_FRAGMENT_SHADER:
3054 {
3055 block.fragmentStaticUse = interfaceBlock.staticUse;
3056 break;
3057 }
3058 case GL_COMPUTE_SHADER:
3059 {
3060 block.computeStaticUse = interfaceBlock.staticUse;
3061 break;
3062 }
3063 default:
3064 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003065 }
3066
Jamie Madill4a3c2342015-10-08 12:58:45 -04003067 block.dataSize = static_cast<unsigned int>(blockSize);
Jiajia Qin729b2c62017-08-14 09:36:11 +08003068 if (interfaceBlock.blockType == sh::BlockType::BLOCK_UNIFORM)
3069 {
3070 mState.mUniformBlocks.push_back(block);
3071 }
3072 else
3073 {
3074 ASSERT(interfaceBlock.blockType == sh::BlockType::BLOCK_BUFFER);
3075 mState.mShaderStorageBlocks.push_back(block);
3076 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003077 }
3078}
3079
Jamie Madille7d84322017-01-10 18:21:59 -05003080void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05003081 GLsizei clampedCount,
3082 const GLint *v)
3083{
Jamie Madill81c2e252017-09-09 23:32:46 -04003084 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
3085 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3086 std::vector<GLuint> *boundTextureUnits =
3087 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05003088
Jamie Madill81c2e252017-09-09 23:32:46 -04003089 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
Jamie Madilld68248b2017-09-11 14:34:14 -04003090
3091 // Invalidate the validation cache.
Jamie Madill81c2e252017-09-09 23:32:46 -04003092 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05003093}
3094
3095template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003096GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
3097 GLsizei count,
3098 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05003099 const T *v)
3100{
Jamie Madill134f93d2017-08-31 17:11:00 -04003101 if (count == 1)
3102 return 1;
3103
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003104 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003105
Corentin Wallez15ac5342016-11-03 17:06:39 -04003106 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3107 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003108 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003109 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003110 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003111
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003112 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003113 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003114 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003115 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003116
3117 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003118}
3119
3120template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003121GLsizei Program::clampMatrixUniformCount(GLint location,
3122 GLsizei count,
3123 GLboolean transpose,
3124 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003125{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003126 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3127
Jamie Madill62d31cb2015-09-11 13:25:51 -04003128 if (!transpose)
3129 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003130 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003131 }
3132
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003133 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003134
3135 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3136 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003137 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
3138 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003139}
3140
Jamie Madill54164b02017-08-28 15:17:37 -04003141// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3142// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003143template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003144void Program::getUniformInternal(const Context *context,
3145 DestT *dataOut,
3146 GLint location,
3147 GLenum nativeType,
3148 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003149{
Jamie Madill54164b02017-08-28 15:17:37 -04003150 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003151 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003152 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003153 {
3154 GLint tempValue[16] = {0};
3155 mProgram->getUniformiv(context, location, tempValue);
3156 UniformStateQueryCastLoop<GLboolean>(
3157 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003158 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003159 }
3160 case GL_INT:
3161 {
3162 GLint tempValue[16] = {0};
3163 mProgram->getUniformiv(context, location, tempValue);
3164 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3165 components);
3166 break;
3167 }
3168 case GL_UNSIGNED_INT:
3169 {
3170 GLuint tempValue[16] = {0};
3171 mProgram->getUniformuiv(context, location, tempValue);
3172 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3173 components);
3174 break;
3175 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003176 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003177 {
3178 GLfloat tempValue[16] = {0};
3179 mProgram->getUniformfv(context, location, tempValue);
3180 UniformStateQueryCastLoop<GLfloat>(
3181 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003182 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003183 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003184 default:
3185 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003186 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003187 }
3188}
Jamie Madilla4595b82017-01-11 17:36:34 -05003189
3190bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3191{
3192 // Must be called after samplers are validated.
3193 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3194
3195 for (const auto &binding : mState.mSamplerBindings)
3196 {
3197 GLenum textureType = binding.textureType;
3198 for (const auto &unit : binding.boundTextureUnits)
3199 {
3200 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3201 if (programTextureID == textureID)
3202 {
3203 // TODO(jmadill): Check for appropriate overlap.
3204 return true;
3205 }
3206 }
3207 }
3208
3209 return false;
3210}
3211
Jamie Madilla2c74982016-12-12 11:20:42 -05003212} // namespace gl