blob: 2680741d146e4fd4cafbcc7c9e2af63fe7b225da [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"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill192745a2016-12-22 15:58:21 -050025#include "libANGLE/VaryingPacking.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040026#include "libANGLE/queryconversions.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040027#include "libANGLE/Uniform.h"
Olli Etuahob78707c2017-03-09 15:03:11 +000028#include "libANGLE/UniformLinker.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 -040036void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
37{
38 stream->writeInt(var.type);
39 stream->writeInt(var.precision);
40 stream->writeString(var.name);
41 stream->writeString(var.mappedName);
42 stream->writeInt(var.arraySize);
43 stream->writeInt(var.staticUse);
44 stream->writeString(var.structName);
45 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050046}
47
Jamie Madill62d31cb2015-09-11 13:25:51 -040048void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
49{
50 var->type = stream->readInt<GLenum>();
51 var->precision = stream->readInt<GLenum>();
52 var->name = stream->readString();
53 var->mappedName = stream->readString();
54 var->arraySize = stream->readInt<unsigned int>();
55 var->staticUse = stream->readBool();
56 var->structName = stream->readString();
57}
58
Jamie Madill62d31cb2015-09-11 13:25:51 -040059// This simplified cast function doesn't need to worry about advanced concepts like
60// depth range values, or casting to bool.
61template <typename DestT, typename SrcT>
62DestT UniformStateQueryCast(SrcT value);
63
64// From-Float-To-Integer Casts
65template <>
66GLint UniformStateQueryCast(GLfloat value)
67{
68 return clampCast<GLint>(roundf(value));
69}
70
71template <>
72GLuint UniformStateQueryCast(GLfloat value)
73{
74 return clampCast<GLuint>(roundf(value));
75}
76
77// From-Integer-to-Integer Casts
78template <>
79GLint UniformStateQueryCast(GLuint value)
80{
81 return clampCast<GLint>(value);
82}
83
84template <>
85GLuint UniformStateQueryCast(GLint value)
86{
87 return clampCast<GLuint>(value);
88}
89
90// From-Boolean-to-Anything Casts
91template <>
92GLfloat UniformStateQueryCast(GLboolean value)
93{
94 return (value == GL_TRUE ? 1.0f : 0.0f);
95}
96
97template <>
98GLint UniformStateQueryCast(GLboolean value)
99{
100 return (value == GL_TRUE ? 1 : 0);
101}
102
103template <>
104GLuint UniformStateQueryCast(GLboolean value)
105{
106 return (value == GL_TRUE ? 1u : 0u);
107}
108
109// Default to static_cast
110template <typename DestT, typename SrcT>
111DestT UniformStateQueryCast(SrcT value)
112{
113 return static_cast<DestT>(value);
114}
115
116template <typename SrcT, typename DestT>
117void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
118{
119 for (int comp = 0; comp < components; ++comp)
120 {
121 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
122 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
123 size_t offset = comp * 4;
124 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
125 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
126 }
127}
128
Jamie Madill192745a2016-12-22 15:58:21 -0500129// true if varying x has a higher priority in packing than y
130bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
131{
jchen10a9042d32017-03-17 08:50:45 +0800132 // If the PackedVarying 'x' or 'y' to be compared is an array element, this clones an equivalent
133 // non-array shader variable 'vx' or 'vy' for actual comparison instead.
134 sh::ShaderVariable vx, vy;
135 const sh::ShaderVariable *px, *py;
136 if (x.isArrayElement())
137 {
138 vx = *x.varying;
139 vx.arraySize = 0;
140 px = &vx;
141 }
142 else
143 {
144 px = x.varying;
145 }
146
147 if (y.isArrayElement())
148 {
149 vy = *y.varying;
150 vy.arraySize = 0;
151 py = &vy;
152 }
153 else
154 {
155 py = y.varying;
156 }
157
158 return gl::CompareShaderVar(*px, *py);
Jamie Madill192745a2016-12-22 15:58:21 -0500159}
160
jchen1015015f72017-03-16 13:54:21 +0800161template <typename VarT>
162GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
163{
164 size_t subscript = GL_INVALID_INDEX;
165 std::string baseName = ParseResourceName(name, &subscript);
166
167 // The app is not allowed to specify array indices other than 0 for arrays of basic types
168 if (subscript != 0 && subscript != GL_INVALID_INDEX)
169 {
170 return GL_INVALID_INDEX;
171 }
172
173 for (size_t index = 0; index < list.size(); index++)
174 {
175 const VarT &resource = list[index];
176 if (resource.name == baseName)
177 {
178 if (resource.isArray() || subscript == GL_INVALID_INDEX)
179 {
180 return static_cast<GLuint>(index);
181 }
182 }
183 }
184
185 return GL_INVALID_INDEX;
186}
187
jchen10fd7c3b52017-03-21 15:36:03 +0800188void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
189{
190 ASSERT(bufSize > 0);
191 strncpy(buffer, string.c_str(), bufSize);
192 buffer[bufSize - 1] = '\0';
193
194 if (length)
195 {
196 *length = static_cast<GLsizei>(strlen(buffer));
197 }
198}
199
jchen10a9042d32017-03-17 08:50:45 +0800200bool IncludeSameArrayElement(const std::set<std::string> &nameSet, const std::string &name)
201{
202 size_t subscript = GL_INVALID_INDEX;
203 std::string baseName = ParseResourceName(name, &subscript);
204 for (auto it = nameSet.begin(); it != nameSet.end(); ++it)
205 {
206 size_t arrayIndex = GL_INVALID_INDEX;
207 std::string arrayName = ParseResourceName(*it, &arrayIndex);
208 if (baseName == arrayName && (subscript == GL_INVALID_INDEX ||
209 arrayIndex == GL_INVALID_INDEX || subscript == arrayIndex))
210 {
211 return true;
212 }
213 }
214 return false;
215}
216
Jamie Madill62d31cb2015-09-11 13:25:51 -0400217} // anonymous namespace
218
Jamie Madill4a3c2342015-10-08 12:58:45 -0400219const char *const g_fakepath = "C:\\fakepath";
220
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400221InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000222{
223}
224
225InfoLog::~InfoLog()
226{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000227}
228
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400229size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000230{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400231 const std::string &logString = mStream.str();
232 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000233}
234
Geoff Lange1a27752015-10-05 13:16:04 -0400235void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000236{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400237 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000238
239 if (bufSize > 0)
240 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400241 const std::string str(mStream.str());
242
243 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000244 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400245 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
246 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000247 }
248
249 infoLog[index] = '\0';
250 }
251
252 if (length)
253 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400254 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000255 }
256}
257
258// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300259// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000260// messages, so lets remove all occurrences of this fake file path from the log.
261void InfoLog::appendSanitized(const char *message)
262{
263 std::string msg(message);
264
265 size_t found;
266 do
267 {
268 found = msg.find(g_fakepath);
269 if (found != std::string::npos)
270 {
271 msg.erase(found, strlen(g_fakepath));
272 }
273 }
274 while (found != std::string::npos);
275
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400276 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000277}
278
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000279void InfoLog::reset()
280{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000281}
282
Geoff Langd8605522016-04-13 10:19:12 -0400283VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000284{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500285}
286
Geoff Langd8605522016-04-13 10:19:12 -0400287VariableLocation::VariableLocation(const std::string &name,
288 unsigned int element,
289 unsigned int index)
290 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500291{
292}
293
Geoff Langd8605522016-04-13 10:19:12 -0400294void Program::Bindings::bindLocation(GLuint index, const std::string &name)
295{
296 mBindings[name] = index;
297}
298
299int Program::Bindings::getBinding(const std::string &name) const
300{
301 auto iter = mBindings.find(name);
302 return (iter != mBindings.end()) ? iter->second : -1;
303}
304
305Program::Bindings::const_iterator Program::Bindings::begin() const
306{
307 return mBindings.begin();
308}
309
310Program::Bindings::const_iterator Program::Bindings::end() const
311{
312 return mBindings.end();
313}
314
Jamie Madill48ef11b2016-04-27 15:21:52 -0400315ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500316 : mLabel(),
317 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400318 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300319 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500320 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500321 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500322 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400323{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300324 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400325}
326
Jamie Madill48ef11b2016-04-27 15:21:52 -0400327ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400328{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500329 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400330}
331
Jamie Madill48ef11b2016-04-27 15:21:52 -0400332const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500333{
334 return mLabel;
335}
336
Jamie Madill48ef11b2016-04-27 15:21:52 -0400337GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400338{
339 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800340 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400341
342 for (size_t location = 0; location < mUniformLocations.size(); ++location)
343 {
344 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400345 if (!uniformLocation.used)
346 {
347 continue;
348 }
349
350 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400351
352 if (uniform.name == baseName)
353 {
Geoff Langd8605522016-04-13 10:19:12 -0400354 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400355 {
Geoff Langd8605522016-04-13 10:19:12 -0400356 if (uniformLocation.element == subscript ||
357 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
358 {
359 return static_cast<GLint>(location);
360 }
361 }
362 else
363 {
364 if (subscript == GL_INVALID_INDEX)
365 {
366 return static_cast<GLint>(location);
367 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400368 }
369 }
370 }
371
372 return -1;
373}
374
Jamie Madille7d84322017-01-10 18:21:59 -0500375GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400376{
jchen1015015f72017-03-16 13:54:21 +0800377 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400378}
379
Jamie Madille7d84322017-01-10 18:21:59 -0500380GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
381{
382 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
383 return mUniformLocations[location].index;
384}
385
386Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
387{
388 GLuint index = getUniformIndexFromLocation(location);
389 if (!isSamplerUniformIndex(index))
390 {
391 return Optional<GLuint>::Invalid();
392 }
393
394 return getSamplerIndexFromUniformIndex(index);
395}
396
397bool ProgramState::isSamplerUniformIndex(GLuint index) const
398{
399 return index >= mSamplerUniformRange.start && index < mSamplerUniformRange.end;
400}
401
402GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
403{
404 ASSERT(isSamplerUniformIndex(uniformIndex));
405 return uniformIndex - mSamplerUniformRange.start;
406}
407
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500408Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400409 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400410 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500411 mLinked(false),
412 mDeleteStatus(false),
413 mRefCount(0),
414 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500415 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500416{
417 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000418
419 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500420 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423Program::~Program()
424{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500425 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
426 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500427 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428}
429
Jamie Madill6c1f6712017-02-14 19:08:04 -0500430void Program::destroy(const Context *context)
431{
432 if (mState.mAttachedVertexShader != nullptr)
433 {
434 mState.mAttachedVertexShader->release(context);
435 mState.mAttachedVertexShader = nullptr;
436 }
437
438 if (mState.mAttachedFragmentShader != nullptr)
439 {
440 mState.mAttachedFragmentShader->release(context);
441 mState.mAttachedFragmentShader = nullptr;
442 }
443
444 if (mState.mAttachedComputeShader != nullptr)
445 {
446 mState.mAttachedComputeShader->release(context);
447 mState.mAttachedComputeShader = nullptr;
448 }
449
450 mProgram->destroy(rx::SafeGetImpl(context));
451}
452
Geoff Lang70d0f492015-12-10 17:45:46 -0500453void Program::setLabel(const std::string &label)
454{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400455 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500456}
457
458const std::string &Program::getLabel() const
459{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400460 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500461}
462
Jamie Madillef300b12016-10-07 15:12:09 -0400463void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300465 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300467 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468 {
Jamie Madillef300b12016-10-07 15:12:09 -0400469 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300470 mState.mAttachedVertexShader = shader;
471 mState.mAttachedVertexShader->addRef();
472 break;
473 }
474 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475 {
Jamie Madillef300b12016-10-07 15:12:09 -0400476 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300477 mState.mAttachedFragmentShader = shader;
478 mState.mAttachedFragmentShader->addRef();
479 break;
480 }
481 case GL_COMPUTE_SHADER:
482 {
Jamie Madillef300b12016-10-07 15:12:09 -0400483 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300484 mState.mAttachedComputeShader = shader;
485 mState.mAttachedComputeShader->addRef();
486 break;
487 }
488 default:
489 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491}
492
Jamie Madillc1d770e2017-04-13 17:31:24 -0400493void Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300495 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300497 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400499 ASSERT(mState.mAttachedVertexShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500500 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 mState.mAttachedVertexShader = nullptr;
502 break;
503 }
504 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400506 ASSERT(mState.mAttachedFragmentShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500507 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300508 mState.mAttachedFragmentShader = nullptr;
509 break;
510 }
511 case GL_COMPUTE_SHADER:
512 {
Jamie Madillc1d770e2017-04-13 17:31:24 -0400513 ASSERT(mState.mAttachedComputeShader == shader);
Jamie Madill6c1f6712017-02-14 19:08:04 -0500514 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300515 mState.mAttachedComputeShader = nullptr;
516 break;
517 }
518 default:
519 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521}
522
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000523int Program::getAttachedShadersCount() const
524{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300525 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
526 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000527}
528
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529void Program::bindAttributeLocation(GLuint index, const char *name)
530{
Geoff Langd8605522016-04-13 10:19:12 -0400531 mAttributeBindings.bindLocation(index, name);
532}
533
534void Program::bindUniformLocation(GLuint index, const char *name)
535{
536 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800537 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538}
539
Sami Väisänen46eaa942016-06-29 10:26:37 +0300540void Program::bindFragmentInputLocation(GLint index, const char *name)
541{
542 mFragmentInputBindings.bindLocation(index, name);
543}
544
545BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
546{
547 BindingInfo ret;
548 ret.type = GL_NONE;
549 ret.valid = false;
550
551 const Shader *fragmentShader = mState.getAttachedFragmentShader();
552 ASSERT(fragmentShader);
553
554 // Find the actual fragment shader varying we're interested in
555 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
556
557 for (const auto &binding : mFragmentInputBindings)
558 {
559 if (binding.second != static_cast<GLuint>(index))
560 continue;
561
562 ret.valid = true;
563
564 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400565 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300566
567 for (const auto &in : inputs)
568 {
569 if (in.name == originalName)
570 {
571 if (in.isArray())
572 {
573 // The client wants to bind either "name" or "name[0]".
574 // GL ES 3.1 spec refers to active array names with language such as:
575 // "if the string identifies the base name of an active array, where the
576 // string would exactly match the name of the variable if the suffix "[0]"
577 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400578 if (arrayIndex == GL_INVALID_INDEX)
579 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300580
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400581 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300582 }
583 else
584 {
585 ret.name = in.mappedName;
586 }
587 ret.type = in.type;
588 return ret;
589 }
590 }
591 }
592
593 return ret;
594}
595
596void Program::pathFragmentInputGen(GLint index,
597 GLenum genMode,
598 GLint components,
599 const GLfloat *coeffs)
600{
601 // If the location is -1 then the command is silently ignored
602 if (index == -1)
603 return;
604
605 const auto &binding = getFragmentInputBindingInfo(index);
606
607 // If the input doesn't exist then then the command is silently ignored
608 // This could happen through optimization for example, the shader translator
609 // decides that a variable is not actually being used and optimizes it away.
610 if (binding.name.empty())
611 return;
612
613 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
614}
615
Martin Radev4c4c8e72016-08-04 12:25:34 +0300616// The attached shaders are checked for linking errors by matching up their variables.
617// Uniform, input and output variables get collected.
618// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500619Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000620{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500621 const auto &data = context->getContextState();
622
Jamie Madill6c1f6712017-02-14 19:08:04 -0500623 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000624
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000625 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000626 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000627
Martin Radev4c4c8e72016-08-04 12:25:34 +0300628 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500629
Jamie Madill192745a2016-12-22 15:58:21 -0500630 auto vertexShader = mState.mAttachedVertexShader;
631 auto fragmentShader = mState.mAttachedFragmentShader;
632 auto computeShader = mState.mAttachedComputeShader;
633
634 bool isComputeShaderAttached = (computeShader != nullptr);
635 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300636 // Check whether we both have a compute and non-compute shaders attached.
637 // If there are of both types attached, then linking should fail.
638 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
639 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500640 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
642 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400643 }
644
Jamie Madill192745a2016-12-22 15:58:21 -0500645 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500646 {
Jamie Madill192745a2016-12-22 15:58:21 -0500647 if (!computeShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648 {
649 mInfoLog << "Attached compute shader is not compiled.";
650 return NoError();
651 }
Jamie Madill192745a2016-12-22 15:58:21 -0500652 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300653
Jamie Madill192745a2016-12-22 15:58:21 -0500654 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300655
656 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
657 // If the work group size is not specified, a link time error should occur.
658 if (!mState.mComputeShaderLocalSize.isDeclared())
659 {
660 mInfoLog << "Work group size is not specified.";
661 return NoError();
662 }
663
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000664 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665 {
666 return NoError();
667 }
668
669 if (!linkUniformBlocks(mInfoLog, caps))
670 {
671 return NoError();
672 }
673
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500674 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
675 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), noPacking, mInfoLog),
676 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500677 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300678 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500679 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300680 }
681 }
682 else
683 {
Jamie Madill192745a2016-12-22 15:58:21 -0500684 if (!fragmentShader || !fragmentShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300685 {
686 return NoError();
687 }
Jamie Madill192745a2016-12-22 15:58:21 -0500688 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689
Jamie Madill192745a2016-12-22 15:58:21 -0500690 if (!vertexShader || !vertexShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300691 {
692 return NoError();
693 }
Jamie Madill192745a2016-12-22 15:58:21 -0500694 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300695
Jamie Madill192745a2016-12-22 15:58:21 -0500696 if (fragmentShader->getShaderVersion() != vertexShader->getShaderVersion())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300697 {
698 mInfoLog << "Fragment shader version does not match vertex shader version.";
699 return NoError();
700 }
701
Jamie Madilleb979bf2016-11-15 12:28:46 -0500702 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 {
704 return NoError();
705 }
706
Jamie Madill192745a2016-12-22 15:58:21 -0500707 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300708 {
709 return NoError();
710 }
711
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000712 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300713 {
714 return NoError();
715 }
716
717 if (!linkUniformBlocks(mInfoLog, caps))
718 {
719 return NoError();
720 }
721
722 const auto &mergedVaryings = getMergedVaryings();
723
jchen10a9042d32017-03-17 08:50:45 +0800724 if (!linkValidateTransformFeedback(context, mInfoLog, mergedVaryings, caps))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300725 {
726 return NoError();
727 }
728
729 linkOutputVariables();
730
Jamie Madill192745a2016-12-22 15:58:21 -0500731 // Validate we can pack the varyings.
732 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
733
734 // Map the varyings to the register file
735 // In WebGL, we use a slightly different handling for packing variables.
736 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
737 : PackMode::ANGLE_RELAXED;
738 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
739 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
740 mState.getTransformFeedbackVaryingNames()))
741 {
742 return NoError();
743 }
744
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500745 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), varyingPacking, mInfoLog),
746 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500747 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300748 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500749 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300750 }
751
752 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500753 }
754
Olli Etuaho48fed632017-03-16 12:05:30 +0000755 setUniformValuesFromBindingQualifiers();
756
Jamie Madill4a3c2342015-10-08 12:58:45 -0400757 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400758
Martin Radev4c4c8e72016-08-04 12:25:34 +0300759 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000760}
761
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000762// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500763void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400765 mState.mAttributes.clear();
766 mState.mActiveAttribLocationsMask.reset();
jchen10a9042d32017-03-17 08:50:45 +0800767 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400768 mState.mUniforms.clear();
769 mState.mUniformLocations.clear();
770 mState.mUniformBlocks.clear();
771 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800772 mState.mOutputLocations.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300773 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500774 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500775
Geoff Lang7dd2e102014-11-10 15:19:26 -0500776 mValidated = false;
777
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000778 mLinked = false;
779}
780
Geoff Lange1a27752015-10-05 13:16:04 -0400781bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000782{
783 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784}
785
Jamie Madilla2c74982016-12-12 11:20:42 -0500786Error Program::loadBinary(const Context *context,
787 GLenum binaryFormat,
788 const void *binary,
789 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000790{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500791 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000792
Geoff Lang7dd2e102014-11-10 15:19:26 -0500793#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800794 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500795#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400796 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
797 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000798 {
Jamie Madillf6113162015-05-07 11:49:21 -0400799 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800800 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500801 }
802
Geoff Langc46cc2f2015-10-01 17:16:20 -0400803 BinaryInputStream stream(binary, length);
804
Jamie Madilla2c74982016-12-12 11:20:42 -0500805 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
806 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
807 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
808 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500809 {
Jamie Madillf6113162015-05-07 11:49:21 -0400810 mInfoLog << "Invalid program binary version.";
He Yunchaoacd18982017-01-04 10:46:42 +0800811 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500812 }
813
Jamie Madilla2c74982016-12-12 11:20:42 -0500814 int majorVersion = stream.readInt<int>();
815 int minorVersion = stream.readInt<int>();
816 if (majorVersion != context->getClientMajorVersion() ||
817 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500818 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500819 mInfoLog << "Cannot load program binaries across different ES context versions.";
He Yunchaoacd18982017-01-04 10:46:42 +0800820 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500821 }
822
Martin Radev4c4c8e72016-08-04 12:25:34 +0300823 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
824 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
825 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
826
Jamie Madill63805b42015-08-25 13:17:39 -0400827 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
828 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500830
Jamie Madill3da79b72015-04-27 11:09:17 -0400831 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400832 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400833 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
834 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400835 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400836 LoadShaderVar(&stream, &attrib);
837 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400838 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400839 }
840
Jamie Madill62d31cb2015-09-11 13:25:51 -0400841 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400843 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
844 {
845 LinkedUniform uniform;
846 LoadShaderVar(&stream, &uniform);
847
848 uniform.blockIndex = stream.readInt<int>();
849 uniform.blockInfo.offset = stream.readInt<int>();
850 uniform.blockInfo.arrayStride = stream.readInt<int>();
851 uniform.blockInfo.matrixStride = stream.readInt<int>();
852 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
853
Jamie Madill48ef11b2016-04-27 15:21:52 -0400854 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400855 }
856
857 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400858 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400859 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
860 uniformIndexIndex++)
861 {
862 VariableLocation variable;
863 stream.readString(&variable.name);
864 stream.readInt(&variable.element);
865 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400866 stream.readBool(&variable.used);
867 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400868
Jamie Madill48ef11b2016-04-27 15:21:52 -0400869 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400870 }
871
872 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400873 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400874 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
875 ++uniformBlockIndex)
876 {
877 UniformBlock uniformBlock;
878 stream.readString(&uniformBlock.name);
879 stream.readBool(&uniformBlock.isArray);
880 stream.readInt(&uniformBlock.arrayElement);
881 stream.readInt(&uniformBlock.dataSize);
882 stream.readBool(&uniformBlock.vertexStaticUse);
883 stream.readBool(&uniformBlock.fragmentStaticUse);
884
885 unsigned int numMembers = stream.readInt<unsigned int>();
886 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
887 {
888 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
889 }
890
Jamie Madill48ef11b2016-04-27 15:21:52 -0400891 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400892 }
893
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500894 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
895 ++bindingIndex)
896 {
897 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
898 mState.mActiveUniformBlockBindings.set(bindingIndex,
899 mState.mUniformBlockBindings[bindingIndex] != 0);
900 }
901
Brandon Jones1048ea72015-10-06 15:34:52 -0700902 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
jchen10a9042d32017-03-17 08:50:45 +0800903 ASSERT(mState.mLinkedTransformFeedbackVaryings.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700904 for (unsigned int transformFeedbackVaryingIndex = 0;
905 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
906 ++transformFeedbackVaryingIndex)
907 {
908 sh::Varying varying;
909 stream.readInt(&varying.arraySize);
910 stream.readInt(&varying.type);
911 stream.readString(&varying.name);
912
jchen10a9042d32017-03-17 08:50:45 +0800913 GLuint arrayIndex = stream.readInt<GLuint>();
914
915 mState.mLinkedTransformFeedbackVaryings.emplace_back(varying, arrayIndex);
Brandon Jones1048ea72015-10-06 15:34:52 -0700916 }
917
Jamie Madill48ef11b2016-04-27 15:21:52 -0400918 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400919
jchen1015015f72017-03-16 13:54:21 +0800920 unsigned int outputCount = stream.readInt<unsigned int>();
921 ASSERT(mState.mOutputVariables.empty());
922 for (unsigned int outputIndex = 0; outputIndex < outputCount; ++outputIndex)
923 {
924 sh::OutputVariable output;
925 LoadShaderVar(&stream, &output);
926 output.location = stream.readInt<int>();
927 mState.mOutputVariables.push_back(output);
928 }
929
Jamie Madill80a6fc02015-08-21 16:53:16 -0400930 unsigned int outputVarCount = stream.readInt<unsigned int>();
931 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
932 {
933 int locationIndex = stream.readInt<int>();
934 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400935 stream.readInt(&locationData.element);
936 stream.readInt(&locationData.index);
937 stream.readString(&locationData.name);
jchen1015015f72017-03-16 13:54:21 +0800938 mState.mOutputLocations[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400939 }
940
Jamie Madille7d84322017-01-10 18:21:59 -0500941 stream.readInt(&mState.mSamplerUniformRange.start);
942 stream.readInt(&mState.mSamplerUniformRange.end);
943
944 unsigned int samplerCount = stream.readInt<unsigned int>();
945 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
946 {
947 GLenum textureType = stream.readInt<GLenum>();
948 size_t bindingCount = stream.readInt<size_t>();
949 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
950 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400951
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500952 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000953
Jamie Madillb0a838b2016-11-13 20:02:12 -0500954 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500955#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500956}
957
Jamie Madilla2c74982016-12-12 11:20:42 -0500958Error Program::saveBinary(const Context *context,
959 GLenum *binaryFormat,
960 void *binary,
961 GLsizei bufSize,
962 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500963{
964 if (binaryFormat)
965 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400966 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500967 }
968
969 BinaryOutputStream stream;
970
Geoff Lang7dd2e102014-11-10 15:19:26 -0500971 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
972
Jamie Madilla2c74982016-12-12 11:20:42 -0500973 // nullptr context is supported when computing binary length.
974 if (context)
975 {
976 stream.writeInt(context->getClientVersion().major);
977 stream.writeInt(context->getClientVersion().minor);
978 }
979 else
980 {
981 stream.writeInt(2);
982 stream.writeInt(0);
983 }
984
Martin Radev4c4c8e72016-08-04 12:25:34 +0300985 stream.writeInt(mState.mComputeShaderLocalSize[0]);
986 stream.writeInt(mState.mComputeShaderLocalSize[1]);
987 stream.writeInt(mState.mComputeShaderLocalSize[2]);
988
Jamie Madill48ef11b2016-04-27 15:21:52 -0400989 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500990
Jamie Madill48ef11b2016-04-27 15:21:52 -0400991 stream.writeInt(mState.mAttributes.size());
992 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400993 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400994 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400995 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400996 }
997
Jamie Madill48ef11b2016-04-27 15:21:52 -0400998 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500999 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001000 {
1001 WriteShaderVar(&stream, uniform);
1002
1003 // FIXME: referenced
1004
1005 stream.writeInt(uniform.blockIndex);
1006 stream.writeInt(uniform.blockInfo.offset);
1007 stream.writeInt(uniform.blockInfo.arrayStride);
1008 stream.writeInt(uniform.blockInfo.matrixStride);
1009 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
1010 }
1011
Jamie Madill48ef11b2016-04-27 15:21:52 -04001012 stream.writeInt(mState.mUniformLocations.size());
1013 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001014 {
1015 stream.writeString(variable.name);
1016 stream.writeInt(variable.element);
1017 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -04001018 stream.writeInt(variable.used);
1019 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001020 }
1021
Jamie Madill48ef11b2016-04-27 15:21:52 -04001022 stream.writeInt(mState.mUniformBlocks.size());
1023 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001024 {
1025 stream.writeString(uniformBlock.name);
1026 stream.writeInt(uniformBlock.isArray);
1027 stream.writeInt(uniformBlock.arrayElement);
1028 stream.writeInt(uniformBlock.dataSize);
1029
1030 stream.writeInt(uniformBlock.vertexStaticUse);
1031 stream.writeInt(uniformBlock.fragmentStaticUse);
1032
1033 stream.writeInt(uniformBlock.memberUniformIndexes.size());
1034 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
1035 {
1036 stream.writeInt(memberUniformIndex);
1037 }
Jamie Madill3da79b72015-04-27 11:09:17 -04001038 }
1039
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001040 for (GLuint binding : mState.mUniformBlockBindings)
1041 {
1042 stream.writeInt(binding);
1043 }
1044
jchen10a9042d32017-03-17 08:50:45 +08001045 stream.writeInt(mState.mLinkedTransformFeedbackVaryings.size());
1046 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Brandon Jones1048ea72015-10-06 15:34:52 -07001047 {
jchen10a9042d32017-03-17 08:50:45 +08001048 stream.writeInt(var.arraySize);
1049 stream.writeInt(var.type);
1050 stream.writeString(var.name);
1051
1052 stream.writeIntOrNegOne(var.arrayIndex);
Brandon Jones1048ea72015-10-06 15:34:52 -07001053 }
1054
Jamie Madill48ef11b2016-04-27 15:21:52 -04001055 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001056
Jamie Madill48ef11b2016-04-27 15:21:52 -04001057 stream.writeInt(mState.mOutputVariables.size());
jchen1015015f72017-03-16 13:54:21 +08001058 for (const sh::OutputVariable &output : mState.mOutputVariables)
1059 {
1060 WriteShaderVar(&stream, output);
1061 stream.writeInt(output.location);
1062 }
1063
1064 stream.writeInt(mState.mOutputLocations.size());
1065 for (const auto &outputPair : mState.mOutputLocations)
Jamie Madill80a6fc02015-08-21 16:53:16 -04001066 {
1067 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -04001068 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001069 stream.writeInt(outputPair.second.index);
1070 stream.writeString(outputPair.second.name);
1071 }
1072
Jamie Madille7d84322017-01-10 18:21:59 -05001073 stream.writeInt(mState.mSamplerUniformRange.start);
1074 stream.writeInt(mState.mSamplerUniformRange.end);
1075
1076 stream.writeInt(mState.mSamplerBindings.size());
1077 for (const auto &samplerBinding : mState.mSamplerBindings)
1078 {
1079 stream.writeInt(samplerBinding.textureType);
1080 stream.writeInt(samplerBinding.boundTextureUnits.size());
1081 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001082
Jamie Madilla2c74982016-12-12 11:20:42 -05001083 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001085 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001086 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087
1088 if (streamLength > bufSize)
1089 {
1090 if (length)
1091 {
1092 *length = 0;
1093 }
1094
1095 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1096 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1097 // sizes and then copy it.
1098 return Error(GL_INVALID_OPERATION);
1099 }
1100
1101 if (binary)
1102 {
1103 char *ptr = reinterpret_cast<char*>(binary);
1104
Jamie Madill48ef11b2016-04-27 15:21:52 -04001105 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106 ptr += streamLength;
1107
1108 ASSERT(ptr - streamLength == binary);
1109 }
1110
1111 if (length)
1112 {
1113 *length = streamLength;
1114 }
1115
He Yunchaoacd18982017-01-04 10:46:42 +08001116 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117}
1118
1119GLint Program::getBinaryLength() const
1120{
1121 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001122 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123 if (error.isError())
1124 {
1125 return 0;
1126 }
1127
1128 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001129}
1130
Geoff Langc5629752015-12-07 16:29:04 -05001131void Program::setBinaryRetrievableHint(bool retrievable)
1132{
1133 // TODO(jmadill) : replace with dirty bits
1134 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001135 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001136}
1137
1138bool Program::getBinaryRetrievableHint() const
1139{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001140 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001141}
1142
Yunchao He61afff12017-03-14 15:34:03 +08001143void Program::setSeparable(bool separable)
1144{
1145 // TODO(yunchao) : replace with dirty bits
1146 if (mState.mSeparable != separable)
1147 {
1148 mProgram->setSeparable(separable);
1149 mState.mSeparable = separable;
1150 }
1151}
1152
1153bool Program::isSeparable() const
1154{
1155 return mState.mSeparable;
1156}
1157
Jamie Madill6c1f6712017-02-14 19:08:04 -05001158void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001159{
1160 mRefCount--;
1161
1162 if (mRefCount == 0 && mDeleteStatus)
1163 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001164 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001165 }
1166}
1167
1168void Program::addRef()
1169{
1170 mRefCount++;
1171}
1172
1173unsigned int Program::getRefCount() const
1174{
1175 return mRefCount;
1176}
1177
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001178int Program::getInfoLogLength() const
1179{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001180 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001181}
1182
Geoff Lange1a27752015-10-05 13:16:04 -04001183void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001185 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001186}
1187
Geoff Lange1a27752015-10-05 13:16:04 -04001188void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001189{
1190 int total = 0;
1191
Martin Radev4c4c8e72016-08-04 12:25:34 +03001192 if (mState.mAttachedComputeShader)
1193 {
1194 if (total < maxCount)
1195 {
1196 shaders[total] = mState.mAttachedComputeShader->getHandle();
1197 total++;
1198 }
1199 }
1200
Jamie Madill48ef11b2016-04-27 15:21:52 -04001201 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001202 {
1203 if (total < maxCount)
1204 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001205 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001206 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001207 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001208 }
1209
Jamie Madill48ef11b2016-04-27 15:21:52 -04001210 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001211 {
1212 if (total < maxCount)
1213 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001214 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001215 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001216 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001217 }
1218
1219 if (count)
1220 {
1221 *count = total;
1222 }
1223}
1224
Geoff Lange1a27752015-10-05 13:16:04 -04001225GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001227 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228 {
jchen1036e120e2017-03-14 14:53:58 +08001229 if (attribute.name == name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001230 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001231 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232 }
1233 }
1234
Austin Kinrossb8af7232015-03-16 22:33:25 -07001235 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001236}
1237
Jamie Madill63805b42015-08-25 13:17:39 -04001238bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001239{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001240 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1241 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242}
1243
jchen10fd7c3b52017-03-21 15:36:03 +08001244void Program::getActiveAttribute(GLuint index,
1245 GLsizei bufsize,
1246 GLsizei *length,
1247 GLint *size,
1248 GLenum *type,
1249 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001250{
Jamie Madillc349ec02015-08-21 16:53:12 -04001251 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252 {
1253 if (bufsize > 0)
1254 {
1255 name[0] = '\0';
1256 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001258 if (length)
1259 {
1260 *length = 0;
1261 }
1262
1263 *type = GL_NONE;
1264 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001265 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001266 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001267
jchen1036e120e2017-03-14 14:53:58 +08001268 ASSERT(index < mState.mAttributes.size());
1269 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001270
1271 if (bufsize > 0)
1272 {
jchen10fd7c3b52017-03-21 15:36:03 +08001273 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001274 }
1275
1276 // Always a single 'type' instance
1277 *size = 1;
1278 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001279}
1280
Geoff Lange1a27752015-10-05 13:16:04 -04001281GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001282{
Jamie Madillc349ec02015-08-21 16:53:12 -04001283 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001284 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001285 return 0;
1286 }
1287
jchen1036e120e2017-03-14 14:53:58 +08001288 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289}
1290
Geoff Lange1a27752015-10-05 13:16:04 -04001291GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001292{
Jamie Madillc349ec02015-08-21 16:53:12 -04001293 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001294 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001295 return 0;
1296 }
1297
1298 size_t maxLength = 0;
1299
Jamie Madill48ef11b2016-04-27 15:21:52 -04001300 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001301 {
jchen1036e120e2017-03-14 14:53:58 +08001302 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001303 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001304
Jamie Madillc349ec02015-08-21 16:53:12 -04001305 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306}
1307
jchen1015015f72017-03-16 13:54:21 +08001308GLuint Program::getInputResourceIndex(const GLchar *name) const
1309{
1310 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1311 {
1312 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1313 if (attribute.name == name)
1314 {
1315 return attributeIndex;
1316 }
1317 }
1318 return GL_INVALID_INDEX;
1319}
1320
1321GLuint Program::getOutputResourceIndex(const GLchar *name) const
1322{
1323 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1324}
1325
jchen10fd7c3b52017-03-21 15:36:03 +08001326size_t Program::getOutputResourceCount() const
1327{
1328 return (mLinked ? mState.mOutputVariables.size() : 0);
1329}
1330
1331void Program::getInputResourceName(GLuint index,
1332 GLsizei bufSize,
1333 GLsizei *length,
1334 GLchar *name) const
1335{
1336 GLint size;
1337 GLenum type;
1338 getActiveAttribute(index, bufSize, length, &size, &type, name);
1339}
1340
1341void Program::getOutputResourceName(GLuint index,
1342 GLsizei bufSize,
1343 GLsizei *length,
1344 GLchar *name) const
1345{
1346 if (length)
1347 {
1348 *length = 0;
1349 }
1350
1351 if (!mLinked)
1352 {
1353 if (bufSize > 0)
1354 {
1355 name[0] = '\0';
1356 }
1357 return;
1358 }
1359 ASSERT(index < mState.mOutputVariables.size());
1360 const auto &output = mState.mOutputVariables[index];
1361
1362 if (bufSize > 0)
1363 {
1364 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1365
1366 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1367 }
1368}
1369
Geoff Lang7dd2e102014-11-10 15:19:26 -05001370GLint Program::getFragDataLocation(const std::string &name) const
1371{
1372 std::string baseName(name);
1373 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001374 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001375 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001376 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1378 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001379 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001380 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001381 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001383}
1384
Geoff Lange1a27752015-10-05 13:16:04 -04001385void Program::getActiveUniform(GLuint index,
1386 GLsizei bufsize,
1387 GLsizei *length,
1388 GLint *size,
1389 GLenum *type,
1390 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001391{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001393 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001394 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001395 ASSERT(index < mState.mUniforms.size());
1396 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397
1398 if (bufsize > 0)
1399 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001400 std::string string = uniform.name;
1401 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402 {
1403 string += "[0]";
1404 }
jchen10fd7c3b52017-03-21 15:36:03 +08001405 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406 }
1407
Jamie Madill62d31cb2015-09-11 13:25:51 -04001408 *size = uniform.elementCount();
1409 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001410 }
1411 else
1412 {
1413 if (bufsize > 0)
1414 {
1415 name[0] = '\0';
1416 }
1417
1418 if (length)
1419 {
1420 *length = 0;
1421 }
1422
1423 *size = 0;
1424 *type = GL_NONE;
1425 }
1426}
1427
Geoff Lange1a27752015-10-05 13:16:04 -04001428GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001429{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001431 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001432 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001433 }
1434 else
1435 {
1436 return 0;
1437 }
1438}
1439
Geoff Lange1a27752015-10-05 13:16:04 -04001440GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001441{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001442 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443
1444 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001445 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001446 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001448 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001450 size_t length = uniform.name.length() + 1u;
1451 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001452 {
1453 length += 3; // Counting in "[0]".
1454 }
1455 maxLength = std::max(length, maxLength);
1456 }
1457 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001458 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459
Jamie Madill62d31cb2015-09-11 13:25:51 -04001460 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461}
1462
1463GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1464{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001465 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001466 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001468 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1470 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1471 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1472 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1473 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1474 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1475 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1476 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1477 default:
1478 UNREACHABLE();
1479 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001480 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481 return 0;
1482}
1483
1484bool Program::isValidUniformLocation(GLint location) const
1485{
Jamie Madille2e406c2016-06-02 13:04:10 -04001486 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001487 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1488 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001489}
1490
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001493 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001494 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495}
1496
Jamie Madillac4e9c32017-01-13 14:07:12 -05001497const VariableLocation &Program::getUniformLocation(GLint location) const
1498{
1499 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1500 return mState.mUniformLocations[location];
1501}
1502
1503const std::vector<VariableLocation> &Program::getUniformLocations() const
1504{
1505 return mState.mUniformLocations;
1506}
1507
1508const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1509{
1510 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1511 return mState.mUniforms[index];
1512}
1513
Jamie Madill62d31cb2015-09-11 13:25:51 -04001514GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001515{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001516 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517}
1518
Jamie Madill62d31cb2015-09-11 13:25:51 -04001519GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520{
Jamie Madille7d84322017-01-10 18:21:59 -05001521 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522}
1523
1524void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1525{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001526 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1527 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528}
1529
1530void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1531{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001532 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1533 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001534}
1535
1536void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1537{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001538 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1539 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540}
1541
1542void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1543{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001544 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1545 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001546}
1547
1548void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1549{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001550 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1551 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552}
1553
1554void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1555{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001556 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1557 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001558}
1559
1560void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1561{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001562 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1563 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564}
1565
1566void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1567{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001568 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1569 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001570}
1571
1572void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1573{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001574 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1575 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576}
1577
1578void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1579{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001580 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1581 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001582}
1583
1584void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1585{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001586 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1587 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001588}
1589
1590void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1591{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001592 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1593 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001594}
1595
1596void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1597{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001598 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1599 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001600}
1601
1602void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1603{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001604 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1605 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606}
1607
1608void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1609{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001610 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1611 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001612}
1613
1614void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1615{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001616 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1617 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618}
1619
1620void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1621{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001622 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1623 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624}
1625
1626void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1627{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001628 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1629 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001630}
1631
1632void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1633{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001634 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1635 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636}
1637
1638void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1639{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001640 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1641 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001642}
1643
1644void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1645{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001646 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1647 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648}
1649
Geoff Lange1a27752015-10-05 13:16:04 -04001650void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001652 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653}
1654
Geoff Lange1a27752015-10-05 13:16:04 -04001655void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658}
1659
Geoff Lange1a27752015-10-05 13:16:04 -04001660void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001662 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663}
1664
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665void Program::flagForDeletion()
1666{
1667 mDeleteStatus = true;
1668}
1669
1670bool Program::isFlaggedForDeletion() const
1671{
1672 return mDeleteStatus;
1673}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001674
Brandon Jones43a53e22014-08-28 16:23:22 -07001675void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001676{
1677 mInfoLog.reset();
1678
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001680 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001681 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001682 }
1683 else
1684 {
Jamie Madillf6113162015-05-07 11:49:21 -04001685 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001686 }
1687}
1688
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1690{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001691 // Skip cache if we're using an infolog, so we get the full error.
1692 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1693 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1694 {
1695 return mCachedValidateSamplersResult.value();
1696 }
1697
1698 if (mTextureUnitTypesCache.empty())
1699 {
1700 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1701 }
1702 else
1703 {
1704 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1705 }
1706
1707 // if any two active samplers in a program are of different types, but refer to the same
1708 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1709 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001710 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001711 {
Jamie Madille7d84322017-01-10 18:21:59 -05001712 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001713
Jamie Madille7d84322017-01-10 18:21:59 -05001714 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001715 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001716 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1717 {
1718 if (infoLog)
1719 {
1720 (*infoLog) << "Sampler uniform (" << textureUnit
1721 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1722 << caps.maxCombinedTextureImageUnits << ")";
1723 }
1724
1725 mCachedValidateSamplersResult = false;
1726 return false;
1727 }
1728
1729 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1730 {
1731 if (textureType != mTextureUnitTypesCache[textureUnit])
1732 {
1733 if (infoLog)
1734 {
1735 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1736 "image unit ("
1737 << textureUnit << ").";
1738 }
1739
1740 mCachedValidateSamplersResult = false;
1741 return false;
1742 }
1743 }
1744 else
1745 {
1746 mTextureUnitTypesCache[textureUnit] = textureType;
1747 }
1748 }
1749 }
1750
1751 mCachedValidateSamplersResult = true;
1752 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001753}
1754
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001755bool Program::isValidated() const
1756{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001757 return mValidated;
1758}
1759
Geoff Lange1a27752015-10-05 13:16:04 -04001760GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001761{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001762 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763}
1764
1765void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1766{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001767 ASSERT(
1768 uniformBlockIndex <
1769 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770
Jamie Madill48ef11b2016-04-27 15:21:52 -04001771 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772
1773 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001774 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 std::string string = uniformBlock.name;
1776
Jamie Madill62d31cb2015-09-11 13:25:51 -04001777 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780 }
jchen10fd7c3b52017-03-21 15:36:03 +08001781 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001782 }
1783}
1784
Geoff Lange1a27752015-10-05 13:16:04 -04001785GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001786{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 int maxLength = 0;
1788
1789 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001790 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001791 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1793 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001794 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 if (!uniformBlock.name.empty())
1796 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001797 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798
1799 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001800 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801
1802 maxLength = std::max(length + arrayLength, maxLength);
1803 }
1804 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001805 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806
1807 return maxLength;
1808}
1809
Geoff Lange1a27752015-10-05 13:16:04 -04001810GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001812 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001813 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001814
Jamie Madill48ef11b2016-04-27 15:21:52 -04001815 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001816 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1817 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001818 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001819 if (uniformBlock.name == baseName)
1820 {
1821 const bool arrayElementZero =
1822 (subscript == GL_INVALID_INDEX &&
1823 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1824 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1825 {
1826 return blockIndex;
1827 }
1828 }
1829 }
1830
1831 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001832}
1833
Jamie Madill62d31cb2015-09-11 13:25:51 -04001834const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001835{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001836 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1837 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001838}
1839
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001840void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1841{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001842 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001843 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001844 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001845}
1846
1847GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1848{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001849 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001850}
1851
1852void Program::resetUniformBlockBindings()
1853{
1854 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1855 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001856 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001857 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001858 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001859}
1860
Geoff Lang48dcae72014-02-05 16:28:24 -05001861void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1862{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001863 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001864 for (GLsizei i = 0; i < count; i++)
1865 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001866 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001867 }
1868
Jamie Madill48ef11b2016-04-27 15:21:52 -04001869 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001870}
1871
1872void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1873{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001875 {
jchen10a9042d32017-03-17 08:50:45 +08001876 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1877 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1878 std::string varName = var.nameWithArrayIndex();
1879 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001880 if (length)
1881 {
1882 *length = lastNameIdx;
1883 }
1884 if (size)
1885 {
jchen10a9042d32017-03-17 08:50:45 +08001886 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001887 }
1888 if (type)
1889 {
jchen10a9042d32017-03-17 08:50:45 +08001890 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001891 }
1892 if (name)
1893 {
jchen10a9042d32017-03-17 08:50:45 +08001894 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001895 name[lastNameIdx] = '\0';
1896 }
1897 }
1898}
1899
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001900GLsizei Program::getTransformFeedbackVaryingCount() const
1901{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001903 {
jchen10a9042d32017-03-17 08:50:45 +08001904 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001905 }
1906 else
1907 {
1908 return 0;
1909 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001910}
1911
1912GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1913{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001914 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001915 {
1916 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001917 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001918 {
jchen10a9042d32017-03-17 08:50:45 +08001919 maxSize =
1920 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001921 }
1922
1923 return maxSize;
1924 }
1925 else
1926 {
1927 return 0;
1928 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001929}
1930
1931GLenum Program::getTransformFeedbackBufferMode() const
1932{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001933 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934}
1935
Jamie Madill192745a2016-12-22 15:58:21 -05001936bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001937{
Jamie Madill192745a2016-12-22 15:58:21 -05001938 const Shader *vertexShader = mState.mAttachedVertexShader;
1939 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1940
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001941 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1942
Jamie Madill4cff2472015-08-21 16:53:18 -04001943 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1944 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945
Sami Väisänen46eaa942016-06-29 10:26:37 +03001946 std::map<GLuint, std::string> staticFragmentInputLocations;
1947
Jamie Madill4cff2472015-08-21 16:53:18 -04001948 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001950 bool matched = false;
1951
1952 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001953 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 {
1955 continue;
1956 }
1957
Jamie Madill4cff2472015-08-21 16:53:18 -04001958 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001960 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001961 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001962 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001963 if (!linkValidateVaryings(infoLog, output.name, input, output,
1964 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965 {
1966 return false;
1967 }
1968
Geoff Lang7dd2e102014-11-10 15:19:26 -05001969 matched = true;
1970 break;
1971 }
1972 }
1973
1974 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001975 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001976 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001977 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001978 return false;
1979 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001980
1981 // Check for aliased path rendering input bindings (if any).
1982 // If more than one binding refer statically to the same
1983 // location the link must fail.
1984
1985 if (!output.staticUse)
1986 continue;
1987
1988 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1989 if (inputBinding == -1)
1990 continue;
1991
1992 const auto it = staticFragmentInputLocations.find(inputBinding);
1993 if (it == std::end(staticFragmentInputLocations))
1994 {
1995 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1996 }
1997 else
1998 {
1999 infoLog << "Binding for fragment input " << output.name << " conflicts with "
2000 << it->second;
2001 return false;
2002 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 }
2004
Yuly Novikov817232e2017-02-22 18:36:10 -05002005 if (!linkValidateBuiltInVaryings(infoLog))
2006 {
2007 return false;
2008 }
2009
Jamie Madillada9ecc2015-08-17 12:53:37 -04002010 // TODO(jmadill): verify no unmatched vertex varyings?
2011
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012 return true;
2013}
2014
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002015bool Program::linkUniforms(InfoLog &infoLog,
2016 const Caps &caps,
2017 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002018{
Olli Etuahob78707c2017-03-09 15:03:11 +00002019 UniformLinker linker(mState);
2020 if (!linker.link(infoLog, caps, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 {
2022 return false;
2023 }
2024
Olli Etuahob78707c2017-03-09 15:03:11 +00002025 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002026
Olli Etuaho48fed632017-03-16 12:05:30 +00002027 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002028
2029 return true;
2030}
2031
Olli Etuaho48fed632017-03-16 12:05:30 +00002032void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002033{
2034 mState.mSamplerUniformRange.end = static_cast<unsigned int>(mState.mUniforms.size());
2035 mState.mSamplerUniformRange.start = mState.mSamplerUniformRange.end;
2036 auto samplerIter = mState.mUniforms.rbegin();
2037 while (samplerIter != mState.mUniforms.rend() && samplerIter->isSampler())
2038 {
2039 --mState.mSamplerUniformRange.start;
2040 ++samplerIter;
2041 }
2042 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2043 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
2044 samplerIndex < mState.mUniforms.size(); ++samplerIndex)
2045 {
2046 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2047 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2048 mState.mSamplerBindings.emplace_back(
2049 SamplerBinding(textureType, samplerUniform.elementCount()));
2050 }
2051}
2052
Martin Radev4c4c8e72016-08-04 12:25:34 +03002053bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2054 const std::string &uniformName,
2055 const sh::InterfaceBlockField &vertexUniform,
2056 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057{
Jamie Madillc4c744222015-11-04 09:39:47 -05002058 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2059 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002060 {
2061 return false;
2062 }
2063
2064 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2065 {
Jamie Madillf6113162015-05-07 11:49:21 -04002066 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 return false;
2068 }
2069
2070 return true;
2071}
2072
Jamie Madilleb979bf2016-11-15 12:28:46 -05002073// Assigns locations to all attributes from the bindings and program locations.
2074bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002076 const auto *vertexShader = mState.getAttachedVertexShader();
2077
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002079 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002080 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002081
2082 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002083 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002084 {
Jamie Madillf6113162015-05-07 11:49:21 -04002085 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002086 return false;
2087 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002089 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002090
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002092 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002093 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002094 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002095 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002096 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002097 attribute.location = bindingLocation;
2098 }
2099
2100 if (attribute.location != -1)
2101 {
2102 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002103 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104
Jamie Madill63805b42015-08-25 13:17:39 -04002105 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 {
Jamie Madillf6113162015-05-07 11:49:21 -04002107 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002108 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109
2110 return false;
2111 }
2112
Jamie Madill63805b42015-08-25 13:17:39 -04002113 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114 {
Jamie Madill63805b42015-08-25 13:17:39 -04002115 const int regLocation = attribute.location + reg;
2116 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117
2118 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002119 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002120 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002122 // TODO(jmadill): fix aliasing on ES2
2123 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002125 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002126 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 return false;
2128 }
2129 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002130 else
2131 {
Jamie Madill63805b42015-08-25 13:17:39 -04002132 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002133 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134
Jamie Madill63805b42015-08-25 13:17:39 -04002135 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002136 }
2137 }
2138 }
2139
2140 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002141 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002143 // Not set by glBindAttribLocation or by location layout qualifier
2144 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 {
Jamie Madill63805b42015-08-25 13:17:39 -04002146 int regs = VariableRegisterCount(attribute.type);
2147 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148
Jamie Madill63805b42015-08-25 13:17:39 -04002149 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150 {
Jamie Madillf6113162015-05-07 11:49:21 -04002151 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002152 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153 }
2154
Jamie Madillc349ec02015-08-21 16:53:12 -04002155 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002156 }
2157 }
2158
Jamie Madill48ef11b2016-04-27 15:21:52 -04002159 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002160 {
Jamie Madill63805b42015-08-25 13:17:39 -04002161 ASSERT(attribute.location != -1);
2162 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002163
Jamie Madill63805b42015-08-25 13:17:39 -04002164 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002165 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002166 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 }
2168 }
2169
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170 return true;
2171}
2172
Martin Radev4c4c8e72016-08-04 12:25:34 +03002173bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2174 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2175 const std::string &errorMessage,
2176 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002178 GLuint blockCount = 0;
2179 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002181 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002182 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002183 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002184 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002186 return false;
2187 }
2188 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002190 return true;
2191}
Jamie Madille473dee2015-08-18 14:49:01 -04002192
Martin Radev4c4c8e72016-08-04 12:25:34 +03002193bool Program::validateVertexAndFragmentInterfaceBlocks(
2194 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2195 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2196 InfoLog &infoLog) const
2197{
2198 // Check that interface blocks defined in the vertex and fragment shaders are identical
2199 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2200 UniformBlockMap linkedUniformBlocks;
2201
2202 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2203 {
2204 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2205 }
2206
Jamie Madille473dee2015-08-18 14:49:01 -04002207 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 {
Jamie Madille473dee2015-08-18 14:49:01 -04002209 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210 if (entry != linkedUniformBlocks.end())
2211 {
2212 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2213 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2214 {
2215 return false;
2216 }
2217 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002218 }
2219 return true;
2220}
Jamie Madille473dee2015-08-18 14:49:01 -04002221
Martin Radev4c4c8e72016-08-04 12:25:34 +03002222bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2223{
2224 if (mState.mAttachedComputeShader)
2225 {
2226 const Shader &computeShader = *mState.mAttachedComputeShader;
2227 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2228
2229 if (!validateUniformBlocksCount(
2230 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2231 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2232 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002234 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002236 return true;
2237 }
2238
2239 const Shader &vertexShader = *mState.mAttachedVertexShader;
2240 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2241
2242 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2243 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2244
2245 if (!validateUniformBlocksCount(
2246 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2247 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2248 {
2249 return false;
2250 }
2251 if (!validateUniformBlocksCount(
2252 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2253 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2254 infoLog))
2255 {
2256
2257 return false;
2258 }
2259 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2260 infoLog))
2261 {
2262 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002263 }
Jamie Madille473dee2015-08-18 14:49:01 -04002264
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265 return true;
2266}
2267
Jamie Madilla2c74982016-12-12 11:20:42 -05002268bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002269 const sh::InterfaceBlock &vertexInterfaceBlock,
2270 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271{
2272 const char* blockName = vertexInterfaceBlock.name.c_str();
2273 // validate blocks for the same member types
2274 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2275 {
Jamie Madillf6113162015-05-07 11:49:21 -04002276 infoLog << "Types for interface block '" << blockName
2277 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 return false;
2279 }
2280 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2281 {
Jamie Madillf6113162015-05-07 11:49:21 -04002282 infoLog << "Array sizes differ for interface block '" << blockName
2283 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002284 return false;
2285 }
2286 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2287 {
Jamie Madillf6113162015-05-07 11:49:21 -04002288 infoLog << "Layout qualifiers differ for interface block '" << blockName
2289 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002290 return false;
2291 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002292 const unsigned int numBlockMembers =
2293 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2295 {
2296 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2297 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2298 if (vertexMember.name != fragmentMember.name)
2299 {
Jamie Madillf6113162015-05-07 11:49:21 -04002300 infoLog << "Name mismatch for field " << blockMemberIndex
2301 << " of interface block '" << blockName
2302 << "': (in vertex: '" << vertexMember.name
2303 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304 return false;
2305 }
2306 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2307 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2308 {
2309 return false;
2310 }
2311 }
2312 return true;
2313}
2314
2315bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2316 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2317{
2318 if (vertexVariable.type != fragmentVariable.type)
2319 {
Jamie Madillf6113162015-05-07 11:49:21 -04002320 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002321 return false;
2322 }
2323 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2324 {
Jamie Madillf6113162015-05-07 11:49:21 -04002325 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002326 return false;
2327 }
2328 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2329 {
Jamie Madillf6113162015-05-07 11:49:21 -04002330 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002331 return false;
2332 }
2333
2334 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2335 {
Jamie Madillf6113162015-05-07 11:49:21 -04002336 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002337 return false;
2338 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002339 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002340 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2341 {
2342 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2343 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2344
2345 if (vertexMember.name != fragmentMember.name)
2346 {
Jamie Madillf6113162015-05-07 11:49:21 -04002347 infoLog << "Name mismatch for field '" << memberIndex
2348 << "' of " << variableName
2349 << ": (in vertex: '" << vertexMember.name
2350 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351 return false;
2352 }
2353
2354 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2355 vertexMember.name + "'";
2356
2357 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2358 {
2359 return false;
2360 }
2361 }
2362
2363 return true;
2364}
2365
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002366bool Program::linkValidateVaryings(InfoLog &infoLog,
2367 const std::string &varyingName,
2368 const sh::Varying &vertexVarying,
2369 const sh::Varying &fragmentVarying,
2370 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371{
2372 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2373 {
2374 return false;
2375 }
2376
Jamie Madille9cc4692015-02-19 16:00:13 -05002377 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002378 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002379 infoLog << "Interpolation types for " << varyingName
2380 << " differ between vertex and fragment shaders.";
2381 return false;
2382 }
2383
2384 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2385 {
2386 infoLog << "Invariance for " << varyingName
2387 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002388 return false;
2389 }
2390
2391 return true;
2392}
2393
Yuly Novikov817232e2017-02-22 18:36:10 -05002394bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
2395{
2396 const Shader *vertexShader = mState.mAttachedVertexShader;
2397 const Shader *fragmentShader = mState.mAttachedFragmentShader;
2398 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
2399 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
2400 int shaderVersion = vertexShader->getShaderVersion();
2401
2402 if (shaderVersion != 100)
2403 {
2404 // Only ESSL 1.0 has restrictions on matching input and output invariance
2405 return true;
2406 }
2407
2408 bool glPositionIsInvariant = false;
2409 bool glPointSizeIsInvariant = false;
2410 bool glFragCoordIsInvariant = false;
2411 bool glPointCoordIsInvariant = false;
2412
2413 for (const sh::Varying &varying : vertexVaryings)
2414 {
2415 if (!varying.isBuiltIn())
2416 {
2417 continue;
2418 }
2419 if (varying.name.compare("gl_Position") == 0)
2420 {
2421 glPositionIsInvariant = varying.isInvariant;
2422 }
2423 else if (varying.name.compare("gl_PointSize") == 0)
2424 {
2425 glPointSizeIsInvariant = varying.isInvariant;
2426 }
2427 }
2428
2429 for (const sh::Varying &varying : fragmentVaryings)
2430 {
2431 if (!varying.isBuiltIn())
2432 {
2433 continue;
2434 }
2435 if (varying.name.compare("gl_FragCoord") == 0)
2436 {
2437 glFragCoordIsInvariant = varying.isInvariant;
2438 }
2439 else if (varying.name.compare("gl_PointCoord") == 0)
2440 {
2441 glPointCoordIsInvariant = varying.isInvariant;
2442 }
2443 }
2444
2445 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2446 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2447 // Not requiring invariance to match is supported by:
2448 // dEQP, WebGL CTS, Nexus 5X GLES
2449 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2450 {
2451 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2452 "declared invariant.";
2453 return false;
2454 }
2455 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2456 {
2457 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2458 "declared invariant.";
2459 return false;
2460 }
2461
2462 return true;
2463}
2464
jchen10a9042d32017-03-17 08:50:45 +08002465bool Program::linkValidateTransformFeedback(const gl::Context *context,
2466 InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002467 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002468 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002469{
2470 size_t totalComponents = 0;
2471
Jamie Madillccdf74b2015-08-18 10:46:12 -04002472 std::set<std::string> uniqueNames;
2473
Jamie Madill48ef11b2016-04-27 15:21:52 -04002474 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002475 {
2476 bool found = false;
jchen10a9042d32017-03-17 08:50:45 +08002477 size_t subscript = GL_INVALID_INDEX;
2478 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
2479
Jamie Madill192745a2016-12-22 15:58:21 -05002480 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002481 {
Jamie Madill192745a2016-12-22 15:58:21 -05002482 const sh::Varying *varying = ref.second.get();
2483
jchen10a9042d32017-03-17 08:50:45 +08002484 if (baseName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002485 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002486 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002487 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002488 infoLog << "Two transform feedback varyings specify the same output variable ("
2489 << tfVaryingName << ").";
2490 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002491 }
jchen10a9042d32017-03-17 08:50:45 +08002492 if (context->getClientVersion() >= Version(3, 1))
2493 {
2494 if (IncludeSameArrayElement(uniqueNames, tfVaryingName))
2495 {
2496 infoLog
2497 << "Two transform feedback varyings include the same array element ("
2498 << tfVaryingName << ").";
2499 return false;
2500 }
2501 }
2502 else if (varying->isArray())
Geoff Lang1a683462015-09-29 15:09:59 -04002503 {
2504 infoLog << "Capture of arrays is undefined and not supported.";
2505 return false;
2506 }
2507
jchen10a9042d32017-03-17 08:50:45 +08002508 uniqueNames.insert(tfVaryingName);
2509
Jamie Madillccdf74b2015-08-18 10:46:12 -04002510 // TODO(jmadill): Investigate implementation limits on D3D11
jchen10a9042d32017-03-17 08:50:45 +08002511 size_t elementCount =
2512 ((varying->isArray() && subscript == GL_INVALID_INDEX) ? varying->elementCount()
2513 : 1);
2514 size_t componentCount = VariableComponentCount(varying->type) * elementCount;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002515 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002516 componentCount > caps.maxTransformFeedbackSeparateComponents)
2517 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002518 infoLog << "Transform feedback varying's " << varying->name << " components ("
2519 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002520 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002521 return false;
2522 }
2523
2524 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002525 found = true;
2526 break;
2527 }
2528 }
jchen10a9042d32017-03-17 08:50:45 +08002529 if (context->getClientVersion() < Version(3, 1) &&
2530 tfVaryingName.find('[') != std::string::npos)
Jamie Madill89bb70e2015-08-31 14:18:39 -04002531 {
Geoff Lang1a683462015-09-29 15:09:59 -04002532 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002533 return false;
2534 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002535 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2536 ASSERT(found);
2537 }
2538
Jamie Madill48ef11b2016-04-27 15:21:52 -04002539 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002540 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002541 {
Jamie Madillf6113162015-05-07 11:49:21 -04002542 infoLog << "Transform feedback varying total components (" << totalComponents
2543 << ") exceed the maximum interleaved components ("
2544 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002545 return false;
2546 }
2547
2548 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002549}
2550
Jamie Madill192745a2016-12-22 15:58:21 -05002551void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002552{
2553 // Gather the linked varyings that are used for transform feedback, they should all exist.
jchen10a9042d32017-03-17 08:50:45 +08002554 mState.mLinkedTransformFeedbackVaryings.clear();
Jamie Madill48ef11b2016-04-27 15:21:52 -04002555 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002556 {
jchen10a9042d32017-03-17 08:50:45 +08002557 size_t subscript = GL_INVALID_INDEX;
2558 std::string baseName = ParseResourceName(tfVaryingName, &subscript);
Jamie Madill192745a2016-12-22 15:58:21 -05002559 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002560 {
Jamie Madill192745a2016-12-22 15:58:21 -05002561 const sh::Varying *varying = ref.second.get();
jchen10a9042d32017-03-17 08:50:45 +08002562 if (baseName == varying->name)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002563 {
jchen10a9042d32017-03-17 08:50:45 +08002564 mState.mLinkedTransformFeedbackVaryings.emplace_back(
2565 *varying, static_cast<GLuint>(subscript));
Jamie Madillccdf74b2015-08-18 10:46:12 -04002566 break;
2567 }
2568 }
2569 }
2570}
2571
Jamie Madill192745a2016-12-22 15:58:21 -05002572Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002573{
Jamie Madill192745a2016-12-22 15:58:21 -05002574 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002575
Jamie Madill48ef11b2016-04-27 15:21:52 -04002576 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002577 {
Jamie Madill192745a2016-12-22 15:58:21 -05002578 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002579 }
2580
Jamie Madill48ef11b2016-04-27 15:21:52 -04002581 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002582 {
Jamie Madill192745a2016-12-22 15:58:21 -05002583 merged[varying.name].fragment = &varying;
2584 }
2585
2586 return merged;
2587}
2588
2589std::vector<PackedVarying> Program::getPackedVaryings(
2590 const Program::MergedVaryings &mergedVaryings) const
2591{
2592 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2593 std::vector<PackedVarying> packedVaryings;
jchen10a9042d32017-03-17 08:50:45 +08002594 std::set<std::string> uniqueFullNames;
Jamie Madill192745a2016-12-22 15:58:21 -05002595
2596 for (const auto &ref : mergedVaryings)
2597 {
2598 const sh::Varying *input = ref.second.vertex;
2599 const sh::Varying *output = ref.second.fragment;
2600
2601 // Only pack varyings that have a matched input or output, plus special builtins.
2602 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002603 {
Jamie Madill192745a2016-12-22 15:58:21 -05002604 // Will get the vertex shader interpolation by default.
2605 auto interpolation = ref.second.get()->interpolation;
2606
2607 // Interpolation qualifiers must match.
2608 if (output->isStruct())
2609 {
2610 ASSERT(!output->isArray());
2611 for (const auto &field : output->fields)
2612 {
2613 ASSERT(!field.isStruct() && !field.isArray());
2614 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2615 }
2616 }
2617 else
2618 {
2619 packedVaryings.push_back(PackedVarying(*output, interpolation));
2620 }
2621 continue;
2622 }
2623
2624 // Keep Transform FB varyings in the merged list always.
2625 if (!input)
2626 {
2627 continue;
2628 }
2629
2630 for (const std::string &tfVarying : tfVaryings)
2631 {
jchen10a9042d32017-03-17 08:50:45 +08002632 size_t subscript = GL_INVALID_INDEX;
2633 std::string baseName = ParseResourceName(tfVarying, &subscript);
2634 if (uniqueFullNames.count(tfVarying) > 0)
2635 {
2636 continue;
2637 }
2638 if (baseName == input->name)
Jamie Madill192745a2016-12-22 15:58:21 -05002639 {
2640 // Transform feedback for varying structs is underspecified.
2641 // See Khronos bug 9856.
2642 // TODO(jmadill): Figure out how to be spec-compliant here.
2643 if (!input->isStruct())
2644 {
2645 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2646 packedVaryings.back().vertexOnly = true;
jchen10a9042d32017-03-17 08:50:45 +08002647 packedVaryings.back().arrayIndex = static_cast<GLuint>(subscript);
2648 uniqueFullNames.insert(tfVarying);
Jamie Madill192745a2016-12-22 15:58:21 -05002649 }
jchen10a9042d32017-03-17 08:50:45 +08002650 if (subscript == GL_INVALID_INDEX)
2651 {
2652 break;
2653 }
Jamie Madill192745a2016-12-22 15:58:21 -05002654 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002655 }
2656 }
2657
Jamie Madill192745a2016-12-22 15:58:21 -05002658 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2659
2660 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002661}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002662
2663void Program::linkOutputVariables()
2664{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002665 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002666 ASSERT(fragmentShader != nullptr);
2667
2668 // Skip this step for GLES2 shaders.
2669 if (fragmentShader->getShaderVersion() == 100)
2670 return;
2671
jchen1015015f72017-03-16 13:54:21 +08002672 mState.mOutputVariables = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002673 // TODO(jmadill): any caps validation here?
2674
jchen1015015f72017-03-16 13:54:21 +08002675 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002676 outputVariableIndex++)
2677 {
jchen1015015f72017-03-16 13:54:21 +08002678 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002679
2680 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2681 if (outputVariable.isBuiltIn())
2682 continue;
2683
2684 // Since multiple output locations must be specified, use 0 for non-specified locations.
2685 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2686
Jamie Madill80a6fc02015-08-21 16:53:16 -04002687 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2688 elementIndex++)
2689 {
2690 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002691 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002692 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002693 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002694 VariableLocation(outputVariable.name, element, outputVariableIndex);
2695 }
2696 }
2697}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002698
Olli Etuaho48fed632017-03-16 12:05:30 +00002699void Program::setUniformValuesFromBindingQualifiers()
2700{
2701 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
2702 samplerIndex < mState.mSamplerUniformRange.end; ++samplerIndex)
2703 {
2704 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2705 if (samplerUniform.binding != -1)
2706 {
2707 GLint location = mState.getUniformLocation(samplerUniform.name);
2708 ASSERT(location != -1);
2709 std::vector<GLint> boundTextureUnits;
2710 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2711 ++elementIndex)
2712 {
2713 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2714 }
2715 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2716 boundTextureUnits.data());
2717 }
2718 }
2719}
2720
Jamie Madill62d31cb2015-09-11 13:25:51 -04002721void Program::gatherInterfaceBlockInfo()
2722{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002723 ASSERT(mState.mUniformBlocks.empty());
2724
2725 if (mState.mAttachedComputeShader)
2726 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002727 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002728
2729 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2730 {
2731
2732 // Only 'packed' blocks are allowed to be considered inactive.
2733 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2734 continue;
2735
Jamie Madilla2c74982016-12-12 11:20:42 -05002736 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002737 {
2738 if (block.name == computeBlock.name)
2739 {
2740 block.computeStaticUse = computeBlock.staticUse;
2741 }
2742 }
2743
2744 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2745 }
2746 return;
2747 }
2748
Jamie Madill62d31cb2015-09-11 13:25:51 -04002749 std::set<std::string> visitedList;
2750
Jamie Madilla2c74982016-12-12 11:20:42 -05002751 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002752
Jamie Madill62d31cb2015-09-11 13:25:51 -04002753 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2754 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002755 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002756 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2757 continue;
2758
2759 if (visitedList.count(vertexBlock.name) > 0)
2760 continue;
2761
2762 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2763 visitedList.insert(vertexBlock.name);
2764 }
2765
Jamie Madilla2c74982016-12-12 11:20:42 -05002766 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767
2768 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2769 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002770 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002771 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2772 continue;
2773
2774 if (visitedList.count(fragmentBlock.name) > 0)
2775 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002776 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002777 {
2778 if (block.name == fragmentBlock.name)
2779 {
2780 block.fragmentStaticUse = fragmentBlock.staticUse;
2781 }
2782 }
2783
2784 continue;
2785 }
2786
2787 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2788 visitedList.insert(fragmentBlock.name);
2789 }
2790}
2791
Jamie Madill4a3c2342015-10-08 12:58:45 -04002792template <typename VarT>
2793void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2794 const std::string &prefix,
2795 int blockIndex)
2796{
2797 for (const VarT &field : fields)
2798 {
2799 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2800
2801 if (field.isStruct())
2802 {
2803 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2804 {
2805 const std::string uniformElementName =
2806 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2807 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2808 }
2809 }
2810 else
2811 {
2812 // If getBlockMemberInfo returns false, the uniform is optimized out.
2813 sh::BlockMemberInfo memberInfo;
2814 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2815 {
2816 continue;
2817 }
2818
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002819 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002820 blockIndex, memberInfo);
2821
2822 // Since block uniforms have no location, we don't need to store them in the uniform
2823 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002824 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002825 }
2826 }
2827}
2828
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2830{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002831 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002832 size_t blockSize = 0;
2833
2834 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002835 std::stringstream blockNameStr;
2836 blockNameStr << interfaceBlock.name;
2837 if (interfaceBlock.arraySize > 0)
2838 {
2839 blockNameStr << "[0]";
2840 }
2841 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002842 {
2843 return;
2844 }
2845
2846 // Track the first and last uniform index to determine the range of active uniforms in the
2847 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002848 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002849 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002850 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002851
2852 std::vector<unsigned int> blockUniformIndexes;
2853 for (size_t blockUniformIndex = firstBlockUniformIndex;
2854 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2855 {
2856 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2857 }
2858
2859 if (interfaceBlock.arraySize > 0)
2860 {
2861 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2862 {
2863 UniformBlock block(interfaceBlock.name, true, arrayElement);
2864 block.memberUniformIndexes = blockUniformIndexes;
2865
Martin Radev4c4c8e72016-08-04 12:25:34 +03002866 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002867 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002868 case GL_VERTEX_SHADER:
2869 {
2870 block.vertexStaticUse = interfaceBlock.staticUse;
2871 break;
2872 }
2873 case GL_FRAGMENT_SHADER:
2874 {
2875 block.fragmentStaticUse = interfaceBlock.staticUse;
2876 break;
2877 }
2878 case GL_COMPUTE_SHADER:
2879 {
2880 block.computeStaticUse = interfaceBlock.staticUse;
2881 break;
2882 }
2883 default:
2884 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002885 }
2886
Qin Jiajia0350a642016-11-01 17:01:51 +08002887 // Since all block elements in an array share the same active uniforms, they will all be
2888 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2889 // here we will add every block element in the array.
2890 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002891 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002892 }
2893 }
2894 else
2895 {
2896 UniformBlock block(interfaceBlock.name, false, 0);
2897 block.memberUniformIndexes = blockUniformIndexes;
2898
Martin Radev4c4c8e72016-08-04 12:25:34 +03002899 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002900 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002901 case GL_VERTEX_SHADER:
2902 {
2903 block.vertexStaticUse = interfaceBlock.staticUse;
2904 break;
2905 }
2906 case GL_FRAGMENT_SHADER:
2907 {
2908 block.fragmentStaticUse = interfaceBlock.staticUse;
2909 break;
2910 }
2911 case GL_COMPUTE_SHADER:
2912 {
2913 block.computeStaticUse = interfaceBlock.staticUse;
2914 break;
2915 }
2916 default:
2917 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002918 }
2919
Jamie Madill4a3c2342015-10-08 12:58:45 -04002920 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002921 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002922 }
2923}
2924
Jamie Madille7d84322017-01-10 18:21:59 -05002925template <>
2926void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2927 const uint8_t *destPointer,
2928 GLsizei clampedCount,
2929 const GLint *v)
2930{
2931 // Invalidate the validation cache only if we modify the sampler data.
2932 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2933 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2934 {
2935 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2936 std::vector<GLuint> *boundTextureUnits =
2937 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2938
2939 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2940 mCachedValidateSamplersResult.reset();
2941 }
2942}
2943
2944template <typename T>
2945void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2946 const uint8_t *destPointer,
2947 GLsizei clampedCount,
2948 const T *v)
2949{
2950}
2951
Jamie Madill62d31cb2015-09-11 13:25:51 -04002952template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002953GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002954{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002955 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2956 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002957 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2958
Corentin Wallez15ac5342016-11-03 17:06:39 -04002959 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2960 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2961 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002962 GLsizei maxElementCount =
2963 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2964
2965 GLsizei count = countIn;
2966 GLsizei clampedCount = count * vectorSize;
2967 if (clampedCount > maxElementCount)
2968 {
2969 clampedCount = maxElementCount;
2970 count = maxElementCount / vectorSize;
2971 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002972
Jamie Madill62d31cb2015-09-11 13:25:51 -04002973 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2974 {
2975 // Do a cast conversion for boolean types. From the spec:
2976 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2977 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002978 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002979 {
2980 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2981 }
2982 }
2983 else
2984 {
Jamie Madille7d84322017-01-10 18:21:59 -05002985 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002986 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002987 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002988
2989 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002990}
2991
2992template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002993GLsizei Program::setMatrixUniformInternal(GLint location,
2994 GLsizei count,
2995 GLboolean transpose,
2996 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002997{
2998 if (!transpose)
2999 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003000 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003001 }
3002
3003 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003004 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3005 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003006 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003007
3008 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3009 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3010 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3011 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3012
3013 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003014 {
3015 size_t elementOffset = element * rows * cols;
3016
3017 for (size_t row = 0; row < rows; ++row)
3018 {
3019 for (size_t col = 0; col < cols; ++col)
3020 {
3021 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3022 }
3023 }
3024 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003025
3026 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003027}
3028
3029template <typename DestT>
3030void Program::getUniformInternal(GLint location, DestT *dataOut) const
3031{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003032 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3033 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003034
3035 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3036
3037 GLenum componentType = VariableComponentType(uniform.type);
3038 if (componentType == GLTypeToGLenum<DestT>::value)
3039 {
3040 memcpy(dataOut, srcPointer, uniform.getElementSize());
3041 return;
3042 }
3043
Corentin Wallez6596c462016-03-17 17:26:58 -04003044 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003045
3046 switch (componentType)
3047 {
3048 case GL_INT:
3049 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3050 break;
3051 case GL_UNSIGNED_INT:
3052 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3053 break;
3054 case GL_BOOL:
3055 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3056 break;
3057 case GL_FLOAT:
3058 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3059 break;
3060 default:
3061 UNREACHABLE();
3062 }
3063}
Jamie Madilla4595b82017-01-11 17:36:34 -05003064
3065bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3066{
3067 // Must be called after samplers are validated.
3068 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3069
3070 for (const auto &binding : mState.mSamplerBindings)
3071 {
3072 GLenum textureType = binding.textureType;
3073 for (const auto &unit : binding.boundTextureUnits)
3074 {
3075 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3076 if (programTextureID == textureID)
3077 {
3078 // TODO(jmadill): Check for appropriate overlap.
3079 return true;
3080 }
3081 }
3082 }
3083
3084 return false;
3085}
3086
Jamie Madilla2c74982016-12-12 11:20:42 -05003087} // namespace gl