blob: a506dc881fac653ec072f5d10817879c675f2549 [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
Jamie Madillbd044ed2017-06-05 12:59:21 -0400730 const auto &mergedVaryings = getMergedVaryings(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300731
jchen10a9042d32017-03-17 08:50:45 +0800732 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733 {
734 return NoError();
735 }
736
Jamie Madillbd044ed2017-06-05 12:59:21 -0400737 linkOutputVariables(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300738
Jamie Madill192745a2016-12-22 15:58:21 -0500739 // Validate we can pack the varyings.
740 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
741
742 // Map the varyings to the register file
743 // In WebGL, we use a slightly different handling for packing variables.
744 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
745 : PackMode::ANGLE_RELAXED;
746 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
747 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
748 mState.getTransformFeedbackVaryingNames()))
749 {
750 return NoError();
751 }
752
Jamie Madillc564c072017-06-01 12:45:42 -0400753 ANGLE_TRY_RESULT(mProgram->link(context, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500754 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300755 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500756 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300757 }
758
759 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500760 }
761
jchen10eaef1e52017-06-13 10:44:11 +0800762 gatherAtomicCounterBuffers();
Jamie Madillbd044ed2017-06-05 12:59:21 -0400763 gatherInterfaceBlockInfo(context);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400764
jchen10eaef1e52017-06-13 10:44:11 +0800765 setUniformValuesFromBindingQualifiers();
766
Jamie Madill32447362017-06-28 14:53:52 -0400767 // Save to the program cache.
768 if (cache && (mState.mLinkedTransformFeedbackVaryings.empty() ||
769 !context->getWorkarounds().disableProgramCachingForTransformFeedback))
770 {
771 cache->putProgram(programHash, context, this);
772 }
773
Martin Radev4c4c8e72016-08-04 12:25:34 +0300774 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000775}
776
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000777// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500778void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 mState.mAttributes.clear();
781 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800782 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400783 mState.mUniforms.clear();
784 mState.mUniformLocations.clear();
785 mState.mUniformBlocks.clear();
jchen107a20b972017-06-13 14:25:26 +0800786 mState.mActiveUniformBlockBindings.reset();
jchen10eaef1e52017-06-13 10:44:11 +0800787 mState.mAtomicCounterBuffers.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400788 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800789 mState.mOutputLocations.clear();
Geoff Lange0cff192017-05-30 13:04:56 -0400790 mState.mOutputVariableTypes.clear();
Corentin Walleze7557742017-06-01 13:09:57 -0400791 mState.mActiveOutputVariables.reset();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300792 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500793 mState.mSamplerBindings.clear();
jchen10eaef1e52017-06-13 10:44:11 +0800794 mState.mImageBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500795
Geoff Lang7dd2e102014-11-10 15:19:26 -0500796 mValidated = false;
797
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000798 mLinked = false;
799}
800
Geoff Lange1a27752015-10-05 13:16:04 -0400801bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000802{
803 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804}
805
Jamie Madilla2c74982016-12-12 11:20:42 -0500806Error Program::loadBinary(const Context *context,
807 GLenum binaryFormat,
808 const void *binary,
809 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000810{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500811 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000812
Geoff Lang7dd2e102014-11-10 15:19:26 -0500813#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800814 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500815#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400816 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
817 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000818 {
Jamie Madillf6113162015-05-07 11:49:21 -0400819 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800820 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500821 }
822
Jamie Madill4f86d052017-06-05 12:59:26 -0400823 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(binary);
824 ANGLE_TRY_RESULT(
825 MemoryProgramCache::Deserialize(context, this, &mState, bytes, length, mInfoLog), mLinked);
Jamie Madill32447362017-06-28 14:53:52 -0400826
827 // Currently we require the full shader text to compute the program hash.
828 // TODO(jmadill): Store the binary in the internal program cache.
829
Jamie Madillb0a838b2016-11-13 20:02:12 -0500830 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500831#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500832}
833
Jamie Madilla2c74982016-12-12 11:20:42 -0500834Error Program::saveBinary(const Context *context,
835 GLenum *binaryFormat,
836 void *binary,
837 GLsizei bufSize,
838 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500839{
840 if (binaryFormat)
841 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400842 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500843 }
844
Jamie Madill4f86d052017-06-05 12:59:26 -0400845 angle::MemoryBuffer memoryBuf;
846 MemoryProgramCache::Serialize(context, this, &memoryBuf);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500847
Jamie Madill4f86d052017-06-05 12:59:26 -0400848 GLsizei streamLength = static_cast<GLsizei>(memoryBuf.size());
849 const uint8_t *streamState = memoryBuf.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500850
851 if (streamLength > bufSize)
852 {
853 if (length)
854 {
855 *length = 0;
856 }
857
858 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
859 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
860 // sizes and then copy it.
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500861 return InternalError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500862 }
863
864 if (binary)
865 {
866 char *ptr = reinterpret_cast<char*>(binary);
867
Jamie Madill48ef11b2016-04-27 15:21:52 -0400868 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500869 ptr += streamLength;
870
871 ASSERT(ptr - streamLength == binary);
872 }
873
874 if (length)
875 {
876 *length = streamLength;
877 }
878
He Yunchaoacd18982017-01-04 10:46:42 +0800879 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500880}
881
Jamie Madillffe00c02017-06-27 16:26:55 -0400882GLint Program::getBinaryLength(const Context *context) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500883{
884 GLint length;
Jamie Madillffe00c02017-06-27 16:26:55 -0400885 Error error = saveBinary(context, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500886 if (error.isError())
887 {
888 return 0;
889 }
890
891 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000892}
893
Geoff Langc5629752015-12-07 16:29:04 -0500894void Program::setBinaryRetrievableHint(bool retrievable)
895{
896 // TODO(jmadill) : replace with dirty bits
897 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400898 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500899}
900
901bool Program::getBinaryRetrievableHint() const
902{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400903 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500904}
905
Yunchao He61afff12017-03-14 15:34:03 +0800906void Program::setSeparable(bool separable)
907{
908 // TODO(yunchao) : replace with dirty bits
909 if (mState.mSeparable != separable)
910 {
911 mProgram->setSeparable(separable);
912 mState.mSeparable = separable;
913 }
914}
915
916bool Program::isSeparable() const
917{
918 return mState.mSeparable;
919}
920
Jamie Madill6c1f6712017-02-14 19:08:04 -0500921void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000922{
923 mRefCount--;
924
925 if (mRefCount == 0 && mDeleteStatus)
926 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500927 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928 }
929}
930
931void Program::addRef()
932{
933 mRefCount++;
934}
935
936unsigned int Program::getRefCount() const
937{
938 return mRefCount;
939}
940
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000941int Program::getInfoLogLength() const
942{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400943 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000944}
945
Geoff Lange1a27752015-10-05 13:16:04 -0400946void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000947{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000948 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000949}
950
Geoff Lange1a27752015-10-05 13:16:04 -0400951void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000952{
953 int total = 0;
954
Martin Radev4c4c8e72016-08-04 12:25:34 +0300955 if (mState.mAttachedComputeShader)
956 {
957 if (total < maxCount)
958 {
959 shaders[total] = mState.mAttachedComputeShader->getHandle();
960 total++;
961 }
962 }
963
Jamie Madill48ef11b2016-04-27 15:21:52 -0400964 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000965 {
966 if (total < maxCount)
967 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400968 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200969 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000970 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000971 }
972
Jamie Madill48ef11b2016-04-27 15:21:52 -0400973 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000974 {
975 if (total < maxCount)
976 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400977 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200978 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000979 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000980 }
981
982 if (count)
983 {
984 *count = total;
985 }
986}
987
Geoff Lange1a27752015-10-05 13:16:04 -0400988GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500989{
Jamie Madill34ca4f52017-06-13 11:49:39 -0400990 return mState.getAttributeLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500991}
992
Jamie Madill63805b42015-08-25 13:17:39 -0400993bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400994{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400995 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
996 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997}
998
jchen10fd7c3b52017-03-21 15:36:03 +0800999void Program::getActiveAttribute(GLuint index,
1000 GLsizei bufsize,
1001 GLsizei *length,
1002 GLint *size,
1003 GLenum *type,
1004 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001005{
Jamie Madillc349ec02015-08-21 16:53:12 -04001006 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001007 {
1008 if (bufsize > 0)
1009 {
1010 name[0] = '\0';
1011 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001012
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001013 if (length)
1014 {
1015 *length = 0;
1016 }
1017
1018 *type = GL_NONE;
1019 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001020 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001021 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001022
jchen1036e120e2017-03-14 14:53:58 +08001023 ASSERT(index < mState.mAttributes.size());
1024 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001025
1026 if (bufsize > 0)
1027 {
jchen10fd7c3b52017-03-21 15:36:03 +08001028 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001029 }
1030
1031 // Always a single 'type' instance
1032 *size = 1;
1033 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001034}
1035
Geoff Lange1a27752015-10-05 13:16:04 -04001036GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001037{
Jamie Madillc349ec02015-08-21 16:53:12 -04001038 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001039 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001040 return 0;
1041 }
1042
jchen1036e120e2017-03-14 14:53:58 +08001043 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001044}
1045
Geoff Lange1a27752015-10-05 13:16:04 -04001046GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001047{
Jamie Madillc349ec02015-08-21 16:53:12 -04001048 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001049 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001050 return 0;
1051 }
1052
1053 size_t maxLength = 0;
1054
Jamie Madill48ef11b2016-04-27 15:21:52 -04001055 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001056 {
jchen1036e120e2017-03-14 14:53:58 +08001057 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001058 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001059
Jamie Madillc349ec02015-08-21 16:53:12 -04001060 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001061}
1062
jchen1015015f72017-03-16 13:54:21 +08001063GLuint Program::getInputResourceIndex(const GLchar *name) const
1064{
1065 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1066 {
1067 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1068 if (attribute.name == name)
1069 {
1070 return attributeIndex;
1071 }
1072 }
1073 return GL_INVALID_INDEX;
1074}
1075
1076GLuint Program::getOutputResourceIndex(const GLchar *name) const
1077{
1078 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1079}
1080
jchen10fd7c3b52017-03-21 15:36:03 +08001081size_t Program::getOutputResourceCount() const
1082{
1083 return (mLinked ? mState.mOutputVariables.size() : 0);
1084}
1085
1086void Program::getInputResourceName(GLuint index,
1087 GLsizei bufSize,
1088 GLsizei *length,
1089 GLchar *name) const
1090{
1091 GLint size;
1092 GLenum type;
1093 getActiveAttribute(index, bufSize, length, &size, &type, name);
1094}
1095
1096void Program::getOutputResourceName(GLuint index,
1097 GLsizei bufSize,
1098 GLsizei *length,
1099 GLchar *name) const
1100{
1101 if (length)
1102 {
1103 *length = 0;
1104 }
1105
1106 if (!mLinked)
1107 {
1108 if (bufSize > 0)
1109 {
1110 name[0] = '\0';
1111 }
1112 return;
1113 }
1114 ASSERT(index < mState.mOutputVariables.size());
1115 const auto &output = mState.mOutputVariables[index];
1116
1117 if (bufSize > 0)
1118 {
1119 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1120
1121 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1122 }
1123}
1124
Geoff Lang7dd2e102014-11-10 15:19:26 -05001125GLint Program::getFragDataLocation(const std::string &name) const
1126{
1127 std::string baseName(name);
1128 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001129 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001130 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001131 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001132 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1133 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001134 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001135 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001136 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001137 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001138}
1139
Geoff Lange1a27752015-10-05 13:16:04 -04001140void Program::getActiveUniform(GLuint index,
1141 GLsizei bufsize,
1142 GLsizei *length,
1143 GLint *size,
1144 GLenum *type,
1145 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001146{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001148 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001149 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001150 ASSERT(index < mState.mUniforms.size());
1151 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152
1153 if (bufsize > 0)
1154 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001155 std::string string = uniform.name;
1156 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157 {
1158 string += "[0]";
1159 }
jchen10fd7c3b52017-03-21 15:36:03 +08001160 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001161 }
1162
Jamie Madill62d31cb2015-09-11 13:25:51 -04001163 *size = uniform.elementCount();
1164 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001165 }
1166 else
1167 {
1168 if (bufsize > 0)
1169 {
1170 name[0] = '\0';
1171 }
1172
1173 if (length)
1174 {
1175 *length = 0;
1176 }
1177
1178 *size = 0;
1179 *type = GL_NONE;
1180 }
1181}
1182
Geoff Lange1a27752015-10-05 13:16:04 -04001183GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001185 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001186 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001187 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001188 }
1189 else
1190 {
1191 return 0;
1192 }
1193}
1194
Geoff Lange1a27752015-10-05 13:16:04 -04001195GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001196{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001197 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198
1199 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001200 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001201 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001203 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001204 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001205 size_t length = uniform.name.length() + 1u;
1206 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207 {
1208 length += 3; // Counting in "[0]".
1209 }
1210 maxLength = std::max(length, maxLength);
1211 }
1212 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001213 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214
Jamie Madill62d31cb2015-09-11 13:25:51 -04001215 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216}
1217
1218GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1219{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001220 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001221 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001223 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001224 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1225 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1226 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
jchen10eaef1e52017-06-13 10:44:11 +08001227 case GL_UNIFORM_BLOCK_INDEX:
1228 return uniform.bufferIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001229 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1230 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1231 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1232 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1233 default:
1234 UNREACHABLE();
1235 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001236 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237 return 0;
1238}
1239
1240bool Program::isValidUniformLocation(GLint location) const
1241{
Jamie Madille2e406c2016-06-02 13:04:10 -04001242 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001243 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1244 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001245}
1246
Jamie Madill62d31cb2015-09-11 13:25:51 -04001247const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001248{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001249 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001250 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251}
1252
Jamie Madillac4e9c32017-01-13 14:07:12 -05001253const VariableLocation &Program::getUniformLocation(GLint location) const
1254{
1255 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1256 return mState.mUniformLocations[location];
1257}
1258
1259const std::vector<VariableLocation> &Program::getUniformLocations() const
1260{
1261 return mState.mUniformLocations;
1262}
1263
1264const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1265{
1266 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1267 return mState.mUniforms[index];
1268}
1269
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001272 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273}
1274
Jamie Madill62d31cb2015-09-11 13:25:51 -04001275GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001276{
Jamie Madille7d84322017-01-10 18:21:59 -05001277 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278}
1279
1280void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1281{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001282 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1283 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001284}
1285
1286void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1287{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001288 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1289 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290}
1291
1292void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1293{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001294 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1295 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001296}
1297
1298void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1299{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001300 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1301 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302}
1303
1304void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1305{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001306 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1307 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308}
1309
1310void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1311{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001312 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1313 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001314}
1315
1316void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1317{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001318 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1319 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001320}
1321
1322void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1323{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001324 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1325 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326}
1327
1328void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1329{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001330 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1331 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
1334void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1335{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001336 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1337 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001338}
1339
1340void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1341{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001342 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1343 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344}
1345
1346void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1347{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001348 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1349 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350}
1351
1352void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1353{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001354 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1355 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356}
1357
1358void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1359{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001360 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1361 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362}
1363
1364void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1365{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001366 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1367 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368}
1369
1370void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1371{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001372 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1373 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
1376void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1377{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001378 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1379 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380}
1381
1382void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1383{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001384 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1385 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386}
1387
1388void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1389{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001390 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1391 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392}
1393
1394void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1395{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001396 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1397 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398}
1399
1400void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1401{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001402 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1403 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404}
1405
Geoff Lange1a27752015-10-05 13:16:04 -04001406void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001408 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
Geoff Lange1a27752015-10-05 13:16:04 -04001411void Program::getUniformiv(GLint location, GLint *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::getUniformuiv(GLint location, GLuint *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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421void Program::flagForDeletion()
1422{
1423 mDeleteStatus = true;
1424}
1425
1426bool Program::isFlaggedForDeletion() const
1427{
1428 return mDeleteStatus;
1429}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001430
Brandon Jones43a53e22014-08-28 16:23:22 -07001431void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001432{
1433 mInfoLog.reset();
1434
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001436 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001437 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001438 }
1439 else
1440 {
Jamie Madillf6113162015-05-07 11:49:21 -04001441 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001442 }
1443}
1444
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1446{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001447 // Skip cache if we're using an infolog, so we get the full error.
1448 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1449 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1450 {
1451 return mCachedValidateSamplersResult.value();
1452 }
1453
1454 if (mTextureUnitTypesCache.empty())
1455 {
1456 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1457 }
1458 else
1459 {
1460 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1461 }
1462
1463 // if any two active samplers in a program are of different types, but refer to the same
1464 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1465 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001466 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001467 {
Jamie Madille7d84322017-01-10 18:21:59 -05001468 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001469
Jamie Madille7d84322017-01-10 18:21:59 -05001470 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001471 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001472 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1473 {
1474 if (infoLog)
1475 {
1476 (*infoLog) << "Sampler uniform (" << textureUnit
1477 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1478 << caps.maxCombinedTextureImageUnits << ")";
1479 }
1480
1481 mCachedValidateSamplersResult = false;
1482 return false;
1483 }
1484
1485 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1486 {
1487 if (textureType != mTextureUnitTypesCache[textureUnit])
1488 {
1489 if (infoLog)
1490 {
1491 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1492 "image unit ("
1493 << textureUnit << ").";
1494 }
1495
1496 mCachedValidateSamplersResult = false;
1497 return false;
1498 }
1499 }
1500 else
1501 {
1502 mTextureUnitTypesCache[textureUnit] = textureType;
1503 }
1504 }
1505 }
1506
1507 mCachedValidateSamplersResult = true;
1508 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509}
1510
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001511bool Program::isValidated() const
1512{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513 return mValidated;
1514}
1515
Geoff Lange1a27752015-10-05 13:16:04 -04001516GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001518 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001519}
1520
1521void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1522{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001523 ASSERT(
1524 uniformBlockIndex <
1525 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001526
Jamie Madill48ef11b2016-04-27 15:21:52 -04001527 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528
1529 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001530 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001531 std::string string = uniformBlock.name;
1532
Jamie Madill62d31cb2015-09-11 13:25:51 -04001533 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001534 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001535 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001536 }
jchen10fd7c3b52017-03-21 15:36:03 +08001537 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001538 }
1539}
1540
Geoff Lange1a27752015-10-05 13:16:04 -04001541GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001542{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543 int maxLength = 0;
1544
1545 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001546 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001547 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1549 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001550 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001551 if (!uniformBlock.name.empty())
1552 {
jchen10af713a22017-04-19 09:10:56 +08001553 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1554 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555 }
1556 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001557 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001558
1559 return maxLength;
1560}
1561
Geoff Lange1a27752015-10-05 13:16:04 -04001562GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001563{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001564 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001565 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001566
Jamie Madill48ef11b2016-04-27 15:21:52 -04001567 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001568 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1569 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001570 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001571 if (uniformBlock.name == baseName)
1572 {
1573 const bool arrayElementZero =
1574 (subscript == GL_INVALID_INDEX &&
1575 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1576 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1577 {
1578 return blockIndex;
1579 }
1580 }
1581 }
1582
1583 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001584}
1585
Jamie Madill62d31cb2015-09-11 13:25:51 -04001586const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001587{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001588 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1589 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001590}
1591
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001592void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1593{
jchen107a20b972017-06-13 14:25:26 +08001594 mState.mUniformBlocks[uniformBlockIndex].binding = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001595 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001596 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001597}
1598
1599GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1600{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001601 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001602}
1603
Geoff Lang48dcae72014-02-05 16:28:24 -05001604void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1605{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001606 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001607 for (GLsizei i = 0; i < count; i++)
1608 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001609 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001610 }
1611
Jamie Madill48ef11b2016-04-27 15:21:52 -04001612 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001613}
1614
1615void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1616{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001618 {
jchen10a9042d32017-03-17 08:50:45 +08001619 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1620 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1621 std::string varName = var.nameWithArrayIndex();
1622 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001623 if (length)
1624 {
1625 *length = lastNameIdx;
1626 }
1627 if (size)
1628 {
jchen10a9042d32017-03-17 08:50:45 +08001629 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001630 }
1631 if (type)
1632 {
jchen10a9042d32017-03-17 08:50:45 +08001633 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001634 }
1635 if (name)
1636 {
jchen10a9042d32017-03-17 08:50:45 +08001637 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001638 name[lastNameIdx] = '\0';
1639 }
1640 }
1641}
1642
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001643GLsizei Program::getTransformFeedbackVaryingCount() const
1644{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001646 {
jchen10a9042d32017-03-17 08:50:45 +08001647 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001648 }
1649 else
1650 {
1651 return 0;
1652 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001653}
1654
1655GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1656{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001657 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001658 {
1659 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001660 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001661 {
jchen10a9042d32017-03-17 08:50:45 +08001662 maxSize =
1663 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001664 }
1665
1666 return maxSize;
1667 }
1668 else
1669 {
1670 return 0;
1671 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001672}
1673
1674GLenum Program::getTransformFeedbackBufferMode() const
1675{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001676 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677}
1678
Jamie Madillbd044ed2017-06-05 12:59:21 -04001679bool Program::linkVaryings(const Context *context, InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001681 Shader *vertexShader = mState.mAttachedVertexShader;
1682 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill192745a2016-12-22 15:58:21 -05001683
Jamie Madillbd044ed2017-06-05 12:59:21 -04001684 ASSERT(vertexShader->getShaderVersion(context) == fragmentShader->getShaderVersion(context));
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001685
Jamie Madillbd044ed2017-06-05 12:59:21 -04001686 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings(context);
1687 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688
Sami Väisänen46eaa942016-06-29 10:26:37 +03001689 std::map<GLuint, std::string> staticFragmentInputLocations;
1690
Jamie Madill4cff2472015-08-21 16:53:18 -04001691 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001692 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693 bool matched = false;
1694
1695 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001696 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697 {
1698 continue;
1699 }
1700
Jamie Madill4cff2472015-08-21 16:53:18 -04001701 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001702 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001703 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001705 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001706 if (!linkValidateVaryings(infoLog, output.name, input, output,
Jamie Madillbd044ed2017-06-05 12:59:21 -04001707 vertexShader->getShaderVersion(context)))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001708 {
1709 return false;
1710 }
1711
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712 matched = true;
1713 break;
1714 }
1715 }
1716
1717 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001718 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001719 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001720 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001721 return false;
1722 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001723
1724 // Check for aliased path rendering input bindings (if any).
1725 // If more than one binding refer statically to the same
1726 // location the link must fail.
1727
1728 if (!output.staticUse)
1729 continue;
1730
1731 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1732 if (inputBinding == -1)
1733 continue;
1734
1735 const auto it = staticFragmentInputLocations.find(inputBinding);
1736 if (it == std::end(staticFragmentInputLocations))
1737 {
1738 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1739 }
1740 else
1741 {
1742 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1743 << it->second;
1744 return false;
1745 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001746 }
1747
Jamie Madillbd044ed2017-06-05 12:59:21 -04001748 if (!linkValidateBuiltInVaryings(context, infoLog))
Yuly Novikov817232e2017-02-22 18:36:10 -05001749 {
1750 return false;
1751 }
1752
Jamie Madillada9ecc2015-08-17 12:53:37 -04001753 // TODO(jmadill): verify no unmatched vertex varyings?
1754
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755 return true;
1756}
1757
Jamie Madillbd044ed2017-06-05 12:59:21 -04001758bool Program::linkUniforms(const Context *context,
1759 InfoLog &infoLog,
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001760 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001761{
Olli Etuahob78707c2017-03-09 15:03:11 +00001762 UniformLinker linker(mState);
Jamie Madillbd044ed2017-06-05 12:59:21 -04001763 if (!linker.link(context, infoLog, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001764 {
1765 return false;
1766 }
1767
Olli Etuahob78707c2017-03-09 15:03:11 +00001768 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001769
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001770 linkSamplerAndImageBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001771
jchen10eaef1e52017-06-13 10:44:11 +08001772 if (!linkAtomicCounterBuffers())
1773 {
1774 return false;
1775 }
1776
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001777 return true;
1778}
1779
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001780void Program::linkSamplerAndImageBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001781{
Jamie Madill982f6e02017-06-07 14:33:04 -04001782 unsigned int high = static_cast<unsigned int>(mState.mUniforms.size());
1783 unsigned int low = high;
1784
jchen10eaef1e52017-06-13 10:44:11 +08001785 for (auto counterIter = mState.mUniforms.rbegin();
1786 counterIter != mState.mUniforms.rend() && counterIter->isAtomicCounter(); ++counterIter)
1787 {
1788 --low;
1789 }
1790
1791 mState.mAtomicCounterUniformRange = RangeUI(low, high);
1792
1793 high = low;
1794
Xinghua Cao65ec0b22017-03-28 16:10:52 +08001795 for (auto imageIter = mState.mUniforms.rbegin();
1796 imageIter != mState.mUniforms.rend() && imageIter->isImage(); ++imageIter)
1797 {
1798 --low;
1799 }
1800
1801 mState.mImageUniformRange = RangeUI(low, high);
1802
1803 // If uniform is a image type, insert it into the mImageBindings array.
1804 for (unsigned int imageIndex : mState.mImageUniformRange)
1805 {
1806 // ES3.1 (section 7.6.1) and GLSL ES3.1 (section 4.4.5), Uniform*i{v}
1807 // commands cannot load values into a uniform defined as an image,
1808 // if declare without a binding qualifier, the image variable is
1809 // initially bound to unit zero.
1810 auto &imageUniform = mState.mUniforms[imageIndex];
1811 if (imageUniform.binding == -1)
1812 {
1813 imageUniform.binding = 0;
1814 }
1815 mState.mImageBindings.emplace_back(
1816 ImageBinding(imageUniform.binding, imageUniform.elementCount()));
1817 }
1818
1819 high = low;
1820
1821 for (auto samplerIter = mState.mUniforms.rbegin() + mState.mImageUniformRange.length();
Jamie Madill982f6e02017-06-07 14:33:04 -04001822 samplerIter != mState.mUniforms.rend() && samplerIter->isSampler(); ++samplerIter)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001823 {
Jamie Madill982f6e02017-06-07 14:33:04 -04001824 --low;
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001825 }
Jamie Madill982f6e02017-06-07 14:33:04 -04001826
1827 mState.mSamplerUniformRange = RangeUI(low, high);
1828
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001829 // If uniform is a sampler type, insert it into the mSamplerBindings array.
Jamie Madill982f6e02017-06-07 14:33:04 -04001830 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001831 {
1832 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1833 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1834 mState.mSamplerBindings.emplace_back(
1835 SamplerBinding(textureType, samplerUniform.elementCount()));
1836 }
1837}
1838
jchen10eaef1e52017-06-13 10:44:11 +08001839bool Program::linkAtomicCounterBuffers()
1840{
1841 for (unsigned int index : mState.mAtomicCounterUniformRange)
1842 {
1843 auto &uniform = mState.mUniforms[index];
1844 bool found = false;
1845 for (unsigned int bufferIndex = 0; bufferIndex < mState.mAtomicCounterBuffers.size();
1846 ++bufferIndex)
1847 {
1848 auto &buffer = mState.mAtomicCounterBuffers[bufferIndex];
1849 if (buffer.binding == uniform.binding)
1850 {
1851 buffer.memberIndexes.push_back(index);
1852 uniform.bufferIndex = bufferIndex;
1853 found = true;
1854 break;
1855 }
1856 }
1857 if (!found)
1858 {
1859 AtomicCounterBuffer atomicCounterBuffer;
1860 atomicCounterBuffer.binding = uniform.binding;
1861 atomicCounterBuffer.memberIndexes.push_back(index);
1862 mState.mAtomicCounterBuffers.push_back(atomicCounterBuffer);
1863 uniform.bufferIndex = static_cast<int>(mState.mAtomicCounterBuffers.size() - 1);
1864 }
1865 }
1866 // TODO(jie.a.chen@intel.com): Count each atomic counter buffer to validate against
1867 // gl_Max[Vertex|Fragment|Compute|Combined]AtomicCounterBuffers.
1868
1869 return true;
1870}
1871
Martin Radev4c4c8e72016-08-04 12:25:34 +03001872bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1873 const std::string &uniformName,
1874 const sh::InterfaceBlockField &vertexUniform,
Frank Henigmanfccbac22017-05-28 17:29:26 -04001875 const sh::InterfaceBlockField &fragmentUniform,
1876 bool webglCompatibility)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877{
Frank Henigmanfccbac22017-05-28 17:29:26 -04001878 // If webgl, validate precision of UBO fields, otherwise don't. See Khronos bug 10287.
1879 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform,
1880 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881 {
1882 return false;
1883 }
1884
1885 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1886 {
Jamie Madillf6113162015-05-07 11:49:21 -04001887 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 return false;
1889 }
1890
1891 return true;
1892}
1893
Jamie Madilleb979bf2016-11-15 12:28:46 -05001894// Assigns locations to all attributes from the bindings and program locations.
Jamie Madillbd044ed2017-06-05 12:59:21 -04001895bool Program::linkAttributes(const Context *context, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896{
Jamie Madillbd044ed2017-06-05 12:59:21 -04001897 const ContextState &data = context->getContextState();
1898 auto *vertexShader = mState.getAttachedVertexShader();
Jamie Madilleb979bf2016-11-15 12:28:46 -05001899
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 unsigned int usedLocations = 0;
Jamie Madillbd044ed2017-06-05 12:59:21 -04001901 mState.mAttributes = vertexShader->getActiveAttributes(context);
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001902 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001903
1904 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001905 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001906 {
Jamie Madillf6113162015-05-07 11:49:21 -04001907 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001908 return false;
1909 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001910
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001911 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001912
Jamie Madillc349ec02015-08-21 16:53:12 -04001913 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001914 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001915 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001916 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001917 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001918 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001919 attribute.location = bindingLocation;
1920 }
1921
1922 if (attribute.location != -1)
1923 {
1924 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001925 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001926
Jamie Madill63805b42015-08-25 13:17:39 -04001927 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928 {
Jamie Madillf6113162015-05-07 11:49:21 -04001929 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001930 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931
1932 return false;
1933 }
1934
Jamie Madill63805b42015-08-25 13:17:39 -04001935 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001936 {
Jamie Madill63805b42015-08-25 13:17:39 -04001937 const int regLocation = attribute.location + reg;
1938 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001939
1940 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001941 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001942 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001944 // TODO(jmadill): fix aliasing on ES2
1945 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001946 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001947 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001948 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949 return false;
1950 }
1951 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001952 else
1953 {
Jamie Madill63805b42015-08-25 13:17:39 -04001954 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001955 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001956
Jamie Madill63805b42015-08-25 13:17:39 -04001957 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001958 }
1959 }
1960 }
1961
1962 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001963 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001965 // Not set by glBindAttribLocation or by location layout qualifier
1966 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967 {
Jamie Madill63805b42015-08-25 13:17:39 -04001968 int regs = VariableRegisterCount(attribute.type);
1969 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970
Jamie Madill63805b42015-08-25 13:17:39 -04001971 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001972 {
Jamie Madillf6113162015-05-07 11:49:21 -04001973 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001974 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975 }
1976
Jamie Madillc349ec02015-08-21 16:53:12 -04001977 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001978 }
1979 }
1980
Jamie Madill48ef11b2016-04-27 15:21:52 -04001981 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001982 {
Jamie Madill63805b42015-08-25 13:17:39 -04001983 ASSERT(attribute.location != -1);
1984 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001985
Jamie Madill63805b42015-08-25 13:17:39 -04001986 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001988 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001989 }
1990 }
1991
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992 return true;
1993}
1994
Martin Radev4c4c8e72016-08-04 12:25:34 +03001995bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
1996 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
1997 const std::string &errorMessage,
1998 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002000 GLuint blockCount = 0;
2001 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002002 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002003 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002004 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002005 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002006 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002007 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002008 return false;
2009 }
2010 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002012 return true;
2013}
Jamie Madille473dee2015-08-18 14:49:01 -04002014
Martin Radev4c4c8e72016-08-04 12:25:34 +03002015bool Program::validateVertexAndFragmentInterfaceBlocks(
2016 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2017 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002018 InfoLog &infoLog,
2019 bool webglCompatibility) const
Martin Radev4c4c8e72016-08-04 12:25:34 +03002020{
2021 // Check that interface blocks defined in the vertex and fragment shaders are identical
2022 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2023 UniformBlockMap linkedUniformBlocks;
2024
2025 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2026 {
2027 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2028 }
2029
Jamie Madille473dee2015-08-18 14:49:01 -04002030 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031 {
Jamie Madille473dee2015-08-18 14:49:01 -04002032 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002033 if (entry != linkedUniformBlocks.end())
2034 {
2035 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
Frank Henigmanfccbac22017-05-28 17:29:26 -04002036 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock,
2037 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038 {
2039 return false;
2040 }
2041 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002042 }
2043 return true;
2044}
Jamie Madille473dee2015-08-18 14:49:01 -04002045
Jamie Madillbd044ed2017-06-05 12:59:21 -04002046bool Program::linkUniformBlocks(const Context *context, InfoLog &infoLog)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002047{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002048 const auto &caps = context->getCaps();
2049
Martin Radev4c4c8e72016-08-04 12:25:34 +03002050 if (mState.mAttachedComputeShader)
2051 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002052 Shader &computeShader = *mState.mAttachedComputeShader;
2053 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002054
2055 if (!validateUniformBlocksCount(
2056 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2057 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2058 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002060 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002062 return true;
2063 }
2064
Jamie Madillbd044ed2017-06-05 12:59:21 -04002065 Shader &vertexShader = *mState.mAttachedVertexShader;
2066 Shader &fragmentShader = *mState.mAttachedFragmentShader;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002067
Jamie Madillbd044ed2017-06-05 12:59:21 -04002068 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(context);
2069 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +03002070
2071 if (!validateUniformBlocksCount(
2072 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2073 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2074 {
2075 return false;
2076 }
2077 if (!validateUniformBlocksCount(
2078 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2079 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2080 infoLog))
2081 {
2082
2083 return false;
2084 }
Jamie Madillbd044ed2017-06-05 12:59:21 -04002085
2086 bool webglCompatibility = context->getExtensions().webglCompatibility;
Martin Radev4c4c8e72016-08-04 12:25:34 +03002087 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002088 infoLog, webglCompatibility))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002089 {
2090 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 }
Jamie Madille473dee2015-08-18 14:49:01 -04002092
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093 return true;
2094}
2095
Jamie Madilla2c74982016-12-12 11:20:42 -05002096bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002097 const sh::InterfaceBlock &vertexInterfaceBlock,
Frank Henigmanfccbac22017-05-28 17:29:26 -04002098 const sh::InterfaceBlock &fragmentInterfaceBlock,
2099 bool webglCompatibility) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100{
2101 const char* blockName = vertexInterfaceBlock.name.c_str();
2102 // validate blocks for the same member types
2103 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2104 {
Jamie Madillf6113162015-05-07 11:49:21 -04002105 infoLog << "Types for interface block '" << blockName
2106 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 return false;
2108 }
2109 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2110 {
Jamie Madillf6113162015-05-07 11:49:21 -04002111 infoLog << "Array sizes differ for interface block '" << blockName
2112 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113 return false;
2114 }
jchen10af713a22017-04-19 09:10:56 +08002115 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2116 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2117 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118 {
Jamie Madillf6113162015-05-07 11:49:21 -04002119 infoLog << "Layout qualifiers differ for interface block '" << blockName
2120 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 return false;
2122 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002123 const unsigned int numBlockMembers =
2124 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2126 {
2127 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2128 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2129 if (vertexMember.name != fragmentMember.name)
2130 {
Jamie Madillf6113162015-05-07 11:49:21 -04002131 infoLog << "Name mismatch for field " << blockMemberIndex
2132 << " of interface block '" << blockName
2133 << "': (in vertex: '" << vertexMember.name
2134 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 return false;
2136 }
2137 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
Frank Henigmanfccbac22017-05-28 17:29:26 -04002138 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember,
2139 webglCompatibility))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 {
2141 return false;
2142 }
2143 }
2144 return true;
2145}
2146
2147bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2148 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2149{
2150 if (vertexVariable.type != fragmentVariable.type)
2151 {
Jamie Madillf6113162015-05-07 11:49:21 -04002152 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153 return false;
2154 }
2155 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2156 {
Jamie Madillf6113162015-05-07 11:49:21 -04002157 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158 return false;
2159 }
2160 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2161 {
Jamie Madillf6113162015-05-07 11:49:21 -04002162 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 return false;
2164 }
Geoff Langbb1e7502017-06-05 16:40:09 -04002165 if (vertexVariable.structName != fragmentVariable.structName)
2166 {
2167 infoLog << "Structure names for " << variableName
2168 << " differ between vertex and fragment shaders";
2169 return false;
2170 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171
2172 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2173 {
Jamie Madillf6113162015-05-07 11:49:21 -04002174 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 return false;
2176 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002177 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2179 {
2180 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2181 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2182
2183 if (vertexMember.name != fragmentMember.name)
2184 {
Jamie Madillf6113162015-05-07 11:49:21 -04002185 infoLog << "Name mismatch for field '" << memberIndex
2186 << "' of " << variableName
2187 << ": (in vertex: '" << vertexMember.name
2188 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189 return false;
2190 }
2191
2192 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2193 vertexMember.name + "'";
2194
2195 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2196 {
2197 return false;
2198 }
2199 }
2200
2201 return true;
2202}
2203
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002204bool Program::linkValidateVaryings(InfoLog &infoLog,
2205 const std::string &varyingName,
2206 const sh::Varying &vertexVarying,
2207 const sh::Varying &fragmentVarying,
2208 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209{
2210 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2211 {
2212 return false;
2213 }
2214
Jamie Madille9cc4692015-02-19 16:00:13 -05002215 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002216 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002217 infoLog << "Interpolation types for " << varyingName
2218 << " differ between vertex and fragment shaders.";
2219 return false;
2220 }
2221
2222 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2223 {
2224 infoLog << "Invariance for " << varyingName
2225 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002226 return false;
2227 }
2228
2229 return true;
2230}
2231
Jamie Madillbd044ed2017-06-05 12:59:21 -04002232bool Program::linkValidateBuiltInVaryings(const Context *context, InfoLog &infoLog) const
Yuly Novikov817232e2017-02-22 18:36:10 -05002233{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002234 Shader *vertexShader = mState.mAttachedVertexShader;
2235 Shader *fragmentShader = mState.mAttachedFragmentShader;
2236 const auto &vertexVaryings = vertexShader->getVaryings(context);
2237 const auto &fragmentVaryings = fragmentShader->getVaryings(context);
2238 int shaderVersion = vertexShader->getShaderVersion(context);
Yuly Novikov817232e2017-02-22 18:36:10 -05002239
2240 if (shaderVersion != 100)
2241 {
2242 // Only ESSL 1.0 has restrictions on matching input and output invariance
2243 return true;
2244 }
2245
2246 bool glPositionIsInvariant = false;
2247 bool glPointSizeIsInvariant = false;
2248 bool glFragCoordIsInvariant = false;
2249 bool glPointCoordIsInvariant = false;
2250
2251 for (const sh::Varying &varying : vertexVaryings)
2252 {
2253 if (!varying.isBuiltIn())
2254 {
2255 continue;
2256 }
2257 if (varying.name.compare("gl_Position") == 0)
2258 {
2259 glPositionIsInvariant = varying.isInvariant;
2260 }
2261 else if (varying.name.compare("gl_PointSize") == 0)
2262 {
2263 glPointSizeIsInvariant = varying.isInvariant;
2264 }
2265 }
2266
2267 for (const sh::Varying &varying : fragmentVaryings)
2268 {
2269 if (!varying.isBuiltIn())
2270 {
2271 continue;
2272 }
2273 if (varying.name.compare("gl_FragCoord") == 0)
2274 {
2275 glFragCoordIsInvariant = varying.isInvariant;
2276 }
2277 else if (varying.name.compare("gl_PointCoord") == 0)
2278 {
2279 glPointCoordIsInvariant = varying.isInvariant;
2280 }
2281 }
2282
2283 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2284 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2285 // Not requiring invariance to match is supported by:
2286 // dEQP, WebGL CTS, Nexus 5X GLES
2287 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2288 {
2289 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2290 "declared invariant.";
2291 return false;
2292 }
2293 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2294 {
2295 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2296 "declared invariant.";
2297 return false;
2298 }
2299
2300 return true;
2301}
2302
jchen10a9042d32017-03-17 08:50:45 +08002303bool Program::linkValidateTransformFeedback(const gl::Context *context,
2304 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002305 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002306 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002307{
2308 size_t totalComponents = 0;
2309
Jamie Madillccdf74b2015-08-18 10:46:12 -04002310 std::set<std::string> uniqueNames;
2311
Jamie Madill48ef11b2016-04-27 15:21:52 -04002312 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002313 {
2314 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002315 size_t subscript = GL_INVALID_INDEX;
2316 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2317
Jamie Madill192745a2016-12-22 15:58:21 -05002318 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002319 {
Jamie Madill192745a2016-12-22 15:58:21 -05002320 const sh::Varying *varying = ref.second.get();
2321
jchen10a9042d32017-03-17 08:50:45 +08002322 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002324 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002326 infoLog << "Two transform feedback varyings specify the same output variable ("
2327 << tfVaryingName << ").";
2328 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002329 }
jchen10a9042d32017-03-17 08:50:45 +08002330 if (context->getClientVersion() >= Version(3, 1))
2331 {
2332 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2333 {
2334 infoLog
2335 << "Two transform feedback varyings include the same array element ("
2336 << tfVaryingName << ").";
2337 return false;
2338 }
2339 }
2340 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002341 {
2342 infoLog << "Capture of arrays is undefined and not supported.";
2343 return false;
2344 }
2345
jchen10a9042d32017-03-17 08:50:45 +08002346 uniqueNames.insert(tfVaryingName);
2347
Jamie Madillccdf74b2015-08-18 10:46:12 -04002348 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002349 size_t elementCount =
2350 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2351 : 1);
2352 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002353 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 componentCount > caps.maxTransformFeedbackSeparateComponents)
2355 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002356 infoLog << "Transform feedback varying's " << varying->name << " components ("
2357 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002358 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 return false;
2360 }
2361
2362 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363 found = true;
2364 break;
2365 }
2366 }
jchen10a9042d32017-03-17 08:50:45 +08002367 if (context->getClientVersion() < Version(3, 1) &&
2368 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002369 {
Geoff Lang1a683462015-09-29 15:09:59 -04002370 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002371 return false;
2372 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2374 ASSERT(found);
2375 }
2376
Jamie Madill48ef11b2016-04-27 15:21:52 -04002377 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002378 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379 {
Jamie Madillf6113162015-05-07 11:49:21 -04002380 infoLog << "Transform feedback varying total components (" << totalComponents
2381 << ") exceed the maximum interleaved components ("
2382 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002383 return false;
2384 }
2385
2386 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002387}
2388
Jamie Madill192745a2016-12-22 15:58:21 -05002389void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002390{
2391 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002392 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002393 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002394 {
jchen10a9042d32017-03-17 08:50:45 +08002395 size_t subscript = GL_INVALID_INDEX;
2396 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002397 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002398 {
Jamie Madill192745a2016-12-22 15:58:21 -05002399 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002400 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002401 {
jchen10a9042d32017-03-17 08:50:45 +08002402 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2403 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002404 break;
2405 }
2406 }
2407 }
2408}
2409
Jamie Madillbd044ed2017-06-05 12:59:21 -04002410Program::MergedVaryings Program::getMergedVaryings(const Context *context) const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002411{
Jamie Madill192745a2016-12-22 15:58:21 -05002412 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002413
Jamie Madillbd044ed2017-06-05 12:59:21 -04002414 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002415 {
Jamie Madill192745a2016-12-22 15:58:21 -05002416 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002417 }
2418
Jamie Madillbd044ed2017-06-05 12:59:21 -04002419 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings(context))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002420 {
Jamie Madill192745a2016-12-22 15:58:21 -05002421 merged[varying.name].fragment = &varying;
2422 }
2423
2424 return merged;
2425}
2426
2427std::vector<PackedVarying> Program::getPackedVaryings(
2428 const Program::MergedVaryings &mergedVaryings) const
2429{
2430 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2431 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002432 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002433
2434 for (const auto &ref : mergedVaryings)
2435 {
2436 const sh::Varying *input = ref.second.vertex;
2437 const sh::Varying *output = ref.second.fragment;
2438
2439 // Only pack varyings that have a matched input or output, plus special builtins.
2440 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002441 {
Jamie Madill192745a2016-12-22 15:58:21 -05002442 // Will get the vertex shader interpolation by default.
2443 auto interpolation = ref.second.get()->interpolation;
2444
2445 // Interpolation qualifiers must match.
2446 if (output->isStruct())
2447 {
2448 ASSERT(!output->isArray());
2449 for (const auto &field : output->fields)
2450 {
2451 ASSERT(!field.isStruct() && !field.isArray());
2452 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2453 }
2454 }
2455 else
2456 {
2457 packedVaryings.push_back(PackedVarying(*output, interpolation));
2458 }
2459 continue;
2460 }
2461
2462 // Keep Transform FB varyings in the merged list always.
2463 if (!input)
2464 {
2465 continue;
2466 }
2467
2468 for (const std::string &tfVarying : tfVaryings)
2469 {
jchen10a9042d32017-03-17 08:50:45 +08002470 size_t subscript = GL_INVALID_INDEX;
2471 std::string baseName = ParseResourceName(tfVarying, &subscript);
2472 if (uniqueFullNames.count(tfVarying) > 0)
2473 {
2474 continue;
2475 }
2476 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002477 {
2478 // Transform feedback for varying structs is underspecified.
2479 // See Khronos bug 9856.
2480 // TODO(jmadill): Figure out how to be spec-compliant here.
2481 if (!input->isStruct())
2482 {
2483 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2484 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002485 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2486 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002487 }
jchen10a9042d32017-03-17 08:50:45 +08002488 if (subscript == GL_INVALID_INDEX)
2489 {
2490 break;
2491 }
Jamie Madill192745a2016-12-22 15:58:21 -05002492 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002493 }
2494 }
2495
Jamie Madill192745a2016-12-22 15:58:21 -05002496 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2497
2498 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002499}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002500
Jamie Madillbd044ed2017-06-05 12:59:21 -04002501void Program::linkOutputVariables(const Context *context)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002502{
Jamie Madillbd044ed2017-06-05 12:59:21 -04002503 Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002504 ASSERT(fragmentShader != nullptr);
2505
Geoff Lange0cff192017-05-30 13:04:56 -04002506 ASSERT(mState.mOutputVariableTypes.empty());
Corentin Walleze7557742017-06-01 13:09:57 -04002507 ASSERT(mState.mActiveOutputVariables.none());
Geoff Lange0cff192017-05-30 13:04:56 -04002508
2509 // Gather output variable types
Jamie Madillbd044ed2017-06-05 12:59:21 -04002510 for (const auto &outputVariable : fragmentShader->getActiveOutputVariables(context))
Geoff Lange0cff192017-05-30 13:04:56 -04002511 {
2512 if (outputVariable.isBuiltIn() && outputVariable.name != "gl_FragColor" &&
2513 outputVariable.name != "gl_FragData")
2514 {
2515 continue;
2516 }
2517
2518 unsigned int baseLocation =
2519 (outputVariable.location == -1 ? 0u
2520 : static_cast<unsigned int>(outputVariable.location));
2521 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2522 elementIndex++)
2523 {
2524 const unsigned int location = baseLocation + elementIndex;
2525 if (location >= mState.mOutputVariableTypes.size())
2526 {
2527 mState.mOutputVariableTypes.resize(location + 1, GL_NONE);
2528 }
Corentin Walleze7557742017-06-01 13:09:57 -04002529 ASSERT(location < mState.mActiveOutputVariables.size());
2530 mState.mActiveOutputVariables.set(location);
Geoff Lange0cff192017-05-30 13:04:56 -04002531 mState.mOutputVariableTypes[location] = VariableComponentType(outputVariable.type);
2532 }
2533 }
2534
Jamie Madill80a6fc02015-08-21 16:53:16 -04002535 // Skip this step for GLES2 shaders.
Jamie Madillbd044ed2017-06-05 12:59:21 -04002536 if (fragmentShader->getShaderVersion(context) == 100)
Jamie Madill80a6fc02015-08-21 16:53:16 -04002537 return;
2538
Jamie Madillbd044ed2017-06-05 12:59:21 -04002539 mState.mOutputVariables = fragmentShader->getActiveOutputVariables(context);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002540 // TODO(jmadill): any caps validation here?
2541
jchen1015015f72017-03-16 13:54:21 +08002542 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002543 outputVariableIndex++)
2544 {
jchen1015015f72017-03-16 13:54:21 +08002545 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002546
2547 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2548 if (outputVariable.isBuiltIn())
2549 continue;
2550
2551 // Since multiple output locations must be specified, use 0 for non-specified locations.
2552 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2553
Jamie Madill80a6fc02015-08-21 16:53:16 -04002554 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2555 elementIndex++)
2556 {
2557 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002558 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002559 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002560 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002561 VariableLocation(outputVariable.name, element, outputVariableIndex);
2562 }
2563 }
2564}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002565
Olli Etuaho48fed632017-03-16 12:05:30 +00002566void Program::setUniformValuesFromBindingQualifiers()
2567{
Jamie Madill982f6e02017-06-07 14:33:04 -04002568 for (unsigned int samplerIndex : mState.mSamplerUniformRange)
Olli Etuaho48fed632017-03-16 12:05:30 +00002569 {
2570 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2571 if (samplerUniform.binding != -1)
2572 {
2573 GLint location = mState.getUniformLocation(samplerUniform.name);
2574 ASSERT(location != -1);
2575 std::vector<GLint> boundTextureUnits;
2576 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2577 ++elementIndex)
2578 {
2579 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2580 }
2581 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2582 boundTextureUnits.data());
2583 }
2584 }
2585}
2586
jchen10eaef1e52017-06-13 10:44:11 +08002587void Program::gatherAtomicCounterBuffers()
2588{
2589 // TODO(jie.a.chen@intel.com): Get the actual OFFSET and ARRAY_STRIDE from the backend for each
2590 // counter.
2591 // TODO(jie.a.chen@intel.com): Get the actual BUFFER_DATA_SIZE from backend for each buffer.
2592}
2593
Jamie Madillbd044ed2017-06-05 12:59:21 -04002594void Program::gatherInterfaceBlockInfo(const Context *context)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002595{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002596 ASSERT(mState.mUniformBlocks.empty());
2597
2598 if (mState.mAttachedComputeShader)
2599 {
Jamie Madillbd044ed2017-06-05 12:59:21 -04002600 Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002601
Jamie Madillbd044ed2017-06-05 12:59:21 -04002602 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks(context))
Martin Radev4c4c8e72016-08-04 12:25:34 +03002603 {
2604
2605 // Only 'packed' blocks are allowed to be considered inactive.
2606 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2607 continue;
2608
Jamie Madilla2c74982016-12-12 11:20:42 -05002609 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002610 {
2611 if (block.name == computeBlock.name)
2612 {
2613 block.computeStaticUse = computeBlock.staticUse;
2614 }
2615 }
2616
2617 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2618 }
2619 return;
2620 }
2621
Jamie Madill62d31cb2015-09-11 13:25:51 -04002622 std::set<std::string> visitedList;
2623
Jamie Madillbd044ed2017-06-05 12:59:21 -04002624 Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002625
Jamie Madillbd044ed2017-06-05 12:59:21 -04002626 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002627 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002628 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002629 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2630 continue;
2631
2632 if (visitedList.count(vertexBlock.name) > 0)
2633 continue;
2634
2635 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2636 visitedList.insert(vertexBlock.name);
2637 }
2638
Jamie Madillbd044ed2017-06-05 12:59:21 -04002639 Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002640
Jamie Madillbd044ed2017-06-05 12:59:21 -04002641 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks(context))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002642 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002643 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002644 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2645 continue;
2646
2647 if (visitedList.count(fragmentBlock.name) > 0)
2648 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002649 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002650 {
2651 if (block.name == fragmentBlock.name)
2652 {
2653 block.fragmentStaticUse = fragmentBlock.staticUse;
2654 }
2655 }
2656
2657 continue;
2658 }
2659
2660 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2661 visitedList.insert(fragmentBlock.name);
2662 }
jchen10af713a22017-04-19 09:10:56 +08002663 // Set initial bindings from shader.
2664 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2665 {
2666 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2667 bindUniformBlock(blockIndex, uniformBlock.binding);
2668 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002669}
2670
Jamie Madill4a3c2342015-10-08 12:58:45 -04002671template <typename VarT>
2672void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2673 const std::string &prefix,
2674 int blockIndex)
2675{
2676 for (const VarT &field : fields)
2677 {
2678 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2679
2680 if (field.isStruct())
2681 {
2682 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2683 {
2684 const std::string uniformElementName =
2685 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2686 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2687 }
2688 }
2689 else
2690 {
2691 // If getBlockMemberInfo returns false, the uniform is optimized out.
2692 sh::BlockMemberInfo memberInfo;
2693 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2694 {
2695 continue;
2696 }
2697
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002698 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
jchen10eaef1e52017-06-13 10:44:11 +08002699 -1, blockIndex, memberInfo);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002700
2701 // Since block uniforms have no location, we don't need to store them in the uniform
2702 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002703 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002704 }
2705 }
2706}
2707
Jamie Madill62d31cb2015-09-11 13:25:51 -04002708void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2709{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002710 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002711 size_t blockSize = 0;
2712
Jamie Madill4a3c2342015-10-08 12:58:45 -04002713 // Track the first and last uniform index to determine the range of active uniforms in the
2714 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002715 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002716 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002717 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002718
2719 std::vector<unsigned int> blockUniformIndexes;
2720 for (size_t blockUniformIndex = firstBlockUniformIndex;
2721 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2722 {
2723 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2724 }
jchen10af713a22017-04-19 09:10:56 +08002725 // ESSL 3.10 section 4.4.4 page 58:
2726 // Any uniform or shader storage block declared without a binding qualifier is initially
2727 // assigned to block binding point zero.
2728 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002729 if (interfaceBlock.arraySize > 0)
2730 {
2731 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2732 {
jchen10af713a22017-04-19 09:10:56 +08002733 // Don't define this block at all if it's not active in the implementation.
2734 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2735 &blockSize))
2736 {
2737 continue;
2738 }
2739 UniformBlock block(interfaceBlock.name, true, arrayElement,
2740 blockBinding + arrayElement);
jchen10eaef1e52017-06-13 10:44:11 +08002741 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002742
Martin Radev4c4c8e72016-08-04 12:25:34 +03002743 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002744 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002745 case GL_VERTEX_SHADER:
2746 {
2747 block.vertexStaticUse = interfaceBlock.staticUse;
2748 break;
2749 }
2750 case GL_FRAGMENT_SHADER:
2751 {
2752 block.fragmentStaticUse = interfaceBlock.staticUse;
2753 break;
2754 }
2755 case GL_COMPUTE_SHADER:
2756 {
2757 block.computeStaticUse = interfaceBlock.staticUse;
2758 break;
2759 }
2760 default:
2761 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002762 }
2763
Qin Jiajia0350a642016-11-01 17:01:51 +08002764 // Since all block elements in an array share the same active uniforms, they will all be
2765 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2766 // here we will add every block element in the array.
2767 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002768 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002769 }
2770 }
2771 else
2772 {
jchen10af713a22017-04-19 09:10:56 +08002773 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2774 {
2775 return;
2776 }
2777 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
jchen10eaef1e52017-06-13 10:44:11 +08002778 block.memberIndexes = blockUniformIndexes;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002779
Martin Radev4c4c8e72016-08-04 12:25:34 +03002780 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002781 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002782 case GL_VERTEX_SHADER:
2783 {
2784 block.vertexStaticUse = interfaceBlock.staticUse;
2785 break;
2786 }
2787 case GL_FRAGMENT_SHADER:
2788 {
2789 block.fragmentStaticUse = interfaceBlock.staticUse;
2790 break;
2791 }
2792 case GL_COMPUTE_SHADER:
2793 {
2794 block.computeStaticUse = interfaceBlock.staticUse;
2795 break;
2796 }
2797 default:
2798 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002799 }
2800
Jamie Madill4a3c2342015-10-08 12:58:45 -04002801 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002802 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002803 }
2804}
2805
Jamie Madille7d84322017-01-10 18:21:59 -05002806template <>
2807void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2808 const uint8_t *destPointer,
2809 GLsizei clampedCount,
2810 const GLint *v)
2811{
2812 // Invalidate the validation cache only if we modify the sampler data.
2813 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2814 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2815 {
2816 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2817 std::vector<GLuint> *boundTextureUnits =
2818 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2819
2820 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2821 mCachedValidateSamplersResult.reset();
2822 }
2823}
2824
2825template <typename T>
2826void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2827 const uint8_t *destPointer,
2828 GLsizei clampedCount,
2829 const T *v)
2830{
2831}
2832
Jamie Madill62d31cb2015-09-11 13:25:51 -04002833template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002834GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002835{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002836 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2837 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002838 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2839
Corentin Wallez15ac5342016-11-03 17:06:39 -04002840 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2841 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2842 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002843 GLsizei maxElementCount =
2844 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2845
2846 GLsizei count = countIn;
2847 GLsizei clampedCount = count * vectorSize;
2848 if (clampedCount > maxElementCount)
2849 {
2850 clampedCount = maxElementCount;
2851 count = maxElementCount / vectorSize;
2852 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002853
Jamie Madill62d31cb2015-09-11 13:25:51 -04002854 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2855 {
2856 // Do a cast conversion for boolean types. From the spec:
2857 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2858 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002859 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002860 {
2861 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2862 }
2863 }
2864 else
2865 {
Jamie Madille7d84322017-01-10 18:21:59 -05002866 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002867 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002868 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002869
2870 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002871}
2872
2873template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002874GLsizei Program::setMatrixUniformInternal(GLint location,
2875 GLsizei count,
2876 GLboolean transpose,
2877 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002878{
2879 if (!transpose)
2880 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002881 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002882 }
2883
2884 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002885 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2886 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002887 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002888
2889 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2890 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2891 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2892 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2893
2894 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002895 {
2896 size_t elementOffset = element * rows * cols;
2897
2898 for (size_t row = 0; row < rows; ++row)
2899 {
2900 for (size_t col = 0; col < cols; ++col)
2901 {
2902 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2903 }
2904 }
2905 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002906
2907 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908}
2909
2910template <typename DestT>
2911void Program::getUniformInternal(GLint location, DestT *dataOut) const
2912{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002913 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2914 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002915
2916 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2917
2918 GLenum componentType = VariableComponentType(uniform.type);
2919 if (componentType == GLTypeToGLenum<DestT>::value)
2920 {
2921 memcpy(dataOut, srcPointer, uniform.getElementSize());
2922 return;
2923 }
2924
Corentin Wallez6596c462016-03-17 17:26:58 -04002925 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926
2927 switch (componentType)
2928 {
2929 case GL_INT:
2930 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2931 break;
2932 case GL_UNSIGNED_INT:
2933 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2934 break;
2935 case GL_BOOL:
2936 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2937 break;
2938 case GL_FLOAT:
2939 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2940 break;
2941 default:
2942 UNREACHABLE();
2943 }
2944}
Jamie Madilla4595b82017-01-11 17:36:34 -05002945
2946bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2947{
2948 // Must be called after samplers are validated.
2949 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2950
2951 for (const auto &binding : mState.mSamplerBindings)
2952 {
2953 GLenum textureType = binding.textureType;
2954 for (const auto &unit : binding.boundTextureUnits)
2955 {
2956 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2957 if (programTextureID == textureID)
2958 {
2959 // TODO(jmadill): Check for appropriate overlap.
2960 return true;
2961 }
2962 }
2963 }
2964
2965 return false;
2966}
2967
Jamie Madilla2c74982016-12-12 11:20:42 -05002968} // namespace gl