blob: cc66a7230195c3107067778e04b91625b005b3a7 [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 {
1811 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v}
1812 // commands cannot load values into a uniform defined as an image,
1813 // if declare without a binding qualifier, the image variable is
1814 // initially bound to unit zero.
1815 auto &imageUniform = mState.mUniforms[imageIndex];
1816 if (imageUniform.binding == -1)
1817 {
1818 imageUniform.binding = 0;
1819 }
1820 mState.mImageBindings.emplace_back(
1821 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1822 }
1823
1824 high = low;
1825
1826 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001827 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001828 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001829 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001830 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001831
1832 mState.mSamplerUniformRange = RangeUI(low, high);
1833
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001834 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001835 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001836 {
1837 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1838 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1839 mState.mSamplerBindings.emplace_back(
1840 SamplerBinding(textureType, samplerUniform.elementCount()));
1841 }
1842}
1843
jchen10eaef1e52017-06-13 10:44:11 +08001844bool Program::linkAtomicCounterBuffers()
1845{
1846 for (unsigned int index : mState.mAtomicCounterUniformRange)
1847 {
1848 auto &uniform = mState.mUniforms[index];
1849 bool found = false;
1850 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1851 ++bufferIndex)
1852 {
1853 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1854 if (buffer.binding == uniform.binding)
1855 {
1856 buffer.memberIndexes.push_back(index);
1857 uniform.bufferIndex = bufferIndex;
1858 found = true;
1859 break;
1860 }
1861 }
1862 if (!found)
1863 {
1864 AtomicCounterBuffer atomicCounterBuffer;
1865 atomicCounterBuffer.binding = uniform.binding;
1866 atomicCounterBuffer.memberIndexes.push_back(index);
1867 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1868 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1869 }
1870 }
1871 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1872 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1873
1874 return true;
1875}
1876
Martin Radev4c4c8e72016-08-04 12:25:34 +03001877bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1878 const std::string &uniformName,
1879 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001880 const sh::InterfaceBlockField &fragmentUniform,
1881 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001882{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001883 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1884 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1885 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001886 {
1887 return false;
1888 }
1889
1890 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1891 {
Jamie Madillf6113162015-05-07 11:49:21 -04001892 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 return false;
1894 }
1895
1896 return true;
1897}
1898
Jamie Madilleb979bf2016-11-15 12:28:46 -05001899// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001900bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001901{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001902 const ContextState &data = context->getContextState();
1903 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001904
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001906 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001907 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001908
1909 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001910 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001911 {
Jamie Madillf6113162015-05-07 11:49:21 -04001912 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001913 return false;
1914 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001915
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001916 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001917
Jamie Madillc349ec02015-08-21 16:53:12 -04001918 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001919 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001920 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001921 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001922 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001923 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001924 attribute.location = bindingLocation;
1925 }
1926
1927 if (attribute.location != -1)
1928 {
1929 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001930 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931
Jamie Madill63805b42015-08-25 13:17:39 -04001932 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001933 {
Jamie Madillf6113162015-05-07 11:49:21 -04001934 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001935 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001936
1937 return false;
1938 }
1939
Jamie Madill63805b42015-08-25 13:17:39 -04001940 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001941 {
Jamie Madill63805b42015-08-25 13:17:39 -04001942 const int regLocation = attribute.location + reg;
1943 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001944
1945 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001946 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001947 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001949 // TODO(jmadill): fix aliasing on ES2
1950 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001952 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001953 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 return false;
1955 }
1956 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001957 else
1958 {
Jamie Madill63805b42015-08-25 13:17:39 -04001959 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001960 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001961
Jamie Madill63805b42015-08-25 13:17:39 -04001962 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001963 }
1964 }
1965 }
1966
1967 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001968 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001969 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001970 // Not set by glBindAttribLocation or by location layout qualifier
1971 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972 {
Jamie Madill63805b42015-08-25 13:17:39 -04001973 int regs = VariableRegisterCount(attribute.type);
1974 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975
Jamie Madill63805b42015-08-25 13:17:39 -04001976 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 {
Jamie Madillf6113162015-05-07 11:49:21 -04001978 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001979 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980 }
1981
Jamie Madillc349ec02015-08-21 16:53:12 -04001982 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001983 }
1984 }
1985
Jamie Madill48ef11b2016-04-27 15:21:52 -04001986 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 {
Jamie Madill63805b42015-08-25 13:17:39 -04001988 ASSERT(attribute.location != -1);
1989 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001990
Jamie Madill63805b42015-08-25 13:17:39 -04001991 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001993 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 }
1995 }
1996
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997 return true;
1998}
1999
Martin Radev4c4c8e72016-08-04 12:25:34 +03002000bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2001 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2002 const std::string &errorMessage,
2003 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002005 GLuint blockCount = 0;
2006 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002008 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002009 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002010 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002011 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002012 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002013 return false;
2014 }
2015 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002016 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002017 return true;
2018}
Jamie Madille473dee2015-08-18 14:49:01 -04002019
Martin Radev4c4c8e72016-08-04 12:25:34 +03002020bool Program::validateVertexAndFragmentInterfaceBlocks(
2021 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2022 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002023 InfoLog &infoLog,
2024 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002025{
2026 // Check that interface blocks defined in the vertex and fragment shaders are identical
2027 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2028 UniformBlockMap linkedUniformBlocks;
2029
2030 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2031 {
2032 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2033 }
2034
Jamie Madille473dee2015-08-18 14:49:01 -04002035 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 {
Jamie Madille473dee2015-08-18 14:49:01 -04002037 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038 if (entry != linkedUniformBlocks.end())
2039 {
2040 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002041 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2042 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
2044 return false;
2045 }
2046 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002047 }
2048 return true;
2049}
Jamie Madille473dee2015-08-18 14:49:01 -04002050
Jamie Madillbd044ed2017-06-05 12:59:21 -04002051bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002052{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002053 const auto &caps = context->getCaps();
2054
Martin Radev4c4c8e72016-08-04 12:25:34 +03002055 if (mState.mAttachedComputeShader)
2056 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002057 Shader &computeShader = *mState.mAttachedComputeShader;
2058 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002059
2060 if (!validateUniformBlocksCount(
2061 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2062 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2063 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002065 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002067 return true;
2068 }
2069
Jamie Madillbd044ed2017-06-05 12:59:21 -04002070 Shader &vertexShader = *mState.mAttachedVertexShader;
2071 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002072
Jamie Madillbd044ed2017-06-05 12:59:21 -04002073 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2074 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002075
2076 if (!validateUniformBlocksCount(
2077 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2078 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2079 {
2080 return false;
2081 }
2082 if (!validateUniformBlocksCount(
2083 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2084 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2085 infoLog))
2086 {
2087
2088 return false;
2089 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002090
2091 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002092 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002093 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002094 {
2095 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 }
Jamie Madille473dee2015-08-18 14:49:01 -04002097
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 return true;
2099}
2100
Jamie Madilla2c74982016-12-12 11:20:42 -05002101bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002102 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002103 const sh::InterfaceBlock &fragmentInterfaceBlock,
2104 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105{
2106 const char* blockName = vertexInterfaceBlock.name.c_str();
2107 // validate blocks for the same member types
2108 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2109 {
Jamie Madillf6113162015-05-07 11:49:21 -04002110 infoLog << "Types for interface block '" << blockName
2111 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 return false;
2113 }
2114 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2115 {
Jamie Madillf6113162015-05-07 11:49:21 -04002116 infoLog << "Array sizes differ for interface block '" << blockName
2117 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118 return false;
2119 }
jchen10af713a22017-04-19 09:10:56 +08002120 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2121 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2122 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 {
Jamie Madillf6113162015-05-07 11:49:21 -04002124 infoLog << "Layout qualifiers differ for interface block '" << blockName
2125 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 return false;
2127 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002128 const unsigned int numBlockMembers =
2129 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2131 {
2132 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2133 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2134 if (vertexMember.name != fragmentMember.name)
2135 {
Jamie Madillf6113162015-05-07 11:49:21 -04002136 infoLog << "Name mismatch for field " << blockMemberIndex
2137 << " of interface block '" << blockName
2138 << "': (in vertex: '" << vertexMember.name
2139 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 return false;
2141 }
2142 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002143 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2144 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 {
2146 return false;
2147 }
2148 }
2149 return true;
2150}
2151
2152bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2153 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2154{
2155 if (vertexVariable.type != fragmentVariable.type)
2156 {
Jamie Madillf6113162015-05-07 11:49:21 -04002157 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158 return false;
2159 }
2160 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2161 {
Jamie Madillf6113162015-05-07 11:49:21 -04002162 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 return false;
2164 }
2165 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2166 {
Jamie Madillf6113162015-05-07 11:49:21 -04002167 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168 return false;
2169 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002170 if (vertexVariable.structName != fragmentVariable.structName)
2171 {
2172 infoLog << "Structure names for " << variableName
2173 << " differ between vertex and fragment shaders";
2174 return false;
2175 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176
2177 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2178 {
Jamie Madillf6113162015-05-07 11:49:21 -04002179 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180 return false;
2181 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002182 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2184 {
2185 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2186 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2187
2188 if (vertexMember.name != fragmentMember.name)
2189 {
Jamie Madillf6113162015-05-07 11:49:21 -04002190 infoLog << "Name mismatch for field '" << memberIndex
2191 << "' of " << variableName
2192 << ": (in vertex: '" << vertexMember.name
2193 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002194 return false;
2195 }
2196
2197 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2198 vertexMember.name + "'";
2199
2200 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2201 {
2202 return false;
2203 }
2204 }
2205
2206 return true;
2207}
2208
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002209bool Program::linkValidateVaryings(InfoLog &infoLog,
2210 const std::string &varyingName,
2211 const sh::Varying &vertexVarying,
2212 const sh::Varying &fragmentVarying,
2213 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002214{
2215 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2216 {
2217 return false;
2218 }
2219
Jamie Madille9cc4692015-02-19 16:00:13 -05002220 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002221 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002222 infoLog << "Interpolation types for " << varyingName
2223 << " differ between vertex and fragment shaders.";
2224 return false;
2225 }
2226
2227 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2228 {
2229 infoLog << "Invariance for " << varyingName
2230 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002231 return false;
2232 }
2233
2234 return true;
2235}
2236
Jamie Madillbd044ed2017-06-05 12:59:21 -04002237bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002238{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002239 Shader *vertexShader = mState.mAttachedVertexShader;
2240 Shader *fragmentShader = mState.mAttachedFragmentShader;
2241 const auto &vertexVaryings = vertexShader->getVaryings(context);
2242 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2243 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002244
2245 if (shaderVersion != 100)
2246 {
2247 // Only ESSL 1.0 has restrictions on matching input and output invariance
2248 return true;
2249 }
2250
2251 bool glPositionIsInvariant = false;
2252 bool glPointSizeIsInvariant = false;
2253 bool glFragCoordIsInvariant = false;
2254 bool glPointCoordIsInvariant = false;
2255
2256 for (const sh::Varying &varying : vertexVaryings)
2257 {
2258 if (!varying.isBuiltIn())
2259 {
2260 continue;
2261 }
2262 if (varying.name.compare("gl_Position") == 0)
2263 {
2264 glPositionIsInvariant = varying.isInvariant;
2265 }
2266 else if (varying.name.compare("gl_PointSize") == 0)
2267 {
2268 glPointSizeIsInvariant = varying.isInvariant;
2269 }
2270 }
2271
2272 for (const sh::Varying &varying : fragmentVaryings)
2273 {
2274 if (!varying.isBuiltIn())
2275 {
2276 continue;
2277 }
2278 if (varying.name.compare("gl_FragCoord") == 0)
2279 {
2280 glFragCoordIsInvariant = varying.isInvariant;
2281 }
2282 else if (varying.name.compare("gl_PointCoord") == 0)
2283 {
2284 glPointCoordIsInvariant = varying.isInvariant;
2285 }
2286 }
2287
2288 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2289 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2290 // Not requiring invariance to match is supported by:
2291 // dEQP, WebGL CTS, Nexus 5X GLES
2292 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2293 {
2294 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2295 "declared invariant.";
2296 return false;
2297 }
2298 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2299 {
2300 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2301 "declared invariant.";
2302 return false;
2303 }
2304
2305 return true;
2306}
2307
jchen10a9042d32017-03-17 08:50:45 +08002308bool Program::linkValidateTransformFeedback(const gl::Context *context,
2309 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002310 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002311 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002312{
2313 size_t totalComponents = 0;
2314
Jamie Madillccdf74b2015-08-18 10:46:12 -04002315 std::set<std::string> uniqueNames;
2316
Jamie Madill48ef11b2016-04-27 15:21:52 -04002317 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002318 {
2319 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002320 size_t subscript = GL_INVALID_INDEX;
2321 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2322
Jamie Madill192745a2016-12-22 15:58:21 -05002323 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002324 {
Jamie Madill192745a2016-12-22 15:58:21 -05002325 const sh::Varying *varying = ref.second.get();
2326
jchen10a9042d32017-03-17 08:50:45 +08002327 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002328 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002329 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002330 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002331 infoLog << "Two transform feedback varyings specify the same output variable ("
2332 << tfVaryingName << ").";
2333 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334 }
jchen10a9042d32017-03-17 08:50:45 +08002335 if (context->getClientVersion() >= Version(3, 1))
2336 {
2337 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2338 {
2339 infoLog
2340 << "Two transform feedback varyings include the same array element ("
2341 << tfVaryingName << ").";
2342 return false;
2343 }
2344 }
2345 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002346 {
2347 infoLog << "Capture of arrays is undefined and not supported.";
2348 return false;
2349 }
2350
jchen10a9042d32017-03-17 08:50:45 +08002351 uniqueNames.insert(tfVaryingName);
2352
Jamie Madillccdf74b2015-08-18 10:46:12 -04002353 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002354 size_t elementCount =
2355 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2356 : 1);
2357 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002358 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 componentCount > caps.maxTransformFeedbackSeparateComponents)
2360 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002361 infoLog << "Transform feedback varying's " << varying->name << " components ("
2362 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002363 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 return false;
2365 }
2366
2367 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368 found = true;
2369 break;
2370 }
2371 }
jchen10a9042d32017-03-17 08:50:45 +08002372 if (context->getClientVersion() < Version(3, 1) &&
2373 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002374 {
Geoff Lang1a683462015-09-29 15:09:59 -04002375 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002376 return false;
2377 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002378 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2379 ASSERT(found);
2380 }
2381
Jamie Madill48ef11b2016-04-27 15:21:52 -04002382 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002383 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002384 {
Jamie Madillf6113162015-05-07 11:49:21 -04002385 infoLog << "Transform feedback varying total components (" << totalComponents
2386 << ") exceed the maximum interleaved components ("
2387 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002388 return false;
2389 }
2390
2391 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002392}
2393
Yuly Novikovcaa5cda2017-06-15 21:14:03 -04002394bool Program::linkValidateGlobalNames(const Context *context, InfoLog &infoLog) const
2395{
2396 const std::vector<sh::Uniform> &vertexUniforms =
2397 mState.mAttachedVertexShader->getUniforms(context);
2398 const std::vector<sh::Uniform> &fragmentUniforms =
2399 mState.mAttachedFragmentShader->getUniforms(context);
2400 const std::vector<sh::Attribute> &attributes =
2401 mState.mAttachedVertexShader->getActiveAttributes(context);
2402 for (const auto &attrib : attributes)
2403 {
2404 for (const auto &uniform : vertexUniforms)
2405 {
2406 if (uniform.name == attrib.name)
2407 {
2408 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2409 return false;
2410 }
2411 }
2412 for (const auto &uniform : fragmentUniforms)
2413 {
2414 if (uniform.name == attrib.name)
2415 {
2416 infoLog << "Name conflicts between a uniform and an attribute: " << attrib.name;
2417 return false;
2418 }
2419 }
2420 }
2421 return true;
2422}
2423
Jamie Madill192745a2016-12-22 15:58:21 -05002424void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002425{
2426 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002427 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002428 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002429 {
jchen10a9042d32017-03-17 08:50:45 +08002430 size_t subscript = GL_INVALID_INDEX;
2431 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002432 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002433 {
Jamie Madill192745a2016-12-22 15:58:21 -05002434 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002435 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436 {
jchen10a9042d32017-03-17 08:50:45 +08002437 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2438 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002439 break;
2440 }
2441 }
2442 }
2443}
2444
Jamie Madillbd044ed2017-06-05 12:59:21 -04002445Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002446{
Jamie Madill192745a2016-12-22 15:58:21 -05002447 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002448
Jamie Madillbd044ed2017-06-05 12:59:21 -04002449 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002450 {
Jamie Madill192745a2016-12-22 15:58:21 -05002451 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002452 }
2453
Jamie Madillbd044ed2017-06-05 12:59:21 -04002454 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002455 {
Jamie Madill192745a2016-12-22 15:58:21 -05002456 merged[varying.name].fragment = &varying;
2457 }
2458
2459 return merged;
2460}
2461
2462std::vector<PackedVarying> Program::getPackedVaryings(
2463 const Program::MergedVaryings &mergedVaryings) const
2464{
2465 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2466 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002467 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002468
2469 for (const auto &ref : mergedVaryings)
2470 {
2471 const sh::Varying *input = ref.second.vertex;
2472 const sh::Varying *output = ref.second.fragment;
2473
2474 // Only pack varyings that have a matched input or output, plus special builtins.
2475 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002476 {
Jamie Madill192745a2016-12-22 15:58:21 -05002477 // Will get the vertex shader interpolation by default.
2478 auto interpolation = ref.second.get()->interpolation;
2479
Olli Etuaho06a06f52017-07-12 12:22:15 +03002480 // Note that we lose the vertex shader static use information here. The data for the
2481 // variable is taken from the fragment shader.
Jamie Madill192745a2016-12-22 15:58:21 -05002482 if (output->isStruct())
2483 {
2484 ASSERT(!output->isArray());
2485 for (const auto &field : output->fields)
2486 {
2487 ASSERT(!field.isStruct() && !field.isArray());
2488 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2489 }
2490 }
2491 else
2492 {
2493 packedVaryings.push_back(PackedVarying(*output, interpolation));
2494 }
2495 continue;
2496 }
2497
2498 // Keep Transform FB varyings in the merged list always.
2499 if (!input)
2500 {
2501 continue;
2502 }
2503
2504 for (const std::string &tfVarying : tfVaryings)
2505 {
jchen10a9042d32017-03-17 08:50:45 +08002506 size_t subscript = GL_INVALID_INDEX;
2507 std::string baseName = ParseResourceName(tfVarying, &subscript);
2508 if (uniqueFullNames.count(tfVarying) > 0)
2509 {
2510 continue;
2511 }
2512 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002513 {
2514 // Transform feedback for varying structs is underspecified.
2515 // See Khronos bug 9856.
2516 // TODO(jmadill): Figure out how to be spec-compliant here.
2517 if (!input->isStruct())
2518 {
2519 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2520 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002521 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2522 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002523 }
jchen10a9042d32017-03-17 08:50:45 +08002524 if (subscript == GL_INVALID_INDEX)
2525 {
2526 break;
2527 }
Jamie Madill192745a2016-12-22 15:58:21 -05002528 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002529 }
2530 }
2531
Jamie Madill192745a2016-12-22 15:58:21 -05002532 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2533
2534 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002535}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002536
Jamie Madillbd044ed2017-06-05 12:59:21 -04002537void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002538{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002539 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002540 ASSERT(fragmentShader != nullptr);
2541
Geoff Lange0cff192017-05-30 13:04:56 -04002542 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002543 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002544
2545 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002546 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002547 {
2548 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2549 outputVariable.name != "gl_FragData")
2550 {
2551 continue;
2552 }
2553
2554 unsigned int baseLocation =
2555 (outputVariable.location == -1 ? 0u
2556 : static_cast<unsigned int>(outputVariable.location));
2557 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2558 elementIndex++)
2559 {
2560 const unsigned int location = baseLocation + elementIndex;
2561 if (location >= mState.mOutputVariableTypes.size())
2562 {
2563 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2564 }
Corentin Walleze7557742017-06-01 13:09:57 -04002565 ASSERT(location < mState.mActiveOutputVariables.size());
2566 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002567 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2568 }
2569 }
2570
Jamie Madill80a6fc02015-08-21 16:53:16 -04002571 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002572 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002573 return;
2574
Jamie Madillbd044ed2017-06-05 12:59:21 -04002575 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002576 // TODO(jmadill): any caps validation here?
2577
jchen1015015f72017-03-16 13:54:21 +08002578 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002579 outputVariableIndex++)
2580 {
jchen1015015f72017-03-16 13:54:21 +08002581 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002582
2583 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2584 if (outputVariable.isBuiltIn())
2585 continue;
2586
2587 // Since multiple output locations must be specified, use 0 for non-specified locations.
2588 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2589
Jamie Madill80a6fc02015-08-21 16:53:16 -04002590 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2591 elementIndex++)
2592 {
2593 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002594 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002595 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002596 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002597 VariableLocation(outputVariable.name, element, outputVariableIndex);
2598 }
2599 }
2600}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002601
Olli Etuaho48fed632017-03-16 12:05:30 +00002602void Program::setUniformValuesFromBindingQualifiers()
2603{
Jamie Madill982f6e02017-06-07 14:33:04 -04002604 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002605 {
2606 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2607 if (samplerUniform.binding != -1)
2608 {
2609 GLint location = mState.getUniformLocation(samplerUniform.name);
2610 ASSERT(location != -1);
2611 std::vector<GLint> boundTextureUnits;
2612 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2613 ++elementIndex)
2614 {
2615 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2616 }
2617 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2618 boundTextureUnits.data());
2619 }
2620 }
2621}
2622
jchen10eaef1e52017-06-13 10:44:11 +08002623void Program::gatherAtomicCounterBuffers()
2624{
2625 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2626 // counter.
2627 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2628}
2629
Jamie Madillbd044ed2017-06-05 12:59:21 -04002630void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002631{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002632 ASSERT(mState.mUniformBlocks.empty());
2633
2634 if (mState.mAttachedComputeShader)
2635 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002636 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002637
Jamie Madillbd044ed2017-06-05 12:59:21 -04002638 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002639 {
2640
2641 // Only 'packed' blocks are allowed to be considered inactive.
2642 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2643 continue;
2644
Jamie Madilla2c74982016-12-12 11:20:42 -05002645 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002646 {
2647 if (block.name == computeBlock.name)
2648 {
2649 block.computeStaticUse = computeBlock.staticUse;
2650 }
2651 }
2652
2653 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2654 }
2655 return;
2656 }
2657
Jamie Madill62d31cb2015-09-11 13:25:51 -04002658 std::set<std::string> visitedList;
2659
Jamie Madillbd044ed2017-06-05 12:59:21 -04002660 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002661
Jamie Madillbd044ed2017-06-05 12:59:21 -04002662 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002663 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002664 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002665 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2666 continue;
2667
2668 if (visitedList.count(vertexBlock.name) > 0)
2669 continue;
2670
2671 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2672 visitedList.insert(vertexBlock.name);
2673 }
2674
Jamie Madillbd044ed2017-06-05 12:59:21 -04002675 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002676
Jamie Madillbd044ed2017-06-05 12:59:21 -04002677 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002678 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002679 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002680 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2681 continue;
2682
2683 if (visitedList.count(fragmentBlock.name) > 0)
2684 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002685 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002686 {
2687 if (block.name == fragmentBlock.name)
2688 {
2689 block.fragmentStaticUse = fragmentBlock.staticUse;
2690 }
2691 }
2692
2693 continue;
2694 }
2695
2696 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2697 visitedList.insert(fragmentBlock.name);
2698 }
jchen10af713a22017-04-19 09:10:56 +08002699 // Set initial bindings from shader.
2700 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2701 {
2702 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2703 bindUniformBlock(blockIndex, uniformBlock.binding);
2704 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002705}
2706
Jamie Madill4a3c2342015-10-08 12:58:45 -04002707template <typename VarT>
2708void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2709 const std::string &prefix,
2710 int blockIndex)
2711{
2712 for (const VarT &field : fields)
2713 {
2714 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2715
2716 if (field.isStruct())
2717 {
2718 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2719 {
2720 const std::string uniformElementName =
2721 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2722 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2723 }
2724 }
2725 else
2726 {
2727 // If getBlockMemberInfo returns false, the uniform is optimized out.
2728 sh::BlockMemberInfo memberInfo;
2729 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2730 {
2731 continue;
2732 }
2733
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002734 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002735 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002736
2737 // Since block uniforms have no location, we don't need to store them in the uniform
2738 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002739 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002740 }
2741 }
2742}
2743
Jamie Madill62d31cb2015-09-11 13:25:51 -04002744void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2745{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002746 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002747 size_t blockSize = 0;
2748
Jamie Madill4a3c2342015-10-08 12:58:45 -04002749 // Track the first and last uniform index to determine the range of active uniforms in the
2750 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002751 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002752 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002753 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002754
2755 std::vector<unsigned int> blockUniformIndexes;
2756 for (size_t blockUniformIndex = firstBlockUniformIndex;
2757 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2758 {
2759 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2760 }
jchen10af713a22017-04-19 09:10:56 +08002761 // ESSL 3.10 section 4.4.4 page 58:
2762 // Any uniform or shader storage block declared without a binding qualifier is initially
2763 // assigned to block binding point zero.
2764 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002765 if (interfaceBlock.arraySize > 0)
2766 {
2767 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2768 {
jchen10af713a22017-04-19 09:10:56 +08002769 // Don't define this block at all if it's not active in the implementation.
2770 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2771 &blockSize))
2772 {
2773 continue;
2774 }
2775 UniformBlock block(interfaceBlock.name, true, arrayElement,
2776 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002777 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002778
Martin Radev4c4c8e72016-08-04 12:25:34 +03002779 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002780 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002781 case GL_VERTEX_SHADER:
2782 {
2783 block.vertexStaticUse = interfaceBlock.staticUse;
2784 break;
2785 }
2786 case GL_FRAGMENT_SHADER:
2787 {
2788 block.fragmentStaticUse = interfaceBlock.staticUse;
2789 break;
2790 }
2791 case GL_COMPUTE_SHADER:
2792 {
2793 block.computeStaticUse = interfaceBlock.staticUse;
2794 break;
2795 }
2796 default:
2797 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002798 }
2799
Qin Jiajia0350a642016-11-01 17:01:51 +08002800 // Since all block elements in an array share the same active uniforms, they will all be
2801 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2802 // here we will add every block element in the array.
2803 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002804 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002805 }
2806 }
2807 else
2808 {
jchen10af713a22017-04-19 09:10:56 +08002809 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2810 {
2811 return;
2812 }
2813 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002814 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002815
Martin Radev4c4c8e72016-08-04 12:25:34 +03002816 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002818 case GL_VERTEX_SHADER:
2819 {
2820 block.vertexStaticUse = interfaceBlock.staticUse;
2821 break;
2822 }
2823 case GL_FRAGMENT_SHADER:
2824 {
2825 block.fragmentStaticUse = interfaceBlock.staticUse;
2826 break;
2827 }
2828 case GL_COMPUTE_SHADER:
2829 {
2830 block.computeStaticUse = interfaceBlock.staticUse;
2831 break;
2832 }
2833 default:
2834 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002835 }
2836
Jamie Madill4a3c2342015-10-08 12:58:45 -04002837 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002838 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002839 }
2840}
2841
Jamie Madille7d84322017-01-10 18:21:59 -05002842template <>
2843void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2844 const uint8_t *destPointer,
2845 GLsizei clampedCount,
2846 const GLint *v)
2847{
2848 // Invalidate the validation cache only if we modify the sampler data.
2849 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2850 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2851 {
2852 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2853 std::vector<GLuint> *boundTextureUnits =
2854 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2855
2856 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2857 mCachedValidateSamplersResult.reset();
2858 }
2859}
2860
2861template <typename T>
2862void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2863 const uint8_t *destPointer,
2864 GLsizei clampedCount,
2865 const T *v)
2866{
2867}
2868
Jamie Madill62d31cb2015-09-11 13:25:51 -04002869template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002870GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002871{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002872 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2873 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002874 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2875
Corentin Wallez15ac5342016-11-03 17:06:39 -04002876 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2877 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2878 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002879 GLsizei maxElementCount =
2880 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2881
2882 GLsizei count = countIn;
2883 GLsizei clampedCount = count * vectorSize;
2884 if (clampedCount > maxElementCount)
2885 {
2886 clampedCount = maxElementCount;
2887 count = maxElementCount / vectorSize;
2888 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002889
Jamie Madill62d31cb2015-09-11 13:25:51 -04002890 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2891 {
2892 // Do a cast conversion for boolean types. From the spec:
2893 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2894 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002895 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002896 {
2897 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2898 }
2899 }
2900 else
2901 {
Jamie Madille7d84322017-01-10 18:21:59 -05002902 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002903 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002904 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002905
2906 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002907}
2908
2909template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002910GLsizei Program::setMatrixUniformInternal(GLint location,
2911 GLsizei count,
2912 GLboolean transpose,
2913 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002914{
2915 if (!transpose)
2916 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002917 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002918 }
2919
2920 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002921 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2922 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002923 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002924
2925 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2926 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2927 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2928 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2929
2930 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002931 {
2932 size_t elementOffset = element * rows * cols;
2933
2934 for (size_t row = 0; row < rows; ++row)
2935 {
2936 for (size_t col = 0; col < cols; ++col)
2937 {
2938 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2939 }
2940 }
2941 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002942
2943 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002944}
2945
2946template <typename DestT>
2947void Program::getUniformInternal(GLint location, DestT *dataOut) const
2948{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002949 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2950 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002951
2952 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2953
2954 GLenum componentType = VariableComponentType(uniform.type);
2955 if (componentType == GLTypeToGLenum<DestT>::value)
2956 {
2957 memcpy(dataOut, srcPointer, uniform.getElementSize());
2958 return;
2959 }
2960
Corentin Wallez6596c462016-03-17 17:26:58 -04002961 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002962
2963 switch (componentType)
2964 {
2965 case GL_INT:
2966 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2967 break;
2968 case GL_UNSIGNED_INT:
2969 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2970 break;
2971 case GL_BOOL:
2972 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2973 break;
2974 case GL_FLOAT:
2975 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2976 break;
2977 default:
2978 UNREACHABLE();
2979 }
2980}
Jamie Madilla4595b82017-01-11 17:36:34 -05002981
2982bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2983{
2984 // Must be called after samplers are validated.
2985 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2986
2987 for (const auto &binding : mState.mSamplerBindings)
2988 {
2989 GLenum textureType = binding.textureType;
2990 for (const auto &unit : binding.boundTextureUnits)
2991 {
2992 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2993 if (programTextureID == textureID)
2994 {
2995 // TODO(jmadill): Check for appropriate overlap.
2996 return true;
2997 }
2998 }
2999 }
3000
3001 return false;
3002}
3003
Jamie Madilla2c74982016-12-12 11:20:42 -05003004} // namespace gl