blob: 76c25d2e3e62862e0b31e883827166ed957d9525 [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
Jamie Madill62d31cb2015-09-11 13:25:51 -0400196} // anonymous namespace
197
Jamie Madill4a3c2342015-10-08 12:58:45 -0400198const char *const g_fakepath = "C:\\fakepath";
199
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400200InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000201{
202}
203
204InfoLog::~InfoLog()
205{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000206}
207
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209{
Jamie Madill23176ce2017-07-31 14:14:33 -0400210 if (!mLazyStream)
211 {
212 return 0;
213 }
214
215 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400216 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000217}
218
Geoff Lange1a27752015-10-05 13:16:04 -0400219void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000220{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400221 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000222
223 if (bufSize > 0)
224 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400225 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400226
Jamie Madill23176ce2017-07-31 14:14:33 -0400227 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000228 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400229 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
230 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000231 }
232
233 infoLog[index] = '\0';
234 }
235
236 if (length)
237 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400238 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000239 }
240}
241
242// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300243// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000244// messages, so lets remove all occurrences of this fake file path from the log.
245void InfoLog::appendSanitized(const char *message)
246{
Jamie Madill23176ce2017-07-31 14:14:33 -0400247 ensureInitialized();
248
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000249 std::string msg(message);
250
251 size_t found;
252 do
253 {
254 found = msg.find(g_fakepath);
255 if (found != std::string::npos)
256 {
257 msg.erase(found, strlen(g_fakepath));
258 }
259 }
260 while (found != std::string::npos);
261
Jamie Madill23176ce2017-07-31 14:14:33 -0400262 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000263}
264
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265void InfoLog::reset()
266{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000267}
268
Jamie Madillfe8b5982017-09-07 17:00:18 -0400269VariableLocation::VariableLocation()
270 : name(), element(0), index(GL_INVALID_INDEX), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000271{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500272}
273
Geoff Langd8605522016-04-13 10:19:12 -0400274VariableLocation::VariableLocation(const std::string &name,
275 unsigned int element,
276 unsigned int index)
277 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500278{
279}
280
Geoff Langd8605522016-04-13 10:19:12 -0400281void Program::Bindings::bindLocation(GLuint index, const std::string &name)
282{
283 mBindings[name] = index;
284}
285
286int Program::Bindings::getBinding(const std::string &name) const
287{
288 auto iter = mBindings.find(name);
289 return (iter != mBindings.end()) ? iter->second : -1;
290}
291
292Program::Bindings::const_iterator Program::Bindings::begin() const
293{
294 return mBindings.begin();
295}
296
297Program::Bindings::const_iterator Program::Bindings::end() const
298{
299 return mBindings.end();
300}
301
Jamie Madill48ef11b2016-04-27 15:21:52 -0400302ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500303 : mLabel(),
304 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400305 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300306 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500307 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500308 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800309 mImageUniformRange(0, 0),
310 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300311 mBinaryRetrieveableHint(false),
312 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400313{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300314 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400315}
316
Jamie Madill48ef11b2016-04-27 15:21:52 -0400317ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400318{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500319 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400320}
321
Jamie Madill48ef11b2016-04-27 15:21:52 -0400322const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500323{
324 return mLabel;
325}
326
Jamie Madill48ef11b2016-04-27 15:21:52 -0400327GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400328{
329 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800330 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400331
332 for (size_t location = 0; location < mUniformLocations.size(); ++location)
333 {
334 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400335 if (!uniformLocation.used)
336 {
337 continue;
338 }
339
340 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400341
342 if (uniform.name == baseName)
343 {
Geoff Langd8605522016-04-13 10:19:12 -0400344 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400345 {
Geoff Langd8605522016-04-13 10:19:12 -0400346 if (uniformLocation.element == subscript ||
347 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
348 {
349 return static_cast<GLint>(location);
350 }
351 }
352 else
353 {
354 if (subscript == GL_INVALID_INDEX)
355 {
356 return static_cast<GLint>(location);
357 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400358 }
359 }
360 }
361
362 return -1;
363}
364
Jamie Madille7d84322017-01-10 18:21:59 -0500365GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400366{
jchen1015015f72017-03-16 13:54:21 +0800367 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400368}
369
Jamie Madille7d84322017-01-10 18:21:59 -0500370GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
371{
372 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
373 return mUniformLocations[location].index;
374}
375
376Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
377{
378 GLuint index = getUniformIndexFromLocation(location);
379 if (!isSamplerUniformIndex(index))
380 {
381 return Optional<GLuint>::Invalid();
382 }
383
384 return getSamplerIndexFromUniformIndex(index);
385}
386
387bool ProgramState::isSamplerUniformIndex(GLuint index) const
388{
Jamie Madill982f6e02017-06-07 14:33:04 -0400389 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500390}
391
392GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
393{
394 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400395 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500396}
397
Jamie Madill34ca4f52017-06-13 11:49:39 -0400398GLuint ProgramState::getAttributeLocation(const std::string &name) const
399{
400 for (const sh::Attribute &attribute : mAttributes)
401 {
402 if (attribute.name == name)
403 {
404 return attribute.location;
405 }
406 }
407
408 return static_cast<GLuint>(-1);
409}
410
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500411Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400412 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400413 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500414 mLinked(false),
415 mDeleteStatus(false),
416 mRefCount(0),
417 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500418 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500419{
420 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000421
Geoff Lang7dd2e102014-11-10 15:19:26 -0500422 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423}
424
425Program::~Program()
426{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400427 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428}
429
Jamie Madill4928b7c2017-06-20 12:57:39 -0400430void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500431{
432 if (mState.mAttachedVertexShader != nullptr)
433 {
434 mState.mAttachedVertexShader->release(context);
435 mState.mAttachedVertexShader = nullptr;
436 }
437
438 if (mState.mAttachedFragmentShader != nullptr)
439 {
440 mState.mAttachedFragmentShader->release(context);
441 mState.mAttachedFragmentShader = nullptr;
442 }
443
444 if (mState.mAttachedComputeShader != nullptr)
445 {
446 mState.mAttachedComputeShader->release(context);
447 mState.mAttachedComputeShader = nullptr;
448 }
449
Jamie Madillc564c072017-06-01 12:45:42 -0400450 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400451
452 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
453 !mState.mAttachedComputeShader);
454 SafeDelete(mProgram);
455
456 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500457}
458
Geoff Lang70d0f492015-12-10 17:45:46 -0500459void Program::setLabel(const std::string &label)
460{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400461 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500462}
463
464const std::string &Program::getLabel() const
465{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400466 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500467}
468
Jamie Madillef300b12016-10-07 15:12:09 -0400469void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300471 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300473 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474 {
Jamie Madillef300b12016-10-07 15:12:09 -0400475 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300476 mState.mAttachedVertexShader = shader;
477 mState.mAttachedVertexShader->addRef();
478 break;
479 }
480 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481 {
Jamie Madillef300b12016-10-07 15:12:09 -0400482 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300483 mState.mAttachedFragmentShader = shader;
484 mState.mAttachedFragmentShader->addRef();
485 break;
486 }
487 case GL_COMPUTE_SHADER:
488 {
Jamie Madillef300b12016-10-07 15:12:09 -0400489 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300490 mState.mAttachedComputeShader = shader;
491 mState.mAttachedComputeShader->addRef();
492 break;
493 }
494 default:
495 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497}
498
Jamie Madillc1d770e2017-04-13 17:31:24 -0400499void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300503 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400505 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500506 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300507 mState.mAttachedVertexShader = nullptr;
508 break;
509 }
510 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400512 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500513 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300514 mState.mAttachedFragmentShader = nullptr;
515 break;
516 }
517 case GL_COMPUTE_SHADER:
518 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400519 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500520 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300521 mState.mAttachedComputeShader = nullptr;
522 break;
523 }
524 default:
525 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527}
528
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000529int Program::getAttachedShadersCount() const
530{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300531 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
532 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000533}
534
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535void Program::bindAttributeLocation(GLuint index, const char *name)
536{
Geoff Langd8605522016-04-13 10:19:12 -0400537 mAttributeBindings.bindLocation(index, name);
538}
539
540void Program::bindUniformLocation(GLuint index, const char *name)
541{
542 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800543 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544}
545
Sami Väisänen46eaa942016-06-29 10:26:37 +0300546void Program::bindFragmentInputLocation(GLint index, const char *name)
547{
548 mFragmentInputBindings.bindLocation(index, name);
549}
550
Jamie Madillbd044ed2017-06-05 12:59:21 -0400551BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300552{
553 BindingInfo ret;
554 ret.type = GL_NONE;
555 ret.valid = false;
556
Jamie Madillbd044ed2017-06-05 12:59:21 -0400557 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300558 ASSERT(fragmentShader);
559
560 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400561 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300562
563 for (const auto &binding : mFragmentInputBindings)
564 {
565 if (binding.second != static_cast<GLuint>(index))
566 continue;
567
568 ret.valid = true;
569
570 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400571 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300572
573 for (const auto &in : inputs)
574 {
575 if (in.name == originalName)
576 {
577 if (in.isArray())
578 {
579 // The client wants to bind either "name" or "name[0]".
580 // GL ES 3.1 spec refers to active array names with language such as:
581 // "if the string identifies the base name of an active array, where the
582 // string would exactly match the name of the variable if the suffix "[0]"
583 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400584 if (arrayIndex == GL_INVALID_INDEX)
585 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300586
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400587 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300588 }
589 else
590 {
591 ret.name = in.mappedName;
592 }
593 ret.type = in.type;
594 return ret;
595 }
596 }
597 }
598
599 return ret;
600}
601
Jamie Madillbd044ed2017-06-05 12:59:21 -0400602void Program::pathFragmentInputGen(const Context *context,
603 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300604 GLenum genMode,
605 GLint components,
606 const GLfloat *coeffs)
607{
608 // If the location is -1 then the command is silently ignored
609 if (index == -1)
610 return;
611
Jamie Madillbd044ed2017-06-05 12:59:21 -0400612 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300613
614 // If the input doesn't exist then then the command is silently ignored
615 // This could happen through optimization for example, the shader translator
616 // decides that a variable is not actually being used and optimizes it away.
617 if (binding.name.empty())
618 return;
619
620 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
621}
622
Martin Radev4c4c8e72016-08-04 12:25:34 +0300623// The attached shaders are checked for linking errors by matching up their variables.
624// Uniform, input and output variables get collected.
625// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500626Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000627{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500628 const auto &data = context->getContextState();
629
Jamie Madill6c58b062017-08-01 13:44:25 -0400630 auto *platform = ANGLEPlatformCurrent();
631 double startTime = platform->currentTime(platform);
632
Jamie Madill6c1f6712017-02-14 19:08:04 -0500633 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000634
Jamie Madill32447362017-06-28 14:53:52 -0400635 ProgramHash programHash;
636 auto *cache = context->getMemoryProgramCache();
637 if (cache)
638 {
639 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
Jamie Madill6c58b062017-08-01 13:44:25 -0400640 ANGLE_HISTOGRAM_BOOLEAN("GPU.ANGLE.ProgramCache.LoadBinarySuccess", mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400641 }
642
643 if (mLinked)
644 {
Jamie Madill6c58b062017-08-01 13:44:25 -0400645 double delta = platform->currentTime(platform) - startTime;
646 int us = static_cast<int>(delta * 1000000.0);
647 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheHitTimeUS", us);
Jamie Madill32447362017-06-28 14:53:52 -0400648 return NoError();
649 }
650
651 // Cache load failed, fall through to normal linking.
652 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000653 mInfoLog.reset();
654
Martin Radev4c4c8e72016-08-04 12:25:34 +0300655 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500656
Jamie Madill192745a2016-12-22 15:58:21 -0500657 auto vertexShader = mState.mAttachedVertexShader;
658 auto fragmentShader = mState.mAttachedFragmentShader;
659 auto computeShader = mState.mAttachedComputeShader;
660
661 bool isComputeShaderAttached = (computeShader != nullptr);
662 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300663 // Check whether we both have a compute and non-compute shaders attached.
664 // If there are of both types attached, then linking should fail.
665 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
666 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500667 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300668 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
669 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400670 }
671
Jamie Madill192745a2016-12-22 15:58:21 -0500672 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500673 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400674 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300675 {
676 mInfoLog << "Attached compute shader is not compiled.";
677 return NoError();
678 }
Jamie Madill192745a2016-12-22 15:58:21 -0500679 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300680
Jamie Madillbd044ed2017-06-05 12:59:21 -0400681 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300682
683 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
684 // If the work group size is not specified, a link time error should occur.
685 if (!mState.mComputeShaderLocalSize.isDeclared())
686 {
687 mInfoLog << "Work group size is not specified.";
688 return NoError();
689 }
690
Jamie Madillbd044ed2017-06-05 12:59:21 -0400691 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300692 {
693 return NoError();
694 }
695
Jamie Madillbd044ed2017-06-05 12:59:21 -0400696 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300697 {
698 return NoError();
699 }
700
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500701 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400702 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500703 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300704 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500705 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300706 }
707 }
708 else
709 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400710 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300711 {
712 return NoError();
713 }
Jamie Madill192745a2016-12-22 15:58:21 -0500714 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300715
Jamie Madillbd044ed2017-06-05 12:59:21 -0400716 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717 {
718 return NoError();
719 }
Jamie Madill192745a2016-12-22 15:58:21 -0500720 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300721
Jamie Madillbd044ed2017-06-05 12:59:21 -0400722 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300723 {
724 mInfoLog << "Fragment shader version does not match vertex shader version.";
725 return NoError();
726 }
727
Jamie Madillbd044ed2017-06-05 12:59:21 -0400728 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300729 {
730 return NoError();
731 }
732
Jamie Madillbd044ed2017-06-05 12:59:21 -0400733 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300734 {
735 return NoError();
736 }
737
Jamie Madillbd044ed2017-06-05 12:59:21 -0400738 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300739 {
740 return NoError();
741 }
742
Jamie Madillbd044ed2017-06-05 12:59:21 -0400743 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300744 {
745 return NoError();
746 }
747
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400748 if (!linkValidateGlobalNames(context, mInfoLog))
749 {
750 return NoError();
751 }
752
Jamie Madillbd044ed2017-06-05 12:59:21 -0400753 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300754
Martin Radev7cf61662017-07-26 17:10:53 +0300755 mState.mNumViews = vertexShader->getNumViews(context);
756
Jamie Madillbd044ed2017-06-05 12:59:21 -0400757 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300758
Jamie Madill192745a2016-12-22 15:58:21 -0500759 // Validate we can pack the varyings.
760 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
761
762 // Map the varyings to the register file
763 // In WebGL, we use a slightly different handling for packing variables.
764 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
765 : PackMode::ANGLE_RELAXED;
766 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
767 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
768 mState.getTransformFeedbackVaryingNames()))
769 {
770 return NoError();
771 }
772
Olli Etuaho39e78122017-08-29 14:34:22 +0300773 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
774 {
775 return NoError();
776 }
777
Jamie Madillc564c072017-06-01 12:45:42 -0400778 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500779 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300780 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500781 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300782 }
783
784 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500785 }
786
jchen10eaef1e52017-06-13 10:44:11 +0800787 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400788 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400789
jchen10eaef1e52017-06-13 10:44:11 +0800790 setUniformValuesFromBindingQualifiers();
791
Jamie Madill54164b02017-08-28 15:17:37 -0400792 // Mark implementation-specific unreferenced uniforms as ignored.
793 mProgram->markUnusedUniformLocations(&mState.mUniformLocations);
794
795 // Update sampler bindings with unreferenced uniforms.
796 for (const auto &location : mState.mUniformLocations)
797 {
798 if (!location.used && mState.isSamplerUniformIndex(location.index))
799 {
800 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(location.index);
801 mState.mSamplerBindings[samplerIndex].unreferenced = true;
802 }
803 }
804
Jamie Madill32447362017-06-28 14:53:52 -0400805 // Save to the program cache.
806 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
807 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
808 {
809 cache->putProgram(programHash, context, this);
810 }
811
Jamie Madill6c58b062017-08-01 13:44:25 -0400812 double delta = platform->currentTime(platform) - startTime;
813 int us = static_cast<int>(delta * 1000000.0);
814 ANGLE_HISTOGRAM_COUNTS("GPU.ANGLE.ProgramCache.ProgramCacheMissTimeUS", us);
815
Martin Radev4c4c8e72016-08-04 12:25:34 +0300816 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000817}
818
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000819// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500820void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400822 mState.mAttributes.clear();
823 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800824 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400825 mState.mUniforms.clear();
826 mState.mUniformLocations.clear();
827 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800828 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800829 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400830 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800831 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400832 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400833 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300834 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500835 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800836 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300837 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500838
Geoff Lang7dd2e102014-11-10 15:19:26 -0500839 mValidated = false;
840
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000841 mLinked = false;
842}
843
Geoff Lange1a27752015-10-05 13:16:04 -0400844bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000845{
846 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madilla2c74982016-12-12 11:20:42 -0500849Error Program::loadBinary(const Context *context,
850 GLenum binaryFormat,
851 const void *binary,
852 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000853{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500854 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000855
Geoff Lang7dd2e102014-11-10 15:19:26 -0500856#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800857 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500858#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400859 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
860 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000861 {
Jamie Madillf6113162015-05-07 11:49:21 -0400862 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800863 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500864 }
865
Jamie Madill4f86d052017-06-05 12:59:26 -0400866 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
867 ANGLE_TRY_RESULT(
868 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400869
870 // Currently we require the full shader text to compute the program hash.
871 // TODO(jmadill): Store the binary in the internal program cache.
872
Jamie Madillb0a838b2016-11-13 20:02:12 -0500873 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500874#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500875}
876
Jamie Madilla2c74982016-12-12 11:20:42 -0500877Error Program::saveBinary(const Context *context,
878 GLenum *binaryFormat,
879 void *binary,
880 GLsizei bufSize,
881 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500882{
883 if (binaryFormat)
884 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400885 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500886 }
887
Jamie Madill4f86d052017-06-05 12:59:26 -0400888 angle::MemoryBuffer memoryBuf;
889 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500890
Jamie Madill4f86d052017-06-05 12:59:26 -0400891 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
892 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500893
894 if (streamLength > bufSize)
895 {
896 if (length)
897 {
898 *length = 0;
899 }
900
901 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
902 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
903 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500904 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500905 }
906
907 if (binary)
908 {
909 char *ptr = reinterpret_cast<char*>(binary);
910
Jamie Madill48ef11b2016-04-27 15:21:52 -0400911 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500912 ptr += streamLength;
913
914 ASSERT(ptr - streamLength == binary);
915 }
916
917 if (length)
918 {
919 *length = streamLength;
920 }
921
He Yunchaoacd18982017-01-04 10:46:42 +0800922 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500923}
924
Jamie Madillffe00c02017-06-27 16:26:55 -0400925GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500926{
927 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400928 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500929 if (error.isError())
930 {
931 return 0;
932 }
933
934 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000935}
936
Geoff Langc5629752015-12-07 16:29:04 -0500937void Program::setBinaryRetrievableHint(bool retrievable)
938{
939 // TODO(jmadill) : replace with dirty bits
940 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400941 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500942}
943
944bool Program::getBinaryRetrievableHint() const
945{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400946 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500947}
948
Yunchao He61afff12017-03-14 15:34:03 +0800949void Program::setSeparable(bool separable)
950{
951 // TODO(yunchao) : replace with dirty bits
952 if (mState.mSeparable != separable)
953 {
954 mProgram->setSeparable(separable);
955 mState.mSeparable = separable;
956 }
957}
958
959bool Program::isSeparable() const
960{
961 return mState.mSeparable;
962}
963
Jamie Madill6c1f6712017-02-14 19:08:04 -0500964void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965{
966 mRefCount--;
967
968 if (mRefCount == 0 && mDeleteStatus)
969 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500970 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000971 }
972}
973
974void Program::addRef()
975{
976 mRefCount++;
977}
978
979unsigned int Program::getRefCount() const
980{
981 return mRefCount;
982}
983
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000984int Program::getInfoLogLength() const
985{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400986 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000987}
988
Geoff Lange1a27752015-10-05 13:16:04 -0400989void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000990{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000991 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000992}
993
Geoff Lange1a27752015-10-05 13:16:04 -0400994void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000995{
996 int total = 0;
997
Martin Radev4c4c8e72016-08-04 12:25:34 +0300998 if (mState.mAttachedComputeShader)
999 {
1000 if (total < maxCount)
1001 {
1002 shaders[total] = mState.mAttachedComputeShader->getHandle();
1003 total++;
1004 }
1005 }
1006
Jamie Madill48ef11b2016-04-27 15:21:52 -04001007 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001008 {
1009 if (total < maxCount)
1010 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001011 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001012 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001013 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001014 }
1015
Jamie Madill48ef11b2016-04-27 15:21:52 -04001016 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001017 {
1018 if (total < maxCount)
1019 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001020 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001021 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001022 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001023 }
1024
1025 if (count)
1026 {
1027 *count = total;
1028 }
1029}
1030
Geoff Lange1a27752015-10-05 13:16:04 -04001031GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001032{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001033 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001034}
1035
Jamie Madill63805b42015-08-25 13:17:39 -04001036bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001037{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001038 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1039 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001040}
1041
jchen10fd7c3b52017-03-21 15:36:03 +08001042void Program::getActiveAttribute(GLuint index,
1043 GLsizei bufsize,
1044 GLsizei *length,
1045 GLint *size,
1046 GLenum *type,
1047 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001048{
Jamie Madillc349ec02015-08-21 16:53:12 -04001049 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001050 {
1051 if (bufsize > 0)
1052 {
1053 name[0] = '\0';
1054 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001056 if (length)
1057 {
1058 *length = 0;
1059 }
1060
1061 *type = GL_NONE;
1062 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001063 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001064 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001065
jchen1036e120e2017-03-14 14:53:58 +08001066 ASSERT(index < mState.mAttributes.size());
1067 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001068
1069 if (bufsize > 0)
1070 {
jchen10fd7c3b52017-03-21 15:36:03 +08001071 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001072 }
1073
1074 // Always a single 'type' instance
1075 *size = 1;
1076 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001077}
1078
Geoff Lange1a27752015-10-05 13:16:04 -04001079GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001080{
Jamie Madillc349ec02015-08-21 16:53:12 -04001081 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001082 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001083 return 0;
1084 }
1085
jchen1036e120e2017-03-14 14:53:58 +08001086 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001087}
1088
Geoff Lange1a27752015-10-05 13:16:04 -04001089GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001090{
Jamie Madillc349ec02015-08-21 16:53:12 -04001091 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001092 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001093 return 0;
1094 }
1095
1096 size_t maxLength = 0;
1097
Jamie Madill48ef11b2016-04-27 15:21:52 -04001098 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001099 {
jchen1036e120e2017-03-14 14:53:58 +08001100 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001101 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001102
Jamie Madillc349ec02015-08-21 16:53:12 -04001103 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104}
1105
jchen1015015f72017-03-16 13:54:21 +08001106GLuint Program::getInputResourceIndex(const GLchar *name) const
1107{
1108 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1109 {
1110 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1111 if (attribute.name == name)
1112 {
1113 return attributeIndex;
1114 }
1115 }
1116 return GL_INVALID_INDEX;
1117}
1118
1119GLuint Program::getOutputResourceIndex(const GLchar *name) const
1120{
1121 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1122}
1123
jchen10fd7c3b52017-03-21 15:36:03 +08001124size_t Program::getOutputResourceCount() const
1125{
1126 return (mLinked ? mState.mOutputVariables.size() : 0);
1127}
1128
1129void Program::getInputResourceName(GLuint index,
1130 GLsizei bufSize,
1131 GLsizei *length,
1132 GLchar *name) const
1133{
1134 GLint size;
1135 GLenum type;
1136 getActiveAttribute(index, bufSize, length, &size, &type, name);
1137}
1138
1139void Program::getOutputResourceName(GLuint index,
1140 GLsizei bufSize,
1141 GLsizei *length,
1142 GLchar *name) const
1143{
1144 if (length)
1145 {
1146 *length = 0;
1147 }
1148
1149 if (!mLinked)
1150 {
1151 if (bufSize > 0)
1152 {
1153 name[0] = '\0';
1154 }
1155 return;
1156 }
1157 ASSERT(index < mState.mOutputVariables.size());
1158 const auto &output = mState.mOutputVariables[index];
1159
1160 if (bufSize > 0)
1161 {
1162 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1163
1164 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1165 }
1166}
1167
jchen10880683b2017-04-12 16:21:55 +08001168const sh::Attribute &Program::getInputResource(GLuint index) const
1169{
1170 ASSERT(index < mState.mAttributes.size());
1171 return mState.mAttributes[index];
1172}
1173
1174const sh::OutputVariable &Program::getOutputResource(GLuint index) const
1175{
1176 ASSERT(index < mState.mOutputVariables.size());
1177 return mState.mOutputVariables[index];
1178}
1179
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180GLint Program::getFragDataLocation(const std::string &name) const
1181{
1182 std::string baseName(name);
1183 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001184 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001185 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001186 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1188 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001189 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001190 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001191 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001192 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001193}
1194
Geoff Lange1a27752015-10-05 13:16:04 -04001195void Program::getActiveUniform(GLuint index,
1196 GLsizei bufsize,
1197 GLsizei *length,
1198 GLint *size,
1199 GLenum *type,
1200 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001201{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001203 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001204 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001205 ASSERT(index < mState.mUniforms.size());
1206 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207
1208 if (bufsize > 0)
1209 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 std::string string = uniform.name;
1211 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001212 {
1213 string += "[0]";
1214 }
jchen10fd7c3b52017-03-21 15:36:03 +08001215 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 }
1217
Jamie Madill62d31cb2015-09-11 13:25:51 -04001218 *size = uniform.elementCount();
1219 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001220 }
1221 else
1222 {
1223 if (bufsize > 0)
1224 {
1225 name[0] = '\0';
1226 }
1227
1228 if (length)
1229 {
1230 *length = 0;
1231 }
1232
1233 *size = 0;
1234 *type = GL_NONE;
1235 }
1236}
1237
Geoff Lange1a27752015-10-05 13:16:04 -04001238GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001239{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001242 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001243 }
1244 else
1245 {
1246 return 0;
1247 }
1248}
1249
Geoff Lange1a27752015-10-05 13:16:04 -04001250GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001251{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001252 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253
1254 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001255 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001256 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001258 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001259 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001260 size_t length = uniform.name.length() + 1u;
1261 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262 {
1263 length += 3; // Counting in "[0]".
1264 }
1265 maxLength = std::max(length, maxLength);
1266 }
1267 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001268 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271}
1272
1273GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1274{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001275 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001276 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001278 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001279 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1280 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1281 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001282 case GL_UNIFORM_BLOCK_INDEX:
1283 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1285 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1286 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1287 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1288 default:
1289 UNREACHABLE();
1290 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001291 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292 return 0;
1293}
1294
1295bool Program::isValidUniformLocation(GLint location) const
1296{
Jamie Madille2e406c2016-06-02 13:04:10 -04001297 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001298 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1299 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001300}
1301
Jamie Madill62d31cb2015-09-11 13:25:51 -04001302const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001303{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001304 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001305 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306}
1307
Jamie Madillac4e9c32017-01-13 14:07:12 -05001308const VariableLocation &Program::getUniformLocation(GLint location) const
1309{
1310 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1311 return mState.mUniformLocations[location];
1312}
1313
1314const std::vector<VariableLocation> &Program::getUniformLocations() const
1315{
1316 return mState.mUniformLocations;
1317}
1318
1319const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1320{
1321 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1322 return mState.mUniforms[index];
1323}
1324
Jamie Madill62d31cb2015-09-11 13:25:51 -04001325GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001327 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001328}
1329
Jamie Madill62d31cb2015-09-11 13:25:51 -04001330GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331{
Jamie Madille7d84322017-01-10 18:21:59 -05001332 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001333}
1334
1335void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1336{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001337 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1338 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001339 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340}
1341
1342void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1343{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001344 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1345 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001346 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347}
1348
1349void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1350{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001351 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1352 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001353 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354}
1355
1356void Program::setUniform4fv(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, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001360 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361}
1362
Jamie Madill81c2e252017-09-09 23:32:46 -04001363Program::SetUniformResult Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001365 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1366 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1367
Jamie Madill81c2e252017-09-09 23:32:46 -04001368 mProgram->setUniform1iv(location, clampedCount, v);
1369
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001370 if (mState.isSamplerUniformIndex(locationInfo.index))
1371 {
1372 updateSamplerUniform(locationInfo, clampedCount, v);
Jamie Madill81c2e252017-09-09 23:32:46 -04001373 return SetUniformResult::SamplerChanged;
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001374 }
1375
Jamie Madill81c2e252017-09-09 23:32:46 -04001376 return SetUniformResult::NoSamplerChange;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377}
1378
1379void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1380{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001381 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1382 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001383 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384}
1385
1386void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1387{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001388 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1389 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001390 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391}
1392
1393void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1394{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001395 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1396 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001397 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398}
1399
1400void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1401{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001402 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1403 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001404 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405}
1406
1407void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1408{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001409 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1410 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001411 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412}
1413
1414void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1415{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001416 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1417 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001418 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419}
1420
1421void Program::setUniform4uiv(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, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001425 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426}
1427
1428void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1429{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001430 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001431 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432}
1433
1434void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1435{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001436 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001437 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438}
1439
1440void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1441{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001442 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001443 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444}
1445
1446void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1447{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001448 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001449 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450}
1451
1452void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1453{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001454 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001455 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456}
1457
1458void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1459{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001460 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001461 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462}
1463
1464void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1465{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001466 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001467 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468}
1469
1470void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1471{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001472 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001473 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
1476void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1477{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001478 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001479 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
Jamie Madill54164b02017-08-28 15:17:37 -04001482void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483{
Jamie Madill54164b02017-08-28 15:17:37 -04001484 const auto &uniformLocation = mState.getUniformLocations()[location];
1485 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1486
1487 GLenum nativeType = gl::VariableComponentType(uniform.type);
1488 if (nativeType == GL_FLOAT)
1489 {
1490 mProgram->getUniformfv(context, location, v);
1491 }
1492 else
1493 {
1494 getUniformInternal(context, v, location, nativeType,
1495 gl::VariableComponentCount(uniform.type));
1496 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497}
1498
Jamie Madill54164b02017-08-28 15:17:37 -04001499void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001500{
Jamie Madill54164b02017-08-28 15:17:37 -04001501 const auto &uniformLocation = mState.getUniformLocations()[location];
1502 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1503
1504 GLenum nativeType = gl::VariableComponentType(uniform.type);
1505 if (nativeType == GL_INT || nativeType == GL_BOOL)
1506 {
1507 mProgram->getUniformiv(context, location, v);
1508 }
1509 else
1510 {
1511 getUniformInternal(context, v, location, nativeType,
1512 gl::VariableComponentCount(uniform.type));
1513 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001514}
1515
Jamie Madill54164b02017-08-28 15:17:37 -04001516void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517{
Jamie Madill54164b02017-08-28 15:17:37 -04001518 const auto &uniformLocation = mState.getUniformLocations()[location];
1519 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1520
1521 GLenum nativeType = gl::VariableComponentType(uniform.type);
1522 if (nativeType == GL_UNSIGNED_INT)
1523 {
1524 mProgram->getUniformuiv(context, location, v);
1525 }
1526 else
1527 {
1528 getUniformInternal(context, v, location, nativeType,
1529 gl::VariableComponentCount(uniform.type));
1530 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001531}
1532
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533void Program::flagForDeletion()
1534{
1535 mDeleteStatus = true;
1536}
1537
1538bool Program::isFlaggedForDeletion() const
1539{
1540 return mDeleteStatus;
1541}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001542
Brandon Jones43a53e22014-08-28 16:23:22 -07001543void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001544{
1545 mInfoLog.reset();
1546
Geoff Lang7dd2e102014-11-10 15:19:26 -05001547 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001548 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001549 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001550 }
1551 else
1552 {
Jamie Madillf6113162015-05-07 11:49:21 -04001553 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001554 }
1555}
1556
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1558{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001559 // Skip cache if we're using an infolog, so we get the full error.
1560 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1561 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1562 {
1563 return mCachedValidateSamplersResult.value();
1564 }
1565
1566 if (mTextureUnitTypesCache.empty())
1567 {
1568 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1569 }
1570 else
1571 {
1572 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1573 }
1574
1575 // if any two active samplers in a program are of different types, but refer to the same
1576 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1577 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001578 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001579 {
Jamie Madill54164b02017-08-28 15:17:37 -04001580 if (samplerBinding.unreferenced)
1581 continue;
1582
Jamie Madille7d84322017-01-10 18:21:59 -05001583 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001584
Jamie Madille7d84322017-01-10 18:21:59 -05001585 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001586 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001587 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1588 {
1589 if (infoLog)
1590 {
1591 (*infoLog) << "Sampler uniform (" << textureUnit
1592 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1593 << caps.maxCombinedTextureImageUnits << ")";
1594 }
1595
1596 mCachedValidateSamplersResult = false;
1597 return false;
1598 }
1599
1600 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1601 {
1602 if (textureType != mTextureUnitTypesCache[textureUnit])
1603 {
1604 if (infoLog)
1605 {
1606 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1607 "image unit ("
1608 << textureUnit << ").";
1609 }
1610
1611 mCachedValidateSamplersResult = false;
1612 return false;
1613 }
1614 }
1615 else
1616 {
1617 mTextureUnitTypesCache[textureUnit] = textureType;
1618 }
1619 }
1620 }
1621
1622 mCachedValidateSamplersResult = true;
1623 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624}
1625
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001626bool Program::isValidated() const
1627{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628 return mValidated;
1629}
1630
Geoff Lange1a27752015-10-05 13:16:04 -04001631GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001633 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634}
1635
1636void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1637{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001638 ASSERT(
1639 uniformBlockIndex <
1640 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001641
Jamie Madill48ef11b2016-04-27 15:21:52 -04001642 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643
1644 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001645 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 std::string string = uniformBlock.name;
1647
Jamie Madill62d31cb2015-09-11 13:25:51 -04001648 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001649 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001650 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651 }
jchen10fd7c3b52017-03-21 15:36:03 +08001652 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001653 }
1654}
1655
Geoff Lange1a27752015-10-05 13:16:04 -04001656GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001657{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658 int maxLength = 0;
1659
1660 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001661 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001662 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1664 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001665 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 if (!uniformBlock.name.empty())
1667 {
jchen10af713a22017-04-19 09:10:56 +08001668 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1669 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670 }
1671 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001672 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673
1674 return maxLength;
1675}
1676
Geoff Lange1a27752015-10-05 13:16:04 -04001677GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001680 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001681
Jamie Madill48ef11b2016-04-27 15:21:52 -04001682 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1684 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001685 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001686 if (uniformBlock.name == baseName)
1687 {
1688 const bool arrayElementZero =
1689 (subscript == GL_INVALID_INDEX &&
1690 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1691 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1692 {
1693 return blockIndex;
1694 }
1695 }
1696 }
1697
1698 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001699}
1700
Jamie Madill62d31cb2015-09-11 13:25:51 -04001701const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001702{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001703 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1704 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001705}
1706
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001707void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1708{
jchen107a20b972017-06-13 14:25:26 +08001709 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001710 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001711 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001712}
1713
1714GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1715{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001716 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001717}
1718
Geoff Lang48dcae72014-02-05 16:28:24 -05001719void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1720{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001722 for (GLsizei i = 0; i < count; i++)
1723 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001724 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001725 }
1726
Jamie Madill48ef11b2016-04-27 15:21:52 -04001727 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001728}
1729
1730void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1731{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001732 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001733 {
jchen10a9042d32017-03-17 08:50:45 +08001734 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1735 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1736 std::string varName = var.nameWithArrayIndex();
1737 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001738 if (length)
1739 {
1740 *length = lastNameIdx;
1741 }
1742 if (size)
1743 {
jchen10a9042d32017-03-17 08:50:45 +08001744 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001745 }
1746 if (type)
1747 {
jchen10a9042d32017-03-17 08:50:45 +08001748 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001749 }
1750 if (name)
1751 {
jchen10a9042d32017-03-17 08:50:45 +08001752 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001753 name[lastNameIdx] = '\0';
1754 }
1755 }
1756}
1757
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001758GLsizei Program::getTransformFeedbackVaryingCount() const
1759{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001760 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001761 {
jchen10a9042d32017-03-17 08:50:45 +08001762 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001763 }
1764 else
1765 {
1766 return 0;
1767 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001768}
1769
1770GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1771{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001773 {
1774 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001775 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001776 {
jchen10a9042d32017-03-17 08:50:45 +08001777 maxSize =
1778 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001779 }
1780
1781 return maxSize;
1782 }
1783 else
1784 {
1785 return 0;
1786 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001787}
1788
1789GLenum Program::getTransformFeedbackBufferMode() const
1790{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001791 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792}
1793
Jamie Madillbd044ed2017-06-05 12:59:21 -04001794bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001796 Shader *vertexShader = mState.mAttachedVertexShader;
1797 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001798
Jamie Madillbd044ed2017-06-05 12:59:21 -04001799 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001800
Jamie Madillbd044ed2017-06-05 12:59:21 -04001801 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1802 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803
Sami Väisänen46eaa942016-06-29 10:26:37 +03001804 std::map<GLuint, std::string> staticFragmentInputLocations;
1805
Jamie Madill4cff2472015-08-21 16:53:18 -04001806 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808 bool matched = false;
1809
1810 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001811 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812 {
1813 continue;
1814 }
1815
Jamie Madill4cff2472015-08-21 16:53:18 -04001816 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001818 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001819 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001820 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001821 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001822 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 {
1824 return false;
1825 }
1826
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827 matched = true;
1828 break;
1829 }
1830 }
1831
1832 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001833 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001835 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001836 return false;
1837 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001838
1839 // Check for aliased path rendering input bindings (if any).
1840 // If more than one binding refer statically to the same
1841 // location the link must fail.
1842
1843 if (!output.staticUse)
1844 continue;
1845
1846 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1847 if (inputBinding == -1)
1848 continue;
1849
1850 const auto it = staticFragmentInputLocations.find(inputBinding);
1851 if (it == std::end(staticFragmentInputLocations))
1852 {
1853 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1854 }
1855 else
1856 {
1857 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1858 << it->second;
1859 return false;
1860 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861 }
1862
Jamie Madillbd044ed2017-06-05 12:59:21 -04001863 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001864 {
1865 return false;
1866 }
1867
Jamie Madillada9ecc2015-08-17 12:53:37 -04001868 // TODO(jmadill): verify no unmatched vertex varyings?
1869
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 return true;
1871}
1872
Jamie Madillbd044ed2017-06-05 12:59:21 -04001873bool Program::linkUniforms(const Context *context,
1874 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001875 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001876{
Olli Etuahob78707c2017-03-09 15:03:11 +00001877 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001878 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001879 {
1880 return false;
1881 }
1882
Olli Etuahob78707c2017-03-09 15:03:11 +00001883 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001884
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001885 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001886
jchen10eaef1e52017-06-13 10:44:11 +08001887 if (!linkAtomicCounterBuffers())
1888 {
1889 return false;
1890 }
1891
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001892 return true;
1893}
1894
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001895void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001896{
Jamie Madill982f6e02017-06-07 14:33:04 -04001897 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1898 unsigned int low = high;
1899
jchen10eaef1e52017-06-13 10:44:11 +08001900 for (auto counterIter = mState.mUniforms.rbegin();
1901 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1902 {
1903 --low;
1904 }
1905
1906 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1907
1908 high = low;
1909
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001910 for (auto imageIter = mState.mUniforms.rbegin();
1911 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1912 {
1913 --low;
1914 }
1915
1916 mState.mImageUniformRange = RangeUI(low, high);
1917
1918 // If uniform is a image type, insert it into the mImageBindings array.
1919 for (unsigned int imageIndex : mState.mImageUniformRange)
1920 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001921 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1922 // cannot load values into a uniform defined as an image. if declare without a
1923 // binding qualifier, any uniform image variable (include all elements of
1924 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001925 auto &imageUniform = mState.mUniforms[imageIndex];
1926 if (imageUniform.binding == -1)
1927 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001928 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001929 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001930 else
1931 {
1932 mState.mImageBindings.emplace_back(
1933 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1934 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001935 }
1936
1937 high = low;
1938
1939 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001940 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001941 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001942 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001943 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001944
1945 mState.mSamplerUniformRange = RangeUI(low, high);
1946
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001947 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001948 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001949 {
1950 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1951 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1952 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001953 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001954 }
1955}
1956
jchen10eaef1e52017-06-13 10:44:11 +08001957bool Program::linkAtomicCounterBuffers()
1958{
1959 for (unsigned int index : mState.mAtomicCounterUniformRange)
1960 {
1961 auto &uniform = mState.mUniforms[index];
1962 bool found = false;
1963 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1964 ++bufferIndex)
1965 {
1966 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1967 if (buffer.binding == uniform.binding)
1968 {
1969 buffer.memberIndexes.push_back(index);
1970 uniform.bufferIndex = bufferIndex;
1971 found = true;
1972 break;
1973 }
1974 }
1975 if (!found)
1976 {
1977 AtomicCounterBuffer atomicCounterBuffer;
1978 atomicCounterBuffer.binding = uniform.binding;
1979 atomicCounterBuffer.memberIndexes.push_back(index);
1980 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1981 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1982 }
1983 }
1984 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1985 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1986
1987 return true;
1988}
1989
Martin Radev4c4c8e72016-08-04 12:25:34 +03001990bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1991 const std::string &uniformName,
1992 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001993 const sh::InterfaceBlockField &fragmentUniform,
1994 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001995{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001996 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1997 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1998 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999 {
2000 return false;
2001 }
2002
2003 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2004 {
Jamie Madillf6113162015-05-07 11:49:21 -04002005 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006 return false;
2007 }
2008
2009 return true;
2010}
2011
Jamie Madilleb979bf2016-11-15 12:28:46 -05002012// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002013bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002015 const ContextState &data = context->getContextState();
2016 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002017
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002019 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002020 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002021
2022 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002023 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002024 {
Jamie Madillf6113162015-05-07 11:49:21 -04002025 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002026 return false;
2027 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002029 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002030
Jamie Madillc349ec02015-08-21 16:53:12 -04002031 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002032 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002033 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002034 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002035 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002036 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002037 attribute.location = bindingLocation;
2038 }
2039
2040 if (attribute.location != -1)
2041 {
2042 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002043 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044
Jamie Madill63805b42015-08-25 13:17:39 -04002045 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046 {
Jamie Madillf6113162015-05-07 11:49:21 -04002047 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002048 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049
2050 return false;
2051 }
2052
Jamie Madill63805b42015-08-25 13:17:39 -04002053 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054 {
Jamie Madill63805b42015-08-25 13:17:39 -04002055 const int regLocation = attribute.location + reg;
2056 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057
2058 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002059 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002060 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002062 // TODO(jmadill): fix aliasing on ES2
2063 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002065 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002066 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 return false;
2068 }
2069 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 else
2071 {
Jamie Madill63805b42015-08-25 13:17:39 -04002072 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002073 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074
Jamie Madill63805b42015-08-25 13:17:39 -04002075 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076 }
2077 }
2078 }
2079
2080 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002081 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002083 // Not set by glBindAttribLocation or by location layout qualifier
2084 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 {
Jamie Madill63805b42015-08-25 13:17:39 -04002086 int regs = VariableRegisterCount(attribute.type);
2087 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088
Jamie Madill63805b42015-08-25 13:17:39 -04002089 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 {
Jamie Madillf6113162015-05-07 11:49:21 -04002091 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002092 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093 }
2094
Jamie Madillc349ec02015-08-21 16:53:12 -04002095 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 }
2097 }
2098
Jamie Madill48ef11b2016-04-27 15:21:52 -04002099 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100 {
Jamie Madill63805b42015-08-25 13:17:39 -04002101 ASSERT(attribute.location != -1);
2102 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002103
Jamie Madill63805b42015-08-25 13:17:39 -04002104 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002106 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 }
2108 }
2109
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 return true;
2111}
2112
Martin Radev4c4c8e72016-08-04 12:25:34 +03002113bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2114 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2115 const std::string &errorMessage,
2116 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002118 GLuint blockCount = 0;
2119 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002121 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002122 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002123 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002124 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002125 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002126 return false;
2127 }
2128 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002130 return true;
2131}
Jamie Madille473dee2015-08-18 14:49:01 -04002132
Martin Radev4c4c8e72016-08-04 12:25:34 +03002133bool Program::validateVertexAndFragmentInterfaceBlocks(
2134 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2135 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002136 InfoLog &infoLog,
2137 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002138{
2139 // Check that interface blocks defined in the vertex and fragment shaders are identical
2140 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2141 UniformBlockMap linkedUniformBlocks;
2142
2143 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2144 {
2145 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2146 }
2147
Jamie Madille473dee2015-08-18 14:49:01 -04002148 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149 {
Jamie Madille473dee2015-08-18 14:49:01 -04002150 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 if (entry != linkedUniformBlocks.end())
2152 {
2153 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002154 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2155 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002156 {
2157 return false;
2158 }
2159 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002160 }
2161 return true;
2162}
Jamie Madille473dee2015-08-18 14:49:01 -04002163
Jamie Madillbd044ed2017-06-05 12:59:21 -04002164bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002165{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002166 const auto &caps = context->getCaps();
2167
Martin Radev4c4c8e72016-08-04 12:25:34 +03002168 if (mState.mAttachedComputeShader)
2169 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002170 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002171 const auto &computeInterfaceBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002172
2173 if (!validateUniformBlocksCount(
2174 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2175 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2176 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002178 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002180 return true;
2181 }
2182
Jamie Madillbd044ed2017-06-05 12:59:21 -04002183 Shader &vertexShader = *mState.mAttachedVertexShader;
2184 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002186 const auto &vertexInterfaceBlocks = vertexShader.getUniformBlocks(context);
2187 const auto &fragmentInterfaceBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002188
2189 if (!validateUniformBlocksCount(
2190 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2191 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2192 {
2193 return false;
2194 }
2195 if (!validateUniformBlocksCount(
2196 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2197 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2198 infoLog))
2199 {
2200
2201 return false;
2202 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002203
2204 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002205 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002206 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002207 {
2208 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209 }
Jamie Madille473dee2015-08-18 14:49:01 -04002210
Geoff Lang7dd2e102014-11-10 15:19:26 -05002211 return true;
2212}
2213
Jamie Madilla2c74982016-12-12 11:20:42 -05002214bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002215 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002216 const sh::InterfaceBlock &fragmentInterfaceBlock,
2217 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218{
2219 const char* blockName = vertexInterfaceBlock.name.c_str();
2220 // validate blocks for the same member types
2221 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2222 {
Jamie Madillf6113162015-05-07 11:49:21 -04002223 infoLog << "Types for interface block '" << blockName
2224 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002225 return false;
2226 }
2227 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2228 {
Jamie Madillf6113162015-05-07 11:49:21 -04002229 infoLog << "Array sizes differ for interface block '" << blockName
2230 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002231 return false;
2232 }
jchen10af713a22017-04-19 09:10:56 +08002233 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2234 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2235 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 {
Jamie Madillf6113162015-05-07 11:49:21 -04002237 infoLog << "Layout qualifiers differ for interface block '" << blockName
2238 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002239 return false;
2240 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002241 const unsigned int numBlockMembers =
2242 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002243 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2244 {
2245 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2246 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2247 if (vertexMember.name != fragmentMember.name)
2248 {
Jamie Madillf6113162015-05-07 11:49:21 -04002249 infoLog << "Name mismatch for field " << blockMemberIndex
2250 << " of interface block '" << blockName
2251 << "': (in vertex: '" << vertexMember.name
2252 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002253 return false;
2254 }
2255 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002256 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2257 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002258 {
2259 return false;
2260 }
2261 }
2262 return true;
2263}
2264
2265bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2266 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2267{
2268 if (vertexVariable.type != fragmentVariable.type)
2269 {
Jamie Madillf6113162015-05-07 11:49:21 -04002270 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271 return false;
2272 }
2273 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2274 {
Jamie Madillf6113162015-05-07 11:49:21 -04002275 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002276 return false;
2277 }
2278 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2279 {
Jamie Madillf6113162015-05-07 11:49:21 -04002280 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002281 return false;
2282 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002283 if (vertexVariable.structName != fragmentVariable.structName)
2284 {
2285 infoLog << "Structure names for " << variableName
2286 << " differ between vertex and fragment shaders";
2287 return false;
2288 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289
2290 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2291 {
Jamie Madillf6113162015-05-07 11:49:21 -04002292 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293 return false;
2294 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002295 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2297 {
2298 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2299 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2300
2301 if (vertexMember.name != fragmentMember.name)
2302 {
Jamie Madillf6113162015-05-07 11:49:21 -04002303 infoLog << "Name mismatch for field '" << memberIndex
2304 << "' of " << variableName
2305 << ": (in vertex: '" << vertexMember.name
2306 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002307 return false;
2308 }
2309
2310 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2311 vertexMember.name + "'";
2312
2313 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2314 {
2315 return false;
2316 }
2317 }
2318
2319 return true;
2320}
2321
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002322bool Program::linkValidateVaryings(InfoLog &infoLog,
2323 const std::string &varyingName,
2324 const sh::Varying &vertexVarying,
2325 const sh::Varying &fragmentVarying,
2326 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002327{
2328 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2329 {
2330 return false;
2331 }
2332
Jamie Madille9cc4692015-02-19 16:00:13 -05002333 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002335 infoLog << "Interpolation types for " << varyingName
2336 << " differ between vertex and fragment shaders.";
2337 return false;
2338 }
2339
2340 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2341 {
2342 infoLog << "Invariance for " << varyingName
2343 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002344 return false;
2345 }
2346
2347 return true;
2348}
2349
Jamie Madillbd044ed2017-06-05 12:59:21 -04002350bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002351{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002352 Shader *vertexShader = mState.mAttachedVertexShader;
2353 Shader *fragmentShader = mState.mAttachedFragmentShader;
2354 const auto &vertexVaryings = vertexShader->getVaryings(context);
2355 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2356 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002357
2358 if (shaderVersion != 100)
2359 {
2360 // Only ESSL 1.0 has restrictions on matching input and output invariance
2361 return true;
2362 }
2363
2364 bool glPositionIsInvariant = false;
2365 bool glPointSizeIsInvariant = false;
2366 bool glFragCoordIsInvariant = false;
2367 bool glPointCoordIsInvariant = false;
2368
2369 for (const sh::Varying &varying : vertexVaryings)
2370 {
2371 if (!varying.isBuiltIn())
2372 {
2373 continue;
2374 }
2375 if (varying.name.compare("gl_Position") == 0)
2376 {
2377 glPositionIsInvariant = varying.isInvariant;
2378 }
2379 else if (varying.name.compare("gl_PointSize") == 0)
2380 {
2381 glPointSizeIsInvariant = varying.isInvariant;
2382 }
2383 }
2384
2385 for (const sh::Varying &varying : fragmentVaryings)
2386 {
2387 if (!varying.isBuiltIn())
2388 {
2389 continue;
2390 }
2391 if (varying.name.compare("gl_FragCoord") == 0)
2392 {
2393 glFragCoordIsInvariant = varying.isInvariant;
2394 }
2395 else if (varying.name.compare("gl_PointCoord") == 0)
2396 {
2397 glPointCoordIsInvariant = varying.isInvariant;
2398 }
2399 }
2400
2401 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2402 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2403 // Not requiring invariance to match is supported by:
2404 // dEQP, WebGL CTS, Nexus 5X GLES
2405 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2406 {
2407 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2408 "declared invariant.";
2409 return false;
2410 }
2411 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2412 {
2413 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2414 "declared invariant.";
2415 return false;
2416 }
2417
2418 return true;
2419}
2420
jchen10a9042d32017-03-17 08:50:45 +08002421bool Program::linkValidateTransformFeedback(const gl::Context *context,
2422 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002423 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002424 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002425{
2426 size_t totalComponents = 0;
2427
Jamie Madillccdf74b2015-08-18 10:46:12 -04002428 std::set<std::string> uniqueNames;
2429
Jamie Madill48ef11b2016-04-27 15:21:52 -04002430 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002431 {
2432 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002433 size_t subscript = GL_INVALID_INDEX;
2434 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2435
Jamie Madill192745a2016-12-22 15:58:21 -05002436 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002437 {
Jamie Madill192745a2016-12-22 15:58:21 -05002438 const sh::Varying *varying = ref.second.get();
2439
jchen10a9042d32017-03-17 08:50:45 +08002440 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002442 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002443 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002444 infoLog << "Two transform feedback varyings specify the same output variable ("
2445 << tfVaryingName << ").";
2446 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447 }
jchen10a9042d32017-03-17 08:50:45 +08002448 if (context->getClientVersion() >= Version(3, 1))
2449 {
2450 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2451 {
2452 infoLog
2453 << "Two transform feedback varyings include the same array element ("
2454 << tfVaryingName << ").";
2455 return false;
2456 }
2457 }
2458 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002459 {
2460 infoLog << "Capture of arrays is undefined and not supported.";
2461 return false;
2462 }
2463
jchen10a9042d32017-03-17 08:50:45 +08002464 uniqueNames.insert(tfVaryingName);
2465
Jamie Madillccdf74b2015-08-18 10:46:12 -04002466 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002467 size_t elementCount =
2468 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2469 : 1);
2470 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002471 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472 componentCount > caps.maxTransformFeedbackSeparateComponents)
2473 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002474 infoLog << "Transform feedback varying's " << varying->name << " components ("
2475 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002476 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 return false;
2478 }
2479
2480 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002481 found = true;
2482 break;
2483 }
2484 }
jchen10a9042d32017-03-17 08:50:45 +08002485 if (context->getClientVersion() < Version(3, 1) &&
2486 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002487 {
Geoff Lang1a683462015-09-29 15:09:59 -04002488 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002489 return false;
2490 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002491 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2492 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002493 ASSERT(found);
2494 }
2495
Jamie Madill48ef11b2016-04-27 15:21:52 -04002496 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002497 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002498 {
Jamie Madillf6113162015-05-07 11:49:21 -04002499 infoLog << "Transform feedback varying total components (" << totalComponents
2500 << ") exceed the maximum interleaved components ("
2501 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002502 return false;
2503 }
2504
2505 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002506}
2507
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002508bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2509{
2510 const std::vector<sh::Uniform> &vertexUniforms =
2511 mState.mAttachedVertexShader->getUniforms(context);
2512 const std::vector<sh::Uniform> &fragmentUniforms =
2513 mState.mAttachedFragmentShader->getUniforms(context);
2514 const std::vector<sh::Attribute> &attributes =
2515 mState.mAttachedVertexShader->getActiveAttributes(context);
2516 for (const auto &attrib : attributes)
2517 {
2518 for (const auto &uniform : vertexUniforms)
2519 {
2520 if (uniform.name == attrib.name)
2521 {
2522 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2523 return false;
2524 }
2525 }
2526 for (const auto &uniform : fragmentUniforms)
2527 {
2528 if (uniform.name == attrib.name)
2529 {
2530 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2531 return false;
2532 }
2533 }
2534 }
2535 return true;
2536}
2537
Jamie Madill192745a2016-12-22 15:58:21 -05002538void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002539{
2540 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002541 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002542 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002543 {
jchen10a9042d32017-03-17 08:50:45 +08002544 size_t subscript = GL_INVALID_INDEX;
2545 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002546 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002547 {
Jamie Madill192745a2016-12-22 15:58:21 -05002548 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002549 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002550 {
jchen10a9042d32017-03-17 08:50:45 +08002551 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2552 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002553 break;
2554 }
2555 }
2556 }
2557}
2558
Jamie Madillbd044ed2017-06-05 12:59:21 -04002559Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002560{
Jamie Madill192745a2016-12-22 15:58:21 -05002561 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002562
Jamie Madillbd044ed2017-06-05 12:59:21 -04002563 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002564 {
Jamie Madill192745a2016-12-22 15:58:21 -05002565 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002566 }
2567
Jamie Madillbd044ed2017-06-05 12:59:21 -04002568 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002569 {
Jamie Madill192745a2016-12-22 15:58:21 -05002570 merged[varying.name].fragment = &varying;
2571 }
2572
2573 return merged;
2574}
2575
2576std::vector<PackedVarying> Program::getPackedVaryings(
2577 const Program::MergedVaryings &mergedVaryings) const
2578{
2579 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2580 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002581 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002582
2583 for (const auto &ref : mergedVaryings)
2584 {
2585 const sh::Varying *input = ref.second.vertex;
2586 const sh::Varying *output = ref.second.fragment;
2587
2588 // Only pack varyings that have a matched input or output, plus special builtins.
2589 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002590 {
Jamie Madill192745a2016-12-22 15:58:21 -05002591 // Will get the vertex shader interpolation by default.
2592 auto interpolation = ref.second.get()->interpolation;
2593
Olli Etuaho06a06f52017-07-12 12:22:15 +03002594 // Note that we lose the vertex shader static use information here. The data for the
2595 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002596 if (output->isStruct())
2597 {
2598 ASSERT(!output->isArray());
2599 for (const auto &field : output->fields)
2600 {
2601 ASSERT(!field.isStruct() && !field.isArray());
2602 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2603 }
2604 }
2605 else
2606 {
2607 packedVaryings.push_back(PackedVarying(*output, interpolation));
2608 }
2609 continue;
2610 }
2611
2612 // Keep Transform FB varyings in the merged list always.
2613 if (!input)
2614 {
2615 continue;
2616 }
2617
2618 for (const std::string &tfVarying : tfVaryings)
2619 {
jchen10a9042d32017-03-17 08:50:45 +08002620 size_t subscript = GL_INVALID_INDEX;
2621 std::string baseName = ParseResourceName(tfVarying, &subscript);
2622 if (uniqueFullNames.count(tfVarying) > 0)
2623 {
2624 continue;
2625 }
2626 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002627 {
2628 // Transform feedback for varying structs is underspecified.
2629 // See Khronos bug 9856.
2630 // TODO(jmadill): Figure out how to be spec-compliant here.
2631 if (!input->isStruct())
2632 {
2633 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2634 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002635 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2636 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002637 }
jchen10a9042d32017-03-17 08:50:45 +08002638 if (subscript == GL_INVALID_INDEX)
2639 {
2640 break;
2641 }
Jamie Madill192745a2016-12-22 15:58:21 -05002642 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002643 }
2644 }
2645
Jamie Madill192745a2016-12-22 15:58:21 -05002646 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2647
2648 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002649}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002650
Jamie Madillbd044ed2017-06-05 12:59:21 -04002651void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002652{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002653 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002654 ASSERT(fragmentShader != nullptr);
2655
Geoff Lange0cff192017-05-30 13:04:56 -04002656 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002657 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002658
2659 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002660 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002661 {
2662 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2663 outputVariable.name != "gl_FragData")
2664 {
2665 continue;
2666 }
2667
2668 unsigned int baseLocation =
2669 (outputVariable.location == -1 ? 0u
2670 : static_cast<unsigned int>(outputVariable.location));
2671 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2672 elementIndex++)
2673 {
2674 const unsigned int location = baseLocation + elementIndex;
2675 if (location >= mState.mOutputVariableTypes.size())
2676 {
2677 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2678 }
Corentin Walleze7557742017-06-01 13:09:57 -04002679 ASSERT(location < mState.mActiveOutputVariables.size());
2680 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002681 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2682 }
2683 }
2684
Jamie Madill80a6fc02015-08-21 16:53:16 -04002685 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002686 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002687 return;
2688
Jamie Madillbd044ed2017-06-05 12:59:21 -04002689 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002690 // TODO(jmadill): any caps validation here?
2691
jchen1015015f72017-03-16 13:54:21 +08002692 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002693 outputVariableIndex++)
2694 {
jchen1015015f72017-03-16 13:54:21 +08002695 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002696
2697 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2698 if (outputVariable.isBuiltIn())
2699 continue;
2700
2701 // Since multiple output locations must be specified, use 0 for non-specified locations.
2702 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2703
Jamie Madill80a6fc02015-08-21 16:53:16 -04002704 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2705 elementIndex++)
2706 {
2707 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002708 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002709 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002710 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002711 VariableLocation(outputVariable.name, element, outputVariableIndex);
2712 }
2713 }
2714}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002715
Olli Etuaho48fed632017-03-16 12:05:30 +00002716void Program::setUniformValuesFromBindingQualifiers()
2717{
Jamie Madill982f6e02017-06-07 14:33:04 -04002718 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002719 {
2720 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2721 if (samplerUniform.binding != -1)
2722 {
2723 GLint location = mState.getUniformLocation(samplerUniform.name);
2724 ASSERT(location != -1);
2725 std::vector<GLint> boundTextureUnits;
2726 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2727 ++elementIndex)
2728 {
2729 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2730 }
2731 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2732 boundTextureUnits.data());
2733 }
2734 }
2735}
2736
jchen10eaef1e52017-06-13 10:44:11 +08002737void Program::gatherAtomicCounterBuffers()
2738{
2739 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2740 // counter.
2741 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2742}
2743
Jamie Madillbd044ed2017-06-05 12:59:21 -04002744void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002745{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002746 ASSERT(mState.mUniformBlocks.empty());
2747
2748 if (mState.mAttachedComputeShader)
2749 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002750 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002751
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002752 for (const sh::InterfaceBlock &computeBlock : computeShader->getUniformBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002753 {
2754
2755 // Only 'packed' blocks are allowed to be considered inactive.
2756 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2757 continue;
2758
Jamie Madilla2c74982016-12-12 11:20:42 -05002759 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002760 {
2761 if (block.name == computeBlock.name)
2762 {
2763 block.computeStaticUse = computeBlock.staticUse;
2764 }
2765 }
2766
2767 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2768 }
2769 return;
2770 }
2771
Jamie Madill62d31cb2015-09-11 13:25:51 -04002772 std::set<std::string> visitedList;
2773
Jamie Madillbd044ed2017-06-05 12:59:21 -04002774 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002775
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002776 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002777 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002778 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002779 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2780 continue;
2781
2782 if (visitedList.count(vertexBlock.name) > 0)
2783 continue;
2784
2785 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2786 visitedList.insert(vertexBlock.name);
2787 }
2788
Jamie Madillbd044ed2017-06-05 12:59:21 -04002789 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002790
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002791 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002792 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002793 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002794 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2795 continue;
2796
2797 if (visitedList.count(fragmentBlock.name) > 0)
2798 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002799 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002800 {
2801 if (block.name == fragmentBlock.name)
2802 {
2803 block.fragmentStaticUse = fragmentBlock.staticUse;
2804 }
2805 }
2806
2807 continue;
2808 }
2809
2810 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2811 visitedList.insert(fragmentBlock.name);
2812 }
jchen10af713a22017-04-19 09:10:56 +08002813 // Set initial bindings from shader.
2814 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2815 {
2816 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2817 bindUniformBlock(blockIndex, uniformBlock.binding);
2818 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819}
2820
Jamie Madill4a3c2342015-10-08 12:58:45 -04002821template <typename VarT>
2822void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2823 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002824 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002825 int blockIndex)
2826{
2827 for (const VarT &field : fields)
2828 {
2829 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2830
Olli Etuaho855d9642017-05-17 14:05:06 +03002831 const std::string &fullMappedName =
2832 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2833
Jamie Madill4a3c2342015-10-08 12:58:45 -04002834 if (field.isStruct())
2835 {
2836 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2837 {
2838 const std::string uniformElementName =
2839 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002840 const std::string uniformElementMappedName =
2841 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2842 defineUniformBlockMembers(field.fields, uniformElementName,
2843 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002844 }
2845 }
2846 else
2847 {
2848 // If getBlockMemberInfo returns false, the uniform is optimized out.
2849 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002850 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002851 {
2852 continue;
2853 }
2854
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002855 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002856 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002857 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002858
2859 // Since block uniforms have no location, we don't need to store them in the uniform
2860 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002861 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002862 }
2863 }
2864}
2865
Jamie Madill62d31cb2015-09-11 13:25:51 -04002866void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2867{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002868 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002869 size_t blockSize = 0;
2870
Jamie Madill4a3c2342015-10-08 12:58:45 -04002871 // Track the first and last uniform index to determine the range of active uniforms in the
2872 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002873 size_t firstBlockUniformIndex = mState.mUniforms.size();
Olli Etuaho855d9642017-05-17 14:05:06 +03002874 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2875 interfaceBlock.fieldMappedPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002876 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002877
2878 std::vector<unsigned int> blockUniformIndexes;
2879 for (size_t blockUniformIndex = firstBlockUniformIndex;
2880 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2881 {
2882 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2883 }
jchen10af713a22017-04-19 09:10:56 +08002884 // ESSL 3.10 section 4.4.4 page 58:
2885 // Any uniform or shader storage block declared without a binding qualifier is initially
2886 // assigned to block binding point zero.
2887 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002888 if (interfaceBlock.arraySize > 0)
2889 {
2890 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2891 {
jchen10af713a22017-04-19 09:10:56 +08002892 // Don't define this block at all if it's not active in the implementation.
Olli Etuaho855d9642017-05-17 14:05:06 +03002893 if (!mProgram->getUniformBlockSize(
2894 interfaceBlock.name + ArrayString(arrayElement),
2895 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
jchen10af713a22017-04-19 09:10:56 +08002896 {
2897 continue;
2898 }
Olli Etuaho855d9642017-05-17 14:05:06 +03002899 UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
jchen10af713a22017-04-19 09:10:56 +08002900 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002901 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002902
Martin Radev4c4c8e72016-08-04 12:25:34 +03002903 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002904 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002905 case GL_VERTEX_SHADER:
2906 {
2907 block.vertexStaticUse = interfaceBlock.staticUse;
2908 break;
2909 }
2910 case GL_FRAGMENT_SHADER:
2911 {
2912 block.fragmentStaticUse = interfaceBlock.staticUse;
2913 break;
2914 }
2915 case GL_COMPUTE_SHADER:
2916 {
2917 block.computeStaticUse = interfaceBlock.staticUse;
2918 break;
2919 }
2920 default:
2921 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002922 }
2923
Qin Jiajia0350a642016-11-01 17:01:51 +08002924 // Since all block elements in an array share the same active uniforms, they will all be
2925 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2926 // here we will add every block element in the array.
2927 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002928 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002929 }
2930 }
2931 else
2932 {
Olli Etuaho855d9642017-05-17 14:05:06 +03002933 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
2934 &blockSize))
jchen10af713a22017-04-19 09:10:56 +08002935 {
2936 return;
2937 }
Olli Etuaho855d9642017-05-17 14:05:06 +03002938 UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002939 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002940
Martin Radev4c4c8e72016-08-04 12:25:34 +03002941 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002942 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002943 case GL_VERTEX_SHADER:
2944 {
2945 block.vertexStaticUse = interfaceBlock.staticUse;
2946 break;
2947 }
2948 case GL_FRAGMENT_SHADER:
2949 {
2950 block.fragmentStaticUse = interfaceBlock.staticUse;
2951 break;
2952 }
2953 case GL_COMPUTE_SHADER:
2954 {
2955 block.computeStaticUse = interfaceBlock.staticUse;
2956 break;
2957 }
2958 default:
2959 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960 }
2961
Jamie Madill4a3c2342015-10-08 12:58:45 -04002962 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002963 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002964 }
2965}
2966
Jamie Madille7d84322017-01-10 18:21:59 -05002967void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002968 GLsizei clampedCount,
2969 const GLint *v)
2970{
2971 // Invalidate the validation cache only if we modify the sampler data.
Jamie Madill81c2e252017-09-09 23:32:46 -04002972 ASSERT(mState.isSamplerUniformIndex(locationInfo.index));
2973 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2974 std::vector<GLuint> *boundTextureUnits =
2975 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
Jamie Madille7d84322017-01-10 18:21:59 -05002976
Jamie Madill81c2e252017-09-09 23:32:46 -04002977 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2978 mCachedValidateSamplersResult.reset();
Jamie Madille7d84322017-01-10 18:21:59 -05002979}
2980
2981template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002982GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2983 GLsizei count,
2984 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002985 const T *v)
2986{
Jamie Madill134f93d2017-08-31 17:11:00 -04002987 if (count == 1)
2988 return 1;
2989
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002990 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002991
Corentin Wallez15ac5342016-11-03 17:06:39 -04002992 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2993 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002994 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002995 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002996 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002997
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002998 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002999 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003000 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003001 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003002
3003 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003004}
3005
3006template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003007GLsizei Program::clampMatrixUniformCount(GLint location,
3008 GLsizei count,
3009 GLboolean transpose,
3010 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003011{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003012 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3013
Jamie Madill62d31cb2015-09-11 13:25:51 -04003014 if (!transpose)
3015 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003016 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003017 }
3018
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003019 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003020
3021 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3022 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003023 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
3024 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003025}
3026
Jamie Madill54164b02017-08-28 15:17:37 -04003027// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3028// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003029template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003030void Program::getUniformInternal(const Context *context,
3031 DestT *dataOut,
3032 GLint location,
3033 GLenum nativeType,
3034 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003035{
Jamie Madill54164b02017-08-28 15:17:37 -04003036 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003037 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003038 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003039 {
3040 GLint tempValue[16] = {0};
3041 mProgram->getUniformiv(context, location, tempValue);
3042 UniformStateQueryCastLoop<GLboolean>(
3043 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003044 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003045 }
3046 case GL_INT:
3047 {
3048 GLint tempValue[16] = {0};
3049 mProgram->getUniformiv(context, location, tempValue);
3050 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3051 components);
3052 break;
3053 }
3054 case GL_UNSIGNED_INT:
3055 {
3056 GLuint tempValue[16] = {0};
3057 mProgram->getUniformuiv(context, location, tempValue);
3058 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3059 components);
3060 break;
3061 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003062 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003063 {
3064 GLfloat tempValue[16] = {0};
3065 mProgram->getUniformfv(context, location, tempValue);
3066 UniformStateQueryCastLoop<GLfloat>(
3067 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003068 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003069 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003070 default:
3071 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003072 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003073 }
3074}
Jamie Madilla4595b82017-01-11 17:36:34 -05003075
3076bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3077{
3078 // Must be called after samplers are validated.
3079 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3080
3081 for (const auto &binding : mState.mSamplerBindings)
3082 {
3083 GLenum textureType = binding.textureType;
3084 for (const auto &unit : binding.boundTextureUnits)
3085 {
3086 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3087 if (programTextureID == textureID)
3088 {
3089 // TODO(jmadill): Check for appropriate overlap.
3090 return true;
3091 }
3092 }
3093 }
3094
3095 return false;
3096}
3097
Jamie Madilla2c74982016-12-12 11:20:42 -05003098} // namespace gl