blob: a0f74366df82066b82fd62ddd6f85a95d76164f0 [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);
jchen10af713a22017-04-19 09:10:56 +0800881 stream.readInt(&uniformBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400882 stream.readInt(&uniformBlock.dataSize);
883 stream.readBool(&uniformBlock.vertexStaticUse);
884 stream.readBool(&uniformBlock.fragmentStaticUse);
885
886 unsigned int numMembers = stream.readInt<unsigned int>();
887 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
888 {
889 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
890 }
891
Jamie Madill48ef11b2016-04-27 15:21:52 -0400892 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400893 }
894
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500895 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
896 ++bindingIndex)
897 {
898 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
899 mState.mActiveUniformBlockBindings.set(bindingIndex,
900 mState.mUniformBlockBindings[bindingIndex] != 0);
901 }
902
Brandon Jones1048ea72015-10-06 15:34:52 -0700903 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
jchen10a9042d32017-03-17 08:50:45 +0800904 ASSERT(mState.mLinkedTransformFeedbackVaryings.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700905 for (unsigned int transformFeedbackVaryingIndex = 0;
906 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
907 ++transformFeedbackVaryingIndex)
908 {
909 sh::Varying varying;
910 stream.readInt(&varying.arraySize);
911 stream.readInt(&varying.type);
912 stream.readString(&varying.name);
913
jchen10a9042d32017-03-17 08:50:45 +0800914 GLuint arrayIndex = stream.readInt<GLuint>();
915
916 mState.mLinkedTransformFeedbackVaryings.emplace_back(varying, arrayIndex);
Brandon Jones1048ea72015-10-06 15:34:52 -0700917 }
918
Jamie Madill48ef11b2016-04-27 15:21:52 -0400919 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400920
jchen1015015f72017-03-16 13:54:21 +0800921 unsigned int outputCount = stream.readInt<unsigned int>();
922 ASSERT(mState.mOutputVariables.empty());
923 for (unsigned int outputIndex = 0; outputIndex < outputCount; ++outputIndex)
924 {
925 sh::OutputVariable output;
926 LoadShaderVar(&stream, &output);
927 output.location = stream.readInt<int>();
928 mState.mOutputVariables.push_back(output);
929 }
930
Jamie Madill80a6fc02015-08-21 16:53:16 -0400931 unsigned int outputVarCount = stream.readInt<unsigned int>();
932 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
933 {
934 int locationIndex = stream.readInt<int>();
935 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400936 stream.readInt(&locationData.element);
937 stream.readInt(&locationData.index);
938 stream.readString(&locationData.name);
jchen1015015f72017-03-16 13:54:21 +0800939 mState.mOutputLocations[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400940 }
941
Jamie Madille7d84322017-01-10 18:21:59 -0500942 stream.readInt(&mState.mSamplerUniformRange.start);
943 stream.readInt(&mState.mSamplerUniformRange.end);
944
945 unsigned int samplerCount = stream.readInt<unsigned int>();
946 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
947 {
948 GLenum textureType = stream.readInt<GLenum>();
949 size_t bindingCount = stream.readInt<size_t>();
950 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
951 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400952
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500953 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000954
Jamie Madillb0a838b2016-11-13 20:02:12 -0500955 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500956#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500957}
958
Jamie Madilla2c74982016-12-12 11:20:42 -0500959Error Program::saveBinary(const Context *context,
960 GLenum *binaryFormat,
961 void *binary,
962 GLsizei bufSize,
963 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500964{
965 if (binaryFormat)
966 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400967 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500968 }
969
970 BinaryOutputStream stream;
971
Geoff Lang7dd2e102014-11-10 15:19:26 -0500972 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
973
Jamie Madilla2c74982016-12-12 11:20:42 -0500974 // nullptr context is supported when computing binary length.
975 if (context)
976 {
977 stream.writeInt(context->getClientVersion().major);
978 stream.writeInt(context->getClientVersion().minor);
979 }
980 else
981 {
982 stream.writeInt(2);
983 stream.writeInt(0);
984 }
985
Martin Radev4c4c8e72016-08-04 12:25:34 +0300986 stream.writeInt(mState.mComputeShaderLocalSize[0]);
987 stream.writeInt(mState.mComputeShaderLocalSize[1]);
988 stream.writeInt(mState.mComputeShaderLocalSize[2]);
989
Jamie Madill48ef11b2016-04-27 15:21:52 -0400990 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500991
Jamie Madill48ef11b2016-04-27 15:21:52 -0400992 stream.writeInt(mState.mAttributes.size());
993 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400994 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400995 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400996 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400997 }
998
Jamie Madill48ef11b2016-04-27 15:21:52 -0400999 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001000 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001001 {
1002 WriteShaderVar(&stream, uniform);
1003
1004 // FIXME: referenced
1005
1006 stream.writeInt(uniform.blockIndex);
1007 stream.writeInt(uniform.blockInfo.offset);
1008 stream.writeInt(uniform.blockInfo.arrayStride);
1009 stream.writeInt(uniform.blockInfo.matrixStride);
1010 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
1011 }
1012
Jamie Madill48ef11b2016-04-27 15:21:52 -04001013 stream.writeInt(mState.mUniformLocations.size());
1014 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001015 {
1016 stream.writeString(variable.name);
1017 stream.writeInt(variable.element);
1018 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -04001019 stream.writeInt(variable.used);
1020 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001021 }
1022
Jamie Madill48ef11b2016-04-27 15:21:52 -04001023 stream.writeInt(mState.mUniformBlocks.size());
1024 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001025 {
1026 stream.writeString(uniformBlock.name);
1027 stream.writeInt(uniformBlock.isArray);
1028 stream.writeInt(uniformBlock.arrayElement);
jchen10af713a22017-04-19 09:10:56 +08001029 stream.writeInt(uniformBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001030 stream.writeInt(uniformBlock.dataSize);
1031
1032 stream.writeInt(uniformBlock.vertexStaticUse);
1033 stream.writeInt(uniformBlock.fragmentStaticUse);
1034
1035 stream.writeInt(uniformBlock.memberUniformIndexes.size());
1036 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
1037 {
1038 stream.writeInt(memberUniformIndex);
1039 }
Jamie Madill3da79b72015-04-27 11:09:17 -04001040 }
1041
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001042 for (GLuint binding : mState.mUniformBlockBindings)
1043 {
1044 stream.writeInt(binding);
1045 }
1046
jchen10a9042d32017-03-17 08:50:45 +08001047 stream.writeInt(mState.mLinkedTransformFeedbackVaryings.size());
1048 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Brandon Jones1048ea72015-10-06 15:34:52 -07001049 {
jchen10a9042d32017-03-17 08:50:45 +08001050 stream.writeInt(var.arraySize);
1051 stream.writeInt(var.type);
1052 stream.writeString(var.name);
1053
1054 stream.writeIntOrNegOne(var.arrayIndex);
Brandon Jones1048ea72015-10-06 15:34:52 -07001055 }
1056
Jamie Madill48ef11b2016-04-27 15:21:52 -04001057 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001058
Jamie Madill48ef11b2016-04-27 15:21:52 -04001059 stream.writeInt(mState.mOutputVariables.size());
jchen1015015f72017-03-16 13:54:21 +08001060 for (const sh::OutputVariable &output : mState.mOutputVariables)
1061 {
1062 WriteShaderVar(&stream, output);
1063 stream.writeInt(output.location);
1064 }
1065
1066 stream.writeInt(mState.mOutputLocations.size());
1067 for (const auto &outputPair : mState.mOutputLocations)
Jamie Madill80a6fc02015-08-21 16:53:16 -04001068 {
1069 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -04001070 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001071 stream.writeInt(outputPair.second.index);
1072 stream.writeString(outputPair.second.name);
1073 }
1074
Jamie Madille7d84322017-01-10 18:21:59 -05001075 stream.writeInt(mState.mSamplerUniformRange.start);
1076 stream.writeInt(mState.mSamplerUniformRange.end);
1077
1078 stream.writeInt(mState.mSamplerBindings.size());
1079 for (const auto &samplerBinding : mState.mSamplerBindings)
1080 {
1081 stream.writeInt(samplerBinding.textureType);
1082 stream.writeInt(samplerBinding.boundTextureUnits.size());
1083 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001084
Jamie Madilla2c74982016-12-12 11:20:42 -05001085 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001086
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001087 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001088 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001089
1090 if (streamLength > bufSize)
1091 {
1092 if (length)
1093 {
1094 *length = 0;
1095 }
1096
1097 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1098 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1099 // sizes and then copy it.
1100 return Error(GL_INVALID_OPERATION);
1101 }
1102
1103 if (binary)
1104 {
1105 char *ptr = reinterpret_cast<char*>(binary);
1106
Jamie Madill48ef11b2016-04-27 15:21:52 -04001107 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001108 ptr += streamLength;
1109
1110 ASSERT(ptr - streamLength == binary);
1111 }
1112
1113 if (length)
1114 {
1115 *length = streamLength;
1116 }
1117
He Yunchaoacd18982017-01-04 10:46:42 +08001118 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001119}
1120
1121GLint Program::getBinaryLength() const
1122{
1123 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001124 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001125 if (error.isError())
1126 {
1127 return 0;
1128 }
1129
1130 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001131}
1132
Geoff Langc5629752015-12-07 16:29:04 -05001133void Program::setBinaryRetrievableHint(bool retrievable)
1134{
1135 // TODO(jmadill) : replace with dirty bits
1136 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001137 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001138}
1139
1140bool Program::getBinaryRetrievableHint() const
1141{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001142 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001143}
1144
Yunchao He61afff12017-03-14 15:34:03 +08001145void Program::setSeparable(bool separable)
1146{
1147 // TODO(yunchao) : replace with dirty bits
1148 if (mState.mSeparable != separable)
1149 {
1150 mProgram->setSeparable(separable);
1151 mState.mSeparable = separable;
1152 }
1153}
1154
1155bool Program::isSeparable() const
1156{
1157 return mState.mSeparable;
1158}
1159
Jamie Madill6c1f6712017-02-14 19:08:04 -05001160void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001161{
1162 mRefCount--;
1163
1164 if (mRefCount == 0 && mDeleteStatus)
1165 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001166 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001167 }
1168}
1169
1170void Program::addRef()
1171{
1172 mRefCount++;
1173}
1174
1175unsigned int Program::getRefCount() const
1176{
1177 return mRefCount;
1178}
1179
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001180int Program::getInfoLogLength() const
1181{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001182 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001183}
1184
Geoff Lange1a27752015-10-05 13:16:04 -04001185void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001186{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001187 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001188}
1189
Geoff Lange1a27752015-10-05 13:16:04 -04001190void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001191{
1192 int total = 0;
1193
Martin Radev4c4c8e72016-08-04 12:25:34 +03001194 if (mState.mAttachedComputeShader)
1195 {
1196 if (total < maxCount)
1197 {
1198 shaders[total] = mState.mAttachedComputeShader->getHandle();
1199 total++;
1200 }
1201 }
1202
Jamie Madill48ef11b2016-04-27 15:21:52 -04001203 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001204 {
1205 if (total < maxCount)
1206 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001207 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001208 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001209 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001210 }
1211
Jamie Madill48ef11b2016-04-27 15:21:52 -04001212 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001213 {
1214 if (total < maxCount)
1215 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001217 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001218 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001219 }
1220
1221 if (count)
1222 {
1223 *count = total;
1224 }
1225}
1226
Geoff Lange1a27752015-10-05 13:16:04 -04001227GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001229 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001230 {
jchen1036e120e2017-03-14 14:53:58 +08001231 if (attribute.name == name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001233 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234 }
1235 }
1236
Austin Kinrossb8af7232015-03-16 22:33:25 -07001237 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238}
1239
Jamie Madill63805b42015-08-25 13:17:39 -04001240bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001241{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001242 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1243 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001244}
1245
jchen10fd7c3b52017-03-21 15:36:03 +08001246void Program::getActiveAttribute(GLuint index,
1247 GLsizei bufsize,
1248 GLsizei *length,
1249 GLint *size,
1250 GLenum *type,
1251 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252{
Jamie Madillc349ec02015-08-21 16:53:12 -04001253 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254 {
1255 if (bufsize > 0)
1256 {
1257 name[0] = '\0';
1258 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001259
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001260 if (length)
1261 {
1262 *length = 0;
1263 }
1264
1265 *type = GL_NONE;
1266 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001267 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001268 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001269
jchen1036e120e2017-03-14 14:53:58 +08001270 ASSERT(index < mState.mAttributes.size());
1271 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001272
1273 if (bufsize > 0)
1274 {
jchen10fd7c3b52017-03-21 15:36:03 +08001275 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001276 }
1277
1278 // Always a single 'type' instance
1279 *size = 1;
1280 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001281}
1282
Geoff Lange1a27752015-10-05 13:16:04 -04001283GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001284{
Jamie Madillc349ec02015-08-21 16:53:12 -04001285 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001286 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001287 return 0;
1288 }
1289
jchen1036e120e2017-03-14 14:53:58 +08001290 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001291}
1292
Geoff Lange1a27752015-10-05 13:16:04 -04001293GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001294{
Jamie Madillc349ec02015-08-21 16:53:12 -04001295 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001296 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001297 return 0;
1298 }
1299
1300 size_t maxLength = 0;
1301
Jamie Madill48ef11b2016-04-27 15:21:52 -04001302 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001303 {
jchen1036e120e2017-03-14 14:53:58 +08001304 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001305 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306
Jamie Madillc349ec02015-08-21 16:53:12 -04001307 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308}
1309
jchen1015015f72017-03-16 13:54:21 +08001310GLuint Program::getInputResourceIndex(const GLchar *name) const
1311{
1312 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1313 {
1314 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1315 if (attribute.name == name)
1316 {
1317 return attributeIndex;
1318 }
1319 }
1320 return GL_INVALID_INDEX;
1321}
1322
1323GLuint Program::getOutputResourceIndex(const GLchar *name) const
1324{
1325 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1326}
1327
jchen10fd7c3b52017-03-21 15:36:03 +08001328size_t Program::getOutputResourceCount() const
1329{
1330 return (mLinked ? mState.mOutputVariables.size() : 0);
1331}
1332
1333void Program::getInputResourceName(GLuint index,
1334 GLsizei bufSize,
1335 GLsizei *length,
1336 GLchar *name) const
1337{
1338 GLint size;
1339 GLenum type;
1340 getActiveAttribute(index, bufSize, length, &size, &type, name);
1341}
1342
1343void Program::getOutputResourceName(GLuint index,
1344 GLsizei bufSize,
1345 GLsizei *length,
1346 GLchar *name) const
1347{
1348 if (length)
1349 {
1350 *length = 0;
1351 }
1352
1353 if (!mLinked)
1354 {
1355 if (bufSize > 0)
1356 {
1357 name[0] = '\0';
1358 }
1359 return;
1360 }
1361 ASSERT(index < mState.mOutputVariables.size());
1362 const auto &output = mState.mOutputVariables[index];
1363
1364 if (bufSize > 0)
1365 {
1366 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1367
1368 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1369 }
1370}
1371
Geoff Lang7dd2e102014-11-10 15:19:26 -05001372GLint Program::getFragDataLocation(const std::string &name) const
1373{
1374 std::string baseName(name);
1375 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001376 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001377 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001378 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1380 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001381 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001383 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001385}
1386
Geoff Lange1a27752015-10-05 13:16:04 -04001387void Program::getActiveUniform(GLuint index,
1388 GLsizei bufsize,
1389 GLsizei *length,
1390 GLint *size,
1391 GLenum *type,
1392 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001393{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001395 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001396 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001397 ASSERT(index < mState.mUniforms.size());
1398 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001399
1400 if (bufsize > 0)
1401 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001402 std::string string = uniform.name;
1403 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404 {
1405 string += "[0]";
1406 }
jchen10fd7c3b52017-03-21 15:36:03 +08001407 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408 }
1409
Jamie Madill62d31cb2015-09-11 13:25:51 -04001410 *size = uniform.elementCount();
1411 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001412 }
1413 else
1414 {
1415 if (bufsize > 0)
1416 {
1417 name[0] = '\0';
1418 }
1419
1420 if (length)
1421 {
1422 *length = 0;
1423 }
1424
1425 *size = 0;
1426 *type = GL_NONE;
1427 }
1428}
1429
Geoff Lange1a27752015-10-05 13:16:04 -04001430GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001431{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001433 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001434 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001435 }
1436 else
1437 {
1438 return 0;
1439 }
1440}
1441
Geoff Lange1a27752015-10-05 13:16:04 -04001442GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001443{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001444 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445
1446 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001447 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001448 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001450 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001452 size_t length = uniform.name.length() + 1u;
1453 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454 {
1455 length += 3; // Counting in "[0]".
1456 }
1457 maxLength = std::max(length, maxLength);
1458 }
1459 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001460 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463}
1464
1465GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1466{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001467 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001468 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001470 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1472 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1473 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1474 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1475 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1476 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1477 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1478 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1479 default:
1480 UNREACHABLE();
1481 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001482 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483 return 0;
1484}
1485
1486bool Program::isValidUniformLocation(GLint location) const
1487{
Jamie Madille2e406c2016-06-02 13:04:10 -04001488 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001489 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1490 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001491}
1492
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001495 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001496 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497}
1498
Jamie Madillac4e9c32017-01-13 14:07:12 -05001499const VariableLocation &Program::getUniformLocation(GLint location) const
1500{
1501 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1502 return mState.mUniformLocations[location];
1503}
1504
1505const std::vector<VariableLocation> &Program::getUniformLocations() const
1506{
1507 return mState.mUniformLocations;
1508}
1509
1510const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1511{
1512 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1513 return mState.mUniforms[index];
1514}
1515
Jamie Madill62d31cb2015-09-11 13:25:51 -04001516GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001518 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001519}
1520
Jamie Madill62d31cb2015-09-11 13:25:51 -04001521GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522{
Jamie Madille7d84322017-01-10 18:21:59 -05001523 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001524}
1525
1526void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1527{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001528 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1529 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530}
1531
1532void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1533{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001534 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1535 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001536}
1537
1538void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1539{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001540 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1541 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001542}
1543
1544void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1545{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001546 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1547 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548}
1549
1550void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1551{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001552 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1553 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001554}
1555
1556void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1557{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001558 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1559 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560}
1561
1562void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1563{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001564 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1565 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001566}
1567
1568void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1569{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001570 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1571 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001572}
1573
1574void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1575{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001576 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1577 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001578}
1579
1580void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1581{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001582 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1583 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584}
1585
1586void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1587{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001588 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1589 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590}
1591
1592void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1593{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001594 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1595 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001596}
1597
1598void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1599{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001600 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1601 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001602}
1603
1604void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1605{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001606 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1607 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001608}
1609
1610void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1611{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001612 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1613 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614}
1615
1616void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1617{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001618 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1619 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620}
1621
1622void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1623{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001624 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1625 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001626}
1627
1628void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1629{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001630 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1631 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632}
1633
1634void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1635{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001636 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1637 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638}
1639
1640void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1641{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001642 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1643 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001644}
1645
1646void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1647{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001648 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1649 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650}
1651
Geoff Lange1a27752015-10-05 13:16:04 -04001652void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001654 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655}
1656
Geoff Lange1a27752015-10-05 13:16:04 -04001657void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001659 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001660}
1661
Geoff Lange1a27752015-10-05 13:16:04 -04001662void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001665}
1666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667void Program::flagForDeletion()
1668{
1669 mDeleteStatus = true;
1670}
1671
1672bool Program::isFlaggedForDeletion() const
1673{
1674 return mDeleteStatus;
1675}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001676
Brandon Jones43a53e22014-08-28 16:23:22 -07001677void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001678{
1679 mInfoLog.reset();
1680
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001682 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001683 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001684 }
1685 else
1686 {
Jamie Madillf6113162015-05-07 11:49:21 -04001687 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001688 }
1689}
1690
Geoff Lang7dd2e102014-11-10 15:19:26 -05001691bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1692{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001693 // Skip cache if we're using an infolog, so we get the full error.
1694 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1695 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1696 {
1697 return mCachedValidateSamplersResult.value();
1698 }
1699
1700 if (mTextureUnitTypesCache.empty())
1701 {
1702 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1703 }
1704 else
1705 {
1706 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1707 }
1708
1709 // if any two active samplers in a program are of different types, but refer to the same
1710 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1711 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001712 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001713 {
Jamie Madille7d84322017-01-10 18:21:59 -05001714 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001715
Jamie Madille7d84322017-01-10 18:21:59 -05001716 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001717 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001718 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1719 {
1720 if (infoLog)
1721 {
1722 (*infoLog) << "Sampler uniform (" << textureUnit
1723 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1724 << caps.maxCombinedTextureImageUnits << ")";
1725 }
1726
1727 mCachedValidateSamplersResult = false;
1728 return false;
1729 }
1730
1731 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1732 {
1733 if (textureType != mTextureUnitTypesCache[textureUnit])
1734 {
1735 if (infoLog)
1736 {
1737 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1738 "image unit ("
1739 << textureUnit << ").";
1740 }
1741
1742 mCachedValidateSamplersResult = false;
1743 return false;
1744 }
1745 }
1746 else
1747 {
1748 mTextureUnitTypesCache[textureUnit] = textureType;
1749 }
1750 }
1751 }
1752
1753 mCachedValidateSamplersResult = true;
1754 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755}
1756
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001757bool Program::isValidated() const
1758{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001759 return mValidated;
1760}
1761
Geoff Lange1a27752015-10-05 13:16:04 -04001762GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001764 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001765}
1766
1767void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1768{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001769 ASSERT(
1770 uniformBlockIndex <
1771 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772
Jamie Madill48ef11b2016-04-27 15:21:52 -04001773 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001774
1775 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001776 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777 std::string string = uniformBlock.name;
1778
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001781 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782 }
jchen10fd7c3b52017-03-21 15:36:03 +08001783 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001784 }
1785}
1786
Geoff Lange1a27752015-10-05 13:16:04 -04001787GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001788{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 int maxLength = 0;
1790
1791 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001792 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001793 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001794 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1795 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001796 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797 if (!uniformBlock.name.empty())
1798 {
jchen10af713a22017-04-19 09:10:56 +08001799 int length = static_cast<int>(uniformBlock.nameWithArrayIndex().length());
1800 maxLength = std::max(length + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801 }
1802 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001803 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804
1805 return maxLength;
1806}
1807
Geoff Lange1a27752015-10-05 13:16:04 -04001808GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001810 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001811 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001812
Jamie Madill48ef11b2016-04-27 15:21:52 -04001813 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001814 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1815 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001816 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001817 if (uniformBlock.name == baseName)
1818 {
1819 const bool arrayElementZero =
1820 (subscript == GL_INVALID_INDEX &&
1821 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1822 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1823 {
1824 return blockIndex;
1825 }
1826 }
1827 }
1828
1829 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001830}
1831
Jamie Madill62d31cb2015-09-11 13:25:51 -04001832const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001833{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001834 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1835 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001836}
1837
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001838void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1839{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001840 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001841 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001842 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001843}
1844
1845GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1846{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001847 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001848}
1849
1850void Program::resetUniformBlockBindings()
1851{
1852 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1853 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001854 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001855 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001856 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001857}
1858
Geoff Lang48dcae72014-02-05 16:28:24 -05001859void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1860{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001861 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001862 for (GLsizei i = 0; i < count; i++)
1863 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001864 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001865 }
1866
Jamie Madill48ef11b2016-04-27 15:21:52 -04001867 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001868}
1869
1870void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1871{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001872 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001873 {
jchen10a9042d32017-03-17 08:50:45 +08001874 ASSERT(index < mState.mLinkedTransformFeedbackVaryings.size());
1875 const auto &var = mState.mLinkedTransformFeedbackVaryings[index];
1876 std::string varName = var.nameWithArrayIndex();
1877 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varName.length()));
Geoff Lang48dcae72014-02-05 16:28:24 -05001878 if (length)
1879 {
1880 *length = lastNameIdx;
1881 }
1882 if (size)
1883 {
jchen10a9042d32017-03-17 08:50:45 +08001884 *size = var.size();
Geoff Lang48dcae72014-02-05 16:28:24 -05001885 }
1886 if (type)
1887 {
jchen10a9042d32017-03-17 08:50:45 +08001888 *type = var.type;
Geoff Lang48dcae72014-02-05 16:28:24 -05001889 }
1890 if (name)
1891 {
jchen10a9042d32017-03-17 08:50:45 +08001892 memcpy(name, varName.c_str(), lastNameIdx);
Geoff Lang48dcae72014-02-05 16:28:24 -05001893 name[lastNameIdx] = '\0';
1894 }
1895 }
1896}
1897
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001898GLsizei Program::getTransformFeedbackVaryingCount() const
1899{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001901 {
jchen10a9042d32017-03-17 08:50:45 +08001902 return static_cast<GLsizei>(mState.mLinkedTransformFeedbackVaryings.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001903 }
1904 else
1905 {
1906 return 0;
1907 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001908}
1909
1910GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1911{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001913 {
1914 GLsizei maxSize = 0;
jchen10a9042d32017-03-17 08:50:45 +08001915 for (const auto &var : mState.mLinkedTransformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -05001916 {
jchen10a9042d32017-03-17 08:50:45 +08001917 maxSize =
1918 std::max(maxSize, static_cast<GLsizei>(var.nameWithArrayIndex().length() + 1));
Geoff Lang48dcae72014-02-05 16:28:24 -05001919 }
1920
1921 return maxSize;
1922 }
1923 else
1924 {
1925 return 0;
1926 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001927}
1928
1929GLenum Program::getTransformFeedbackBufferMode() const
1930{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001931 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932}
1933
Jamie Madill192745a2016-12-22 15:58:21 -05001934bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001935{
Jamie Madill192745a2016-12-22 15:58:21 -05001936 const Shader *vertexShader = mState.mAttachedVertexShader;
1937 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1938
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001939 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1940
Jamie Madill4cff2472015-08-21 16:53:18 -04001941 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1942 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943
Sami Väisänen46eaa942016-06-29 10:26:37 +03001944 std::map<GLuint, std::string> staticFragmentInputLocations;
1945
Jamie Madill4cff2472015-08-21 16:53:18 -04001946 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001948 bool matched = false;
1949
1950 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001951 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001952 {
1953 continue;
1954 }
1955
Jamie Madill4cff2472015-08-21 16:53:18 -04001956 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001958 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001960 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001961 if (!linkValidateVaryings(infoLog, output.name, input, output,
1962 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001963 {
1964 return false;
1965 }
1966
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967 matched = true;
1968 break;
1969 }
1970 }
1971
1972 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001973 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001974 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001975 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001976 return false;
1977 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001978
1979 // Check for aliased path rendering input bindings (if any).
1980 // If more than one binding refer statically to the same
1981 // location the link must fail.
1982
1983 if (!output.staticUse)
1984 continue;
1985
1986 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1987 if (inputBinding == -1)
1988 continue;
1989
1990 const auto it = staticFragmentInputLocations.find(inputBinding);
1991 if (it == std::end(staticFragmentInputLocations))
1992 {
1993 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1994 }
1995 else
1996 {
1997 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1998 << it->second;
1999 return false;
2000 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001 }
2002
Yuly Novikov817232e2017-02-22 18:36:10 -05002003 if (!linkValidateBuiltInVaryings(infoLog))
2004 {
2005 return false;
2006 }
2007
Jamie Madillada9ecc2015-08-17 12:53:37 -04002008 // TODO(jmadill): verify no unmatched vertex varyings?
2009
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 return true;
2011}
2012
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002013bool Program::linkUniforms(InfoLog &infoLog,
2014 const Caps &caps,
2015 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002016{
Olli Etuahob78707c2017-03-09 15:03:11 +00002017 UniformLinker linker(mState);
2018 if (!linker.link(infoLog, caps, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 {
2020 return false;
2021 }
2022
Olli Etuahob78707c2017-03-09 15:03:11 +00002023 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002024
Olli Etuaho48fed632017-03-16 12:05:30 +00002025 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002026
2027 return true;
2028}
2029
Olli Etuaho48fed632017-03-16 12:05:30 +00002030void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002031{
2032 mState.mSamplerUniformRange.end = static_cast<unsigned int>(mState.mUniforms.size());
2033 mState.mSamplerUniformRange.start = mState.mSamplerUniformRange.end;
2034 auto samplerIter = mState.mUniforms.rbegin();
2035 while (samplerIter != mState.mUniforms.rend() && samplerIter->isSampler())
2036 {
2037 --mState.mSamplerUniformRange.start;
2038 ++samplerIter;
2039 }
2040 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2041 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
2042 samplerIndex < mState.mUniforms.size(); ++samplerIndex)
2043 {
2044 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2045 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2046 mState.mSamplerBindings.emplace_back(
2047 SamplerBinding(textureType, samplerUniform.elementCount()));
2048 }
2049}
2050
Martin Radev4c4c8e72016-08-04 12:25:34 +03002051bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2052 const std::string &uniformName,
2053 const sh::InterfaceBlockField &vertexUniform,
2054 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002055{
Jamie Madillc4c744222015-11-04 09:39:47 -05002056 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2057 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 {
2059 return false;
2060 }
2061
2062 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2063 {
Jamie Madillf6113162015-05-07 11:49:21 -04002064 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065 return false;
2066 }
2067
2068 return true;
2069}
2070
Jamie Madilleb979bf2016-11-15 12:28:46 -05002071// Assigns locations to all attributes from the bindings and program locations.
2072bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002074 const auto *vertexShader = mState.getAttachedVertexShader();
2075
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002077 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002078 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002079
2080 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002081 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002082 {
Jamie Madillf6113162015-05-07 11:49:21 -04002083 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002084 return false;
2085 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002087 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002088
Jamie Madillc349ec02015-08-21 16:53:12 -04002089 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002090 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002092 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002093 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002094 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002095 attribute.location = bindingLocation;
2096 }
2097
2098 if (attribute.location != -1)
2099 {
2100 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002101 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102
Jamie Madill63805b42015-08-25 13:17:39 -04002103 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 {
Jamie Madillf6113162015-05-07 11:49:21 -04002105 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002106 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107
2108 return false;
2109 }
2110
Jamie Madill63805b42015-08-25 13:17:39 -04002111 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 {
Jamie Madill63805b42015-08-25 13:17:39 -04002113 const int regLocation = attribute.location + reg;
2114 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115
2116 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002117 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002120 // TODO(jmadill): fix aliasing on ES2
2121 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002123 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002124 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 return false;
2126 }
2127 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002128 else
2129 {
Jamie Madill63805b42015-08-25 13:17:39 -04002130 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002131 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132
Jamie Madill63805b42015-08-25 13:17:39 -04002133 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 }
2135 }
2136 }
2137
2138 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002139 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002141 // Not set by glBindAttribLocation or by location layout qualifier
2142 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 {
Jamie Madill63805b42015-08-25 13:17:39 -04002144 int regs = VariableRegisterCount(attribute.type);
2145 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146
Jamie Madill63805b42015-08-25 13:17:39 -04002147 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 {
Jamie Madillf6113162015-05-07 11:49:21 -04002149 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002150 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 }
2152
Jamie Madillc349ec02015-08-21 16:53:12 -04002153 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 }
2155 }
2156
Jamie Madill48ef11b2016-04-27 15:21:52 -04002157 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158 {
Jamie Madill63805b42015-08-25 13:17:39 -04002159 ASSERT(attribute.location != -1);
2160 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002161
Jamie Madill63805b42015-08-25 13:17:39 -04002162 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002164 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002165 }
2166 }
2167
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168 return true;
2169}
2170
Martin Radev4c4c8e72016-08-04 12:25:34 +03002171bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2172 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2173 const std::string &errorMessage,
2174 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002176 GLuint blockCount = 0;
2177 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002179 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002180 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002181 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002182 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002183 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002184 return false;
2185 }
2186 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002188 return true;
2189}
Jamie Madille473dee2015-08-18 14:49:01 -04002190
Martin Radev4c4c8e72016-08-04 12:25:34 +03002191bool Program::validateVertexAndFragmentInterfaceBlocks(
2192 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2193 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2194 InfoLog &infoLog) const
2195{
2196 // Check that interface blocks defined in the vertex and fragment shaders are identical
2197 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2198 UniformBlockMap linkedUniformBlocks;
2199
2200 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2201 {
2202 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2203 }
2204
Jamie Madille473dee2015-08-18 14:49:01 -04002205 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002206 {
Jamie Madille473dee2015-08-18 14:49:01 -04002207 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 if (entry != linkedUniformBlocks.end())
2209 {
2210 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2211 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2212 {
2213 return false;
2214 }
2215 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002216 }
2217 return true;
2218}
Jamie Madille473dee2015-08-18 14:49:01 -04002219
Martin Radev4c4c8e72016-08-04 12:25:34 +03002220bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2221{
2222 if (mState.mAttachedComputeShader)
2223 {
2224 const Shader &computeShader = *mState.mAttachedComputeShader;
2225 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2226
2227 if (!validateUniformBlocksCount(
2228 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2229 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2230 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002231 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002232 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002234 return true;
2235 }
2236
2237 const Shader &vertexShader = *mState.mAttachedVertexShader;
2238 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2239
2240 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2241 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2242
2243 if (!validateUniformBlocksCount(
2244 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2245 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2246 {
2247 return false;
2248 }
2249 if (!validateUniformBlocksCount(
2250 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2251 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2252 infoLog))
2253 {
2254
2255 return false;
2256 }
2257 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2258 infoLog))
2259 {
2260 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002261 }
Jamie Madille473dee2015-08-18 14:49:01 -04002262
Geoff Lang7dd2e102014-11-10 15:19:26 -05002263 return true;
2264}
2265
Jamie Madilla2c74982016-12-12 11:20:42 -05002266bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002267 const sh::InterfaceBlock &vertexInterfaceBlock,
2268 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269{
2270 const char* blockName = vertexInterfaceBlock.name.c_str();
2271 // validate blocks for the same member types
2272 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2273 {
Jamie Madillf6113162015-05-07 11:49:21 -04002274 infoLog << "Types for interface block '" << blockName
2275 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002276 return false;
2277 }
2278 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2279 {
Jamie Madillf6113162015-05-07 11:49:21 -04002280 infoLog << "Array sizes differ for interface block '" << blockName
2281 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002282 return false;
2283 }
jchen10af713a22017-04-19 09:10:56 +08002284 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout ||
2285 vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout ||
2286 vertexInterfaceBlock.binding != fragmentInterfaceBlock.binding)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002287 {
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 }
jchen10af713a22017-04-19 09:10:56 +08002790 // Set initial bindings from shader.
2791 for (unsigned int blockIndex = 0; blockIndex < mState.mUniformBlocks.size(); blockIndex++)
2792 {
2793 UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
2794 bindUniformBlock(blockIndex, uniformBlock.binding);
2795 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002796}
2797
Jamie Madill4a3c2342015-10-08 12:58:45 -04002798template <typename VarT>
2799void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2800 const std::string &prefix,
2801 int blockIndex)
2802{
2803 for (const VarT &field : fields)
2804 {
2805 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2806
2807 if (field.isStruct())
2808 {
2809 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2810 {
2811 const std::string uniformElementName =
2812 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2813 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2814 }
2815 }
2816 else
2817 {
2818 // If getBlockMemberInfo returns false, the uniform is optimized out.
2819 sh::BlockMemberInfo memberInfo;
2820 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2821 {
2822 continue;
2823 }
2824
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002825 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002826 blockIndex, memberInfo);
2827
2828 // Since block uniforms have no location, we don't need to store them in the uniform
2829 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002830 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002831 }
2832 }
2833}
2834
Jamie Madill62d31cb2015-09-11 13:25:51 -04002835void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2836{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002837 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002838 size_t blockSize = 0;
2839
Jamie Madill4a3c2342015-10-08 12:58:45 -04002840 // Track the first and last uniform index to determine the range of active uniforms in the
2841 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002842 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002843 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002844 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002845
2846 std::vector<unsigned int> blockUniformIndexes;
2847 for (size_t blockUniformIndex = firstBlockUniformIndex;
2848 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2849 {
2850 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2851 }
jchen10af713a22017-04-19 09:10:56 +08002852 // ESSL 3.10 section 4.4.4 page 58:
2853 // Any uniform or shader storage block declared without a binding qualifier is initially
2854 // assigned to block binding point zero.
2855 int blockBinding = (interfaceBlock.binding == -1 ? 0 : interfaceBlock.binding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 if (interfaceBlock.arraySize > 0)
2857 {
2858 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2859 {
jchen10af713a22017-04-19 09:10:56 +08002860 // Don't define this block at all if it's not active in the implementation.
2861 if (!mProgram->getUniformBlockSize(interfaceBlock.name + ArrayString(arrayElement),
2862 &blockSize))
2863 {
2864 continue;
2865 }
2866 UniformBlock block(interfaceBlock.name, true, arrayElement,
2867 blockBinding + arrayElement);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002868 block.memberUniformIndexes = blockUniformIndexes;
2869
Martin Radev4c4c8e72016-08-04 12:25:34 +03002870 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002871 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002872 case GL_VERTEX_SHADER:
2873 {
2874 block.vertexStaticUse = interfaceBlock.staticUse;
2875 break;
2876 }
2877 case GL_FRAGMENT_SHADER:
2878 {
2879 block.fragmentStaticUse = interfaceBlock.staticUse;
2880 break;
2881 }
2882 case GL_COMPUTE_SHADER:
2883 {
2884 block.computeStaticUse = interfaceBlock.staticUse;
2885 break;
2886 }
2887 default:
2888 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002889 }
2890
Qin Jiajia0350a642016-11-01 17:01:51 +08002891 // Since all block elements in an array share the same active uniforms, they will all be
2892 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2893 // here we will add every block element in the array.
2894 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002895 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002896 }
2897 }
2898 else
2899 {
jchen10af713a22017-04-19 09:10:56 +08002900 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2901 {
2902 return;
2903 }
2904 UniformBlock block(interfaceBlock.name, false, 0, blockBinding);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002905 block.memberUniformIndexes = blockUniformIndexes;
2906
Martin Radev4c4c8e72016-08-04 12:25:34 +03002907 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002909 case GL_VERTEX_SHADER:
2910 {
2911 block.vertexStaticUse = interfaceBlock.staticUse;
2912 break;
2913 }
2914 case GL_FRAGMENT_SHADER:
2915 {
2916 block.fragmentStaticUse = interfaceBlock.staticUse;
2917 break;
2918 }
2919 case GL_COMPUTE_SHADER:
2920 {
2921 block.computeStaticUse = interfaceBlock.staticUse;
2922 break;
2923 }
2924 default:
2925 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926 }
2927
Jamie Madill4a3c2342015-10-08 12:58:45 -04002928 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002929 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002930 }
2931}
2932
Jamie Madille7d84322017-01-10 18:21:59 -05002933template <>
2934void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2935 const uint8_t *destPointer,
2936 GLsizei clampedCount,
2937 const GLint *v)
2938{
2939 // Invalidate the validation cache only if we modify the sampler data.
2940 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2941 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2942 {
2943 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2944 std::vector<GLuint> *boundTextureUnits =
2945 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2946
2947 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2948 mCachedValidateSamplersResult.reset();
2949 }
2950}
2951
2952template <typename T>
2953void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2954 const uint8_t *destPointer,
2955 GLsizei clampedCount,
2956 const T *v)
2957{
2958}
2959
Jamie Madill62d31cb2015-09-11 13:25:51 -04002960template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002961GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002962{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002963 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2964 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002965 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2966
Corentin Wallez15ac5342016-11-03 17:06:39 -04002967 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2968 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2969 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002970 GLsizei maxElementCount =
2971 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2972
2973 GLsizei count = countIn;
2974 GLsizei clampedCount = count * vectorSize;
2975 if (clampedCount > maxElementCount)
2976 {
2977 clampedCount = maxElementCount;
2978 count = maxElementCount / vectorSize;
2979 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002980
Jamie Madill62d31cb2015-09-11 13:25:51 -04002981 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2982 {
2983 // Do a cast conversion for boolean types. From the spec:
2984 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2985 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002986 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002987 {
2988 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2989 }
2990 }
2991 else
2992 {
Jamie Madille7d84322017-01-10 18:21:59 -05002993 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002994 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002995 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002996
2997 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002998}
2999
3000template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003001GLsizei Program::setMatrixUniformInternal(GLint location,
3002 GLsizei count,
3003 GLboolean transpose,
3004 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003005{
3006 if (!transpose)
3007 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003008 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003009 }
3010
3011 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003012 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3013 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003014 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003015
3016 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3017 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3018 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3019 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3020
3021 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003022 {
3023 size_t elementOffset = element * rows * cols;
3024
3025 for (size_t row = 0; row < rows; ++row)
3026 {
3027 for (size_t col = 0; col < cols; ++col)
3028 {
3029 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3030 }
3031 }
3032 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003033
3034 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003035}
3036
3037template <typename DestT>
3038void Program::getUniformInternal(GLint location, DestT *dataOut) const
3039{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003040 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3041 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003042
3043 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3044
3045 GLenum componentType = VariableComponentType(uniform.type);
3046 if (componentType == GLTypeToGLenum<DestT>::value)
3047 {
3048 memcpy(dataOut, srcPointer, uniform.getElementSize());
3049 return;
3050 }
3051
Corentin Wallez6596c462016-03-17 17:26:58 -04003052 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003053
3054 switch (componentType)
3055 {
3056 case GL_INT:
3057 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3058 break;
3059 case GL_UNSIGNED_INT:
3060 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3061 break;
3062 case GL_BOOL:
3063 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3064 break;
3065 case GL_FLOAT:
3066 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3067 break;
3068 default:
3069 UNREACHABLE();
3070 }
3071}
Jamie Madilla4595b82017-01-11 17:36:34 -05003072
3073bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3074{
3075 // Must be called after samplers are validated.
3076 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3077
3078 for (const auto &binding : mState.mSamplerBindings)
3079 {
3080 GLenum textureType = binding.textureType;
3081 for (const auto &unit : binding.boundTextureUnits)
3082 {
3083 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3084 if (programTextureID == textureID)
3085 {
3086 // TODO(jmadill): Check for appropriate overlap.
3087 return true;
3088 }
3089 }
3090 }
3091
3092 return false;
3093}
3094
Jamie Madilla2c74982016-12-12 11:20:42 -05003095} // namespace gl