blob: e3ce80fad135fb47eb0f35221587120ee78caf4d [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 Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.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"
Geoff Lang7dd2e102014-11-10 15:19:26 -050028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029namespace gl
30{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000031
Geoff Lang7dd2e102014-11-10 15:19:26 -050032namespace
33{
34
Jamie Madill62d31cb2015-09-11 13:25:51 -040035void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
36{
37 stream->writeInt(var.type);
38 stream->writeInt(var.precision);
39 stream->writeString(var.name);
40 stream->writeString(var.mappedName);
41 stream->writeInt(var.arraySize);
42 stream->writeInt(var.staticUse);
43 stream->writeString(var.structName);
44 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050045}
46
Jamie Madill62d31cb2015-09-11 13:25:51 -040047void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
48{
49 var->type = stream->readInt<GLenum>();
50 var->precision = stream->readInt<GLenum>();
51 var->name = stream->readString();
52 var->mappedName = stream->readString();
53 var->arraySize = stream->readInt<unsigned int>();
54 var->staticUse = stream->readBool();
55 var->structName = stream->readString();
56}
57
Jamie Madill62d31cb2015-09-11 13:25:51 -040058// This simplified cast function doesn't need to worry about advanced concepts like
59// depth range values, or casting to bool.
60template <typename DestT, typename SrcT>
61DestT UniformStateQueryCast(SrcT value);
62
63// From-Float-To-Integer Casts
64template <>
65GLint UniformStateQueryCast(GLfloat value)
66{
67 return clampCast<GLint>(roundf(value));
68}
69
70template <>
71GLuint UniformStateQueryCast(GLfloat value)
72{
73 return clampCast<GLuint>(roundf(value));
74}
75
76// From-Integer-to-Integer Casts
77template <>
78GLint UniformStateQueryCast(GLuint value)
79{
80 return clampCast<GLint>(value);
81}
82
83template <>
84GLuint UniformStateQueryCast(GLint value)
85{
86 return clampCast<GLuint>(value);
87}
88
89// From-Boolean-to-Anything Casts
90template <>
91GLfloat UniformStateQueryCast(GLboolean value)
92{
93 return (value == GL_TRUE ? 1.0f : 0.0f);
94}
95
96template <>
97GLint UniformStateQueryCast(GLboolean value)
98{
99 return (value == GL_TRUE ? 1 : 0);
100}
101
102template <>
103GLuint UniformStateQueryCast(GLboolean value)
104{
105 return (value == GL_TRUE ? 1u : 0u);
106}
107
108// Default to static_cast
109template <typename DestT, typename SrcT>
110DestT UniformStateQueryCast(SrcT value)
111{
112 return static_cast<DestT>(value);
113}
114
115template <typename SrcT, typename DestT>
116void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
117{
118 for (int comp = 0; comp < components; ++comp)
119 {
120 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
121 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
122 size_t offset = comp * 4;
123 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
124 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
125 }
126}
127
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400128bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
129{
130 for (const LinkedUniform &uniform : list)
131 {
132 if (uniform.name == name)
133 return true;
134 }
135
136 return false;
137}
138
Jamie Madill192745a2016-12-22 15:58:21 -0500139// true if varying x has a higher priority in packing than y
140bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
141{
142 return gl::CompareShaderVar(*x.varying, *y.varying);
143}
144
Jamie Madill62d31cb2015-09-11 13:25:51 -0400145} // anonymous namespace
146
Jamie Madill4a3c2342015-10-08 12:58:45 -0400147const char *const g_fakepath = "C:\\fakepath";
148
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000150{
151}
152
153InfoLog::~InfoLog()
154{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000155}
156
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400157size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000158{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400159 const std::string &logString = mStream.str();
160 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000161}
162
Geoff Lange1a27752015-10-05 13:16:04 -0400163void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000164{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400165 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000166
167 if (bufSize > 0)
168 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400169 const std::string str(mStream.str());
170
171 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000172 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400173 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
174 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000175 }
176
177 infoLog[index] = '\0';
178 }
179
180 if (length)
181 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400182 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000183 }
184}
185
186// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300187// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000188// messages, so lets remove all occurrences of this fake file path from the log.
189void InfoLog::appendSanitized(const char *message)
190{
191 std::string msg(message);
192
193 size_t found;
194 do
195 {
196 found = msg.find(g_fakepath);
197 if (found != std::string::npos)
198 {
199 msg.erase(found, strlen(g_fakepath));
200 }
201 }
202 while (found != std::string::npos);
203
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400204 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000205}
206
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207void InfoLog::reset()
208{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209}
210
Geoff Langd8605522016-04-13 10:19:12 -0400211VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000212{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500213}
214
Geoff Langd8605522016-04-13 10:19:12 -0400215VariableLocation::VariableLocation(const std::string &name,
216 unsigned int element,
217 unsigned int index)
218 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500219{
220}
221
Geoff Langd8605522016-04-13 10:19:12 -0400222void Program::Bindings::bindLocation(GLuint index, const std::string &name)
223{
224 mBindings[name] = index;
225}
226
227int Program::Bindings::getBinding(const std::string &name) const
228{
229 auto iter = mBindings.find(name);
230 return (iter != mBindings.end()) ? iter->second : -1;
231}
232
233Program::Bindings::const_iterator Program::Bindings::begin() const
234{
235 return mBindings.begin();
236}
237
238Program::Bindings::const_iterator Program::Bindings::end() const
239{
240 return mBindings.end();
241}
242
Jamie Madill48ef11b2016-04-27 15:21:52 -0400243ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500244 : mLabel(),
245 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400246 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300247 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500248 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500249 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500250 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400251{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300252 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400253}
254
Jamie Madill48ef11b2016-04-27 15:21:52 -0400255ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400256{
257 if (mAttachedVertexShader != nullptr)
258 {
259 mAttachedVertexShader->release();
260 }
261
262 if (mAttachedFragmentShader != nullptr)
263 {
264 mAttachedFragmentShader->release();
265 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300266
267 if (mAttachedComputeShader != nullptr)
268 {
269 mAttachedComputeShader->release();
270 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400271}
272
Jamie Madill48ef11b2016-04-27 15:21:52 -0400273const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500274{
275 return mLabel;
276}
277
Jamie Madill48ef11b2016-04-27 15:21:52 -0400278const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400279{
280 for (const LinkedUniform &linkedUniform : mUniforms)
281 {
282 if (linkedUniform.name == name)
283 {
284 return &linkedUniform;
285 }
286 }
287
288 return nullptr;
289}
290
Jamie Madill48ef11b2016-04-27 15:21:52 -0400291GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400292{
293 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500294 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400295
296 for (size_t location = 0; location < mUniformLocations.size(); ++location)
297 {
298 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400299 if (!uniformLocation.used)
300 {
301 continue;
302 }
303
304 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400305
306 if (uniform.name == baseName)
307 {
Geoff Langd8605522016-04-13 10:19:12 -0400308 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400309 {
Geoff Langd8605522016-04-13 10:19:12 -0400310 if (uniformLocation.element == subscript ||
311 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
312 {
313 return static_cast<GLint>(location);
314 }
315 }
316 else
317 {
318 if (subscript == GL_INVALID_INDEX)
319 {
320 return static_cast<GLint>(location);
321 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400322 }
323 }
324 }
325
326 return -1;
327}
328
Jamie Madille7d84322017-01-10 18:21:59 -0500329GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400330{
331 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500332 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400333
334 // The app is not allowed to specify array indices other than 0 for arrays of basic types
335 if (subscript != 0 && subscript != GL_INVALID_INDEX)
336 {
337 return GL_INVALID_INDEX;
338 }
339
340 for (size_t index = 0; index < mUniforms.size(); index++)
341 {
342 const LinkedUniform &uniform = mUniforms[index];
343 if (uniform.name == baseName)
344 {
345 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
346 {
347 return static_cast<GLuint>(index);
348 }
349 }
350 }
351
352 return GL_INVALID_INDEX;
353}
354
Jamie Madille7d84322017-01-10 18:21:59 -0500355GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
356{
357 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
358 return mUniformLocations[location].index;
359}
360
361Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
362{
363 GLuint index = getUniformIndexFromLocation(location);
364 if (!isSamplerUniformIndex(index))
365 {
366 return Optional<GLuint>::Invalid();
367 }
368
369 return getSamplerIndexFromUniformIndex(index);
370}
371
372bool ProgramState::isSamplerUniformIndex(GLuint index) const
373{
374 return index >= mSamplerUniformRange.start && index < mSamplerUniformRange.end;
375}
376
377GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
378{
379 ASSERT(isSamplerUniformIndex(uniformIndex));
380 return uniformIndex - mSamplerUniformRange.start;
381}
382
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500383Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400384 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400385 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500386 mLinked(false),
387 mDeleteStatus(false),
388 mRefCount(0),
389 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500390 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500391{
392 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000393
394 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500395 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396}
397
398Program::~Program()
399{
400 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000401
Geoff Lang7dd2e102014-11-10 15:19:26 -0500402 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
Geoff Lang70d0f492015-12-10 17:45:46 -0500405void Program::setLabel(const std::string &label)
406{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400407 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500408}
409
410const std::string &Program::getLabel() const
411{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400412 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500413}
414
Jamie Madillef300b12016-10-07 15:12:09 -0400415void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300417 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300419 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420 {
Jamie Madillef300b12016-10-07 15:12:09 -0400421 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300422 mState.mAttachedVertexShader = shader;
423 mState.mAttachedVertexShader->addRef();
424 break;
425 }
426 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427 {
Jamie Madillef300b12016-10-07 15:12:09 -0400428 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300429 mState.mAttachedFragmentShader = shader;
430 mState.mAttachedFragmentShader->addRef();
431 break;
432 }
433 case GL_COMPUTE_SHADER:
434 {
Jamie Madillef300b12016-10-07 15:12:09 -0400435 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300436 mState.mAttachedComputeShader = shader;
437 mState.mAttachedComputeShader->addRef();
438 break;
439 }
440 default:
441 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443}
444
445bool Program::detachShader(Shader *shader)
446{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300447 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300449 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300451 if (mState.mAttachedVertexShader != shader)
452 {
453 return false;
454 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455
Martin Radev4c4c8e72016-08-04 12:25:34 +0300456 shader->release();
457 mState.mAttachedVertexShader = nullptr;
458 break;
459 }
460 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300462 if (mState.mAttachedFragmentShader != shader)
463 {
464 return false;
465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466
Martin Radev4c4c8e72016-08-04 12:25:34 +0300467 shader->release();
468 mState.mAttachedFragmentShader = nullptr;
469 break;
470 }
471 case GL_COMPUTE_SHADER:
472 {
473 if (mState.mAttachedComputeShader != shader)
474 {
475 return false;
476 }
477
478 shader->release();
479 mState.mAttachedComputeShader = nullptr;
480 break;
481 }
482 default:
483 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486 return true;
487}
488
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000489int Program::getAttachedShadersCount() const
490{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300491 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
492 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000493}
494
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495void Program::bindAttributeLocation(GLuint index, const char *name)
496{
Geoff Langd8605522016-04-13 10:19:12 -0400497 mAttributeBindings.bindLocation(index, name);
498}
499
500void Program::bindUniformLocation(GLuint index, const char *name)
501{
502 // Bind the base uniform name only since array indices other than 0 cannot be bound
503 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504}
505
Sami Väisänen46eaa942016-06-29 10:26:37 +0300506void Program::bindFragmentInputLocation(GLint index, const char *name)
507{
508 mFragmentInputBindings.bindLocation(index, name);
509}
510
511BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
512{
513 BindingInfo ret;
514 ret.type = GL_NONE;
515 ret.valid = false;
516
517 const Shader *fragmentShader = mState.getAttachedFragmentShader();
518 ASSERT(fragmentShader);
519
520 // Find the actual fragment shader varying we're interested in
521 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
522
523 for (const auto &binding : mFragmentInputBindings)
524 {
525 if (binding.second != static_cast<GLuint>(index))
526 continue;
527
528 ret.valid = true;
529
530 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400531 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300532
533 for (const auto &in : inputs)
534 {
535 if (in.name == originalName)
536 {
537 if (in.isArray())
538 {
539 // The client wants to bind either "name" or "name[0]".
540 // GL ES 3.1 spec refers to active array names with language such as:
541 // "if the string identifies the base name of an active array, where the
542 // string would exactly match the name of the variable if the suffix "[0]"
543 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400544 if (arrayIndex == GL_INVALID_INDEX)
545 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300546
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400547 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300548 }
549 else
550 {
551 ret.name = in.mappedName;
552 }
553 ret.type = in.type;
554 return ret;
555 }
556 }
557 }
558
559 return ret;
560}
561
562void Program::pathFragmentInputGen(GLint index,
563 GLenum genMode,
564 GLint components,
565 const GLfloat *coeffs)
566{
567 // If the location is -1 then the command is silently ignored
568 if (index == -1)
569 return;
570
571 const auto &binding = getFragmentInputBindingInfo(index);
572
573 // If the input doesn't exist then then the command is silently ignored
574 // This could happen through optimization for example, the shader translator
575 // decides that a variable is not actually being used and optimizes it away.
576 if (binding.name.empty())
577 return;
578
579 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
580}
581
Martin Radev4c4c8e72016-08-04 12:25:34 +0300582// The attached shaders are checked for linking errors by matching up their variables.
583// Uniform, input and output variables get collected.
584// The code gets compiled into binaries.
Jamie Madill9082b982016-04-27 15:21:51 -0400585Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000586{
587 unlink(false);
588
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000589 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000590 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000591
Martin Radev4c4c8e72016-08-04 12:25:34 +0300592 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500593
Jamie Madill192745a2016-12-22 15:58:21 -0500594 auto vertexShader = mState.mAttachedVertexShader;
595 auto fragmentShader = mState.mAttachedFragmentShader;
596 auto computeShader = mState.mAttachedComputeShader;
597
598 bool isComputeShaderAttached = (computeShader != nullptr);
599 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300600 // Check whether we both have a compute and non-compute shaders attached.
601 // If there are of both types attached, then linking should fail.
602 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
603 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500604 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300605 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
606 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400607 }
608
Jamie Madill192745a2016-12-22 15:58:21 -0500609 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500610 {
Jamie Madill192745a2016-12-22 15:58:21 -0500611 if (!computeShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300612 {
613 mInfoLog << "Attached compute shader is not compiled.";
614 return NoError();
615 }
Jamie Madill192745a2016-12-22 15:58:21 -0500616 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300617
Jamie Madill192745a2016-12-22 15:58:21 -0500618 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300619
620 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
621 // If the work group size is not specified, a link time error should occur.
622 if (!mState.mComputeShaderLocalSize.isDeclared())
623 {
624 mInfoLog << "Work group size is not specified.";
625 return NoError();
626 }
627
628 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
629 {
630 return NoError();
631 }
632
633 if (!linkUniformBlocks(mInfoLog, caps))
634 {
635 return NoError();
636 }
637
Jamie Madill192745a2016-12-22 15:58:21 -0500638 gl::VaryingPacking noVaryingPacking(0, PackMode::ANGLE_RELAXED);
639 ANGLE_TRY_RESULT(mProgram->link(data, noVaryingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500640 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500642 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300643 }
644 }
645 else
646 {
Jamie Madill192745a2016-12-22 15:58:21 -0500647 if (!fragmentShader || !fragmentShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648 {
649 return NoError();
650 }
Jamie Madill192745a2016-12-22 15:58:21 -0500651 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300652
Jamie Madill192745a2016-12-22 15:58:21 -0500653 if (!vertexShader || !vertexShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300654 {
655 return NoError();
656 }
Jamie Madill192745a2016-12-22 15:58:21 -0500657 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658
Jamie Madill192745a2016-12-22 15:58:21 -0500659 if (fragmentShader->getShaderVersion() != vertexShader->getShaderVersion())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660 {
661 mInfoLog << "Fragment shader version does not match vertex shader version.";
662 return NoError();
663 }
664
Jamie Madilleb979bf2016-11-15 12:28:46 -0500665 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300666 {
667 return NoError();
668 }
669
Jamie Madill192745a2016-12-22 15:58:21 -0500670 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300671 {
672 return NoError();
673 }
674
675 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
676 {
677 return NoError();
678 }
679
680 if (!linkUniformBlocks(mInfoLog, caps))
681 {
682 return NoError();
683 }
684
685 const auto &mergedVaryings = getMergedVaryings();
686
687 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
688 {
689 return NoError();
690 }
691
692 linkOutputVariables();
693
Jamie Madill192745a2016-12-22 15:58:21 -0500694 // Validate we can pack the varyings.
695 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
696
697 // Map the varyings to the register file
698 // In WebGL, we use a slightly different handling for packing variables.
699 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
700 : PackMode::ANGLE_RELAXED;
701 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
702 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
703 mState.getTransformFeedbackVaryingNames()))
704 {
705 return NoError();
706 }
707
708 ANGLE_TRY_RESULT(mProgram->link(data, varyingPacking, mInfoLog), mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500709 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300710 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500711 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300712 }
713
714 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500715 }
716
Jamie Madill4a3c2342015-10-08 12:58:45 -0400717 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400718
Martin Radev4c4c8e72016-08-04 12:25:34 +0300719 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000720}
721
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000722// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723void Program::unlink(bool destroy)
724{
725 if (destroy) // Object being destructed
726 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400727 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400729 mState.mAttachedFragmentShader->release();
730 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000731 }
732
Jamie Madill48ef11b2016-04-27 15:21:52 -0400733 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400735 mState.mAttachedVertexShader->release();
736 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300738
739 if (mState.mAttachedComputeShader)
740 {
741 mState.mAttachedComputeShader->release();
742 mState.mAttachedComputeShader = nullptr;
743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744 }
745
Jamie Madill48ef11b2016-04-27 15:21:52 -0400746 mState.mAttributes.clear();
747 mState.mActiveAttribLocationsMask.reset();
748 mState.mTransformFeedbackVaryingVars.clear();
749 mState.mUniforms.clear();
750 mState.mUniformLocations.clear();
751 mState.mUniformBlocks.clear();
752 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300753 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500754 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500755
Geoff Lang7dd2e102014-11-10 15:19:26 -0500756 mValidated = false;
757
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000758 mLinked = false;
759}
760
Geoff Lange1a27752015-10-05 13:16:04 -0400761bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000762{
763 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764}
765
Jamie Madilla2c74982016-12-12 11:20:42 -0500766Error Program::loadBinary(const Context *context,
767 GLenum binaryFormat,
768 const void *binary,
769 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000770{
771 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000772
Geoff Lang7dd2e102014-11-10 15:19:26 -0500773#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800774 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500775#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400776 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
777 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000778 {
Jamie Madillf6113162015-05-07 11:49:21 -0400779 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800780 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500781 }
782
Geoff Langc46cc2f2015-10-01 17:16:20 -0400783 BinaryInputStream stream(binary, length);
784
Jamie Madilla2c74982016-12-12 11:20:42 -0500785 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
786 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
787 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
788 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500789 {
Jamie Madillf6113162015-05-07 11:49:21 -0400790 mInfoLog << "Invalid program binary version.";
He Yunchaoacd18982017-01-04 10:46:42 +0800791 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500792 }
793
Jamie Madilla2c74982016-12-12 11:20:42 -0500794 int majorVersion = stream.readInt<int>();
795 int minorVersion = stream.readInt<int>();
796 if (majorVersion != context->getClientMajorVersion() ||
797 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500798 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500799 mInfoLog << "Cannot load program binaries across different ES context versions.";
He Yunchaoacd18982017-01-04 10:46:42 +0800800 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500801 }
802
Martin Radev4c4c8e72016-08-04 12:25:34 +0300803 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
804 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
805 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
806
Jamie Madill63805b42015-08-25 13:17:39 -0400807 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
808 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400809 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500810
Jamie Madill3da79b72015-04-27 11:09:17 -0400811 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400812 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400813 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
814 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400815 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 LoadShaderVar(&stream, &attrib);
817 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400818 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400819 }
820
Jamie Madill62d31cb2015-09-11 13:25:51 -0400821 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400822 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400823 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
824 {
825 LinkedUniform uniform;
826 LoadShaderVar(&stream, &uniform);
827
828 uniform.blockIndex = stream.readInt<int>();
829 uniform.blockInfo.offset = stream.readInt<int>();
830 uniform.blockInfo.arrayStride = stream.readInt<int>();
831 uniform.blockInfo.matrixStride = stream.readInt<int>();
832 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
833
Jamie Madill48ef11b2016-04-27 15:21:52 -0400834 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400835 }
836
837 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400838 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400839 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
840 uniformIndexIndex++)
841 {
842 VariableLocation variable;
843 stream.readString(&variable.name);
844 stream.readInt(&variable.element);
845 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400846 stream.readBool(&variable.used);
847 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400848
Jamie Madill48ef11b2016-04-27 15:21:52 -0400849 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400850 }
851
852 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400853 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400854 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
855 ++uniformBlockIndex)
856 {
857 UniformBlock uniformBlock;
858 stream.readString(&uniformBlock.name);
859 stream.readBool(&uniformBlock.isArray);
860 stream.readInt(&uniformBlock.arrayElement);
861 stream.readInt(&uniformBlock.dataSize);
862 stream.readBool(&uniformBlock.vertexStaticUse);
863 stream.readBool(&uniformBlock.fragmentStaticUse);
864
865 unsigned int numMembers = stream.readInt<unsigned int>();
866 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
867 {
868 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
869 }
870
Jamie Madill48ef11b2016-04-27 15:21:52 -0400871 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400872 }
873
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500874 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
875 ++bindingIndex)
876 {
877 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
878 mState.mActiveUniformBlockBindings.set(bindingIndex,
879 mState.mUniformBlockBindings[bindingIndex] != 0);
880 }
881
Brandon Jones1048ea72015-10-06 15:34:52 -0700882 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400883 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700884 for (unsigned int transformFeedbackVaryingIndex = 0;
885 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
886 ++transformFeedbackVaryingIndex)
887 {
888 sh::Varying varying;
889 stream.readInt(&varying.arraySize);
890 stream.readInt(&varying.type);
891 stream.readString(&varying.name);
892
Jamie Madill48ef11b2016-04-27 15:21:52 -0400893 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700894 }
895
Jamie Madill48ef11b2016-04-27 15:21:52 -0400896 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400897
Jamie Madill80a6fc02015-08-21 16:53:16 -0400898 unsigned int outputVarCount = stream.readInt<unsigned int>();
899 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
900 {
901 int locationIndex = stream.readInt<int>();
902 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400903 stream.readInt(&locationData.element);
904 stream.readInt(&locationData.index);
905 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400906 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400907 }
908
Jamie Madille7d84322017-01-10 18:21:59 -0500909 stream.readInt(&mState.mSamplerUniformRange.start);
910 stream.readInt(&mState.mSamplerUniformRange.end);
911
912 unsigned int samplerCount = stream.readInt<unsigned int>();
913 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
914 {
915 GLenum textureType = stream.readInt<GLenum>();
916 size_t bindingCount = stream.readInt<size_t>();
917 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
918 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400919
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500920 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000921
Jamie Madillb0a838b2016-11-13 20:02:12 -0500922 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500923#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500924}
925
Jamie Madilla2c74982016-12-12 11:20:42 -0500926Error Program::saveBinary(const Context *context,
927 GLenum *binaryFormat,
928 void *binary,
929 GLsizei bufSize,
930 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500931{
932 if (binaryFormat)
933 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400934 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500935 }
936
937 BinaryOutputStream stream;
938
Geoff Lang7dd2e102014-11-10 15:19:26 -0500939 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
940
Jamie Madilla2c74982016-12-12 11:20:42 -0500941 // nullptr context is supported when computing binary length.
942 if (context)
943 {
944 stream.writeInt(context->getClientVersion().major);
945 stream.writeInt(context->getClientVersion().minor);
946 }
947 else
948 {
949 stream.writeInt(2);
950 stream.writeInt(0);
951 }
952
Martin Radev4c4c8e72016-08-04 12:25:34 +0300953 stream.writeInt(mState.mComputeShaderLocalSize[0]);
954 stream.writeInt(mState.mComputeShaderLocalSize[1]);
955 stream.writeInt(mState.mComputeShaderLocalSize[2]);
956
Jamie Madill48ef11b2016-04-27 15:21:52 -0400957 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500958
Jamie Madill48ef11b2016-04-27 15:21:52 -0400959 stream.writeInt(mState.mAttributes.size());
960 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400961 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400962 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400963 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400964 }
965
Jamie Madill48ef11b2016-04-27 15:21:52 -0400966 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500967 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400968 {
969 WriteShaderVar(&stream, uniform);
970
971 // FIXME: referenced
972
973 stream.writeInt(uniform.blockIndex);
974 stream.writeInt(uniform.blockInfo.offset);
975 stream.writeInt(uniform.blockInfo.arrayStride);
976 stream.writeInt(uniform.blockInfo.matrixStride);
977 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
978 }
979
Jamie Madill48ef11b2016-04-27 15:21:52 -0400980 stream.writeInt(mState.mUniformLocations.size());
981 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400982 {
983 stream.writeString(variable.name);
984 stream.writeInt(variable.element);
985 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400986 stream.writeInt(variable.used);
987 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400988 }
989
Jamie Madill48ef11b2016-04-27 15:21:52 -0400990 stream.writeInt(mState.mUniformBlocks.size());
991 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400992 {
993 stream.writeString(uniformBlock.name);
994 stream.writeInt(uniformBlock.isArray);
995 stream.writeInt(uniformBlock.arrayElement);
996 stream.writeInt(uniformBlock.dataSize);
997
998 stream.writeInt(uniformBlock.vertexStaticUse);
999 stream.writeInt(uniformBlock.fragmentStaticUse);
1000
1001 stream.writeInt(uniformBlock.memberUniformIndexes.size());
1002 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
1003 {
1004 stream.writeInt(memberUniformIndex);
1005 }
Jamie Madill3da79b72015-04-27 11:09:17 -04001006 }
1007
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001008 for (GLuint binding : mState.mUniformBlockBindings)
1009 {
1010 stream.writeInt(binding);
1011 }
1012
Jamie Madill48ef11b2016-04-27 15:21:52 -04001013 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
1014 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -07001015 {
1016 stream.writeInt(varying.arraySize);
1017 stream.writeInt(varying.type);
1018 stream.writeString(varying.name);
1019 }
1020
Jamie Madill48ef11b2016-04-27 15:21:52 -04001021 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001022
Jamie Madill48ef11b2016-04-27 15:21:52 -04001023 stream.writeInt(mState.mOutputVariables.size());
1024 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -04001025 {
1026 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -04001027 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001028 stream.writeInt(outputPair.second.index);
1029 stream.writeString(outputPair.second.name);
1030 }
1031
Jamie Madille7d84322017-01-10 18:21:59 -05001032 stream.writeInt(mState.mSamplerUniformRange.start);
1033 stream.writeInt(mState.mSamplerUniformRange.end);
1034
1035 stream.writeInt(mState.mSamplerBindings.size());
1036 for (const auto &samplerBinding : mState.mSamplerBindings)
1037 {
1038 stream.writeInt(samplerBinding.textureType);
1039 stream.writeInt(samplerBinding.boundTextureUnits.size());
1040 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001041
Jamie Madilla2c74982016-12-12 11:20:42 -05001042 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001043
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001044 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001045 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001046
1047 if (streamLength > bufSize)
1048 {
1049 if (length)
1050 {
1051 *length = 0;
1052 }
1053
1054 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1055 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1056 // sizes and then copy it.
1057 return Error(GL_INVALID_OPERATION);
1058 }
1059
1060 if (binary)
1061 {
1062 char *ptr = reinterpret_cast<char*>(binary);
1063
Jamie Madill48ef11b2016-04-27 15:21:52 -04001064 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001065 ptr += streamLength;
1066
1067 ASSERT(ptr - streamLength == binary);
1068 }
1069
1070 if (length)
1071 {
1072 *length = streamLength;
1073 }
1074
He Yunchaoacd18982017-01-04 10:46:42 +08001075 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076}
1077
1078GLint Program::getBinaryLength() const
1079{
1080 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001081 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001082 if (error.isError())
1083 {
1084 return 0;
1085 }
1086
1087 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001088}
1089
Geoff Langc5629752015-12-07 16:29:04 -05001090void Program::setBinaryRetrievableHint(bool retrievable)
1091{
1092 // TODO(jmadill) : replace with dirty bits
1093 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001094 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001095}
1096
1097bool Program::getBinaryRetrievableHint() const
1098{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001099 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001100}
1101
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001102void Program::release()
1103{
1104 mRefCount--;
1105
1106 if (mRefCount == 0 && mDeleteStatus)
1107 {
1108 mResourceManager->deleteProgram(mHandle);
1109 }
1110}
1111
1112void Program::addRef()
1113{
1114 mRefCount++;
1115}
1116
1117unsigned int Program::getRefCount() const
1118{
1119 return mRefCount;
1120}
1121
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001122int Program::getInfoLogLength() const
1123{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001124 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001125}
1126
Geoff Lange1a27752015-10-05 13:16:04 -04001127void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001128{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001129 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001130}
1131
Geoff Lange1a27752015-10-05 13:16:04 -04001132void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001133{
1134 int total = 0;
1135
Martin Radev4c4c8e72016-08-04 12:25:34 +03001136 if (mState.mAttachedComputeShader)
1137 {
1138 if (total < maxCount)
1139 {
1140 shaders[total] = mState.mAttachedComputeShader->getHandle();
1141 total++;
1142 }
1143 }
1144
Jamie Madill48ef11b2016-04-27 15:21:52 -04001145 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001146 {
1147 if (total < maxCount)
1148 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001149 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001150 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001151 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001152 }
1153
Jamie Madill48ef11b2016-04-27 15:21:52 -04001154 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001155 {
1156 if (total < maxCount)
1157 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001158 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001159 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001160 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001161 }
1162
1163 if (count)
1164 {
1165 *count = total;
1166 }
1167}
1168
Geoff Lange1a27752015-10-05 13:16:04 -04001169GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001170{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001171 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001172 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001173 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001174 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001175 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001176 }
1177 }
1178
Austin Kinrossb8af7232015-03-16 22:33:25 -07001179 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180}
1181
Jamie Madill63805b42015-08-25 13:17:39 -04001182bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001183{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001184 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1185 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186}
1187
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001188void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1189{
Jamie Madillc349ec02015-08-21 16:53:12 -04001190 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001191 {
1192 if (bufsize > 0)
1193 {
1194 name[0] = '\0';
1195 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001196
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001197 if (length)
1198 {
1199 *length = 0;
1200 }
1201
1202 *type = GL_NONE;
1203 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001204 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001205 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001206
1207 size_t attributeIndex = 0;
1208
Jamie Madill48ef11b2016-04-27 15:21:52 -04001209 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001210 {
1211 // Skip over inactive attributes
1212 if (attribute.staticUse)
1213 {
1214 if (static_cast<size_t>(index) == attributeIndex)
1215 {
1216 break;
1217 }
1218 attributeIndex++;
1219 }
1220 }
1221
Jamie Madill48ef11b2016-04-27 15:21:52 -04001222 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1223 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001224
1225 if (bufsize > 0)
1226 {
1227 const char *string = attrib.name.c_str();
1228
1229 strncpy(name, string, bufsize);
1230 name[bufsize - 1] = '\0';
1231
1232 if (length)
1233 {
1234 *length = static_cast<GLsizei>(strlen(name));
1235 }
1236 }
1237
1238 // Always a single 'type' instance
1239 *size = 1;
1240 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001241}
1242
Geoff Lange1a27752015-10-05 13:16:04 -04001243GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001244{
Jamie Madillc349ec02015-08-21 16:53:12 -04001245 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001246 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001247 return 0;
1248 }
1249
1250 GLint count = 0;
1251
Jamie Madill48ef11b2016-04-27 15:21:52 -04001252 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001253 {
1254 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001255 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001256
1257 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001258}
1259
Geoff Lange1a27752015-10-05 13:16:04 -04001260GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001261{
Jamie Madillc349ec02015-08-21 16:53:12 -04001262 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001263 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001264 return 0;
1265 }
1266
1267 size_t maxLength = 0;
1268
Jamie Madill48ef11b2016-04-27 15:21:52 -04001269 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001270 {
1271 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001273 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001275 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001276
Jamie Madillc349ec02015-08-21 16:53:12 -04001277 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001278}
1279
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280GLint Program::getFragDataLocation(const std::string &name) const
1281{
1282 std::string baseName(name);
1283 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001284 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001285 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001286 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1288 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001289 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001291 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001293}
1294
Geoff Lange1a27752015-10-05 13:16:04 -04001295void Program::getActiveUniform(GLuint index,
1296 GLsizei bufsize,
1297 GLsizei *length,
1298 GLint *size,
1299 GLenum *type,
1300 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001301{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001303 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001304 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001305 ASSERT(index < mState.mUniforms.size());
1306 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001307
1308 if (bufsize > 0)
1309 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001310 std::string string = uniform.name;
1311 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001312 {
1313 string += "[0]";
1314 }
1315
1316 strncpy(name, string.c_str(), bufsize);
1317 name[bufsize - 1] = '\0';
1318
1319 if (length)
1320 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001321 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001322 }
1323 }
1324
Jamie Madill62d31cb2015-09-11 13:25:51 -04001325 *size = uniform.elementCount();
1326 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001327 }
1328 else
1329 {
1330 if (bufsize > 0)
1331 {
1332 name[0] = '\0';
1333 }
1334
1335 if (length)
1336 {
1337 *length = 0;
1338 }
1339
1340 *size = 0;
1341 *type = GL_NONE;
1342 }
1343}
1344
Geoff Lange1a27752015-10-05 13:16:04 -04001345GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001346{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001348 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001349 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001350 }
1351 else
1352 {
1353 return 0;
1354 }
1355}
1356
Geoff Lange1a27752015-10-05 13:16:04 -04001357GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001358{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001359 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001360
1361 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001362 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001363 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001365 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001366 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001367 size_t length = uniform.name.length() + 1u;
1368 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001369 {
1370 length += 3; // Counting in "[0]".
1371 }
1372 maxLength = std::max(length, maxLength);
1373 }
1374 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001375 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376
Jamie Madill62d31cb2015-09-11 13:25:51 -04001377 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378}
1379
1380GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1381{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001382 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001383 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001385 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1387 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1388 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1389 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1390 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1391 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1392 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1393 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1394 default:
1395 UNREACHABLE();
1396 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001397 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398 return 0;
1399}
1400
1401bool Program::isValidUniformLocation(GLint location) const
1402{
Jamie Madille2e406c2016-06-02 13:04:10 -04001403 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001404 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1405 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001406}
1407
Jamie Madill62d31cb2015-09-11 13:25:51 -04001408const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001410 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001411 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412}
1413
Jamie Madillac4e9c32017-01-13 14:07:12 -05001414const VariableLocation &Program::getUniformLocation(GLint location) const
1415{
1416 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1417 return mState.mUniformLocations[location];
1418}
1419
1420const std::vector<VariableLocation> &Program::getUniformLocations() const
1421{
1422 return mState.mUniformLocations;
1423}
1424
1425const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1426{
1427 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1428 return mState.mUniforms[index];
1429}
1430
Jamie Madill62d31cb2015-09-11 13:25:51 -04001431GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001433 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434}
1435
Jamie Madill62d31cb2015-09-11 13:25:51 -04001436GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437{
Jamie Madille7d84322017-01-10 18:21:59 -05001438 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439}
1440
1441void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1442{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001443 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1444 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445}
1446
1447void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1448{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001449 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1450 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451}
1452
1453void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1454{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001455 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1456 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001457}
1458
1459void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1460{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001461 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1462 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463}
1464
1465void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1466{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001467 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1468 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469}
1470
1471void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1472{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001473 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1474 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475}
1476
1477void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1478{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001479 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1480 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481}
1482
1483void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1484{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001485 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1486 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487}
1488
1489void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1490{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001491 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1492 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001493}
1494
1495void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1496{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001497 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1498 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499}
1500
1501void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1502{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001503 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1504 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001505}
1506
1507void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1508{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001509 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1510 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511}
1512
1513void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1514{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001515 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1516 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517}
1518
1519void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1520{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001521 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1522 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523}
1524
1525void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1526{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001527 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1528 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001529}
1530
1531void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1532{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001533 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1534 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535}
1536
1537void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1538{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001539 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1540 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001541}
1542
1543void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1544{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001545 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1546 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001547}
1548
1549void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1550{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001551 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1552 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001553}
1554
1555void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1556{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001557 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1558 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001559}
1560
1561void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1562{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001563 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1564 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001565}
1566
Geoff Lange1a27752015-10-05 13:16:04 -04001567void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001569 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001570}
1571
Geoff Lange1a27752015-10-05 13:16:04 -04001572void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001573{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001574 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001575}
1576
Geoff Lange1a27752015-10-05 13:16:04 -04001577void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001578{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001579 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580}
1581
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582void Program::flagForDeletion()
1583{
1584 mDeleteStatus = true;
1585}
1586
1587bool Program::isFlaggedForDeletion() const
1588{
1589 return mDeleteStatus;
1590}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001591
Brandon Jones43a53e22014-08-28 16:23:22 -07001592void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001593{
1594 mInfoLog.reset();
1595
Geoff Lang7dd2e102014-11-10 15:19:26 -05001596 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001597 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001598 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001599 }
1600 else
1601 {
Jamie Madillf6113162015-05-07 11:49:21 -04001602 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001603 }
1604}
1605
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1607{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001608 // Skip cache if we're using an infolog, so we get the full error.
1609 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1610 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1611 {
1612 return mCachedValidateSamplersResult.value();
1613 }
1614
1615 if (mTextureUnitTypesCache.empty())
1616 {
1617 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1618 }
1619 else
1620 {
1621 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1622 }
1623
1624 // if any two active samplers in a program are of different types, but refer to the same
1625 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1626 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001627 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001628 {
Jamie Madille7d84322017-01-10 18:21:59 -05001629 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001630
Jamie Madille7d84322017-01-10 18:21:59 -05001631 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001632 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001633 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1634 {
1635 if (infoLog)
1636 {
1637 (*infoLog) << "Sampler uniform (" << textureUnit
1638 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1639 << caps.maxCombinedTextureImageUnits << ")";
1640 }
1641
1642 mCachedValidateSamplersResult = false;
1643 return false;
1644 }
1645
1646 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1647 {
1648 if (textureType != mTextureUnitTypesCache[textureUnit])
1649 {
1650 if (infoLog)
1651 {
1652 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1653 "image unit ("
1654 << textureUnit << ").";
1655 }
1656
1657 mCachedValidateSamplersResult = false;
1658 return false;
1659 }
1660 }
1661 else
1662 {
1663 mTextureUnitTypesCache[textureUnit] = textureType;
1664 }
1665 }
1666 }
1667
1668 mCachedValidateSamplersResult = true;
1669 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670}
1671
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001672bool Program::isValidated() const
1673{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001674 return mValidated;
1675}
1676
Geoff Lange1a27752015-10-05 13:16:04 -04001677GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001678{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001679 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680}
1681
1682void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1683{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001684 ASSERT(
1685 uniformBlockIndex <
1686 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687
Jamie Madill48ef11b2016-04-27 15:21:52 -04001688 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689
1690 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001691 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001692 std::string string = uniformBlock.name;
1693
Jamie Madill62d31cb2015-09-11 13:25:51 -04001694 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001696 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697 }
1698
1699 strncpy(uniformBlockName, string.c_str(), bufSize);
1700 uniformBlockName[bufSize - 1] = '\0';
1701
1702 if (length)
1703 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001704 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001705 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001706 }
1707}
1708
Geoff Lange1a27752015-10-05 13:16:04 -04001709GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001710{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001711 int maxLength = 0;
1712
1713 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001714 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001715 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001716 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1717 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001718 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001719 if (!uniformBlock.name.empty())
1720 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001721 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722
1723 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001724 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001725
1726 maxLength = std::max(length + arrayLength, maxLength);
1727 }
1728 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001729 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730
1731 return maxLength;
1732}
1733
Geoff Lange1a27752015-10-05 13:16:04 -04001734GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001736 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001737 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001738
Jamie Madill48ef11b2016-04-27 15:21:52 -04001739 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001740 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1741 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001742 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001743 if (uniformBlock.name == baseName)
1744 {
1745 const bool arrayElementZero =
1746 (subscript == GL_INVALID_INDEX &&
1747 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1748 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1749 {
1750 return blockIndex;
1751 }
1752 }
1753 }
1754
1755 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001756}
1757
Jamie Madill62d31cb2015-09-11 13:25:51 -04001758const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001759{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001760 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1761 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001762}
1763
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001764void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1765{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001766 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001767 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001768 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001769}
1770
1771GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1772{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001773 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001774}
1775
1776void Program::resetUniformBlockBindings()
1777{
1778 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1779 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001780 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001781 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001782 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001783}
1784
Geoff Lang48dcae72014-02-05 16:28:24 -05001785void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1786{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001787 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001788 for (GLsizei i = 0; i < count; i++)
1789 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001790 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001791 }
1792
Jamie Madill48ef11b2016-04-27 15:21:52 -04001793 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001794}
1795
1796void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1797{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001799 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001800 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1801 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001802 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1803 if (length)
1804 {
1805 *length = lastNameIdx;
1806 }
1807 if (size)
1808 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001809 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001810 }
1811 if (type)
1812 {
1813 *type = varying.type;
1814 }
1815 if (name)
1816 {
1817 memcpy(name, varying.name.c_str(), lastNameIdx);
1818 name[lastNameIdx] = '\0';
1819 }
1820 }
1821}
1822
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001823GLsizei Program::getTransformFeedbackVaryingCount() const
1824{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001826 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001827 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001828 }
1829 else
1830 {
1831 return 0;
1832 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001833}
1834
1835GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1836{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001838 {
1839 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001840 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001841 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001842 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1843 }
1844
1845 return maxSize;
1846 }
1847 else
1848 {
1849 return 0;
1850 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001851}
1852
1853GLenum Program::getTransformFeedbackBufferMode() const
1854{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001855 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856}
1857
Jamie Madill192745a2016-12-22 15:58:21 -05001858bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859{
Jamie Madill192745a2016-12-22 15:58:21 -05001860 const Shader *vertexShader = mState.mAttachedVertexShader;
1861 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1862
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001863 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1864
Jamie Madill4cff2472015-08-21 16:53:18 -04001865 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1866 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867
Sami Väisänen46eaa942016-06-29 10:26:37 +03001868 std::map<GLuint, std::string> staticFragmentInputLocations;
1869
Jamie Madill4cff2472015-08-21 16:53:18 -04001870 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001872 bool matched = false;
1873
1874 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001875 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001876 {
1877 continue;
1878 }
1879
Jamie Madill4cff2472015-08-21 16:53:18 -04001880 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001882 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001884 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001885 if (!linkValidateVaryings(infoLog, output.name, input, output,
1886 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001887 {
1888 return false;
1889 }
1890
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891 matched = true;
1892 break;
1893 }
1894 }
1895
1896 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001897 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001899 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 return false;
1901 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001902
1903 // Check for aliased path rendering input bindings (if any).
1904 // If more than one binding refer statically to the same
1905 // location the link must fail.
1906
1907 if (!output.staticUse)
1908 continue;
1909
1910 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1911 if (inputBinding == -1)
1912 continue;
1913
1914 const auto it = staticFragmentInputLocations.find(inputBinding);
1915 if (it == std::end(staticFragmentInputLocations))
1916 {
1917 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1918 }
1919 else
1920 {
1921 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1922 << it->second;
1923 return false;
1924 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001925 }
1926
Jamie Madillada9ecc2015-08-17 12:53:37 -04001927 // TODO(jmadill): verify no unmatched vertex varyings?
1928
Geoff Lang7dd2e102014-11-10 15:19:26 -05001929 return true;
1930}
1931
Martin Radev4c4c8e72016-08-04 12:25:34 +03001932bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001933{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001934 // Check that uniforms defined in the vertex and fragment shaders are identical
1935 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001936 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1937 const std::vector<sh::Uniform> &fragmentUniforms =
1938 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001939
Jamie Madillea918db2015-08-18 14:48:59 -04001940 for (const sh::Uniform &vertexUniform : vertexUniforms)
1941 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001943 }
1944
1945 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1946 {
1947 auto entry = linkedUniforms.find(fragmentUniform.name);
1948 if (entry != linkedUniforms.end())
1949 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001950 LinkedUniform *vertexUniform = &entry->second;
1951 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1952 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001953 {
1954 return false;
1955 }
1956 }
1957 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001958 return true;
1959}
1960
Jamie Madilla2c74982016-12-12 11:20:42 -05001961bool Program::linkUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001962{
1963 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1964 {
1965 ASSERT(mState.mAttachedComputeShader == nullptr);
1966 if (!validateVertexAndFragmentUniforms(infoLog))
1967 {
1968 return false;
1969 }
1970 }
Jamie Madillea918db2015-08-18 14:48:59 -04001971
Jamie Madill62d31cb2015-09-11 13:25:51 -04001972 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1973 // Also check the maximum uniform vector and sampler counts.
1974 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1975 {
1976 return false;
1977 }
1978
Geoff Langd8605522016-04-13 10:19:12 -04001979 if (!indexUniforms(infoLog, caps, uniformBindings))
1980 {
1981 return false;
1982 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001983
Jamie Madillea918db2015-08-18 14:48:59 -04001984 return true;
1985}
1986
Jamie Madilla2c74982016-12-12 11:20:42 -05001987bool Program::indexUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001988{
Geoff Langd8605522016-04-13 10:19:12 -04001989 // Uniforms awaiting a location
1990 std::vector<VariableLocation> unboundUniforms;
1991 std::map<GLuint, VariableLocation> boundUniforms;
1992 int maxUniformLocation = -1;
1993
1994 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001995 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001996 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001997 const LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001998
Geoff Langd8605522016-04-13 10:19:12 -04001999 if (uniform.isBuiltIn())
2000 {
2001 continue;
2002 }
2003
2004 int bindingLocation = uniformBindings.getBinding(uniform.name);
2005
2006 // Verify that this location isn't bound twice
2007 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
2008 {
2009 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
2010 return false;
2011 }
2012
Jamie Madill62d31cb2015-09-11 13:25:51 -04002013 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
2014 {
Geoff Langd8605522016-04-13 10:19:12 -04002015 VariableLocation location(uniform.name, arrayIndex,
2016 static_cast<unsigned int>(uniformIndex));
2017
2018 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 {
Geoff Langd8605522016-04-13 10:19:12 -04002020 boundUniforms[bindingLocation] = location;
2021 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
2022 }
2023 else
2024 {
2025 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002026 }
2027 }
2028 }
Geoff Langd8605522016-04-13 10:19:12 -04002029
2030 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
2031 // not be assigned to those locations.
2032 std::set<GLuint> reservedLocations;
2033 for (const auto &binding : uniformBindings)
2034 {
2035 GLuint location = binding.second;
2036 if (boundUniforms.find(location) == boundUniforms.end())
2037 {
2038 reservedLocations.insert(location);
2039 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
2040 }
2041 }
2042
2043 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04002044 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04002045 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
2046 static_cast<size_t>(maxUniformLocation + 1)));
2047
2048 // Assign bound uniforms
2049 for (const auto &boundUniform : boundUniforms)
2050 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002051 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002052 }
2053
2054 // Assign reserved uniforms
2055 for (const auto &reservedLocation : reservedLocations)
2056 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002057 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002058 }
2059
2060 // Assign unbound uniforms
2061 size_t nextUniformLocation = 0;
2062 for (const auto &unboundUniform : unboundUniforms)
2063 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002064 while (mState.mUniformLocations[nextUniformLocation].used ||
2065 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002066 {
2067 nextUniformLocation++;
2068 }
2069
Jamie Madill48ef11b2016-04-27 15:21:52 -04002070 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
2071 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002072 nextUniformLocation++;
2073 }
2074
2075 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002076}
2077
Martin Radev4c4c8e72016-08-04 12:25:34 +03002078bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2079 const std::string &uniformName,
2080 const sh::InterfaceBlockField &vertexUniform,
2081 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082{
Jamie Madillc4c744222015-11-04 09:39:47 -05002083 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2084 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 {
2086 return false;
2087 }
2088
2089 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2090 {
Jamie Madillf6113162015-05-07 11:49:21 -04002091 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 return false;
2093 }
2094
2095 return true;
2096}
2097
Jamie Madilleb979bf2016-11-15 12:28:46 -05002098// Assigns locations to all attributes from the bindings and program locations.
2099bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002101 const auto *vertexShader = mState.getAttachedVertexShader();
2102
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002104 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002105 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002106
2107 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002108 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002109 {
Jamie Madillf6113162015-05-07 11:49:21 -04002110 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002111 return false;
2112 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002114 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002115
Jamie Madillc349ec02015-08-21 16:53:12 -04002116 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002117 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 {
2119 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 ASSERT(attribute.staticUse);
2121
Jamie Madilleb979bf2016-11-15 12:28:46 -05002122 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002123 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002124 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002125 attribute.location = bindingLocation;
2126 }
2127
2128 if (attribute.location != -1)
2129 {
2130 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002131 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132
Jamie Madill63805b42015-08-25 13:17:39 -04002133 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 {
Jamie Madillf6113162015-05-07 11:49:21 -04002135 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002136 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137
2138 return false;
2139 }
2140
Jamie Madill63805b42015-08-25 13:17:39 -04002141 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142 {
Jamie Madill63805b42015-08-25 13:17:39 -04002143 const int regLocation = attribute.location + reg;
2144 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145
2146 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002147 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002148 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002150 // TODO(jmadill): fix aliasing on ES2
2151 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002153 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002154 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155 return false;
2156 }
2157 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002158 else
2159 {
Jamie Madill63805b42015-08-25 13:17:39 -04002160 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002161 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162
Jamie Madill63805b42015-08-25 13:17:39 -04002163 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164 }
2165 }
2166 }
2167
2168 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002169 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171 ASSERT(attribute.staticUse);
2172
Jamie Madillc349ec02015-08-21 16:53:12 -04002173 // Not set by glBindAttribLocation or by location layout qualifier
2174 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 {
Jamie Madill63805b42015-08-25 13:17:39 -04002176 int regs = VariableRegisterCount(attribute.type);
2177 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178
Jamie Madill63805b42015-08-25 13:17:39 -04002179 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180 {
Jamie Madillf6113162015-05-07 11:49:21 -04002181 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002182 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183 }
2184
Jamie Madillc349ec02015-08-21 16:53:12 -04002185 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002186 }
2187 }
2188
Jamie Madill48ef11b2016-04-27 15:21:52 -04002189 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002191 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002192 ASSERT(attribute.location != -1);
2193 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002194
Jamie Madill63805b42015-08-25 13:17:39 -04002195 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002196 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002197 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198 }
2199 }
2200
Geoff Lang7dd2e102014-11-10 15:19:26 -05002201 return true;
2202}
2203
Martin Radev4c4c8e72016-08-04 12:25:34 +03002204bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2205 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2206 const std::string &errorMessage,
2207 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002209 GLuint blockCount = 0;
2210 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002211 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002212 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002213 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002214 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002215 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002216 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002217 return false;
2218 }
2219 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002220 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002221 return true;
2222}
Jamie Madille473dee2015-08-18 14:49:01 -04002223
Martin Radev4c4c8e72016-08-04 12:25:34 +03002224bool Program::validateVertexAndFragmentInterfaceBlocks(
2225 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2226 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2227 InfoLog &infoLog) const
2228{
2229 // Check that interface blocks defined in the vertex and fragment shaders are identical
2230 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2231 UniformBlockMap linkedUniformBlocks;
2232
2233 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2234 {
2235 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2236 }
2237
Jamie Madille473dee2015-08-18 14:49:01 -04002238 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002239 {
Jamie Madille473dee2015-08-18 14:49:01 -04002240 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002241 if (entry != linkedUniformBlocks.end())
2242 {
2243 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2244 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2245 {
2246 return false;
2247 }
2248 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002249 }
2250 return true;
2251}
Jamie Madille473dee2015-08-18 14:49:01 -04002252
Martin Radev4c4c8e72016-08-04 12:25:34 +03002253bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2254{
2255 if (mState.mAttachedComputeShader)
2256 {
2257 const Shader &computeShader = *mState.mAttachedComputeShader;
2258 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2259
2260 if (!validateUniformBlocksCount(
2261 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2262 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2263 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002264 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002265 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002266 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002267 return true;
2268 }
2269
2270 const Shader &vertexShader = *mState.mAttachedVertexShader;
2271 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2272
2273 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2274 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2275
2276 if (!validateUniformBlocksCount(
2277 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2278 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2279 {
2280 return false;
2281 }
2282 if (!validateUniformBlocksCount(
2283 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2284 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2285 infoLog))
2286 {
2287
2288 return false;
2289 }
2290 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2291 infoLog))
2292 {
2293 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294 }
Jamie Madille473dee2015-08-18 14:49:01 -04002295
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296 return true;
2297}
2298
Jamie Madilla2c74982016-12-12 11:20:42 -05002299bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002300 const sh::InterfaceBlock &vertexInterfaceBlock,
2301 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002302{
2303 const char* blockName = vertexInterfaceBlock.name.c_str();
2304 // validate blocks for the same member types
2305 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2306 {
Jamie Madillf6113162015-05-07 11:49:21 -04002307 infoLog << "Types for interface block '" << blockName
2308 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002309 return false;
2310 }
2311 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2312 {
Jamie Madillf6113162015-05-07 11:49:21 -04002313 infoLog << "Array sizes differ for interface block '" << blockName
2314 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315 return false;
2316 }
2317 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2318 {
Jamie Madillf6113162015-05-07 11:49:21 -04002319 infoLog << "Layout qualifiers differ for interface block '" << blockName
2320 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002321 return false;
2322 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002323 const unsigned int numBlockMembers =
2324 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2326 {
2327 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2328 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2329 if (vertexMember.name != fragmentMember.name)
2330 {
Jamie Madillf6113162015-05-07 11:49:21 -04002331 infoLog << "Name mismatch for field " << blockMemberIndex
2332 << " of interface block '" << blockName
2333 << "': (in vertex: '" << vertexMember.name
2334 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002335 return false;
2336 }
2337 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2338 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2339 {
2340 return false;
2341 }
2342 }
2343 return true;
2344}
2345
2346bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2347 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2348{
2349 if (vertexVariable.type != fragmentVariable.type)
2350 {
Jamie Madillf6113162015-05-07 11:49:21 -04002351 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002352 return false;
2353 }
2354 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2355 {
Jamie Madillf6113162015-05-07 11:49:21 -04002356 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002357 return false;
2358 }
2359 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2360 {
Jamie Madillf6113162015-05-07 11:49:21 -04002361 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002362 return false;
2363 }
2364
2365 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2366 {
Jamie Madillf6113162015-05-07 11:49:21 -04002367 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368 return false;
2369 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002370 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2372 {
2373 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2374 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2375
2376 if (vertexMember.name != fragmentMember.name)
2377 {
Jamie Madillf6113162015-05-07 11:49:21 -04002378 infoLog << "Name mismatch for field '" << memberIndex
2379 << "' of " << variableName
2380 << ": (in vertex: '" << vertexMember.name
2381 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002382 return false;
2383 }
2384
2385 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2386 vertexMember.name + "'";
2387
2388 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2389 {
2390 return false;
2391 }
2392 }
2393
2394 return true;
2395}
2396
2397bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2398{
Cooper Partin1acf4382015-06-12 12:38:57 -07002399#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2400 const bool validatePrecision = true;
2401#else
2402 const bool validatePrecision = false;
2403#endif
2404
2405 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002406 {
2407 return false;
2408 }
2409
2410 return true;
2411}
2412
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002413bool Program::linkValidateVaryings(InfoLog &infoLog,
2414 const std::string &varyingName,
2415 const sh::Varying &vertexVarying,
2416 const sh::Varying &fragmentVarying,
2417 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002418{
2419 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2420 {
2421 return false;
2422 }
2423
Jamie Madille9cc4692015-02-19 16:00:13 -05002424 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002425 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002426 infoLog << "Interpolation types for " << varyingName
2427 << " differ between vertex and fragment shaders.";
2428 return false;
2429 }
2430
2431 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2432 {
2433 infoLog << "Invariance for " << varyingName
2434 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002435 return false;
2436 }
2437
2438 return true;
2439}
2440
Jamie Madillccdf74b2015-08-18 10:46:12 -04002441bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002442 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002443 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002444{
2445 size_t totalComponents = 0;
2446
Jamie Madillccdf74b2015-08-18 10:46:12 -04002447 std::set<std::string> uniqueNames;
2448
Jamie Madill48ef11b2016-04-27 15:21:52 -04002449 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 {
2451 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002452 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002453 {
Jamie Madill192745a2016-12-22 15:58:21 -05002454 const sh::Varying *varying = ref.second.get();
2455
Jamie Madillccdf74b2015-08-18 10:46:12 -04002456 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002457 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002458 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002459 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002460 infoLog << "Two transform feedback varyings specify the same output variable ("
2461 << tfVaryingName << ").";
2462 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002463 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002465
Geoff Lang1a683462015-09-29 15:09:59 -04002466 if (varying->isArray())
2467 {
2468 infoLog << "Capture of arrays is undefined and not supported.";
2469 return false;
2470 }
2471
Jamie Madillccdf74b2015-08-18 10:46:12 -04002472 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002473 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002474 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002475 componentCount > caps.maxTransformFeedbackSeparateComponents)
2476 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002477 infoLog << "Transform feedback varying's " << varying->name << " components ("
2478 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002479 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002480 return false;
2481 }
2482
2483 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002484 found = true;
2485 break;
2486 }
2487 }
2488
Jamie Madill89bb70e2015-08-31 14:18:39 -04002489 if (tfVaryingName.find('[') != std::string::npos)
2490 {
Geoff Lang1a683462015-09-29 15:09:59 -04002491 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002492 return false;
2493 }
2494
Geoff Lang7dd2e102014-11-10 15:19:26 -05002495 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2496 ASSERT(found);
2497 }
2498
Jamie Madill48ef11b2016-04-27 15:21:52 -04002499 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002500 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002501 {
Jamie Madillf6113162015-05-07 11:49:21 -04002502 infoLog << "Transform feedback varying total components (" << totalComponents
2503 << ") exceed the maximum interleaved components ("
2504 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002505 return false;
2506 }
2507
2508 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002509}
2510
Jamie Madill192745a2016-12-22 15:58:21 -05002511void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002512{
2513 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002514 mState.mTransformFeedbackVaryingVars.clear();
2515 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002516 {
Jamie Madill192745a2016-12-22 15:58:21 -05002517 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002518 {
Jamie Madill192745a2016-12-22 15:58:21 -05002519 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002520 if (tfVaryingName == varying->name)
2521 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002522 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002523 break;
2524 }
2525 }
2526 }
2527}
2528
Jamie Madill192745a2016-12-22 15:58:21 -05002529Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002530{
Jamie Madill192745a2016-12-22 15:58:21 -05002531 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002532
Jamie Madill48ef11b2016-04-27 15:21:52 -04002533 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002534 {
Jamie Madill192745a2016-12-22 15:58:21 -05002535 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002536 }
2537
Jamie Madill48ef11b2016-04-27 15:21:52 -04002538 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002539 {
Jamie Madill192745a2016-12-22 15:58:21 -05002540 merged[varying.name].fragment = &varying;
2541 }
2542
2543 return merged;
2544}
2545
2546std::vector<PackedVarying> Program::getPackedVaryings(
2547 const Program::MergedVaryings &mergedVaryings) const
2548{
2549 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2550 std::vector<PackedVarying> packedVaryings;
2551
2552 for (const auto &ref : mergedVaryings)
2553 {
2554 const sh::Varying *input = ref.second.vertex;
2555 const sh::Varying *output = ref.second.fragment;
2556
2557 // Only pack varyings that have a matched input or output, plus special builtins.
2558 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002559 {
Jamie Madill192745a2016-12-22 15:58:21 -05002560 // Will get the vertex shader interpolation by default.
2561 auto interpolation = ref.second.get()->interpolation;
2562
2563 // Interpolation qualifiers must match.
2564 if (output->isStruct())
2565 {
2566 ASSERT(!output->isArray());
2567 for (const auto &field : output->fields)
2568 {
2569 ASSERT(!field.isStruct() && !field.isArray());
2570 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2571 }
2572 }
2573 else
2574 {
2575 packedVaryings.push_back(PackedVarying(*output, interpolation));
2576 }
2577 continue;
2578 }
2579
2580 // Keep Transform FB varyings in the merged list always.
2581 if (!input)
2582 {
2583 continue;
2584 }
2585
2586 for (const std::string &tfVarying : tfVaryings)
2587 {
2588 if (tfVarying == input->name)
2589 {
2590 // Transform feedback for varying structs is underspecified.
2591 // See Khronos bug 9856.
2592 // TODO(jmadill): Figure out how to be spec-compliant here.
2593 if (!input->isStruct())
2594 {
2595 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2596 packedVaryings.back().vertexOnly = true;
2597 }
2598 break;
2599 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002600 }
2601 }
2602
Jamie Madill192745a2016-12-22 15:58:21 -05002603 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2604
2605 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002606}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002607
2608void Program::linkOutputVariables()
2609{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002610 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002611 ASSERT(fragmentShader != nullptr);
2612
2613 // Skip this step for GLES2 shaders.
2614 if (fragmentShader->getShaderVersion() == 100)
2615 return;
2616
Jamie Madilla0a9e122015-09-02 15:54:30 -04002617 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002618
2619 // TODO(jmadill): any caps validation here?
2620
2621 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2622 outputVariableIndex++)
2623 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002624 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002625
2626 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2627 if (outputVariable.isBuiltIn())
2628 continue;
2629
2630 // Since multiple output locations must be specified, use 0 for non-specified locations.
2631 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2632
2633 ASSERT(outputVariable.staticUse);
2634
2635 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2636 elementIndex++)
2637 {
2638 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002639 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002640 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002641 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002642 VariableLocation(outputVariable.name, element, outputVariableIndex);
2643 }
2644 }
2645}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002646
Jamie Madilla2c74982016-12-12 11:20:42 -05002647bool Program::flattenUniformsAndCheckCapsForShader(const Shader &shader,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002648 GLuint maxUniformComponents,
2649 GLuint maxTextureImageUnits,
2650 const std::string &componentsErrorMessage,
2651 const std::string &samplerErrorMessage,
2652 std::vector<LinkedUniform> &samplerUniforms,
2653 InfoLog &infoLog)
2654{
2655 VectorAndSamplerCount vasCount;
2656 for (const sh::Uniform &uniform : shader.getUniforms())
2657 {
2658 if (uniform.staticUse)
2659 {
2660 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2661 }
2662 }
2663
2664 if (vasCount.vectorCount > maxUniformComponents)
2665 {
2666 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2667 return false;
2668 }
2669
2670 if (vasCount.samplerCount > maxTextureImageUnits)
2671 {
2672 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2673 return false;
2674 }
2675
2676 return true;
2677}
2678
Jamie Madill62d31cb2015-09-11 13:25:51 -04002679bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2680{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002681 std::vector<LinkedUniform> samplerUniforms;
2682
Martin Radev4c4c8e72016-08-04 12:25:34 +03002683 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002684 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002685 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002686
2687 // TODO (mradev): check whether we need finer-grained component counting
2688 if (!flattenUniformsAndCheckCapsForShader(
2689 *computeShader, caps.maxComputeUniformComponents / 4,
2690 caps.maxComputeTextureImageUnits,
2691 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2692 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2693 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002694 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002695 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002696 }
2697 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002698 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002699 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002700 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002701
Martin Radev4c4c8e72016-08-04 12:25:34 +03002702 if (!flattenUniformsAndCheckCapsForShader(
2703 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2704 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2705 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2706 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002707 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002708 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002709 }
Jamie Madilla2c74982016-12-12 11:20:42 -05002710 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002711
Martin Radev4c4c8e72016-08-04 12:25:34 +03002712 if (!flattenUniformsAndCheckCapsForShader(
2713 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2714 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2715 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2716 infoLog))
2717 {
2718 return false;
2719 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002720 }
2721
Jamie Madille7d84322017-01-10 18:21:59 -05002722 mState.mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
2723 mState.mSamplerUniformRange.end =
2724 mState.mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002725
Jamie Madill48ef11b2016-04-27 15:21:52 -04002726 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002727
Jamie Madille7d84322017-01-10 18:21:59 -05002728 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2729 for (const auto &samplerUniform : samplerUniforms)
2730 {
2731 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2732 mState.mSamplerBindings.emplace_back(
2733 SamplerBinding(textureType, samplerUniform.elementCount()));
2734 }
2735
Jamie Madill62d31cb2015-09-11 13:25:51 -04002736 return true;
2737}
2738
2739Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002740 const std::string &fullName,
2741 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002742{
2743 VectorAndSamplerCount vectorAndSamplerCount;
2744
2745 if (uniform.isStruct())
2746 {
2747 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2748 {
2749 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2750
2751 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2752 {
2753 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2754 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2755
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002756 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002757 }
2758 }
2759
2760 return vectorAndSamplerCount;
2761 }
2762
2763 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002764 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002765 if (!UniformInList(mState.getUniforms(), fullName) &&
2766 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002768 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
2769 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002770 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002771
2772 // Store sampler uniforms separately, so we'll append them to the end of the list.
2773 if (isSampler)
2774 {
2775 samplerUniforms->push_back(linkedUniform);
2776 }
2777 else
2778 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002779 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002780 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002781 }
2782
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002783 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002784
2785 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2786 // Likewise, don't count "real" uniforms towards sampler count.
2787 vectorAndSamplerCount.vectorCount =
2788 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002789 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002790
2791 return vectorAndSamplerCount;
2792}
2793
2794void Program::gatherInterfaceBlockInfo()
2795{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002796 ASSERT(mState.mUniformBlocks.empty());
2797
2798 if (mState.mAttachedComputeShader)
2799 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002800 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002801
2802 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2803 {
2804
2805 // Only 'packed' blocks are allowed to be considered inactive.
2806 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2807 continue;
2808
Jamie Madilla2c74982016-12-12 11:20:42 -05002809 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002810 {
2811 if (block.name == computeBlock.name)
2812 {
2813 block.computeStaticUse = computeBlock.staticUse;
2814 }
2815 }
2816
2817 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2818 }
2819 return;
2820 }
2821
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822 std::set<std::string> visitedList;
2823
Jamie Madilla2c74982016-12-12 11:20:42 -05002824 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002825
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2827 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002828 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2830 continue;
2831
2832 if (visitedList.count(vertexBlock.name) > 0)
2833 continue;
2834
2835 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2836 visitedList.insert(vertexBlock.name);
2837 }
2838
Jamie Madilla2c74982016-12-12 11:20:42 -05002839 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002840
2841 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2842 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002843 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002844 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2845 continue;
2846
2847 if (visitedList.count(fragmentBlock.name) > 0)
2848 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002849 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002850 {
2851 if (block.name == fragmentBlock.name)
2852 {
2853 block.fragmentStaticUse = fragmentBlock.staticUse;
2854 }
2855 }
2856
2857 continue;
2858 }
2859
2860 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2861 visitedList.insert(fragmentBlock.name);
2862 }
2863}
2864
Jamie Madill4a3c2342015-10-08 12:58:45 -04002865template <typename VarT>
2866void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2867 const std::string &prefix,
2868 int blockIndex)
2869{
2870 for (const VarT &field : fields)
2871 {
2872 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2873
2874 if (field.isStruct())
2875 {
2876 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2877 {
2878 const std::string uniformElementName =
2879 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2880 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2881 }
2882 }
2883 else
2884 {
2885 // If getBlockMemberInfo returns false, the uniform is optimized out.
2886 sh::BlockMemberInfo memberInfo;
2887 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2888 {
2889 continue;
2890 }
2891
2892 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2893 blockIndex, memberInfo);
2894
2895 // Since block uniforms have no location, we don't need to store them in the uniform
2896 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002897 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002898 }
2899 }
2900}
2901
Jamie Madill62d31cb2015-09-11 13:25:51 -04002902void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2903{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002904 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002905 size_t blockSize = 0;
2906
2907 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002908 std::stringstream blockNameStr;
2909 blockNameStr << interfaceBlock.name;
2910 if (interfaceBlock.arraySize > 0)
2911 {
2912 blockNameStr << "[0]";
2913 }
2914 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002915 {
2916 return;
2917 }
2918
2919 // Track the first and last uniform index to determine the range of active uniforms in the
2920 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002921 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002922 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002923 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002924
2925 std::vector<unsigned int> blockUniformIndexes;
2926 for (size_t blockUniformIndex = firstBlockUniformIndex;
2927 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2928 {
2929 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2930 }
2931
2932 if (interfaceBlock.arraySize > 0)
2933 {
2934 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2935 {
2936 UniformBlock block(interfaceBlock.name, true, arrayElement);
2937 block.memberUniformIndexes = blockUniformIndexes;
2938
Martin Radev4c4c8e72016-08-04 12:25:34 +03002939 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002940 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002941 case GL_VERTEX_SHADER:
2942 {
2943 block.vertexStaticUse = interfaceBlock.staticUse;
2944 break;
2945 }
2946 case GL_FRAGMENT_SHADER:
2947 {
2948 block.fragmentStaticUse = interfaceBlock.staticUse;
2949 break;
2950 }
2951 case GL_COMPUTE_SHADER:
2952 {
2953 block.computeStaticUse = interfaceBlock.staticUse;
2954 break;
2955 }
2956 default:
2957 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002958 }
2959
Qin Jiajia0350a642016-11-01 17:01:51 +08002960 // Since all block elements in an array share the same active uniforms, they will all be
2961 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2962 // here we will add every block element in the array.
2963 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002964 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002965 }
2966 }
2967 else
2968 {
2969 UniformBlock block(interfaceBlock.name, false, 0);
2970 block.memberUniformIndexes = blockUniformIndexes;
2971
Martin Radev4c4c8e72016-08-04 12:25:34 +03002972 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002973 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002974 case GL_VERTEX_SHADER:
2975 {
2976 block.vertexStaticUse = interfaceBlock.staticUse;
2977 break;
2978 }
2979 case GL_FRAGMENT_SHADER:
2980 {
2981 block.fragmentStaticUse = interfaceBlock.staticUse;
2982 break;
2983 }
2984 case GL_COMPUTE_SHADER:
2985 {
2986 block.computeStaticUse = interfaceBlock.staticUse;
2987 break;
2988 }
2989 default:
2990 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002991 }
2992
Jamie Madill4a3c2342015-10-08 12:58:45 -04002993 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002994 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002995 }
2996}
2997
Jamie Madille7d84322017-01-10 18:21:59 -05002998template <>
2999void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3000 const uint8_t *destPointer,
3001 GLsizei clampedCount,
3002 const GLint *v)
3003{
3004 // Invalidate the validation cache only if we modify the sampler data.
3005 if (mState.isSamplerUniformIndex(locationInfo.index) &&
3006 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
3007 {
3008 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3009 std::vector<GLuint> *boundTextureUnits =
3010 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
3011
3012 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
3013 mCachedValidateSamplersResult.reset();
3014 }
3015}
3016
3017template <typename T>
3018void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3019 const uint8_t *destPointer,
3020 GLsizei clampedCount,
3021 const T *v)
3022{
3023}
3024
Jamie Madill62d31cb2015-09-11 13:25:51 -04003025template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003026GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003027{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003028 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3029 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003030 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
3031
Corentin Wallez15ac5342016-11-03 17:06:39 -04003032 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3033 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3034 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003035 GLsizei maxElementCount =
3036 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
3037
3038 GLsizei count = countIn;
3039 GLsizei clampedCount = count * vectorSize;
3040 if (clampedCount > maxElementCount)
3041 {
3042 clampedCount = maxElementCount;
3043 count = maxElementCount / vectorSize;
3044 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04003045
Jamie Madill62d31cb2015-09-11 13:25:51 -04003046 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
3047 {
3048 // Do a cast conversion for boolean types. From the spec:
3049 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
3050 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003051 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003052 {
3053 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
3054 }
3055 }
3056 else
3057 {
Jamie Madille7d84322017-01-10 18:21:59 -05003058 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003059 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003060 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003061
3062 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003063}
3064
3065template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003066GLsizei Program::setMatrixUniformInternal(GLint location,
3067 GLsizei count,
3068 GLboolean transpose,
3069 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003070{
3071 if (!transpose)
3072 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003073 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003074 }
3075
3076 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003077 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3078 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003079 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003080
3081 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3082 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3083 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3084 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3085
3086 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003087 {
3088 size_t elementOffset = element * rows * cols;
3089
3090 for (size_t row = 0; row < rows; ++row)
3091 {
3092 for (size_t col = 0; col < cols; ++col)
3093 {
3094 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3095 }
3096 }
3097 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003098
3099 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003100}
3101
3102template <typename DestT>
3103void Program::getUniformInternal(GLint location, DestT *dataOut) const
3104{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003105 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3106 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003107
3108 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3109
3110 GLenum componentType = VariableComponentType(uniform.type);
3111 if (componentType == GLTypeToGLenum<DestT>::value)
3112 {
3113 memcpy(dataOut, srcPointer, uniform.getElementSize());
3114 return;
3115 }
3116
Corentin Wallez6596c462016-03-17 17:26:58 -04003117 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003118
3119 switch (componentType)
3120 {
3121 case GL_INT:
3122 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3123 break;
3124 case GL_UNSIGNED_INT:
3125 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3126 break;
3127 case GL_BOOL:
3128 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3129 break;
3130 case GL_FLOAT:
3131 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3132 break;
3133 default:
3134 UNREACHABLE();
3135 }
3136}
Jamie Madilla4595b82017-01-11 17:36:34 -05003137
3138bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3139{
3140 // Must be called after samplers are validated.
3141 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3142
3143 for (const auto &binding : mState.mSamplerBindings)
3144 {
3145 GLenum textureType = binding.textureType;
3146 for (const auto &unit : binding.boundTextureUnits)
3147 {
3148 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3149 if (programTextureID == textureID)
3150 {
3151 // TODO(jmadill): Check for appropriate overlap.
3152 return true;
3153 }
3154 }
3155 }
3156
3157 return false;
3158}
3159
Jamie Madilla2c74982016-12-12 11:20:42 -05003160} // namespace gl