blob: 191cdfee3167bf6b852b79b3fa2f560fac087d7b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Jamie Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie 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;
Jamie Madilla2c74982016-12-12 11:20:42 -0500286 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400287
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;
Jamie Madilla2c74982016-12-12 11:20:42 -0500324 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400325
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
Jamie Madillb0a838b2016-11-13 20:02:12 -0500600 ANGLE_TRY_RESULT(mProgram->link(data, mInfoLog), mLinked);
601 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300602 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500603 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300604 }
605 }
606 else
607 {
608 if (!mState.mAttachedFragmentShader || !mState.mAttachedFragmentShader->isCompiled())
609 {
610 return NoError();
611 }
612 ASSERT(mState.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
613
614 if (!mState.mAttachedVertexShader || !mState.mAttachedVertexShader->isCompiled())
615 {
616 return NoError();
617 }
618 ASSERT(mState.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
619
620 if (mState.mAttachedFragmentShader->getShaderVersion() !=
621 mState.mAttachedVertexShader->getShaderVersion())
622 {
623 mInfoLog << "Fragment shader version does not match vertex shader version.";
624 return NoError();
625 }
626
Jamie Madilleb979bf2016-11-15 12:28:46 -0500627 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300628 {
629 return NoError();
630 }
631
632 if (!linkVaryings(mInfoLog, mState.mAttachedVertexShader, mState.mAttachedFragmentShader))
633 {
634 return NoError();
635 }
636
637 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
638 {
639 return NoError();
640 }
641
642 if (!linkUniformBlocks(mInfoLog, caps))
643 {
644 return NoError();
645 }
646
647 const auto &mergedVaryings = getMergedVaryings();
648
649 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
650 {
651 return NoError();
652 }
653
654 linkOutputVariables();
655
Jamie Madillb0a838b2016-11-13 20:02:12 -0500656 ANGLE_TRY_RESULT(mProgram->link(data, mInfoLog), mLinked);
657 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500659 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660 }
661
662 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500663 }
664
Jamie Madill4a3c2342015-10-08 12:58:45 -0400665 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400666
Martin Radev4c4c8e72016-08-04 12:25:34 +0300667 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000668}
669
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000670// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671void Program::unlink(bool destroy)
672{
673 if (destroy) // Object being destructed
674 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400675 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400677 mState.mAttachedFragmentShader->release();
678 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 }
680
Jamie Madill48ef11b2016-04-27 15:21:52 -0400681 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400683 mState.mAttachedVertexShader->release();
684 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300686
687 if (mState.mAttachedComputeShader)
688 {
689 mState.mAttachedComputeShader->release();
690 mState.mAttachedComputeShader = nullptr;
691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692 }
693
Jamie Madill48ef11b2016-04-27 15:21:52 -0400694 mState.mAttributes.clear();
695 mState.mActiveAttribLocationsMask.reset();
696 mState.mTransformFeedbackVaryingVars.clear();
697 mState.mUniforms.clear();
698 mState.mUniformLocations.clear();
699 mState.mUniformBlocks.clear();
700 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701 mState.mComputeShaderLocalSize.fill(1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500702
Geoff Lang7dd2e102014-11-10 15:19:26 -0500703 mValidated = false;
704
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000705 mLinked = false;
706}
707
Geoff Lange1a27752015-10-05 13:16:04 -0400708bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000709{
710 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711}
712
Jamie Madilla2c74982016-12-12 11:20:42 -0500713Error Program::loadBinary(const Context *context,
714 GLenum binaryFormat,
715 const void *binary,
716 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000717{
718 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000719
Geoff Lang7dd2e102014-11-10 15:19:26 -0500720#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
721 return Error(GL_NO_ERROR);
722#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400723 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
724 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000725 {
Jamie Madillf6113162015-05-07 11:49:21 -0400726 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500727 return Error(GL_NO_ERROR);
728 }
729
Geoff Langc46cc2f2015-10-01 17:16:20 -0400730 BinaryInputStream stream(binary, length);
731
Jamie Madilla2c74982016-12-12 11:20:42 -0500732 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
733 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
734 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
735 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500736 {
Jamie Madillf6113162015-05-07 11:49:21 -0400737 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500738 return Error(GL_NO_ERROR);
739 }
740
Jamie Madilla2c74982016-12-12 11:20:42 -0500741 int majorVersion = stream.readInt<int>();
742 int minorVersion = stream.readInt<int>();
743 if (majorVersion != context->getClientMajorVersion() ||
744 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500745 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500746 mInfoLog << "Cannot load program binaries across different ES context versions.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500747 return Error(GL_NO_ERROR);
748 }
749
Martin Radev4c4c8e72016-08-04 12:25:34 +0300750 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
751 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
752 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
753
Jamie Madill63805b42015-08-25 13:17:39 -0400754 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
755 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400756 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500757
Jamie Madill3da79b72015-04-27 11:09:17 -0400758 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400759 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400760 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
761 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400762 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400763 LoadShaderVar(&stream, &attrib);
764 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400765 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400766 }
767
Jamie Madill62d31cb2015-09-11 13:25:51 -0400768 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400769 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400770 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
771 {
772 LinkedUniform uniform;
773 LoadShaderVar(&stream, &uniform);
774
775 uniform.blockIndex = stream.readInt<int>();
776 uniform.blockInfo.offset = stream.readInt<int>();
777 uniform.blockInfo.arrayStride = stream.readInt<int>();
778 uniform.blockInfo.matrixStride = stream.readInt<int>();
779 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
780
Jamie Madill48ef11b2016-04-27 15:21:52 -0400781 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400782 }
783
784 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400785 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400786 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
787 uniformIndexIndex++)
788 {
789 VariableLocation variable;
790 stream.readString(&variable.name);
791 stream.readInt(&variable.element);
792 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400793 stream.readBool(&variable.used);
794 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400795
Jamie Madill48ef11b2016-04-27 15:21:52 -0400796 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400797 }
798
799 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400800 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400801 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
802 ++uniformBlockIndex)
803 {
804 UniformBlock uniformBlock;
805 stream.readString(&uniformBlock.name);
806 stream.readBool(&uniformBlock.isArray);
807 stream.readInt(&uniformBlock.arrayElement);
808 stream.readInt(&uniformBlock.dataSize);
809 stream.readBool(&uniformBlock.vertexStaticUse);
810 stream.readBool(&uniformBlock.fragmentStaticUse);
811
812 unsigned int numMembers = stream.readInt<unsigned int>();
813 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
814 {
815 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
816 }
817
Jamie Madill48ef11b2016-04-27 15:21:52 -0400818 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400819 }
820
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500821 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
822 ++bindingIndex)
823 {
824 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
825 mState.mActiveUniformBlockBindings.set(bindingIndex,
826 mState.mUniformBlockBindings[bindingIndex] != 0);
827 }
828
Brandon Jones1048ea72015-10-06 15:34:52 -0700829 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400830 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700831 for (unsigned int transformFeedbackVaryingIndex = 0;
832 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
833 ++transformFeedbackVaryingIndex)
834 {
835 sh::Varying varying;
836 stream.readInt(&varying.arraySize);
837 stream.readInt(&varying.type);
838 stream.readString(&varying.name);
839
Jamie Madill48ef11b2016-04-27 15:21:52 -0400840 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700841 }
842
Jamie Madill48ef11b2016-04-27 15:21:52 -0400843 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400844
Jamie Madill80a6fc02015-08-21 16:53:16 -0400845 unsigned int outputVarCount = stream.readInt<unsigned int>();
846 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
847 {
848 int locationIndex = stream.readInt<int>();
849 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400850 stream.readInt(&locationData.element);
851 stream.readInt(&locationData.index);
852 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400853 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400854 }
855
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400856 stream.readInt(&mSamplerUniformRange.start);
857 stream.readInt(&mSamplerUniformRange.end);
858
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500859 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000860
Jamie Madillb0a838b2016-11-13 20:02:12 -0500861 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500862#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500863}
864
Jamie Madilla2c74982016-12-12 11:20:42 -0500865Error Program::saveBinary(const Context *context,
866 GLenum *binaryFormat,
867 void *binary,
868 GLsizei bufSize,
869 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500870{
871 if (binaryFormat)
872 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400873 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500874 }
875
876 BinaryOutputStream stream;
877
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
879
Jamie Madilla2c74982016-12-12 11:20:42 -0500880 // nullptr context is supported when computing binary length.
881 if (context)
882 {
883 stream.writeInt(context->getClientVersion().major);
884 stream.writeInt(context->getClientVersion().minor);
885 }
886 else
887 {
888 stream.writeInt(2);
889 stream.writeInt(0);
890 }
891
Martin Radev4c4c8e72016-08-04 12:25:34 +0300892 stream.writeInt(mState.mComputeShaderLocalSize[0]);
893 stream.writeInt(mState.mComputeShaderLocalSize[1]);
894 stream.writeInt(mState.mComputeShaderLocalSize[2]);
895
Jamie Madill48ef11b2016-04-27 15:21:52 -0400896 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500897
Jamie Madill48ef11b2016-04-27 15:21:52 -0400898 stream.writeInt(mState.mAttributes.size());
899 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400900 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400901 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400902 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400903 }
904
Jamie Madill48ef11b2016-04-27 15:21:52 -0400905 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500906 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400907 {
908 WriteShaderVar(&stream, uniform);
909
910 // FIXME: referenced
911
912 stream.writeInt(uniform.blockIndex);
913 stream.writeInt(uniform.blockInfo.offset);
914 stream.writeInt(uniform.blockInfo.arrayStride);
915 stream.writeInt(uniform.blockInfo.matrixStride);
916 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
917 }
918
Jamie Madill48ef11b2016-04-27 15:21:52 -0400919 stream.writeInt(mState.mUniformLocations.size());
920 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400921 {
922 stream.writeString(variable.name);
923 stream.writeInt(variable.element);
924 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400925 stream.writeInt(variable.used);
926 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400927 }
928
Jamie Madill48ef11b2016-04-27 15:21:52 -0400929 stream.writeInt(mState.mUniformBlocks.size());
930 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400931 {
932 stream.writeString(uniformBlock.name);
933 stream.writeInt(uniformBlock.isArray);
934 stream.writeInt(uniformBlock.arrayElement);
935 stream.writeInt(uniformBlock.dataSize);
936
937 stream.writeInt(uniformBlock.vertexStaticUse);
938 stream.writeInt(uniformBlock.fragmentStaticUse);
939
940 stream.writeInt(uniformBlock.memberUniformIndexes.size());
941 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
942 {
943 stream.writeInt(memberUniformIndex);
944 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400945 }
946
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500947 for (GLuint binding : mState.mUniformBlockBindings)
948 {
949 stream.writeInt(binding);
950 }
951
Jamie Madill48ef11b2016-04-27 15:21:52 -0400952 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
953 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700954 {
955 stream.writeInt(varying.arraySize);
956 stream.writeInt(varying.type);
957 stream.writeString(varying.name);
958 }
959
Jamie Madill48ef11b2016-04-27 15:21:52 -0400960 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400961
Jamie Madill48ef11b2016-04-27 15:21:52 -0400962 stream.writeInt(mState.mOutputVariables.size());
963 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400964 {
965 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400966 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400967 stream.writeInt(outputPair.second.index);
968 stream.writeString(outputPair.second.name);
969 }
970
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400971 stream.writeInt(mSamplerUniformRange.start);
972 stream.writeInt(mSamplerUniformRange.end);
973
Jamie Madilla2c74982016-12-12 11:20:42 -0500974 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -0500975
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700976 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400977 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500978
979 if (streamLength > bufSize)
980 {
981 if (length)
982 {
983 *length = 0;
984 }
985
986 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
987 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
988 // sizes and then copy it.
989 return Error(GL_INVALID_OPERATION);
990 }
991
992 if (binary)
993 {
994 char *ptr = reinterpret_cast<char*>(binary);
995
Jamie Madill48ef11b2016-04-27 15:21:52 -0400996 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997 ptr += streamLength;
998
999 ASSERT(ptr - streamLength == binary);
1000 }
1001
1002 if (length)
1003 {
1004 *length = streamLength;
1005 }
1006
1007 return Error(GL_NO_ERROR);
1008}
1009
1010GLint Program::getBinaryLength() const
1011{
1012 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001013 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001014 if (error.isError())
1015 {
1016 return 0;
1017 }
1018
1019 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001020}
1021
Geoff Langc5629752015-12-07 16:29:04 -05001022void Program::setBinaryRetrievableHint(bool retrievable)
1023{
1024 // TODO(jmadill) : replace with dirty bits
1025 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001026 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001027}
1028
1029bool Program::getBinaryRetrievableHint() const
1030{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001031 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001032}
1033
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001034void Program::release()
1035{
1036 mRefCount--;
1037
1038 if (mRefCount == 0 && mDeleteStatus)
1039 {
1040 mResourceManager->deleteProgram(mHandle);
1041 }
1042}
1043
1044void Program::addRef()
1045{
1046 mRefCount++;
1047}
1048
1049unsigned int Program::getRefCount() const
1050{
1051 return mRefCount;
1052}
1053
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001054int Program::getInfoLogLength() const
1055{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001056 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001057}
1058
Geoff Lange1a27752015-10-05 13:16:04 -04001059void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001060{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001061 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001062}
1063
Geoff Lange1a27752015-10-05 13:16:04 -04001064void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001065{
1066 int total = 0;
1067
Martin Radev4c4c8e72016-08-04 12:25:34 +03001068 if (mState.mAttachedComputeShader)
1069 {
1070 if (total < maxCount)
1071 {
1072 shaders[total] = mState.mAttachedComputeShader->getHandle();
1073 total++;
1074 }
1075 }
1076
Jamie Madill48ef11b2016-04-27 15:21:52 -04001077 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001078 {
1079 if (total < maxCount)
1080 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001081 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001082 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001083 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001084 }
1085
Jamie Madill48ef11b2016-04-27 15:21:52 -04001086 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001087 {
1088 if (total < maxCount)
1089 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001090 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001091 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001092 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001093 }
1094
1095 if (count)
1096 {
1097 *count = total;
1098 }
1099}
1100
Geoff Lange1a27752015-10-05 13:16:04 -04001101GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001102{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001103 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001105 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001107 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001108 }
1109 }
1110
Austin Kinrossb8af7232015-03-16 22:33:25 -07001111 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001112}
1113
Jamie Madill63805b42015-08-25 13:17:39 -04001114bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001115{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001116 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1117 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001118}
1119
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001120void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1121{
Jamie Madillc349ec02015-08-21 16:53:12 -04001122 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001123 {
1124 if (bufsize > 0)
1125 {
1126 name[0] = '\0';
1127 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001129 if (length)
1130 {
1131 *length = 0;
1132 }
1133
1134 *type = GL_NONE;
1135 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001136 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001137 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001138
1139 size_t attributeIndex = 0;
1140
Jamie Madill48ef11b2016-04-27 15:21:52 -04001141 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001142 {
1143 // Skip over inactive attributes
1144 if (attribute.staticUse)
1145 {
1146 if (static_cast<size_t>(index) == attributeIndex)
1147 {
1148 break;
1149 }
1150 attributeIndex++;
1151 }
1152 }
1153
Jamie Madill48ef11b2016-04-27 15:21:52 -04001154 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1155 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001156
1157 if (bufsize > 0)
1158 {
1159 const char *string = attrib.name.c_str();
1160
1161 strncpy(name, string, bufsize);
1162 name[bufsize - 1] = '\0';
1163
1164 if (length)
1165 {
1166 *length = static_cast<GLsizei>(strlen(name));
1167 }
1168 }
1169
1170 // Always a single 'type' instance
1171 *size = 1;
1172 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001173}
1174
Geoff Lange1a27752015-10-05 13:16:04 -04001175GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001176{
Jamie Madillc349ec02015-08-21 16:53:12 -04001177 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001178 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001179 return 0;
1180 }
1181
1182 GLint count = 0;
1183
Jamie Madill48ef11b2016-04-27 15:21:52 -04001184 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001185 {
1186 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001187 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001188
1189 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001190}
1191
Geoff Lange1a27752015-10-05 13:16:04 -04001192GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001193{
Jamie Madillc349ec02015-08-21 16:53:12 -04001194 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001195 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001196 return 0;
1197 }
1198
1199 size_t maxLength = 0;
1200
Jamie Madill48ef11b2016-04-27 15:21:52 -04001201 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001202 {
1203 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001204 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001205 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001206 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001207 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001208
Jamie Madillc349ec02015-08-21 16:53:12 -04001209 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001210}
1211
Geoff Lang7dd2e102014-11-10 15:19:26 -05001212GLint Program::getFragDataLocation(const std::string &name) const
1213{
1214 std::string baseName(name);
1215 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001217 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001218 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001219 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1220 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001221 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001223 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001224 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001225}
1226
Geoff Lange1a27752015-10-05 13:16:04 -04001227void Program::getActiveUniform(GLuint index,
1228 GLsizei bufsize,
1229 GLsizei *length,
1230 GLint *size,
1231 GLenum *type,
1232 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001233{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001235 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001237 ASSERT(index < mState.mUniforms.size());
1238 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001239
1240 if (bufsize > 0)
1241 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001242 std::string string = uniform.name;
1243 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001244 {
1245 string += "[0]";
1246 }
1247
1248 strncpy(name, string.c_str(), bufsize);
1249 name[bufsize - 1] = '\0';
1250
1251 if (length)
1252 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001253 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001254 }
1255 }
1256
Jamie Madill62d31cb2015-09-11 13:25:51 -04001257 *size = uniform.elementCount();
1258 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001259 }
1260 else
1261 {
1262 if (bufsize > 0)
1263 {
1264 name[0] = '\0';
1265 }
1266
1267 if (length)
1268 {
1269 *length = 0;
1270 }
1271
1272 *size = 0;
1273 *type = GL_NONE;
1274 }
1275}
1276
Geoff Lange1a27752015-10-05 13:16:04 -04001277GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001278{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001279 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001280 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001281 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001282 }
1283 else
1284 {
1285 return 0;
1286 }
1287}
1288
Geoff Lange1a27752015-10-05 13:16:04 -04001289GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001290{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001291 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292
1293 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001294 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001295 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001296 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001297 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001298 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001299 size_t length = uniform.name.length() + 1u;
1300 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001301 {
1302 length += 3; // Counting in "[0]".
1303 }
1304 maxLength = std::max(length, maxLength);
1305 }
1306 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001307 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308
Jamie Madill62d31cb2015-09-11 13:25:51 -04001309 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001310}
1311
1312GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1313{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001314 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001315 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001316 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001317 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001318 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1319 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1320 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1321 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1322 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1323 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1324 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1325 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1326 default:
1327 UNREACHABLE();
1328 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001329 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330 return 0;
1331}
1332
1333bool Program::isValidUniformLocation(GLint location) const
1334{
Jamie Madille2e406c2016-06-02 13:04:10 -04001335 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001336 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1337 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001338}
1339
1340bool Program::isIgnoredUniformLocation(GLint location) const
1341{
1342 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1343 // out
1344 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001345 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1346 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347}
1348
Jamie Madill62d31cb2015-09-11 13:25:51 -04001349const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001350{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001351 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1352 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
Jamie Madill62d31cb2015-09-11 13:25:51 -04001355GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001357 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001358}
1359
Jamie Madill62d31cb2015-09-11 13:25:51 -04001360GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001362 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001363}
1364
1365void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1366{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001367 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1368 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001369}
1370
1371void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1372{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001373 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1374 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375}
1376
1377void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1378{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001379 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1380 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381}
1382
1383void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1384{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001385 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1386 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387}
1388
1389void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1390{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001391 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1392 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001393}
1394
1395void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1396{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001397 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1398 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001399}
1400
1401void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1402{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001403 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1404 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405}
1406
1407void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1408{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001409 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1410 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411}
1412
1413void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1414{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001415 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1416 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417}
1418
1419void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1420{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001421 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1422 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423}
1424
1425void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1426{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001427 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1428 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429}
1430
1431void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1432{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001433 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1434 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435}
1436
1437void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1438{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001439 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1440 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001441}
1442
1443void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1444{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001445 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1446 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001447}
1448
1449void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1450{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001451 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1452 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001453}
1454
1455void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1456{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001457 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1458 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459}
1460
1461void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1462{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001463 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1464 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465}
1466
1467void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1468{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001469 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1470 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471}
1472
1473void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1474{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001475 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1476 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477}
1478
1479void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1480{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001481 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1482 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483}
1484
1485void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1486{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001487 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1488 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489}
1490
Geoff Lange1a27752015-10-05 13:16:04 -04001491void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001493 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494}
1495
Geoff Lange1a27752015-10-05 13:16:04 -04001496void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499}
1500
Geoff Lange1a27752015-10-05 13:16:04 -04001501void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001502{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506void Program::flagForDeletion()
1507{
1508 mDeleteStatus = true;
1509}
1510
1511bool Program::isFlaggedForDeletion() const
1512{
1513 return mDeleteStatus;
1514}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001515
Brandon Jones43a53e22014-08-28 16:23:22 -07001516void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001517{
1518 mInfoLog.reset();
1519
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001521 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001522 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001523 }
1524 else
1525 {
Jamie Madillf6113162015-05-07 11:49:21 -04001526 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001527 }
1528}
1529
Geoff Lang7dd2e102014-11-10 15:19:26 -05001530bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1531{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001532 // Skip cache if we're using an infolog, so we get the full error.
1533 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1534 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1535 {
1536 return mCachedValidateSamplersResult.value();
1537 }
1538
1539 if (mTextureUnitTypesCache.empty())
1540 {
1541 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1542 }
1543 else
1544 {
1545 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1546 }
1547
1548 // if any two active samplers in a program are of different types, but refer to the same
1549 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1550 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1551 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1552 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1553 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001554 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001555 ASSERT(uniform.isSampler());
1556
1557 if (!uniform.staticUse)
1558 continue;
1559
1560 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1561 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1562
1563 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1564 {
1565 GLuint textureUnit = dataPtr[arrayElement];
1566
1567 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1568 {
1569 if (infoLog)
1570 {
1571 (*infoLog) << "Sampler uniform (" << textureUnit
1572 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1573 << caps.maxCombinedTextureImageUnits << ")";
1574 }
1575
1576 mCachedValidateSamplersResult = false;
1577 return false;
1578 }
1579
1580 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1581 {
1582 if (textureType != mTextureUnitTypesCache[textureUnit])
1583 {
1584 if (infoLog)
1585 {
1586 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1587 "image unit ("
1588 << textureUnit << ").";
1589 }
1590
1591 mCachedValidateSamplersResult = false;
1592 return false;
1593 }
1594 }
1595 else
1596 {
1597 mTextureUnitTypesCache[textureUnit] = textureType;
1598 }
1599 }
1600 }
1601
1602 mCachedValidateSamplersResult = true;
1603 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604}
1605
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001606bool Program::isValidated() const
1607{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001608 return mValidated;
1609}
1610
Geoff Lange1a27752015-10-05 13:16:04 -04001611GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001612{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001613 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614}
1615
1616void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1617{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001618 ASSERT(
1619 uniformBlockIndex <
1620 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621
Jamie Madill48ef11b2016-04-27 15:21:52 -04001622 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623
1624 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001625 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001626 std::string string = uniformBlock.name;
1627
Jamie Madill62d31cb2015-09-11 13:25:51 -04001628 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631 }
1632
1633 strncpy(uniformBlockName, string.c_str(), bufSize);
1634 uniformBlockName[bufSize - 1] = '\0';
1635
1636 if (length)
1637 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001638 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001640 }
1641}
1642
Geoff Lange1a27752015-10-05 13:16:04 -04001643GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001644{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645 int maxLength = 0;
1646
1647 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001648 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001649 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1651 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001652 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653 if (!uniformBlock.name.empty())
1654 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001655 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656
1657 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001658 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001659
1660 maxLength = std::max(length + arrayLength, maxLength);
1661 }
1662 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001663 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001664
1665 return maxLength;
1666}
1667
Geoff Lange1a27752015-10-05 13:16:04 -04001668GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001669{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001670 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001671 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001672
Jamie Madill48ef11b2016-04-27 15:21:52 -04001673 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1675 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001676 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001677 if (uniformBlock.name == baseName)
1678 {
1679 const bool arrayElementZero =
1680 (subscript == GL_INVALID_INDEX &&
1681 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1682 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1683 {
1684 return blockIndex;
1685 }
1686 }
1687 }
1688
1689 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001690}
1691
Jamie Madill62d31cb2015-09-11 13:25:51 -04001692const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001693{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001694 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1695 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001696}
1697
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001698void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1699{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001700 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001701 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001702 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001703}
1704
1705GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1706{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001707 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001708}
1709
1710void Program::resetUniformBlockBindings()
1711{
1712 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1713 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001714 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001715 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001716 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001717}
1718
Geoff Lang48dcae72014-02-05 16:28:24 -05001719void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1720{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001722 for (GLsizei i = 0; i < count; i++)
1723 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001724 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001725 }
1726
Jamie Madill48ef11b2016-04-27 15:21:52 -04001727 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001728}
1729
1730void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1731{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001732 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001733 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001734 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1735 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001736 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1737 if (length)
1738 {
1739 *length = lastNameIdx;
1740 }
1741 if (size)
1742 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001743 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001744 }
1745 if (type)
1746 {
1747 *type = varying.type;
1748 }
1749 if (name)
1750 {
1751 memcpy(name, varying.name.c_str(), lastNameIdx);
1752 name[lastNameIdx] = '\0';
1753 }
1754 }
1755}
1756
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001757GLsizei Program::getTransformFeedbackVaryingCount() const
1758{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001759 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001760 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001761 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001762 }
1763 else
1764 {
1765 return 0;
1766 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001767}
1768
1769GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1770{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001772 {
1773 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001774 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001775 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001776 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1777 }
1778
1779 return maxSize;
1780 }
1781 else
1782 {
1783 return 0;
1784 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001785}
1786
1787GLenum Program::getTransformFeedbackBufferMode() const
1788{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001789 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001790}
1791
Jamie Madillada9ecc2015-08-17 12:53:37 -04001792bool Program::linkVaryings(InfoLog &infoLog,
1793 const Shader *vertexShader,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001794 const Shader *fragmentShader) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795{
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001796 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1797
Jamie Madill4cff2472015-08-21 16:53:18 -04001798 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1799 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001800
Sami Väisänen46eaa942016-06-29 10:26:37 +03001801 std::map<GLuint, std::string> staticFragmentInputLocations;
1802
Jamie Madill4cff2472015-08-21 16:53:18 -04001803 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805 bool matched = false;
1806
1807 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001808 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001809 {
1810 continue;
1811 }
1812
Jamie Madill4cff2472015-08-21 16:53:18 -04001813 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001815 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001817 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001818 if (!linkValidateVaryings(infoLog, output.name, input, output,
1819 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820 {
1821 return false;
1822 }
1823
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824 matched = true;
1825 break;
1826 }
1827 }
1828
1829 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001830 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001832 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 return false;
1834 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001835
1836 // Check for aliased path rendering input bindings (if any).
1837 // If more than one binding refer statically to the same
1838 // location the link must fail.
1839
1840 if (!output.staticUse)
1841 continue;
1842
1843 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1844 if (inputBinding == -1)
1845 continue;
1846
1847 const auto it = staticFragmentInputLocations.find(inputBinding);
1848 if (it == std::end(staticFragmentInputLocations))
1849 {
1850 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1851 }
1852 else
1853 {
1854 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1855 << it->second;
1856 return false;
1857 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001858 }
1859
Jamie Madillada9ecc2015-08-17 12:53:37 -04001860 // TODO(jmadill): verify no unmatched vertex varyings?
1861
Geoff Lang7dd2e102014-11-10 15:19:26 -05001862 return true;
1863}
1864
Martin Radev4c4c8e72016-08-04 12:25:34 +03001865bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001866{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001867 // Check that uniforms defined in the vertex and fragment shaders are identical
1868 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001869 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1870 const std::vector<sh::Uniform> &fragmentUniforms =
1871 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001872
Jamie Madillea918db2015-08-18 14:48:59 -04001873 for (const sh::Uniform &vertexUniform : vertexUniforms)
1874 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001875 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001876 }
1877
1878 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1879 {
1880 auto entry = linkedUniforms.find(fragmentUniform.name);
1881 if (entry != linkedUniforms.end())
1882 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001883 LinkedUniform *vertexUniform = &entry->second;
1884 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1885 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001886 {
1887 return false;
1888 }
1889 }
1890 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001891 return true;
1892}
1893
Jamie Madilla2c74982016-12-12 11:20:42 -05001894bool Program::linkUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001895{
1896 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1897 {
1898 ASSERT(mState.mAttachedComputeShader == nullptr);
1899 if (!validateVertexAndFragmentUniforms(infoLog))
1900 {
1901 return false;
1902 }
1903 }
Jamie Madillea918db2015-08-18 14:48:59 -04001904
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1906 // Also check the maximum uniform vector and sampler counts.
1907 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1908 {
1909 return false;
1910 }
1911
Geoff Langd8605522016-04-13 10:19:12 -04001912 if (!indexUniforms(infoLog, caps, uniformBindings))
1913 {
1914 return false;
1915 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001916
Jamie Madillea918db2015-08-18 14:48:59 -04001917 return true;
1918}
1919
Jamie Madilla2c74982016-12-12 11:20:42 -05001920bool Program::indexUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921{
Geoff Langd8605522016-04-13 10:19:12 -04001922 // Uniforms awaiting a location
1923 std::vector<VariableLocation> unboundUniforms;
1924 std::map<GLuint, VariableLocation> boundUniforms;
1925 int maxUniformLocation = -1;
1926
1927 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001928 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001929 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001930 const LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001931
Geoff Langd8605522016-04-13 10:19:12 -04001932 if (uniform.isBuiltIn())
1933 {
1934 continue;
1935 }
1936
1937 int bindingLocation = uniformBindings.getBinding(uniform.name);
1938
1939 // Verify that this location isn't bound twice
1940 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1941 {
1942 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1943 return false;
1944 }
1945
Jamie Madill62d31cb2015-09-11 13:25:51 -04001946 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1947 {
Geoff Langd8605522016-04-13 10:19:12 -04001948 VariableLocation location(uniform.name, arrayIndex,
1949 static_cast<unsigned int>(uniformIndex));
1950
1951 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001952 {
Geoff Langd8605522016-04-13 10:19:12 -04001953 boundUniforms[bindingLocation] = location;
1954 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1955 }
1956 else
1957 {
1958 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001959 }
1960 }
1961 }
Geoff Langd8605522016-04-13 10:19:12 -04001962
1963 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1964 // not be assigned to those locations.
1965 std::set<GLuint> reservedLocations;
1966 for (const auto &binding : uniformBindings)
1967 {
1968 GLuint location = binding.second;
1969 if (boundUniforms.find(location) == boundUniforms.end())
1970 {
1971 reservedLocations.insert(location);
1972 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1973 }
1974 }
1975
1976 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04001977 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04001978 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1979 static_cast<size_t>(maxUniformLocation + 1)));
1980
1981 // Assign bound uniforms
1982 for (const auto &boundUniform : boundUniforms)
1983 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001984 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04001985 }
1986
1987 // Assign reserved uniforms
1988 for (const auto &reservedLocation : reservedLocations)
1989 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001990 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04001991 }
1992
1993 // Assign unbound uniforms
1994 size_t nextUniformLocation = 0;
1995 for (const auto &unboundUniform : unboundUniforms)
1996 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001997 while (mState.mUniformLocations[nextUniformLocation].used ||
1998 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04001999 {
2000 nextUniformLocation++;
2001 }
2002
Jamie Madill48ef11b2016-04-27 15:21:52 -04002003 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
2004 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002005 nextUniformLocation++;
2006 }
2007
2008 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002009}
2010
Martin Radev4c4c8e72016-08-04 12:25:34 +03002011bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2012 const std::string &uniformName,
2013 const sh::InterfaceBlockField &vertexUniform,
2014 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015{
Jamie Madillc4c744222015-11-04 09:39:47 -05002016 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2017 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018 {
2019 return false;
2020 }
2021
2022 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2023 {
Jamie Madillf6113162015-05-07 11:49:21 -04002024 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002025 return false;
2026 }
2027
2028 return true;
2029}
2030
Jamie Madilleb979bf2016-11-15 12:28:46 -05002031// Assigns locations to all attributes from the bindings and program locations.
2032bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002033{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002034 const auto *vertexShader = mState.getAttachedVertexShader();
2035
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002037 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002038 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002039
2040 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002041 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002042 {
Jamie Madillf6113162015-05-07 11:49:21 -04002043 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002044 return false;
2045 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002047 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002048
Jamie Madillc349ec02015-08-21 16:53:12 -04002049 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002050 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002051 {
2052 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 ASSERT(attribute.staticUse);
2054
Jamie Madilleb979bf2016-11-15 12:28:46 -05002055 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002056 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002057 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002058 attribute.location = bindingLocation;
2059 }
2060
2061 if (attribute.location != -1)
2062 {
2063 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002064 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065
Jamie Madill63805b42015-08-25 13:17:39 -04002066 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 {
Jamie Madillf6113162015-05-07 11:49:21 -04002068 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002069 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070
2071 return false;
2072 }
2073
Jamie Madill63805b42015-08-25 13:17:39 -04002074 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075 {
Jamie Madill63805b42015-08-25 13:17:39 -04002076 const int regLocation = attribute.location + reg;
2077 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078
2079 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002080 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002081 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002083 // TODO(jmadill): fix aliasing on ES2
2084 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002086 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002087 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088 return false;
2089 }
2090 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 else
2092 {
Jamie Madill63805b42015-08-25 13:17:39 -04002093 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002094 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095
Jamie Madill63805b42015-08-25 13:17:39 -04002096 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002097 }
2098 }
2099 }
2100
2101 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002102 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 ASSERT(attribute.staticUse);
2105
Jamie Madillc349ec02015-08-21 16:53:12 -04002106 // Not set by glBindAttribLocation or by location layout qualifier
2107 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108 {
Jamie Madill63805b42015-08-25 13:17:39 -04002109 int regs = VariableRegisterCount(attribute.type);
2110 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002111
Jamie Madill63805b42015-08-25 13:17:39 -04002112 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113 {
Jamie Madillf6113162015-05-07 11:49:21 -04002114 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002115 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116 }
2117
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119 }
2120 }
2121
Jamie Madill48ef11b2016-04-27 15:21:52 -04002122 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002124 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002125 ASSERT(attribute.location != -1);
2126 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002127
Jamie Madill63805b42015-08-25 13:17:39 -04002128 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002130 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 }
2132 }
2133
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 return true;
2135}
2136
Martin Radev4c4c8e72016-08-04 12:25:34 +03002137bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2138 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2139 const std::string &errorMessage,
2140 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002141{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002142 GLuint blockCount = 0;
2143 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002145 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002146 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002147 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002148 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002149 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002150 return false;
2151 }
2152 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002153 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002154 return true;
2155}
Jamie Madille473dee2015-08-18 14:49:01 -04002156
Martin Radev4c4c8e72016-08-04 12:25:34 +03002157bool Program::validateVertexAndFragmentInterfaceBlocks(
2158 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2159 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2160 InfoLog &infoLog) const
2161{
2162 // Check that interface blocks defined in the vertex and fragment shaders are identical
2163 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2164 UniformBlockMap linkedUniformBlocks;
2165
2166 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2167 {
2168 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2169 }
2170
Jamie Madille473dee2015-08-18 14:49:01 -04002171 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
Jamie Madille473dee2015-08-18 14:49:01 -04002173 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174 if (entry != linkedUniformBlocks.end())
2175 {
2176 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2177 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2178 {
2179 return false;
2180 }
2181 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002182 }
2183 return true;
2184}
Jamie Madille473dee2015-08-18 14:49:01 -04002185
Martin Radev4c4c8e72016-08-04 12:25:34 +03002186bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2187{
2188 if (mState.mAttachedComputeShader)
2189 {
2190 const Shader &computeShader = *mState.mAttachedComputeShader;
2191 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2192
2193 if (!validateUniformBlocksCount(
2194 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2195 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2196 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002197 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002198 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002199 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002200 return true;
2201 }
2202
2203 const Shader &vertexShader = *mState.mAttachedVertexShader;
2204 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2205
2206 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2207 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2208
2209 if (!validateUniformBlocksCount(
2210 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2211 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2212 {
2213 return false;
2214 }
2215 if (!validateUniformBlocksCount(
2216 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2217 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2218 infoLog))
2219 {
2220
2221 return false;
2222 }
2223 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2224 infoLog))
2225 {
2226 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002227 }
Jamie Madille473dee2015-08-18 14:49:01 -04002228
Geoff Lang7dd2e102014-11-10 15:19:26 -05002229 return true;
2230}
2231
Jamie Madilla2c74982016-12-12 11:20:42 -05002232bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002233 const sh::InterfaceBlock &vertexInterfaceBlock,
2234 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235{
2236 const char* blockName = vertexInterfaceBlock.name.c_str();
2237 // validate blocks for the same member types
2238 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2239 {
Jamie Madillf6113162015-05-07 11:49:21 -04002240 infoLog << "Types for interface block '" << blockName
2241 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002242 return false;
2243 }
2244 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2245 {
Jamie Madillf6113162015-05-07 11:49:21 -04002246 infoLog << "Array sizes differ for interface block '" << blockName
2247 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248 return false;
2249 }
2250 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2251 {
Jamie Madillf6113162015-05-07 11:49:21 -04002252 infoLog << "Layout qualifiers differ for interface block '" << blockName
2253 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002254 return false;
2255 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002256 const unsigned int numBlockMembers =
2257 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002258 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2259 {
2260 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2261 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2262 if (vertexMember.name != fragmentMember.name)
2263 {
Jamie Madillf6113162015-05-07 11:49:21 -04002264 infoLog << "Name mismatch for field " << blockMemberIndex
2265 << " of interface block '" << blockName
2266 << "': (in vertex: '" << vertexMember.name
2267 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002268 return false;
2269 }
2270 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2271 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2272 {
2273 return false;
2274 }
2275 }
2276 return true;
2277}
2278
2279bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2280 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2281{
2282 if (vertexVariable.type != fragmentVariable.type)
2283 {
Jamie Madillf6113162015-05-07 11:49:21 -04002284 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002285 return false;
2286 }
2287 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2288 {
Jamie Madillf6113162015-05-07 11:49:21 -04002289 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002290 return false;
2291 }
2292 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2293 {
Jamie Madillf6113162015-05-07 11:49:21 -04002294 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002295 return false;
2296 }
2297
2298 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2299 {
Jamie Madillf6113162015-05-07 11:49:21 -04002300 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002301 return false;
2302 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002303 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002304 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2305 {
2306 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2307 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2308
2309 if (vertexMember.name != fragmentMember.name)
2310 {
Jamie Madillf6113162015-05-07 11:49:21 -04002311 infoLog << "Name mismatch for field '" << memberIndex
2312 << "' of " << variableName
2313 << ": (in vertex: '" << vertexMember.name
2314 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315 return false;
2316 }
2317
2318 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2319 vertexMember.name + "'";
2320
2321 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2322 {
2323 return false;
2324 }
2325 }
2326
2327 return true;
2328}
2329
2330bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2331{
Cooper Partin1acf4382015-06-12 12:38:57 -07002332#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2333 const bool validatePrecision = true;
2334#else
2335 const bool validatePrecision = false;
2336#endif
2337
2338 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002339 {
2340 return false;
2341 }
2342
2343 return true;
2344}
2345
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002346bool Program::linkValidateVaryings(InfoLog &infoLog,
2347 const std::string &varyingName,
2348 const sh::Varying &vertexVarying,
2349 const sh::Varying &fragmentVarying,
2350 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002351{
2352 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2353 {
2354 return false;
2355 }
2356
Jamie Madille9cc4692015-02-19 16:00:13 -05002357 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002359 infoLog << "Interpolation types for " << varyingName
2360 << " differ between vertex and fragment shaders.";
2361 return false;
2362 }
2363
2364 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2365 {
2366 infoLog << "Invariance for " << varyingName
2367 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368 return false;
2369 }
2370
2371 return true;
2372}
2373
Jamie Madillccdf74b2015-08-18 10:46:12 -04002374bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2375 const std::vector<const sh::Varying *> &varyings,
2376 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002377{
2378 size_t totalComponents = 0;
2379
Jamie Madillccdf74b2015-08-18 10:46:12 -04002380 std::set<std::string> uniqueNames;
2381
Jamie Madill48ef11b2016-04-27 15:21:52 -04002382 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002383 {
2384 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002385 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002387 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002388 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002389 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002391 infoLog << "Two transform feedback varyings specify the same output variable ("
2392 << tfVaryingName << ").";
2393 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002394 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002395 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396
Geoff Lang1a683462015-09-29 15:09:59 -04002397 if (varying->isArray())
2398 {
2399 infoLog << "Capture of arrays is undefined and not supported.";
2400 return false;
2401 }
2402
Jamie Madillccdf74b2015-08-18 10:46:12 -04002403 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002404 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002405 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002406 componentCount > caps.maxTransformFeedbackSeparateComponents)
2407 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002408 infoLog << "Transform feedback varying's " << varying->name << " components ("
2409 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002410 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 return false;
2412 }
2413
2414 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415 found = true;
2416 break;
2417 }
2418 }
2419
Jamie Madill89bb70e2015-08-31 14:18:39 -04002420 if (tfVaryingName.find('[') != std::string::npos)
2421 {
Geoff Lang1a683462015-09-29 15:09:59 -04002422 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002423 return false;
2424 }
2425
Geoff Lang7dd2e102014-11-10 15:19:26 -05002426 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2427 ASSERT(found);
2428 }
2429
Jamie Madill48ef11b2016-04-27 15:21:52 -04002430 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002431 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002432 {
Jamie Madillf6113162015-05-07 11:49:21 -04002433 infoLog << "Transform feedback varying total components (" << totalComponents
2434 << ") exceed the maximum interleaved components ("
2435 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002436 return false;
2437 }
2438
2439 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002440}
2441
Jamie Madillccdf74b2015-08-18 10:46:12 -04002442void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2443{
2444 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002445 mState.mTransformFeedbackVaryingVars.clear();
2446 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002447 {
2448 for (const sh::Varying *varying : varyings)
2449 {
2450 if (tfVaryingName == varying->name)
2451 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002452 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453 break;
2454 }
2455 }
2456 }
2457}
2458
2459std::vector<const sh::Varying *> Program::getMergedVaryings() const
2460{
2461 std::set<std::string> uniqueNames;
2462 std::vector<const sh::Varying *> varyings;
2463
Jamie Madill48ef11b2016-04-27 15:21:52 -04002464 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002465 {
2466 if (uniqueNames.count(varying.name) == 0)
2467 {
2468 uniqueNames.insert(varying.name);
2469 varyings.push_back(&varying);
2470 }
2471 }
2472
Jamie Madill48ef11b2016-04-27 15:21:52 -04002473 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002474 {
2475 if (uniqueNames.count(varying.name) == 0)
2476 {
2477 uniqueNames.insert(varying.name);
2478 varyings.push_back(&varying);
2479 }
2480 }
2481
2482 return varyings;
2483}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002484
2485void Program::linkOutputVariables()
2486{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002487 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002488 ASSERT(fragmentShader != nullptr);
2489
2490 // Skip this step for GLES2 shaders.
2491 if (fragmentShader->getShaderVersion() == 100)
2492 return;
2493
Jamie Madilla0a9e122015-09-02 15:54:30 -04002494 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002495
2496 // TODO(jmadill): any caps validation here?
2497
2498 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2499 outputVariableIndex++)
2500 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002501 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002502
2503 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2504 if (outputVariable.isBuiltIn())
2505 continue;
2506
2507 // Since multiple output locations must be specified, use 0 for non-specified locations.
2508 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2509
2510 ASSERT(outputVariable.staticUse);
2511
2512 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2513 elementIndex++)
2514 {
2515 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002516 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002517 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002518 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002519 VariableLocation(outputVariable.name, element, outputVariableIndex);
2520 }
2521 }
2522}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002523
Jamie Madilla2c74982016-12-12 11:20:42 -05002524bool Program::flattenUniformsAndCheckCapsForShader(const Shader &shader,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002525 GLuint maxUniformComponents,
2526 GLuint maxTextureImageUnits,
2527 const std::string &componentsErrorMessage,
2528 const std::string &samplerErrorMessage,
2529 std::vector<LinkedUniform> &samplerUniforms,
2530 InfoLog &infoLog)
2531{
2532 VectorAndSamplerCount vasCount;
2533 for (const sh::Uniform &uniform : shader.getUniforms())
2534 {
2535 if (uniform.staticUse)
2536 {
2537 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2538 }
2539 }
2540
2541 if (vasCount.vectorCount > maxUniformComponents)
2542 {
2543 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2544 return false;
2545 }
2546
2547 if (vasCount.samplerCount > maxTextureImageUnits)
2548 {
2549 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2550 return false;
2551 }
2552
2553 return true;
2554}
2555
Jamie Madill62d31cb2015-09-11 13:25:51 -04002556bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2557{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002558 std::vector<LinkedUniform> samplerUniforms;
2559
Martin Radev4c4c8e72016-08-04 12:25:34 +03002560 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002561 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002562 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002563
2564 // TODO (mradev): check whether we need finer-grained component counting
2565 if (!flattenUniformsAndCheckCapsForShader(
2566 *computeShader, caps.maxComputeUniformComponents / 4,
2567 caps.maxComputeTextureImageUnits,
2568 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2569 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2570 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002571 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002572 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002573 }
2574 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002575 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002576 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002577 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002578
Martin Radev4c4c8e72016-08-04 12:25:34 +03002579 if (!flattenUniformsAndCheckCapsForShader(
2580 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2581 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2582 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2583 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002584 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002585 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002586 }
Jamie Madilla2c74982016-12-12 11:20:42 -05002587 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002588
Martin Radev4c4c8e72016-08-04 12:25:34 +03002589 if (!flattenUniformsAndCheckCapsForShader(
2590 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2591 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2592 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2593 infoLog))
2594 {
2595 return false;
2596 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002597 }
2598
Jamie Madill48ef11b2016-04-27 15:21:52 -04002599 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002600 mSamplerUniformRange.end =
2601 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2602
Jamie Madill48ef11b2016-04-27 15:21:52 -04002603 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002604
Jamie Madill62d31cb2015-09-11 13:25:51 -04002605 return true;
2606}
2607
2608Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002609 const std::string &fullName,
2610 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002611{
2612 VectorAndSamplerCount vectorAndSamplerCount;
2613
2614 if (uniform.isStruct())
2615 {
2616 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2617 {
2618 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2619
2620 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2621 {
2622 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2623 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2624
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002625 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002626 }
2627 }
2628
2629 return vectorAndSamplerCount;
2630 }
2631
2632 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002633 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002634 if (!UniformInList(mState.getUniforms(), fullName) &&
2635 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002636 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002637 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
2638 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002639 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002640
2641 // Store sampler uniforms separately, so we'll append them to the end of the list.
2642 if (isSampler)
2643 {
2644 samplerUniforms->push_back(linkedUniform);
2645 }
2646 else
2647 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002648 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002649 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002650 }
2651
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002652 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002653
2654 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2655 // Likewise, don't count "real" uniforms towards sampler count.
2656 vectorAndSamplerCount.vectorCount =
2657 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002658 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002659
2660 return vectorAndSamplerCount;
2661}
2662
2663void Program::gatherInterfaceBlockInfo()
2664{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002665 ASSERT(mState.mUniformBlocks.empty());
2666
2667 if (mState.mAttachedComputeShader)
2668 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002669 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002670
2671 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2672 {
2673
2674 // Only 'packed' blocks are allowed to be considered inactive.
2675 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2676 continue;
2677
Jamie Madilla2c74982016-12-12 11:20:42 -05002678 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002679 {
2680 if (block.name == computeBlock.name)
2681 {
2682 block.computeStaticUse = computeBlock.staticUse;
2683 }
2684 }
2685
2686 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2687 }
2688 return;
2689 }
2690
Jamie Madill62d31cb2015-09-11 13:25:51 -04002691 std::set<std::string> visitedList;
2692
Jamie Madilla2c74982016-12-12 11:20:42 -05002693 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002694
Jamie Madill62d31cb2015-09-11 13:25:51 -04002695 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2696 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002697 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002698 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2699 continue;
2700
2701 if (visitedList.count(vertexBlock.name) > 0)
2702 continue;
2703
2704 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2705 visitedList.insert(vertexBlock.name);
2706 }
2707
Jamie Madilla2c74982016-12-12 11:20:42 -05002708 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002709
2710 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2711 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002712 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002713 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2714 continue;
2715
2716 if (visitedList.count(fragmentBlock.name) > 0)
2717 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002718 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002719 {
2720 if (block.name == fragmentBlock.name)
2721 {
2722 block.fragmentStaticUse = fragmentBlock.staticUse;
2723 }
2724 }
2725
2726 continue;
2727 }
2728
2729 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2730 visitedList.insert(fragmentBlock.name);
2731 }
2732}
2733
Jamie Madill4a3c2342015-10-08 12:58:45 -04002734template <typename VarT>
2735void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2736 const std::string &prefix,
2737 int blockIndex)
2738{
2739 for (const VarT &field : fields)
2740 {
2741 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2742
2743 if (field.isStruct())
2744 {
2745 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2746 {
2747 const std::string uniformElementName =
2748 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2749 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2750 }
2751 }
2752 else
2753 {
2754 // If getBlockMemberInfo returns false, the uniform is optimized out.
2755 sh::BlockMemberInfo memberInfo;
2756 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2757 {
2758 continue;
2759 }
2760
2761 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2762 blockIndex, memberInfo);
2763
2764 // Since block uniforms have no location, we don't need to store them in the uniform
2765 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002766 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002767 }
2768 }
2769}
2770
Jamie Madill62d31cb2015-09-11 13:25:51 -04002771void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2772{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002773 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002774 size_t blockSize = 0;
2775
2776 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002777 std::stringstream blockNameStr;
2778 blockNameStr << interfaceBlock.name;
2779 if (interfaceBlock.arraySize > 0)
2780 {
2781 blockNameStr << "[0]";
2782 }
2783 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002784 {
2785 return;
2786 }
2787
2788 // Track the first and last uniform index to determine the range of active uniforms in the
2789 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002790 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002791 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002792 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002793
2794 std::vector<unsigned int> blockUniformIndexes;
2795 for (size_t blockUniformIndex = firstBlockUniformIndex;
2796 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2797 {
2798 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2799 }
2800
2801 if (interfaceBlock.arraySize > 0)
2802 {
2803 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2804 {
2805 UniformBlock block(interfaceBlock.name, true, arrayElement);
2806 block.memberUniformIndexes = blockUniformIndexes;
2807
Martin Radev4c4c8e72016-08-04 12:25:34 +03002808 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002809 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002810 case GL_VERTEX_SHADER:
2811 {
2812 block.vertexStaticUse = interfaceBlock.staticUse;
2813 break;
2814 }
2815 case GL_FRAGMENT_SHADER:
2816 {
2817 block.fragmentStaticUse = interfaceBlock.staticUse;
2818 break;
2819 }
2820 case GL_COMPUTE_SHADER:
2821 {
2822 block.computeStaticUse = interfaceBlock.staticUse;
2823 break;
2824 }
2825 default:
2826 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002827 }
2828
Qin Jiajia0350a642016-11-01 17:01:51 +08002829 // Since all block elements in an array share the same active uniforms, they will all be
2830 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2831 // here we will add every block element in the array.
2832 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002833 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002834 }
2835 }
2836 else
2837 {
2838 UniformBlock block(interfaceBlock.name, false, 0);
2839 block.memberUniformIndexes = blockUniformIndexes;
2840
Martin Radev4c4c8e72016-08-04 12:25:34 +03002841 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002843 case GL_VERTEX_SHADER:
2844 {
2845 block.vertexStaticUse = interfaceBlock.staticUse;
2846 break;
2847 }
2848 case GL_FRAGMENT_SHADER:
2849 {
2850 block.fragmentStaticUse = interfaceBlock.staticUse;
2851 break;
2852 }
2853 case GL_COMPUTE_SHADER:
2854 {
2855 block.computeStaticUse = interfaceBlock.staticUse;
2856 break;
2857 }
2858 default:
2859 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002860 }
2861
Jamie Madill4a3c2342015-10-08 12:58:45 -04002862 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002863 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002864 }
2865}
2866
2867template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002868GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002869{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002870 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2871 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002872 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2873
Corentin Wallez15ac5342016-11-03 17:06:39 -04002874 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2875 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2876 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002877 GLsizei maxElementCount =
2878 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2879
2880 GLsizei count = countIn;
2881 GLsizei clampedCount = count * vectorSize;
2882 if (clampedCount > maxElementCount)
2883 {
2884 clampedCount = maxElementCount;
2885 count = maxElementCount / vectorSize;
2886 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002887
Jamie Madill62d31cb2015-09-11 13:25:51 -04002888 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2889 {
2890 // Do a cast conversion for boolean types. From the spec:
2891 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2892 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002893 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002894 {
2895 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2896 }
2897 }
2898 else
2899 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002900 // Invalide the validation cache if we modify the sampler data.
Corentin Wallez15ac5342016-11-03 17:06:39 -04002901 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * clampedCount) != 0)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002902 {
2903 mCachedValidateSamplersResult.reset();
2904 }
2905
Corentin Wallez15ac5342016-11-03 17:06:39 -04002906 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002907 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002908
2909 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002910}
2911
2912template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002913GLsizei Program::setMatrixUniformInternal(GLint location,
2914 GLsizei count,
2915 GLboolean transpose,
2916 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002917{
2918 if (!transpose)
2919 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002920 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002921 }
2922
2923 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002924 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2925 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002926 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002927
2928 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2929 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2930 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2931 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2932
2933 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002934 {
2935 size_t elementOffset = element * rows * cols;
2936
2937 for (size_t row = 0; row < rows; ++row)
2938 {
2939 for (size_t col = 0; col < cols; ++col)
2940 {
2941 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2942 }
2943 }
2944 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002945
2946 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002947}
2948
2949template <typename DestT>
2950void Program::getUniformInternal(GLint location, DestT *dataOut) const
2951{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002952 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2953 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002954
2955 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2956
2957 GLenum componentType = VariableComponentType(uniform.type);
2958 if (componentType == GLTypeToGLenum<DestT>::value)
2959 {
2960 memcpy(dataOut, srcPointer, uniform.getElementSize());
2961 return;
2962 }
2963
Corentin Wallez6596c462016-03-17 17:26:58 -04002964 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002965
2966 switch (componentType)
2967 {
2968 case GL_INT:
2969 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2970 break;
2971 case GL_UNSIGNED_INT:
2972 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2973 break;
2974 case GL_BOOL:
2975 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2976 break;
2977 case GL_FLOAT:
2978 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2979 break;
2980 default:
2981 UNREACHABLE();
2982 }
2983}
Jamie Madilla2c74982016-12-12 11:20:42 -05002984} // namespace gl