blob: 6c101e2545b276715703e8174f96202c59af30b2 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050019#include "libANGLE/Context.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040020#include "libANGLE/MemoryProgramCache.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040022#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000023#include "libANGLE/UniformLinker.h"
Jamie Madill4f86d052017-06-05 12:59:26 -040024#include "libANGLE/VaryingPacking.h"
25#include "libANGLE/features.h"
26#include "libANGLE/queryconversions.h"
27#include "libANGLE/renderer/GLImplFactory.h"
28#include "libANGLE/renderer/ProgramImpl.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000032
Geoff Lang7dd2e102014-11-10 15:19:26 -050033namespace
34{
35
Jamie Madill62d31cb2015-09-11 13:25:51 -040036// This simplified cast function doesn't need to worry about advanced concepts like
37// depth range values, or casting to bool.
38template <typename DestT, typename SrcT>
39DestT UniformStateQueryCast(SrcT value);
40
41// From-Float-To-Integer Casts
42template <>
43GLint UniformStateQueryCast(GLfloat value)
44{
45 return clampCast<GLint>(roundf(value));
46}
47
48template <>
49GLuint UniformStateQueryCast(GLfloat value)
50{
51 return clampCast<GLuint>(roundf(value));
52}
53
54// From-Integer-to-Integer Casts
55template <>
56GLint UniformStateQueryCast(GLuint value)
57{
58 return clampCast<GLint>(value);
59}
60
61template <>
62GLuint UniformStateQueryCast(GLint value)
63{
64 return clampCast<GLuint>(value);
65}
66
67// From-Boolean-to-Anything Casts
68template <>
69GLfloat UniformStateQueryCast(GLboolean value)
70{
71 return (value == GL_TRUE ? 1.0f : 0.0f);
72}
73
74template <>
75GLint UniformStateQueryCast(GLboolean value)
76{
77 return (value == GL_TRUE ? 1 : 0);
78}
79
80template <>
81GLuint UniformStateQueryCast(GLboolean value)
82{
83 return (value == GL_TRUE ? 1u : 0u);
84}
85
86// Default to static_cast
87template <typename DestT, typename SrcT>
88DestT UniformStateQueryCast(SrcT value)
89{
90 return static_cast<DestT>(value);
91}
92
93template <typename SrcT, typename DestT>
94void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
95{
96 for (int comp = 0; comp < components; ++comp)
97 {
98 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
99 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
100 size_t offset = comp * 4;
101 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
102 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
103 }
104}
105
Jamie Madill192745a2016-12-22 15:58:21 -0500106// true if varying x has a higher priority in packing than y
107bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
108{
jchen10a9042d32017-03-17 08:50:45 +0800109 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
110 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
111 sh::ShaderVariable vx, vy;
112 const sh::ShaderVariable *px, *py;
113 if (x.isArrayElement())
114 {
115 vx = *x.varying;
116 vx.arraySize = 0;
117 px = &vx;
118 }
119 else
120 {
121 px = x.varying;
122 }
123
124 if (y.isArrayElement())
125 {
126 vy = *y.varying;
127 vy.arraySize = 0;
128 py = &vy;
129 }
130 else
131 {
132 py = y.varying;
133 }
134
135 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500136}
137
jchen1015015f72017-03-16 13:54:21 +0800138template <typename VarT>
139GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
140{
141 size_t subscript = GL_INVALID_INDEX;
142 std::string baseName = ParseResourceName(name, &subscript);
143
144 // The app is not allowed to specify array indices other than 0 for arrays of basic types
145 if (subscript != 0 && subscript != GL_INVALID_INDEX)
146 {
147 return GL_INVALID_INDEX;
148 }
149
150 for (size_t index = 0; index < list.size(); index++)
151 {
152 const VarT &resource = list[index];
153 if (resource.name == baseName)
154 {
155 if (resource.isArray() || subscript == GL_INVALID_INDEX)
156 {
157 return static_cast<GLuint>(index);
158 }
159 }
160 }
161
162 return GL_INVALID_INDEX;
163}
164
jchen10fd7c3b52017-03-21 15:36:03 +0800165void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
166{
167 ASSERT(bufSize > 0);
168 strncpy(buffer, string.c_str(), bufSize);
169 buffer[bufSize - 1] = '\0';
170
171 if (length)
172 {
173 *length = static_cast<GLsizei>(strlen(buffer));
174 }
175}
176
jchen10a9042d32017-03-17 08:50:45 +0800177bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
178{
179 size_t subscript = GL_INVALID_INDEX;
180 std::string baseName = ParseResourceName(name, &subscript);
181 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
182 {
183 size_t arrayIndex = GL_INVALID_INDEX;
184 std::string arrayName = ParseResourceName(*it, &arrayIndex);
185 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
186 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
187 {
188 return true;
189 }
190 }
191 return false;
192}
193
Jamie Madill62d31cb2015-09-11 13:25:51 -0400194} // anonymous namespace
195
Jamie Madill4a3c2342015-10-08 12:58:45 -0400196const char *const g_fakepath = "C:\\fakepath";
197
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400198InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199{
200}
201
202InfoLog::~InfoLog()
203{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204}
205
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400206size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208 const std::string &logString = mStream.str();
209 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000210}
211
Geoff Lange1a27752015-10-05 13:16:04 -0400212void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000213{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400214 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000215
216 if (bufSize > 0)
217 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400218 const std::string str(mStream.str());
219
220 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000221 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400222 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
223 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000224 }
225
226 infoLog[index] = '\0';
227 }
228
229 if (length)
230 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400231 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000232 }
233}
234
235// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300236// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000237// messages, so lets remove all occurrences of this fake file path from the log.
238void InfoLog::appendSanitized(const char *message)
239{
240 std::string msg(message);
241
242 size_t found;
243 do
244 {
245 found = msg.find(g_fakepath);
246 if (found != std::string::npos)
247 {
248 msg.erase(found, strlen(g_fakepath));
249 }
250 }
251 while (found != std::string::npos);
252
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400253 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000254}
255
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000256void InfoLog::reset()
257{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000258}
259
Geoff Langd8605522016-04-13 10:19:12 -0400260VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000261{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500262}
263
Geoff Langd8605522016-04-13 10:19:12 -0400264VariableLocation::VariableLocation(const std::string &name,
265 unsigned int element,
266 unsigned int index)
267 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500268{
269}
270
Geoff Langd8605522016-04-13 10:19:12 -0400271void Program::Bindings::bindLocation(GLuint index, const std::string &name)
272{
273 mBindings[name] = index;
274}
275
276int Program::Bindings::getBinding(const std::string &name) const
277{
278 auto iter = mBindings.find(name);
279 return (iter != mBindings.end()) ? iter->second : -1;
280}
281
282Program::Bindings::const_iterator Program::Bindings::begin() const
283{
284 return mBindings.begin();
285}
286
287Program::Bindings::const_iterator Program::Bindings::end() const
288{
289 return mBindings.end();
290}
291
Jamie Madill48ef11b2016-04-27 15:21:52 -0400292ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500293 : mLabel(),
294 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400295 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300296 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500297 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500298 mSamplerUniformRange(0, 0),
jchen10eaef1e52017-06-13 10:44:11 +0800299 mImageUniformRange(0, 0),
300 mAtomicCounterUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500301 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400302{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300303 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400304}
305
Jamie Madill48ef11b2016-04-27 15:21:52 -0400306ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400307{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500308 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400309}
310
Jamie Madill48ef11b2016-04-27 15:21:52 -0400311const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500312{
313 return mLabel;
314}
315
Jamie Madill48ef11b2016-04-27 15:21:52 -0400316GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400317{
318 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800319 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400320
321 for (size_t location = 0; location < mUniformLocations.size(); ++location)
322 {
323 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400324 if (!uniformLocation.used)
325 {
326 continue;
327 }
328
329 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400330
331 if (uniform.name == baseName)
332 {
Geoff Langd8605522016-04-13 10:19:12 -0400333 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400334 {
Geoff Langd8605522016-04-13 10:19:12 -0400335 if (uniformLocation.element == subscript ||
336 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
337 {
338 return static_cast<GLint>(location);
339 }
340 }
341 else
342 {
343 if (subscript == GL_INVALID_INDEX)
344 {
345 return static_cast<GLint>(location);
346 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400347 }
348 }
349 }
350
351 return -1;
352}
353
Jamie Madille7d84322017-01-10 18:21:59 -0500354GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400355{
jchen1015015f72017-03-16 13:54:21 +0800356 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400357}
358
Jamie Madille7d84322017-01-10 18:21:59 -0500359GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
360{
361 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
362 return mUniformLocations[location].index;
363}
364
365Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
366{
367 GLuint index = getUniformIndexFromLocation(location);
368 if (!isSamplerUniformIndex(index))
369 {
370 return Optional<GLuint>::Invalid();
371 }
372
373 return getSamplerIndexFromUniformIndex(index);
374}
375
376bool ProgramState::isSamplerUniformIndex(GLuint index) const
377{
Jamie Madill982f6e02017-06-07 14:33:04 -0400378 return mSamplerUniformRange.contains(index);
Jamie Madille7d84322017-01-10 18:21:59 -0500379}
380
381GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
382{
383 ASSERT(isSamplerUniformIndex(uniformIndex));
Jamie Madill982f6e02017-06-07 14:33:04 -0400384 return uniformIndex - mSamplerUniformRange.low();
Jamie Madille7d84322017-01-10 18:21:59 -0500385}
386
Jamie Madill34ca4f52017-06-13 11:49:39 -0400387GLuint ProgramState::getAttributeLocation(const std::string &name) const
388{
389 for (const sh::Attribute &attribute : mAttributes)
390 {
391 if (attribute.name == name)
392 {
393 return attribute.location;
394 }
395 }
396
397 return static_cast<GLuint>(-1);
398}
399
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500400Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400401 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400402 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500403 mLinked(false),
404 mDeleteStatus(false),
405 mRefCount(0),
406 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500407 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500408{
409 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000410
Geoff Lang7dd2e102014-11-10 15:19:26 -0500411 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412}
413
414Program::~Program()
415{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400416 ASSERT(!mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417}
418
Jamie Madill4928b7c2017-06-20 12:57:39 -0400419void Program::onDestroy(const Context *context)
Jamie Madill6c1f6712017-02-14 19:08:04 -0500420{
421 if (mState.mAttachedVertexShader != nullptr)
422 {
423 mState.mAttachedVertexShader->release(context);
424 mState.mAttachedVertexShader = nullptr;
425 }
426
427 if (mState.mAttachedFragmentShader != nullptr)
428 {
429 mState.mAttachedFragmentShader->release(context);
430 mState.mAttachedFragmentShader = nullptr;
431 }
432
433 if (mState.mAttachedComputeShader != nullptr)
434 {
435 mState.mAttachedComputeShader->release(context);
436 mState.mAttachedComputeShader = nullptr;
437 }
438
Jamie Madillc564c072017-06-01 12:45:42 -0400439 mProgram->destroy(context);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400440
441 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
442 !mState.mAttachedComputeShader);
443 SafeDelete(mProgram);
444
445 delete this;
Jamie Madill6c1f6712017-02-14 19:08:04 -0500446}
447
Geoff Lang70d0f492015-12-10 17:45:46 -0500448void Program::setLabel(const std::string &label)
449{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400450 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500451}
452
453const std::string &Program::getLabel() const
454{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400455 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500456}
457
Jamie Madillef300b12016-10-07 15:12:09 -0400458void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300460 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300462 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463 {
Jamie Madillef300b12016-10-07 15:12:09 -0400464 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300465 mState.mAttachedVertexShader = shader;
466 mState.mAttachedVertexShader->addRef();
467 break;
468 }
469 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470 {
Jamie Madillef300b12016-10-07 15:12:09 -0400471 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300472 mState.mAttachedFragmentShader = shader;
473 mState.mAttachedFragmentShader->addRef();
474 break;
475 }
476 case GL_COMPUTE_SHADER:
477 {
Jamie Madillef300b12016-10-07 15:12:09 -0400478 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300479 mState.mAttachedComputeShader = shader;
480 mState.mAttachedComputeShader->addRef();
481 break;
482 }
483 default:
484 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
Jamie Madillc1d770e2017-04-13 17:31:24 -0400488void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300490 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300492 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400494 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500495 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300496 mState.mAttachedVertexShader = nullptr;
497 break;
498 }
499 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400501 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500502 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300503 mState.mAttachedFragmentShader = nullptr;
504 break;
505 }
506 case GL_COMPUTE_SHADER:
507 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400508 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500509 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300510 mState.mAttachedComputeShader = nullptr;
511 break;
512 }
513 default:
514 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516}
517
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000518int Program::getAttachedShadersCount() const
519{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300520 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
521 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000522}
523
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524void Program::bindAttributeLocation(GLuint index, const char *name)
525{
Geoff Langd8605522016-04-13 10:19:12 -0400526 mAttributeBindings.bindLocation(index, name);
527}
528
529void Program::bindUniformLocation(GLuint index, const char *name)
530{
531 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800532 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533}
534
Sami Väisänen46eaa942016-06-29 10:26:37 +0300535void Program::bindFragmentInputLocation(GLint index, const char *name)
536{
537 mFragmentInputBindings.bindLocation(index, name);
538}
539
Jamie Madillbd044ed2017-06-05 12:59:21 -0400540BindingInfo Program::getFragmentInputBindingInfo(const Context *context, GLint index) const
Sami Väisänen46eaa942016-06-29 10:26:37 +0300541{
542 BindingInfo ret;
543 ret.type = GL_NONE;
544 ret.valid = false;
545
Jamie Madillbd044ed2017-06-05 12:59:21 -0400546 Shader *fragmentShader = mState.getAttachedFragmentShader();
Sami Väisänen46eaa942016-06-29 10:26:37 +0300547 ASSERT(fragmentShader);
548
549 // Find the actual fragment shader varying we're interested in
Jamie Madillbd044ed2017-06-05 12:59:21 -0400550 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings(context);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300551
552 for (const auto &binding : mFragmentInputBindings)
553 {
554 if (binding.second != static_cast<GLuint>(index))
555 continue;
556
557 ret.valid = true;
558
559 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400560 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300561
562 for (const auto &in : inputs)
563 {
564 if (in.name == originalName)
565 {
566 if (in.isArray())
567 {
568 // The client wants to bind either "name" or "name[0]".
569 // GL ES 3.1 spec refers to active array names with language such as:
570 // "if the string identifies the base name of an active array, where the
571 // string would exactly match the name of the variable if the suffix "[0]"
572 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400573 if (arrayIndex == GL_INVALID_INDEX)
574 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300575
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400576 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300577 }
578 else
579 {
580 ret.name = in.mappedName;
581 }
582 ret.type = in.type;
583 return ret;
584 }
585 }
586 }
587
588 return ret;
589}
590
Jamie Madillbd044ed2017-06-05 12:59:21 -0400591void Program::pathFragmentInputGen(const Context *context,
592 GLint index,
Sami Väisänen46eaa942016-06-29 10:26:37 +0300593 GLenum genMode,
594 GLint components,
595 const GLfloat *coeffs)
596{
597 // If the location is -1 then the command is silently ignored
598 if (index == -1)
599 return;
600
Jamie Madillbd044ed2017-06-05 12:59:21 -0400601 const auto &binding = getFragmentInputBindingInfo(context, index);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300602
603 // If the input doesn't exist then then the command is silently ignored
604 // This could happen through optimization for example, the shader translator
605 // decides that a variable is not actually being used and optimizes it away.
606 if (binding.name.empty())
607 return;
608
609 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
610}
611
Martin Radev4c4c8e72016-08-04 12:25:34 +0300612// The attached shaders are checked for linking errors by matching up their variables.
613// Uniform, input and output variables get collected.
614// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500615Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000616{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500617 const auto &data = context->getContextState();
618
Jamie Madill6c1f6712017-02-14 19:08:04 -0500619 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000620
Jamie Madill32447362017-06-28 14:53:52 -0400621 ProgramHash programHash;
622 auto *cache = context->getMemoryProgramCache();
623 if (cache)
624 {
625 ANGLE_TRY_RESULT(cache->getProgram(context, this, &mState, &programHash), mLinked);
626 }
627
628 if (mLinked)
629 {
630 return NoError();
631 }
632
633 // Cache load failed, fall through to normal linking.
634 unlink();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000635 mInfoLog.reset();
636
Martin Radev4c4c8e72016-08-04 12:25:34 +0300637 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500638
Jamie Madill192745a2016-12-22 15:58:21 -0500639 auto vertexShader = mState.mAttachedVertexShader;
640 auto fragmentShader = mState.mAttachedFragmentShader;
641 auto computeShader = mState.mAttachedComputeShader;
642
643 bool isComputeShaderAttached = (computeShader != nullptr);
644 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300645 // Check whether we both have a compute and non-compute shaders attached.
646 // If there are of both types attached, then linking should fail.
647 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
648 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500649 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300650 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
651 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400652 }
653
Jamie Madill192745a2016-12-22 15:58:21 -0500654 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500655 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400656 if (!computeShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300657 {
658 mInfoLog << "Attached compute shader is not compiled.";
659 return NoError();
660 }
Jamie Madill192745a2016-12-22 15:58:21 -0500661 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662
Jamie Madillbd044ed2017-06-05 12:59:21 -0400663 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300664
665 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
666 // If the work group size is not specified, a link time error should occur.
667 if (!mState.mComputeShaderLocalSize.isDeclared())
668 {
669 mInfoLog << "Work group size is not specified.";
670 return NoError();
671 }
672
Jamie Madillbd044ed2017-06-05 12:59:21 -0400673 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300674 {
675 return NoError();
676 }
677
Jamie Madillbd044ed2017-06-05 12:59:21 -0400678 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679 {
680 return NoError();
681 }
682
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500683 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
Jamie Madillc564c072017-06-01 12:45:42 -0400684 ANGLE_TRY_RESULT(mProgram->link(context, noPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500685 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300686 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500687 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300688 }
689 }
690 else
691 {
Jamie Madillbd044ed2017-06-05 12:59:21 -0400692 if (!fragmentShader || !fragmentShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300693 {
694 return NoError();
695 }
Jamie Madill192745a2016-12-22 15:58:21 -0500696 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300697
Jamie Madillbd044ed2017-06-05 12:59:21 -0400698 if (!vertexShader || !vertexShader->isCompiled(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300699 {
700 return NoError();
701 }
Jamie Madill192745a2016-12-22 15:58:21 -0500702 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703
Jamie Madillbd044ed2017-06-05 12:59:21 -0400704 if (fragmentShader->getShaderVersion(context) != vertexShader->getShaderVersion(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300705 {
706 mInfoLog << "Fragment shader version does not match vertex shader version.";
707 return NoError();
708 }
709
Jamie Madillbd044ed2017-06-05 12:59:21 -0400710 if (!linkAttributes(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300711 {
712 return NoError();
713 }
714
Jamie Madillbd044ed2017-06-05 12:59:21 -0400715 if (!linkVaryings(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300716 {
717 return NoError();
718 }
719
Jamie Madillbd044ed2017-06-05 12:59:21 -0400720 if (!linkUniforms(context, mInfoLog, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300721 {
722 return NoError();
723 }
724
Jamie Madillbd044ed2017-06-05 12:59:21 -0400725 if (!linkUniformBlocks(context, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300726 {
727 return NoError();
728 }
729
Yuly Novikovcaa5cda2017-06-15 21:14:03 -0400730 if (!linkValidateGlobalNames(context, mInfoLog))
731 {
732 return NoError();
733 }
734
Jamie Madillbd044ed2017-06-05 12:59:21 -0400735 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300736
jchen10a9042d32017-03-17 08:50:45 +0800737 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300738 {
739 return NoError();
740 }
741
Jamie Madillbd044ed2017-06-05 12:59:21 -0400742 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300743
Jamie Madill192745a2016-12-22 15:58:21 -0500744 // Validate we can pack the varyings.
745 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
746
747 // Map the varyings to the register file
748 // In WebGL, we use a slightly different handling for packing variables.
749 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
750 : PackMode::ANGLE_RELAXED;
751 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
752 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
753 mState.getTransformFeedbackVaryingNames()))
754 {
755 return NoError();
756 }
757
Jamie Madillc564c072017-06-01 12:45:42 -0400758 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500759 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300760 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500761 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300762 }
763
764 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500765 }
766
jchen10eaef1e52017-06-13 10:44:11 +0800767 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400768 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400769
jchen10eaef1e52017-06-13 10:44:11 +0800770 setUniformValuesFromBindingQualifiers();
771
Jamie Madill32447362017-06-28 14:53:52 -0400772 // Save to the program cache.
773 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
774 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
775 {
776 cache->putProgram(programHash, context, this);
777 }
778
Martin Radev4c4c8e72016-08-04 12:25:34 +0300779 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000780}
781
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000782// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500783void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400785 mState.mAttributes.clear();
786 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800787 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400788 mState.mUniforms.clear();
789 mState.mUniformLocations.clear();
790 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800791 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800792 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400793 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800794 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400795 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400796 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300797 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500798 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800799 mState.mImageBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500800
Geoff Lang7dd2e102014-11-10 15:19:26 -0500801 mValidated = false;
802
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000803 mLinked = false;
804}
805
Geoff Lange1a27752015-10-05 13:16:04 -0400806bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000807{
808 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
Jamie Madilla2c74982016-12-12 11:20:42 -0500811Error Program::loadBinary(const Context *context,
812 GLenum binaryFormat,
813 const void *binary,
814 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000815{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500816 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000817
Geoff Lang7dd2e102014-11-10 15:19:26 -0500818#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800819 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500820#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400821 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
822 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000823 {
Jamie Madillf6113162015-05-07 11:49:21 -0400824 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800825 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500826 }
827
Jamie Madill4f86d052017-06-05 12:59:26 -0400828 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
829 ANGLE_TRY_RESULT(
830 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400831
832 // Currently we require the full shader text to compute the program hash.
833 // TODO(jmadill): Store the binary in the internal program cache.
834
Jamie Madillb0a838b2016-11-13 20:02:12 -0500835 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500836#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500837}
838
Jamie Madilla2c74982016-12-12 11:20:42 -0500839Error Program::saveBinary(const Context *context,
840 GLenum *binaryFormat,
841 void *binary,
842 GLsizei bufSize,
843 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500844{
845 if (binaryFormat)
846 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400847 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500848 }
849
Jamie Madill4f86d052017-06-05 12:59:26 -0400850 angle::MemoryBuffer memoryBuf;
851 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500852
Jamie Madill4f86d052017-06-05 12:59:26 -0400853 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
854 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500855
856 if (streamLength > bufSize)
857 {
858 if (length)
859 {
860 *length = 0;
861 }
862
863 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
864 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
865 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500866 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500867 }
868
869 if (binary)
870 {
871 char *ptr = reinterpret_cast<char*>(binary);
872
Jamie Madill48ef11b2016-04-27 15:21:52 -0400873 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500874 ptr += streamLength;
875
876 ASSERT(ptr - streamLength == binary);
877 }
878
879 if (length)
880 {
881 *length = streamLength;
882 }
883
He Yunchaoacd18982017-01-04 10:46:42 +0800884 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500885}
886
Jamie Madillffe00c02017-06-27 16:26:55 -0400887GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500888{
889 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400890 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891 if (error.isError())
892 {
893 return 0;
894 }
895
896 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000897}
898
Geoff Langc5629752015-12-07 16:29:04 -0500899void Program::setBinaryRetrievableHint(bool retrievable)
900{
901 // TODO(jmadill) : replace with dirty bits
902 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400903 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500904}
905
906bool Program::getBinaryRetrievableHint() const
907{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400908 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500909}
910
Yunchao He61afff12017-03-14 15:34:03 +0800911void Program::setSeparable(bool separable)
912{
913 // TODO(yunchao) : replace with dirty bits
914 if (mState.mSeparable != separable)
915 {
916 mProgram->setSeparable(separable);
917 mState.mSeparable = separable;
918 }
919}
920
921bool Program::isSeparable() const
922{
923 return mState.mSeparable;
924}
925
Jamie Madill6c1f6712017-02-14 19:08:04 -0500926void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000927{
928 mRefCount--;
929
930 if (mRefCount == 0 && mDeleteStatus)
931 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500932 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000933 }
934}
935
936void Program::addRef()
937{
938 mRefCount++;
939}
940
941unsigned int Program::getRefCount() const
942{
943 return mRefCount;
944}
945
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000946int Program::getInfoLogLength() const
947{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400948 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000949}
950
Geoff Lange1a27752015-10-05 13:16:04 -0400951void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000952{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000953 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000954}
955
Geoff Lange1a27752015-10-05 13:16:04 -0400956void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000957{
958 int total = 0;
959
Martin Radev4c4c8e72016-08-04 12:25:34 +0300960 if (mState.mAttachedComputeShader)
961 {
962 if (total < maxCount)
963 {
964 shaders[total] = mState.mAttachedComputeShader->getHandle();
965 total++;
966 }
967 }
968
Jamie Madill48ef11b2016-04-27 15:21:52 -0400969 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000970 {
971 if (total < maxCount)
972 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400973 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200974 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000975 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000976 }
977
Jamie Madill48ef11b2016-04-27 15:21:52 -0400978 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000979 {
980 if (total < maxCount)
981 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400982 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200983 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000984 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000985 }
986
987 if (count)
988 {
989 *count = total;
990 }
991}
992
Geoff Lange1a27752015-10-05 13:16:04 -0400993GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500994{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400995 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500996}
997
Jamie Madill63805b42015-08-25 13:17:39 -0400998bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400999{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001000 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1001 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001002}
1003
jchen10fd7c3b52017-03-21 15:36:03 +08001004void Program::getActiveAttribute(GLuint index,
1005 GLsizei bufsize,
1006 GLsizei *length,
1007 GLint *size,
1008 GLenum *type,
1009 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001010{
Jamie Madillc349ec02015-08-21 16:53:12 -04001011 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001012 {
1013 if (bufsize > 0)
1014 {
1015 name[0] = '\0';
1016 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001018 if (length)
1019 {
1020 *length = 0;
1021 }
1022
1023 *type = GL_NONE;
1024 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001025 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001026 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001027
jchen1036e120e2017-03-14 14:53:58 +08001028 ASSERT(index < mState.mAttributes.size());
1029 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001030
1031 if (bufsize > 0)
1032 {
jchen10fd7c3b52017-03-21 15:36:03 +08001033 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001034 }
1035
1036 // Always a single 'type' instance
1037 *size = 1;
1038 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001039}
1040
Geoff Lange1a27752015-10-05 13:16:04 -04001041GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001042{
Jamie Madillc349ec02015-08-21 16:53:12 -04001043 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001044 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001045 return 0;
1046 }
1047
jchen1036e120e2017-03-14 14:53:58 +08001048 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001049}
1050
Geoff Lange1a27752015-10-05 13:16:04 -04001051GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001052{
Jamie Madillc349ec02015-08-21 16:53:12 -04001053 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001054 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001055 return 0;
1056 }
1057
1058 size_t maxLength = 0;
1059
Jamie Madill48ef11b2016-04-27 15:21:52 -04001060 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001061 {
jchen1036e120e2017-03-14 14:53:58 +08001062 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001063 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064
Jamie Madillc349ec02015-08-21 16:53:12 -04001065 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001066}
1067
jchen1015015f72017-03-16 13:54:21 +08001068GLuint Program::getInputResourceIndex(const GLchar *name) const
1069{
1070 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1071 {
1072 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1073 if (attribute.name == name)
1074 {
1075 return attributeIndex;
1076 }
1077 }
1078 return GL_INVALID_INDEX;
1079}
1080
1081GLuint Program::getOutputResourceIndex(const GLchar *name) const
1082{
1083 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1084}
1085
jchen10fd7c3b52017-03-21 15:36:03 +08001086size_t Program::getOutputResourceCount() const
1087{
1088 return (mLinked ? mState.mOutputVariables.size() : 0);
1089}
1090
1091void Program::getInputResourceName(GLuint index,
1092 GLsizei bufSize,
1093 GLsizei *length,
1094 GLchar *name) const
1095{
1096 GLint size;
1097 GLenum type;
1098 getActiveAttribute(index, bufSize, length, &size, &type, name);
1099}
1100
1101void Program::getOutputResourceName(GLuint index,
1102 GLsizei bufSize,
1103 GLsizei *length,
1104 GLchar *name) const
1105{
1106 if (length)
1107 {
1108 *length = 0;
1109 }
1110
1111 if (!mLinked)
1112 {
1113 if (bufSize > 0)
1114 {
1115 name[0] = '\0';
1116 }
1117 return;
1118 }
1119 ASSERT(index < mState.mOutputVariables.size());
1120 const auto &output = mState.mOutputVariables[index];
1121
1122 if (bufSize > 0)
1123 {
1124 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1125
1126 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1127 }
1128}
1129
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130GLint Program::getFragDataLocation(const std::string &name) const
1131{
1132 std::string baseName(name);
1133 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001134 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001135 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001136 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001137 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1138 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001139 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001140 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001141 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001143}
1144
Geoff Lange1a27752015-10-05 13:16:04 -04001145void Program::getActiveUniform(GLuint index,
1146 GLsizei bufsize,
1147 GLsizei *length,
1148 GLint *size,
1149 GLenum *type,
1150 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001151{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001153 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001154 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001155 ASSERT(index < mState.mUniforms.size());
1156 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157
1158 if (bufsize > 0)
1159 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001160 std::string string = uniform.name;
1161 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001162 {
1163 string += "[0]";
1164 }
jchen10fd7c3b52017-03-21 15:36:03 +08001165 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166 }
1167
Jamie Madill62d31cb2015-09-11 13:25:51 -04001168 *size = uniform.elementCount();
1169 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001170 }
1171 else
1172 {
1173 if (bufsize > 0)
1174 {
1175 name[0] = '\0';
1176 }
1177
1178 if (length)
1179 {
1180 *length = 0;
1181 }
1182
1183 *size = 0;
1184 *type = GL_NONE;
1185 }
1186}
1187
Geoff Lange1a27752015-10-05 13:16:04 -04001188GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001189{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001190 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001191 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001192 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001193 }
1194 else
1195 {
1196 return 0;
1197 }
1198}
1199
Geoff Lange1a27752015-10-05 13:16:04 -04001200GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001201{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001202 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203
1204 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001205 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001206 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001208 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001209 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 size_t length = uniform.name.length() + 1u;
1211 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001212 {
1213 length += 3; // Counting in "[0]".
1214 }
1215 maxLength = std::max(length, maxLength);
1216 }
1217 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001218 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001219
Jamie Madill62d31cb2015-09-11 13:25:51 -04001220 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001221}
1222
1223GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1224{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001225 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001226 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001227 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001228 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001229 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1230 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1231 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001232 case GL_UNIFORM_BLOCK_INDEX:
1233 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1235 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1236 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1237 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1238 default:
1239 UNREACHABLE();
1240 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242 return 0;
1243}
1244
1245bool Program::isValidUniformLocation(GLint location) const
1246{
Jamie Madille2e406c2016-06-02 13:04:10 -04001247 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001248 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1249 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001250}
1251
Jamie Madill62d31cb2015-09-11 13:25:51 -04001252const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001254 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001255 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001256}
1257
Jamie Madillac4e9c32017-01-13 14:07:12 -05001258const VariableLocation &Program::getUniformLocation(GLint location) const
1259{
1260 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1261 return mState.mUniformLocations[location];
1262}
1263
1264const std::vector<VariableLocation> &Program::getUniformLocations() const
1265{
1266 return mState.mUniformLocations;
1267}
1268
1269const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1270{
1271 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1272 return mState.mUniforms[index];
1273}
1274
Jamie Madill62d31cb2015-09-11 13:25:51 -04001275GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001276{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001277 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278}
1279
Jamie Madill62d31cb2015-09-11 13:25:51 -04001280GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001281{
Jamie Madille7d84322017-01-10 18:21:59 -05001282 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283}
1284
1285void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1286{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001287 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1288 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289}
1290
1291void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1292{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001293 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1294 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295}
1296
1297void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1298{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001299 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1300 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001301}
1302
1303void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1304{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001305 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1306 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307}
1308
1309void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1310{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001311 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1312 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313}
1314
1315void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1316{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001317 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1318 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
1321void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1322{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001323 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1324 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325}
1326
1327void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1328{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001329 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1330 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331}
1332
1333void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1334{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001335 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1336 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
1339void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1340{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001341 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1342 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343}
1344
1345void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1346{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001347 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1348 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349}
1350
1351void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1352{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001353 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1354 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355}
1356
1357void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1358{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001359 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1360 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361}
1362
1363void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1364{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001365 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1366 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001367}
1368
1369void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1370{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001371 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1372 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373}
1374
1375void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1376{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001377 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1378 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
1381void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1382{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001383 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1384 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385}
1386
1387void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1388{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001389 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1390 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391}
1392
1393void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1394{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001395 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1396 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397}
1398
1399void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1400{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001401 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1402 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
1405void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1406{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001407 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1408 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
Geoff Lange1a27752015-10-05 13:16:04 -04001411void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001413 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414}
1415
Geoff Lange1a27752015-10-05 13:16:04 -04001416void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001418 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419}
1420
Geoff Lange1a27752015-10-05 13:16:04 -04001421void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001422{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001423 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424}
1425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001426void Program::flagForDeletion()
1427{
1428 mDeleteStatus = true;
1429}
1430
1431bool Program::isFlaggedForDeletion() const
1432{
1433 return mDeleteStatus;
1434}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001435
Brandon Jones43a53e22014-08-28 16:23:22 -07001436void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001437{
1438 mInfoLog.reset();
1439
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001441 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001442 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001443 }
1444 else
1445 {
Jamie Madillf6113162015-05-07 11:49:21 -04001446 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001447 }
1448}
1449
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1451{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001452 // Skip cache if we're using an infolog, so we get the full error.
1453 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1454 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1455 {
1456 return mCachedValidateSamplersResult.value();
1457 }
1458
1459 if (mTextureUnitTypesCache.empty())
1460 {
1461 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1462 }
1463 else
1464 {
1465 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1466 }
1467
1468 // if any two active samplers in a program are of different types, but refer to the same
1469 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1470 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001471 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001472 {
Jamie Madille7d84322017-01-10 18:21:59 -05001473 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001474
Jamie Madille7d84322017-01-10 18:21:59 -05001475 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001476 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001477 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1478 {
1479 if (infoLog)
1480 {
1481 (*infoLog) << "Sampler uniform (" << textureUnit
1482 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1483 << caps.maxCombinedTextureImageUnits << ")";
1484 }
1485
1486 mCachedValidateSamplersResult = false;
1487 return false;
1488 }
1489
1490 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1491 {
1492 if (textureType != mTextureUnitTypesCache[textureUnit])
1493 {
1494 if (infoLog)
1495 {
1496 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1497 "image unit ("
1498 << textureUnit << ").";
1499 }
1500
1501 mCachedValidateSamplersResult = false;
1502 return false;
1503 }
1504 }
1505 else
1506 {
1507 mTextureUnitTypesCache[textureUnit] = textureType;
1508 }
1509 }
1510 }
1511
1512 mCachedValidateSamplersResult = true;
1513 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001514}
1515
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001516bool Program::isValidated() const
1517{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518 return mValidated;
1519}
1520
Geoff Lange1a27752015-10-05 13:16:04 -04001521GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001523 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524}
1525
1526void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1527{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001528 ASSERT(
1529 uniformBlockIndex <
1530 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001531
Jamie Madill48ef11b2016-04-27 15:21:52 -04001532 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001533
1534 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001535 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001536 std::string string = uniformBlock.name;
1537
Jamie Madill62d31cb2015-09-11 13:25:51 -04001538 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001539 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001540 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001541 }
jchen10fd7c3b52017-03-21 15:36:03 +08001542 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001543 }
1544}
1545
Geoff Lange1a27752015-10-05 13:16:04 -04001546GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001547{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548 int maxLength = 0;
1549
1550 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001551 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001552 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001553 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1554 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001555 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001556 if (!uniformBlock.name.empty())
1557 {
jchen10af713a22017-04-19 09:10:56 +08001558 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1559 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560 }
1561 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001562 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001563
1564 return maxLength;
1565}
1566
Geoff Lange1a27752015-10-05 13:16:04 -04001567GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001569 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001570 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001571
Jamie Madill48ef11b2016-04-27 15:21:52 -04001572 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001573 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1574 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001575 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001576 if (uniformBlock.name == baseName)
1577 {
1578 const bool arrayElementZero =
1579 (subscript == GL_INVALID_INDEX &&
1580 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1581 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1582 {
1583 return blockIndex;
1584 }
1585 }
1586 }
1587
1588 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001589}
1590
Jamie Madill62d31cb2015-09-11 13:25:51 -04001591const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001592{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001593 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1594 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001595}
1596
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001597void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1598{
jchen107a20b972017-06-13 14:25:26 +08001599 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001600 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001601 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001602}
1603
1604GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1605{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001606 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001607}
1608
Geoff Lang48dcae72014-02-05 16:28:24 -05001609void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1610{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001611 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001612 for (GLsizei i = 0; i < count; i++)
1613 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001614 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001615 }
1616
Jamie Madill48ef11b2016-04-27 15:21:52 -04001617 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001618}
1619
1620void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1621{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001623 {
jchen10a9042d32017-03-17 08:50:45 +08001624 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1625 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1626 std::string varName = var.nameWithArrayIndex();
1627 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001628 if (length)
1629 {
1630 *length = lastNameIdx;
1631 }
1632 if (size)
1633 {
jchen10a9042d32017-03-17 08:50:45 +08001634 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001635 }
1636 if (type)
1637 {
jchen10a9042d32017-03-17 08:50:45 +08001638 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001639 }
1640 if (name)
1641 {
jchen10a9042d32017-03-17 08:50:45 +08001642 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001643 name[lastNameIdx] = '\0';
1644 }
1645 }
1646}
1647
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001648GLsizei Program::getTransformFeedbackVaryingCount() const
1649{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001651 {
jchen10a9042d32017-03-17 08:50:45 +08001652 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001653 }
1654 else
1655 {
1656 return 0;
1657 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001658}
1659
1660GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1661{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001663 {
1664 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001665 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001666 {
jchen10a9042d32017-03-17 08:50:45 +08001667 maxSize =
1668 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001669 }
1670
1671 return maxSize;
1672 }
1673 else
1674 {
1675 return 0;
1676 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001677}
1678
1679GLenum Program::getTransformFeedbackBufferMode() const
1680{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001681 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682}
1683
Jamie Madillbd044ed2017-06-05 12:59:21 -04001684bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001685{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001686 Shader *vertexShader = mState.mAttachedVertexShader;
1687 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001688
Jamie Madillbd044ed2017-06-05 12:59:21 -04001689 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001690
Jamie Madillbd044ed2017-06-05 12:59:21 -04001691 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1692 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693
Sami Väisänen46eaa942016-06-29 10:26:37 +03001694 std::map<GLuint, std::string> staticFragmentInputLocations;
1695
Jamie Madill4cff2472015-08-21 16:53:18 -04001696 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 bool matched = false;
1699
1700 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001701 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702 {
1703 continue;
1704 }
1705
Jamie Madill4cff2472015-08-21 16:53:18 -04001706 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001707 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001708 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001710 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001711 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001712 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713 {
1714 return false;
1715 }
1716
Geoff Lang7dd2e102014-11-10 15:19:26 -05001717 matched = true;
1718 break;
1719 }
1720 }
1721
1722 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001723 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001725 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001726 return false;
1727 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001728
1729 // Check for aliased path rendering input bindings (if any).
1730 // If more than one binding refer statically to the same
1731 // location the link must fail.
1732
1733 if (!output.staticUse)
1734 continue;
1735
1736 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1737 if (inputBinding == -1)
1738 continue;
1739
1740 const auto it = staticFragmentInputLocations.find(inputBinding);
1741 if (it == std::end(staticFragmentInputLocations))
1742 {
1743 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1744 }
1745 else
1746 {
1747 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1748 << it->second;
1749 return false;
1750 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751 }
1752
Jamie Madillbd044ed2017-06-05 12:59:21 -04001753 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001754 {
1755 return false;
1756 }
1757
Jamie Madillada9ecc2015-08-17 12:53:37 -04001758 // TODO(jmadill): verify no unmatched vertex varyings?
1759
Geoff Lang7dd2e102014-11-10 15:19:26 -05001760 return true;
1761}
1762
Jamie Madillbd044ed2017-06-05 12:59:21 -04001763bool Program::linkUniforms(const Context *context,
1764 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001765 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001766{
Olli Etuahob78707c2017-03-09 15:03:11 +00001767 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001768 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001769 {
1770 return false;
1771 }
1772
Olli Etuahob78707c2017-03-09 15:03:11 +00001773 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001774
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001775 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001776
jchen10eaef1e52017-06-13 10:44:11 +08001777 if (!linkAtomicCounterBuffers())
1778 {
1779 return false;
1780 }
1781
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001782 return true;
1783}
1784
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001785void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001786{
Jamie Madill982f6e02017-06-07 14:33:04 -04001787 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1788 unsigned int low = high;
1789
jchen10eaef1e52017-06-13 10:44:11 +08001790 for (auto counterIter = mState.mUniforms.rbegin();
1791 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1792 {
1793 --low;
1794 }
1795
1796 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1797
1798 high = low;
1799
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001800 for (auto imageIter = mState.mUniforms.rbegin();
1801 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1802 {
1803 --low;
1804 }
1805
1806 mState.mImageUniformRange = RangeUI(low, high);
1807
1808 // If uniform is a image type, insert it into the mImageBindings array.
1809 for (unsigned int imageIndex : mState.mImageUniformRange)
1810 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001811 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v} commands
1812 // cannot load values into a uniform defined as an image. if declare without a
1813 // binding qualifier, any uniform image variable (include all elements of
1814 // unbound image array) shoud be bound to unit zero.
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001815 auto &imageUniform = mState.mUniforms[imageIndex];
1816 if (imageUniform.binding == -1)
1817 {
Xinghua Cao0328b572017-06-26 15:51:36 +08001818 mState.mImageBindings.emplace_back(ImageBinding(imageUniform.elementCount()));
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001819 }
Xinghua Cao0328b572017-06-26 15:51:36 +08001820 else
1821 {
1822 mState.mImageBindings.emplace_back(
1823 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1824 }
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001825 }
1826
1827 high = low;
1828
1829 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001830 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001831 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001832 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001833 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001834
1835 mState.mSamplerUniformRange = RangeUI(low, high);
1836
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001837 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001838 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001839 {
1840 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1841 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1842 mState.mSamplerBindings.emplace_back(
1843 SamplerBinding(textureType, samplerUniform.elementCount()));
1844 }
1845}
1846
jchen10eaef1e52017-06-13 10:44:11 +08001847bool Program::linkAtomicCounterBuffers()
1848{
1849 for (unsigned int index : mState.mAtomicCounterUniformRange)
1850 {
1851 auto &uniform = mState.mUniforms[index];
1852 bool found = false;
1853 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1854 ++bufferIndex)
1855 {
1856 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1857 if (buffer.binding == uniform.binding)
1858 {
1859 buffer.memberIndexes.push_back(index);
1860 uniform.bufferIndex = bufferIndex;
1861 found = true;
1862 break;
1863 }
1864 }
1865 if (!found)
1866 {
1867 AtomicCounterBuffer atomicCounterBuffer;
1868 atomicCounterBuffer.binding = uniform.binding;
1869 atomicCounterBuffer.memberIndexes.push_back(index);
1870 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1871 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1872 }
1873 }
1874 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1875 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1876
1877 return true;
1878}
1879
Martin Radev4c4c8e72016-08-04 12:25:34 +03001880bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1881 const std::string &uniformName,
1882 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001883 const sh::InterfaceBlockField &fragmentUniform,
1884 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001885{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001886 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1887 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1888 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001889 {
1890 return false;
1891 }
1892
1893 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1894 {
Jamie Madillf6113162015-05-07 11:49:21 -04001895 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896 return false;
1897 }
1898
1899 return true;
1900}
1901
Jamie Madilleb979bf2016-11-15 12:28:46 -05001902// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001903bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001905 const ContextState &data = context->getContextState();
1906 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001907
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001909 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001910 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001911
1912 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001913 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001914 {
Jamie Madillf6113162015-05-07 11:49:21 -04001915 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001916 return false;
1917 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001919 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001920
Jamie Madillc349ec02015-08-21 16:53:12 -04001921 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001922 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001923 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001924 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001925 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001926 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001927 attribute.location = bindingLocation;
1928 }
1929
1930 if (attribute.location != -1)
1931 {
1932 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001933 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934
Jamie Madill63805b42015-08-25 13:17:39 -04001935 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001936 {
Jamie Madillf6113162015-05-07 11:49:21 -04001937 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001938 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001939
1940 return false;
1941 }
1942
Jamie Madill63805b42015-08-25 13:17:39 -04001943 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001944 {
Jamie Madill63805b42015-08-25 13:17:39 -04001945 const int regLocation = attribute.location + reg;
1946 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947
1948 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001949 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001950 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001952 // TODO(jmadill): fix aliasing on ES2
1953 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001955 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001956 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 return false;
1958 }
1959 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001960 else
1961 {
Jamie Madill63805b42015-08-25 13:17:39 -04001962 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001963 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964
Jamie Madill63805b42015-08-25 13:17:39 -04001965 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966 }
1967 }
1968 }
1969
1970 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001971 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001973 // Not set by glBindAttribLocation or by location layout qualifier
1974 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975 {
Jamie Madill63805b42015-08-25 13:17:39 -04001976 int regs = VariableRegisterCount(attribute.type);
1977 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001978
Jamie Madill63805b42015-08-25 13:17:39 -04001979 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980 {
Jamie Madillf6113162015-05-07 11:49:21 -04001981 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001982 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001983 }
1984
Jamie Madillc349ec02015-08-21 16:53:12 -04001985 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 }
1987 }
1988
Jamie Madill48ef11b2016-04-27 15:21:52 -04001989 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990 {
Jamie Madill63805b42015-08-25 13:17:39 -04001991 ASSERT(attribute.location != -1);
1992 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001993
Jamie Madill63805b42015-08-25 13:17:39 -04001994 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001995 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001996 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997 }
1998 }
1999
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000 return true;
2001}
2002
Martin Radev4c4c8e72016-08-04 12:25:34 +03002003bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2004 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2005 const std::string &errorMessage,
2006 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002008 GLuint blockCount = 0;
2009 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002011 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002012 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002013 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002014 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002015 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002016 return false;
2017 }
2018 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002020 return true;
2021}
Jamie Madille473dee2015-08-18 14:49:01 -04002022
Martin Radev4c4c8e72016-08-04 12:25:34 +03002023bool Program::validateVertexAndFragmentInterfaceBlocks(
2024 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2025 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002026 InfoLog &infoLog,
2027 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002028{
2029 // Check that interface blocks defined in the vertex and fragment shaders are identical
2030 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2031 UniformBlockMap linkedUniformBlocks;
2032
2033 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2034 {
2035 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2036 }
2037
Jamie Madille473dee2015-08-18 14:49:01 -04002038 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 {
Jamie Madille473dee2015-08-18 14:49:01 -04002040 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041 if (entry != linkedUniformBlocks.end())
2042 {
2043 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002044 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2045 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046 {
2047 return false;
2048 }
2049 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002050 }
2051 return true;
2052}
Jamie Madille473dee2015-08-18 14:49:01 -04002053
Jamie Madillbd044ed2017-06-05 12:59:21 -04002054bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002055{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002056 const auto &caps = context->getCaps();
2057
Martin Radev4c4c8e72016-08-04 12:25:34 +03002058 if (mState.mAttachedComputeShader)
2059 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002060 Shader &computeShader = *mState.mAttachedComputeShader;
2061 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002062
2063 if (!validateUniformBlocksCount(
2064 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2065 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2066 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002068 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002070 return true;
2071 }
2072
Jamie Madillbd044ed2017-06-05 12:59:21 -04002073 Shader &vertexShader = *mState.mAttachedVertexShader;
2074 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002075
Jamie Madillbd044ed2017-06-05 12:59:21 -04002076 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2077 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002078
2079 if (!validateUniformBlocksCount(
2080 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2081 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2082 {
2083 return false;
2084 }
2085 if (!validateUniformBlocksCount(
2086 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2087 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2088 infoLog))
2089 {
2090
2091 return false;
2092 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002093
2094 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002095 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002096 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002097 {
2098 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 }
Jamie Madille473dee2015-08-18 14:49:01 -04002100
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101 return true;
2102}
2103
Jamie Madilla2c74982016-12-12 11:20:42 -05002104bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002105 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002106 const sh::InterfaceBlock &fragmentInterfaceBlock,
2107 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108{
2109 const char* blockName = vertexInterfaceBlock.name.c_str();
2110 // validate blocks for the same member types
2111 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2112 {
Jamie Madillf6113162015-05-07 11:49:21 -04002113 infoLog << "Types for interface block '" << blockName
2114 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115 return false;
2116 }
2117 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2118 {
Jamie Madillf6113162015-05-07 11:49:21 -04002119 infoLog << "Array sizes differ for interface block '" << blockName
2120 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 return false;
2122 }
jchen10af713a22017-04-19 09:10:56 +08002123 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2124 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2125 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 {
Jamie Madillf6113162015-05-07 11:49:21 -04002127 infoLog << "Layout qualifiers differ for interface block '" << blockName
2128 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 return false;
2130 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002131 const unsigned int numBlockMembers =
2132 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2134 {
2135 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2136 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2137 if (vertexMember.name != fragmentMember.name)
2138 {
Jamie Madillf6113162015-05-07 11:49:21 -04002139 infoLog << "Name mismatch for field " << blockMemberIndex
2140 << " of interface block '" << blockName
2141 << "': (in vertex: '" << vertexMember.name
2142 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 return false;
2144 }
2145 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002146 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2147 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 {
2149 return false;
2150 }
2151 }
2152 return true;
2153}
2154
2155bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2156 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2157{
2158 if (vertexVariable.type != fragmentVariable.type)
2159 {
Jamie Madillf6113162015-05-07 11:49:21 -04002160 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 return false;
2162 }
2163 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2164 {
Jamie Madillf6113162015-05-07 11:49:21 -04002165 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002166 return false;
2167 }
2168 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2169 {
Jamie Madillf6113162015-05-07 11:49:21 -04002170 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171 return false;
2172 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002173 if (vertexVariable.structName != fragmentVariable.structName)
2174 {
2175 infoLog << "Structure names for " << variableName
2176 << " differ between vertex and fragment shaders";
2177 return false;
2178 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179
2180 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2181 {
Jamie Madillf6113162015-05-07 11:49:21 -04002182 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183 return false;
2184 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002185 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002186 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2187 {
2188 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2189 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2190
2191 if (vertexMember.name != fragmentMember.name)
2192 {
Jamie Madillf6113162015-05-07 11:49:21 -04002193 infoLog << "Name mismatch for field '" << memberIndex
2194 << "' of " << variableName
2195 << ": (in vertex: '" << vertexMember.name
2196 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197 return false;
2198 }
2199
2200 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2201 vertexMember.name + "'";
2202
2203 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2204 {
2205 return false;
2206 }
2207 }
2208
2209 return true;
2210}
2211
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002212bool Program::linkValidateVaryings(InfoLog &infoLog,
2213 const std::string &varyingName,
2214 const sh::Varying &vertexVarying,
2215 const sh::Varying &fragmentVarying,
2216 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002217{
2218 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2219 {
2220 return false;
2221 }
2222
Jamie Madille9cc4692015-02-19 16:00:13 -05002223 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002225 infoLog << "Interpolation types for " << varyingName
2226 << " differ between vertex and fragment shaders.";
2227 return false;
2228 }
2229
2230 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2231 {
2232 infoLog << "Invariance for " << varyingName
2233 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234 return false;
2235 }
2236
2237 return true;
2238}
2239
Jamie Madillbd044ed2017-06-05 12:59:21 -04002240bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002241{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002242 Shader *vertexShader = mState.mAttachedVertexShader;
2243 Shader *fragmentShader = mState.mAttachedFragmentShader;
2244 const auto &vertexVaryings = vertexShader->getVaryings(context);
2245 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2246 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002247
2248 if (shaderVersion != 100)
2249 {
2250 // Only ESSL 1.0 has restrictions on matching input and output invariance
2251 return true;
2252 }
2253
2254 bool glPositionIsInvariant = false;
2255 bool glPointSizeIsInvariant = false;
2256 bool glFragCoordIsInvariant = false;
2257 bool glPointCoordIsInvariant = false;
2258
2259 for (const sh::Varying &varying : vertexVaryings)
2260 {
2261 if (!varying.isBuiltIn())
2262 {
2263 continue;
2264 }
2265 if (varying.name.compare("gl_Position") == 0)
2266 {
2267 glPositionIsInvariant = varying.isInvariant;
2268 }
2269 else if (varying.name.compare("gl_PointSize") == 0)
2270 {
2271 glPointSizeIsInvariant = varying.isInvariant;
2272 }
2273 }
2274
2275 for (const sh::Varying &varying : fragmentVaryings)
2276 {
2277 if (!varying.isBuiltIn())
2278 {
2279 continue;
2280 }
2281 if (varying.name.compare("gl_FragCoord") == 0)
2282 {
2283 glFragCoordIsInvariant = varying.isInvariant;
2284 }
2285 else if (varying.name.compare("gl_PointCoord") == 0)
2286 {
2287 glPointCoordIsInvariant = varying.isInvariant;
2288 }
2289 }
2290
2291 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2292 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2293 // Not requiring invariance to match is supported by:
2294 // dEQP, WebGL CTS, Nexus 5X GLES
2295 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2296 {
2297 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2298 "declared invariant.";
2299 return false;
2300 }
2301 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2302 {
2303 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2304 "declared invariant.";
2305 return false;
2306 }
2307
2308 return true;
2309}
2310
jchen10a9042d32017-03-17 08:50:45 +08002311bool Program::linkValidateTransformFeedback(const gl::Context *context,
2312 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002313 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002314 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315{
2316 size_t totalComponents = 0;
2317
Jamie Madillccdf74b2015-08-18 10:46:12 -04002318 std::set<std::string> uniqueNames;
2319
Jamie Madill48ef11b2016-04-27 15:21:52 -04002320 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002321 {
2322 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002323 size_t subscript = GL_INVALID_INDEX;
2324 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2325
Jamie Madill192745a2016-12-22 15:58:21 -05002326 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002327 {
Jamie Madill192745a2016-12-22 15:58:21 -05002328 const sh::Varying *varying = ref.second.get();
2329
jchen10a9042d32017-03-17 08:50:45 +08002330 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002331 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002332 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002333 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002334 infoLog << "Two transform feedback varyings specify the same output variable ("
2335 << tfVaryingName << ").";
2336 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002337 }
jchen10a9042d32017-03-17 08:50:45 +08002338 if (context->getClientVersion() >= Version(3, 1))
2339 {
2340 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2341 {
2342 infoLog
2343 << "Two transform feedback varyings include the same array element ("
2344 << tfVaryingName << ").";
2345 return false;
2346 }
2347 }
2348 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002349 {
2350 infoLog << "Capture of arrays is undefined and not supported.";
2351 return false;
2352 }
2353
jchen10a9042d32017-03-17 08:50:45 +08002354 uniqueNames.insert(tfVaryingName);
2355
Jamie Madillccdf74b2015-08-18 10:46:12 -04002356 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002357 size_t elementCount =
2358 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2359 : 1);
2360 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002361 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002362 componentCount > caps.maxTransformFeedbackSeparateComponents)
2363 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002364 infoLog << "Transform feedback varying's " << varying->name << " components ("
2365 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002366 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002367 return false;
2368 }
2369
2370 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371 found = true;
2372 break;
2373 }
2374 }
jchen10a9042d32017-03-17 08:50:45 +08002375 if (context->getClientVersion() < Version(3, 1) &&
2376 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002377 {
Geoff Lang1a683462015-09-29 15:09:59 -04002378 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002379 return false;
2380 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002381 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2382 ASSERT(found);
2383 }
2384
Jamie Madill48ef11b2016-04-27 15:21:52 -04002385 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002386 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387 {
Jamie Madillf6113162015-05-07 11:49:21 -04002388 infoLog << "Transform feedback varying total components (" << totalComponents
2389 << ") exceed the maximum interleaved components ("
2390 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 return false;
2392 }
2393
2394 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002395}
2396
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002397bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2398{
2399 const std::vector<sh::Uniform> &vertexUniforms =
2400 mState.mAttachedVertexShader->getUniforms(context);
2401 const std::vector<sh::Uniform> &fragmentUniforms =
2402 mState.mAttachedFragmentShader->getUniforms(context);
2403 const std::vector<sh::Attribute> &attributes =
2404 mState.mAttachedVertexShader->getActiveAttributes(context);
2405 for (const auto &attrib : attributes)
2406 {
2407 for (const auto &uniform : vertexUniforms)
2408 {
2409 if (uniform.name == attrib.name)
2410 {
2411 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2412 return false;
2413 }
2414 }
2415 for (const auto &uniform : fragmentUniforms)
2416 {
2417 if (uniform.name == attrib.name)
2418 {
2419 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2420 return false;
2421 }
2422 }
2423 }
2424 return true;
2425}
2426
Jamie Madill192745a2016-12-22 15:58:21 -05002427void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002428{
2429 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002430 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002431 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002432 {
jchen10a9042d32017-03-17 08:50:45 +08002433 size_t subscript = GL_INVALID_INDEX;
2434 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002435 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436 {
Jamie Madill192745a2016-12-22 15:58:21 -05002437 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002438 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002439 {
jchen10a9042d32017-03-17 08:50:45 +08002440 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2441 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002442 break;
2443 }
2444 }
2445 }
2446}
2447
Jamie Madillbd044ed2017-06-05 12:59:21 -04002448Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002449{
Jamie Madill192745a2016-12-22 15:58:21 -05002450 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002451
Jamie Madillbd044ed2017-06-05 12:59:21 -04002452 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453 {
Jamie Madill192745a2016-12-22 15:58:21 -05002454 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002455 }
2456
Jamie Madillbd044ed2017-06-05 12:59:21 -04002457 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002458 {
Jamie Madill192745a2016-12-22 15:58:21 -05002459 merged[varying.name].fragment = &varying;
2460 }
2461
2462 return merged;
2463}
2464
2465std::vector<PackedVarying> Program::getPackedVaryings(
2466 const Program::MergedVaryings &mergedVaryings) const
2467{
2468 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2469 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002470 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002471
2472 for (const auto &ref : mergedVaryings)
2473 {
2474 const sh::Varying *input = ref.second.vertex;
2475 const sh::Varying *output = ref.second.fragment;
2476
2477 // Only pack varyings that have a matched input or output, plus special builtins.
2478 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002479 {
Jamie Madill192745a2016-12-22 15:58:21 -05002480 // Will get the vertex shader interpolation by default.
2481 auto interpolation = ref.second.get()->interpolation;
2482
Olli Etuaho06a06f52017-07-12 12:22:15 +03002483 // Note that we lose the vertex shader static use information here. The data for the
2484 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002485 if (output->isStruct())
2486 {
2487 ASSERT(!output->isArray());
2488 for (const auto &field : output->fields)
2489 {
2490 ASSERT(!field.isStruct() && !field.isArray());
2491 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2492 }
2493 }
2494 else
2495 {
2496 packedVaryings.push_back(PackedVarying(*output, interpolation));
2497 }
2498 continue;
2499 }
2500
2501 // Keep Transform FB varyings in the merged list always.
2502 if (!input)
2503 {
2504 continue;
2505 }
2506
2507 for (const std::string &tfVarying : tfVaryings)
2508 {
jchen10a9042d32017-03-17 08:50:45 +08002509 size_t subscript = GL_INVALID_INDEX;
2510 std::string baseName = ParseResourceName(tfVarying, &subscript);
2511 if (uniqueFullNames.count(tfVarying) > 0)
2512 {
2513 continue;
2514 }
2515 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002516 {
2517 // Transform feedback for varying structs is underspecified.
2518 // See Khronos bug 9856.
2519 // TODO(jmadill): Figure out how to be spec-compliant here.
2520 if (!input->isStruct())
2521 {
2522 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2523 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002524 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2525 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002526 }
jchen10a9042d32017-03-17 08:50:45 +08002527 if (subscript == GL_INVALID_INDEX)
2528 {
2529 break;
2530 }
Jamie Madill192745a2016-12-22 15:58:21 -05002531 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002532 }
2533 }
2534
Jamie Madill192745a2016-12-22 15:58:21 -05002535 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2536
2537 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002538}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002539
Jamie Madillbd044ed2017-06-05 12:59:21 -04002540void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002541{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002542 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002543 ASSERT(fragmentShader != nullptr);
2544
Geoff Lange0cff192017-05-30 13:04:56 -04002545 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002546 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002547
2548 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002549 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002550 {
2551 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2552 outputVariable.name != "gl_FragData")
2553 {
2554 continue;
2555 }
2556
2557 unsigned int baseLocation =
2558 (outputVariable.location == -1 ? 0u
2559 : static_cast<unsigned int>(outputVariable.location));
2560 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2561 elementIndex++)
2562 {
2563 const unsigned int location = baseLocation + elementIndex;
2564 if (location >= mState.mOutputVariableTypes.size())
2565 {
2566 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2567 }
Corentin Walleze7557742017-06-01 13:09:57 -04002568 ASSERT(location < mState.mActiveOutputVariables.size());
2569 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002570 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2571 }
2572 }
2573
Jamie Madill80a6fc02015-08-21 16:53:16 -04002574 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002575 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002576 return;
2577
Jamie Madillbd044ed2017-06-05 12:59:21 -04002578 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002579 // TODO(jmadill): any caps validation here?
2580
jchen1015015f72017-03-16 13:54:21 +08002581 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002582 outputVariableIndex++)
2583 {
jchen1015015f72017-03-16 13:54:21 +08002584 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002585
2586 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2587 if (outputVariable.isBuiltIn())
2588 continue;
2589
2590 // Since multiple output locations must be specified, use 0 for non-specified locations.
2591 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2592
Jamie Madill80a6fc02015-08-21 16:53:16 -04002593 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2594 elementIndex++)
2595 {
2596 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002597 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002598 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002599 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002600 VariableLocation(outputVariable.name, element, outputVariableIndex);
2601 }
2602 }
2603}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002604
Olli Etuaho48fed632017-03-16 12:05:30 +00002605void Program::setUniformValuesFromBindingQualifiers()
2606{
Jamie Madill982f6e02017-06-07 14:33:04 -04002607 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002608 {
2609 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2610 if (samplerUniform.binding != -1)
2611 {
2612 GLint location = mState.getUniformLocation(samplerUniform.name);
2613 ASSERT(location != -1);
2614 std::vector<GLint> boundTextureUnits;
2615 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2616 ++elementIndex)
2617 {
2618 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2619 }
2620 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2621 boundTextureUnits.data());
2622 }
2623 }
2624}
2625
jchen10eaef1e52017-06-13 10:44:11 +08002626void Program::gatherAtomicCounterBuffers()
2627{
2628 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2629 // counter.
2630 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2631}
2632
Jamie Madillbd044ed2017-06-05 12:59:21 -04002633void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002634{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002635 ASSERT(mState.mUniformBlocks.empty());
2636
2637 if (mState.mAttachedComputeShader)
2638 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002639 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002640
Jamie Madillbd044ed2017-06-05 12:59:21 -04002641 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002642 {
2643
2644 // Only 'packed' blocks are allowed to be considered inactive.
2645 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2646 continue;
2647
Jamie Madilla2c74982016-12-12 11:20:42 -05002648 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002649 {
2650 if (block.name == computeBlock.name)
2651 {
2652 block.computeStaticUse = computeBlock.staticUse;
2653 }
2654 }
2655
2656 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2657 }
2658 return;
2659 }
2660
Jamie Madill62d31cb2015-09-11 13:25:51 -04002661 std::set<std::string> visitedList;
2662
Jamie Madillbd044ed2017-06-05 12:59:21 -04002663 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002664
Jamie Madillbd044ed2017-06-05 12:59:21 -04002665 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002666 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002667 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002668 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2669 continue;
2670
2671 if (visitedList.count(vertexBlock.name) > 0)
2672 continue;
2673
2674 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2675 visitedList.insert(vertexBlock.name);
2676 }
2677
Jamie Madillbd044ed2017-06-05 12:59:21 -04002678 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002679
Jamie Madillbd044ed2017-06-05 12:59:21 -04002680 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002681 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002682 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002683 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2684 continue;
2685
2686 if (visitedList.count(fragmentBlock.name) > 0)
2687 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002688 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002689 {
2690 if (block.name == fragmentBlock.name)
2691 {
2692 block.fragmentStaticUse = fragmentBlock.staticUse;
2693 }
2694 }
2695
2696 continue;
2697 }
2698
2699 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2700 visitedList.insert(fragmentBlock.name);
2701 }
jchen10af713a22017-04-19 09:10:56 +08002702 // Set initial bindings from shader.
2703 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2704 {
2705 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2706 bindUniformBlock(blockIndex, uniformBlock.binding);
2707 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002708}
2709
Jamie Madill4a3c2342015-10-08 12:58:45 -04002710template <typename VarT>
2711void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2712 const std::string &prefix,
2713 int blockIndex)
2714{
2715 for (const VarT &field : fields)
2716 {
2717 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2718
2719 if (field.isStruct())
2720 {
2721 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2722 {
2723 const std::string uniformElementName =
2724 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2725 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2726 }
2727 }
2728 else
2729 {
2730 // If getBlockMemberInfo returns false, the uniform is optimized out.
2731 sh::BlockMemberInfo memberInfo;
2732 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2733 {
2734 continue;
2735 }
2736
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002737 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002738 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002739
2740 // Since block uniforms have no location, we don't need to store them in the uniform
2741 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002742 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002743 }
2744 }
2745}
2746
Jamie Madill62d31cb2015-09-11 13:25:51 -04002747void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2748{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002749 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002750 size_t blockSize = 0;
2751
Jamie Madill4a3c2342015-10-08 12:58:45 -04002752 // Track the first and last uniform index to determine the range of active uniforms in the
2753 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002754 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002755 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002756 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002757
2758 std::vector<unsigned int> blockUniformIndexes;
2759 for (size_t blockUniformIndex = firstBlockUniformIndex;
2760 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2761 {
2762 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2763 }
jchen10af713a22017-04-19 09:10:56 +08002764 // ESSL 3.10 section 4.4.4 page 58:
2765 // Any uniform or shader storage block declared without a binding qualifier is initially
2766 // assigned to block binding point zero.
2767 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002768 if (interfaceBlock.arraySize > 0)
2769 {
2770 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2771 {
jchen10af713a22017-04-19 09:10:56 +08002772 // Don't define this block at all if it's not active in the implementation.
2773 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2774 &blockSize))
2775 {
2776 continue;
2777 }
2778 UniformBlock block(interfaceBlock.name, true, arrayElement,
2779 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002780 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002781
Martin Radev4c4c8e72016-08-04 12:25:34 +03002782 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002783 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002784 case GL_VERTEX_SHADER:
2785 {
2786 block.vertexStaticUse = interfaceBlock.staticUse;
2787 break;
2788 }
2789 case GL_FRAGMENT_SHADER:
2790 {
2791 block.fragmentStaticUse = interfaceBlock.staticUse;
2792 break;
2793 }
2794 case GL_COMPUTE_SHADER:
2795 {
2796 block.computeStaticUse = interfaceBlock.staticUse;
2797 break;
2798 }
2799 default:
2800 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002801 }
2802
Qin Jiajia0350a642016-11-01 17:01:51 +08002803 // Since all block elements in an array share the same active uniforms, they will all be
2804 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2805 // here we will add every block element in the array.
2806 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002807 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002808 }
2809 }
2810 else
2811 {
jchen10af713a22017-04-19 09:10:56 +08002812 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2813 {
2814 return;
2815 }
2816 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002817 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818
Martin Radev4c4c8e72016-08-04 12:25:34 +03002819 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002820 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002821 case GL_VERTEX_SHADER:
2822 {
2823 block.vertexStaticUse = interfaceBlock.staticUse;
2824 break;
2825 }
2826 case GL_FRAGMENT_SHADER:
2827 {
2828 block.fragmentStaticUse = interfaceBlock.staticUse;
2829 break;
2830 }
2831 case GL_COMPUTE_SHADER:
2832 {
2833 block.computeStaticUse = interfaceBlock.staticUse;
2834 break;
2835 }
2836 default:
2837 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002838 }
2839
Jamie Madill4a3c2342015-10-08 12:58:45 -04002840 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002841 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842 }
2843}
2844
Jamie Madille7d84322017-01-10 18:21:59 -05002845template <>
2846void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2847 const uint8_t *destPointer,
2848 GLsizei clampedCount,
2849 const GLint *v)
2850{
2851 // Invalidate the validation cache only if we modify the sampler data.
2852 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2853 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2854 {
2855 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2856 std::vector<GLuint> *boundTextureUnits =
2857 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2858
2859 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2860 mCachedValidateSamplersResult.reset();
2861 }
2862}
2863
2864template <typename T>
2865void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2866 const uint8_t *destPointer,
2867 GLsizei clampedCount,
2868 const T *v)
2869{
2870}
2871
Jamie Madill62d31cb2015-09-11 13:25:51 -04002872template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002873GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002874{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002875 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2876 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002877 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2878
Corentin Wallez15ac5342016-11-03 17:06:39 -04002879 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2880 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2881 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002882 GLsizei maxElementCount =
2883 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2884
2885 GLsizei count = countIn;
2886 GLsizei clampedCount = count * vectorSize;
2887 if (clampedCount > maxElementCount)
2888 {
2889 clampedCount = maxElementCount;
2890 count = maxElementCount / vectorSize;
2891 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002892
Jamie Madill62d31cb2015-09-11 13:25:51 -04002893 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2894 {
2895 // Do a cast conversion for boolean types. From the spec:
2896 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2897 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002898 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002899 {
2900 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2901 }
2902 }
2903 else
2904 {
Jamie Madille7d84322017-01-10 18:21:59 -05002905 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002906 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002907 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002908
2909 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002910}
2911
2912template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002913GLsizei Program::setMatrixUniformInternal(GLint location,
2914 GLsizei count,
2915 GLboolean transpose,
2916 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002917{
2918 if (!transpose)
2919 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002920 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002921 }
2922
2923 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002924 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2925 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002927
2928 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2929 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2930 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2931 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2932
2933 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002934 {
2935 size_t elementOffset = element * rows * cols;
2936
2937 for (size_t row = 0; row < rows; ++row)
2938 {
2939 for (size_t col = 0; col < cols; ++col)
2940 {
2941 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2942 }
2943 }
2944 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002945
2946 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002947}
2948
2949template <typename DestT>
2950void Program::getUniformInternal(GLint location, DestT *dataOut) const
2951{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002952 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2953 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002954
2955 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2956
2957 GLenum componentType = VariableComponentType(uniform.type);
2958 if (componentType == GLTypeToGLenum<DestT>::value)
2959 {
2960 memcpy(dataOut, srcPointer, uniform.getElementSize());
2961 return;
2962 }
2963
Corentin Wallez6596c462016-03-17 17:26:58 -04002964 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002965
2966 switch (componentType)
2967 {
2968 case GL_INT:
2969 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2970 break;
2971 case GL_UNSIGNED_INT:
2972 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2973 break;
2974 case GL_BOOL:
2975 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2976 break;
2977 case GL_FLOAT:
2978 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2979 break;
2980 default:
2981 UNREACHABLE();
2982 }
2983}
Jamie Madilla4595b82017-01-11 17:36:34 -05002984
2985bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2986{
2987 // Must be called after samplers are validated.
2988 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2989
2990 for (const auto &binding : mState.mSamplerBindings)
2991 {
2992 GLenum textureType = binding.textureType;
2993 for (const auto &unit : binding.boundTextureUnits)
2994 {
2995 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2996 if (programTextureID == textureID)
2997 {
2998 // TODO(jmadill): Check for appropriate overlap.
2999 return true;
3000 }
3001 }
3002 }
3003
3004 return false;
3005}
3006
Jamie Madilla2c74982016-12-12 11:20:42 -05003007} // namespace gl