blob: 52c2dfa6c3f430ac46491cddd5089ed7ef6de7b2 [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{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500257 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400258}
259
Jamie Madill48ef11b2016-04-27 15:21:52 -0400260const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500261{
262 return mLabel;
263}
264
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400266{
267 for (const LinkedUniform &linkedUniform : mUniforms)
268 {
269 if (linkedUniform.name == name)
270 {
271 return &linkedUniform;
272 }
273 }
274
275 return nullptr;
276}
277
Jamie Madill48ef11b2016-04-27 15:21:52 -0400278GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400279{
280 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500281 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400282
283 for (size_t location = 0; location < mUniformLocations.size(); ++location)
284 {
285 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400286 if (!uniformLocation.used)
287 {
288 continue;
289 }
290
291 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400292
293 if (uniform.name == baseName)
294 {
Geoff Langd8605522016-04-13 10:19:12 -0400295 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400296 {
Geoff Langd8605522016-04-13 10:19:12 -0400297 if (uniformLocation.element == subscript ||
298 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
299 {
300 return static_cast<GLint>(location);
301 }
302 }
303 else
304 {
305 if (subscript == GL_INVALID_INDEX)
306 {
307 return static_cast<GLint>(location);
308 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400309 }
310 }
311 }
312
313 return -1;
314}
315
Jamie Madille7d84322017-01-10 18:21:59 -0500316GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400317{
318 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500319 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400320
321 // The app is not allowed to specify array indices other than 0 for arrays of basic types
322 if (subscript != 0 && subscript != GL_INVALID_INDEX)
323 {
324 return GL_INVALID_INDEX;
325 }
326
327 for (size_t index = 0; index < mUniforms.size(); index++)
328 {
329 const LinkedUniform &uniform = mUniforms[index];
330 if (uniform.name == baseName)
331 {
332 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
333 {
334 return static_cast<GLuint>(index);
335 }
336 }
337 }
338
339 return GL_INVALID_INDEX;
340}
341
Jamie Madille7d84322017-01-10 18:21:59 -0500342GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
343{
344 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
345 return mUniformLocations[location].index;
346}
347
348Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
349{
350 GLuint index = getUniformIndexFromLocation(location);
351 if (!isSamplerUniformIndex(index))
352 {
353 return Optional<GLuint>::Invalid();
354 }
355
356 return getSamplerIndexFromUniformIndex(index);
357}
358
359bool ProgramState::isSamplerUniformIndex(GLuint index) const
360{
361 return index >= mSamplerUniformRange.start && index < mSamplerUniformRange.end;
362}
363
364GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
365{
366 ASSERT(isSamplerUniformIndex(uniformIndex));
367 return uniformIndex - mSamplerUniformRange.start;
368}
369
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500370Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400371 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400372 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500373 mLinked(false),
374 mDeleteStatus(false),
375 mRefCount(0),
376 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500377 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500378{
379 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000380
381 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500382 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383}
384
385Program::~Program()
386{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500387 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
388 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500389 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
Jamie Madill6c1f6712017-02-14 19:08:04 -0500392void Program::destroy(const Context *context)
393{
394 if (mState.mAttachedVertexShader != nullptr)
395 {
396 mState.mAttachedVertexShader->release(context);
397 mState.mAttachedVertexShader = nullptr;
398 }
399
400 if (mState.mAttachedFragmentShader != nullptr)
401 {
402 mState.mAttachedFragmentShader->release(context);
403 mState.mAttachedFragmentShader = nullptr;
404 }
405
406 if (mState.mAttachedComputeShader != nullptr)
407 {
408 mState.mAttachedComputeShader->release(context);
409 mState.mAttachedComputeShader = nullptr;
410 }
411
412 mProgram->destroy(rx::SafeGetImpl(context));
413}
414
Geoff Lang70d0f492015-12-10 17:45:46 -0500415void Program::setLabel(const std::string &label)
416{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400417 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500418}
419
420const std::string &Program::getLabel() const
421{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400422 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500423}
424
Jamie Madillef300b12016-10-07 15:12:09 -0400425void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300429 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430 {
Jamie Madillef300b12016-10-07 15:12:09 -0400431 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 mState.mAttachedVertexShader = shader;
433 mState.mAttachedVertexShader->addRef();
434 break;
435 }
436 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437 {
Jamie Madillef300b12016-10-07 15:12:09 -0400438 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300439 mState.mAttachedFragmentShader = shader;
440 mState.mAttachedFragmentShader->addRef();
441 break;
442 }
443 case GL_COMPUTE_SHADER:
444 {
Jamie Madillef300b12016-10-07 15:12:09 -0400445 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300446 mState.mAttachedComputeShader = shader;
447 mState.mAttachedComputeShader->addRef();
448 break;
449 }
450 default:
451 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453}
454
Jamie Madill6c1f6712017-02-14 19:08:04 -0500455bool Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300457 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300459 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300461 if (mState.mAttachedVertexShader != shader)
462 {
463 return false;
464 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465
Jamie Madill6c1f6712017-02-14 19:08:04 -0500466 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300467 mState.mAttachedVertexShader = nullptr;
468 break;
469 }
470 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300472 if (mState.mAttachedFragmentShader != shader)
473 {
474 return false;
475 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476
Jamie Madill6c1f6712017-02-14 19:08:04 -0500477 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300478 mState.mAttachedFragmentShader = nullptr;
479 break;
480 }
481 case GL_COMPUTE_SHADER:
482 {
483 if (mState.mAttachedComputeShader != shader)
484 {
485 return false;
486 }
487
Jamie Madill6c1f6712017-02-14 19:08:04 -0500488 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300489 mState.mAttachedComputeShader = nullptr;
490 break;
491 }
492 default:
493 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496 return true;
497}
498
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000499int Program::getAttachedShadersCount() const
500{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
502 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000503}
504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505void Program::bindAttributeLocation(GLuint index, const char *name)
506{
Geoff Langd8605522016-04-13 10:19:12 -0400507 mAttributeBindings.bindLocation(index, name);
508}
509
510void Program::bindUniformLocation(GLuint index, const char *name)
511{
512 // Bind the base uniform name only since array indices other than 0 cannot be bound
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000513 mUniformLocationBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514}
515
Sami Väisänen46eaa942016-06-29 10:26:37 +0300516void Program::bindFragmentInputLocation(GLint index, const char *name)
517{
518 mFragmentInputBindings.bindLocation(index, name);
519}
520
521BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
522{
523 BindingInfo ret;
524 ret.type = GL_NONE;
525 ret.valid = false;
526
527 const Shader *fragmentShader = mState.getAttachedFragmentShader();
528 ASSERT(fragmentShader);
529
530 // Find the actual fragment shader varying we're interested in
531 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
532
533 for (const auto &binding : mFragmentInputBindings)
534 {
535 if (binding.second != static_cast<GLuint>(index))
536 continue;
537
538 ret.valid = true;
539
540 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400541 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300542
543 for (const auto &in : inputs)
544 {
545 if (in.name == originalName)
546 {
547 if (in.isArray())
548 {
549 // The client wants to bind either "name" or "name[0]".
550 // GL ES 3.1 spec refers to active array names with language such as:
551 // "if the string identifies the base name of an active array, where the
552 // string would exactly match the name of the variable if the suffix "[0]"
553 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400554 if (arrayIndex == GL_INVALID_INDEX)
555 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300556
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400557 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300558 }
559 else
560 {
561 ret.name = in.mappedName;
562 }
563 ret.type = in.type;
564 return ret;
565 }
566 }
567 }
568
569 return ret;
570}
571
572void Program::pathFragmentInputGen(GLint index,
573 GLenum genMode,
574 GLint components,
575 const GLfloat *coeffs)
576{
577 // If the location is -1 then the command is silently ignored
578 if (index == -1)
579 return;
580
581 const auto &binding = getFragmentInputBindingInfo(index);
582
583 // If the input doesn't exist then then the command is silently ignored
584 // This could happen through optimization for example, the shader translator
585 // decides that a variable is not actually being used and optimizes it away.
586 if (binding.name.empty())
587 return;
588
589 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
590}
591
Martin Radev4c4c8e72016-08-04 12:25:34 +0300592// The attached shaders are checked for linking errors by matching up their variables.
593// Uniform, input and output variables get collected.
594// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500595Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000596{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500597 const auto &data = context->getContextState();
598
Jamie Madill6c1f6712017-02-14 19:08:04 -0500599 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000600
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000601 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000602 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000603
Martin Radev4c4c8e72016-08-04 12:25:34 +0300604 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500605
Jamie Madill192745a2016-12-22 15:58:21 -0500606 auto vertexShader = mState.mAttachedVertexShader;
607 auto fragmentShader = mState.mAttachedFragmentShader;
608 auto computeShader = mState.mAttachedComputeShader;
609
610 bool isComputeShaderAttached = (computeShader != nullptr);
611 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300612 // Check whether we both have a compute and non-compute shaders attached.
613 // If there are of both types attached, then linking should fail.
614 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
615 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500616 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300617 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
618 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400619 }
620
Jamie Madill192745a2016-12-22 15:58:21 -0500621 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500622 {
Jamie Madill192745a2016-12-22 15:58:21 -0500623 if (!computeShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300624 {
625 mInfoLog << "Attached compute shader is not compiled.";
626 return NoError();
627 }
Jamie Madill192745a2016-12-22 15:58:21 -0500628 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300629
Jamie Madill192745a2016-12-22 15:58:21 -0500630 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300631
632 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
633 // If the work group size is not specified, a link time error should occur.
634 if (!mState.mComputeShaderLocalSize.isDeclared())
635 {
636 mInfoLog << "Work group size is not specified.";
637 return NoError();
638 }
639
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000640 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641 {
642 return NoError();
643 }
644
645 if (!linkUniformBlocks(mInfoLog, caps))
646 {
647 return NoError();
648 }
649
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500650 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
651 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), noPacking, mInfoLog),
652 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500653 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300654 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500655 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300656 }
657 }
658 else
659 {
Jamie Madill192745a2016-12-22 15:58:21 -0500660 if (!fragmentShader || !fragmentShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300661 {
662 return NoError();
663 }
Jamie Madill192745a2016-12-22 15:58:21 -0500664 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665
Jamie Madill192745a2016-12-22 15:58:21 -0500666 if (!vertexShader || !vertexShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300667 {
668 return NoError();
669 }
Jamie Madill192745a2016-12-22 15:58:21 -0500670 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300671
Jamie Madill192745a2016-12-22 15:58:21 -0500672 if (fragmentShader->getShaderVersion() != vertexShader->getShaderVersion())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300673 {
674 mInfoLog << "Fragment shader version does not match vertex shader version.";
675 return NoError();
676 }
677
Jamie Madilleb979bf2016-11-15 12:28:46 -0500678 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679 {
680 return NoError();
681 }
682
Jamie Madill192745a2016-12-22 15:58:21 -0500683 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300684 {
685 return NoError();
686 }
687
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000688 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 {
690 return NoError();
691 }
692
693 if (!linkUniformBlocks(mInfoLog, caps))
694 {
695 return NoError();
696 }
697
698 const auto &mergedVaryings = getMergedVaryings();
699
700 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
701 {
702 return NoError();
703 }
704
705 linkOutputVariables();
706
Jamie Madill192745a2016-12-22 15:58:21 -0500707 // Validate we can pack the varyings.
708 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
709
710 // Map the varyings to the register file
711 // In WebGL, we use a slightly different handling for packing variables.
712 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
713 : PackMode::ANGLE_RELAXED;
714 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
715 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
716 mState.getTransformFeedbackVaryingNames()))
717 {
718 return NoError();
719 }
720
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500721 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), varyingPacking, mInfoLog),
722 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500723 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500725 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300726 }
727
728 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500729 }
730
Jamie Madill4a3c2342015-10-08 12:58:45 -0400731 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400732
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000734}
735
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000736// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500737void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400739 mState.mAttributes.clear();
740 mState.mActiveAttribLocationsMask.reset();
741 mState.mTransformFeedbackVaryingVars.clear();
742 mState.mUniforms.clear();
743 mState.mUniformLocations.clear();
744 mState.mUniformBlocks.clear();
745 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300746 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500747 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500748
Geoff Lang7dd2e102014-11-10 15:19:26 -0500749 mValidated = false;
750
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000751 mLinked = false;
752}
753
Geoff Lange1a27752015-10-05 13:16:04 -0400754bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000755{
756 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757}
758
Jamie Madilla2c74982016-12-12 11:20:42 -0500759Error Program::loadBinary(const Context *context,
760 GLenum binaryFormat,
761 const void *binary,
762 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000763{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500764 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000765
Geoff Lang7dd2e102014-11-10 15:19:26 -0500766#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800767 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500768#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400769 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
770 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000771 {
Jamie Madillf6113162015-05-07 11:49:21 -0400772 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800773 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500774 }
775
Geoff Langc46cc2f2015-10-01 17:16:20 -0400776 BinaryInputStream stream(binary, length);
777
Jamie Madilla2c74982016-12-12 11:20:42 -0500778 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
779 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
780 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
781 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500782 {
Jamie Madillf6113162015-05-07 11:49:21 -0400783 mInfoLog << "Invalid program binary version.";
He Yunchaoacd18982017-01-04 10:46:42 +0800784 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500785 }
786
Jamie Madilla2c74982016-12-12 11:20:42 -0500787 int majorVersion = stream.readInt<int>();
788 int minorVersion = stream.readInt<int>();
789 if (majorVersion != context->getClientMajorVersion() ||
790 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500791 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500792 mInfoLog << "Cannot load program binaries across different ES context versions.";
He Yunchaoacd18982017-01-04 10:46:42 +0800793 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500794 }
795
Martin Radev4c4c8e72016-08-04 12:25:34 +0300796 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
797 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
798 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
799
Jamie Madill63805b42015-08-25 13:17:39 -0400800 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
801 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400802 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500803
Jamie Madill3da79b72015-04-27 11:09:17 -0400804 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400805 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400806 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
807 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400808 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400809 LoadShaderVar(&stream, &attrib);
810 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400812 }
813
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400815 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
817 {
818 LinkedUniform uniform;
819 LoadShaderVar(&stream, &uniform);
820
821 uniform.blockIndex = stream.readInt<int>();
822 uniform.blockInfo.offset = stream.readInt<int>();
823 uniform.blockInfo.arrayStride = stream.readInt<int>();
824 uniform.blockInfo.matrixStride = stream.readInt<int>();
825 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
826
Jamie Madill48ef11b2016-04-27 15:21:52 -0400827 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400828 }
829
830 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400831 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400832 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
833 uniformIndexIndex++)
834 {
835 VariableLocation variable;
836 stream.readString(&variable.name);
837 stream.readInt(&variable.element);
838 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400839 stream.readBool(&variable.used);
840 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400841
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400843 }
844
845 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400846 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400847 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
848 ++uniformBlockIndex)
849 {
850 UniformBlock uniformBlock;
851 stream.readString(&uniformBlock.name);
852 stream.readBool(&uniformBlock.isArray);
853 stream.readInt(&uniformBlock.arrayElement);
854 stream.readInt(&uniformBlock.dataSize);
855 stream.readBool(&uniformBlock.vertexStaticUse);
856 stream.readBool(&uniformBlock.fragmentStaticUse);
857
858 unsigned int numMembers = stream.readInt<unsigned int>();
859 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
860 {
861 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
862 }
863
Jamie Madill48ef11b2016-04-27 15:21:52 -0400864 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400865 }
866
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500867 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
868 ++bindingIndex)
869 {
870 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
871 mState.mActiveUniformBlockBindings.set(bindingIndex,
872 mState.mUniformBlockBindings[bindingIndex] != 0);
873 }
874
Brandon Jones1048ea72015-10-06 15:34:52 -0700875 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400876 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700877 for (unsigned int transformFeedbackVaryingIndex = 0;
878 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
879 ++transformFeedbackVaryingIndex)
880 {
881 sh::Varying varying;
882 stream.readInt(&varying.arraySize);
883 stream.readInt(&varying.type);
884 stream.readString(&varying.name);
885
Jamie Madill48ef11b2016-04-27 15:21:52 -0400886 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700887 }
888
Jamie Madill48ef11b2016-04-27 15:21:52 -0400889 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400890
Jamie Madill80a6fc02015-08-21 16:53:16 -0400891 unsigned int outputVarCount = stream.readInt<unsigned int>();
892 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
893 {
894 int locationIndex = stream.readInt<int>();
895 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400896 stream.readInt(&locationData.element);
897 stream.readInt(&locationData.index);
898 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400899 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400900 }
901
Jamie Madille7d84322017-01-10 18:21:59 -0500902 stream.readInt(&mState.mSamplerUniformRange.start);
903 stream.readInt(&mState.mSamplerUniformRange.end);
904
905 unsigned int samplerCount = stream.readInt<unsigned int>();
906 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
907 {
908 GLenum textureType = stream.readInt<GLenum>();
909 size_t bindingCount = stream.readInt<size_t>();
910 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
911 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400912
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500913 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000914
Jamie Madillb0a838b2016-11-13 20:02:12 -0500915 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500916#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500917}
918
Jamie Madilla2c74982016-12-12 11:20:42 -0500919Error Program::saveBinary(const Context *context,
920 GLenum *binaryFormat,
921 void *binary,
922 GLsizei bufSize,
923 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500924{
925 if (binaryFormat)
926 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400927 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500928 }
929
930 BinaryOutputStream stream;
931
Geoff Lang7dd2e102014-11-10 15:19:26 -0500932 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
933
Jamie Madilla2c74982016-12-12 11:20:42 -0500934 // nullptr context is supported when computing binary length.
935 if (context)
936 {
937 stream.writeInt(context->getClientVersion().major);
938 stream.writeInt(context->getClientVersion().minor);
939 }
940 else
941 {
942 stream.writeInt(2);
943 stream.writeInt(0);
944 }
945
Martin Radev4c4c8e72016-08-04 12:25:34 +0300946 stream.writeInt(mState.mComputeShaderLocalSize[0]);
947 stream.writeInt(mState.mComputeShaderLocalSize[1]);
948 stream.writeInt(mState.mComputeShaderLocalSize[2]);
949
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951
Jamie Madill48ef11b2016-04-27 15:21:52 -0400952 stream.writeInt(mState.mAttributes.size());
953 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400954 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400955 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400956 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400957 }
958
Jamie Madill48ef11b2016-04-27 15:21:52 -0400959 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500960 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400961 {
962 WriteShaderVar(&stream, uniform);
963
964 // FIXME: referenced
965
966 stream.writeInt(uniform.blockIndex);
967 stream.writeInt(uniform.blockInfo.offset);
968 stream.writeInt(uniform.blockInfo.arrayStride);
969 stream.writeInt(uniform.blockInfo.matrixStride);
970 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
971 }
972
Jamie Madill48ef11b2016-04-27 15:21:52 -0400973 stream.writeInt(mState.mUniformLocations.size());
974 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400975 {
976 stream.writeString(variable.name);
977 stream.writeInt(variable.element);
978 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400979 stream.writeInt(variable.used);
980 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400981 }
982
Jamie Madill48ef11b2016-04-27 15:21:52 -0400983 stream.writeInt(mState.mUniformBlocks.size());
984 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400985 {
986 stream.writeString(uniformBlock.name);
987 stream.writeInt(uniformBlock.isArray);
988 stream.writeInt(uniformBlock.arrayElement);
989 stream.writeInt(uniformBlock.dataSize);
990
991 stream.writeInt(uniformBlock.vertexStaticUse);
992 stream.writeInt(uniformBlock.fragmentStaticUse);
993
994 stream.writeInt(uniformBlock.memberUniformIndexes.size());
995 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
996 {
997 stream.writeInt(memberUniformIndex);
998 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400999 }
1000
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001001 for (GLuint binding : mState.mUniformBlockBindings)
1002 {
1003 stream.writeInt(binding);
1004 }
1005
Jamie Madill48ef11b2016-04-27 15:21:52 -04001006 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
1007 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -07001008 {
1009 stream.writeInt(varying.arraySize);
1010 stream.writeInt(varying.type);
1011 stream.writeString(varying.name);
1012 }
1013
Jamie Madill48ef11b2016-04-27 15:21:52 -04001014 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001015
Jamie Madill48ef11b2016-04-27 15:21:52 -04001016 stream.writeInt(mState.mOutputVariables.size());
1017 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -04001018 {
1019 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -04001020 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001021 stream.writeInt(outputPair.second.index);
1022 stream.writeString(outputPair.second.name);
1023 }
1024
Jamie Madille7d84322017-01-10 18:21:59 -05001025 stream.writeInt(mState.mSamplerUniformRange.start);
1026 stream.writeInt(mState.mSamplerUniformRange.end);
1027
1028 stream.writeInt(mState.mSamplerBindings.size());
1029 for (const auto &samplerBinding : mState.mSamplerBindings)
1030 {
1031 stream.writeInt(samplerBinding.textureType);
1032 stream.writeInt(samplerBinding.boundTextureUnits.size());
1033 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001034
Jamie Madilla2c74982016-12-12 11:20:42 -05001035 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001036
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001037 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001038 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039
1040 if (streamLength > bufSize)
1041 {
1042 if (length)
1043 {
1044 *length = 0;
1045 }
1046
1047 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1048 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1049 // sizes and then copy it.
1050 return Error(GL_INVALID_OPERATION);
1051 }
1052
1053 if (binary)
1054 {
1055 char *ptr = reinterpret_cast<char*>(binary);
1056
Jamie Madill48ef11b2016-04-27 15:21:52 -04001057 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001058 ptr += streamLength;
1059
1060 ASSERT(ptr - streamLength == binary);
1061 }
1062
1063 if (length)
1064 {
1065 *length = streamLength;
1066 }
1067
He Yunchaoacd18982017-01-04 10:46:42 +08001068 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069}
1070
1071GLint Program::getBinaryLength() const
1072{
1073 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001074 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001075 if (error.isError())
1076 {
1077 return 0;
1078 }
1079
1080 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001081}
1082
Geoff Langc5629752015-12-07 16:29:04 -05001083void Program::setBinaryRetrievableHint(bool retrievable)
1084{
1085 // TODO(jmadill) : replace with dirty bits
1086 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001087 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001088}
1089
1090bool Program::getBinaryRetrievableHint() const
1091{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001092 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001093}
1094
Jamie Madill6c1f6712017-02-14 19:08:04 -05001095void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001096{
1097 mRefCount--;
1098
1099 if (mRefCount == 0 && mDeleteStatus)
1100 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001101 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001102 }
1103}
1104
1105void Program::addRef()
1106{
1107 mRefCount++;
1108}
1109
1110unsigned int Program::getRefCount() const
1111{
1112 return mRefCount;
1113}
1114
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001115int Program::getInfoLogLength() const
1116{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001117 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001118}
1119
Geoff Lange1a27752015-10-05 13:16:04 -04001120void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001121{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001122 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001123}
1124
Geoff Lange1a27752015-10-05 13:16:04 -04001125void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001126{
1127 int total = 0;
1128
Martin Radev4c4c8e72016-08-04 12:25:34 +03001129 if (mState.mAttachedComputeShader)
1130 {
1131 if (total < maxCount)
1132 {
1133 shaders[total] = mState.mAttachedComputeShader->getHandle();
1134 total++;
1135 }
1136 }
1137
Jamie Madill48ef11b2016-04-27 15:21:52 -04001138 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001139 {
1140 if (total < maxCount)
1141 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001142 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001143 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001144 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001145 }
1146
Jamie Madill48ef11b2016-04-27 15:21:52 -04001147 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001148 {
1149 if (total < maxCount)
1150 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001151 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001152 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001153 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001154 }
1155
1156 if (count)
1157 {
1158 *count = total;
1159 }
1160}
1161
Geoff Lange1a27752015-10-05 13:16:04 -04001162GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001164 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001165 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001166 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001167 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001168 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 }
1170 }
1171
Austin Kinrossb8af7232015-03-16 22:33:25 -07001172 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001173}
1174
Jamie Madill63805b42015-08-25 13:17:39 -04001175bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001176{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001177 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1178 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001179}
1180
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001181void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1182{
Jamie Madillc349ec02015-08-21 16:53:12 -04001183 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184 {
1185 if (bufsize > 0)
1186 {
1187 name[0] = '\0';
1188 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001190 if (length)
1191 {
1192 *length = 0;
1193 }
1194
1195 *type = GL_NONE;
1196 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001197 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001198 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001199
1200 size_t attributeIndex = 0;
1201
Jamie Madill48ef11b2016-04-27 15:21:52 -04001202 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001203 {
1204 // Skip over inactive attributes
1205 if (attribute.staticUse)
1206 {
1207 if (static_cast<size_t>(index) == attributeIndex)
1208 {
1209 break;
1210 }
1211 attributeIndex++;
1212 }
1213 }
1214
Jamie Madill48ef11b2016-04-27 15:21:52 -04001215 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1216 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001217
1218 if (bufsize > 0)
1219 {
1220 const char *string = attrib.name.c_str();
1221
1222 strncpy(name, string, bufsize);
1223 name[bufsize - 1] = '\0';
1224
1225 if (length)
1226 {
1227 *length = static_cast<GLsizei>(strlen(name));
1228 }
1229 }
1230
1231 // Always a single 'type' instance
1232 *size = 1;
1233 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001234}
1235
Geoff Lange1a27752015-10-05 13:16:04 -04001236GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001237{
Jamie Madillc349ec02015-08-21 16:53:12 -04001238 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001239 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001240 return 0;
1241 }
1242
1243 GLint count = 0;
1244
Jamie Madill48ef11b2016-04-27 15:21:52 -04001245 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001246 {
1247 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001248 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001249
1250 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001251}
1252
Geoff Lange1a27752015-10-05 13:16:04 -04001253GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254{
Jamie Madillc349ec02015-08-21 16:53:12 -04001255 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001256 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001257 return 0;
1258 }
1259
1260 size_t maxLength = 0;
1261
Jamie Madill48ef11b2016-04-27 15:21:52 -04001262 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001263 {
1264 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001266 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001268 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269
Jamie Madillc349ec02015-08-21 16:53:12 -04001270 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271}
1272
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273GLint Program::getFragDataLocation(const std::string &name) const
1274{
1275 std::string baseName(name);
1276 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001277 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001278 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001279 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1281 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001282 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001284 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001286}
1287
Geoff Lange1a27752015-10-05 13:16:04 -04001288void Program::getActiveUniform(GLuint index,
1289 GLsizei bufsize,
1290 GLsizei *length,
1291 GLint *size,
1292 GLenum *type,
1293 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001294{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001296 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001297 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001298 ASSERT(index < mState.mUniforms.size());
1299 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001300
1301 if (bufsize > 0)
1302 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001303 std::string string = uniform.name;
1304 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305 {
1306 string += "[0]";
1307 }
1308
1309 strncpy(name, string.c_str(), bufsize);
1310 name[bufsize - 1] = '\0';
1311
1312 if (length)
1313 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001314 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001315 }
1316 }
1317
Jamie Madill62d31cb2015-09-11 13:25:51 -04001318 *size = uniform.elementCount();
1319 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001320 }
1321 else
1322 {
1323 if (bufsize > 0)
1324 {
1325 name[0] = '\0';
1326 }
1327
1328 if (length)
1329 {
1330 *length = 0;
1331 }
1332
1333 *size = 0;
1334 *type = GL_NONE;
1335 }
1336}
1337
Geoff Lange1a27752015-10-05 13:16:04 -04001338GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001339{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001341 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001342 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001343 }
1344 else
1345 {
1346 return 0;
1347 }
1348}
1349
Geoff Lange1a27752015-10-05 13:16:04 -04001350GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001351{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001352 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353
1354 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001355 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001356 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001358 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001360 size_t length = uniform.name.length() + 1u;
1361 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362 {
1363 length += 3; // Counting in "[0]".
1364 }
1365 maxLength = std::max(length, maxLength);
1366 }
1367 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001368 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001369
Jamie Madill62d31cb2015-09-11 13:25:51 -04001370 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371}
1372
1373GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1374{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001375 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001376 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001378 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1380 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1381 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1382 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1383 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1384 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1385 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1386 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1387 default:
1388 UNREACHABLE();
1389 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001390 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391 return 0;
1392}
1393
1394bool Program::isValidUniformLocation(GLint location) const
1395{
Jamie Madille2e406c2016-06-02 13:04:10 -04001396 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001397 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1398 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001399}
1400
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001403 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001404 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405}
1406
Jamie Madillac4e9c32017-01-13 14:07:12 -05001407const VariableLocation &Program::getUniformLocation(GLint location) const
1408{
1409 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1410 return mState.mUniformLocations[location];
1411}
1412
1413const std::vector<VariableLocation> &Program::getUniformLocations() const
1414{
1415 return mState.mUniformLocations;
1416}
1417
1418const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1419{
1420 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1421 return mState.mUniforms[index];
1422}
1423
Jamie Madill62d31cb2015-09-11 13:25:51 -04001424GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001426 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427}
1428
Jamie Madill62d31cb2015-09-11 13:25:51 -04001429GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430{
Jamie Madille7d84322017-01-10 18:21:59 -05001431 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432}
1433
1434void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1435{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001436 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1437 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438}
1439
1440void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1441{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001442 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1443 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444}
1445
1446void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1447{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001448 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1449 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450}
1451
1452void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1453{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001454 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1455 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456}
1457
1458void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1459{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001460 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1461 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462}
1463
1464void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1465{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001466 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1467 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468}
1469
1470void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1471{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001472 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1473 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
1476void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1477{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001478 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1479 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
1482void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1483{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001484 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1485 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486}
1487
1488void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1489{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001490 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1491 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492}
1493
1494void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1495{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001496 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1497 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498}
1499
1500void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1501{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001502 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1503 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
1506void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1507{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001508 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1509 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510}
1511
1512void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1513{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001514 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1515 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001516}
1517
1518void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1519{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001520 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1521 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522}
1523
1524void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1525{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001526 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1527 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528}
1529
1530void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1531{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001532 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1533 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001534}
1535
1536void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1537{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001538 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1539 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540}
1541
1542void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1543{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001544 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1545 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001546}
1547
1548void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1549{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001550 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1551 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552}
1553
1554void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1555{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001556 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1557 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001558}
1559
Geoff Lange1a27752015-10-05 13:16:04 -04001560void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001561{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001563}
1564
Geoff Lange1a27752015-10-05 13:16:04 -04001565void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001566{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001567 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568}
1569
Geoff Lange1a27752015-10-05 13:16:04 -04001570void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001571{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001572 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001573}
1574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001575void Program::flagForDeletion()
1576{
1577 mDeleteStatus = true;
1578}
1579
1580bool Program::isFlaggedForDeletion() const
1581{
1582 return mDeleteStatus;
1583}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001584
Brandon Jones43a53e22014-08-28 16:23:22 -07001585void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001586{
1587 mInfoLog.reset();
1588
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001590 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001591 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001592 }
1593 else
1594 {
Jamie Madillf6113162015-05-07 11:49:21 -04001595 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001596 }
1597}
1598
Geoff Lang7dd2e102014-11-10 15:19:26 -05001599bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1600{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001601 // Skip cache if we're using an infolog, so we get the full error.
1602 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1603 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1604 {
1605 return mCachedValidateSamplersResult.value();
1606 }
1607
1608 if (mTextureUnitTypesCache.empty())
1609 {
1610 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1611 }
1612 else
1613 {
1614 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1615 }
1616
1617 // if any two active samplers in a program are of different types, but refer to the same
1618 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1619 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001620 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001621 {
Jamie Madille7d84322017-01-10 18:21:59 -05001622 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001623
Jamie Madille7d84322017-01-10 18:21:59 -05001624 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001625 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001626 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1627 {
1628 if (infoLog)
1629 {
1630 (*infoLog) << "Sampler uniform (" << textureUnit
1631 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1632 << caps.maxCombinedTextureImageUnits << ")";
1633 }
1634
1635 mCachedValidateSamplersResult = false;
1636 return false;
1637 }
1638
1639 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1640 {
1641 if (textureType != mTextureUnitTypesCache[textureUnit])
1642 {
1643 if (infoLog)
1644 {
1645 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1646 "image unit ("
1647 << textureUnit << ").";
1648 }
1649
1650 mCachedValidateSamplersResult = false;
1651 return false;
1652 }
1653 }
1654 else
1655 {
1656 mTextureUnitTypesCache[textureUnit] = textureType;
1657 }
1658 }
1659 }
1660
1661 mCachedValidateSamplersResult = true;
1662 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663}
1664
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001665bool Program::isValidated() const
1666{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 return mValidated;
1668}
1669
Geoff Lange1a27752015-10-05 13:16:04 -04001670GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001672 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673}
1674
1675void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1676{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001677 ASSERT(
1678 uniformBlockIndex <
1679 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680
Jamie Madill48ef11b2016-04-27 15:21:52 -04001681 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682
1683 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001684 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001685 std::string string = uniformBlock.name;
1686
Jamie Madill62d31cb2015-09-11 13:25:51 -04001687 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001689 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001690 }
1691
1692 strncpy(uniformBlockName, string.c_str(), bufSize);
1693 uniformBlockName[bufSize - 1] = '\0';
1694
1695 if (length)
1696 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001697 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699 }
1700}
1701
Geoff Lange1a27752015-10-05 13:16:04 -04001702GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001703{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704 int maxLength = 0;
1705
1706 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001707 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001708 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1710 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001711 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712 if (!uniformBlock.name.empty())
1713 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001714 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001715
1716 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718
1719 maxLength = std::max(length + arrayLength, maxLength);
1720 }
1721 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001722 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001723
1724 return maxLength;
1725}
1726
Geoff Lange1a27752015-10-05 13:16:04 -04001727GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001729 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001730 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001731
Jamie Madill48ef11b2016-04-27 15:21:52 -04001732 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001733 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1734 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001735 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001736 if (uniformBlock.name == baseName)
1737 {
1738 const bool arrayElementZero =
1739 (subscript == GL_INVALID_INDEX &&
1740 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1741 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1742 {
1743 return blockIndex;
1744 }
1745 }
1746 }
1747
1748 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001749}
1750
Jamie Madill62d31cb2015-09-11 13:25:51 -04001751const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001752{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001753 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1754 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001755}
1756
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001757void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1758{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001759 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001760 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001761 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001762}
1763
1764GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1765{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001766 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001767}
1768
1769void Program::resetUniformBlockBindings()
1770{
1771 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1772 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001773 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001774 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001775 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001776}
1777
Geoff Lang48dcae72014-02-05 16:28:24 -05001778void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1779{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001780 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001781 for (GLsizei i = 0; i < count; i++)
1782 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001783 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001784 }
1785
Jamie Madill48ef11b2016-04-27 15:21:52 -04001786 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001787}
1788
1789void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1790{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001792 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001793 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1794 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001795 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1796 if (length)
1797 {
1798 *length = lastNameIdx;
1799 }
1800 if (size)
1801 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001802 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001803 }
1804 if (type)
1805 {
1806 *type = varying.type;
1807 }
1808 if (name)
1809 {
1810 memcpy(name, varying.name.c_str(), lastNameIdx);
1811 name[lastNameIdx] = '\0';
1812 }
1813 }
1814}
1815
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001816GLsizei Program::getTransformFeedbackVaryingCount() const
1817{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001819 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001820 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001821 }
1822 else
1823 {
1824 return 0;
1825 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001826}
1827
1828GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1829{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001831 {
1832 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001833 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001834 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001835 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1836 }
1837
1838 return maxSize;
1839 }
1840 else
1841 {
1842 return 0;
1843 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001844}
1845
1846GLenum Program::getTransformFeedbackBufferMode() const
1847{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001848 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849}
1850
Jamie Madill192745a2016-12-22 15:58:21 -05001851bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852{
Jamie Madill192745a2016-12-22 15:58:21 -05001853 const Shader *vertexShader = mState.mAttachedVertexShader;
1854 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1855
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001856 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1857
Jamie Madill4cff2472015-08-21 16:53:18 -04001858 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1859 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001860
Sami Väisänen46eaa942016-06-29 10:26:37 +03001861 std::map<GLuint, std::string> staticFragmentInputLocations;
1862
Jamie Madill4cff2472015-08-21 16:53:18 -04001863 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865 bool matched = false;
1866
1867 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001868 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 {
1870 continue;
1871 }
1872
Jamie Madill4cff2472015-08-21 16:53:18 -04001873 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001875 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001876 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001877 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001878 if (!linkValidateVaryings(infoLog, output.name, input, output,
1879 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 {
1881 return false;
1882 }
1883
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 matched = true;
1885 break;
1886 }
1887 }
1888
1889 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001890 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001892 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 return false;
1894 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001895
1896 // Check for aliased path rendering input bindings (if any).
1897 // If more than one binding refer statically to the same
1898 // location the link must fail.
1899
1900 if (!output.staticUse)
1901 continue;
1902
1903 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1904 if (inputBinding == -1)
1905 continue;
1906
1907 const auto it = staticFragmentInputLocations.find(inputBinding);
1908 if (it == std::end(staticFragmentInputLocations))
1909 {
1910 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1911 }
1912 else
1913 {
1914 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1915 << it->second;
1916 return false;
1917 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 }
1919
Yuly Novikov817232e2017-02-22 18:36:10 -05001920 if (!linkValidateBuiltInVaryings(infoLog))
1921 {
1922 return false;
1923 }
1924
Jamie Madillada9ecc2015-08-17 12:53:37 -04001925 // TODO(jmadill): verify no unmatched vertex varyings?
1926
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 return true;
1928}
1929
Martin Radev4c4c8e72016-08-04 12:25:34 +03001930bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001931{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001932 // Check that uniforms defined in the vertex and fragment shaders are identical
1933 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001934 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1935 const std::vector<sh::Uniform> &fragmentUniforms =
1936 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001937
Jamie Madillea918db2015-08-18 14:48:59 -04001938 for (const sh::Uniform &vertexUniform : vertexUniforms)
1939 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001940 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001941 }
1942
1943 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1944 {
1945 auto entry = linkedUniforms.find(fragmentUniform.name);
1946 if (entry != linkedUniforms.end())
1947 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001948 LinkedUniform *vertexUniform = &entry->second;
1949 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1950 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001951 {
1952 return false;
1953 }
1954 }
1955 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001956 return true;
1957}
1958
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001959bool Program::linkUniforms(InfoLog &infoLog,
1960 const Caps &caps,
1961 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001962{
1963 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1964 {
1965 ASSERT(mState.mAttachedComputeShader == nullptr);
1966 if (!validateVertexAndFragmentUniforms(infoLog))
1967 {
1968 return false;
1969 }
1970 }
Jamie Madillea918db2015-08-18 14:48:59 -04001971
Jamie Madill62d31cb2015-09-11 13:25:51 -04001972 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1973 // Also check the maximum uniform vector and sampler counts.
1974 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1975 {
1976 return false;
1977 }
1978
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001979 if (!indexUniforms(infoLog, caps, uniformLocationBindings))
Geoff Langd8605522016-04-13 10:19:12 -04001980 {
1981 return false;
1982 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001983
Jamie Madillea918db2015-08-18 14:48:59 -04001984 return true;
1985}
1986
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001987bool Program::indexUniforms(InfoLog &infoLog,
1988 const Caps &caps,
1989 const Bindings &uniformLocationBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001990{
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001991 std::vector<VariableLocation> unlocatedUniforms;
1992 std::map<GLuint, VariableLocation> preLocatedUniforms;
Geoff Langd8605522016-04-13 10:19:12 -04001993 int maxUniformLocation = -1;
1994
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001995 // Gather uniforms that have their location pre-set and uniforms that don't yet have a location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001996 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001997 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001998 const LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001999
Geoff Langd8605522016-04-13 10:19:12 -04002000 if (uniform.isBuiltIn())
2001 {
2002 continue;
2003 }
2004
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002005 int preSetLocation = uniformLocationBindings.getBinding(uniform.name);
Geoff Langd8605522016-04-13 10:19:12 -04002006
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002007 // Verify that this location isn't used twice
2008 if (preSetLocation != -1 &&
2009 preLocatedUniforms.find(preSetLocation) != preLocatedUniforms.end())
Geoff Langd8605522016-04-13 10:19:12 -04002010 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002011 infoLog << "Multiple uniforms bound to location " << preSetLocation << ".";
Geoff Langd8605522016-04-13 10:19:12 -04002012 return false;
2013 }
2014
Jamie Madill62d31cb2015-09-11 13:25:51 -04002015 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
2016 {
Geoff Langd8605522016-04-13 10:19:12 -04002017 VariableLocation location(uniform.name, arrayIndex,
2018 static_cast<unsigned int>(uniformIndex));
2019
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002020 if (arrayIndex == 0 && preSetLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002021 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002022 preLocatedUniforms[preSetLocation] = location;
2023 maxUniformLocation = std::max(maxUniformLocation, preSetLocation);
Geoff Langd8605522016-04-13 10:19:12 -04002024 }
2025 else
2026 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002027 unlocatedUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002028 }
2029 }
2030 }
Geoff Langd8605522016-04-13 10:19:12 -04002031
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002032 // Gather the reserved locations, ones that are bound but not referenced. Other uniforms should
Geoff Langd8605522016-04-13 10:19:12 -04002033 // not be assigned to those locations.
2034 std::set<GLuint> reservedLocations;
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002035 for (const auto &locationBinding : uniformLocationBindings)
Geoff Langd8605522016-04-13 10:19:12 -04002036 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002037 GLuint location = locationBinding.second;
2038 if (preLocatedUniforms.find(location) == preLocatedUniforms.end())
Geoff Langd8605522016-04-13 10:19:12 -04002039 {
2040 reservedLocations.insert(location);
2041 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
2042 }
2043 }
2044
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002045 // Make enough space for all uniforms, with pre-set locations or not.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002046 mState.mUniformLocations.resize(
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002047 std::max(unlocatedUniforms.size() + preLocatedUniforms.size() + reservedLocations.size(),
Geoff Langd8605522016-04-13 10:19:12 -04002048 static_cast<size_t>(maxUniformLocation + 1)));
2049
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002050 // Assign uniforms with pre-set locations
2051 for (const auto &uniform : preLocatedUniforms)
Geoff Langd8605522016-04-13 10:19:12 -04002052 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002053 mState.mUniformLocations[uniform.first] = uniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002054 }
2055
2056 // Assign reserved uniforms
2057 for (const auto &reservedLocation : reservedLocations)
2058 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002059 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002060 }
2061
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002062 // Automatically assign locations for the rest of the uniforms
Geoff Langd8605522016-04-13 10:19:12 -04002063 size_t nextUniformLocation = 0;
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002064 for (const auto &unlocatedUniform : unlocatedUniforms)
Geoff Langd8605522016-04-13 10:19:12 -04002065 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002066 while (mState.mUniformLocations[nextUniformLocation].used ||
2067 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002068 {
2069 nextUniformLocation++;
2070 }
2071
Jamie Madill48ef11b2016-04-27 15:21:52 -04002072 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002073 mState.mUniformLocations[nextUniformLocation] = unlocatedUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002074 nextUniformLocation++;
2075 }
2076
2077 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002078}
2079
Martin Radev4c4c8e72016-08-04 12:25:34 +03002080bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2081 const std::string &uniformName,
2082 const sh::InterfaceBlockField &vertexUniform,
2083 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084{
Jamie Madillc4c744222015-11-04 09:39:47 -05002085 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2086 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087 {
2088 return false;
2089 }
2090
2091 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2092 {
Jamie Madillf6113162015-05-07 11:49:21 -04002093 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094 return false;
2095 }
2096
2097 return true;
2098}
2099
Jamie Madilleb979bf2016-11-15 12:28:46 -05002100// Assigns locations to all attributes from the bindings and program locations.
2101bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002103 const auto *vertexShader = mState.getAttachedVertexShader();
2104
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002106 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002107 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002108
2109 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002110 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002111 {
Jamie Madillf6113162015-05-07 11:49:21 -04002112 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002113 return false;
2114 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002116 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002117
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002119 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002120 {
2121 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002122 ASSERT(attribute.staticUse);
2123
Jamie Madilleb979bf2016-11-15 12:28:46 -05002124 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002125 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002126 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002127 attribute.location = bindingLocation;
2128 }
2129
2130 if (attribute.location != -1)
2131 {
2132 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002133 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134
Jamie Madill63805b42015-08-25 13:17:39 -04002135 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002136 {
Jamie Madillf6113162015-05-07 11:49:21 -04002137 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002138 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139
2140 return false;
2141 }
2142
Jamie Madill63805b42015-08-25 13:17:39 -04002143 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 {
Jamie Madill63805b42015-08-25 13:17:39 -04002145 const int regLocation = attribute.location + reg;
2146 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147
2148 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002149 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002150 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002152 // TODO(jmadill): fix aliasing on ES2
2153 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002155 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002156 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 return false;
2158 }
2159 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002160 else
2161 {
Jamie Madill63805b42015-08-25 13:17:39 -04002162 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002163 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164
Jamie Madill63805b42015-08-25 13:17:39 -04002165 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002166 }
2167 }
2168 }
2169
2170 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002171 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 ASSERT(attribute.staticUse);
2174
Jamie Madillc349ec02015-08-21 16:53:12 -04002175 // Not set by glBindAttribLocation or by location layout qualifier
2176 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177 {
Jamie Madill63805b42015-08-25 13:17:39 -04002178 int regs = VariableRegisterCount(attribute.type);
2179 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180
Jamie Madill63805b42015-08-25 13:17:39 -04002181 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182 {
Jamie Madillf6113162015-05-07 11:49:21 -04002183 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002184 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002185 }
2186
Jamie Madillc349ec02015-08-21 16:53:12 -04002187 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188 }
2189 }
2190
Jamie Madill48ef11b2016-04-27 15:21:52 -04002191 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002193 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002194 ASSERT(attribute.location != -1);
2195 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002196
Jamie Madill63805b42015-08-25 13:17:39 -04002197 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002199 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002200 }
2201 }
2202
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203 return true;
2204}
2205
Martin Radev4c4c8e72016-08-04 12:25:34 +03002206bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2207 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2208 const std::string &errorMessage,
2209 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002211 GLuint blockCount = 0;
2212 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002213 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002214 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002215 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002216 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002217 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002218 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002219 return false;
2220 }
2221 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002222 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002223 return true;
2224}
Jamie Madille473dee2015-08-18 14:49:01 -04002225
Martin Radev4c4c8e72016-08-04 12:25:34 +03002226bool Program::validateVertexAndFragmentInterfaceBlocks(
2227 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2228 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2229 InfoLog &infoLog) const
2230{
2231 // Check that interface blocks defined in the vertex and fragment shaders are identical
2232 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2233 UniformBlockMap linkedUniformBlocks;
2234
2235 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2236 {
2237 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2238 }
2239
Jamie Madille473dee2015-08-18 14:49:01 -04002240 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002241 {
Jamie Madille473dee2015-08-18 14:49:01 -04002242 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002243 if (entry != linkedUniformBlocks.end())
2244 {
2245 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2246 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2247 {
2248 return false;
2249 }
2250 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002251 }
2252 return true;
2253}
Jamie Madille473dee2015-08-18 14:49:01 -04002254
Martin Radev4c4c8e72016-08-04 12:25:34 +03002255bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2256{
2257 if (mState.mAttachedComputeShader)
2258 {
2259 const Shader &computeShader = *mState.mAttachedComputeShader;
2260 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2261
2262 if (!validateUniformBlocksCount(
2263 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2264 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2265 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002266 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002267 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002269 return true;
2270 }
2271
2272 const Shader &vertexShader = *mState.mAttachedVertexShader;
2273 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2274
2275 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2276 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2277
2278 if (!validateUniformBlocksCount(
2279 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2280 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2281 {
2282 return false;
2283 }
2284 if (!validateUniformBlocksCount(
2285 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2286 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2287 infoLog))
2288 {
2289
2290 return false;
2291 }
2292 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2293 infoLog))
2294 {
2295 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296 }
Jamie Madille473dee2015-08-18 14:49:01 -04002297
Geoff Lang7dd2e102014-11-10 15:19:26 -05002298 return true;
2299}
2300
Jamie Madilla2c74982016-12-12 11:20:42 -05002301bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002302 const sh::InterfaceBlock &vertexInterfaceBlock,
2303 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304{
2305 const char* blockName = vertexInterfaceBlock.name.c_str();
2306 // validate blocks for the same member types
2307 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2308 {
Jamie Madillf6113162015-05-07 11:49:21 -04002309 infoLog << "Types for interface block '" << blockName
2310 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002311 return false;
2312 }
2313 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2314 {
Jamie Madillf6113162015-05-07 11:49:21 -04002315 infoLog << "Array sizes differ for interface block '" << blockName
2316 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002317 return false;
2318 }
2319 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2320 {
Jamie Madillf6113162015-05-07 11:49:21 -04002321 infoLog << "Layout qualifiers differ for interface block '" << blockName
2322 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002323 return false;
2324 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002325 const unsigned int numBlockMembers =
2326 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002327 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2328 {
2329 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2330 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2331 if (vertexMember.name != fragmentMember.name)
2332 {
Jamie Madillf6113162015-05-07 11:49:21 -04002333 infoLog << "Name mismatch for field " << blockMemberIndex
2334 << " of interface block '" << blockName
2335 << "': (in vertex: '" << vertexMember.name
2336 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002337 return false;
2338 }
2339 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2340 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2341 {
2342 return false;
2343 }
2344 }
2345 return true;
2346}
2347
2348bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2349 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2350{
2351 if (vertexVariable.type != fragmentVariable.type)
2352 {
Jamie Madillf6113162015-05-07 11:49:21 -04002353 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 return false;
2355 }
2356 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2357 {
Jamie Madillf6113162015-05-07 11:49:21 -04002358 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 return false;
2360 }
2361 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2362 {
Jamie Madillf6113162015-05-07 11:49:21 -04002363 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 return false;
2365 }
2366
2367 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2368 {
Jamie Madillf6113162015-05-07 11:49:21 -04002369 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370 return false;
2371 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002372 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2374 {
2375 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2376 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2377
2378 if (vertexMember.name != fragmentMember.name)
2379 {
Jamie Madillf6113162015-05-07 11:49:21 -04002380 infoLog << "Name mismatch for field '" << memberIndex
2381 << "' of " << variableName
2382 << ": (in vertex: '" << vertexMember.name
2383 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002384 return false;
2385 }
2386
2387 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2388 vertexMember.name + "'";
2389
2390 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2391 {
2392 return false;
2393 }
2394 }
2395
2396 return true;
2397}
2398
Olli Etuaho547cbd42017-02-27 11:54:00 +02002399// GLSL ES Spec 3.00.3, section 4.3.5.
2400// GLSL ES Spec 3.10.4, section 4.4.5.
Geoff Lang7dd2e102014-11-10 15:19:26 -05002401bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2402{
Cooper Partin1acf4382015-06-12 12:38:57 -07002403#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2404 const bool validatePrecision = true;
2405#else
2406 const bool validatePrecision = false;
2407#endif
2408
2409 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002410 {
2411 return false;
2412 }
2413
Olli Etuaho547cbd42017-02-27 11:54:00 +02002414 if (vertexUniform.binding != -1 && fragmentUniform.binding != -1 &&
2415 vertexUniform.binding != fragmentUniform.binding)
2416 {
2417 infoLog << "Binding layout qualifiers for " << uniformName
2418 << " differ between vertex and fragment shaders.";
2419 return false;
2420 }
2421
Geoff Lang7dd2e102014-11-10 15:19:26 -05002422 return true;
2423}
2424
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002425bool Program::linkValidateVaryings(InfoLog &infoLog,
2426 const std::string &varyingName,
2427 const sh::Varying &vertexVarying,
2428 const sh::Varying &fragmentVarying,
2429 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002430{
2431 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2432 {
2433 return false;
2434 }
2435
Jamie Madille9cc4692015-02-19 16:00:13 -05002436 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002437 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002438 infoLog << "Interpolation types for " << varyingName
2439 << " differ between vertex and fragment shaders.";
2440 return false;
2441 }
2442
2443 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2444 {
2445 infoLog << "Invariance for " << varyingName
2446 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447 return false;
2448 }
2449
2450 return true;
2451}
2452
Yuly Novikov817232e2017-02-22 18:36:10 -05002453bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
2454{
2455 const Shader *vertexShader = mState.mAttachedVertexShader;
2456 const Shader *fragmentShader = mState.mAttachedFragmentShader;
2457 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
2458 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
2459 int shaderVersion = vertexShader->getShaderVersion();
2460
2461 if (shaderVersion != 100)
2462 {
2463 // Only ESSL 1.0 has restrictions on matching input and output invariance
2464 return true;
2465 }
2466
2467 bool glPositionIsInvariant = false;
2468 bool glPointSizeIsInvariant = false;
2469 bool glFragCoordIsInvariant = false;
2470 bool glPointCoordIsInvariant = false;
2471
2472 for (const sh::Varying &varying : vertexVaryings)
2473 {
2474 if (!varying.isBuiltIn())
2475 {
2476 continue;
2477 }
2478 if (varying.name.compare("gl_Position") == 0)
2479 {
2480 glPositionIsInvariant = varying.isInvariant;
2481 }
2482 else if (varying.name.compare("gl_PointSize") == 0)
2483 {
2484 glPointSizeIsInvariant = varying.isInvariant;
2485 }
2486 }
2487
2488 for (const sh::Varying &varying : fragmentVaryings)
2489 {
2490 if (!varying.isBuiltIn())
2491 {
2492 continue;
2493 }
2494 if (varying.name.compare("gl_FragCoord") == 0)
2495 {
2496 glFragCoordIsInvariant = varying.isInvariant;
2497 }
2498 else if (varying.name.compare("gl_PointCoord") == 0)
2499 {
2500 glPointCoordIsInvariant = varying.isInvariant;
2501 }
2502 }
2503
2504 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2505 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2506 // Not requiring invariance to match is supported by:
2507 // dEQP, WebGL CTS, Nexus 5X GLES
2508 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2509 {
2510 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2511 "declared invariant.";
2512 return false;
2513 }
2514 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2515 {
2516 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2517 "declared invariant.";
2518 return false;
2519 }
2520
2521 return true;
2522}
2523
Jamie Madillccdf74b2015-08-18 10:46:12 -04002524bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002525 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002526 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002527{
2528 size_t totalComponents = 0;
2529
Jamie Madillccdf74b2015-08-18 10:46:12 -04002530 std::set<std::string> uniqueNames;
2531
Jamie Madill48ef11b2016-04-27 15:21:52 -04002532 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002533 {
2534 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002535 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002536 {
Jamie Madill192745a2016-12-22 15:58:21 -05002537 const sh::Varying *varying = ref.second.get();
2538
Jamie Madillccdf74b2015-08-18 10:46:12 -04002539 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002540 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002541 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002542 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002543 infoLog << "Two transform feedback varyings specify the same output variable ("
2544 << tfVaryingName << ").";
2545 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002546 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002547 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002548
Geoff Lang1a683462015-09-29 15:09:59 -04002549 if (varying->isArray())
2550 {
2551 infoLog << "Capture of arrays is undefined and not supported.";
2552 return false;
2553 }
2554
Jamie Madillccdf74b2015-08-18 10:46:12 -04002555 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002556 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002557 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002558 componentCount > caps.maxTransformFeedbackSeparateComponents)
2559 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002560 infoLog << "Transform feedback varying's " << varying->name << " components ("
2561 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002562 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002563 return false;
2564 }
2565
2566 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002567 found = true;
2568 break;
2569 }
2570 }
2571
Jamie Madill89bb70e2015-08-31 14:18:39 -04002572 if (tfVaryingName.find('[') != std::string::npos)
2573 {
Geoff Lang1a683462015-09-29 15:09:59 -04002574 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002575 return false;
2576 }
2577
Geoff Lang7dd2e102014-11-10 15:19:26 -05002578 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2579 ASSERT(found);
2580 }
2581
Jamie Madill48ef11b2016-04-27 15:21:52 -04002582 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002583 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002584 {
Jamie Madillf6113162015-05-07 11:49:21 -04002585 infoLog << "Transform feedback varying total components (" << totalComponents
2586 << ") exceed the maximum interleaved components ("
2587 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002588 return false;
2589 }
2590
2591 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002592}
2593
Jamie Madill192745a2016-12-22 15:58:21 -05002594void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002595{
2596 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002597 mState.mTransformFeedbackVaryingVars.clear();
2598 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002599 {
Jamie Madill192745a2016-12-22 15:58:21 -05002600 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002601 {
Jamie Madill192745a2016-12-22 15:58:21 -05002602 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002603 if (tfVaryingName == varying->name)
2604 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002605 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002606 break;
2607 }
2608 }
2609 }
2610}
2611
Jamie Madill192745a2016-12-22 15:58:21 -05002612Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002613{
Jamie Madill192745a2016-12-22 15:58:21 -05002614 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002615
Jamie Madill48ef11b2016-04-27 15:21:52 -04002616 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002617 {
Jamie Madill192745a2016-12-22 15:58:21 -05002618 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002619 }
2620
Jamie Madill48ef11b2016-04-27 15:21:52 -04002621 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002622 {
Jamie Madill192745a2016-12-22 15:58:21 -05002623 merged[varying.name].fragment = &varying;
2624 }
2625
2626 return merged;
2627}
2628
2629std::vector<PackedVarying> Program::getPackedVaryings(
2630 const Program::MergedVaryings &mergedVaryings) const
2631{
2632 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2633 std::vector<PackedVarying> packedVaryings;
2634
2635 for (const auto &ref : mergedVaryings)
2636 {
2637 const sh::Varying *input = ref.second.vertex;
2638 const sh::Varying *output = ref.second.fragment;
2639
2640 // Only pack varyings that have a matched input or output, plus special builtins.
2641 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002642 {
Jamie Madill192745a2016-12-22 15:58:21 -05002643 // Will get the vertex shader interpolation by default.
2644 auto interpolation = ref.second.get()->interpolation;
2645
2646 // Interpolation qualifiers must match.
2647 if (output->isStruct())
2648 {
2649 ASSERT(!output->isArray());
2650 for (const auto &field : output->fields)
2651 {
2652 ASSERT(!field.isStruct() && !field.isArray());
2653 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2654 }
2655 }
2656 else
2657 {
2658 packedVaryings.push_back(PackedVarying(*output, interpolation));
2659 }
2660 continue;
2661 }
2662
2663 // Keep Transform FB varyings in the merged list always.
2664 if (!input)
2665 {
2666 continue;
2667 }
2668
2669 for (const std::string &tfVarying : tfVaryings)
2670 {
2671 if (tfVarying == input->name)
2672 {
2673 // Transform feedback for varying structs is underspecified.
2674 // See Khronos bug 9856.
2675 // TODO(jmadill): Figure out how to be spec-compliant here.
2676 if (!input->isStruct())
2677 {
2678 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2679 packedVaryings.back().vertexOnly = true;
2680 }
2681 break;
2682 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002683 }
2684 }
2685
Jamie Madill192745a2016-12-22 15:58:21 -05002686 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2687
2688 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002689}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002690
2691void Program::linkOutputVariables()
2692{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002693 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002694 ASSERT(fragmentShader != nullptr);
2695
2696 // Skip this step for GLES2 shaders.
2697 if (fragmentShader->getShaderVersion() == 100)
2698 return;
2699
Jamie Madilla0a9e122015-09-02 15:54:30 -04002700 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002701
2702 // TODO(jmadill): any caps validation here?
2703
2704 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2705 outputVariableIndex++)
2706 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002707 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002708
2709 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2710 if (outputVariable.isBuiltIn())
2711 continue;
2712
2713 // Since multiple output locations must be specified, use 0 for non-specified locations.
2714 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2715
2716 ASSERT(outputVariable.staticUse);
2717
2718 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2719 elementIndex++)
2720 {
2721 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002722 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002723 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002724 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002725 VariableLocation(outputVariable.name, element, outputVariableIndex);
2726 }
2727 }
2728}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002729
Jamie Madilla2c74982016-12-12 11:20:42 -05002730bool Program::flattenUniformsAndCheckCapsForShader(const Shader &shader,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002731 GLuint maxUniformComponents,
2732 GLuint maxTextureImageUnits,
2733 const std::string &componentsErrorMessage,
2734 const std::string &samplerErrorMessage,
2735 std::vector<LinkedUniform> &samplerUniforms,
2736 InfoLog &infoLog)
2737{
2738 VectorAndSamplerCount vasCount;
2739 for (const sh::Uniform &uniform : shader.getUniforms())
2740 {
2741 if (uniform.staticUse)
2742 {
2743 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2744 }
2745 }
2746
2747 if (vasCount.vectorCount > maxUniformComponents)
2748 {
2749 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2750 return false;
2751 }
2752
2753 if (vasCount.samplerCount > maxTextureImageUnits)
2754 {
2755 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2756 return false;
2757 }
2758
2759 return true;
2760}
2761
Jamie Madill62d31cb2015-09-11 13:25:51 -04002762bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2763{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002764 std::vector<LinkedUniform> samplerUniforms;
2765
Martin Radev4c4c8e72016-08-04 12:25:34 +03002766 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002768 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002769
2770 // TODO (mradev): check whether we need finer-grained component counting
2771 if (!flattenUniformsAndCheckCapsForShader(
2772 *computeShader, caps.maxComputeUniformComponents / 4,
2773 caps.maxComputeTextureImageUnits,
2774 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2775 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2776 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002777 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002778 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002779 }
2780 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002781 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002782 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002783 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002784
Martin Radev4c4c8e72016-08-04 12:25:34 +03002785 if (!flattenUniformsAndCheckCapsForShader(
2786 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2787 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2788 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2789 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002790 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002791 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002792 }
Jamie Madilla2c74982016-12-12 11:20:42 -05002793 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002794
Martin Radev4c4c8e72016-08-04 12:25:34 +03002795 if (!flattenUniformsAndCheckCapsForShader(
2796 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2797 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2798 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2799 infoLog))
2800 {
2801 return false;
2802 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002803 }
2804
Jamie Madille7d84322017-01-10 18:21:59 -05002805 mState.mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
2806 mState.mSamplerUniformRange.end =
2807 mState.mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002808
Jamie Madill48ef11b2016-04-27 15:21:52 -04002809 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002810
Jamie Madille7d84322017-01-10 18:21:59 -05002811 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2812 for (const auto &samplerUniform : samplerUniforms)
2813 {
2814 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2815 mState.mSamplerBindings.emplace_back(
2816 SamplerBinding(textureType, samplerUniform.elementCount()));
2817 }
2818
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 return true;
2820}
2821
2822Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002823 const std::string &fullName,
2824 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002825{
2826 VectorAndSamplerCount vectorAndSamplerCount;
2827
2828 if (uniform.isStruct())
2829 {
2830 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2831 {
2832 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2833
2834 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2835 {
2836 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2837 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2838
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002839 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002840 }
2841 }
2842
2843 return vectorAndSamplerCount;
2844 }
2845
2846 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002847 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002848 if (!UniformInList(mState.getUniforms(), fullName) &&
2849 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002850 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002851 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
2852 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002853 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002854
2855 // Store sampler uniforms separately, so we'll append them to the end of the list.
2856 if (isSampler)
2857 {
2858 samplerUniforms->push_back(linkedUniform);
2859 }
2860 else
2861 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002862 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002863 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002864 }
2865
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002866 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002867
2868 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2869 // Likewise, don't count "real" uniforms towards sampler count.
2870 vectorAndSamplerCount.vectorCount =
2871 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002872 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002873
2874 return vectorAndSamplerCount;
2875}
2876
2877void Program::gatherInterfaceBlockInfo()
2878{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002879 ASSERT(mState.mUniformBlocks.empty());
2880
2881 if (mState.mAttachedComputeShader)
2882 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002883 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002884
2885 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2886 {
2887
2888 // Only 'packed' blocks are allowed to be considered inactive.
2889 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2890 continue;
2891
Jamie Madilla2c74982016-12-12 11:20:42 -05002892 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002893 {
2894 if (block.name == computeBlock.name)
2895 {
2896 block.computeStaticUse = computeBlock.staticUse;
2897 }
2898 }
2899
2900 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2901 }
2902 return;
2903 }
2904
Jamie Madill62d31cb2015-09-11 13:25:51 -04002905 std::set<std::string> visitedList;
2906
Jamie Madilla2c74982016-12-12 11:20:42 -05002907 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908
Jamie Madill62d31cb2015-09-11 13:25:51 -04002909 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2910 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002911 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002912 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2913 continue;
2914
2915 if (visitedList.count(vertexBlock.name) > 0)
2916 continue;
2917
2918 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2919 visitedList.insert(vertexBlock.name);
2920 }
2921
Jamie Madilla2c74982016-12-12 11:20:42 -05002922 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002923
2924 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2925 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002926 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002927 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2928 continue;
2929
2930 if (visitedList.count(fragmentBlock.name) > 0)
2931 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002932 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002933 {
2934 if (block.name == fragmentBlock.name)
2935 {
2936 block.fragmentStaticUse = fragmentBlock.staticUse;
2937 }
2938 }
2939
2940 continue;
2941 }
2942
2943 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2944 visitedList.insert(fragmentBlock.name);
2945 }
2946}
2947
Jamie Madill4a3c2342015-10-08 12:58:45 -04002948template <typename VarT>
2949void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2950 const std::string &prefix,
2951 int blockIndex)
2952{
2953 for (const VarT &field : fields)
2954 {
2955 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2956
2957 if (field.isStruct())
2958 {
2959 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2960 {
2961 const std::string uniformElementName =
2962 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2963 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2964 }
2965 }
2966 else
2967 {
2968 // If getBlockMemberInfo returns false, the uniform is optimized out.
2969 sh::BlockMemberInfo memberInfo;
2970 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2971 {
2972 continue;
2973 }
2974
2975 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2976 blockIndex, memberInfo);
2977
2978 // Since block uniforms have no location, we don't need to store them in the uniform
2979 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002980 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002981 }
2982 }
2983}
2984
Jamie Madill62d31cb2015-09-11 13:25:51 -04002985void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2986{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002987 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002988 size_t blockSize = 0;
2989
2990 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002991 std::stringstream blockNameStr;
2992 blockNameStr << interfaceBlock.name;
2993 if (interfaceBlock.arraySize > 0)
2994 {
2995 blockNameStr << "[0]";
2996 }
2997 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002998 {
2999 return;
3000 }
3001
3002 // Track the first and last uniform index to determine the range of active uniforms in the
3003 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003004 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05003005 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04003006 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003007
3008 std::vector<unsigned int> blockUniformIndexes;
3009 for (size_t blockUniformIndex = firstBlockUniformIndex;
3010 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
3011 {
3012 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
3013 }
3014
3015 if (interfaceBlock.arraySize > 0)
3016 {
3017 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
3018 {
3019 UniformBlock block(interfaceBlock.name, true, arrayElement);
3020 block.memberUniformIndexes = blockUniformIndexes;
3021
Martin Radev4c4c8e72016-08-04 12:25:34 +03003022 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003023 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003024 case GL_VERTEX_SHADER:
3025 {
3026 block.vertexStaticUse = interfaceBlock.staticUse;
3027 break;
3028 }
3029 case GL_FRAGMENT_SHADER:
3030 {
3031 block.fragmentStaticUse = interfaceBlock.staticUse;
3032 break;
3033 }
3034 case GL_COMPUTE_SHADER:
3035 {
3036 block.computeStaticUse = interfaceBlock.staticUse;
3037 break;
3038 }
3039 default:
3040 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003041 }
3042
Qin Jiajia0350a642016-11-01 17:01:51 +08003043 // Since all block elements in an array share the same active uniforms, they will all be
3044 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
3045 // here we will add every block element in the array.
3046 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04003047 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003048 }
3049 }
3050 else
3051 {
3052 UniformBlock block(interfaceBlock.name, false, 0);
3053 block.memberUniformIndexes = blockUniformIndexes;
3054
Martin Radev4c4c8e72016-08-04 12:25:34 +03003055 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003056 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003057 case GL_VERTEX_SHADER:
3058 {
3059 block.vertexStaticUse = interfaceBlock.staticUse;
3060 break;
3061 }
3062 case GL_FRAGMENT_SHADER:
3063 {
3064 block.fragmentStaticUse = interfaceBlock.staticUse;
3065 break;
3066 }
3067 case GL_COMPUTE_SHADER:
3068 {
3069 block.computeStaticUse = interfaceBlock.staticUse;
3070 break;
3071 }
3072 default:
3073 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04003074 }
3075
Jamie Madill4a3c2342015-10-08 12:58:45 -04003076 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04003077 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003078 }
3079}
3080
Jamie Madille7d84322017-01-10 18:21:59 -05003081template <>
3082void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3083 const uint8_t *destPointer,
3084 GLsizei clampedCount,
3085 const GLint *v)
3086{
3087 // Invalidate the validation cache only if we modify the sampler data.
3088 if (mState.isSamplerUniformIndex(locationInfo.index) &&
3089 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
3090 {
3091 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3092 std::vector<GLuint> *boundTextureUnits =
3093 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
3094
3095 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
3096 mCachedValidateSamplersResult.reset();
3097 }
3098}
3099
3100template <typename T>
3101void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3102 const uint8_t *destPointer,
3103 GLsizei clampedCount,
3104 const T *v)
3105{
3106}
3107
Jamie Madill62d31cb2015-09-11 13:25:51 -04003108template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003109GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003110{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003111 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3112 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003113 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
3114
Corentin Wallez15ac5342016-11-03 17:06:39 -04003115 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3116 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3117 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003118 GLsizei maxElementCount =
3119 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
3120
3121 GLsizei count = countIn;
3122 GLsizei clampedCount = count * vectorSize;
3123 if (clampedCount > maxElementCount)
3124 {
3125 clampedCount = maxElementCount;
3126 count = maxElementCount / vectorSize;
3127 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04003128
Jamie Madill62d31cb2015-09-11 13:25:51 -04003129 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
3130 {
3131 // Do a cast conversion for boolean types. From the spec:
3132 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
3133 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003134 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003135 {
3136 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
3137 }
3138 }
3139 else
3140 {
Jamie Madille7d84322017-01-10 18:21:59 -05003141 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003142 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003143 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003144
3145 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003146}
3147
3148template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003149GLsizei Program::setMatrixUniformInternal(GLint location,
3150 GLsizei count,
3151 GLboolean transpose,
3152 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003153{
3154 if (!transpose)
3155 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003156 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003157 }
3158
3159 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003160 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3161 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003162 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003163
3164 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3165 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3166 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3167 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3168
3169 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003170 {
3171 size_t elementOffset = element * rows * cols;
3172
3173 for (size_t row = 0; row < rows; ++row)
3174 {
3175 for (size_t col = 0; col < cols; ++col)
3176 {
3177 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3178 }
3179 }
3180 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003181
3182 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003183}
3184
3185template <typename DestT>
3186void Program::getUniformInternal(GLint location, DestT *dataOut) const
3187{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003188 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3189 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003190
3191 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3192
3193 GLenum componentType = VariableComponentType(uniform.type);
3194 if (componentType == GLTypeToGLenum<DestT>::value)
3195 {
3196 memcpy(dataOut, srcPointer, uniform.getElementSize());
3197 return;
3198 }
3199
Corentin Wallez6596c462016-03-17 17:26:58 -04003200 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003201
3202 switch (componentType)
3203 {
3204 case GL_INT:
3205 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3206 break;
3207 case GL_UNSIGNED_INT:
3208 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3209 break;
3210 case GL_BOOL:
3211 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3212 break;
3213 case GL_FLOAT:
3214 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3215 break;
3216 default:
3217 UNREACHABLE();
3218 }
3219}
Jamie Madilla4595b82017-01-11 17:36:34 -05003220
3221bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3222{
3223 // Must be called after samplers are validated.
3224 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3225
3226 for (const auto &binding : mState.mSamplerBindings)
3227 {
3228 GLenum textureType = binding.textureType;
3229 for (const auto &unit : binding.boundTextureUnits)
3230 {
3231 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3232 if (programTextureID == textureID)
3233 {
3234 // TODO(jmadill): Check for appropriate overlap.
3235 return true;
3236 }
3237 }
3238 }
3239
3240 return false;
3241}
3242
Jamie Madilla2c74982016-12-12 11:20:42 -05003243} // namespace gl