blob: 8aa6a1783ad397561d2f658517af7f78a3a23c65 [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
Jamie Madill7aea7e02016-05-10 10:39:45 -0400383Program::Program(rx::GLImplFactory *factory, ResourceManager *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
1408bool Program::isIgnoredUniformLocation(GLint location) const
1409{
1410 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1411 // out
1412 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001413 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1414 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001419 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001420 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421}
1422
Jamie Madill62d31cb2015-09-11 13:25:51 -04001423GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001425 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426}
1427
Jamie Madill62d31cb2015-09-11 13:25:51 -04001428GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429{
Jamie Madille7d84322017-01-10 18:21:59 -05001430 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431}
1432
1433void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1434{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001435 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1436 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437}
1438
1439void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1440{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001441 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1442 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443}
1444
1445void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1446{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001447 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1448 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449}
1450
1451void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1452{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001453 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1454 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455}
1456
1457void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1458{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001459 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1460 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461}
1462
1463void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1464{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001465 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1466 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467}
1468
1469void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1470{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001471 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1472 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001473}
1474
1475void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1476{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001477 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1478 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001479}
1480
1481void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1482{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001483 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1484 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001485}
1486
1487void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1488{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001489 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1490 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001491}
1492
1493void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1494{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001495 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1496 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497}
1498
1499void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1500{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001501 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1502 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001503}
1504
1505void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1506{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001507 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1508 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509}
1510
1511void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1512{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001513 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1514 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001515}
1516
1517void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1518{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001519 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1520 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001521}
1522
1523void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1524{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001525 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1526 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001527}
1528
1529void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1530{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001531 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1532 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001533}
1534
1535void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1536{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001537 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1538 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001539}
1540
1541void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1542{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001543 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1544 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001545}
1546
1547void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1548{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001549 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1550 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001551}
1552
1553void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1554{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001555 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1556 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001557}
1558
Geoff Lange1a27752015-10-05 13:16:04 -04001559void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001560{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001561 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562}
1563
Geoff Lange1a27752015-10-05 13:16:04 -04001564void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001565{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001566 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567}
1568
Geoff Lange1a27752015-10-05 13:16:04 -04001569void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001570{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001571 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001572}
1573
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574void Program::flagForDeletion()
1575{
1576 mDeleteStatus = true;
1577}
1578
1579bool Program::isFlaggedForDeletion() const
1580{
1581 return mDeleteStatus;
1582}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001583
Brandon Jones43a53e22014-08-28 16:23:22 -07001584void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001585{
1586 mInfoLog.reset();
1587
Geoff Lang7dd2e102014-11-10 15:19:26 -05001588 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001589 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001590 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001591 }
1592 else
1593 {
Jamie Madillf6113162015-05-07 11:49:21 -04001594 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001595 }
1596}
1597
Geoff Lang7dd2e102014-11-10 15:19:26 -05001598bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1599{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001600 // Skip cache if we're using an infolog, so we get the full error.
1601 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1602 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1603 {
1604 return mCachedValidateSamplersResult.value();
1605 }
1606
1607 if (mTextureUnitTypesCache.empty())
1608 {
1609 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1610 }
1611 else
1612 {
1613 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1614 }
1615
1616 // if any two active samplers in a program are of different types, but refer to the same
1617 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1618 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001619 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001620 {
Jamie Madille7d84322017-01-10 18:21:59 -05001621 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001622
Jamie Madille7d84322017-01-10 18:21:59 -05001623 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001624 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001625 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1626 {
1627 if (infoLog)
1628 {
1629 (*infoLog) << "Sampler uniform (" << textureUnit
1630 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1631 << caps.maxCombinedTextureImageUnits << ")";
1632 }
1633
1634 mCachedValidateSamplersResult = false;
1635 return false;
1636 }
1637
1638 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1639 {
1640 if (textureType != mTextureUnitTypesCache[textureUnit])
1641 {
1642 if (infoLog)
1643 {
1644 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1645 "image unit ("
1646 << textureUnit << ").";
1647 }
1648
1649 mCachedValidateSamplersResult = false;
1650 return false;
1651 }
1652 }
1653 else
1654 {
1655 mTextureUnitTypesCache[textureUnit] = textureType;
1656 }
1657 }
1658 }
1659
1660 mCachedValidateSamplersResult = true;
1661 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662}
1663
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001664bool Program::isValidated() const
1665{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 return mValidated;
1667}
1668
Geoff Lange1a27752015-10-05 13:16:04 -04001669GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001671 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001672}
1673
1674void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1675{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001676 ASSERT(
1677 uniformBlockIndex <
1678 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001679
Jamie Madill48ef11b2016-04-27 15:21:52 -04001680 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681
1682 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001683 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001684 std::string string = uniformBlock.name;
1685
Jamie Madill62d31cb2015-09-11 13:25:51 -04001686 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689 }
1690
1691 strncpy(uniformBlockName, string.c_str(), bufSize);
1692 uniformBlockName[bufSize - 1] = '\0';
1693
1694 if (length)
1695 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001696 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001698 }
1699}
1700
Geoff Lange1a27752015-10-05 13:16:04 -04001701GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001702{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001703 int maxLength = 0;
1704
1705 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001706 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001707 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001708 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1709 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001710 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001711 if (!uniformBlock.name.empty())
1712 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001713 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001714
1715 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001716 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001717
1718 maxLength = std::max(length + arrayLength, maxLength);
1719 }
1720 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001721 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722
1723 return maxLength;
1724}
1725
Geoff Lange1a27752015-10-05 13:16:04 -04001726GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001727{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001728 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001729 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001730
Jamie Madill48ef11b2016-04-27 15:21:52 -04001731 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001732 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1733 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001734 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001735 if (uniformBlock.name == baseName)
1736 {
1737 const bool arrayElementZero =
1738 (subscript == GL_INVALID_INDEX &&
1739 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1740 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1741 {
1742 return blockIndex;
1743 }
1744 }
1745 }
1746
1747 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001748}
1749
Jamie Madill62d31cb2015-09-11 13:25:51 -04001750const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001751{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001752 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1753 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001754}
1755
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001756void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1757{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001758 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001759 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001760 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001761}
1762
1763GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1764{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001765 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001766}
1767
1768void Program::resetUniformBlockBindings()
1769{
1770 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1771 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001772 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001773 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001774 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001775}
1776
Geoff Lang48dcae72014-02-05 16:28:24 -05001777void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1778{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001779 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001780 for (GLsizei i = 0; i < count; i++)
1781 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001782 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001783 }
1784
Jamie Madill48ef11b2016-04-27 15:21:52 -04001785 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001786}
1787
1788void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1789{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001790 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001791 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001792 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1793 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001794 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1795 if (length)
1796 {
1797 *length = lastNameIdx;
1798 }
1799 if (size)
1800 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001801 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001802 }
1803 if (type)
1804 {
1805 *type = varying.type;
1806 }
1807 if (name)
1808 {
1809 memcpy(name, varying.name.c_str(), lastNameIdx);
1810 name[lastNameIdx] = '\0';
1811 }
1812 }
1813}
1814
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001815GLsizei Program::getTransformFeedbackVaryingCount() const
1816{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001818 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001819 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001820 }
1821 else
1822 {
1823 return 0;
1824 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001825}
1826
1827GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1828{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001830 {
1831 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001832 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001833 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001834 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1835 }
1836
1837 return maxSize;
1838 }
1839 else
1840 {
1841 return 0;
1842 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001843}
1844
1845GLenum Program::getTransformFeedbackBufferMode() const
1846{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001847 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848}
1849
Jamie Madill192745a2016-12-22 15:58:21 -05001850bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851{
Jamie Madill192745a2016-12-22 15:58:21 -05001852 const Shader *vertexShader = mState.mAttachedVertexShader;
1853 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1854
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001855 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1856
Jamie Madill4cff2472015-08-21 16:53:18 -04001857 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1858 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859
Sami Väisänen46eaa942016-06-29 10:26:37 +03001860 std::map<GLuint, std::string> staticFragmentInputLocations;
1861
Jamie Madill4cff2472015-08-21 16:53:18 -04001862 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001863 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 bool matched = false;
1865
1866 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001867 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 {
1869 continue;
1870 }
1871
Jamie Madill4cff2472015-08-21 16:53:18 -04001872 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001874 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001876 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001877 if (!linkValidateVaryings(infoLog, output.name, input, output,
1878 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879 {
1880 return false;
1881 }
1882
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 matched = true;
1884 break;
1885 }
1886 }
1887
1888 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001889 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001891 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 return false;
1893 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001894
1895 // Check for aliased path rendering input bindings (if any).
1896 // If more than one binding refer statically to the same
1897 // location the link must fail.
1898
1899 if (!output.staticUse)
1900 continue;
1901
1902 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1903 if (inputBinding == -1)
1904 continue;
1905
1906 const auto it = staticFragmentInputLocations.find(inputBinding);
1907 if (it == std::end(staticFragmentInputLocations))
1908 {
1909 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1910 }
1911 else
1912 {
1913 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1914 << it->second;
1915 return false;
1916 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917 }
1918
Jamie Madillada9ecc2015-08-17 12:53:37 -04001919 // TODO(jmadill): verify no unmatched vertex varyings?
1920
Geoff Lang7dd2e102014-11-10 15:19:26 -05001921 return true;
1922}
1923
Martin Radev4c4c8e72016-08-04 12:25:34 +03001924bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001925{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001926 // Check that uniforms defined in the vertex and fragment shaders are identical
1927 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001928 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1929 const std::vector<sh::Uniform> &fragmentUniforms =
1930 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001931
Jamie Madillea918db2015-08-18 14:48:59 -04001932 for (const sh::Uniform &vertexUniform : vertexUniforms)
1933 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001935 }
1936
1937 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1938 {
1939 auto entry = linkedUniforms.find(fragmentUniform.name);
1940 if (entry != linkedUniforms.end())
1941 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001942 LinkedUniform *vertexUniform = &entry->second;
1943 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1944 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001945 {
1946 return false;
1947 }
1948 }
1949 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001950 return true;
1951}
1952
Jamie Madilla2c74982016-12-12 11:20:42 -05001953bool Program::linkUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001954{
1955 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1956 {
1957 ASSERT(mState.mAttachedComputeShader == nullptr);
1958 if (!validateVertexAndFragmentUniforms(infoLog))
1959 {
1960 return false;
1961 }
1962 }
Jamie Madillea918db2015-08-18 14:48:59 -04001963
Jamie Madill62d31cb2015-09-11 13:25:51 -04001964 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1965 // Also check the maximum uniform vector and sampler counts.
1966 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1967 {
1968 return false;
1969 }
1970
Geoff Langd8605522016-04-13 10:19:12 -04001971 if (!indexUniforms(infoLog, caps, uniformBindings))
1972 {
1973 return false;
1974 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001975
Jamie Madillea918db2015-08-18 14:48:59 -04001976 return true;
1977}
1978
Jamie Madilla2c74982016-12-12 11:20:42 -05001979bool Program::indexUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001980{
Geoff Langd8605522016-04-13 10:19:12 -04001981 // Uniforms awaiting a location
1982 std::vector<VariableLocation> unboundUniforms;
1983 std::map<GLuint, VariableLocation> boundUniforms;
1984 int maxUniformLocation = -1;
1985
1986 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001987 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001988 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001989 const LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001990
Geoff Langd8605522016-04-13 10:19:12 -04001991 if (uniform.isBuiltIn())
1992 {
1993 continue;
1994 }
1995
1996 int bindingLocation = uniformBindings.getBinding(uniform.name);
1997
1998 // Verify that this location isn't bound twice
1999 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
2000 {
2001 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
2002 return false;
2003 }
2004
Jamie Madill62d31cb2015-09-11 13:25:51 -04002005 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
2006 {
Geoff Langd8605522016-04-13 10:19:12 -04002007 VariableLocation location(uniform.name, arrayIndex,
2008 static_cast<unsigned int>(uniformIndex));
2009
2010 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002011 {
Geoff Langd8605522016-04-13 10:19:12 -04002012 boundUniforms[bindingLocation] = location;
2013 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
2014 }
2015 else
2016 {
2017 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002018 }
2019 }
2020 }
Geoff Langd8605522016-04-13 10:19:12 -04002021
2022 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
2023 // not be assigned to those locations.
2024 std::set<GLuint> reservedLocations;
2025 for (const auto &binding : uniformBindings)
2026 {
2027 GLuint location = binding.second;
2028 if (boundUniforms.find(location) == boundUniforms.end())
2029 {
2030 reservedLocations.insert(location);
2031 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
2032 }
2033 }
2034
2035 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04002036 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04002037 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
2038 static_cast<size_t>(maxUniformLocation + 1)));
2039
2040 // Assign bound uniforms
2041 for (const auto &boundUniform : boundUniforms)
2042 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002043 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002044 }
2045
2046 // Assign reserved uniforms
2047 for (const auto &reservedLocation : reservedLocations)
2048 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002049 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002050 }
2051
2052 // Assign unbound uniforms
2053 size_t nextUniformLocation = 0;
2054 for (const auto &unboundUniform : unboundUniforms)
2055 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002056 while (mState.mUniformLocations[nextUniformLocation].used ||
2057 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002058 {
2059 nextUniformLocation++;
2060 }
2061
Jamie Madill48ef11b2016-04-27 15:21:52 -04002062 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
2063 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002064 nextUniformLocation++;
2065 }
2066
2067 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002068}
2069
Martin Radev4c4c8e72016-08-04 12:25:34 +03002070bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2071 const std::string &uniformName,
2072 const sh::InterfaceBlockField &vertexUniform,
2073 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074{
Jamie Madillc4c744222015-11-04 09:39:47 -05002075 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2076 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077 {
2078 return false;
2079 }
2080
2081 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2082 {
Jamie Madillf6113162015-05-07 11:49:21 -04002083 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084 return false;
2085 }
2086
2087 return true;
2088}
2089
Jamie Madilleb979bf2016-11-15 12:28:46 -05002090// Assigns locations to all attributes from the bindings and program locations.
2091bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002093 const auto *vertexShader = mState.getAttachedVertexShader();
2094
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002096 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002097 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002098
2099 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002100 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002101 {
Jamie Madillf6113162015-05-07 11:49:21 -04002102 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002103 return false;
2104 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002106 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002107
Jamie Madillc349ec02015-08-21 16:53:12 -04002108 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002109 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002110 {
2111 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 ASSERT(attribute.staticUse);
2113
Jamie Madilleb979bf2016-11-15 12:28:46 -05002114 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002115 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002116 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002117 attribute.location = bindingLocation;
2118 }
2119
2120 if (attribute.location != -1)
2121 {
2122 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002123 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124
Jamie Madill63805b42015-08-25 13:17:39 -04002125 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 {
Jamie Madillf6113162015-05-07 11:49:21 -04002127 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002128 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129
2130 return false;
2131 }
2132
Jamie Madill63805b42015-08-25 13:17:39 -04002133 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 {
Jamie Madill63805b42015-08-25 13:17:39 -04002135 const int regLocation = attribute.location + reg;
2136 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137
2138 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002139 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002140 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002141 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002142 // TODO(jmadill): fix aliasing on ES2
2143 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002145 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002146 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147 return false;
2148 }
2149 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002150 else
2151 {
Jamie Madill63805b42015-08-25 13:17:39 -04002152 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002153 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154
Jamie Madill63805b42015-08-25 13:17:39 -04002155 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002156 }
2157 }
2158 }
2159
2160 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002161 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 ASSERT(attribute.staticUse);
2164
Jamie Madillc349ec02015-08-21 16:53:12 -04002165 // Not set by glBindAttribLocation or by location layout qualifier
2166 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 {
Jamie Madill63805b42015-08-25 13:17:39 -04002168 int regs = VariableRegisterCount(attribute.type);
2169 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170
Jamie Madill63805b42015-08-25 13:17:39 -04002171 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
Jamie Madillf6113162015-05-07 11:49:21 -04002173 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002174 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 }
2176
Jamie Madillc349ec02015-08-21 16:53:12 -04002177 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002178 }
2179 }
2180
Jamie Madill48ef11b2016-04-27 15:21:52 -04002181 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002183 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002184 ASSERT(attribute.location != -1);
2185 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002186
Jamie Madill63805b42015-08-25 13:17:39 -04002187 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002189 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002190 }
2191 }
2192
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193 return true;
2194}
2195
Martin Radev4c4c8e72016-08-04 12:25:34 +03002196bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2197 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2198 const std::string &errorMessage,
2199 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002200{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002201 GLuint blockCount = 0;
2202 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002204 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002205 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002206 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002207 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002208 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002209 return false;
2210 }
2211 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002212 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002213 return true;
2214}
Jamie Madille473dee2015-08-18 14:49:01 -04002215
Martin Radev4c4c8e72016-08-04 12:25:34 +03002216bool Program::validateVertexAndFragmentInterfaceBlocks(
2217 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2218 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2219 InfoLog &infoLog) const
2220{
2221 // Check that interface blocks defined in the vertex and fragment shaders are identical
2222 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2223 UniformBlockMap linkedUniformBlocks;
2224
2225 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2226 {
2227 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2228 }
2229
Jamie Madille473dee2015-08-18 14:49:01 -04002230 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002231 {
Jamie Madille473dee2015-08-18 14:49:01 -04002232 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233 if (entry != linkedUniformBlocks.end())
2234 {
2235 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2236 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2237 {
2238 return false;
2239 }
2240 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002241 }
2242 return true;
2243}
Jamie Madille473dee2015-08-18 14:49:01 -04002244
Martin Radev4c4c8e72016-08-04 12:25:34 +03002245bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2246{
2247 if (mState.mAttachedComputeShader)
2248 {
2249 const Shader &computeShader = *mState.mAttachedComputeShader;
2250 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2251
2252 if (!validateUniformBlocksCount(
2253 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2254 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2255 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002256 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002257 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002258 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002259 return true;
2260 }
2261
2262 const Shader &vertexShader = *mState.mAttachedVertexShader;
2263 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2264
2265 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2266 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2267
2268 if (!validateUniformBlocksCount(
2269 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2270 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2271 {
2272 return false;
2273 }
2274 if (!validateUniformBlocksCount(
2275 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2276 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2277 infoLog))
2278 {
2279
2280 return false;
2281 }
2282 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2283 infoLog))
2284 {
2285 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002286 }
Jamie Madille473dee2015-08-18 14:49:01 -04002287
Geoff Lang7dd2e102014-11-10 15:19:26 -05002288 return true;
2289}
2290
Jamie Madilla2c74982016-12-12 11:20:42 -05002291bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002292 const sh::InterfaceBlock &vertexInterfaceBlock,
2293 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002294{
2295 const char* blockName = vertexInterfaceBlock.name.c_str();
2296 // validate blocks for the same member types
2297 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2298 {
Jamie Madillf6113162015-05-07 11:49:21 -04002299 infoLog << "Types for interface block '" << blockName
2300 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002301 return false;
2302 }
2303 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2304 {
Jamie Madillf6113162015-05-07 11:49:21 -04002305 infoLog << "Array sizes differ for interface block '" << blockName
2306 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002307 return false;
2308 }
2309 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2310 {
Jamie Madillf6113162015-05-07 11:49:21 -04002311 infoLog << "Layout qualifiers differ for interface block '" << blockName
2312 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002313 return false;
2314 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002315 const unsigned int numBlockMembers =
2316 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002317 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2318 {
2319 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2320 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2321 if (vertexMember.name != fragmentMember.name)
2322 {
Jamie Madillf6113162015-05-07 11:49:21 -04002323 infoLog << "Name mismatch for field " << blockMemberIndex
2324 << " of interface block '" << blockName
2325 << "': (in vertex: '" << vertexMember.name
2326 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002327 return false;
2328 }
2329 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2330 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2331 {
2332 return false;
2333 }
2334 }
2335 return true;
2336}
2337
2338bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2339 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2340{
2341 if (vertexVariable.type != fragmentVariable.type)
2342 {
Jamie Madillf6113162015-05-07 11:49:21 -04002343 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002344 return false;
2345 }
2346 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2347 {
Jamie Madillf6113162015-05-07 11:49:21 -04002348 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002349 return false;
2350 }
2351 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2352 {
Jamie Madillf6113162015-05-07 11:49:21 -04002353 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 return false;
2355 }
2356
2357 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2358 {
Jamie Madillf6113162015-05-07 11:49:21 -04002359 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002360 return false;
2361 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002362 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2364 {
2365 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2366 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2367
2368 if (vertexMember.name != fragmentMember.name)
2369 {
Jamie Madillf6113162015-05-07 11:49:21 -04002370 infoLog << "Name mismatch for field '" << memberIndex
2371 << "' of " << variableName
2372 << ": (in vertex: '" << vertexMember.name
2373 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374 return false;
2375 }
2376
2377 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2378 vertexMember.name + "'";
2379
2380 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2381 {
2382 return false;
2383 }
2384 }
2385
2386 return true;
2387}
2388
2389bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2390{
Cooper Partin1acf4382015-06-12 12:38:57 -07002391#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2392 const bool validatePrecision = true;
2393#else
2394 const bool validatePrecision = false;
2395#endif
2396
2397 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398 {
2399 return false;
2400 }
2401
2402 return true;
2403}
2404
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002405bool Program::linkValidateVaryings(InfoLog &infoLog,
2406 const std::string &varyingName,
2407 const sh::Varying &vertexVarying,
2408 const sh::Varying &fragmentVarying,
2409 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002410{
2411 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2412 {
2413 return false;
2414 }
2415
Jamie Madille9cc4692015-02-19 16:00:13 -05002416 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002417 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002418 infoLog << "Interpolation types for " << varyingName
2419 << " differ between vertex and fragment shaders.";
2420 return false;
2421 }
2422
2423 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2424 {
2425 infoLog << "Invariance for " << varyingName
2426 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002427 return false;
2428 }
2429
2430 return true;
2431}
2432
Jamie Madillccdf74b2015-08-18 10:46:12 -04002433bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002434 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002435 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002436{
2437 size_t totalComponents = 0;
2438
Jamie Madillccdf74b2015-08-18 10:46:12 -04002439 std::set<std::string> uniqueNames;
2440
Jamie Madill48ef11b2016-04-27 15:21:52 -04002441 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002442 {
2443 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002444 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002445 {
Jamie Madill192745a2016-12-22 15:58:21 -05002446 const sh::Varying *varying = ref.second.get();
2447
Jamie Madillccdf74b2015-08-18 10:46:12 -04002448 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002449 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002450 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002451 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002452 infoLog << "Two transform feedback varyings specify the same output variable ("
2453 << tfVaryingName << ").";
2454 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002455 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002456 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002457
Geoff Lang1a683462015-09-29 15:09:59 -04002458 if (varying->isArray())
2459 {
2460 infoLog << "Capture of arrays is undefined and not supported.";
2461 return false;
2462 }
2463
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002465 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002466 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002467 componentCount > caps.maxTransformFeedbackSeparateComponents)
2468 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002469 infoLog << "Transform feedback varying's " << varying->name << " components ("
2470 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002471 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472 return false;
2473 }
2474
2475 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002476 found = true;
2477 break;
2478 }
2479 }
2480
Jamie Madill89bb70e2015-08-31 14:18:39 -04002481 if (tfVaryingName.find('[') != std::string::npos)
2482 {
Geoff Lang1a683462015-09-29 15:09:59 -04002483 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002484 return false;
2485 }
2486
Geoff Lang7dd2e102014-11-10 15:19:26 -05002487 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2488 ASSERT(found);
2489 }
2490
Jamie Madill48ef11b2016-04-27 15:21:52 -04002491 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002492 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002493 {
Jamie Madillf6113162015-05-07 11:49:21 -04002494 infoLog << "Transform feedback varying total components (" << totalComponents
2495 << ") exceed the maximum interleaved components ("
2496 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002497 return false;
2498 }
2499
2500 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002501}
2502
Jamie Madill192745a2016-12-22 15:58:21 -05002503void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002504{
2505 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002506 mState.mTransformFeedbackVaryingVars.clear();
2507 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002508 {
Jamie Madill192745a2016-12-22 15:58:21 -05002509 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002510 {
Jamie Madill192745a2016-12-22 15:58:21 -05002511 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002512 if (tfVaryingName == varying->name)
2513 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002514 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002515 break;
2516 }
2517 }
2518 }
2519}
2520
Jamie Madill192745a2016-12-22 15:58:21 -05002521Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002522{
Jamie Madill192745a2016-12-22 15:58:21 -05002523 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002524
Jamie Madill48ef11b2016-04-27 15:21:52 -04002525 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002526 {
Jamie Madill192745a2016-12-22 15:58:21 -05002527 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002528 }
2529
Jamie Madill48ef11b2016-04-27 15:21:52 -04002530 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002531 {
Jamie Madill192745a2016-12-22 15:58:21 -05002532 merged[varying.name].fragment = &varying;
2533 }
2534
2535 return merged;
2536}
2537
2538std::vector<PackedVarying> Program::getPackedVaryings(
2539 const Program::MergedVaryings &mergedVaryings) const
2540{
2541 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2542 std::vector<PackedVarying> packedVaryings;
2543
2544 for (const auto &ref : mergedVaryings)
2545 {
2546 const sh::Varying *input = ref.second.vertex;
2547 const sh::Varying *output = ref.second.fragment;
2548
2549 // Only pack varyings that have a matched input or output, plus special builtins.
2550 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002551 {
Jamie Madill192745a2016-12-22 15:58:21 -05002552 // Will get the vertex shader interpolation by default.
2553 auto interpolation = ref.second.get()->interpolation;
2554
2555 // Interpolation qualifiers must match.
2556 if (output->isStruct())
2557 {
2558 ASSERT(!output->isArray());
2559 for (const auto &field : output->fields)
2560 {
2561 ASSERT(!field.isStruct() && !field.isArray());
2562 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2563 }
2564 }
2565 else
2566 {
2567 packedVaryings.push_back(PackedVarying(*output, interpolation));
2568 }
2569 continue;
2570 }
2571
2572 // Keep Transform FB varyings in the merged list always.
2573 if (!input)
2574 {
2575 continue;
2576 }
2577
2578 for (const std::string &tfVarying : tfVaryings)
2579 {
2580 if (tfVarying == input->name)
2581 {
2582 // Transform feedback for varying structs is underspecified.
2583 // See Khronos bug 9856.
2584 // TODO(jmadill): Figure out how to be spec-compliant here.
2585 if (!input->isStruct())
2586 {
2587 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2588 packedVaryings.back().vertexOnly = true;
2589 }
2590 break;
2591 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002592 }
2593 }
2594
Jamie Madill192745a2016-12-22 15:58:21 -05002595 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2596
2597 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002598}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002599
2600void Program::linkOutputVariables()
2601{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002602 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002603 ASSERT(fragmentShader != nullptr);
2604
2605 // Skip this step for GLES2 shaders.
2606 if (fragmentShader->getShaderVersion() == 100)
2607 return;
2608
Jamie Madilla0a9e122015-09-02 15:54:30 -04002609 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002610
2611 // TODO(jmadill): any caps validation here?
2612
2613 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2614 outputVariableIndex++)
2615 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002616 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002617
2618 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2619 if (outputVariable.isBuiltIn())
2620 continue;
2621
2622 // Since multiple output locations must be specified, use 0 for non-specified locations.
2623 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2624
2625 ASSERT(outputVariable.staticUse);
2626
2627 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2628 elementIndex++)
2629 {
2630 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002631 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002632 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002633 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002634 VariableLocation(outputVariable.name, element, outputVariableIndex);
2635 }
2636 }
2637}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002638
Jamie Madilla2c74982016-12-12 11:20:42 -05002639bool Program::flattenUniformsAndCheckCapsForShader(const Shader &shader,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002640 GLuint maxUniformComponents,
2641 GLuint maxTextureImageUnits,
2642 const std::string &componentsErrorMessage,
2643 const std::string &samplerErrorMessage,
2644 std::vector<LinkedUniform> &samplerUniforms,
2645 InfoLog &infoLog)
2646{
2647 VectorAndSamplerCount vasCount;
2648 for (const sh::Uniform &uniform : shader.getUniforms())
2649 {
2650 if (uniform.staticUse)
2651 {
2652 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2653 }
2654 }
2655
2656 if (vasCount.vectorCount > maxUniformComponents)
2657 {
2658 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2659 return false;
2660 }
2661
2662 if (vasCount.samplerCount > maxTextureImageUnits)
2663 {
2664 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2665 return false;
2666 }
2667
2668 return true;
2669}
2670
Jamie Madill62d31cb2015-09-11 13:25:51 -04002671bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2672{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002673 std::vector<LinkedUniform> samplerUniforms;
2674
Martin Radev4c4c8e72016-08-04 12:25:34 +03002675 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002676 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002677 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002678
2679 // TODO (mradev): check whether we need finer-grained component counting
2680 if (!flattenUniformsAndCheckCapsForShader(
2681 *computeShader, caps.maxComputeUniformComponents / 4,
2682 caps.maxComputeTextureImageUnits,
2683 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2684 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2685 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002686 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002687 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002688 }
2689 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002690 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002691 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002692 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002693
Martin Radev4c4c8e72016-08-04 12:25:34 +03002694 if (!flattenUniformsAndCheckCapsForShader(
2695 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2696 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2697 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2698 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002699 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002700 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002701 }
Jamie Madilla2c74982016-12-12 11:20:42 -05002702 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002703
Martin Radev4c4c8e72016-08-04 12:25:34 +03002704 if (!flattenUniformsAndCheckCapsForShader(
2705 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2706 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2707 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2708 infoLog))
2709 {
2710 return false;
2711 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002712 }
2713
Jamie Madille7d84322017-01-10 18:21:59 -05002714 mState.mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
2715 mState.mSamplerUniformRange.end =
2716 mState.mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002717
Jamie Madill48ef11b2016-04-27 15:21:52 -04002718 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002719
Jamie Madille7d84322017-01-10 18:21:59 -05002720 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2721 for (const auto &samplerUniform : samplerUniforms)
2722 {
2723 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2724 mState.mSamplerBindings.emplace_back(
2725 SamplerBinding(textureType, samplerUniform.elementCount()));
2726 }
2727
Jamie Madill62d31cb2015-09-11 13:25:51 -04002728 return true;
2729}
2730
2731Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002732 const std::string &fullName,
2733 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002734{
2735 VectorAndSamplerCount vectorAndSamplerCount;
2736
2737 if (uniform.isStruct())
2738 {
2739 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2740 {
2741 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2742
2743 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2744 {
2745 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2746 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2747
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002748 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002749 }
2750 }
2751
2752 return vectorAndSamplerCount;
2753 }
2754
2755 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002756 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002757 if (!UniformInList(mState.getUniforms(), fullName) &&
2758 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002759 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002760 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
2761 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002762 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002763
2764 // Store sampler uniforms separately, so we'll append them to the end of the list.
2765 if (isSampler)
2766 {
2767 samplerUniforms->push_back(linkedUniform);
2768 }
2769 else
2770 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002771 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002772 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002773 }
2774
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002775 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002776
2777 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2778 // Likewise, don't count "real" uniforms towards sampler count.
2779 vectorAndSamplerCount.vectorCount =
2780 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002781 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002782
2783 return vectorAndSamplerCount;
2784}
2785
2786void Program::gatherInterfaceBlockInfo()
2787{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002788 ASSERT(mState.mUniformBlocks.empty());
2789
2790 if (mState.mAttachedComputeShader)
2791 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002792 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002793
2794 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2795 {
2796
2797 // Only 'packed' blocks are allowed to be considered inactive.
2798 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2799 continue;
2800
Jamie Madilla2c74982016-12-12 11:20:42 -05002801 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002802 {
2803 if (block.name == computeBlock.name)
2804 {
2805 block.computeStaticUse = computeBlock.staticUse;
2806 }
2807 }
2808
2809 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2810 }
2811 return;
2812 }
2813
Jamie Madill62d31cb2015-09-11 13:25:51 -04002814 std::set<std::string> visitedList;
2815
Jamie Madilla2c74982016-12-12 11:20:42 -05002816 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002817
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2819 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002820 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002821 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2822 continue;
2823
2824 if (visitedList.count(vertexBlock.name) > 0)
2825 continue;
2826
2827 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2828 visitedList.insert(vertexBlock.name);
2829 }
2830
Jamie Madilla2c74982016-12-12 11:20:42 -05002831 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002832
2833 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2834 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002835 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002836 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2837 continue;
2838
2839 if (visitedList.count(fragmentBlock.name) > 0)
2840 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002841 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842 {
2843 if (block.name == fragmentBlock.name)
2844 {
2845 block.fragmentStaticUse = fragmentBlock.staticUse;
2846 }
2847 }
2848
2849 continue;
2850 }
2851
2852 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2853 visitedList.insert(fragmentBlock.name);
2854 }
2855}
2856
Jamie Madill4a3c2342015-10-08 12:58:45 -04002857template <typename VarT>
2858void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2859 const std::string &prefix,
2860 int blockIndex)
2861{
2862 for (const VarT &field : fields)
2863 {
2864 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2865
2866 if (field.isStruct())
2867 {
2868 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2869 {
2870 const std::string uniformElementName =
2871 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2872 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2873 }
2874 }
2875 else
2876 {
2877 // If getBlockMemberInfo returns false, the uniform is optimized out.
2878 sh::BlockMemberInfo memberInfo;
2879 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2880 {
2881 continue;
2882 }
2883
2884 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2885 blockIndex, memberInfo);
2886
2887 // Since block uniforms have no location, we don't need to store them in the uniform
2888 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002889 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002890 }
2891 }
2892}
2893
Jamie Madill62d31cb2015-09-11 13:25:51 -04002894void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2895{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002896 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002897 size_t blockSize = 0;
2898
2899 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002900 std::stringstream blockNameStr;
2901 blockNameStr << interfaceBlock.name;
2902 if (interfaceBlock.arraySize > 0)
2903 {
2904 blockNameStr << "[0]";
2905 }
2906 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002907 {
2908 return;
2909 }
2910
2911 // Track the first and last uniform index to determine the range of active uniforms in the
2912 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002913 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002914 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002915 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002916
2917 std::vector<unsigned int> blockUniformIndexes;
2918 for (size_t blockUniformIndex = firstBlockUniformIndex;
2919 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2920 {
2921 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2922 }
2923
2924 if (interfaceBlock.arraySize > 0)
2925 {
2926 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2927 {
2928 UniformBlock block(interfaceBlock.name, true, arrayElement);
2929 block.memberUniformIndexes = blockUniformIndexes;
2930
Martin Radev4c4c8e72016-08-04 12:25:34 +03002931 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002932 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002933 case GL_VERTEX_SHADER:
2934 {
2935 block.vertexStaticUse = interfaceBlock.staticUse;
2936 break;
2937 }
2938 case GL_FRAGMENT_SHADER:
2939 {
2940 block.fragmentStaticUse = interfaceBlock.staticUse;
2941 break;
2942 }
2943 case GL_COMPUTE_SHADER:
2944 {
2945 block.computeStaticUse = interfaceBlock.staticUse;
2946 break;
2947 }
2948 default:
2949 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002950 }
2951
Qin Jiajia0350a642016-11-01 17:01:51 +08002952 // Since all block elements in an array share the same active uniforms, they will all be
2953 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2954 // here we will add every block element in the array.
2955 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002956 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002957 }
2958 }
2959 else
2960 {
2961 UniformBlock block(interfaceBlock.name, false, 0);
2962 block.memberUniformIndexes = blockUniformIndexes;
2963
Martin Radev4c4c8e72016-08-04 12:25:34 +03002964 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002965 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002966 case GL_VERTEX_SHADER:
2967 {
2968 block.vertexStaticUse = interfaceBlock.staticUse;
2969 break;
2970 }
2971 case GL_FRAGMENT_SHADER:
2972 {
2973 block.fragmentStaticUse = interfaceBlock.staticUse;
2974 break;
2975 }
2976 case GL_COMPUTE_SHADER:
2977 {
2978 block.computeStaticUse = interfaceBlock.staticUse;
2979 break;
2980 }
2981 default:
2982 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002983 }
2984
Jamie Madill4a3c2342015-10-08 12:58:45 -04002985 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002986 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002987 }
2988}
2989
Jamie Madille7d84322017-01-10 18:21:59 -05002990template <>
2991void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2992 const uint8_t *destPointer,
2993 GLsizei clampedCount,
2994 const GLint *v)
2995{
2996 // Invalidate the validation cache only if we modify the sampler data.
2997 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2998 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2999 {
3000 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3001 std::vector<GLuint> *boundTextureUnits =
3002 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
3003
3004 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
3005 mCachedValidateSamplersResult.reset();
3006 }
3007}
3008
3009template <typename T>
3010void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3011 const uint8_t *destPointer,
3012 GLsizei clampedCount,
3013 const T *v)
3014{
3015}
3016
Jamie Madill62d31cb2015-09-11 13:25:51 -04003017template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003018GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003019{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003020 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3021 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003022 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
3023
Corentin Wallez15ac5342016-11-03 17:06:39 -04003024 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3025 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3026 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003027 GLsizei maxElementCount =
3028 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
3029
3030 GLsizei count = countIn;
3031 GLsizei clampedCount = count * vectorSize;
3032 if (clampedCount > maxElementCount)
3033 {
3034 clampedCount = maxElementCount;
3035 count = maxElementCount / vectorSize;
3036 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04003037
Jamie Madill62d31cb2015-09-11 13:25:51 -04003038 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
3039 {
3040 // Do a cast conversion for boolean types. From the spec:
3041 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
3042 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003043 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003044 {
3045 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
3046 }
3047 }
3048 else
3049 {
Jamie Madille7d84322017-01-10 18:21:59 -05003050 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003051 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003052 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003053
3054 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003055}
3056
3057template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003058GLsizei Program::setMatrixUniformInternal(GLint location,
3059 GLsizei count,
3060 GLboolean transpose,
3061 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003062{
3063 if (!transpose)
3064 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003065 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003066 }
3067
3068 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003069 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3070 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003071 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003072
3073 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3074 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3075 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3076 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3077
3078 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003079 {
3080 size_t elementOffset = element * rows * cols;
3081
3082 for (size_t row = 0; row < rows; ++row)
3083 {
3084 for (size_t col = 0; col < cols; ++col)
3085 {
3086 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3087 }
3088 }
3089 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003090
3091 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003092}
3093
3094template <typename DestT>
3095void Program::getUniformInternal(GLint location, DestT *dataOut) const
3096{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003097 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3098 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003099
3100 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3101
3102 GLenum componentType = VariableComponentType(uniform.type);
3103 if (componentType == GLTypeToGLenum<DestT>::value)
3104 {
3105 memcpy(dataOut, srcPointer, uniform.getElementSize());
3106 return;
3107 }
3108
Corentin Wallez6596c462016-03-17 17:26:58 -04003109 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003110
3111 switch (componentType)
3112 {
3113 case GL_INT:
3114 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3115 break;
3116 case GL_UNSIGNED_INT:
3117 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3118 break;
3119 case GL_BOOL:
3120 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3121 break;
3122 case GL_FLOAT:
3123 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3124 break;
3125 default:
3126 UNREACHABLE();
3127 }
3128}
Jamie Madilla2c74982016-12-12 11:20:42 -05003129} // namespace gl