blob: ab345528b7605ed0067792477703d3e5d2aa5baa [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 Madill9082b982016-04-27 15:21:51 -040020#include "libANGLE/ContextState.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 Madill62d31cb2015-09-11 13:25:51 -040025#include "libANGLE/queryconversions.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040026#include "libANGLE/Uniform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028namespace gl
29{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000030
Geoff Lang7dd2e102014-11-10 15:19:26 -050031namespace
32{
33
Jamie Madill62d31cb2015-09-11 13:25:51 -040034void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
35{
36 stream->writeInt(var.type);
37 stream->writeInt(var.precision);
38 stream->writeString(var.name);
39 stream->writeString(var.mappedName);
40 stream->writeInt(var.arraySize);
41 stream->writeInt(var.staticUse);
42 stream->writeString(var.structName);
43 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050044}
45
Jamie Madill62d31cb2015-09-11 13:25:51 -040046void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
47{
48 var->type = stream->readInt<GLenum>();
49 var->precision = stream->readInt<GLenum>();
50 var->name = stream->readString();
51 var->mappedName = stream->readString();
52 var->arraySize = stream->readInt<unsigned int>();
53 var->staticUse = stream->readBool();
54 var->structName = stream->readString();
55}
56
Jamie Madill62d31cb2015-09-11 13:25:51 -040057// This simplified cast function doesn't need to worry about advanced concepts like
58// depth range values, or casting to bool.
59template <typename DestT, typename SrcT>
60DestT UniformStateQueryCast(SrcT value);
61
62// From-Float-To-Integer Casts
63template <>
64GLint UniformStateQueryCast(GLfloat value)
65{
66 return clampCast<GLint>(roundf(value));
67}
68
69template <>
70GLuint UniformStateQueryCast(GLfloat value)
71{
72 return clampCast<GLuint>(roundf(value));
73}
74
75// From-Integer-to-Integer Casts
76template <>
77GLint UniformStateQueryCast(GLuint value)
78{
79 return clampCast<GLint>(value);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLint value)
84{
85 return clampCast<GLuint>(value);
86}
87
88// From-Boolean-to-Anything Casts
89template <>
90GLfloat UniformStateQueryCast(GLboolean value)
91{
92 return (value == GL_TRUE ? 1.0f : 0.0f);
93}
94
95template <>
96GLint UniformStateQueryCast(GLboolean value)
97{
98 return (value == GL_TRUE ? 1 : 0);
99}
100
101template <>
102GLuint UniformStateQueryCast(GLboolean value)
103{
104 return (value == GL_TRUE ? 1u : 0u);
105}
106
107// Default to static_cast
108template <typename DestT, typename SrcT>
109DestT UniformStateQueryCast(SrcT value)
110{
111 return static_cast<DestT>(value);
112}
113
114template <typename SrcT, typename DestT>
115void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
116{
117 for (int comp = 0; comp < components; ++comp)
118 {
119 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
120 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
121 size_t offset = comp * 4;
122 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
123 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
124 }
125}
126
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400127bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
128{
129 for (const LinkedUniform &uniform : list)
130 {
131 if (uniform.name == name)
132 return true;
133 }
134
135 return false;
136}
137
Jamie Madill62d31cb2015-09-11 13:25:51 -0400138} // anonymous namespace
139
Jamie Madill4a3c2342015-10-08 12:58:45 -0400140const char *const g_fakepath = "C:\\fakepath";
141
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400142InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000143{
144}
145
146InfoLog::~InfoLog()
147{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000148}
149
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400150size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000151{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400152 const std::string &logString = mStream.str();
153 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000154}
155
Geoff Lange1a27752015-10-05 13:16:04 -0400156void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000157{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400158 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000159
160 if (bufSize > 0)
161 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400162 const std::string str(mStream.str());
163
164 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000165 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400166 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
167 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000168 }
169
170 infoLog[index] = '\0';
171 }
172
173 if (length)
174 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400175 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000176 }
177}
178
179// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300180// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000181// messages, so lets remove all occurrences of this fake file path from the log.
182void InfoLog::appendSanitized(const char *message)
183{
184 std::string msg(message);
185
186 size_t found;
187 do
188 {
189 found = msg.find(g_fakepath);
190 if (found != std::string::npos)
191 {
192 msg.erase(found, strlen(g_fakepath));
193 }
194 }
195 while (found != std::string::npos);
196
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400197 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000198}
199
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000200void InfoLog::reset()
201{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000202}
203
Geoff Langd8605522016-04-13 10:19:12 -0400204VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000205{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500206}
207
Geoff Langd8605522016-04-13 10:19:12 -0400208VariableLocation::VariableLocation(const std::string &name,
209 unsigned int element,
210 unsigned int index)
211 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500212{
213}
214
Geoff Langd8605522016-04-13 10:19:12 -0400215void Program::Bindings::bindLocation(GLuint index, const std::string &name)
216{
217 mBindings[name] = index;
218}
219
220int Program::Bindings::getBinding(const std::string &name) const
221{
222 auto iter = mBindings.find(name);
223 return (iter != mBindings.end()) ? iter->second : -1;
224}
225
226Program::Bindings::const_iterator Program::Bindings::begin() const
227{
228 return mBindings.begin();
229}
230
231Program::Bindings::const_iterator Program::Bindings::end() const
232{
233 return mBindings.end();
234}
235
Jamie Madill48ef11b2016-04-27 15:21:52 -0400236ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500237 : mLabel(),
238 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400239 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300240 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500241 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
242 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400243{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300244 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400245}
246
Jamie Madill48ef11b2016-04-27 15:21:52 -0400247ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400248{
249 if (mAttachedVertexShader != nullptr)
250 {
251 mAttachedVertexShader->release();
252 }
253
254 if (mAttachedFragmentShader != nullptr)
255 {
256 mAttachedFragmentShader->release();
257 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300258
259 if (mAttachedComputeShader != nullptr)
260 {
261 mAttachedComputeShader->release();
262 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400263}
264
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500266{
267 return mLabel;
268}
269
Jamie Madill48ef11b2016-04-27 15:21:52 -0400270const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400271{
272 for (const LinkedUniform &linkedUniform : mUniforms)
273 {
274 if (linkedUniform.name == name)
275 {
276 return &linkedUniform;
277 }
278 }
279
280 return nullptr;
281}
282
Jamie Madill48ef11b2016-04-27 15:21:52 -0400283GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400284{
285 size_t subscript = GL_INVALID_INDEX;
286 std::string baseName = gl::ParseUniformName(name, &subscript);
287
288 for (size_t location = 0; location < mUniformLocations.size(); ++location)
289 {
290 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400291 if (!uniformLocation.used)
292 {
293 continue;
294 }
295
296 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400297
298 if (uniform.name == baseName)
299 {
Geoff Langd8605522016-04-13 10:19:12 -0400300 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400301 {
Geoff Langd8605522016-04-13 10:19:12 -0400302 if (uniformLocation.element == subscript ||
303 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
304 {
305 return static_cast<GLint>(location);
306 }
307 }
308 else
309 {
310 if (subscript == GL_INVALID_INDEX)
311 {
312 return static_cast<GLint>(location);
313 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400314 }
315 }
316 }
317
318 return -1;
319}
320
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321GLuint ProgramState::getUniformIndex(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400322{
323 size_t subscript = GL_INVALID_INDEX;
324 std::string baseName = gl::ParseUniformName(name, &subscript);
325
326 // The app is not allowed to specify array indices other than 0 for arrays of basic types
327 if (subscript != 0 && subscript != GL_INVALID_INDEX)
328 {
329 return GL_INVALID_INDEX;
330 }
331
332 for (size_t index = 0; index < mUniforms.size(); index++)
333 {
334 const LinkedUniform &uniform = mUniforms[index];
335 if (uniform.name == baseName)
336 {
337 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
338 {
339 return static_cast<GLuint>(index);
340 }
341 }
342 }
343
344 return GL_INVALID_INDEX;
345}
346
Jamie Madill7aea7e02016-05-10 10:39:45 -0400347Program::Program(rx::GLImplFactory *factory, ResourceManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400348 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500350 mLinked(false),
351 mDeleteStatus(false),
352 mRefCount(0),
353 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400354 mHandle(handle),
355 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500356{
357 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000358
359 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500360 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361}
362
363Program::~Program()
364{
365 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000366
Geoff Lang7dd2e102014-11-10 15:19:26 -0500367 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
Geoff Lang70d0f492015-12-10 17:45:46 -0500370void Program::setLabel(const std::string &label)
371{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400372 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500373}
374
375const std::string &Program::getLabel() const
376{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400377 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500378}
379
Jamie Madillef300b12016-10-07 15:12:09 -0400380void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300382 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300384 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Jamie Madillef300b12016-10-07 15:12:09 -0400386 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300387 mState.mAttachedVertexShader = shader;
388 mState.mAttachedVertexShader->addRef();
389 break;
390 }
391 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392 {
Jamie Madillef300b12016-10-07 15:12:09 -0400393 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300394 mState.mAttachedFragmentShader = shader;
395 mState.mAttachedFragmentShader->addRef();
396 break;
397 }
398 case GL_COMPUTE_SHADER:
399 {
Jamie Madillef300b12016-10-07 15:12:09 -0400400 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300401 mState.mAttachedComputeShader = shader;
402 mState.mAttachedComputeShader->addRef();
403 break;
404 }
405 default:
406 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408}
409
410bool Program::detachShader(Shader *shader)
411{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300412 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300414 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300416 if (mState.mAttachedVertexShader != shader)
417 {
418 return false;
419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
Martin Radev4c4c8e72016-08-04 12:25:34 +0300421 shader->release();
422 mState.mAttachedVertexShader = nullptr;
423 break;
424 }
425 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 if (mState.mAttachedFragmentShader != shader)
428 {
429 return false;
430 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 shader->release();
433 mState.mAttachedFragmentShader = nullptr;
434 break;
435 }
436 case GL_COMPUTE_SHADER:
437 {
438 if (mState.mAttachedComputeShader != shader)
439 {
440 return false;
441 }
442
443 shader->release();
444 mState.mAttachedComputeShader = nullptr;
445 break;
446 }
447 default:
448 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451 return true;
452}
453
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000454int Program::getAttachedShadersCount() const
455{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300456 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
457 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000458}
459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460void Program::bindAttributeLocation(GLuint index, const char *name)
461{
Geoff Langd8605522016-04-13 10:19:12 -0400462 mAttributeBindings.bindLocation(index, name);
463}
464
465void Program::bindUniformLocation(GLuint index, const char *name)
466{
467 // Bind the base uniform name only since array indices other than 0 cannot be bound
468 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469}
470
Sami Väisänen46eaa942016-06-29 10:26:37 +0300471void Program::bindFragmentInputLocation(GLint index, const char *name)
472{
473 mFragmentInputBindings.bindLocation(index, name);
474}
475
476BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
477{
478 BindingInfo ret;
479 ret.type = GL_NONE;
480 ret.valid = false;
481
482 const Shader *fragmentShader = mState.getAttachedFragmentShader();
483 ASSERT(fragmentShader);
484
485 // Find the actual fragment shader varying we're interested in
486 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
487
488 for (const auto &binding : mFragmentInputBindings)
489 {
490 if (binding.second != static_cast<GLuint>(index))
491 continue;
492
493 ret.valid = true;
494
495 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400496 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300497
498 for (const auto &in : inputs)
499 {
500 if (in.name == originalName)
501 {
502 if (in.isArray())
503 {
504 // The client wants to bind either "name" or "name[0]".
505 // GL ES 3.1 spec refers to active array names with language such as:
506 // "if the string identifies the base name of an active array, where the
507 // string would exactly match the name of the variable if the suffix "[0]"
508 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400509 if (arrayIndex == GL_INVALID_INDEX)
510 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300511
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400512 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300513 }
514 else
515 {
516 ret.name = in.mappedName;
517 }
518 ret.type = in.type;
519 return ret;
520 }
521 }
522 }
523
524 return ret;
525}
526
527void Program::pathFragmentInputGen(GLint index,
528 GLenum genMode,
529 GLint components,
530 const GLfloat *coeffs)
531{
532 // If the location is -1 then the command is silently ignored
533 if (index == -1)
534 return;
535
536 const auto &binding = getFragmentInputBindingInfo(index);
537
538 // If the input doesn't exist then then the command is silently ignored
539 // This could happen through optimization for example, the shader translator
540 // decides that a variable is not actually being used and optimizes it away.
541 if (binding.name.empty())
542 return;
543
544 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
545}
546
Martin Radev4c4c8e72016-08-04 12:25:34 +0300547// The attached shaders are checked for linking errors by matching up their variables.
548// Uniform, input and output variables get collected.
549// The code gets compiled into binaries.
Jamie Madill9082b982016-04-27 15:21:51 -0400550Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000551{
552 unlink(false);
553
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000554 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000555 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000556
Martin Radev4c4c8e72016-08-04 12:25:34 +0300557 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558
Martin Radev4c4c8e72016-08-04 12:25:34 +0300559 bool isComputeShaderAttached = (mState.mAttachedComputeShader != nullptr);
560 bool nonComputeShadersAttached =
561 (mState.mAttachedVertexShader != nullptr || mState.mAttachedFragmentShader != nullptr);
562 // Check whether we both have a compute and non-compute shaders attached.
563 // If there are of both types attached, then linking should fail.
564 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
565 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500566 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300567 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
568 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400569 }
570
Martin Radev4c4c8e72016-08-04 12:25:34 +0300571 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500572 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300573 if (!mState.mAttachedComputeShader->isCompiled())
574 {
575 mInfoLog << "Attached compute shader is not compiled.";
576 return NoError();
577 }
578 ASSERT(mState.mAttachedComputeShader->getType() == GL_COMPUTE_SHADER);
579
580 mState.mComputeShaderLocalSize = mState.mAttachedComputeShader->getWorkGroupSize();
581
582 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
583 // If the work group size is not specified, a link time error should occur.
584 if (!mState.mComputeShaderLocalSize.isDeclared())
585 {
586 mInfoLog << "Work group size is not specified.";
587 return NoError();
588 }
589
590 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
591 {
592 return NoError();
593 }
594
595 if (!linkUniformBlocks(mInfoLog, caps))
596 {
597 return NoError();
598 }
599
600 rx::LinkResult result = mProgram->link(data, mInfoLog);
601
602 if (result.error.isError() || !result.linkSuccess)
603 {
604 return result.error;
605 }
606 }
607 else
608 {
609 if (!mState.mAttachedFragmentShader || !mState.mAttachedFragmentShader->isCompiled())
610 {
611 return NoError();
612 }
613 ASSERT(mState.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
614
615 if (!mState.mAttachedVertexShader || !mState.mAttachedVertexShader->isCompiled())
616 {
617 return NoError();
618 }
619 ASSERT(mState.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
620
621 if (mState.mAttachedFragmentShader->getShaderVersion() !=
622 mState.mAttachedVertexShader->getShaderVersion())
623 {
624 mInfoLog << "Fragment shader version does not match vertex shader version.";
625 return NoError();
626 }
627
628 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mState.mAttachedVertexShader))
629 {
630 return NoError();
631 }
632
633 if (!linkVaryings(mInfoLog, mState.mAttachedVertexShader, mState.mAttachedFragmentShader))
634 {
635 return NoError();
636 }
637
638 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
639 {
640 return NoError();
641 }
642
643 if (!linkUniformBlocks(mInfoLog, caps))
644 {
645 return NoError();
646 }
647
648 const auto &mergedVaryings = getMergedVaryings();
649
650 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
651 {
652 return NoError();
653 }
654
655 linkOutputVariables();
656
657 rx::LinkResult result = mProgram->link(data, mInfoLog);
658 if (result.error.isError() || !result.linkSuccess)
659 {
660 return result.error;
661 }
662
663 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500664 }
665
Jamie Madill4a3c2342015-10-08 12:58:45 -0400666 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400667
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668 mLinked = true;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300669 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000670}
671
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000672// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673void Program::unlink(bool destroy)
674{
675 if (destroy) // Object being destructed
676 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400677 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400679 mState.mAttachedFragmentShader->release();
680 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681 }
682
Jamie Madill48ef11b2016-04-27 15:21:52 -0400683 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400685 mState.mAttachedVertexShader->release();
686 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300688
689 if (mState.mAttachedComputeShader)
690 {
691 mState.mAttachedComputeShader->release();
692 mState.mAttachedComputeShader = nullptr;
693 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694 }
695
Jamie Madill48ef11b2016-04-27 15:21:52 -0400696 mState.mAttributes.clear();
697 mState.mActiveAttribLocationsMask.reset();
698 mState.mTransformFeedbackVaryingVars.clear();
699 mState.mUniforms.clear();
700 mState.mUniformLocations.clear();
701 mState.mUniformBlocks.clear();
702 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 mState.mComputeShaderLocalSize.fill(1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500704
Geoff Lang7dd2e102014-11-10 15:19:26 -0500705 mValidated = false;
706
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000707 mLinked = false;
708}
709
Geoff Lange1a27752015-10-05 13:16:04 -0400710bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000711{
712 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713}
714
Geoff Lang7dd2e102014-11-10 15:19:26 -0500715Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000716{
717 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000718
Geoff Lang7dd2e102014-11-10 15:19:26 -0500719#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
720 return Error(GL_NO_ERROR);
721#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400722 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
723 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000724 {
Jamie Madillf6113162015-05-07 11:49:21 -0400725 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500726 return Error(GL_NO_ERROR);
727 }
728
Geoff Langc46cc2f2015-10-01 17:16:20 -0400729 BinaryInputStream stream(binary, length);
730
Geoff Lang7dd2e102014-11-10 15:19:26 -0500731 int majorVersion = stream.readInt<int>();
732 int minorVersion = stream.readInt<int>();
733 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
734 {
Jamie Madillf6113162015-05-07 11:49:21 -0400735 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500736 return Error(GL_NO_ERROR);
737 }
738
739 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
740 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
741 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
742 {
Jamie Madillf6113162015-05-07 11:49:21 -0400743 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500744 return Error(GL_NO_ERROR);
745 }
746
Martin Radev4c4c8e72016-08-04 12:25:34 +0300747 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
748 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
749 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
750
Jamie Madill63805b42015-08-25 13:17:39 -0400751 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
752 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400753 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500754
Jamie Madill3da79b72015-04-27 11:09:17 -0400755 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400756 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400757 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
758 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400759 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400760 LoadShaderVar(&stream, &attrib);
761 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400762 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400763 }
764
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400766 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400767 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
768 {
769 LinkedUniform uniform;
770 LoadShaderVar(&stream, &uniform);
771
772 uniform.blockIndex = stream.readInt<int>();
773 uniform.blockInfo.offset = stream.readInt<int>();
774 uniform.blockInfo.arrayStride = stream.readInt<int>();
775 uniform.blockInfo.matrixStride = stream.readInt<int>();
776 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
777
Jamie Madill48ef11b2016-04-27 15:21:52 -0400778 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400779 }
780
781 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400782 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400783 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
784 uniformIndexIndex++)
785 {
786 VariableLocation variable;
787 stream.readString(&variable.name);
788 stream.readInt(&variable.element);
789 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400790 stream.readBool(&variable.used);
791 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792
Jamie Madill48ef11b2016-04-27 15:21:52 -0400793 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400794 }
795
796 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400797 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400798 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
799 ++uniformBlockIndex)
800 {
801 UniformBlock uniformBlock;
802 stream.readString(&uniformBlock.name);
803 stream.readBool(&uniformBlock.isArray);
804 stream.readInt(&uniformBlock.arrayElement);
805 stream.readInt(&uniformBlock.dataSize);
806 stream.readBool(&uniformBlock.vertexStaticUse);
807 stream.readBool(&uniformBlock.fragmentStaticUse);
808
809 unsigned int numMembers = stream.readInt<unsigned int>();
810 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
811 {
812 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
813 }
814
Jamie Madill48ef11b2016-04-27 15:21:52 -0400815 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 }
817
Brandon Jones1048ea72015-10-06 15:34:52 -0700818 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400819 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700820 for (unsigned int transformFeedbackVaryingIndex = 0;
821 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
822 ++transformFeedbackVaryingIndex)
823 {
824 sh::Varying varying;
825 stream.readInt(&varying.arraySize);
826 stream.readInt(&varying.type);
827 stream.readString(&varying.name);
828
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700830 }
831
Jamie Madill48ef11b2016-04-27 15:21:52 -0400832 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400833
Jamie Madill80a6fc02015-08-21 16:53:16 -0400834 unsigned int outputVarCount = stream.readInt<unsigned int>();
835 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
836 {
837 int locationIndex = stream.readInt<int>();
838 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400839 stream.readInt(&locationData.element);
840 stream.readInt(&locationData.index);
841 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400843 }
844
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400845 stream.readInt(&mSamplerUniformRange.start);
846 stream.readInt(&mSamplerUniformRange.end);
847
Geoff Lang7dd2e102014-11-10 15:19:26 -0500848 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
849 if (result.error.isError() || !result.linkSuccess)
850 {
Geoff Langb543aff2014-09-30 14:52:54 -0400851 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000852 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000853
Geoff Lang7dd2e102014-11-10 15:19:26 -0500854 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400855 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500856#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
857}
858
859Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
860{
861 if (binaryFormat)
862 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400863 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500864 }
865
866 BinaryOutputStream stream;
867
Geoff Lang7dd2e102014-11-10 15:19:26 -0500868 stream.writeInt(ANGLE_MAJOR_VERSION);
869 stream.writeInt(ANGLE_MINOR_VERSION);
870 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
871
Martin Radev4c4c8e72016-08-04 12:25:34 +0300872 stream.writeInt(mState.mComputeShaderLocalSize[0]);
873 stream.writeInt(mState.mComputeShaderLocalSize[1]);
874 stream.writeInt(mState.mComputeShaderLocalSize[2]);
875
Jamie Madill48ef11b2016-04-27 15:21:52 -0400876 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500877
Jamie Madill48ef11b2016-04-27 15:21:52 -0400878 stream.writeInt(mState.mAttributes.size());
879 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400880 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400881 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400882 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400883 }
884
Jamie Madill48ef11b2016-04-27 15:21:52 -0400885 stream.writeInt(mState.mUniforms.size());
886 for (const gl::LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400887 {
888 WriteShaderVar(&stream, uniform);
889
890 // FIXME: referenced
891
892 stream.writeInt(uniform.blockIndex);
893 stream.writeInt(uniform.blockInfo.offset);
894 stream.writeInt(uniform.blockInfo.arrayStride);
895 stream.writeInt(uniform.blockInfo.matrixStride);
896 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
897 }
898
Jamie Madill48ef11b2016-04-27 15:21:52 -0400899 stream.writeInt(mState.mUniformLocations.size());
900 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400901 {
902 stream.writeString(variable.name);
903 stream.writeInt(variable.element);
904 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400905 stream.writeInt(variable.used);
906 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400907 }
908
Jamie Madill48ef11b2016-04-27 15:21:52 -0400909 stream.writeInt(mState.mUniformBlocks.size());
910 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400911 {
912 stream.writeString(uniformBlock.name);
913 stream.writeInt(uniformBlock.isArray);
914 stream.writeInt(uniformBlock.arrayElement);
915 stream.writeInt(uniformBlock.dataSize);
916
917 stream.writeInt(uniformBlock.vertexStaticUse);
918 stream.writeInt(uniformBlock.fragmentStaticUse);
919
920 stream.writeInt(uniformBlock.memberUniformIndexes.size());
921 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
922 {
923 stream.writeInt(memberUniformIndex);
924 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400925 }
926
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
928 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700929 {
930 stream.writeInt(varying.arraySize);
931 stream.writeInt(varying.type);
932 stream.writeString(varying.name);
933 }
934
Jamie Madill48ef11b2016-04-27 15:21:52 -0400935 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400936
Jamie Madill48ef11b2016-04-27 15:21:52 -0400937 stream.writeInt(mState.mOutputVariables.size());
938 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400939 {
940 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400941 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400942 stream.writeInt(outputPair.second.index);
943 stream.writeString(outputPair.second.name);
944 }
945
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400946 stream.writeInt(mSamplerUniformRange.start);
947 stream.writeInt(mSamplerUniformRange.end);
948
Geoff Lang7dd2e102014-11-10 15:19:26 -0500949 gl::Error error = mProgram->save(&stream);
950 if (error.isError())
951 {
952 return error;
953 }
954
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700955 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400956 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500957
958 if (streamLength > bufSize)
959 {
960 if (length)
961 {
962 *length = 0;
963 }
964
965 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
966 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
967 // sizes and then copy it.
968 return Error(GL_INVALID_OPERATION);
969 }
970
971 if (binary)
972 {
973 char *ptr = reinterpret_cast<char*>(binary);
974
Jamie Madill48ef11b2016-04-27 15:21:52 -0400975 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500976 ptr += streamLength;
977
978 ASSERT(ptr - streamLength == binary);
979 }
980
981 if (length)
982 {
983 *length = streamLength;
984 }
985
986 return Error(GL_NO_ERROR);
987}
988
989GLint Program::getBinaryLength() const
990{
991 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400992 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500993 if (error.isError())
994 {
995 return 0;
996 }
997
998 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000999}
1000
Geoff Langc5629752015-12-07 16:29:04 -05001001void Program::setBinaryRetrievableHint(bool retrievable)
1002{
1003 // TODO(jmadill) : replace with dirty bits
1004 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001005 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001006}
1007
1008bool Program::getBinaryRetrievableHint() const
1009{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001010 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001011}
1012
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013void Program::release()
1014{
1015 mRefCount--;
1016
1017 if (mRefCount == 0 && mDeleteStatus)
1018 {
1019 mResourceManager->deleteProgram(mHandle);
1020 }
1021}
1022
1023void Program::addRef()
1024{
1025 mRefCount++;
1026}
1027
1028unsigned int Program::getRefCount() const
1029{
1030 return mRefCount;
1031}
1032
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001033int Program::getInfoLogLength() const
1034{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001035 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001036}
1037
Geoff Lange1a27752015-10-05 13:16:04 -04001038void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001039{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001040 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001041}
1042
Geoff Lange1a27752015-10-05 13:16:04 -04001043void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001044{
1045 int total = 0;
1046
Martin Radev4c4c8e72016-08-04 12:25:34 +03001047 if (mState.mAttachedComputeShader)
1048 {
1049 if (total < maxCount)
1050 {
1051 shaders[total] = mState.mAttachedComputeShader->getHandle();
1052 total++;
1053 }
1054 }
1055
Jamie Madill48ef11b2016-04-27 15:21:52 -04001056 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001057 {
1058 if (total < maxCount)
1059 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001060 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001061 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001062 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001063 }
1064
Jamie Madill48ef11b2016-04-27 15:21:52 -04001065 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001066 {
1067 if (total < maxCount)
1068 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001069 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001070 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001071 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001072 }
1073
1074 if (count)
1075 {
1076 *count = total;
1077 }
1078}
1079
Geoff Lange1a27752015-10-05 13:16:04 -04001080GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001082 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001084 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001086 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087 }
1088 }
1089
Austin Kinrossb8af7232015-03-16 22:33:25 -07001090 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001091}
1092
Jamie Madill63805b42015-08-25 13:17:39 -04001093bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001094{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001095 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1096 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097}
1098
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001099void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1100{
Jamie Madillc349ec02015-08-21 16:53:12 -04001101 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001102 {
1103 if (bufsize > 0)
1104 {
1105 name[0] = '\0';
1106 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108 if (length)
1109 {
1110 *length = 0;
1111 }
1112
1113 *type = GL_NONE;
1114 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001115 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001117
1118 size_t attributeIndex = 0;
1119
Jamie Madill48ef11b2016-04-27 15:21:52 -04001120 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001121 {
1122 // Skip over inactive attributes
1123 if (attribute.staticUse)
1124 {
1125 if (static_cast<size_t>(index) == attributeIndex)
1126 {
1127 break;
1128 }
1129 attributeIndex++;
1130 }
1131 }
1132
Jamie Madill48ef11b2016-04-27 15:21:52 -04001133 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1134 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001135
1136 if (bufsize > 0)
1137 {
1138 const char *string = attrib.name.c_str();
1139
1140 strncpy(name, string, bufsize);
1141 name[bufsize - 1] = '\0';
1142
1143 if (length)
1144 {
1145 *length = static_cast<GLsizei>(strlen(name));
1146 }
1147 }
1148
1149 // Always a single 'type' instance
1150 *size = 1;
1151 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001152}
1153
Geoff Lange1a27752015-10-05 13:16:04 -04001154GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001155{
Jamie Madillc349ec02015-08-21 16:53:12 -04001156 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001157 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001158 return 0;
1159 }
1160
1161 GLint count = 0;
1162
Jamie Madill48ef11b2016-04-27 15:21:52 -04001163 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001164 {
1165 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001166 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001167
1168 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001169}
1170
Geoff Lange1a27752015-10-05 13:16:04 -04001171GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001172{
Jamie Madillc349ec02015-08-21 16:53:12 -04001173 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001174 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001175 return 0;
1176 }
1177
1178 size_t maxLength = 0;
1179
Jamie Madill48ef11b2016-04-27 15:21:52 -04001180 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001181 {
1182 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001183 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001184 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001185 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001186 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187
Jamie Madillc349ec02015-08-21 16:53:12 -04001188 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189}
1190
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191GLint Program::getFragDataLocation(const std::string &name) const
1192{
1193 std::string baseName(name);
1194 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001195 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001196 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001197 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1199 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001200 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001202 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001204}
1205
Geoff Lange1a27752015-10-05 13:16:04 -04001206void Program::getActiveUniform(GLuint index,
1207 GLsizei bufsize,
1208 GLsizei *length,
1209 GLint *size,
1210 GLenum *type,
1211 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001212{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001213 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001214 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001215 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 ASSERT(index < mState.mUniforms.size());
1217 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001218
1219 if (bufsize > 0)
1220 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001221 std::string string = uniform.name;
1222 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 {
1224 string += "[0]";
1225 }
1226
1227 strncpy(name, string.c_str(), bufsize);
1228 name[bufsize - 1] = '\0';
1229
1230 if (length)
1231 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001232 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233 }
1234 }
1235
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236 *size = uniform.elementCount();
1237 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001238 }
1239 else
1240 {
1241 if (bufsize > 0)
1242 {
1243 name[0] = '\0';
1244 }
1245
1246 if (length)
1247 {
1248 *length = 0;
1249 }
1250
1251 *size = 0;
1252 *type = GL_NONE;
1253 }
1254}
1255
Geoff Lange1a27752015-10-05 13:16:04 -04001256GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001257{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001259 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001260 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001261 }
1262 else
1263 {
1264 return 0;
1265 }
1266}
1267
Geoff Lange1a27752015-10-05 13:16:04 -04001268GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001269{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271
1272 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001273 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001274 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001275 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001276 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001278 size_t length = uniform.name.length() + 1u;
1279 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280 {
1281 length += 3; // Counting in "[0]".
1282 }
1283 maxLength = std::max(length, maxLength);
1284 }
1285 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001286 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287
Jamie Madill62d31cb2015-09-11 13:25:51 -04001288 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289}
1290
1291GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1292{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001293 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
1294 const gl::LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001296 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001297 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1298 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1299 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1300 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1301 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1302 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1303 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1304 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1305 default:
1306 UNREACHABLE();
1307 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001308 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309 return 0;
1310}
1311
1312bool Program::isValidUniformLocation(GLint location) const
1313{
Jamie Madille2e406c2016-06-02 13:04:10 -04001314 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001315 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1316 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001317}
1318
1319bool Program::isIgnoredUniformLocation(GLint location) const
1320{
1321 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1322 // out
1323 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001324 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1325 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326}
1327
Jamie Madill62d31cb2015-09-11 13:25:51 -04001328const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001330 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1331 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
Jamie Madill62d31cb2015-09-11 13:25:51 -04001334GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001336 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
Jamie Madill62d31cb2015-09-11 13:25:51 -04001339GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001341 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342}
1343
1344void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1345{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001346 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347 mProgram->setUniform1fv(location, count, v);
1348}
1349
1350void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1351{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001352 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353 mProgram->setUniform2fv(location, count, v);
1354}
1355
1356void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1357{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001358 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 mProgram->setUniform3fv(location, count, v);
1360}
1361
1362void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1363{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001364 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365 mProgram->setUniform4fv(location, count, v);
1366}
1367
1368void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1369{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001370 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371 mProgram->setUniform1iv(location, count, v);
1372}
1373
1374void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1375{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001376 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377 mProgram->setUniform2iv(location, count, v);
1378}
1379
1380void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1381{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001382 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383 mProgram->setUniform3iv(location, count, v);
1384}
1385
1386void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1387{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001388 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389 mProgram->setUniform4iv(location, count, v);
1390}
1391
1392void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1393{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001394 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395 mProgram->setUniform1uiv(location, count, v);
1396}
1397
1398void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1399{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001400 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401 mProgram->setUniform2uiv(location, count, v);
1402}
1403
1404void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1405{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001406 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407 mProgram->setUniform3uiv(location, count, v);
1408}
1409
1410void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1411{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001412 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 mProgram->setUniform4uiv(location, count, v);
1414}
1415
1416void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1417{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001418 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1420}
1421
1422void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1423{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001424 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1426}
1427
1428void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1429{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001430 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1432}
1433
1434void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1435{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001436 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1438}
1439
1440void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1441{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001442 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1444}
1445
1446void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1447{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001448 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1450}
1451
1452void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1453{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001454 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1456}
1457
1458void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1459{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001460 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1462}
1463
1464void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1465{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001466 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1468}
1469
Geoff Lange1a27752015-10-05 13:16:04 -04001470void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001472 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001473}
1474
Geoff Lange1a27752015-10-05 13:16:04 -04001475void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478}
1479
Geoff Lange1a27752015-10-05 13:16:04 -04001480void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001482 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483}
1484
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485void Program::flagForDeletion()
1486{
1487 mDeleteStatus = true;
1488}
1489
1490bool Program::isFlaggedForDeletion() const
1491{
1492 return mDeleteStatus;
1493}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001494
Brandon Jones43a53e22014-08-28 16:23:22 -07001495void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001496{
1497 mInfoLog.reset();
1498
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001500 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001501 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001502 }
1503 else
1504 {
Jamie Madillf6113162015-05-07 11:49:21 -04001505 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001506 }
1507}
1508
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1510{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001511 // Skip cache if we're using an infolog, so we get the full error.
1512 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1513 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1514 {
1515 return mCachedValidateSamplersResult.value();
1516 }
1517
1518 if (mTextureUnitTypesCache.empty())
1519 {
1520 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1521 }
1522 else
1523 {
1524 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1525 }
1526
1527 // if any two active samplers in a program are of different types, but refer to the same
1528 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1529 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1530 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1531 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1532 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001533 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001534 ASSERT(uniform.isSampler());
1535
1536 if (!uniform.staticUse)
1537 continue;
1538
1539 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1540 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1541
1542 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1543 {
1544 GLuint textureUnit = dataPtr[arrayElement];
1545
1546 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1547 {
1548 if (infoLog)
1549 {
1550 (*infoLog) << "Sampler uniform (" << textureUnit
1551 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1552 << caps.maxCombinedTextureImageUnits << ")";
1553 }
1554
1555 mCachedValidateSamplersResult = false;
1556 return false;
1557 }
1558
1559 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1560 {
1561 if (textureType != mTextureUnitTypesCache[textureUnit])
1562 {
1563 if (infoLog)
1564 {
1565 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1566 "image unit ("
1567 << textureUnit << ").";
1568 }
1569
1570 mCachedValidateSamplersResult = false;
1571 return false;
1572 }
1573 }
1574 else
1575 {
1576 mTextureUnitTypesCache[textureUnit] = textureType;
1577 }
1578 }
1579 }
1580
1581 mCachedValidateSamplersResult = true;
1582 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001585bool Program::isValidated() const
1586{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587 return mValidated;
1588}
1589
Geoff Lange1a27752015-10-05 13:16:04 -04001590GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001591{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001592 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593}
1594
1595void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1596{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001597 ASSERT(
1598 uniformBlockIndex <
1599 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001600
Jamie Madill48ef11b2016-04-27 15:21:52 -04001601 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001602
1603 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001604 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001605 std::string string = uniformBlock.name;
1606
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001608 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001609 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001610 }
1611
1612 strncpy(uniformBlockName, string.c_str(), bufSize);
1613 uniformBlockName[bufSize - 1] = '\0';
1614
1615 if (length)
1616 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001617 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001619 }
1620}
1621
Geoff Lange1a27752015-10-05 13:16:04 -04001622GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001623{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 int maxLength = 0;
1625
1626 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001627 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001628 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1630 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001631 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 if (!uniformBlock.name.empty())
1633 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001634 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635
1636 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001637 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638
1639 maxLength = std::max(length + arrayLength, maxLength);
1640 }
1641 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001642 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643
1644 return maxLength;
1645}
1646
Geoff Lange1a27752015-10-05 13:16:04 -04001647GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001649 size_t subscript = GL_INVALID_INDEX;
1650 std::string baseName = gl::ParseUniformName(name, &subscript);
1651
Jamie Madill48ef11b2016-04-27 15:21:52 -04001652 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1654 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001655 const gl::UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 if (uniformBlock.name == baseName)
1657 {
1658 const bool arrayElementZero =
1659 (subscript == GL_INVALID_INDEX &&
1660 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1661 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1662 {
1663 return blockIndex;
1664 }
1665 }
1666 }
1667
1668 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001669}
1670
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001672{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001673 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1674 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001675}
1676
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001677void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1678{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001679 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001680 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001681}
1682
1683GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1684{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001685 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001686}
1687
1688void Program::resetUniformBlockBindings()
1689{
1690 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1691 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001692 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001693 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001694 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001695}
1696
Geoff Lang48dcae72014-02-05 16:28:24 -05001697void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1698{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001699 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001700 for (GLsizei i = 0; i < count; i++)
1701 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001702 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001703 }
1704
Jamie Madill48ef11b2016-04-27 15:21:52 -04001705 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001706}
1707
1708void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1709{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001710 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001711 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001712 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1713 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001714 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1715 if (length)
1716 {
1717 *length = lastNameIdx;
1718 }
1719 if (size)
1720 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001721 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001722 }
1723 if (type)
1724 {
1725 *type = varying.type;
1726 }
1727 if (name)
1728 {
1729 memcpy(name, varying.name.c_str(), lastNameIdx);
1730 name[lastNameIdx] = '\0';
1731 }
1732 }
1733}
1734
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001735GLsizei Program::getTransformFeedbackVaryingCount() const
1736{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001737 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001738 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001739 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001740 }
1741 else
1742 {
1743 return 0;
1744 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001745}
1746
1747GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1748{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001749 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001750 {
1751 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001752 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001753 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001754 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1755 }
1756
1757 return maxSize;
1758 }
1759 else
1760 {
1761 return 0;
1762 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001763}
1764
1765GLenum Program::getTransformFeedbackBufferMode() const
1766{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001767 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001768}
1769
Jamie Madillada9ecc2015-08-17 12:53:37 -04001770bool Program::linkVaryings(InfoLog &infoLog,
1771 const Shader *vertexShader,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001772 const Shader *fragmentShader) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773{
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001774 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1775
Jamie Madill4cff2472015-08-21 16:53:18 -04001776 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1777 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778
Sami Väisänen46eaa942016-06-29 10:26:37 +03001779 std::map<GLuint, std::string> staticFragmentInputLocations;
1780
Jamie Madill4cff2472015-08-21 16:53:18 -04001781 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001783 bool matched = false;
1784
1785 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001786 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 {
1788 continue;
1789 }
1790
Jamie Madill4cff2472015-08-21 16:53:18 -04001791 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001793 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001794 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001795 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001796 if (!linkValidateVaryings(infoLog, output.name, input, output,
1797 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798 {
1799 return false;
1800 }
1801
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 matched = true;
1803 break;
1804 }
1805 }
1806
1807 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001808 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001810 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811 return false;
1812 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001813
1814 // Check for aliased path rendering input bindings (if any).
1815 // If more than one binding refer statically to the same
1816 // location the link must fail.
1817
1818 if (!output.staticUse)
1819 continue;
1820
1821 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1822 if (inputBinding == -1)
1823 continue;
1824
1825 const auto it = staticFragmentInputLocations.find(inputBinding);
1826 if (it == std::end(staticFragmentInputLocations))
1827 {
1828 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1829 }
1830 else
1831 {
1832 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1833 << it->second;
1834 return false;
1835 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001836 }
1837
Jamie Madillada9ecc2015-08-17 12:53:37 -04001838 // TODO(jmadill): verify no unmatched vertex varyings?
1839
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840 return true;
1841}
1842
Martin Radev4c4c8e72016-08-04 12:25:34 +03001843bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001844{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001845 // Check that uniforms defined in the vertex and fragment shaders are identical
1846 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001847 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1848 const std::vector<sh::Uniform> &fragmentUniforms =
1849 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001850
Jamie Madillea918db2015-08-18 14:48:59 -04001851 for (const sh::Uniform &vertexUniform : vertexUniforms)
1852 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001853 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001854 }
1855
1856 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1857 {
1858 auto entry = linkedUniforms.find(fragmentUniform.name);
1859 if (entry != linkedUniforms.end())
1860 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001861 LinkedUniform *vertexUniform = &entry->second;
1862 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1863 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001864 {
1865 return false;
1866 }
1867 }
1868 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001869 return true;
1870}
1871
1872bool Program::linkUniforms(gl::InfoLog &infoLog,
1873 const gl::Caps &caps,
1874 const Bindings &uniformBindings)
1875{
1876 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1877 {
1878 ASSERT(mState.mAttachedComputeShader == nullptr);
1879 if (!validateVertexAndFragmentUniforms(infoLog))
1880 {
1881 return false;
1882 }
1883 }
Jamie Madillea918db2015-08-18 14:48:59 -04001884
Jamie Madill62d31cb2015-09-11 13:25:51 -04001885 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1886 // Also check the maximum uniform vector and sampler counts.
1887 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1888 {
1889 return false;
1890 }
1891
Geoff Langd8605522016-04-13 10:19:12 -04001892 if (!indexUniforms(infoLog, caps, uniformBindings))
1893 {
1894 return false;
1895 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001896
Jamie Madillea918db2015-08-18 14:48:59 -04001897 return true;
1898}
1899
Geoff Langd8605522016-04-13 10:19:12 -04001900bool Program::indexUniforms(gl::InfoLog &infoLog,
1901 const gl::Caps &caps,
1902 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001903{
Geoff Langd8605522016-04-13 10:19:12 -04001904 // Uniforms awaiting a location
1905 std::vector<VariableLocation> unboundUniforms;
1906 std::map<GLuint, VariableLocation> boundUniforms;
1907 int maxUniformLocation = -1;
1908
1909 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001910 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001911 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001912 const gl::LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913
Geoff Langd8605522016-04-13 10:19:12 -04001914 if (uniform.isBuiltIn())
1915 {
1916 continue;
1917 }
1918
1919 int bindingLocation = uniformBindings.getBinding(uniform.name);
1920
1921 // Verify that this location isn't bound twice
1922 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1923 {
1924 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1925 return false;
1926 }
1927
Jamie Madill62d31cb2015-09-11 13:25:51 -04001928 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1929 {
Geoff Langd8605522016-04-13 10:19:12 -04001930 VariableLocation location(uniform.name, arrayIndex,
1931 static_cast<unsigned int>(uniformIndex));
1932
1933 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 {
Geoff Langd8605522016-04-13 10:19:12 -04001935 boundUniforms[bindingLocation] = location;
1936 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1937 }
1938 else
1939 {
1940 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941 }
1942 }
1943 }
Geoff Langd8605522016-04-13 10:19:12 -04001944
1945 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1946 // not be assigned to those locations.
1947 std::set<GLuint> reservedLocations;
1948 for (const auto &binding : uniformBindings)
1949 {
1950 GLuint location = binding.second;
1951 if (boundUniforms.find(location) == boundUniforms.end())
1952 {
1953 reservedLocations.insert(location);
1954 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1955 }
1956 }
1957
1958 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04001959 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04001960 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1961 static_cast<size_t>(maxUniformLocation + 1)));
1962
1963 // Assign bound uniforms
1964 for (const auto &boundUniform : boundUniforms)
1965 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001966 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04001967 }
1968
1969 // Assign reserved uniforms
1970 for (const auto &reservedLocation : reservedLocations)
1971 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001972 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04001973 }
1974
1975 // Assign unbound uniforms
1976 size_t nextUniformLocation = 0;
1977 for (const auto &unboundUniform : unboundUniforms)
1978 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001979 while (mState.mUniformLocations[nextUniformLocation].used ||
1980 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04001981 {
1982 nextUniformLocation++;
1983 }
1984
Jamie Madill48ef11b2016-04-27 15:21:52 -04001985 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
1986 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04001987 nextUniformLocation++;
1988 }
1989
1990 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991}
1992
Martin Radev4c4c8e72016-08-04 12:25:34 +03001993bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1994 const std::string &uniformName,
1995 const sh::InterfaceBlockField &vertexUniform,
1996 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997{
Jamie Madillc4c744222015-11-04 09:39:47 -05001998 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1999 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000 {
2001 return false;
2002 }
2003
2004 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2005 {
Jamie Madillf6113162015-05-07 11:49:21 -04002006 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 return false;
2008 }
2009
2010 return true;
2011}
2012
2013// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill9082b982016-04-27 15:21:51 -04002014bool Program::linkAttributes(const ContextState &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04002015 InfoLog &infoLog,
Geoff Langd8605522016-04-13 10:19:12 -04002016 const Bindings &attributeBindings,
Jamie Madill3da79b72015-04-27 11:09:17 -04002017 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002020 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002021 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002022
2023 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002024 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002025 {
Jamie Madillf6113162015-05-07 11:49:21 -04002026 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002027 return false;
2028 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002030 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002031
Jamie Madillc349ec02015-08-21 16:53:12 -04002032 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002033 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002034 {
2035 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 ASSERT(attribute.staticUse);
2037
Geoff Langd8605522016-04-13 10:19:12 -04002038 int bindingLocation = attributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002039 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002040 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002041 attribute.location = bindingLocation;
2042 }
2043
2044 if (attribute.location != -1)
2045 {
2046 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002047 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048
Jamie Madill63805b42015-08-25 13:17:39 -04002049 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 {
Jamie Madillf6113162015-05-07 11:49:21 -04002051 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002052 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053
2054 return false;
2055 }
2056
Jamie Madill63805b42015-08-25 13:17:39 -04002057 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 {
Jamie Madill63805b42015-08-25 13:17:39 -04002059 const int regLocation = attribute.location + reg;
2060 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061
2062 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002063 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002064 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002066 // TODO(jmadill): fix aliasing on ES2
2067 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002069 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002070 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 return false;
2072 }
2073 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002074 else
2075 {
Jamie Madill63805b42015-08-25 13:17:39 -04002076 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002077 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078
Jamie Madill63805b42015-08-25 13:17:39 -04002079 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080 }
2081 }
2082 }
2083
2084 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002085 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087 ASSERT(attribute.staticUse);
2088
Jamie Madillc349ec02015-08-21 16:53:12 -04002089 // Not set by glBindAttribLocation or by location layout qualifier
2090 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 {
Jamie Madill63805b42015-08-25 13:17:39 -04002092 int regs = VariableRegisterCount(attribute.type);
2093 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094
Jamie Madill63805b42015-08-25 13:17:39 -04002095 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 {
Jamie Madillf6113162015-05-07 11:49:21 -04002097 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002098 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 }
2100
Jamie Madillc349ec02015-08-21 16:53:12 -04002101 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102 }
2103 }
2104
Jamie Madill48ef11b2016-04-27 15:21:52 -04002105 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002107 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002108 ASSERT(attribute.location != -1);
2109 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002110
Jamie Madill63805b42015-08-25 13:17:39 -04002111 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002112 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002113 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114 }
2115 }
2116
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 return true;
2118}
2119
Martin Radev4c4c8e72016-08-04 12:25:34 +03002120bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2121 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2122 const std::string &errorMessage,
2123 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002125 GLuint blockCount = 0;
2126 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002128 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002129 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002130 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002131 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002132 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002133 return false;
2134 }
2135 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002136 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002137 return true;
2138}
Jamie Madille473dee2015-08-18 14:49:01 -04002139
Martin Radev4c4c8e72016-08-04 12:25:34 +03002140bool Program::validateVertexAndFragmentInterfaceBlocks(
2141 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2142 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2143 InfoLog &infoLog) const
2144{
2145 // Check that interface blocks defined in the vertex and fragment shaders are identical
2146 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2147 UniformBlockMap linkedUniformBlocks;
2148
2149 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2150 {
2151 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2152 }
2153
Jamie Madille473dee2015-08-18 14:49:01 -04002154 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155 {
Jamie Madille473dee2015-08-18 14:49:01 -04002156 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 if (entry != linkedUniformBlocks.end())
2158 {
2159 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2160 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2161 {
2162 return false;
2163 }
2164 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002165 }
2166 return true;
2167}
Jamie Madille473dee2015-08-18 14:49:01 -04002168
Martin Radev4c4c8e72016-08-04 12:25:34 +03002169bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2170{
2171 if (mState.mAttachedComputeShader)
2172 {
2173 const Shader &computeShader = *mState.mAttachedComputeShader;
2174 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2175
2176 if (!validateUniformBlocksCount(
2177 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2178 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2179 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002181 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002183 return true;
2184 }
2185
2186 const Shader &vertexShader = *mState.mAttachedVertexShader;
2187 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2188
2189 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2190 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2191
2192 if (!validateUniformBlocksCount(
2193 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2194 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2195 {
2196 return false;
2197 }
2198 if (!validateUniformBlocksCount(
2199 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2200 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2201 infoLog))
2202 {
2203
2204 return false;
2205 }
2206 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2207 infoLog))
2208 {
2209 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210 }
Jamie Madille473dee2015-08-18 14:49:01 -04002211
Geoff Lang7dd2e102014-11-10 15:19:26 -05002212 return true;
2213}
2214
Martin Radev4c4c8e72016-08-04 12:25:34 +03002215bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog,
2216 const sh::InterfaceBlock &vertexInterfaceBlock,
2217 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218{
2219 const char* blockName = vertexInterfaceBlock.name.c_str();
2220 // validate blocks for the same member types
2221 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2222 {
Jamie Madillf6113162015-05-07 11:49:21 -04002223 infoLog << "Types for interface block '" << blockName
2224 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002225 return false;
2226 }
2227 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2228 {
Jamie Madillf6113162015-05-07 11:49:21 -04002229 infoLog << "Array sizes differ for interface block '" << blockName
2230 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002231 return false;
2232 }
2233 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2234 {
Jamie Madillf6113162015-05-07 11:49:21 -04002235 infoLog << "Layout qualifiers differ for interface block '" << blockName
2236 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002237 return false;
2238 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002239 const unsigned int numBlockMembers =
2240 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002241 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2242 {
2243 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2244 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2245 if (vertexMember.name != fragmentMember.name)
2246 {
Jamie Madillf6113162015-05-07 11:49:21 -04002247 infoLog << "Name mismatch for field " << blockMemberIndex
2248 << " of interface block '" << blockName
2249 << "': (in vertex: '" << vertexMember.name
2250 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002251 return false;
2252 }
2253 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2254 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2255 {
2256 return false;
2257 }
2258 }
2259 return true;
2260}
2261
2262bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2263 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2264{
2265 if (vertexVariable.type != fragmentVariable.type)
2266 {
Jamie Madillf6113162015-05-07 11:49:21 -04002267 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 return false;
2269 }
2270 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2271 {
Jamie Madillf6113162015-05-07 11:49:21 -04002272 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002273 return false;
2274 }
2275 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2276 {
Jamie Madillf6113162015-05-07 11:49:21 -04002277 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002278 return false;
2279 }
2280
2281 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2282 {
Jamie Madillf6113162015-05-07 11:49:21 -04002283 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002284 return false;
2285 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002286 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002287 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2288 {
2289 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2290 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2291
2292 if (vertexMember.name != fragmentMember.name)
2293 {
Jamie Madillf6113162015-05-07 11:49:21 -04002294 infoLog << "Name mismatch for field '" << memberIndex
2295 << "' of " << variableName
2296 << ": (in vertex: '" << vertexMember.name
2297 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002298 return false;
2299 }
2300
2301 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2302 vertexMember.name + "'";
2303
2304 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2305 {
2306 return false;
2307 }
2308 }
2309
2310 return true;
2311}
2312
2313bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2314{
Cooper Partin1acf4382015-06-12 12:38:57 -07002315#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2316 const bool validatePrecision = true;
2317#else
2318 const bool validatePrecision = false;
2319#endif
2320
2321 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 {
2323 return false;
2324 }
2325
2326 return true;
2327}
2328
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002329bool Program::linkValidateVaryings(InfoLog &infoLog,
2330 const std::string &varyingName,
2331 const sh::Varying &vertexVarying,
2332 const sh::Varying &fragmentVarying,
2333 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334{
2335 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2336 {
2337 return false;
2338 }
2339
Jamie Madille9cc4692015-02-19 16:00:13 -05002340 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002341 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002342 infoLog << "Interpolation types for " << varyingName
2343 << " differ between vertex and fragment shaders.";
2344 return false;
2345 }
2346
2347 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2348 {
2349 infoLog << "Invariance for " << varyingName
2350 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351 return false;
2352 }
2353
2354 return true;
2355}
2356
Jamie Madillccdf74b2015-08-18 10:46:12 -04002357bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2358 const std::vector<const sh::Varying *> &varyings,
2359 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002360{
2361 size_t totalComponents = 0;
2362
Jamie Madillccdf74b2015-08-18 10:46:12 -04002363 std::set<std::string> uniqueNames;
2364
Jamie Madill48ef11b2016-04-27 15:21:52 -04002365 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002366 {
2367 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002368 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002370 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002372 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002373 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002374 infoLog << "Two transform feedback varyings specify the same output variable ("
2375 << tfVaryingName << ").";
2376 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002377 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002378 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379
Geoff Lang1a683462015-09-29 15:09:59 -04002380 if (varying->isArray())
2381 {
2382 infoLog << "Capture of arrays is undefined and not supported.";
2383 return false;
2384 }
2385
Jamie Madillccdf74b2015-08-18 10:46:12 -04002386 // TODO(jmadill): Investigate implementation limits on D3D11
2387 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002388 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002389 componentCount > caps.maxTransformFeedbackSeparateComponents)
2390 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002391 infoLog << "Transform feedback varying's " << varying->name << " components ("
2392 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002393 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002394 return false;
2395 }
2396
2397 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398 found = true;
2399 break;
2400 }
2401 }
2402
Jamie Madill89bb70e2015-08-31 14:18:39 -04002403 if (tfVaryingName.find('[') != std::string::npos)
2404 {
Geoff Lang1a683462015-09-29 15:09:59 -04002405 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002406 return false;
2407 }
2408
Geoff Lang7dd2e102014-11-10 15:19:26 -05002409 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2410 ASSERT(found);
2411 }
2412
Jamie Madill48ef11b2016-04-27 15:21:52 -04002413 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002414 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415 {
Jamie Madillf6113162015-05-07 11:49:21 -04002416 infoLog << "Transform feedback varying total components (" << totalComponents
2417 << ") exceed the maximum interleaved components ("
2418 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002419 return false;
2420 }
2421
2422 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002423}
2424
Jamie Madillccdf74b2015-08-18 10:46:12 -04002425void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2426{
2427 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002428 mState.mTransformFeedbackVaryingVars.clear();
2429 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002430 {
2431 for (const sh::Varying *varying : varyings)
2432 {
2433 if (tfVaryingName == varying->name)
2434 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002435 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436 break;
2437 }
2438 }
2439 }
2440}
2441
2442std::vector<const sh::Varying *> Program::getMergedVaryings() const
2443{
2444 std::set<std::string> uniqueNames;
2445 std::vector<const sh::Varying *> varyings;
2446
Jamie Madill48ef11b2016-04-27 15:21:52 -04002447 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002448 {
2449 if (uniqueNames.count(varying.name) == 0)
2450 {
2451 uniqueNames.insert(varying.name);
2452 varyings.push_back(&varying);
2453 }
2454 }
2455
Jamie Madill48ef11b2016-04-27 15:21:52 -04002456 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002457 {
2458 if (uniqueNames.count(varying.name) == 0)
2459 {
2460 uniqueNames.insert(varying.name);
2461 varyings.push_back(&varying);
2462 }
2463 }
2464
2465 return varyings;
2466}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002467
2468void Program::linkOutputVariables()
2469{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002470 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002471 ASSERT(fragmentShader != nullptr);
2472
2473 // Skip this step for GLES2 shaders.
2474 if (fragmentShader->getShaderVersion() == 100)
2475 return;
2476
Jamie Madilla0a9e122015-09-02 15:54:30 -04002477 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002478
2479 // TODO(jmadill): any caps validation here?
2480
2481 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2482 outputVariableIndex++)
2483 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002484 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002485
2486 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2487 if (outputVariable.isBuiltIn())
2488 continue;
2489
2490 // Since multiple output locations must be specified, use 0 for non-specified locations.
2491 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2492
2493 ASSERT(outputVariable.staticUse);
2494
2495 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2496 elementIndex++)
2497 {
2498 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002499 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002500 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002501 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002502 VariableLocation(outputVariable.name, element, outputVariableIndex);
2503 }
2504 }
2505}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002506
Martin Radev4c4c8e72016-08-04 12:25:34 +03002507bool Program::flattenUniformsAndCheckCapsForShader(const gl::Shader &shader,
2508 GLuint maxUniformComponents,
2509 GLuint maxTextureImageUnits,
2510 const std::string &componentsErrorMessage,
2511 const std::string &samplerErrorMessage,
2512 std::vector<LinkedUniform> &samplerUniforms,
2513 InfoLog &infoLog)
2514{
2515 VectorAndSamplerCount vasCount;
2516 for (const sh::Uniform &uniform : shader.getUniforms())
2517 {
2518 if (uniform.staticUse)
2519 {
2520 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2521 }
2522 }
2523
2524 if (vasCount.vectorCount > maxUniformComponents)
2525 {
2526 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2527 return false;
2528 }
2529
2530 if (vasCount.samplerCount > maxTextureImageUnits)
2531 {
2532 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2533 return false;
2534 }
2535
2536 return true;
2537}
2538
Jamie Madill62d31cb2015-09-11 13:25:51 -04002539bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2540{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002541 std::vector<LinkedUniform> samplerUniforms;
2542
Martin Radev4c4c8e72016-08-04 12:25:34 +03002543 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002544 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002545 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2546
2547 // TODO (mradev): check whether we need finer-grained component counting
2548 if (!flattenUniformsAndCheckCapsForShader(
2549 *computeShader, caps.maxComputeUniformComponents / 4,
2550 caps.maxComputeTextureImageUnits,
2551 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2552 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2553 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002554 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002555 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002556 }
2557 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002558 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002559 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002560 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002561
Martin Radev4c4c8e72016-08-04 12:25:34 +03002562 if (!flattenUniformsAndCheckCapsForShader(
2563 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2564 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2565 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2566 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002567 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002568 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002569 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002570 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002571
Martin Radev4c4c8e72016-08-04 12:25:34 +03002572 if (!flattenUniformsAndCheckCapsForShader(
2573 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2574 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2575 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2576 infoLog))
2577 {
2578 return false;
2579 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002580 }
2581
Jamie Madill48ef11b2016-04-27 15:21:52 -04002582 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002583 mSamplerUniformRange.end =
2584 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2585
Jamie Madill48ef11b2016-04-27 15:21:52 -04002586 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002587
Jamie Madill62d31cb2015-09-11 13:25:51 -04002588 return true;
2589}
2590
2591Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002592 const std::string &fullName,
2593 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002594{
2595 VectorAndSamplerCount vectorAndSamplerCount;
2596
2597 if (uniform.isStruct())
2598 {
2599 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2600 {
2601 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2602
2603 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2604 {
2605 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2606 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2607
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002608 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002609 }
2610 }
2611
2612 return vectorAndSamplerCount;
2613 }
2614
2615 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002616 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002617 if (!UniformInList(mState.getUniforms(), fullName) &&
2618 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002619 {
2620 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2621 uniform.arraySize, -1,
2622 sh::BlockMemberInfo::getDefaultBlockInfo());
2623 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002624
2625 // Store sampler uniforms separately, so we'll append them to the end of the list.
2626 if (isSampler)
2627 {
2628 samplerUniforms->push_back(linkedUniform);
2629 }
2630 else
2631 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002632 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002633 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002634 }
2635
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002636 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002637
2638 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2639 // Likewise, don't count "real" uniforms towards sampler count.
2640 vectorAndSamplerCount.vectorCount =
2641 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002642 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002643
2644 return vectorAndSamplerCount;
2645}
2646
2647void Program::gatherInterfaceBlockInfo()
2648{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002649 ASSERT(mState.mUniformBlocks.empty());
2650
2651 if (mState.mAttachedComputeShader)
2652 {
2653 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2654
2655 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2656 {
2657
2658 // Only 'packed' blocks are allowed to be considered inactive.
2659 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2660 continue;
2661
2662 for (gl::UniformBlock &block : mState.mUniformBlocks)
2663 {
2664 if (block.name == computeBlock.name)
2665 {
2666 block.computeStaticUse = computeBlock.staticUse;
2667 }
2668 }
2669
2670 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2671 }
2672 return;
2673 }
2674
Jamie Madill62d31cb2015-09-11 13:25:51 -04002675 std::set<std::string> visitedList;
2676
Jamie Madill48ef11b2016-04-27 15:21:52 -04002677 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002678
Jamie Madill62d31cb2015-09-11 13:25:51 -04002679 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2680 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002681 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002682 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2683 continue;
2684
2685 if (visitedList.count(vertexBlock.name) > 0)
2686 continue;
2687
2688 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2689 visitedList.insert(vertexBlock.name);
2690 }
2691
Jamie Madill48ef11b2016-04-27 15:21:52 -04002692 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002693
2694 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2695 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002696 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002697 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2698 continue;
2699
2700 if (visitedList.count(fragmentBlock.name) > 0)
2701 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002702 for (gl::UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002703 {
2704 if (block.name == fragmentBlock.name)
2705 {
2706 block.fragmentStaticUse = fragmentBlock.staticUse;
2707 }
2708 }
2709
2710 continue;
2711 }
2712
2713 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2714 visitedList.insert(fragmentBlock.name);
2715 }
2716}
2717
Jamie Madill4a3c2342015-10-08 12:58:45 -04002718template <typename VarT>
2719void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2720 const std::string &prefix,
2721 int blockIndex)
2722{
2723 for (const VarT &field : fields)
2724 {
2725 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2726
2727 if (field.isStruct())
2728 {
2729 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2730 {
2731 const std::string uniformElementName =
2732 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2733 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2734 }
2735 }
2736 else
2737 {
2738 // If getBlockMemberInfo returns false, the uniform is optimized out.
2739 sh::BlockMemberInfo memberInfo;
2740 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2741 {
2742 continue;
2743 }
2744
2745 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2746 blockIndex, memberInfo);
2747
2748 // Since block uniforms have no location, we don't need to store them in the uniform
2749 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002750 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002751 }
2752 }
2753}
2754
Jamie Madill62d31cb2015-09-11 13:25:51 -04002755void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2756{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002757 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002758 size_t blockSize = 0;
2759
2760 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002761 std::stringstream blockNameStr;
2762 blockNameStr << interfaceBlock.name;
2763 if (interfaceBlock.arraySize > 0)
2764 {
2765 blockNameStr << "[0]";
2766 }
2767 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002768 {
2769 return;
2770 }
2771
2772 // Track the first and last uniform index to determine the range of active uniforms in the
2773 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002774 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002775 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002776 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002777
2778 std::vector<unsigned int> blockUniformIndexes;
2779 for (size_t blockUniformIndex = firstBlockUniformIndex;
2780 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2781 {
2782 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2783 }
2784
2785 if (interfaceBlock.arraySize > 0)
2786 {
2787 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2788 {
2789 UniformBlock block(interfaceBlock.name, true, arrayElement);
2790 block.memberUniformIndexes = blockUniformIndexes;
2791
Martin Radev4c4c8e72016-08-04 12:25:34 +03002792 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002793 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002794 case GL_VERTEX_SHADER:
2795 {
2796 block.vertexStaticUse = interfaceBlock.staticUse;
2797 break;
2798 }
2799 case GL_FRAGMENT_SHADER:
2800 {
2801 block.fragmentStaticUse = interfaceBlock.staticUse;
2802 break;
2803 }
2804 case GL_COMPUTE_SHADER:
2805 {
2806 block.computeStaticUse = interfaceBlock.staticUse;
2807 break;
2808 }
2809 default:
2810 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002811 }
2812
Qin Jiajia0350a642016-11-01 17:01:51 +08002813 // Since all block elements in an array share the same active uniforms, they will all be
2814 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2815 // here we will add every block element in the array.
2816 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002817 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818 }
2819 }
2820 else
2821 {
2822 UniformBlock block(interfaceBlock.name, false, 0);
2823 block.memberUniformIndexes = blockUniformIndexes;
2824
Martin Radev4c4c8e72016-08-04 12:25:34 +03002825 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002827 case GL_VERTEX_SHADER:
2828 {
2829 block.vertexStaticUse = interfaceBlock.staticUse;
2830 break;
2831 }
2832 case GL_FRAGMENT_SHADER:
2833 {
2834 block.fragmentStaticUse = interfaceBlock.staticUse;
2835 break;
2836 }
2837 case GL_COMPUTE_SHADER:
2838 {
2839 block.computeStaticUse = interfaceBlock.staticUse;
2840 break;
2841 }
2842 default:
2843 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002844 }
2845
Jamie Madill4a3c2342015-10-08 12:58:45 -04002846 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002847 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002848 }
2849}
2850
2851template <typename T>
2852void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2853{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002854 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2855 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2857
2858 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2859 {
2860 // Do a cast conversion for boolean types. From the spec:
2861 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2862 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2863 for (GLsizei component = 0; component < count; ++component)
2864 {
2865 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2866 }
2867 }
2868 else
2869 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002870 // Invalide the validation cache if we modify the sampler data.
2871 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2872 {
2873 mCachedValidateSamplersResult.reset();
2874 }
2875
Jamie Madill62d31cb2015-09-11 13:25:51 -04002876 memcpy(destPointer, v, sizeof(T) * count);
2877 }
2878}
2879
2880template <size_t cols, size_t rows, typename T>
2881void Program::setMatrixUniformInternal(GLint location,
2882 GLsizei count,
2883 GLboolean transpose,
2884 const T *v)
2885{
2886 if (!transpose)
2887 {
2888 setUniformInternal(location, count * cols * rows, v);
2889 return;
2890 }
2891
2892 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002893 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2894 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002895 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2896 for (GLsizei element = 0; element < count; ++element)
2897 {
2898 size_t elementOffset = element * rows * cols;
2899
2900 for (size_t row = 0; row < rows; ++row)
2901 {
2902 for (size_t col = 0; col < cols; ++col)
2903 {
2904 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2905 }
2906 }
2907 }
2908}
2909
2910template <typename DestT>
2911void Program::getUniformInternal(GLint location, DestT *dataOut) const
2912{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002913 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2914 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002915
2916 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2917
2918 GLenum componentType = VariableComponentType(uniform.type);
2919 if (componentType == GLTypeToGLenum<DestT>::value)
2920 {
2921 memcpy(dataOut, srcPointer, uniform.getElementSize());
2922 return;
2923 }
2924
Corentin Wallez6596c462016-03-17 17:26:58 -04002925 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926
2927 switch (componentType)
2928 {
2929 case GL_INT:
2930 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2931 break;
2932 case GL_UNSIGNED_INT:
2933 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2934 break;
2935 case GL_BOOL:
2936 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2937 break;
2938 case GL_FLOAT:
2939 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2940 break;
2941 default:
2942 UNREACHABLE();
2943 }
2944}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002945}