blob: 0c5ad19340c9b380ffbffe49742889a402d73956 [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
1363void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1364{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001365 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1366 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
1367
1368 if (mState.isSamplerUniformIndex(locationInfo.index))
1369 {
1370 updateSamplerUniform(locationInfo, clampedCount, v);
1371 }
1372
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001373 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
1376void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1377{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001378 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1379 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001380 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381}
1382
1383void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1384{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001385 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1386 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001387 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388}
1389
1390void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1391{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001392 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1393 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001394 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395}
1396
1397void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1398{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001399 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1400 GLsizei clampedCount = clampUniformCount(locationInfo, count, 1, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001401 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402}
1403
1404void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1405{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001406 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1407 GLsizei clampedCount = clampUniformCount(locationInfo, count, 2, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001408 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
1411void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1412{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001413 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1414 GLsizei clampedCount = clampUniformCount(locationInfo, count, 3, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001415 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416}
1417
1418void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1419{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001420 const VariableLocation &locationInfo = mState.mUniformLocations[location];
1421 GLsizei clampedCount = clampUniformCount(locationInfo, count, 4, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001422 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423}
1424
1425void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1426{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001427 GLsizei clampedCount = clampMatrixUniformCount<2, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001428 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429}
1430
1431void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1432{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001433 GLsizei clampedCount = clampMatrixUniformCount<3, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001434 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435}
1436
1437void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1438{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001439 GLsizei clampedCount = clampMatrixUniformCount<4, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001440 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001441}
1442
1443void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1444{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001445 GLsizei clampedCount = clampMatrixUniformCount<2, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001446 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447}
1448
1449void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1450{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001451 GLsizei clampedCount = clampMatrixUniformCount<2, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001452 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001453}
1454
1455void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1456{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001457 GLsizei clampedCount = clampMatrixUniformCount<3, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001458 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459}
1460
1461void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1462{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001463 GLsizei clampedCount = clampMatrixUniformCount<3, 4>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001464 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465}
1466
1467void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1468{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001469 GLsizei clampedCount = clampMatrixUniformCount<4, 2>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001470 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471}
1472
1473void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1474{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04001475 GLsizei clampedCount = clampMatrixUniformCount<4, 3>(location, count, transpose, v);
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001476 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477}
1478
Jamie Madill54164b02017-08-28 15:17:37 -04001479void Program::getUniformfv(const Context *context, GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480{
Jamie Madill54164b02017-08-28 15:17:37 -04001481 const auto &uniformLocation = mState.getUniformLocations()[location];
1482 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1483
1484 GLenum nativeType = gl::VariableComponentType(uniform.type);
1485 if (nativeType == GL_FLOAT)
1486 {
1487 mProgram->getUniformfv(context, location, v);
1488 }
1489 else
1490 {
1491 getUniformInternal(context, v, location, nativeType,
1492 gl::VariableComponentCount(uniform.type));
1493 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494}
1495
Jamie Madill54164b02017-08-28 15:17:37 -04001496void Program::getUniformiv(const Context *context, GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497{
Jamie Madill54164b02017-08-28 15:17:37 -04001498 const auto &uniformLocation = mState.getUniformLocations()[location];
1499 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1500
1501 GLenum nativeType = gl::VariableComponentType(uniform.type);
1502 if (nativeType == GL_INT || nativeType == GL_BOOL)
1503 {
1504 mProgram->getUniformiv(context, location, v);
1505 }
1506 else
1507 {
1508 getUniformInternal(context, v, location, nativeType,
1509 gl::VariableComponentCount(uniform.type));
1510 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511}
1512
Jamie Madill54164b02017-08-28 15:17:37 -04001513void Program::getUniformuiv(const Context *context, GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001514{
Jamie Madill54164b02017-08-28 15:17:37 -04001515 const auto &uniformLocation = mState.getUniformLocations()[location];
1516 const auto &uniform = mState.getUniforms()[uniformLocation.index];
1517
1518 GLenum nativeType = gl::VariableComponentType(uniform.type);
1519 if (nativeType == GL_UNSIGNED_INT)
1520 {
1521 mProgram->getUniformuiv(context, location, v);
1522 }
1523 else
1524 {
1525 getUniformInternal(context, v, location, nativeType,
1526 gl::VariableComponentCount(uniform.type));
1527 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528}
1529
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530void Program::flagForDeletion()
1531{
1532 mDeleteStatus = true;
1533}
1534
1535bool Program::isFlaggedForDeletion() const
1536{
1537 return mDeleteStatus;
1538}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001539
Brandon Jones43a53e22014-08-28 16:23:22 -07001540void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001541{
1542 mInfoLog.reset();
1543
Geoff Lang7dd2e102014-11-10 15:19:26 -05001544 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001545 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001546 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001547 }
1548 else
1549 {
Jamie Madillf6113162015-05-07 11:49:21 -04001550 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001551 }
1552}
1553
Geoff Lang7dd2e102014-11-10 15:19:26 -05001554bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1555{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001556 // Skip cache if we're using an infolog, so we get the full error.
1557 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1558 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1559 {
1560 return mCachedValidateSamplersResult.value();
1561 }
1562
1563 if (mTextureUnitTypesCache.empty())
1564 {
1565 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1566 }
1567 else
1568 {
1569 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1570 }
1571
1572 // if any two active samplers in a program are of different types, but refer to the same
1573 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1574 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001575 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001576 {
Jamie Madill54164b02017-08-28 15:17:37 -04001577 if (samplerBinding.unreferenced)
1578 continue;
1579
Jamie Madille7d84322017-01-10 18:21:59 -05001580 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001581
Jamie Madille7d84322017-01-10 18:21:59 -05001582 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001583 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001584 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1585 {
1586 if (infoLog)
1587 {
1588 (*infoLog) << "Sampler uniform (" << textureUnit
1589 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1590 << caps.maxCombinedTextureImageUnits << ")";
1591 }
1592
1593 mCachedValidateSamplersResult = false;
1594 return false;
1595 }
1596
1597 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1598 {
1599 if (textureType != mTextureUnitTypesCache[textureUnit])
1600 {
1601 if (infoLog)
1602 {
1603 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1604 "image unit ("
1605 << textureUnit << ").";
1606 }
1607
1608 mCachedValidateSamplersResult = false;
1609 return false;
1610 }
1611 }
1612 else
1613 {
1614 mTextureUnitTypesCache[textureUnit] = textureType;
1615 }
1616 }
1617 }
1618
1619 mCachedValidateSamplersResult = true;
1620 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621}
1622
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001623bool Program::isValidated() const
1624{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 return mValidated;
1626}
1627
Geoff Lange1a27752015-10-05 13:16:04 -04001628GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001630 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631}
1632
1633void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1634{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001635 ASSERT(
1636 uniformBlockIndex <
1637 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638
Jamie Madill48ef11b2016-04-27 15:21:52 -04001639 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640
1641 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001642 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643 std::string string = uniformBlock.name;
1644
Jamie Madill62d31cb2015-09-11 13:25:51 -04001645 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001647 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648 }
jchen10fd7c3b52017-03-21 15:36:03 +08001649 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001650 }
1651}
1652
Geoff Lange1a27752015-10-05 13:16:04 -04001653GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001654{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655 int maxLength = 0;
1656
1657 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001658 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001659 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001660 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1661 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001662 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663 if (!uniformBlock.name.empty())
1664 {
jchen10af713a22017-04-19 09:10:56 +08001665 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1666 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 }
1668 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001669 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670
1671 return maxLength;
1672}
1673
Geoff Lange1a27752015-10-05 13:16:04 -04001674GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001676 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001677 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001678
Jamie Madill48ef11b2016-04-27 15:21:52 -04001679 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001680 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1681 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001682 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 if (uniformBlock.name == baseName)
1684 {
1685 const bool arrayElementZero =
1686 (subscript == GL_INVALID_INDEX &&
1687 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1688 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1689 {
1690 return blockIndex;
1691 }
1692 }
1693 }
1694
1695 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001696}
1697
Jamie Madill62d31cb2015-09-11 13:25:51 -04001698const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001699{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001700 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1701 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001702}
1703
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001704void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1705{
jchen107a20b972017-06-13 14:25:26 +08001706 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001707 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001708 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001709}
1710
1711GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1712{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001713 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001714}
1715
Geoff Lang48dcae72014-02-05 16:28:24 -05001716void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1717{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001718 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001719 for (GLsizei i = 0; i < count; i++)
1720 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001722 }
1723
Jamie Madill48ef11b2016-04-27 15:21:52 -04001724 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001725}
1726
1727void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1728{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001729 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001730 {
jchen10a9042d32017-03-17 08:50:45 +08001731 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1732 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1733 std::string varName = var.nameWithArrayIndex();
1734 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001735 if (length)
1736 {
1737 *length = lastNameIdx;
1738 }
1739 if (size)
1740 {
jchen10a9042d32017-03-17 08:50:45 +08001741 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001742 }
1743 if (type)
1744 {
jchen10a9042d32017-03-17 08:50:45 +08001745 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001746 }
1747 if (name)
1748 {
jchen10a9042d32017-03-17 08:50:45 +08001749 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001750 name[lastNameIdx] = '\0';
1751 }
1752 }
1753}
1754
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001755GLsizei Program::getTransformFeedbackVaryingCount() const
1756{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001757 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001758 {
jchen10a9042d32017-03-17 08:50:45 +08001759 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001760 }
1761 else
1762 {
1763 return 0;
1764 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001765}
1766
1767GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1768{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001769 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001770 {
1771 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001772 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001773 {
jchen10a9042d32017-03-17 08:50:45 +08001774 maxSize =
1775 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001776 }
1777
1778 return maxSize;
1779 }
1780 else
1781 {
1782 return 0;
1783 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001784}
1785
1786GLenum Program::getTransformFeedbackBufferMode() const
1787{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001788 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789}
1790
Jamie Madillbd044ed2017-06-05 12:59:21 -04001791bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001793 Shader *vertexShader = mState.mAttachedVertexShader;
1794 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001795
Jamie Madillbd044ed2017-06-05 12:59:21 -04001796 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001797
Jamie Madillbd044ed2017-06-05 12:59:21 -04001798 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1799 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001800
Sami Väisänen46eaa942016-06-29 10:26:37 +03001801 std::map<GLuint, std::string> staticFragmentInputLocations;
1802
Jamie Madill4cff2472015-08-21 16:53:18 -04001803 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805 bool matched = false;
1806
1807 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001808 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809 {
1810 continue;
1811 }
1812
Jamie Madill4cff2472015-08-21 16:53:18 -04001813 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001815 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001817 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001818 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001819 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820 {
1821 return false;
1822 }
1823
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824 matched = true;
1825 break;
1826 }
1827 }
1828
1829 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001830 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001832 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 return false;
1834 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001835
1836 // Check for aliased path rendering input bindings (if any).
1837 // If more than one binding refer statically to the same
1838 // location the link must fail.
1839
1840 if (!output.staticUse)
1841 continue;
1842
1843 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1844 if (inputBinding == -1)
1845 continue;
1846
1847 const auto it = staticFragmentInputLocations.find(inputBinding);
1848 if (it == std::end(staticFragmentInputLocations))
1849 {
1850 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1851 }
1852 else
1853 {
1854 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1855 << it->second;
1856 return false;
1857 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001858 }
1859
Jamie Madillbd044ed2017-06-05 12:59:21 -04001860 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001861 {
1862 return false;
1863 }
1864
Jamie Madillada9ecc2015-08-17 12:53:37 -04001865 // TODO(jmadill): verify no unmatched vertex varyings?
1866
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867 return true;
1868}
1869
Jamie Madillbd044ed2017-06-05 12:59:21 -04001870bool Program::linkUniforms(const Context *context,
1871 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001872 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001873{
Olli Etuahob78707c2017-03-09 15:03:11 +00001874 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001875 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001876 {
1877 return false;
1878 }
1879
Olli Etuahob78707c2017-03-09 15:03:11 +00001880 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001881
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001882 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001883
jchen10eaef1e52017-06-13 10:44:11 +08001884 if (!linkAtomicCounterBuffers())
1885 {
1886 return false;
1887 }
1888
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001889 return true;
1890}
1891
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001892void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001893{
Jamie Madill982f6e02017-06-07 14:33:04 -04001894 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1895 unsigned int low = high;
1896
jchen10eaef1e52017-06-13 10:44:11 +08001897 for (auto counterIter = mState.mUniforms.rbegin();
1898 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1899 {
1900 --low;
1901 }
1902
1903 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1904
1905 high = low;
1906
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001907 for (auto imageIter = mState.mUniforms.rbegin();
1908 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1909 {
1910 --low;
1911 }
1912
1913 mState.mImageUniformRange = RangeUI(low, high);
1914
1915 // If uniform is a image type, insert it into the mImageBindings array.
1916 for (unsigned int imageIndex : mState.mImageUniformRange)
1917 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001918 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1919 // cannot load values into a uniform defined as an image. if declare without a
1920 // binding qualifier, any uniform image variable (include all elements of
1921 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001922 auto &imageUniform = mState.mUniforms[imageIndex];
1923 if (imageUniform.binding == -1)
1924 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001925 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001926 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001927 else
1928 {
1929 mState.mImageBindings.emplace_back(
1930 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1931 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001932 }
1933
1934 high = low;
1935
1936 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001937 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001938 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001939 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001940 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001941
1942 mState.mSamplerUniformRange = RangeUI(low, high);
1943
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001944 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001945 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001946 {
1947 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1948 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1949 mState.mSamplerBindings.emplace_back(
Jamie Madill54164b02017-08-28 15:17:37 -04001950 SamplerBinding(textureType, samplerUniform.elementCount(), false));
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001951 }
1952}
1953
jchen10eaef1e52017-06-13 10:44:11 +08001954bool Program::linkAtomicCounterBuffers()
1955{
1956 for (unsigned int index : mState.mAtomicCounterUniformRange)
1957 {
1958 auto &uniform = mState.mUniforms[index];
1959 bool found = false;
1960 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1961 ++bufferIndex)
1962 {
1963 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1964 if (buffer.binding == uniform.binding)
1965 {
1966 buffer.memberIndexes.push_back(index);
1967 uniform.bufferIndex = bufferIndex;
1968 found = true;
1969 break;
1970 }
1971 }
1972 if (!found)
1973 {
1974 AtomicCounterBuffer atomicCounterBuffer;
1975 atomicCounterBuffer.binding = uniform.binding;
1976 atomicCounterBuffer.memberIndexes.push_back(index);
1977 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1978 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1979 }
1980 }
1981 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1982 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1983
1984 return true;
1985}
1986
Martin Radev4c4c8e72016-08-04 12:25:34 +03001987bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1988 const std::string &uniformName,
1989 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001990 const sh::InterfaceBlockField &fragmentUniform,
1991 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001993 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1994 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1995 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001996 {
1997 return false;
1998 }
1999
2000 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2001 {
Jamie Madillf6113162015-05-07 11:49:21 -04002002 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 return false;
2004 }
2005
2006 return true;
2007}
2008
Jamie Madilleb979bf2016-11-15 12:28:46 -05002009// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002010bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002012 const ContextState &data = context->getContextState();
2013 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05002014
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04002016 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002017 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002018
2019 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002020 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002021 {
Jamie Madillf6113162015-05-07 11:49:21 -04002022 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002023 return false;
2024 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002025
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002026 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002027
Jamie Madillc349ec02015-08-21 16:53:12 -04002028 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002029 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002030 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002031 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002032 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002033 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002034 attribute.location = bindingLocation;
2035 }
2036
2037 if (attribute.location != -1)
2038 {
2039 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002040 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041
Jamie Madill63805b42015-08-25 13:17:39 -04002042 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
Jamie Madillf6113162015-05-07 11:49:21 -04002044 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002045 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046
2047 return false;
2048 }
2049
Jamie Madill63805b42015-08-25 13:17:39 -04002050 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051 {
Jamie Madill63805b42015-08-25 13:17:39 -04002052 const int regLocation = attribute.location + reg;
2053 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054
2055 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002056 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002057 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002059 // TODO(jmadill): fix aliasing on ES2
2060 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002062 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002063 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 return false;
2065 }
2066 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002067 else
2068 {
Jamie Madill63805b42015-08-25 13:17:39 -04002069 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071
Jamie Madill63805b42015-08-25 13:17:39 -04002072 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073 }
2074 }
2075 }
2076
2077 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002078 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002080 // Not set by glBindAttribLocation or by location layout qualifier
2081 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082 {
Jamie Madill63805b42015-08-25 13:17:39 -04002083 int regs = VariableRegisterCount(attribute.type);
2084 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085
Jamie Madill63805b42015-08-25 13:17:39 -04002086 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087 {
Jamie Madillf6113162015-05-07 11:49:21 -04002088 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002089 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002090 }
2091
Jamie Madillc349ec02015-08-21 16:53:12 -04002092 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093 }
2094 }
2095
Jamie Madill48ef11b2016-04-27 15:21:52 -04002096 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002097 {
Jamie Madill63805b42015-08-25 13:17:39 -04002098 ASSERT(attribute.location != -1);
2099 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002100
Jamie Madill63805b42015-08-25 13:17:39 -04002101 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002103 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 }
2105 }
2106
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 return true;
2108}
2109
Martin Radev4c4c8e72016-08-04 12:25:34 +03002110bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2111 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2112 const std::string &errorMessage,
2113 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002115 GLuint blockCount = 0;
2116 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002118 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002119 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002120 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002121 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002122 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002123 return false;
2124 }
2125 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002127 return true;
2128}
Jamie Madille473dee2015-08-18 14:49:01 -04002129
Martin Radev4c4c8e72016-08-04 12:25:34 +03002130bool Program::validateVertexAndFragmentInterfaceBlocks(
2131 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2132 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002133 InfoLog &infoLog,
2134 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002135{
2136 // Check that interface blocks defined in the vertex and fragment shaders are identical
2137 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2138 UniformBlockMap linkedUniformBlocks;
2139
2140 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2141 {
2142 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2143 }
2144
Jamie Madille473dee2015-08-18 14:49:01 -04002145 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146 {
Jamie Madille473dee2015-08-18 14:49:01 -04002147 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 if (entry != linkedUniformBlocks.end())
2149 {
2150 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002151 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2152 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153 {
2154 return false;
2155 }
2156 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002157 }
2158 return true;
2159}
Jamie Madille473dee2015-08-18 14:49:01 -04002160
Jamie Madillbd044ed2017-06-05 12:59:21 -04002161bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002162{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002163 const auto &caps = context->getCaps();
2164
Martin Radev4c4c8e72016-08-04 12:25:34 +03002165 if (mState.mAttachedComputeShader)
2166 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002167 Shader &computeShader = *mState.mAttachedComputeShader;
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002168 const auto &computeInterfaceBlocks = computeShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002169
2170 if (!validateUniformBlocksCount(
2171 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2172 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2173 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002175 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002177 return true;
2178 }
2179
Jamie Madillbd044ed2017-06-05 12:59:21 -04002180 Shader &vertexShader = *mState.mAttachedVertexShader;
2181 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002182
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002183 const auto &vertexInterfaceBlocks = vertexShader.getUniformBlocks(context);
2184 const auto &fragmentInterfaceBlocks = fragmentShader.getUniformBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185
2186 if (!validateUniformBlocksCount(
2187 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2188 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2189 {
2190 return false;
2191 }
2192 if (!validateUniformBlocksCount(
2193 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2194 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2195 infoLog))
2196 {
2197
2198 return false;
2199 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002200
2201 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002202 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002203 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002204 {
2205 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002206 }
Jamie Madille473dee2015-08-18 14:49:01 -04002207
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 return true;
2209}
2210
Jamie Madilla2c74982016-12-12 11:20:42 -05002211bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002212 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002213 const sh::InterfaceBlock &fragmentInterfaceBlock,
2214 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002215{
2216 const char* blockName = vertexInterfaceBlock.name.c_str();
2217 // validate blocks for the same member types
2218 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2219 {
Jamie Madillf6113162015-05-07 11:49:21 -04002220 infoLog << "Types for interface block '" << blockName
2221 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002222 return false;
2223 }
2224 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2225 {
Jamie Madillf6113162015-05-07 11:49:21 -04002226 infoLog << "Array sizes differ for interface block '" << blockName
2227 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228 return false;
2229 }
jchen10af713a22017-04-19 09:10:56 +08002230 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2231 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2232 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233 {
Jamie Madillf6113162015-05-07 11:49:21 -04002234 infoLog << "Layout qualifiers differ for interface block '" << blockName
2235 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 return false;
2237 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002238 const unsigned int numBlockMembers =
2239 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002240 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2241 {
2242 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2243 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2244 if (vertexMember.name != fragmentMember.name)
2245 {
Jamie Madillf6113162015-05-07 11:49:21 -04002246 infoLog << "Name mismatch for field " << blockMemberIndex
2247 << " of interface block '" << blockName
2248 << "': (in vertex: '" << vertexMember.name
2249 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002250 return false;
2251 }
2252 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002253 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2254 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255 {
2256 return false;
2257 }
2258 }
2259 return true;
2260}
2261
2262bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2263 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2264{
2265 if (vertexVariable.type != fragmentVariable.type)
2266 {
Jamie Madillf6113162015-05-07 11:49:21 -04002267 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 return false;
2269 }
2270 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2271 {
Jamie Madillf6113162015-05-07 11:49:21 -04002272 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002273 return false;
2274 }
2275 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2276 {
Jamie Madillf6113162015-05-07 11:49:21 -04002277 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 return false;
2279 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002280 if (vertexVariable.structName != fragmentVariable.structName)
2281 {
2282 infoLog << "Structure names for " << variableName
2283 << " differ between vertex and fragment shaders";
2284 return false;
2285 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002286
2287 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2288 {
Jamie Madillf6113162015-05-07 11:49:21 -04002289 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002290 return false;
2291 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002292 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2294 {
2295 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2296 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2297
2298 if (vertexMember.name != fragmentMember.name)
2299 {
Jamie Madillf6113162015-05-07 11:49:21 -04002300 infoLog << "Name mismatch for field '" << memberIndex
2301 << "' of " << variableName
2302 << ": (in vertex: '" << vertexMember.name
2303 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304 return false;
2305 }
2306
2307 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2308 vertexMember.name + "'";
2309
2310 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2311 {
2312 return false;
2313 }
2314 }
2315
2316 return true;
2317}
2318
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002319bool Program::linkValidateVaryings(InfoLog &infoLog,
2320 const std::string &varyingName,
2321 const sh::Varying &vertexVarying,
2322 const sh::Varying &fragmentVarying,
2323 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002324{
2325 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2326 {
2327 return false;
2328 }
2329
Jamie Madille9cc4692015-02-19 16:00:13 -05002330 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002331 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002332 infoLog << "Interpolation types for " << varyingName
2333 << " differ between vertex and fragment shaders.";
2334 return false;
2335 }
2336
2337 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2338 {
2339 infoLog << "Invariance for " << varyingName
2340 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002341 return false;
2342 }
2343
2344 return true;
2345}
2346
Jamie Madillbd044ed2017-06-05 12:59:21 -04002347bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002348{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002349 Shader *vertexShader = mState.mAttachedVertexShader;
2350 Shader *fragmentShader = mState.mAttachedFragmentShader;
2351 const auto &vertexVaryings = vertexShader->getVaryings(context);
2352 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2353 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002354
2355 if (shaderVersion != 100)
2356 {
2357 // Only ESSL 1.0 has restrictions on matching input and output invariance
2358 return true;
2359 }
2360
2361 bool glPositionIsInvariant = false;
2362 bool glPointSizeIsInvariant = false;
2363 bool glFragCoordIsInvariant = false;
2364 bool glPointCoordIsInvariant = false;
2365
2366 for (const sh::Varying &varying : vertexVaryings)
2367 {
2368 if (!varying.isBuiltIn())
2369 {
2370 continue;
2371 }
2372 if (varying.name.compare("gl_Position") == 0)
2373 {
2374 glPositionIsInvariant = varying.isInvariant;
2375 }
2376 else if (varying.name.compare("gl_PointSize") == 0)
2377 {
2378 glPointSizeIsInvariant = varying.isInvariant;
2379 }
2380 }
2381
2382 for (const sh::Varying &varying : fragmentVaryings)
2383 {
2384 if (!varying.isBuiltIn())
2385 {
2386 continue;
2387 }
2388 if (varying.name.compare("gl_FragCoord") == 0)
2389 {
2390 glFragCoordIsInvariant = varying.isInvariant;
2391 }
2392 else if (varying.name.compare("gl_PointCoord") == 0)
2393 {
2394 glPointCoordIsInvariant = varying.isInvariant;
2395 }
2396 }
2397
2398 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2399 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2400 // Not requiring invariance to match is supported by:
2401 // dEQP, WebGL CTS, Nexus 5X GLES
2402 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2403 {
2404 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2405 "declared invariant.";
2406 return false;
2407 }
2408 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2409 {
2410 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2411 "declared invariant.";
2412 return false;
2413 }
2414
2415 return true;
2416}
2417
jchen10a9042d32017-03-17 08:50:45 +08002418bool Program::linkValidateTransformFeedback(const gl::Context *context,
2419 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002420 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002421 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002422{
2423 size_t totalComponents = 0;
2424
Jamie Madillccdf74b2015-08-18 10:46:12 -04002425 std::set<std::string> uniqueNames;
2426
Jamie Madill48ef11b2016-04-27 15:21:52 -04002427 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002428 {
2429 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002430 size_t subscript = GL_INVALID_INDEX;
2431 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2432
Jamie Madill192745a2016-12-22 15:58:21 -05002433 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002434 {
Jamie Madill192745a2016-12-22 15:58:21 -05002435 const sh::Varying *varying = ref.second.get();
2436
jchen10a9042d32017-03-17 08:50:45 +08002437 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002438 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002439 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002440 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002441 infoLog << "Two transform feedback varyings specify the same output variable ("
2442 << tfVaryingName << ").";
2443 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002444 }
jchen10a9042d32017-03-17 08:50:45 +08002445 if (context->getClientVersion() >= Version(3, 1))
2446 {
2447 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2448 {
2449 infoLog
2450 << "Two transform feedback varyings include the same array element ("
2451 << tfVaryingName << ").";
2452 return false;
2453 }
2454 }
2455 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002456 {
2457 infoLog << "Capture of arrays is undefined and not supported.";
2458 return false;
2459 }
2460
jchen10a9042d32017-03-17 08:50:45 +08002461 uniqueNames.insert(tfVaryingName);
2462
Jamie Madillccdf74b2015-08-18 10:46:12 -04002463 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002464 size_t elementCount =
2465 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2466 : 1);
2467 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002468 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002469 componentCount > caps.maxTransformFeedbackSeparateComponents)
2470 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002471 infoLog << "Transform feedback varying's " << varying->name << " components ("
2472 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002473 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002474 return false;
2475 }
2476
2477 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002478 found = true;
2479 break;
2480 }
2481 }
jchen10a9042d32017-03-17 08:50:45 +08002482 if (context->getClientVersion() < Version(3, 1) &&
2483 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002484 {
Geoff Lang1a683462015-09-29 15:09:59 -04002485 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002486 return false;
2487 }
Olli Etuaho39e78122017-08-29 14:34:22 +03002488 // All transform feedback varyings are expected to exist since packUserVaryings checks for
2489 // them.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 ASSERT(found);
2491 }
2492
Jamie Madill48ef11b2016-04-27 15:21:52 -04002493 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002494 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002495 {
Jamie Madillf6113162015-05-07 11:49:21 -04002496 infoLog << "Transform feedback varying total components (" << totalComponents
2497 << ") exceed the maximum interleaved components ("
2498 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002499 return false;
2500 }
2501
2502 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002503}
2504
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002505bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2506{
2507 const std::vector<sh::Uniform> &vertexUniforms =
2508 mState.mAttachedVertexShader->getUniforms(context);
2509 const std::vector<sh::Uniform> &fragmentUniforms =
2510 mState.mAttachedFragmentShader->getUniforms(context);
2511 const std::vector<sh::Attribute> &attributes =
2512 mState.mAttachedVertexShader->getActiveAttributes(context);
2513 for (const auto &attrib : attributes)
2514 {
2515 for (const auto &uniform : vertexUniforms)
2516 {
2517 if (uniform.name == attrib.name)
2518 {
2519 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2520 return false;
2521 }
2522 }
2523 for (const auto &uniform : fragmentUniforms)
2524 {
2525 if (uniform.name == attrib.name)
2526 {
2527 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2528 return false;
2529 }
2530 }
2531 }
2532 return true;
2533}
2534
Jamie Madill192745a2016-12-22 15:58:21 -05002535void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002536{
2537 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002538 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002539 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002540 {
jchen10a9042d32017-03-17 08:50:45 +08002541 size_t subscript = GL_INVALID_INDEX;
2542 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002543 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002544 {
Jamie Madill192745a2016-12-22 15:58:21 -05002545 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002546 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002547 {
jchen10a9042d32017-03-17 08:50:45 +08002548 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2549 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002550 break;
2551 }
2552 }
2553 }
2554}
2555
Jamie Madillbd044ed2017-06-05 12:59:21 -04002556Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002557{
Jamie Madill192745a2016-12-22 15:58:21 -05002558 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002559
Jamie Madillbd044ed2017-06-05 12:59:21 -04002560 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002561 {
Jamie Madill192745a2016-12-22 15:58:21 -05002562 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002563 }
2564
Jamie Madillbd044ed2017-06-05 12:59:21 -04002565 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002566 {
Jamie Madill192745a2016-12-22 15:58:21 -05002567 merged[varying.name].fragment = &varying;
2568 }
2569
2570 return merged;
2571}
2572
2573std::vector<PackedVarying> Program::getPackedVaryings(
2574 const Program::MergedVaryings &mergedVaryings) const
2575{
2576 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2577 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002578 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002579
2580 for (const auto &ref : mergedVaryings)
2581 {
2582 const sh::Varying *input = ref.second.vertex;
2583 const sh::Varying *output = ref.second.fragment;
2584
2585 // Only pack varyings that have a matched input or output, plus special builtins.
2586 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002587 {
Jamie Madill192745a2016-12-22 15:58:21 -05002588 // Will get the vertex shader interpolation by default.
2589 auto interpolation = ref.second.get()->interpolation;
2590
Olli Etuaho06a06f52017-07-12 12:22:15 +03002591 // Note that we lose the vertex shader static use information here. The data for the
2592 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002593 if (output->isStruct())
2594 {
2595 ASSERT(!output->isArray());
2596 for (const auto &field : output->fields)
2597 {
2598 ASSERT(!field.isStruct() && !field.isArray());
2599 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2600 }
2601 }
2602 else
2603 {
2604 packedVaryings.push_back(PackedVarying(*output, interpolation));
2605 }
2606 continue;
2607 }
2608
2609 // Keep Transform FB varyings in the merged list always.
2610 if (!input)
2611 {
2612 continue;
2613 }
2614
2615 for (const std::string &tfVarying : tfVaryings)
2616 {
jchen10a9042d32017-03-17 08:50:45 +08002617 size_t subscript = GL_INVALID_INDEX;
2618 std::string baseName = ParseResourceName(tfVarying, &subscript);
2619 if (uniqueFullNames.count(tfVarying) > 0)
2620 {
2621 continue;
2622 }
2623 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002624 {
2625 // Transform feedback for varying structs is underspecified.
2626 // See Khronos bug 9856.
2627 // TODO(jmadill): Figure out how to be spec-compliant here.
2628 if (!input->isStruct())
2629 {
2630 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2631 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002632 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2633 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002634 }
jchen10a9042d32017-03-17 08:50:45 +08002635 if (subscript == GL_INVALID_INDEX)
2636 {
2637 break;
2638 }
Jamie Madill192745a2016-12-22 15:58:21 -05002639 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002640 }
2641 }
2642
Jamie Madill192745a2016-12-22 15:58:21 -05002643 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2644
2645 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002646}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002647
Jamie Madillbd044ed2017-06-05 12:59:21 -04002648void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002649{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002650 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002651 ASSERT(fragmentShader != nullptr);
2652
Geoff Lange0cff192017-05-30 13:04:56 -04002653 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002654 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002655
2656 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002657 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002658 {
2659 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2660 outputVariable.name != "gl_FragData")
2661 {
2662 continue;
2663 }
2664
2665 unsigned int baseLocation =
2666 (outputVariable.location == -1 ? 0u
2667 : static_cast<unsigned int>(outputVariable.location));
2668 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2669 elementIndex++)
2670 {
2671 const unsigned int location = baseLocation + elementIndex;
2672 if (location >= mState.mOutputVariableTypes.size())
2673 {
2674 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2675 }
Corentin Walleze7557742017-06-01 13:09:57 -04002676 ASSERT(location < mState.mActiveOutputVariables.size());
2677 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002678 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2679 }
2680 }
2681
Jamie Madill80a6fc02015-08-21 16:53:16 -04002682 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002683 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002684 return;
2685
Jamie Madillbd044ed2017-06-05 12:59:21 -04002686 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002687 // TODO(jmadill): any caps validation here?
2688
jchen1015015f72017-03-16 13:54:21 +08002689 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002690 outputVariableIndex++)
2691 {
jchen1015015f72017-03-16 13:54:21 +08002692 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002693
2694 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2695 if (outputVariable.isBuiltIn())
2696 continue;
2697
2698 // Since multiple output locations must be specified, use 0 for non-specified locations.
2699 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2700
Jamie Madill80a6fc02015-08-21 16:53:16 -04002701 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2702 elementIndex++)
2703 {
2704 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002705 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002706 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002707 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002708 VariableLocation(outputVariable.name, element, outputVariableIndex);
2709 }
2710 }
2711}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002712
Olli Etuaho48fed632017-03-16 12:05:30 +00002713void Program::setUniformValuesFromBindingQualifiers()
2714{
Jamie Madill982f6e02017-06-07 14:33:04 -04002715 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002716 {
2717 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2718 if (samplerUniform.binding != -1)
2719 {
2720 GLint location = mState.getUniformLocation(samplerUniform.name);
2721 ASSERT(location != -1);
2722 std::vector<GLint> boundTextureUnits;
2723 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2724 ++elementIndex)
2725 {
2726 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2727 }
2728 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2729 boundTextureUnits.data());
2730 }
2731 }
2732}
2733
jchen10eaef1e52017-06-13 10:44:11 +08002734void Program::gatherAtomicCounterBuffers()
2735{
2736 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2737 // counter.
2738 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2739}
2740
Jamie Madillbd044ed2017-06-05 12:59:21 -04002741void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002742{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002743 ASSERT(mState.mUniformBlocks.empty());
2744
2745 if (mState.mAttachedComputeShader)
2746 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002747 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002748
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002749 for (const sh::InterfaceBlock &computeBlock : computeShader->getUniformBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002750 {
2751
2752 // Only 'packed' blocks are allowed to be considered inactive.
2753 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2754 continue;
2755
Jamie Madilla2c74982016-12-12 11:20:42 -05002756 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002757 {
2758 if (block.name == computeBlock.name)
2759 {
2760 block.computeStaticUse = computeBlock.staticUse;
2761 }
2762 }
2763
2764 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2765 }
2766 return;
2767 }
2768
Jamie Madill62d31cb2015-09-11 13:25:51 -04002769 std::set<std::string> visitedList;
2770
Jamie Madillbd044ed2017-06-05 12:59:21 -04002771 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002772
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002773 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002774 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002775 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002776 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2777 continue;
2778
2779 if (visitedList.count(vertexBlock.name) > 0)
2780 continue;
2781
2782 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2783 visitedList.insert(vertexBlock.name);
2784 }
2785
Jamie Madillbd044ed2017-06-05 12:59:21 -04002786 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002787
Jiajia Qin9b11ea42017-07-11 16:50:08 +08002788 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getUniformBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002789 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002790 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002791 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2792 continue;
2793
2794 if (visitedList.count(fragmentBlock.name) > 0)
2795 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002796 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002797 {
2798 if (block.name == fragmentBlock.name)
2799 {
2800 block.fragmentStaticUse = fragmentBlock.staticUse;
2801 }
2802 }
2803
2804 continue;
2805 }
2806
2807 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2808 visitedList.insert(fragmentBlock.name);
2809 }
jchen10af713a22017-04-19 09:10:56 +08002810 // Set initial bindings from shader.
2811 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2812 {
2813 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2814 bindUniformBlock(blockIndex, uniformBlock.binding);
2815 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002816}
2817
Jamie Madill4a3c2342015-10-08 12:58:45 -04002818template <typename VarT>
2819void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2820 const std::string &prefix,
Olli Etuaho855d9642017-05-17 14:05:06 +03002821 const std::string &mappedPrefix,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002822 int blockIndex)
2823{
2824 for (const VarT &field : fields)
2825 {
2826 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2827
Olli Etuaho855d9642017-05-17 14:05:06 +03002828 const std::string &fullMappedName =
2829 (mappedPrefix.empty() ? field.mappedName : mappedPrefix + "." + field.mappedName);
2830
Jamie Madill4a3c2342015-10-08 12:58:45 -04002831 if (field.isStruct())
2832 {
2833 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2834 {
2835 const std::string uniformElementName =
2836 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
Olli Etuaho855d9642017-05-17 14:05:06 +03002837 const std::string uniformElementMappedName =
2838 fullMappedName + (field.isArray() ? ArrayString(arrayElement) : "");
2839 defineUniformBlockMembers(field.fields, uniformElementName,
2840 uniformElementMappedName, blockIndex);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002841 }
2842 }
2843 else
2844 {
2845 // If getBlockMemberInfo returns false, the uniform is optimized out.
2846 sh::BlockMemberInfo memberInfo;
Olli Etuaho855d9642017-05-17 14:05:06 +03002847 if (!mProgram->getUniformBlockMemberInfo(fullName, fullMappedName, &memberInfo))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002848 {
2849 continue;
2850 }
2851
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002852 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002853 -1, blockIndex, memberInfo);
Olli Etuaho855d9642017-05-17 14:05:06 +03002854 newUniform.mappedName = fullMappedName;
Jamie Madill4a3c2342015-10-08 12:58:45 -04002855
2856 // Since block uniforms have no location, we don't need to store them in the uniform
2857 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002858 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002859 }
2860 }
2861}
2862
Jamie Madill62d31cb2015-09-11 13:25:51 -04002863void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2864{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002865 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002866 size_t blockSize = 0;
2867
Jamie Madill4a3c2342015-10-08 12:58:45 -04002868 // Track the first and last uniform index to determine the range of active uniforms in the
2869 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002870 size_t firstBlockUniformIndex = mState.mUniforms.size();
Olli Etuaho855d9642017-05-17 14:05:06 +03002871 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(),
2872 interfaceBlock.fieldMappedPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002873 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002874
2875 std::vector<unsigned int> blockUniformIndexes;
2876 for (size_t blockUniformIndex = firstBlockUniformIndex;
2877 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2878 {
2879 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2880 }
jchen10af713a22017-04-19 09:10:56 +08002881 // ESSL 3.10 section 4.4.4 page 58:
2882 // Any uniform or shader storage block declared without a binding qualifier is initially
2883 // assigned to block binding point zero.
2884 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002885 if (interfaceBlock.arraySize > 0)
2886 {
2887 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2888 {
jchen10af713a22017-04-19 09:10:56 +08002889 // Don't define this block at all if it's not active in the implementation.
Olli Etuaho855d9642017-05-17 14:05:06 +03002890 if (!mProgram->getUniformBlockSize(
2891 interfaceBlock.name + ArrayString(arrayElement),
2892 interfaceBlock.mappedName + ArrayString(arrayElement), &blockSize))
jchen10af713a22017-04-19 09:10:56 +08002893 {
2894 continue;
2895 }
Olli Etuaho855d9642017-05-17 14:05:06 +03002896 UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, true, arrayElement,
jchen10af713a22017-04-19 09:10:56 +08002897 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002898 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002899
Martin Radev4c4c8e72016-08-04 12:25:34 +03002900 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002901 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002902 case GL_VERTEX_SHADER:
2903 {
2904 block.vertexStaticUse = interfaceBlock.staticUse;
2905 break;
2906 }
2907 case GL_FRAGMENT_SHADER:
2908 {
2909 block.fragmentStaticUse = interfaceBlock.staticUse;
2910 break;
2911 }
2912 case GL_COMPUTE_SHADER:
2913 {
2914 block.computeStaticUse = interfaceBlock.staticUse;
2915 break;
2916 }
2917 default:
2918 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002919 }
2920
Qin Jiajia0350a642016-11-01 17:01:51 +08002921 // Since all block elements in an array share the same active uniforms, they will all be
2922 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2923 // here we will add every block element in the array.
2924 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002925 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926 }
2927 }
2928 else
2929 {
Olli Etuaho855d9642017-05-17 14:05:06 +03002930 if (!mProgram->getUniformBlockSize(interfaceBlock.name, interfaceBlock.mappedName,
2931 &blockSize))
jchen10af713a22017-04-19 09:10:56 +08002932 {
2933 return;
2934 }
Olli Etuaho855d9642017-05-17 14:05:06 +03002935 UniformBlock block(interfaceBlock.name, interfaceBlock.mappedName, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002936 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002937
Martin Radev4c4c8e72016-08-04 12:25:34 +03002938 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002939 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002940 case GL_VERTEX_SHADER:
2941 {
2942 block.vertexStaticUse = interfaceBlock.staticUse;
2943 break;
2944 }
2945 case GL_FRAGMENT_SHADER:
2946 {
2947 block.fragmentStaticUse = interfaceBlock.staticUse;
2948 break;
2949 }
2950 case GL_COMPUTE_SHADER:
2951 {
2952 block.computeStaticUse = interfaceBlock.staticUse;
2953 break;
2954 }
2955 default:
2956 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002957 }
2958
Jamie Madill4a3c2342015-10-08 12:58:45 -04002959 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002960 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002961 }
2962}
2963
Jamie Madille7d84322017-01-10 18:21:59 -05002964void Program::updateSamplerUniform(const VariableLocation &locationInfo,
Jamie Madille7d84322017-01-10 18:21:59 -05002965 GLsizei clampedCount,
2966 const GLint *v)
2967{
2968 // Invalidate the validation cache only if we modify the sampler data.
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002969 if (mState.isSamplerUniformIndex(locationInfo.index))
Jamie Madille7d84322017-01-10 18:21:59 -05002970 {
2971 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2972 std::vector<GLuint> *boundTextureUnits =
2973 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2974
2975 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2976 mCachedValidateSamplersResult.reset();
2977 }
2978}
2979
2980template <typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002981GLsizei Program::clampUniformCount(const VariableLocation &locationInfo,
2982 GLsizei count,
2983 int vectorSize,
Jamie Madille7d84322017-01-10 18:21:59 -05002984 const T *v)
2985{
Jamie Madill134f93d2017-08-31 17:11:00 -04002986 if (count == 1)
2987 return 1;
2988
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002989 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002990
Corentin Wallez15ac5342016-11-03 17:06:39 -04002991 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2992 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002993 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002994 GLsizei maxElementCount =
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002995 static_cast<GLsizei>(remainingElements * linkedUniform.getElementComponents());
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002996
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002997 if (count * vectorSize > maxElementCount)
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002998 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04002999 return maxElementCount / vectorSize;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003000 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003001
3002 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003003}
3004
3005template <size_t cols, size_t rows, typename T>
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003006GLsizei Program::clampMatrixUniformCount(GLint location,
3007 GLsizei count,
3008 GLboolean transpose,
3009 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003010{
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003011 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3012
Jamie Madill62d31cb2015-09-11 13:25:51 -04003013 if (!transpose)
3014 {
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003015 return clampUniformCount(locationInfo, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003016 }
3017
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003018 const LinkedUniform &linkedUniform = mState.mUniforms[locationInfo.index];
Corentin Wallez15ac5342016-11-03 17:06:39 -04003019
3020 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3021 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
Jamie Madillbe5e2ec2017-08-31 13:28:28 -04003022 unsigned int remainingElements = linkedUniform.elementCount() - locationInfo.element;
3023 return std::min(count, static_cast<GLsizei>(remainingElements));
Jamie Madill62d31cb2015-09-11 13:25:51 -04003024}
3025
Jamie Madill54164b02017-08-28 15:17:37 -04003026// Driver differences mean that doing the uniform value cast ourselves gives consistent results.
3027// EG: on NVIDIA drivers, it was observed that getUniformi for MAX_INT+1 returned MIN_INT.
Jamie Madill62d31cb2015-09-11 13:25:51 -04003028template <typename DestT>
Jamie Madill54164b02017-08-28 15:17:37 -04003029void Program::getUniformInternal(const Context *context,
3030 DestT *dataOut,
3031 GLint location,
3032 GLenum nativeType,
3033 int components) const
Jamie Madill62d31cb2015-09-11 13:25:51 -04003034{
Jamie Madill54164b02017-08-28 15:17:37 -04003035 switch (nativeType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003036 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04003037 case GL_BOOL:
Jamie Madill54164b02017-08-28 15:17:37 -04003038 {
3039 GLint tempValue[16] = {0};
3040 mProgram->getUniformiv(context, location, tempValue);
3041 UniformStateQueryCastLoop<GLboolean>(
3042 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003043 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003044 }
3045 case GL_INT:
3046 {
3047 GLint tempValue[16] = {0};
3048 mProgram->getUniformiv(context, location, tempValue);
3049 UniformStateQueryCastLoop<GLint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3050 components);
3051 break;
3052 }
3053 case GL_UNSIGNED_INT:
3054 {
3055 GLuint tempValue[16] = {0};
3056 mProgram->getUniformuiv(context, location, tempValue);
3057 UniformStateQueryCastLoop<GLuint>(dataOut, reinterpret_cast<const uint8_t *>(tempValue),
3058 components);
3059 break;
3060 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003061 case GL_FLOAT:
Jamie Madill54164b02017-08-28 15:17:37 -04003062 {
3063 GLfloat tempValue[16] = {0};
3064 mProgram->getUniformfv(context, location, tempValue);
3065 UniformStateQueryCastLoop<GLfloat>(
3066 dataOut, reinterpret_cast<const uint8_t *>(tempValue), components);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003067 break;
Jamie Madill54164b02017-08-28 15:17:37 -04003068 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04003069 default:
3070 UNREACHABLE();
Jamie Madill54164b02017-08-28 15:17:37 -04003071 break;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003072 }
3073}
Jamie Madilla4595b82017-01-11 17:36:34 -05003074
3075bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3076{
3077 // Must be called after samplers are validated.
3078 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3079
3080 for (const auto &binding : mState.mSamplerBindings)
3081 {
3082 GLenum textureType = binding.textureType;
3083 for (const auto &unit : binding.boundTextureUnits)
3084 {
3085 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3086 if (programTextureID == textureID)
3087 {
3088 // TODO(jmadill): Check for appropriate overlap.
3089 return true;
3090 }
3091 }
3092 }
3093
3094 return false;
3095}
3096
Jamie Madilla2c74982016-12-12 11:20:42 -05003097} // namespace gl