blob: 58180365ed57d75d058e42dab8f96b0daeee438b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050019#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040020#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040022#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000023#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040024#include "libANGLE/VaryingPacking.h"
25#include "libANGLE/features.h"
26#include "libANGLE/queryconversions.h"
27#include "libANGLE/renderer/GLImplFactory.h"
28#include "libANGLE/renderer/ProgramImpl.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000032
Geoff Lang7dd2e102014-11-10 15:19:26 -050033namespace
34{
35
Jamie Madill62d31cb2015-09-11 13:25:51 -040036// This simplified cast function doesn't need to worry about advanced concepts like
37// depth range values, or casting to bool.
38template <typename DestT, typename SrcT>
39DestT UniformStateQueryCast(SrcT value);
40
41// From-Float-To-Integer Casts
42template <>
43GLint UniformStateQueryCast(GLfloat value)
44{
45 return clampCast<GLint>(roundf(value));
46}
47
48template <>
49GLuint UniformStateQueryCast(GLfloat value)
50{
51 return clampCast<GLuint>(roundf(value));
52}
53
54// From-Integer-to-Integer Casts
55template <>
56GLint UniformStateQueryCast(GLuint value)
57{
58 return clampCast<GLint>(value);
59}
60
61template <>
62GLuint UniformStateQueryCast(GLint value)
63{
64 return clampCast<GLuint>(value);
65}
66
67// From-Boolean-to-Anything Casts
68template <>
69GLfloat UniformStateQueryCast(GLboolean value)
70{
71 return (value == GL_TRUE ? 1.0f : 0.0f);
72}
73
74template <>
75GLint UniformStateQueryCast(GLboolean value)
76{
77 return (value == GL_TRUE ? 1 : 0);
78}
79
80template <>
81GLuint UniformStateQueryCast(GLboolean value)
82{
83 return (value == GL_TRUE ? 1u : 0u);
84}
85
86// Default to static_cast
87template <typename DestT, typename SrcT>
88DestT UniformStateQueryCast(SrcT value)
89{
90 return static_cast<DestT>(value);
91}
92
93template <typename SrcT, typename DestT>
94void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
95{
96 for (int comp = 0; comp < components; ++comp)
97 {
98 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
99 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
100 size_t offset = comp * 4;
101 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
102 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
103 }
104}
105
Jamie Madill192745a2016-12-22 15:58:21 -0500106// true if varying x has a higher priority in packing than y
107bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
108{
jchen10a9042d32017-03-17 08:50:45 +0800109 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
110 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
111 sh::ShaderVariable vx, vy;
112 const sh::ShaderVariable *px, *py;
113 if (x.isArrayElement())
114 {
115 vx = *x.varying;
116 vx.arraySize = 0;
117 px = &vx;
118 }
119 else
120 {
121 px = x.varying;
122 }
123
124 if (y.isArrayElement())
125 {
126 vy = *y.varying;
127 vy.arraySize = 0;
128 py = &vy;
129 }
130 else
131 {
132 py = y.varying;
133 }
134
135 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500136}
137
jchen1015015f72017-03-16 13:54:21 +0800138template <typename VarT>
139GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
140{
141 size_t subscript = GL_INVALID_INDEX;
142 std::string baseName = ParseResourceName(name, &subscript);
143
144 // The app is not allowed to specify array indices other than 0 for arrays of basic types
145 if (subscript != 0 && subscript != GL_INVALID_INDEX)
146 {
147 return GL_INVALID_INDEX;
148 }
149
150 for (size_t index = 0; index < list.size(); index++)
151 {
152 const VarT &resource = list[index];
153 if (resource.name == baseName)
154 {
155 if (resource.isArray() || subscript == GL_INVALID_INDEX)
156 {
157 return static_cast<GLuint>(index);
158 }
159 }
160 }
161
162 return GL_INVALID_INDEX;
163}
164
jchen10fd7c3b52017-03-21 15:36:03 +0800165void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
166{
167 ASSERT(bufSize > 0);
168 strncpy(buffer, string.c_str(), bufSize);
169 buffer[bufSize - 1] = '\0';
170
171 if (length)
172 {
173 *length = static_cast<GLsizei>(strlen(buffer));
174 }
175}
176
jchen10a9042d32017-03-17 08:50:45 +0800177bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
178{
179 size_t subscript = GL_INVALID_INDEX;
180 std::string baseName = ParseResourceName(name, &subscript);
181 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
182 {
183 size_t arrayIndex = GL_INVALID_INDEX;
184 std::string arrayName = ParseResourceName(*it, &arrayIndex);
185 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
186 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
187 {
188 return true;
189 }
190 }
191 return false;
192}
193
Jamie Madill62d31cb2015-09-11 13:25:51 -0400194} // anonymous namespace
195
Jamie Madill4a3c2342015-10-08 12:58:45 -0400196const char *const g_fakepath = "C:\\fakepath";
197
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400198InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199{
200}
201
202InfoLog::~InfoLog()
203{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204}
205
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400206size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207{
Jamie Madill23176ce2017-07-31 14:14:33 -0400208 if (!mLazyStream)
209 {
210 return 0;
211 }
212
213 const std::string &logString = mLazyStream->str();
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400214 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000215}
216
Geoff Lange1a27752015-10-05 13:16:04 -0400217void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000218{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400219 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000220
221 if (bufSize > 0)
222 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400223 const std::string logString(str());
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400224
Jamie Madill23176ce2017-07-31 14:14:33 -0400225 if (!logString.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000226 {
Jamie Madill23176ce2017-07-31 14:14:33 -0400227 index = std::min(static_cast<size_t>(bufSize) - 1, logString.length());
228 memcpy(infoLog, logString.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000229 }
230
231 infoLog[index] = '\0';
232 }
233
234 if (length)
235 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400236 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000237 }
238}
239
240// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300241// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000242// messages, so lets remove all occurrences of this fake file path from the log.
243void InfoLog::appendSanitized(const char *message)
244{
Jamie Madill23176ce2017-07-31 14:14:33 -0400245 ensureInitialized();
246
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000247 std::string msg(message);
248
249 size_t found;
250 do
251 {
252 found = msg.find(g_fakepath);
253 if (found != std::string::npos)
254 {
255 msg.erase(found, strlen(g_fakepath));
256 }
257 }
258 while (found != std::string::npos);
259
Jamie Madill23176ce2017-07-31 14:14:33 -0400260 *mLazyStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000261}
262
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000263void InfoLog::reset()
264{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000265}
266
Geoff Langd8605522016-04-13 10:19:12 -0400267VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000268{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500269}
270
Geoff Langd8605522016-04-13 10:19:12 -0400271VariableLocation::VariableLocation(const std::string &name,
272 unsigned int element,
273 unsigned int index)
274 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500275{
276}
277
Geoff Langd8605522016-04-13 10:19:12 -0400278void Program::Bindings::bindLocation(GLuint index, const std::string &name)
279{
280 mBindings[name] = index;
281}
282
283int Program::Bindings::getBinding(const std::string &name) const
284{
285 auto iter = mBindings.find(name);
286 return (iter != mBindings.end()) ? iter->second : -1;
287}
288
289Program::Bindings::const_iterator Program::Bindings::begin() const
290{
291 return mBindings.begin();
292}
293
294Program::Bindings::const_iterator Program::Bindings::end() const
295{
296 return mBindings.end();
297}
298
Jamie Madill48ef11b2016-04-27 15:21:52 -0400299ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500300 : mLabel(),
301 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400302 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300303 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500304 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500305 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800306 mImageUniformRange(0, 0),
307 mAtomicCounterUniformRange(0, 0),
Martin Radev7cf61662017-07-26 17:10:53 +0300308 mBinaryRetrieveableHint(false),
309 mNumViews(-1)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400310{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300311 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400312}
313
Jamie Madill48ef11b2016-04-27 15:21:52 -0400314ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400315{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500316 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400317}
318
Jamie Madill48ef11b2016-04-27 15:21:52 -0400319const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500320{
321 return mLabel;
322}
323
Jamie Madill48ef11b2016-04-27 15:21:52 -0400324GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400325{
326 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800327 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400328
329 for (size_t location = 0; location < mUniformLocations.size(); ++location)
330 {
331 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400332 if (!uniformLocation.used)
333 {
334 continue;
335 }
336
337 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400338
339 if (uniform.name == baseName)
340 {
Geoff Langd8605522016-04-13 10:19:12 -0400341 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400342 {
Geoff Langd8605522016-04-13 10:19:12 -0400343 if (uniformLocation.element == subscript ||
344 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
345 {
346 return static_cast<GLint>(location);
347 }
348 }
349 else
350 {
351 if (subscript == GL_INVALID_INDEX)
352 {
353 return static_cast<GLint>(location);
354 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400355 }
356 }
357 }
358
359 return -1;
360}
361
Jamie Madille7d84322017-01-10 18:21:59 -0500362GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400363{
jchen1015015f72017-03-16 13:54:21 +0800364 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400365}
366
Jamie Madille7d84322017-01-10 18:21:59 -0500367GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
368{
369 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
370 return mUniformLocations[location].index;
371}
372
373Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
374{
375 GLuint index = getUniformIndexFromLocation(location);
376 if (!isSamplerUniformIndex(index))
377 {
378 return Optional<GLuint>::Invalid();
379 }
380
381 return getSamplerIndexFromUniformIndex(index);
382}
383
384bool ProgramState::isSamplerUniformIndex(GLuint index) const
385{
Jamie Madill982f6e02017-06-07 14:33:04 -0400386 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500387}
388
389GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
390{
391 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400392 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500393}
394
Jamie Madill34ca4f52017-06-13 11:49:39 -0400395GLuint ProgramState::getAttributeLocation(const std::string &name) const
396{
397 for (const sh::Attribute &attribute : mAttributes)
398 {
399 if (attribute.name == name)
400 {
401 return attribute.location;
402 }
403 }
404
405 return static_cast<GLuint>(-1);
406}
407
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500408Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400409 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400410 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500411 mLinked(false),
412 mDeleteStatus(false),
413 mRefCount(0),
414 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500415 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500416{
417 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000418
Geoff Lang7dd2e102014-11-10 15:19:26 -0500419 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422Program::~Program()
423{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400424 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425}
426
Jamie Madill4928b7c2017-06-20 12:57:39 -0400427void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500428{
429 if (mState.mAttachedVertexShader != nullptr)
430 {
431 mState.mAttachedVertexShader->release(context);
432 mState.mAttachedVertexShader = nullptr;
433 }
434
435 if (mState.mAttachedFragmentShader != nullptr)
436 {
437 mState.mAttachedFragmentShader->release(context);
438 mState.mAttachedFragmentShader = nullptr;
439 }
440
441 if (mState.mAttachedComputeShader != nullptr)
442 {
443 mState.mAttachedComputeShader->release(context);
444 mState.mAttachedComputeShader = nullptr;
445 }
446
Jamie Madillc564c072017-06-01 12:45:42 -0400447 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400448
449 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
450 !mState.mAttachedComputeShader);
451 SafeDelete(mProgram);
452
453 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500454}
455
Geoff Lang70d0f492015-12-10 17:45:46 -0500456void Program::setLabel(const std::string &label)
457{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400458 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500459}
460
461const std::string &Program::getLabel() const
462{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400463 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500464}
465
Jamie Madillef300b12016-10-07 15:12:09 -0400466void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300468 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300470 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
Jamie Madillef300b12016-10-07 15:12:09 -0400472 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300473 mState.mAttachedVertexShader = shader;
474 mState.mAttachedVertexShader->addRef();
475 break;
476 }
477 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478 {
Jamie Madillef300b12016-10-07 15:12:09 -0400479 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300480 mState.mAttachedFragmentShader = shader;
481 mState.mAttachedFragmentShader->addRef();
482 break;
483 }
484 case GL_COMPUTE_SHADER:
485 {
Jamie Madillef300b12016-10-07 15:12:09 -0400486 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300487 mState.mAttachedComputeShader = shader;
488 mState.mAttachedComputeShader->addRef();
489 break;
490 }
491 default:
492 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494}
495
Jamie Madillc1d770e2017-04-13 17:31:24 -0400496void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300498 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300500 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400502 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500503 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300504 mState.mAttachedVertexShader = nullptr;
505 break;
506 }
507 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400509 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500510 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300511 mState.mAttachedFragmentShader = nullptr;
512 break;
513 }
514 case GL_COMPUTE_SHADER:
515 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400516 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500517 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300518 mState.mAttachedComputeShader = nullptr;
519 break;
520 }
521 default:
522 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524}
525
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000526int Program::getAttachedShadersCount() const
527{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300528 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
529 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000530}
531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532void Program::bindAttributeLocation(GLuint index, const char *name)
533{
Geoff Langd8605522016-04-13 10:19:12 -0400534 mAttributeBindings.bindLocation(index, name);
535}
536
537void Program::bindUniformLocation(GLuint index, const char *name)
538{
539 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800540 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541}
542
Sami Väisänen46eaa942016-06-29 10:26:37 +0300543void Program::bindFragmentInputLocation(GLint index, const char *name)
544{
545 mFragmentInputBindings.bindLocation(index, name);
546}
547
Jamie Madillbd044ed2017-06-05 12:59:21 -0400548BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300549{
550 BindingInfo ret;
551 ret.type = GL_NONE;
552 ret.valid = false;
553
Jamie Madillbd044ed2017-06-05 12:59:21 -0400554 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300555 ASSERT(fragmentShader);
556
557 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400558 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300559
560 for (const auto &binding : mFragmentInputBindings)
561 {
562 if (binding.second != static_cast<GLuint>(index))
563 continue;
564
565 ret.valid = true;
566
567 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400568 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300569
570 for (const auto &in : inputs)
571 {
572 if (in.name == originalName)
573 {
574 if (in.isArray())
575 {
576 // The client wants to bind either "name" or "name[0]".
577 // GL ES 3.1 spec refers to active array names with language such as:
578 // "if the string identifies the base name of an active array, where the
579 // string would exactly match the name of the variable if the suffix "[0]"
580 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400581 if (arrayIndex == GL_INVALID_INDEX)
582 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300583
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400584 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300585 }
586 else
587 {
588 ret.name = in.mappedName;
589 }
590 ret.type = in.type;
591 return ret;
592 }
593 }
594 }
595
596 return ret;
597}
598
Jamie Madillbd044ed2017-06-05 12:59:21 -0400599void Program::pathFragmentInputGen(const Context *context,
600 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300601 GLenum genMode,
602 GLint components,
603 const GLfloat *coeffs)
604{
605 // If the location is -1 then the command is silently ignored
606 if (index == -1)
607 return;
608
Jamie Madillbd044ed2017-06-05 12:59:21 -0400609 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300610
611 // If the input doesn't exist then then the command is silently ignored
612 // This could happen through optimization for example, the shader translator
613 // decides that a variable is not actually being used and optimizes it away.
614 if (binding.name.empty())
615 return;
616
617 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
618}
619
Martin Radev4c4c8e72016-08-04 12:25:34 +0300620// The attached shaders are checked for linking errors by matching up their variables.
621// Uniform, input and output variables get collected.
622// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500623Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000624{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500625 const auto &data = context->getContextState();
626
Jamie Madill6c1f6712017-02-14 19:08:04 -0500627 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000628
Jamie Madill32447362017-06-28 14:53:52 -0400629 ProgramHash programHash;
630 auto *cache = context->getMemoryProgramCache();
631 if (cache)
632 {
633 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
634 }
635
636 if (mLinked)
637 {
638 return NoError();
639 }
640
641 // Cache load failed, fall through to normal linking.
642 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000643 mInfoLog.reset();
644
Martin Radev4c4c8e72016-08-04 12:25:34 +0300645 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500646
Jamie Madill192745a2016-12-22 15:58:21 -0500647 auto vertexShader = mState.mAttachedVertexShader;
648 auto fragmentShader = mState.mAttachedFragmentShader;
649 auto computeShader = mState.mAttachedComputeShader;
650
651 bool isComputeShaderAttached = (computeShader != nullptr);
652 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300653 // Check whether we both have a compute and non-compute shaders attached.
654 // If there are of both types attached, then linking should fail.
655 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
656 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500657 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
659 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400660 }
661
Jamie Madill192745a2016-12-22 15:58:21 -0500662 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500663 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400664 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665 {
666 mInfoLog << "Attached compute shader is not compiled.";
667 return NoError();
668 }
Jamie Madill192745a2016-12-22 15:58:21 -0500669 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300670
Jamie Madillbd044ed2017-06-05 12:59:21 -0400671 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300672
673 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
674 // If the work group size is not specified, a link time error should occur.
675 if (!mState.mComputeShaderLocalSize.isDeclared())
676 {
677 mInfoLog << "Work group size is not specified.";
678 return NoError();
679 }
680
Jamie Madillbd044ed2017-06-05 12:59:21 -0400681 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300682 {
683 return NoError();
684 }
685
Jamie Madillbd044ed2017-06-05 12:59:21 -0400686 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300687 {
688 return NoError();
689 }
690
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500691 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400692 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500693 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300694 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500695 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300696 }
697 }
698 else
699 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400700 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701 {
702 return NoError();
703 }
Jamie Madill192745a2016-12-22 15:58:21 -0500704 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705
Jamie Madillbd044ed2017-06-05 12:59:21 -0400706 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300707 {
708 return NoError();
709 }
Jamie Madill192745a2016-12-22 15:58:21 -0500710 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300711
Jamie Madillbd044ed2017-06-05 12:59:21 -0400712 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300713 {
714 mInfoLog << "Fragment shader version does not match vertex shader version.";
715 return NoError();
716 }
717
Jamie Madillbd044ed2017-06-05 12:59:21 -0400718 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300719 {
720 return NoError();
721 }
722
Jamie Madillbd044ed2017-06-05 12:59:21 -0400723 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 {
725 return NoError();
726 }
727
Jamie Madillbd044ed2017-06-05 12:59:21 -0400728 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300729 {
730 return NoError();
731 }
732
Jamie Madillbd044ed2017-06-05 12:59:21 -0400733 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300734 {
735 return NoError();
736 }
737
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400738 if (!linkValidateGlobalNames(context, mInfoLog))
739 {
740 return NoError();
741 }
742
Jamie Madillbd044ed2017-06-05 12:59:21 -0400743 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300744
jchen10a9042d32017-03-17 08:50:45 +0800745 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300746 {
747 return NoError();
748 }
749
Martin Radev7cf61662017-07-26 17:10:53 +0300750 mState.mNumViews = vertexShader->getNumViews(context);
751
Jamie Madillbd044ed2017-06-05 12:59:21 -0400752 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753
Jamie Madill192745a2016-12-22 15:58:21 -0500754 // Validate we can pack the varyings.
755 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
756
757 // Map the varyings to the register file
758 // In WebGL, we use a slightly different handling for packing variables.
759 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
760 : PackMode::ANGLE_RELAXED;
761 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
762 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
763 mState.getTransformFeedbackVaryingNames()))
764 {
765 return NoError();
766 }
767
Jamie Madillc564c072017-06-01 12:45:42 -0400768 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500769 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300770 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500771 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300772 }
773
774 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500775 }
776
jchen10eaef1e52017-06-13 10:44:11 +0800777 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400778 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400779
jchen10eaef1e52017-06-13 10:44:11 +0800780 setUniformValuesFromBindingQualifiers();
781
Jamie Madill32447362017-06-28 14:53:52 -0400782 // Save to the program cache.
783 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
784 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
785 {
786 cache->putProgram(programHash, context, this);
787 }
788
Martin Radev4c4c8e72016-08-04 12:25:34 +0300789 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000790}
791
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000792// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500793void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400795 mState.mAttributes.clear();
796 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800797 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400798 mState.mUniforms.clear();
799 mState.mUniformLocations.clear();
800 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800801 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800802 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400803 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800804 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400805 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400806 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300807 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500808 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800809 mState.mImageBindings.clear();
Martin Radev7cf61662017-07-26 17:10:53 +0300810 mState.mNumViews = -1;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500811
Geoff Lang7dd2e102014-11-10 15:19:26 -0500812 mValidated = false;
813
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000814 mLinked = false;
815}
816
Geoff Lange1a27752015-10-05 13:16:04 -0400817bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000818{
819 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820}
821
Jamie Madilla2c74982016-12-12 11:20:42 -0500822Error Program::loadBinary(const Context *context,
823 GLenum binaryFormat,
824 const void *binary,
825 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000826{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500827 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000828
Geoff Lang7dd2e102014-11-10 15:19:26 -0500829#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800830 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500831#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400832 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
833 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000834 {
Jamie Madillf6113162015-05-07 11:49:21 -0400835 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800836 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500837 }
838
Jamie Madill4f86d052017-06-05 12:59:26 -0400839 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
840 ANGLE_TRY_RESULT(
841 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400842
843 // Currently we require the full shader text to compute the program hash.
844 // TODO(jmadill): Store the binary in the internal program cache.
845
Jamie Madillb0a838b2016-11-13 20:02:12 -0500846 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500847#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500848}
849
Jamie Madilla2c74982016-12-12 11:20:42 -0500850Error Program::saveBinary(const Context *context,
851 GLenum *binaryFormat,
852 void *binary,
853 GLsizei bufSize,
854 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500855{
856 if (binaryFormat)
857 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400858 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500859 }
860
Jamie Madill4f86d052017-06-05 12:59:26 -0400861 angle::MemoryBuffer memoryBuf;
862 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500863
Jamie Madill4f86d052017-06-05 12:59:26 -0400864 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
865 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500866
867 if (streamLength > bufSize)
868 {
869 if (length)
870 {
871 *length = 0;
872 }
873
874 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
875 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
876 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500877 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878 }
879
880 if (binary)
881 {
882 char *ptr = reinterpret_cast<char*>(binary);
883
Jamie Madill48ef11b2016-04-27 15:21:52 -0400884 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885 ptr += streamLength;
886
887 ASSERT(ptr - streamLength == binary);
888 }
889
890 if (length)
891 {
892 *length = streamLength;
893 }
894
He Yunchaoacd18982017-01-04 10:46:42 +0800895 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500896}
897
Jamie Madillffe00c02017-06-27 16:26:55 -0400898GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500899{
900 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400901 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500902 if (error.isError())
903 {
904 return 0;
905 }
906
907 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000908}
909
Geoff Langc5629752015-12-07 16:29:04 -0500910void Program::setBinaryRetrievableHint(bool retrievable)
911{
912 // TODO(jmadill) : replace with dirty bits
913 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400914 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500915}
916
917bool Program::getBinaryRetrievableHint() const
918{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400919 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500920}
921
Yunchao He61afff12017-03-14 15:34:03 +0800922void Program::setSeparable(bool separable)
923{
924 // TODO(yunchao) : replace with dirty bits
925 if (mState.mSeparable != separable)
926 {
927 mProgram->setSeparable(separable);
928 mState.mSeparable = separable;
929 }
930}
931
932bool Program::isSeparable() const
933{
934 return mState.mSeparable;
935}
936
Jamie Madill6c1f6712017-02-14 19:08:04 -0500937void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000938{
939 mRefCount--;
940
941 if (mRefCount == 0 && mDeleteStatus)
942 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500943 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944 }
945}
946
947void Program::addRef()
948{
949 mRefCount++;
950}
951
952unsigned int Program::getRefCount() const
953{
954 return mRefCount;
955}
956
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000957int Program::getInfoLogLength() const
958{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400959 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000960}
961
Geoff Lange1a27752015-10-05 13:16:04 -0400962void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000963{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000964 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000965}
966
Geoff Lange1a27752015-10-05 13:16:04 -0400967void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000968{
969 int total = 0;
970
Martin Radev4c4c8e72016-08-04 12:25:34 +0300971 if (mState.mAttachedComputeShader)
972 {
973 if (total < maxCount)
974 {
975 shaders[total] = mState.mAttachedComputeShader->getHandle();
976 total++;
977 }
978 }
979
Jamie Madill48ef11b2016-04-27 15:21:52 -0400980 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000981 {
982 if (total < maxCount)
983 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400984 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200985 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000986 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000987 }
988
Jamie Madill48ef11b2016-04-27 15:21:52 -0400989 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000990 {
991 if (total < maxCount)
992 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400993 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200994 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000995 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000996 }
997
998 if (count)
999 {
1000 *count = total;
1001 }
1002}
1003
Geoff Lange1a27752015-10-05 13:16:04 -04001004GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001005{
Jamie Madill34ca4f52017-06-13 11:49:39 -04001006 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007}
1008
Jamie Madill63805b42015-08-25 13:17:39 -04001009bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001010{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001011 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1012 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001013}
1014
jchen10fd7c3b52017-03-21 15:36:03 +08001015void Program::getActiveAttribute(GLuint index,
1016 GLsizei bufsize,
1017 GLsizei *length,
1018 GLint *size,
1019 GLenum *type,
1020 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001021{
Jamie Madillc349ec02015-08-21 16:53:12 -04001022 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001023 {
1024 if (bufsize > 0)
1025 {
1026 name[0] = '\0';
1027 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001028
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001029 if (length)
1030 {
1031 *length = 0;
1032 }
1033
1034 *type = GL_NONE;
1035 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001036 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001037 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001038
jchen1036e120e2017-03-14 14:53:58 +08001039 ASSERT(index < mState.mAttributes.size());
1040 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001041
1042 if (bufsize > 0)
1043 {
jchen10fd7c3b52017-03-21 15:36:03 +08001044 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001045 }
1046
1047 // Always a single 'type' instance
1048 *size = 1;
1049 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001050}
1051
Geoff Lange1a27752015-10-05 13:16:04 -04001052GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001053{
Jamie Madillc349ec02015-08-21 16:53:12 -04001054 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001055 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001056 return 0;
1057 }
1058
jchen1036e120e2017-03-14 14:53:58 +08001059 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001060}
1061
Geoff Lange1a27752015-10-05 13:16:04 -04001062GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001063{
Jamie Madillc349ec02015-08-21 16:53:12 -04001064 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001065 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001066 return 0;
1067 }
1068
1069 size_t maxLength = 0;
1070
Jamie Madill48ef11b2016-04-27 15:21:52 -04001071 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001072 {
jchen1036e120e2017-03-14 14:53:58 +08001073 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001074 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001075
Jamie Madillc349ec02015-08-21 16:53:12 -04001076 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001077}
1078
jchen1015015f72017-03-16 13:54:21 +08001079GLuint Program::getInputResourceIndex(const GLchar *name) const
1080{
1081 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1082 {
1083 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1084 if (attribute.name == name)
1085 {
1086 return attributeIndex;
1087 }
1088 }
1089 return GL_INVALID_INDEX;
1090}
1091
1092GLuint Program::getOutputResourceIndex(const GLchar *name) const
1093{
1094 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1095}
1096
jchen10fd7c3b52017-03-21 15:36:03 +08001097size_t Program::getOutputResourceCount() const
1098{
1099 return (mLinked ? mState.mOutputVariables.size() : 0);
1100}
1101
1102void Program::getInputResourceName(GLuint index,
1103 GLsizei bufSize,
1104 GLsizei *length,
1105 GLchar *name) const
1106{
1107 GLint size;
1108 GLenum type;
1109 getActiveAttribute(index, bufSize, length, &size, &type, name);
1110}
1111
1112void Program::getOutputResourceName(GLuint index,
1113 GLsizei bufSize,
1114 GLsizei *length,
1115 GLchar *name) const
1116{
1117 if (length)
1118 {
1119 *length = 0;
1120 }
1121
1122 if (!mLinked)
1123 {
1124 if (bufSize > 0)
1125 {
1126 name[0] = '\0';
1127 }
1128 return;
1129 }
1130 ASSERT(index < mState.mOutputVariables.size());
1131 const auto &output = mState.mOutputVariables[index];
1132
1133 if (bufSize > 0)
1134 {
1135 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1136
1137 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1138 }
1139}
1140
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141GLint Program::getFragDataLocation(const std::string &name) const
1142{
1143 std::string baseName(name);
1144 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001145 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001146 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001147 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001148 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1149 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001150 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001151 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001152 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001153 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001154}
1155
Geoff Lange1a27752015-10-05 13:16:04 -04001156void Program::getActiveUniform(GLuint index,
1157 GLsizei bufsize,
1158 GLsizei *length,
1159 GLint *size,
1160 GLenum *type,
1161 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001162{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001164 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001165 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001166 ASSERT(index < mState.mUniforms.size());
1167 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001168
1169 if (bufsize > 0)
1170 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001171 std::string string = uniform.name;
1172 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001173 {
1174 string += "[0]";
1175 }
jchen10fd7c3b52017-03-21 15:36:03 +08001176 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001177 }
1178
Jamie Madill62d31cb2015-09-11 13:25:51 -04001179 *size = uniform.elementCount();
1180 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001181 }
1182 else
1183 {
1184 if (bufsize > 0)
1185 {
1186 name[0] = '\0';
1187 }
1188
1189 if (length)
1190 {
1191 *length = 0;
1192 }
1193
1194 *size = 0;
1195 *type = GL_NONE;
1196 }
1197}
1198
Geoff Lange1a27752015-10-05 13:16:04 -04001199GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001200{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001202 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001203 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001204 }
1205 else
1206 {
1207 return 0;
1208 }
1209}
1210
Geoff Lange1a27752015-10-05 13:16:04 -04001211GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001212{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001213 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214
1215 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001216 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001217 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001218 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001219 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001220 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001221 size_t length = uniform.name.length() + 1u;
1222 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 {
1224 length += 3; // Counting in "[0]".
1225 }
1226 maxLength = std::max(length, maxLength);
1227 }
1228 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001229 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001230
Jamie Madill62d31cb2015-09-11 13:25:51 -04001231 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232}
1233
1234GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1235{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001236 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001237 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001239 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1241 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1242 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001243 case GL_UNIFORM_BLOCK_INDEX:
1244 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1246 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1247 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1248 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1249 default:
1250 UNREACHABLE();
1251 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253 return 0;
1254}
1255
1256bool Program::isValidUniformLocation(GLint location) const
1257{
Jamie Madille2e406c2016-06-02 13:04:10 -04001258 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001259 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1260 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001261}
1262
Jamie Madill62d31cb2015-09-11 13:25:51 -04001263const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001265 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001266 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267}
1268
Jamie Madillac4e9c32017-01-13 14:07:12 -05001269const VariableLocation &Program::getUniformLocation(GLint location) const
1270{
1271 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1272 return mState.mUniformLocations[location];
1273}
1274
1275const std::vector<VariableLocation> &Program::getUniformLocations() const
1276{
1277 return mState.mUniformLocations;
1278}
1279
1280const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1281{
1282 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1283 return mState.mUniforms[index];
1284}
1285
Jamie Madill62d31cb2015-09-11 13:25:51 -04001286GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001288 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289}
1290
Jamie Madill62d31cb2015-09-11 13:25:51 -04001291GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292{
Jamie Madille7d84322017-01-10 18:21:59 -05001293 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001294}
1295
1296void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1297{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001298 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1299 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001300}
1301
1302void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1303{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001304 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1305 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306}
1307
1308void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1309{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001310 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1311 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001312}
1313
1314void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1315{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001316 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1317 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001318}
1319
1320void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1321{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001322 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1323 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001324}
1325
1326void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1327{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001328 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1329 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330}
1331
1332void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1333{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001334 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1335 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001336}
1337
1338void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1339{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001340 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1341 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342}
1343
1344void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1345{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001346 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1347 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001348}
1349
1350void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1351{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001352 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1353 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354}
1355
1356void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1357{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001358 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1359 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001360}
1361
1362void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1363{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001364 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1365 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001366}
1367
1368void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1369{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001370 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1371 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001372}
1373
1374void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1375{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001376 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1377 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378}
1379
1380void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1381{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001382 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1383 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384}
1385
1386void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1387{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001388 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1389 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390}
1391
1392void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1393{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001394 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1395 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001396}
1397
1398void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1399{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001400 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1401 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402}
1403
1404void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1405{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001406 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1407 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408}
1409
1410void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1411{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001412 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1413 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414}
1415
1416void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1417{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001418 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1419 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001420}
1421
Geoff Lange1a27752015-10-05 13:16:04 -04001422void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001424 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425}
1426
Geoff Lange1a27752015-10-05 13:16:04 -04001427void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001429 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430}
1431
Geoff Lange1a27752015-10-05 13:16:04 -04001432void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001434 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435}
1436
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001437void Program::flagForDeletion()
1438{
1439 mDeleteStatus = true;
1440}
1441
1442bool Program::isFlaggedForDeletion() const
1443{
1444 return mDeleteStatus;
1445}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001446
Brandon Jones43a53e22014-08-28 16:23:22 -07001447void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001448{
1449 mInfoLog.reset();
1450
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001452 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001453 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001454 }
1455 else
1456 {
Jamie Madillf6113162015-05-07 11:49:21 -04001457 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001458 }
1459}
1460
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1462{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001463 // Skip cache if we're using an infolog, so we get the full error.
1464 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1465 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1466 {
1467 return mCachedValidateSamplersResult.value();
1468 }
1469
1470 if (mTextureUnitTypesCache.empty())
1471 {
1472 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1473 }
1474 else
1475 {
1476 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1477 }
1478
1479 // if any two active samplers in a program are of different types, but refer to the same
1480 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1481 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001482 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001483 {
Jamie Madille7d84322017-01-10 18:21:59 -05001484 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001485
Jamie Madille7d84322017-01-10 18:21:59 -05001486 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001487 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001488 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1489 {
1490 if (infoLog)
1491 {
1492 (*infoLog) << "Sampler uniform (" << textureUnit
1493 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1494 << caps.maxCombinedTextureImageUnits << ")";
1495 }
1496
1497 mCachedValidateSamplersResult = false;
1498 return false;
1499 }
1500
1501 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1502 {
1503 if (textureType != mTextureUnitTypesCache[textureUnit])
1504 {
1505 if (infoLog)
1506 {
1507 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1508 "image unit ("
1509 << textureUnit << ").";
1510 }
1511
1512 mCachedValidateSamplersResult = false;
1513 return false;
1514 }
1515 }
1516 else
1517 {
1518 mTextureUnitTypesCache[textureUnit] = textureType;
1519 }
1520 }
1521 }
1522
1523 mCachedValidateSamplersResult = true;
1524 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525}
1526
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001527bool Program::isValidated() const
1528{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001529 return mValidated;
1530}
1531
Geoff Lange1a27752015-10-05 13:16:04 -04001532GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001533{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001534 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535}
1536
1537void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1538{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001539 ASSERT(
1540 uniformBlockIndex <
1541 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001542
Jamie Madill48ef11b2016-04-27 15:21:52 -04001543 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001544
1545 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001546 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001547 std::string string = uniformBlock.name;
1548
Jamie Madill62d31cb2015-09-11 13:25:51 -04001549 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001550 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001551 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552 }
jchen10fd7c3b52017-03-21 15:36:03 +08001553 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001554 }
1555}
1556
Geoff Lange1a27752015-10-05 13:16:04 -04001557GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001558{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001559 int maxLength = 0;
1560
1561 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001562 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001563 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1565 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001566 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567 if (!uniformBlock.name.empty())
1568 {
jchen10af713a22017-04-19 09:10:56 +08001569 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1570 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001571 }
1572 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001573 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001574
1575 return maxLength;
1576}
1577
Geoff Lange1a27752015-10-05 13:16:04 -04001578GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001579{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001580 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001581 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001582
Jamie Madill48ef11b2016-04-27 15:21:52 -04001583 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001584 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1585 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001586 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001587 if (uniformBlock.name == baseName)
1588 {
1589 const bool arrayElementZero =
1590 (subscript == GL_INVALID_INDEX &&
1591 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1592 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1593 {
1594 return blockIndex;
1595 }
1596 }
1597 }
1598
1599 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001600}
1601
Jamie Madill62d31cb2015-09-11 13:25:51 -04001602const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001603{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001604 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1605 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001606}
1607
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001608void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1609{
jchen107a20b972017-06-13 14:25:26 +08001610 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001611 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001612 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001613}
1614
1615GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1616{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001617 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001618}
1619
Geoff Lang48dcae72014-02-05 16:28:24 -05001620void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1621{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001622 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001623 for (GLsizei i = 0; i < count; i++)
1624 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001625 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001626 }
1627
Jamie Madill48ef11b2016-04-27 15:21:52 -04001628 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001629}
1630
1631void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1632{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001633 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001634 {
jchen10a9042d32017-03-17 08:50:45 +08001635 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1636 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1637 std::string varName = var.nameWithArrayIndex();
1638 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001639 if (length)
1640 {
1641 *length = lastNameIdx;
1642 }
1643 if (size)
1644 {
jchen10a9042d32017-03-17 08:50:45 +08001645 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001646 }
1647 if (type)
1648 {
jchen10a9042d32017-03-17 08:50:45 +08001649 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001650 }
1651 if (name)
1652 {
jchen10a9042d32017-03-17 08:50:45 +08001653 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001654 name[lastNameIdx] = '\0';
1655 }
1656 }
1657}
1658
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001659GLsizei Program::getTransformFeedbackVaryingCount() const
1660{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001662 {
jchen10a9042d32017-03-17 08:50:45 +08001663 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001664 }
1665 else
1666 {
1667 return 0;
1668 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001669}
1670
1671GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1672{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001674 {
1675 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001676 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001677 {
jchen10a9042d32017-03-17 08:50:45 +08001678 maxSize =
1679 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001680 }
1681
1682 return maxSize;
1683 }
1684 else
1685 {
1686 return 0;
1687 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001688}
1689
1690GLenum Program::getTransformFeedbackBufferMode() const
1691{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001692 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693}
1694
Jamie Madillbd044ed2017-06-05 12:59:21 -04001695bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001697 Shader *vertexShader = mState.mAttachedVertexShader;
1698 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001699
Jamie Madillbd044ed2017-06-05 12:59:21 -04001700 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001701
Jamie Madillbd044ed2017-06-05 12:59:21 -04001702 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1703 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704
Sami Väisänen46eaa942016-06-29 10:26:37 +03001705 std::map<GLuint, std::string> staticFragmentInputLocations;
1706
Jamie Madill4cff2472015-08-21 16:53:18 -04001707 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001708 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709 bool matched = false;
1710
1711 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001712 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713 {
1714 continue;
1715 }
1716
Jamie Madill4cff2472015-08-21 16:53:18 -04001717 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001719 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001720 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001721 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001722 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001723 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 {
1725 return false;
1726 }
1727
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728 matched = true;
1729 break;
1730 }
1731 }
1732
1733 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001734 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001736 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001737 return false;
1738 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001739
1740 // Check for aliased path rendering input bindings (if any).
1741 // If more than one binding refer statically to the same
1742 // location the link must fail.
1743
1744 if (!output.staticUse)
1745 continue;
1746
1747 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1748 if (inputBinding == -1)
1749 continue;
1750
1751 const auto it = staticFragmentInputLocations.find(inputBinding);
1752 if (it == std::end(staticFragmentInputLocations))
1753 {
1754 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1755 }
1756 else
1757 {
1758 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1759 << it->second;
1760 return false;
1761 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001762 }
1763
Jamie Madillbd044ed2017-06-05 12:59:21 -04001764 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001765 {
1766 return false;
1767 }
1768
Jamie Madillada9ecc2015-08-17 12:53:37 -04001769 // TODO(jmadill): verify no unmatched vertex varyings?
1770
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771 return true;
1772}
1773
Jamie Madillbd044ed2017-06-05 12:59:21 -04001774bool Program::linkUniforms(const Context *context,
1775 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001776 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001777{
Olli Etuahob78707c2017-03-09 15:03:11 +00001778 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001779 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001780 {
1781 return false;
1782 }
1783
Olli Etuahob78707c2017-03-09 15:03:11 +00001784 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001785
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001786 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001787
jchen10eaef1e52017-06-13 10:44:11 +08001788 if (!linkAtomicCounterBuffers())
1789 {
1790 return false;
1791 }
1792
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001793 return true;
1794}
1795
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001796void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001797{
Jamie Madill982f6e02017-06-07 14:33:04 -04001798 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1799 unsigned int low = high;
1800
jchen10eaef1e52017-06-13 10:44:11 +08001801 for (auto counterIter = mState.mUniforms.rbegin();
1802 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1803 {
1804 --low;
1805 }
1806
1807 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1808
1809 high = low;
1810
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001811 for (auto imageIter = mState.mUniforms.rbegin();
1812 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1813 {
1814 --low;
1815 }
1816
1817 mState.mImageUniformRange = RangeUI(low, high);
1818
1819 // If uniform is a image type, insert it into the mImageBindings array.
1820 for (unsigned int imageIndex : mState.mImageUniformRange)
1821 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001822 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1823 // cannot load values into a uniform defined as an image. if declare without a
1824 // binding qualifier, any uniform image variable (include all elements of
1825 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001826 auto &imageUniform = mState.mUniforms[imageIndex];
1827 if (imageUniform.binding == -1)
1828 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001829 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001830 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001831 else
1832 {
1833 mState.mImageBindings.emplace_back(
1834 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1835 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001836 }
1837
1838 high = low;
1839
1840 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001841 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001842 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001843 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001844 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001845
1846 mState.mSamplerUniformRange = RangeUI(low, high);
1847
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001848 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001849 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001850 {
1851 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1852 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1853 mState.mSamplerBindings.emplace_back(
1854 SamplerBinding(textureType, samplerUniform.elementCount()));
1855 }
1856}
1857
jchen10eaef1e52017-06-13 10:44:11 +08001858bool Program::linkAtomicCounterBuffers()
1859{
1860 for (unsigned int index : mState.mAtomicCounterUniformRange)
1861 {
1862 auto &uniform = mState.mUniforms[index];
1863 bool found = false;
1864 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1865 ++bufferIndex)
1866 {
1867 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1868 if (buffer.binding == uniform.binding)
1869 {
1870 buffer.memberIndexes.push_back(index);
1871 uniform.bufferIndex = bufferIndex;
1872 found = true;
1873 break;
1874 }
1875 }
1876 if (!found)
1877 {
1878 AtomicCounterBuffer atomicCounterBuffer;
1879 atomicCounterBuffer.binding = uniform.binding;
1880 atomicCounterBuffer.memberIndexes.push_back(index);
1881 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1882 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1883 }
1884 }
1885 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1886 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1887
1888 return true;
1889}
1890
Martin Radev4c4c8e72016-08-04 12:25:34 +03001891bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1892 const std::string &uniformName,
1893 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001894 const sh::InterfaceBlockField &fragmentUniform,
1895 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001897 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1898 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1899 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 {
1901 return false;
1902 }
1903
1904 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1905 {
Jamie Madillf6113162015-05-07 11:49:21 -04001906 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 return false;
1908 }
1909
1910 return true;
1911}
1912
Jamie Madilleb979bf2016-11-15 12:28:46 -05001913// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001914bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001915{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001916 const ContextState &data = context->getContextState();
1917 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001918
Geoff Lang7dd2e102014-11-10 15:19:26 -05001919 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001920 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001921 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001922
1923 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001924 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001925 {
Jamie Madillf6113162015-05-07 11:49:21 -04001926 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001927 return false;
1928 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001930 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001931
Jamie Madillc349ec02015-08-21 16:53:12 -04001932 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001933 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001934 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001935 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001936 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001937 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001938 attribute.location = bindingLocation;
1939 }
1940
1941 if (attribute.location != -1)
1942 {
1943 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001944 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945
Jamie Madill63805b42015-08-25 13:17:39 -04001946 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947 {
Jamie Madillf6113162015-05-07 11:49:21 -04001948 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001949 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001950
1951 return false;
1952 }
1953
Jamie Madill63805b42015-08-25 13:17:39 -04001954 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955 {
Jamie Madill63805b42015-08-25 13:17:39 -04001956 const int regLocation = attribute.location + reg;
1957 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001958
1959 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001960 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001961 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001962 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001963 // TODO(jmadill): fix aliasing on ES2
1964 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001966 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001967 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 return false;
1969 }
1970 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001971 else
1972 {
Jamie Madill63805b42015-08-25 13:17:39 -04001973 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001974 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975
Jamie Madill63805b42015-08-25 13:17:39 -04001976 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 }
1978 }
1979 }
1980
1981 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001982 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001983 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001984 // Not set by glBindAttribLocation or by location layout qualifier
1985 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 {
Jamie Madill63805b42015-08-25 13:17:39 -04001987 int regs = VariableRegisterCount(attribute.type);
1988 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001989
Jamie Madill63805b42015-08-25 13:17:39 -04001990 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001991 {
Jamie Madillf6113162015-05-07 11:49:21 -04001992 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001993 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 }
1995
Jamie Madillc349ec02015-08-21 16:53:12 -04001996 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997 }
1998 }
1999
Jamie Madill48ef11b2016-04-27 15:21:52 -04002000 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001 {
Jamie Madill63805b42015-08-25 13:17:39 -04002002 ASSERT(attribute.location != -1);
2003 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002004
Jamie Madill63805b42015-08-25 13:17:39 -04002005 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002007 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 }
2009 }
2010
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011 return true;
2012}
2013
Martin Radev4c4c8e72016-08-04 12:25:34 +03002014bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2015 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2016 const std::string &errorMessage,
2017 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002019 GLuint blockCount = 0;
2020 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002022 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002023 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002024 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002025 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002026 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002027 return false;
2028 }
2029 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002031 return true;
2032}
Jamie Madille473dee2015-08-18 14:49:01 -04002033
Martin Radev4c4c8e72016-08-04 12:25:34 +03002034bool Program::validateVertexAndFragmentInterfaceBlocks(
2035 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2036 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002037 InfoLog &infoLog,
2038 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002039{
2040 // Check that interface blocks defined in the vertex and fragment shaders are identical
2041 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2042 UniformBlockMap linkedUniformBlocks;
2043
2044 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2045 {
2046 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2047 }
2048
Jamie Madille473dee2015-08-18 14:49:01 -04002049 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 {
Jamie Madille473dee2015-08-18 14:49:01 -04002051 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002052 if (entry != linkedUniformBlocks.end())
2053 {
2054 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002055 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2056 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 {
2058 return false;
2059 }
2060 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002061 }
2062 return true;
2063}
Jamie Madille473dee2015-08-18 14:49:01 -04002064
Jamie Madillbd044ed2017-06-05 12:59:21 -04002065bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002066{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002067 const auto &caps = context->getCaps();
2068
Martin Radev4c4c8e72016-08-04 12:25:34 +03002069 if (mState.mAttachedComputeShader)
2070 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002071 Shader &computeShader = *mState.mAttachedComputeShader;
2072 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002073
2074 if (!validateUniformBlocksCount(
2075 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2076 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2077 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002079 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002081 return true;
2082 }
2083
Jamie Madillbd044ed2017-06-05 12:59:21 -04002084 Shader &vertexShader = *mState.mAttachedVertexShader;
2085 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002086
Jamie Madillbd044ed2017-06-05 12:59:21 -04002087 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2088 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002089
2090 if (!validateUniformBlocksCount(
2091 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2092 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2093 {
2094 return false;
2095 }
2096 if (!validateUniformBlocksCount(
2097 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2098 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2099 infoLog))
2100 {
2101
2102 return false;
2103 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002104
2105 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002106 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002107 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002108 {
2109 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 }
Jamie Madille473dee2015-08-18 14:49:01 -04002111
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 return true;
2113}
2114
Jamie Madilla2c74982016-12-12 11:20:42 -05002115bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002116 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002117 const sh::InterfaceBlock &fragmentInterfaceBlock,
2118 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119{
2120 const char* blockName = vertexInterfaceBlock.name.c_str();
2121 // validate blocks for the same member types
2122 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2123 {
Jamie Madillf6113162015-05-07 11:49:21 -04002124 infoLog << "Types for interface block '" << blockName
2125 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 return false;
2127 }
2128 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2129 {
Jamie Madillf6113162015-05-07 11:49:21 -04002130 infoLog << "Array sizes differ for interface block '" << blockName
2131 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132 return false;
2133 }
jchen10af713a22017-04-19 09:10:56 +08002134 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2135 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2136 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 {
Jamie Madillf6113162015-05-07 11:49:21 -04002138 infoLog << "Layout qualifiers differ for interface block '" << blockName
2139 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 return false;
2141 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002142 const unsigned int numBlockMembers =
2143 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2145 {
2146 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2147 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2148 if (vertexMember.name != fragmentMember.name)
2149 {
Jamie Madillf6113162015-05-07 11:49:21 -04002150 infoLog << "Name mismatch for field " << blockMemberIndex
2151 << " of interface block '" << blockName
2152 << "': (in vertex: '" << vertexMember.name
2153 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 return false;
2155 }
2156 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002157 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2158 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002159 {
2160 return false;
2161 }
2162 }
2163 return true;
2164}
2165
2166bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2167 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2168{
2169 if (vertexVariable.type != fragmentVariable.type)
2170 {
Jamie Madillf6113162015-05-07 11:49:21 -04002171 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 return false;
2173 }
2174 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2175 {
Jamie Madillf6113162015-05-07 11:49:21 -04002176 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177 return false;
2178 }
2179 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2180 {
Jamie Madillf6113162015-05-07 11:49:21 -04002181 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182 return false;
2183 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002184 if (vertexVariable.structName != fragmentVariable.structName)
2185 {
2186 infoLog << "Structure names for " << variableName
2187 << " differ between vertex and fragment shaders";
2188 return false;
2189 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190
2191 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2192 {
Jamie Madillf6113162015-05-07 11:49:21 -04002193 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002194 return false;
2195 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002196 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2198 {
2199 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2200 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2201
2202 if (vertexMember.name != fragmentMember.name)
2203 {
Jamie Madillf6113162015-05-07 11:49:21 -04002204 infoLog << "Name mismatch for field '" << memberIndex
2205 << "' of " << variableName
2206 << ": (in vertex: '" << vertexMember.name
2207 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 return false;
2209 }
2210
2211 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2212 vertexMember.name + "'";
2213
2214 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2215 {
2216 return false;
2217 }
2218 }
2219
2220 return true;
2221}
2222
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002223bool Program::linkValidateVaryings(InfoLog &infoLog,
2224 const std::string &varyingName,
2225 const sh::Varying &vertexVarying,
2226 const sh::Varying &fragmentVarying,
2227 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228{
2229 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2230 {
2231 return false;
2232 }
2233
Jamie Madille9cc4692015-02-19 16:00:13 -05002234 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002236 infoLog << "Interpolation types for " << varyingName
2237 << " differ between vertex and fragment shaders.";
2238 return false;
2239 }
2240
2241 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2242 {
2243 infoLog << "Invariance for " << varyingName
2244 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245 return false;
2246 }
2247
2248 return true;
2249}
2250
Jamie Madillbd044ed2017-06-05 12:59:21 -04002251bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002252{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002253 Shader *vertexShader = mState.mAttachedVertexShader;
2254 Shader *fragmentShader = mState.mAttachedFragmentShader;
2255 const auto &vertexVaryings = vertexShader->getVaryings(context);
2256 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2257 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002258
2259 if (shaderVersion != 100)
2260 {
2261 // Only ESSL 1.0 has restrictions on matching input and output invariance
2262 return true;
2263 }
2264
2265 bool glPositionIsInvariant = false;
2266 bool glPointSizeIsInvariant = false;
2267 bool glFragCoordIsInvariant = false;
2268 bool glPointCoordIsInvariant = false;
2269
2270 for (const sh::Varying &varying : vertexVaryings)
2271 {
2272 if (!varying.isBuiltIn())
2273 {
2274 continue;
2275 }
2276 if (varying.name.compare("gl_Position") == 0)
2277 {
2278 glPositionIsInvariant = varying.isInvariant;
2279 }
2280 else if (varying.name.compare("gl_PointSize") == 0)
2281 {
2282 glPointSizeIsInvariant = varying.isInvariant;
2283 }
2284 }
2285
2286 for (const sh::Varying &varying : fragmentVaryings)
2287 {
2288 if (!varying.isBuiltIn())
2289 {
2290 continue;
2291 }
2292 if (varying.name.compare("gl_FragCoord") == 0)
2293 {
2294 glFragCoordIsInvariant = varying.isInvariant;
2295 }
2296 else if (varying.name.compare("gl_PointCoord") == 0)
2297 {
2298 glPointCoordIsInvariant = varying.isInvariant;
2299 }
2300 }
2301
2302 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2303 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2304 // Not requiring invariance to match is supported by:
2305 // dEQP, WebGL CTS, Nexus 5X GLES
2306 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2307 {
2308 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2309 "declared invariant.";
2310 return false;
2311 }
2312 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2313 {
2314 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2315 "declared invariant.";
2316 return false;
2317 }
2318
2319 return true;
2320}
2321
jchen10a9042d32017-03-17 08:50:45 +08002322bool Program::linkValidateTransformFeedback(const gl::Context *context,
2323 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002324 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002325 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002326{
2327 size_t totalComponents = 0;
2328
Jamie Madillccdf74b2015-08-18 10:46:12 -04002329 std::set<std::string> uniqueNames;
2330
Jamie Madill48ef11b2016-04-27 15:21:52 -04002331 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002332 {
2333 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002334 size_t subscript = GL_INVALID_INDEX;
2335 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2336
Jamie Madill192745a2016-12-22 15:58:21 -05002337 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002338 {
Jamie Madill192745a2016-12-22 15:58:21 -05002339 const sh::Varying *varying = ref.second.get();
2340
jchen10a9042d32017-03-17 08:50:45 +08002341 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002342 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002343 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002344 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002345 infoLog << "Two transform feedback varyings specify the same output variable ("
2346 << tfVaryingName << ").";
2347 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002348 }
jchen10a9042d32017-03-17 08:50:45 +08002349 if (context->getClientVersion() >= Version(3, 1))
2350 {
2351 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2352 {
2353 infoLog
2354 << "Two transform feedback varyings include the same array element ("
2355 << tfVaryingName << ").";
2356 return false;
2357 }
2358 }
2359 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002360 {
2361 infoLog << "Capture of arrays is undefined and not supported.";
2362 return false;
2363 }
2364
jchen10a9042d32017-03-17 08:50:45 +08002365 uniqueNames.insert(tfVaryingName);
2366
Jamie Madillccdf74b2015-08-18 10:46:12 -04002367 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002368 size_t elementCount =
2369 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2370 : 1);
2371 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002372 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373 componentCount > caps.maxTransformFeedbackSeparateComponents)
2374 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002375 infoLog << "Transform feedback varying's " << varying->name << " components ("
2376 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002377 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002378 return false;
2379 }
2380
2381 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002382 found = true;
2383 break;
2384 }
2385 }
jchen10a9042d32017-03-17 08:50:45 +08002386 if (context->getClientVersion() < Version(3, 1) &&
2387 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002388 {
Geoff Lang1a683462015-09-29 15:09:59 -04002389 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002390 return false;
2391 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002392 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2393 ASSERT(found);
2394 }
2395
Jamie Madill48ef11b2016-04-27 15:21:52 -04002396 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002397 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398 {
Jamie Madillf6113162015-05-07 11:49:21 -04002399 infoLog << "Transform feedback varying total components (" << totalComponents
2400 << ") exceed the maximum interleaved components ("
2401 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002402 return false;
2403 }
2404
2405 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002406}
2407
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002408bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2409{
2410 const std::vector<sh::Uniform> &vertexUniforms =
2411 mState.mAttachedVertexShader->getUniforms(context);
2412 const std::vector<sh::Uniform> &fragmentUniforms =
2413 mState.mAttachedFragmentShader->getUniforms(context);
2414 const std::vector<sh::Attribute> &attributes =
2415 mState.mAttachedVertexShader->getActiveAttributes(context);
2416 for (const auto &attrib : attributes)
2417 {
2418 for (const auto &uniform : vertexUniforms)
2419 {
2420 if (uniform.name == attrib.name)
2421 {
2422 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2423 return false;
2424 }
2425 }
2426 for (const auto &uniform : fragmentUniforms)
2427 {
2428 if (uniform.name == attrib.name)
2429 {
2430 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2431 return false;
2432 }
2433 }
2434 }
2435 return true;
2436}
2437
Jamie Madill192745a2016-12-22 15:58:21 -05002438void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002439{
2440 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002441 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002442 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002443 {
jchen10a9042d32017-03-17 08:50:45 +08002444 size_t subscript = GL_INVALID_INDEX;
2445 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002446 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002447 {
Jamie Madill192745a2016-12-22 15:58:21 -05002448 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002449 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002450 {
jchen10a9042d32017-03-17 08:50:45 +08002451 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2452 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453 break;
2454 }
2455 }
2456 }
2457}
2458
Jamie Madillbd044ed2017-06-05 12:59:21 -04002459Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002460{
Jamie Madill192745a2016-12-22 15:58:21 -05002461 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002462
Jamie Madillbd044ed2017-06-05 12:59:21 -04002463 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464 {
Jamie Madill192745a2016-12-22 15:58:21 -05002465 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002466 }
2467
Jamie Madillbd044ed2017-06-05 12:59:21 -04002468 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002469 {
Jamie Madill192745a2016-12-22 15:58:21 -05002470 merged[varying.name].fragment = &varying;
2471 }
2472
2473 return merged;
2474}
2475
2476std::vector<PackedVarying> Program::getPackedVaryings(
2477 const Program::MergedVaryings &mergedVaryings) const
2478{
2479 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2480 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002481 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002482
2483 for (const auto &ref : mergedVaryings)
2484 {
2485 const sh::Varying *input = ref.second.vertex;
2486 const sh::Varying *output = ref.second.fragment;
2487
2488 // Only pack varyings that have a matched input or output, plus special builtins.
2489 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002490 {
Jamie Madill192745a2016-12-22 15:58:21 -05002491 // Will get the vertex shader interpolation by default.
2492 auto interpolation = ref.second.get()->interpolation;
2493
Olli Etuaho06a06f52017-07-12 12:22:15 +03002494 // Note that we lose the vertex shader static use information here. The data for the
2495 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002496 if (output->isStruct())
2497 {
2498 ASSERT(!output->isArray());
2499 for (const auto &field : output->fields)
2500 {
2501 ASSERT(!field.isStruct() && !field.isArray());
2502 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2503 }
2504 }
2505 else
2506 {
2507 packedVaryings.push_back(PackedVarying(*output, interpolation));
2508 }
2509 continue;
2510 }
2511
2512 // Keep Transform FB varyings in the merged list always.
2513 if (!input)
2514 {
2515 continue;
2516 }
2517
2518 for (const std::string &tfVarying : tfVaryings)
2519 {
jchen10a9042d32017-03-17 08:50:45 +08002520 size_t subscript = GL_INVALID_INDEX;
2521 std::string baseName = ParseResourceName(tfVarying, &subscript);
2522 if (uniqueFullNames.count(tfVarying) > 0)
2523 {
2524 continue;
2525 }
2526 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002527 {
2528 // Transform feedback for varying structs is underspecified.
2529 // See Khronos bug 9856.
2530 // TODO(jmadill): Figure out how to be spec-compliant here.
2531 if (!input->isStruct())
2532 {
2533 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2534 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002535 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2536 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002537 }
jchen10a9042d32017-03-17 08:50:45 +08002538 if (subscript == GL_INVALID_INDEX)
2539 {
2540 break;
2541 }
Jamie Madill192745a2016-12-22 15:58:21 -05002542 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002543 }
2544 }
2545
Jamie Madill192745a2016-12-22 15:58:21 -05002546 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2547
2548 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002549}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002550
Jamie Madillbd044ed2017-06-05 12:59:21 -04002551void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002552{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002553 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002554 ASSERT(fragmentShader != nullptr);
2555
Geoff Lange0cff192017-05-30 13:04:56 -04002556 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002557 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002558
2559 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002560 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002561 {
2562 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2563 outputVariable.name != "gl_FragData")
2564 {
2565 continue;
2566 }
2567
2568 unsigned int baseLocation =
2569 (outputVariable.location == -1 ? 0u
2570 : static_cast<unsigned int>(outputVariable.location));
2571 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2572 elementIndex++)
2573 {
2574 const unsigned int location = baseLocation + elementIndex;
2575 if (location >= mState.mOutputVariableTypes.size())
2576 {
2577 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2578 }
Corentin Walleze7557742017-06-01 13:09:57 -04002579 ASSERT(location < mState.mActiveOutputVariables.size());
2580 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002581 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2582 }
2583 }
2584
Jamie Madill80a6fc02015-08-21 16:53:16 -04002585 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002586 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002587 return;
2588
Jamie Madillbd044ed2017-06-05 12:59:21 -04002589 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002590 // TODO(jmadill): any caps validation here?
2591
jchen1015015f72017-03-16 13:54:21 +08002592 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002593 outputVariableIndex++)
2594 {
jchen1015015f72017-03-16 13:54:21 +08002595 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002596
2597 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2598 if (outputVariable.isBuiltIn())
2599 continue;
2600
2601 // Since multiple output locations must be specified, use 0 for non-specified locations.
2602 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2603
Jamie Madill80a6fc02015-08-21 16:53:16 -04002604 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2605 elementIndex++)
2606 {
2607 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002608 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002609 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002610 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002611 VariableLocation(outputVariable.name, element, outputVariableIndex);
2612 }
2613 }
2614}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002615
Olli Etuaho48fed632017-03-16 12:05:30 +00002616void Program::setUniformValuesFromBindingQualifiers()
2617{
Jamie Madill982f6e02017-06-07 14:33:04 -04002618 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002619 {
2620 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2621 if (samplerUniform.binding != -1)
2622 {
2623 GLint location = mState.getUniformLocation(samplerUniform.name);
2624 ASSERT(location != -1);
2625 std::vector<GLint> boundTextureUnits;
2626 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2627 ++elementIndex)
2628 {
2629 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2630 }
2631 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2632 boundTextureUnits.data());
2633 }
2634 }
2635}
2636
jchen10eaef1e52017-06-13 10:44:11 +08002637void Program::gatherAtomicCounterBuffers()
2638{
2639 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2640 // counter.
2641 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2642}
2643
Jamie Madillbd044ed2017-06-05 12:59:21 -04002644void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002645{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002646 ASSERT(mState.mUniformBlocks.empty());
2647
2648 if (mState.mAttachedComputeShader)
2649 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002650 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002651
Jamie Madillbd044ed2017-06-05 12:59:21 -04002652 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002653 {
2654
2655 // Only 'packed' blocks are allowed to be considered inactive.
2656 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2657 continue;
2658
Jamie Madilla2c74982016-12-12 11:20:42 -05002659 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002660 {
2661 if (block.name == computeBlock.name)
2662 {
2663 block.computeStaticUse = computeBlock.staticUse;
2664 }
2665 }
2666
2667 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2668 }
2669 return;
2670 }
2671
Jamie Madill62d31cb2015-09-11 13:25:51 -04002672 std::set<std::string> visitedList;
2673
Jamie Madillbd044ed2017-06-05 12:59:21 -04002674 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002675
Jamie Madillbd044ed2017-06-05 12:59:21 -04002676 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002677 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002678 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002679 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2680 continue;
2681
2682 if (visitedList.count(vertexBlock.name) > 0)
2683 continue;
2684
2685 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2686 visitedList.insert(vertexBlock.name);
2687 }
2688
Jamie Madillbd044ed2017-06-05 12:59:21 -04002689 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002690
Jamie Madillbd044ed2017-06-05 12:59:21 -04002691 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002692 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002693 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002694 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2695 continue;
2696
2697 if (visitedList.count(fragmentBlock.name) > 0)
2698 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002699 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002700 {
2701 if (block.name == fragmentBlock.name)
2702 {
2703 block.fragmentStaticUse = fragmentBlock.staticUse;
2704 }
2705 }
2706
2707 continue;
2708 }
2709
2710 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2711 visitedList.insert(fragmentBlock.name);
2712 }
jchen10af713a22017-04-19 09:10:56 +08002713 // Set initial bindings from shader.
2714 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2715 {
2716 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2717 bindUniformBlock(blockIndex, uniformBlock.binding);
2718 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002719}
2720
Jamie Madill4a3c2342015-10-08 12:58:45 -04002721template <typename VarT>
2722void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2723 const std::string &prefix,
2724 int blockIndex)
2725{
2726 for (const VarT &field : fields)
2727 {
2728 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2729
2730 if (field.isStruct())
2731 {
2732 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2733 {
2734 const std::string uniformElementName =
2735 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2736 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2737 }
2738 }
2739 else
2740 {
2741 // If getBlockMemberInfo returns false, the uniform is optimized out.
2742 sh::BlockMemberInfo memberInfo;
2743 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2744 {
2745 continue;
2746 }
2747
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002748 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002749 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002750
2751 // Since block uniforms have no location, we don't need to store them in the uniform
2752 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002753 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002754 }
2755 }
2756}
2757
Jamie Madill62d31cb2015-09-11 13:25:51 -04002758void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2759{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002760 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002761 size_t blockSize = 0;
2762
Jamie Madill4a3c2342015-10-08 12:58:45 -04002763 // Track the first and last uniform index to determine the range of active uniforms in the
2764 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002765 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002766 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002767 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002768
2769 std::vector<unsigned int> blockUniformIndexes;
2770 for (size_t blockUniformIndex = firstBlockUniformIndex;
2771 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2772 {
2773 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2774 }
jchen10af713a22017-04-19 09:10:56 +08002775 // ESSL 3.10 section 4.4.4 page 58:
2776 // Any uniform or shader storage block declared without a binding qualifier is initially
2777 // assigned to block binding point zero.
2778 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002779 if (interfaceBlock.arraySize > 0)
2780 {
2781 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2782 {
jchen10af713a22017-04-19 09:10:56 +08002783 // Don't define this block at all if it's not active in the implementation.
2784 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2785 &blockSize))
2786 {
2787 continue;
2788 }
2789 UniformBlock block(interfaceBlock.name, true, arrayElement,
2790 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002791 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002792
Martin Radev4c4c8e72016-08-04 12:25:34 +03002793 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002794 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002795 case GL_VERTEX_SHADER:
2796 {
2797 block.vertexStaticUse = interfaceBlock.staticUse;
2798 break;
2799 }
2800 case GL_FRAGMENT_SHADER:
2801 {
2802 block.fragmentStaticUse = interfaceBlock.staticUse;
2803 break;
2804 }
2805 case GL_COMPUTE_SHADER:
2806 {
2807 block.computeStaticUse = interfaceBlock.staticUse;
2808 break;
2809 }
2810 default:
2811 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002812 }
2813
Qin Jiajia0350a642016-11-01 17:01:51 +08002814 // Since all block elements in an array share the same active uniforms, they will all be
2815 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2816 // here we will add every block element in the array.
2817 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002818 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 }
2820 }
2821 else
2822 {
jchen10af713a22017-04-19 09:10:56 +08002823 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2824 {
2825 return;
2826 }
2827 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002828 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829
Martin Radev4c4c8e72016-08-04 12:25:34 +03002830 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002831 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002832 case GL_VERTEX_SHADER:
2833 {
2834 block.vertexStaticUse = interfaceBlock.staticUse;
2835 break;
2836 }
2837 case GL_FRAGMENT_SHADER:
2838 {
2839 block.fragmentStaticUse = interfaceBlock.staticUse;
2840 break;
2841 }
2842 case GL_COMPUTE_SHADER:
2843 {
2844 block.computeStaticUse = interfaceBlock.staticUse;
2845 break;
2846 }
2847 default:
2848 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002849 }
2850
Jamie Madill4a3c2342015-10-08 12:58:45 -04002851 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002852 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002853 }
2854}
2855
Jamie Madille7d84322017-01-10 18:21:59 -05002856template <>
2857void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2858 const uint8_t *destPointer,
2859 GLsizei clampedCount,
2860 const GLint *v)
2861{
2862 // Invalidate the validation cache only if we modify the sampler data.
2863 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2864 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2865 {
2866 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2867 std::vector<GLuint> *boundTextureUnits =
2868 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2869
2870 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2871 mCachedValidateSamplersResult.reset();
2872 }
2873}
2874
2875template <typename T>
2876void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2877 const uint8_t *destPointer,
2878 GLsizei clampedCount,
2879 const T *v)
2880{
2881}
2882
Jamie Madill62d31cb2015-09-11 13:25:51 -04002883template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002884GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002885{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002886 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2887 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002888 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2889
Corentin Wallez15ac5342016-11-03 17:06:39 -04002890 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2891 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2892 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002893 GLsizei maxElementCount =
2894 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2895
2896 GLsizei count = countIn;
2897 GLsizei clampedCount = count * vectorSize;
2898 if (clampedCount > maxElementCount)
2899 {
2900 clampedCount = maxElementCount;
2901 count = maxElementCount / vectorSize;
2902 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002903
Jamie Madill44183cc2017-08-01 12:48:34 -04002904 // VariableComponentType(linkedUniform->type) has a dozens of compares and thus is evil for
2905 // inlining with regards to code size. This version is one subtract and one compare only.
2906 if (IsVariableComponentTypeBool(linkedUniform->type))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002907 {
2908 // Do a cast conversion for boolean types. From the spec:
2909 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2910 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002911 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002912 {
2913 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2914 }
2915 }
2916 else
2917 {
Jamie Madille7d84322017-01-10 18:21:59 -05002918 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002919 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002920 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002921
2922 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002923}
2924
2925template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002926GLsizei Program::setMatrixUniformInternal(GLint location,
2927 GLsizei count,
2928 GLboolean transpose,
2929 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002930{
2931 if (!transpose)
2932 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002933 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002934 }
2935
2936 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002937 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2938 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002939 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002940
2941 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2942 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2943 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2944 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2945
2946 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002947 {
2948 size_t elementOffset = element * rows * cols;
2949
2950 for (size_t row = 0; row < rows; ++row)
2951 {
2952 for (size_t col = 0; col < cols; ++col)
2953 {
2954 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2955 }
2956 }
2957 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002958
2959 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960}
2961
2962template <typename DestT>
2963void Program::getUniformInternal(GLint location, DestT *dataOut) const
2964{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002965 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2966 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002967
2968 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2969
2970 GLenum componentType = VariableComponentType(uniform.type);
2971 if (componentType == GLTypeToGLenum<DestT>::value)
2972 {
2973 memcpy(dataOut, srcPointer, uniform.getElementSize());
2974 return;
2975 }
2976
Corentin Wallez6596c462016-03-17 17:26:58 -04002977 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002978
2979 switch (componentType)
2980 {
2981 case GL_INT:
2982 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2983 break;
2984 case GL_UNSIGNED_INT:
2985 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2986 break;
2987 case GL_BOOL:
2988 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2989 break;
2990 case GL_FLOAT:
2991 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2992 break;
2993 default:
2994 UNREACHABLE();
2995 }
2996}
Jamie Madilla4595b82017-01-11 17:36:34 -05002997
2998bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2999{
3000 // Must be called after samplers are validated.
3001 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3002
3003 for (const auto &binding : mState.mSamplerBindings)
3004 {
3005 GLenum textureType = binding.textureType;
3006 for (const auto &unit : binding.boundTextureUnits)
3007 {
3008 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3009 if (programTextureID == textureID)
3010 {
3011 // TODO(jmadill): Check for appropriate overlap.
3012 return true;
3013 }
3014 }
3015 }
3016
3017 return false;
3018}
3019
Jamie Madilla2c74982016-12-12 11:20:42 -05003020} // namespace gl