blob: 424ce2371681328bc7d186589c373f0c6129349c [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Jamie Madill9082b982016-04-27 15:21:51 -040020#include "libANGLE/ContextState.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040025#include "libANGLE/queryconversions.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040026#include "libANGLE/Uniform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028namespace gl
29{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000030
Geoff Lang7dd2e102014-11-10 15:19:26 -050031namespace
32{
33
Jamie Madill62d31cb2015-09-11 13:25:51 -040034void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
35{
36 stream->writeInt(var.type);
37 stream->writeInt(var.precision);
38 stream->writeString(var.name);
39 stream->writeString(var.mappedName);
40 stream->writeInt(var.arraySize);
41 stream->writeInt(var.staticUse);
42 stream->writeString(var.structName);
43 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050044}
45
Jamie Madill62d31cb2015-09-11 13:25:51 -040046void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
47{
48 var->type = stream->readInt<GLenum>();
49 var->precision = stream->readInt<GLenum>();
50 var->name = stream->readString();
51 var->mappedName = stream->readString();
52 var->arraySize = stream->readInt<unsigned int>();
53 var->staticUse = stream->readBool();
54 var->structName = stream->readString();
55}
56
Jamie Madill62d31cb2015-09-11 13:25:51 -040057// This simplified cast function doesn't need to worry about advanced concepts like
58// depth range values, or casting to bool.
59template <typename DestT, typename SrcT>
60DestT UniformStateQueryCast(SrcT value);
61
62// From-Float-To-Integer Casts
63template <>
64GLint UniformStateQueryCast(GLfloat value)
65{
66 return clampCast<GLint>(roundf(value));
67}
68
69template <>
70GLuint UniformStateQueryCast(GLfloat value)
71{
72 return clampCast<GLuint>(roundf(value));
73}
74
75// From-Integer-to-Integer Casts
76template <>
77GLint UniformStateQueryCast(GLuint value)
78{
79 return clampCast<GLint>(value);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLint value)
84{
85 return clampCast<GLuint>(value);
86}
87
88// From-Boolean-to-Anything Casts
89template <>
90GLfloat UniformStateQueryCast(GLboolean value)
91{
92 return (value == GL_TRUE ? 1.0f : 0.0f);
93}
94
95template <>
96GLint UniformStateQueryCast(GLboolean value)
97{
98 return (value == GL_TRUE ? 1 : 0);
99}
100
101template <>
102GLuint UniformStateQueryCast(GLboolean value)
103{
104 return (value == GL_TRUE ? 1u : 0u);
105}
106
107// Default to static_cast
108template <typename DestT, typename SrcT>
109DestT UniformStateQueryCast(SrcT value)
110{
111 return static_cast<DestT>(value);
112}
113
114template <typename SrcT, typename DestT>
115void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
116{
117 for (int comp = 0; comp < components; ++comp)
118 {
119 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
120 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
121 size_t offset = comp * 4;
122 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
123 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
124 }
125}
126
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400127bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
128{
129 for (const LinkedUniform &uniform : list)
130 {
131 if (uniform.name == name)
132 return true;
133 }
134
135 return false;
136}
137
Jamie Madill62d31cb2015-09-11 13:25:51 -0400138} // anonymous namespace
139
Jamie Madill4a3c2342015-10-08 12:58:45 -0400140const char *const g_fakepath = "C:\\fakepath";
141
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400142InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000143{
144}
145
146InfoLog::~InfoLog()
147{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000148}
149
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400150size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000151{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400152 const std::string &logString = mStream.str();
153 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000154}
155
Geoff Lange1a27752015-10-05 13:16:04 -0400156void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000157{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400158 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000159
160 if (bufSize > 0)
161 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400162 const std::string str(mStream.str());
163
164 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000165 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400166 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
167 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000168 }
169
170 infoLog[index] = '\0';
171 }
172
173 if (length)
174 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400175 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000176 }
177}
178
179// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300180// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000181// messages, so lets remove all occurrences of this fake file path from the log.
182void InfoLog::appendSanitized(const char *message)
183{
184 std::string msg(message);
185
186 size_t found;
187 do
188 {
189 found = msg.find(g_fakepath);
190 if (found != std::string::npos)
191 {
192 msg.erase(found, strlen(g_fakepath));
193 }
194 }
195 while (found != std::string::npos);
196
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400197 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000198}
199
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000200void InfoLog::reset()
201{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000202}
203
Geoff Langd8605522016-04-13 10:19:12 -0400204VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000205{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500206}
207
Geoff Langd8605522016-04-13 10:19:12 -0400208VariableLocation::VariableLocation(const std::string &name,
209 unsigned int element,
210 unsigned int index)
211 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500212{
213}
214
Geoff Langd8605522016-04-13 10:19:12 -0400215void Program::Bindings::bindLocation(GLuint index, const std::string &name)
216{
217 mBindings[name] = index;
218}
219
220int Program::Bindings::getBinding(const std::string &name) const
221{
222 auto iter = mBindings.find(name);
223 return (iter != mBindings.end()) ? iter->second : -1;
224}
225
226Program::Bindings::const_iterator Program::Bindings::begin() const
227{
228 return mBindings.begin();
229}
230
231Program::Bindings::const_iterator Program::Bindings::end() const
232{
233 return mBindings.end();
234}
235
Jamie Madill48ef11b2016-04-27 15:21:52 -0400236ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500237 : mLabel(),
238 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400239 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300240 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500241 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
242 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400243{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300244 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400245}
246
Jamie Madill48ef11b2016-04-27 15:21:52 -0400247ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400248{
249 if (mAttachedVertexShader != nullptr)
250 {
251 mAttachedVertexShader->release();
252 }
253
254 if (mAttachedFragmentShader != nullptr)
255 {
256 mAttachedFragmentShader->release();
257 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300258
259 if (mAttachedComputeShader != nullptr)
260 {
261 mAttachedComputeShader->release();
262 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400263}
264
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500266{
267 return mLabel;
268}
269
Jamie Madill48ef11b2016-04-27 15:21:52 -0400270const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400271{
272 for (const LinkedUniform &linkedUniform : mUniforms)
273 {
274 if (linkedUniform.name == name)
275 {
276 return &linkedUniform;
277 }
278 }
279
280 return nullptr;
281}
282
Jamie Madill48ef11b2016-04-27 15:21:52 -0400283GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400284{
285 size_t subscript = GL_INVALID_INDEX;
286 std::string baseName = gl::ParseUniformName(name, &subscript);
287
288 for (size_t location = 0; location < mUniformLocations.size(); ++location)
289 {
290 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400291 if (!uniformLocation.used)
292 {
293 continue;
294 }
295
296 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400297
298 if (uniform.name == baseName)
299 {
Geoff Langd8605522016-04-13 10:19:12 -0400300 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400301 {
Geoff Langd8605522016-04-13 10:19:12 -0400302 if (uniformLocation.element == subscript ||
303 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
304 {
305 return static_cast<GLint>(location);
306 }
307 }
308 else
309 {
310 if (subscript == GL_INVALID_INDEX)
311 {
312 return static_cast<GLint>(location);
313 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400314 }
315 }
316 }
317
318 return -1;
319}
320
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321GLuint ProgramState::getUniformIndex(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400322{
323 size_t subscript = GL_INVALID_INDEX;
324 std::string baseName = gl::ParseUniformName(name, &subscript);
325
326 // The app is not allowed to specify array indices other than 0 for arrays of basic types
327 if (subscript != 0 && subscript != GL_INVALID_INDEX)
328 {
329 return GL_INVALID_INDEX;
330 }
331
332 for (size_t index = 0; index < mUniforms.size(); index++)
333 {
334 const LinkedUniform &uniform = mUniforms[index];
335 if (uniform.name == baseName)
336 {
337 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
338 {
339 return static_cast<GLuint>(index);
340 }
341 }
342 }
343
344 return GL_INVALID_INDEX;
345}
346
Jamie Madill7aea7e02016-05-10 10:39:45 -0400347Program::Program(rx::GLImplFactory *factory, ResourceManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400348 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500350 mLinked(false),
351 mDeleteStatus(false),
352 mRefCount(0),
353 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400354 mHandle(handle),
355 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500356{
357 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000358
359 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500360 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361}
362
363Program::~Program()
364{
365 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000366
Geoff Lang7dd2e102014-11-10 15:19:26 -0500367 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
Geoff Lang70d0f492015-12-10 17:45:46 -0500370void Program::setLabel(const std::string &label)
371{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400372 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500373}
374
375const std::string &Program::getLabel() const
376{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400377 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500378}
379
Jamie Madillef300b12016-10-07 15:12:09 -0400380void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300382 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300384 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Jamie Madillef300b12016-10-07 15:12:09 -0400386 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300387 mState.mAttachedVertexShader = shader;
388 mState.mAttachedVertexShader->addRef();
389 break;
390 }
391 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392 {
Jamie Madillef300b12016-10-07 15:12:09 -0400393 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300394 mState.mAttachedFragmentShader = shader;
395 mState.mAttachedFragmentShader->addRef();
396 break;
397 }
398 case GL_COMPUTE_SHADER:
399 {
Jamie Madillef300b12016-10-07 15:12:09 -0400400 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300401 mState.mAttachedComputeShader = shader;
402 mState.mAttachedComputeShader->addRef();
403 break;
404 }
405 default:
406 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408}
409
410bool Program::detachShader(Shader *shader)
411{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300412 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300414 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300416 if (mState.mAttachedVertexShader != shader)
417 {
418 return false;
419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
Martin Radev4c4c8e72016-08-04 12:25:34 +0300421 shader->release();
422 mState.mAttachedVertexShader = nullptr;
423 break;
424 }
425 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 if (mState.mAttachedFragmentShader != shader)
428 {
429 return false;
430 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 shader->release();
433 mState.mAttachedFragmentShader = nullptr;
434 break;
435 }
436 case GL_COMPUTE_SHADER:
437 {
438 if (mState.mAttachedComputeShader != shader)
439 {
440 return false;
441 }
442
443 shader->release();
444 mState.mAttachedComputeShader = nullptr;
445 break;
446 }
447 default:
448 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451 return true;
452}
453
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000454int Program::getAttachedShadersCount() const
455{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300456 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
457 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000458}
459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460void Program::bindAttributeLocation(GLuint index, const char *name)
461{
Geoff Langd8605522016-04-13 10:19:12 -0400462 mAttributeBindings.bindLocation(index, name);
463}
464
465void Program::bindUniformLocation(GLuint index, const char *name)
466{
467 // Bind the base uniform name only since array indices other than 0 cannot be bound
468 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469}
470
Sami Väisänen46eaa942016-06-29 10:26:37 +0300471void Program::bindFragmentInputLocation(GLint index, const char *name)
472{
473 mFragmentInputBindings.bindLocation(index, name);
474}
475
476BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
477{
478 BindingInfo ret;
479 ret.type = GL_NONE;
480 ret.valid = false;
481
482 const Shader *fragmentShader = mState.getAttachedFragmentShader();
483 ASSERT(fragmentShader);
484
485 // Find the actual fragment shader varying we're interested in
486 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
487
488 for (const auto &binding : mFragmentInputBindings)
489 {
490 if (binding.second != static_cast<GLuint>(index))
491 continue;
492
493 ret.valid = true;
494
495 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400496 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300497
498 for (const auto &in : inputs)
499 {
500 if (in.name == originalName)
501 {
502 if (in.isArray())
503 {
504 // The client wants to bind either "name" or "name[0]".
505 // GL ES 3.1 spec refers to active array names with language such as:
506 // "if the string identifies the base name of an active array, where the
507 // string would exactly match the name of the variable if the suffix "[0]"
508 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400509 if (arrayIndex == GL_INVALID_INDEX)
510 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300511
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400512 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300513 }
514 else
515 {
516 ret.name = in.mappedName;
517 }
518 ret.type = in.type;
519 return ret;
520 }
521 }
522 }
523
524 return ret;
525}
526
527void Program::pathFragmentInputGen(GLint index,
528 GLenum genMode,
529 GLint components,
530 const GLfloat *coeffs)
531{
532 // If the location is -1 then the command is silently ignored
533 if (index == -1)
534 return;
535
536 const auto &binding = getFragmentInputBindingInfo(index);
537
538 // If the input doesn't exist then then the command is silently ignored
539 // This could happen through optimization for example, the shader translator
540 // decides that a variable is not actually being used and optimizes it away.
541 if (binding.name.empty())
542 return;
543
544 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
545}
546
Martin Radev4c4c8e72016-08-04 12:25:34 +0300547// The attached shaders are checked for linking errors by matching up their variables.
548// Uniform, input and output variables get collected.
549// The code gets compiled into binaries.
Jamie Madill9082b982016-04-27 15:21:51 -0400550Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000551{
552 unlink(false);
553
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000554 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000555 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000556
Martin Radev4c4c8e72016-08-04 12:25:34 +0300557 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558
Martin Radev4c4c8e72016-08-04 12:25:34 +0300559 bool isComputeShaderAttached = (mState.mAttachedComputeShader != nullptr);
560 bool nonComputeShadersAttached =
561 (mState.mAttachedVertexShader != nullptr || mState.mAttachedFragmentShader != nullptr);
562 // Check whether we both have a compute and non-compute shaders attached.
563 // If there are of both types attached, then linking should fail.
564 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
565 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500566 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300567 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
568 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400569 }
570
Martin Radev4c4c8e72016-08-04 12:25:34 +0300571 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500572 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300573 if (!mState.mAttachedComputeShader->isCompiled())
574 {
575 mInfoLog << "Attached compute shader is not compiled.";
576 return NoError();
577 }
578 ASSERT(mState.mAttachedComputeShader->getType() == GL_COMPUTE_SHADER);
579
580 mState.mComputeShaderLocalSize = mState.mAttachedComputeShader->getWorkGroupSize();
581
582 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
583 // If the work group size is not specified, a link time error should occur.
584 if (!mState.mComputeShaderLocalSize.isDeclared())
585 {
586 mInfoLog << "Work group size is not specified.";
587 return NoError();
588 }
589
590 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
591 {
592 return NoError();
593 }
594
595 if (!linkUniformBlocks(mInfoLog, caps))
596 {
597 return NoError();
598 }
599
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
627 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mState.mAttachedVertexShader))
628 {
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
Geoff Lang7dd2e102014-11-10 15:19:26 -0500713Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000714{
715 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000716
Geoff Lang7dd2e102014-11-10 15:19:26 -0500717#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
718 return Error(GL_NO_ERROR);
719#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400720 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
721 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000722 {
Jamie Madillf6113162015-05-07 11:49:21 -0400723 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724 return Error(GL_NO_ERROR);
725 }
726
Geoff Langc46cc2f2015-10-01 17:16:20 -0400727 BinaryInputStream stream(binary, length);
728
Geoff Lang7dd2e102014-11-10 15:19:26 -0500729 int majorVersion = stream.readInt<int>();
730 int minorVersion = stream.readInt<int>();
731 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
732 {
Jamie Madillf6113162015-05-07 11:49:21 -0400733 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500734 return Error(GL_NO_ERROR);
735 }
736
737 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
738 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
739 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
740 {
Jamie Madillf6113162015-05-07 11:49:21 -0400741 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500742 return Error(GL_NO_ERROR);
743 }
744
Martin Radev4c4c8e72016-08-04 12:25:34 +0300745 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
746 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
747 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
748
Jamie Madill63805b42015-08-25 13:17:39 -0400749 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
750 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400751 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500752
Jamie Madill3da79b72015-04-27 11:09:17 -0400753 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400754 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400755 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
756 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400757 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400758 LoadShaderVar(&stream, &attrib);
759 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400760 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400761 }
762
Jamie Madill62d31cb2015-09-11 13:25:51 -0400763 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400764 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
766 {
767 LinkedUniform uniform;
768 LoadShaderVar(&stream, &uniform);
769
770 uniform.blockIndex = stream.readInt<int>();
771 uniform.blockInfo.offset = stream.readInt<int>();
772 uniform.blockInfo.arrayStride = stream.readInt<int>();
773 uniform.blockInfo.matrixStride = stream.readInt<int>();
774 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
775
Jamie Madill48ef11b2016-04-27 15:21:52 -0400776 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400777 }
778
779 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400781 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
782 uniformIndexIndex++)
783 {
784 VariableLocation variable;
785 stream.readString(&variable.name);
786 stream.readInt(&variable.element);
787 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400788 stream.readBool(&variable.used);
789 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400790
Jamie Madill48ef11b2016-04-27 15:21:52 -0400791 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 }
793
794 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400795 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400796 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
797 ++uniformBlockIndex)
798 {
799 UniformBlock uniformBlock;
800 stream.readString(&uniformBlock.name);
801 stream.readBool(&uniformBlock.isArray);
802 stream.readInt(&uniformBlock.arrayElement);
803 stream.readInt(&uniformBlock.dataSize);
804 stream.readBool(&uniformBlock.vertexStaticUse);
805 stream.readBool(&uniformBlock.fragmentStaticUse);
806
807 unsigned int numMembers = stream.readInt<unsigned int>();
808 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
809 {
810 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
811 }
812
Jamie Madill48ef11b2016-04-27 15:21:52 -0400813 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814 }
815
Brandon Jones1048ea72015-10-06 15:34:52 -0700816 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400817 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700818 for (unsigned int transformFeedbackVaryingIndex = 0;
819 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
820 ++transformFeedbackVaryingIndex)
821 {
822 sh::Varying varying;
823 stream.readInt(&varying.arraySize);
824 stream.readInt(&varying.type);
825 stream.readString(&varying.name);
826
Jamie Madill48ef11b2016-04-27 15:21:52 -0400827 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700828 }
829
Jamie Madill48ef11b2016-04-27 15:21:52 -0400830 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400831
Jamie Madill80a6fc02015-08-21 16:53:16 -0400832 unsigned int outputVarCount = stream.readInt<unsigned int>();
833 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
834 {
835 int locationIndex = stream.readInt<int>();
836 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400837 stream.readInt(&locationData.element);
838 stream.readInt(&locationData.index);
839 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400840 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400841 }
842
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400843 stream.readInt(&mSamplerUniformRange.start);
844 stream.readInt(&mSamplerUniformRange.end);
845
Jamie Madillb0a838b2016-11-13 20:02:12 -0500846 ANGLE_TRY_RESULT(mProgram->load(mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000847
Jamie Madillb0a838b2016-11-13 20:02:12 -0500848 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500849#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
850}
851
852Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
853{
854 if (binaryFormat)
855 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400856 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500857 }
858
859 BinaryOutputStream stream;
860
Geoff Lang7dd2e102014-11-10 15:19:26 -0500861 stream.writeInt(ANGLE_MAJOR_VERSION);
862 stream.writeInt(ANGLE_MINOR_VERSION);
863 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
864
Martin Radev4c4c8e72016-08-04 12:25:34 +0300865 stream.writeInt(mState.mComputeShaderLocalSize[0]);
866 stream.writeInt(mState.mComputeShaderLocalSize[1]);
867 stream.writeInt(mState.mComputeShaderLocalSize[2]);
868
Jamie Madill48ef11b2016-04-27 15:21:52 -0400869 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500870
Jamie Madill48ef11b2016-04-27 15:21:52 -0400871 stream.writeInt(mState.mAttributes.size());
872 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400873 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400874 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400875 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400876 }
877
Jamie Madill48ef11b2016-04-27 15:21:52 -0400878 stream.writeInt(mState.mUniforms.size());
879 for (const gl::LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400880 {
881 WriteShaderVar(&stream, uniform);
882
883 // FIXME: referenced
884
885 stream.writeInt(uniform.blockIndex);
886 stream.writeInt(uniform.blockInfo.offset);
887 stream.writeInt(uniform.blockInfo.arrayStride);
888 stream.writeInt(uniform.blockInfo.matrixStride);
889 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
890 }
891
Jamie Madill48ef11b2016-04-27 15:21:52 -0400892 stream.writeInt(mState.mUniformLocations.size());
893 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400894 {
895 stream.writeString(variable.name);
896 stream.writeInt(variable.element);
897 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400898 stream.writeInt(variable.used);
899 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400900 }
901
Jamie Madill48ef11b2016-04-27 15:21:52 -0400902 stream.writeInt(mState.mUniformBlocks.size());
903 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400904 {
905 stream.writeString(uniformBlock.name);
906 stream.writeInt(uniformBlock.isArray);
907 stream.writeInt(uniformBlock.arrayElement);
908 stream.writeInt(uniformBlock.dataSize);
909
910 stream.writeInt(uniformBlock.vertexStaticUse);
911 stream.writeInt(uniformBlock.fragmentStaticUse);
912
913 stream.writeInt(uniformBlock.memberUniformIndexes.size());
914 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
915 {
916 stream.writeInt(memberUniformIndex);
917 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400918 }
919
Jamie Madill48ef11b2016-04-27 15:21:52 -0400920 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
921 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700922 {
923 stream.writeInt(varying.arraySize);
924 stream.writeInt(varying.type);
925 stream.writeString(varying.name);
926 }
927
Jamie Madill48ef11b2016-04-27 15:21:52 -0400928 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400929
Jamie Madill48ef11b2016-04-27 15:21:52 -0400930 stream.writeInt(mState.mOutputVariables.size());
931 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400932 {
933 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400934 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400935 stream.writeInt(outputPair.second.index);
936 stream.writeString(outputPair.second.name);
937 }
938
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400939 stream.writeInt(mSamplerUniformRange.start);
940 stream.writeInt(mSamplerUniformRange.end);
941
Geoff Lang7dd2e102014-11-10 15:19:26 -0500942 gl::Error error = mProgram->save(&stream);
943 if (error.isError())
944 {
945 return error;
946 }
947
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700948 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400949 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500950
951 if (streamLength > bufSize)
952 {
953 if (length)
954 {
955 *length = 0;
956 }
957
958 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
959 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
960 // sizes and then copy it.
961 return Error(GL_INVALID_OPERATION);
962 }
963
964 if (binary)
965 {
966 char *ptr = reinterpret_cast<char*>(binary);
967
Jamie Madill48ef11b2016-04-27 15:21:52 -0400968 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500969 ptr += streamLength;
970
971 ASSERT(ptr - streamLength == binary);
972 }
973
974 if (length)
975 {
976 *length = streamLength;
977 }
978
979 return Error(GL_NO_ERROR);
980}
981
982GLint Program::getBinaryLength() const
983{
984 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400985 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500986 if (error.isError())
987 {
988 return 0;
989 }
990
991 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000992}
993
Geoff Langc5629752015-12-07 16:29:04 -0500994void Program::setBinaryRetrievableHint(bool retrievable)
995{
996 // TODO(jmadill) : replace with dirty bits
997 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400998 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500999}
1000
1001bool Program::getBinaryRetrievableHint() const
1002{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001003 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001004}
1005
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006void Program::release()
1007{
1008 mRefCount--;
1009
1010 if (mRefCount == 0 && mDeleteStatus)
1011 {
1012 mResourceManager->deleteProgram(mHandle);
1013 }
1014}
1015
1016void Program::addRef()
1017{
1018 mRefCount++;
1019}
1020
1021unsigned int Program::getRefCount() const
1022{
1023 return mRefCount;
1024}
1025
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001026int Program::getInfoLogLength() const
1027{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001028 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001029}
1030
Geoff Lange1a27752015-10-05 13:16:04 -04001031void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001032{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001033 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001034}
1035
Geoff Lange1a27752015-10-05 13:16:04 -04001036void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001037{
1038 int total = 0;
1039
Martin Radev4c4c8e72016-08-04 12:25:34 +03001040 if (mState.mAttachedComputeShader)
1041 {
1042 if (total < maxCount)
1043 {
1044 shaders[total] = mState.mAttachedComputeShader->getHandle();
1045 total++;
1046 }
1047 }
1048
Jamie Madill48ef11b2016-04-27 15:21:52 -04001049 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001050 {
1051 if (total < maxCount)
1052 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001053 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001054 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001055 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001056 }
1057
Jamie Madill48ef11b2016-04-27 15:21:52 -04001058 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001059 {
1060 if (total < maxCount)
1061 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001062 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001063 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001064 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001065 }
1066
1067 if (count)
1068 {
1069 *count = total;
1070 }
1071}
1072
Geoff Lange1a27752015-10-05 13:16:04 -04001073GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001074{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001075 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001077 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001078 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001079 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001080 }
1081 }
1082
Austin Kinrossb8af7232015-03-16 22:33:25 -07001083 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084}
1085
Jamie Madill63805b42015-08-25 13:17:39 -04001086bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001087{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001088 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1089 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090}
1091
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1093{
Jamie Madillc349ec02015-08-21 16:53:12 -04001094 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001095 {
1096 if (bufsize > 0)
1097 {
1098 name[0] = '\0';
1099 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001100
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001101 if (length)
1102 {
1103 *length = 0;
1104 }
1105
1106 *type = GL_NONE;
1107 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001108 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001109 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001110
1111 size_t attributeIndex = 0;
1112
Jamie Madill48ef11b2016-04-27 15:21:52 -04001113 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001114 {
1115 // Skip over inactive attributes
1116 if (attribute.staticUse)
1117 {
1118 if (static_cast<size_t>(index) == attributeIndex)
1119 {
1120 break;
1121 }
1122 attributeIndex++;
1123 }
1124 }
1125
Jamie Madill48ef11b2016-04-27 15:21:52 -04001126 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1127 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001128
1129 if (bufsize > 0)
1130 {
1131 const char *string = attrib.name.c_str();
1132
1133 strncpy(name, string, bufsize);
1134 name[bufsize - 1] = '\0';
1135
1136 if (length)
1137 {
1138 *length = static_cast<GLsizei>(strlen(name));
1139 }
1140 }
1141
1142 // Always a single 'type' instance
1143 *size = 1;
1144 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001145}
1146
Geoff Lange1a27752015-10-05 13:16:04 -04001147GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001148{
Jamie Madillc349ec02015-08-21 16:53:12 -04001149 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001150 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001151 return 0;
1152 }
1153
1154 GLint count = 0;
1155
Jamie Madill48ef11b2016-04-27 15:21:52 -04001156 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001157 {
1158 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001159 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001160
1161 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001162}
1163
Geoff Lange1a27752015-10-05 13:16:04 -04001164GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001165{
Jamie Madillc349ec02015-08-21 16:53:12 -04001166 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001167 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001168 return 0;
1169 }
1170
1171 size_t maxLength = 0;
1172
Jamie Madill48ef11b2016-04-27 15:21:52 -04001173 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001174 {
1175 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001176 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001177 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001179 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180
Jamie Madillc349ec02015-08-21 16:53:12 -04001181 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001182}
1183
Geoff Lang7dd2e102014-11-10 15:19:26 -05001184GLint Program::getFragDataLocation(const std::string &name) const
1185{
1186 std::string baseName(name);
1187 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001188 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001189 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001190 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1192 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001193 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001194 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001195 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001196 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001197}
1198
Geoff Lange1a27752015-10-05 13:16:04 -04001199void Program::getActiveUniform(GLuint index,
1200 GLsizei bufsize,
1201 GLsizei *length,
1202 GLint *size,
1203 GLenum *type,
1204 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001205{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001206 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001207 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001208 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001209 ASSERT(index < mState.mUniforms.size());
1210 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211
1212 if (bufsize > 0)
1213 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001214 std::string string = uniform.name;
1215 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 {
1217 string += "[0]";
1218 }
1219
1220 strncpy(name, string.c_str(), bufsize);
1221 name[bufsize - 1] = '\0';
1222
1223 if (length)
1224 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001225 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226 }
1227 }
1228
Jamie Madill62d31cb2015-09-11 13:25:51 -04001229 *size = uniform.elementCount();
1230 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001231 }
1232 else
1233 {
1234 if (bufsize > 0)
1235 {
1236 name[0] = '\0';
1237 }
1238
1239 if (length)
1240 {
1241 *length = 0;
1242 }
1243
1244 *size = 0;
1245 *type = GL_NONE;
1246 }
1247}
1248
Geoff Lange1a27752015-10-05 13:16:04 -04001249GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001250{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001253 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254 }
1255 else
1256 {
1257 return 0;
1258 }
1259}
1260
Geoff Lange1a27752015-10-05 13:16:04 -04001261GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001262{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001263 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264
1265 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001266 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001267 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001268 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001269 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001271 size_t length = uniform.name.length() + 1u;
1272 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273 {
1274 length += 3; // Counting in "[0]".
1275 }
1276 maxLength = std::max(length, maxLength);
1277 }
1278 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001279 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280
Jamie Madill62d31cb2015-09-11 13:25:51 -04001281 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001282}
1283
1284GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1285{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001286 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
1287 const gl::LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1291 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1292 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1293 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1294 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1295 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1296 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1297 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1298 default:
1299 UNREACHABLE();
1300 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001301 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302 return 0;
1303}
1304
1305bool Program::isValidUniformLocation(GLint location) const
1306{
Jamie Madille2e406c2016-06-02 13:04:10 -04001307 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001308 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1309 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001310}
1311
1312bool Program::isIgnoredUniformLocation(GLint location) const
1313{
1314 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1315 // out
1316 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001317 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1318 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
Jamie Madill62d31cb2015-09-11 13:25:51 -04001321const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001322{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001323 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1324 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325}
1326
Jamie Madill62d31cb2015-09-11 13:25:51 -04001327GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001328{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001329 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330}
1331
Jamie Madill62d31cb2015-09-11 13:25:51 -04001332GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001333{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001334 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335}
1336
1337void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1338{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001339 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340 mProgram->setUniform1fv(location, count, v);
1341}
1342
1343void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1344{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001345 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346 mProgram->setUniform2fv(location, count, v);
1347}
1348
1349void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1350{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001351 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001352 mProgram->setUniform3fv(location, count, v);
1353}
1354
1355void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1356{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001357 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001358 mProgram->setUniform4fv(location, count, v);
1359}
1360
1361void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1362{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001363 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364 mProgram->setUniform1iv(location, count, v);
1365}
1366
1367void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1368{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001369 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001370 mProgram->setUniform2iv(location, count, v);
1371}
1372
1373void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1374{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001375 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376 mProgram->setUniform3iv(location, count, v);
1377}
1378
1379void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1380{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001381 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382 mProgram->setUniform4iv(location, count, v);
1383}
1384
1385void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1386{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001387 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388 mProgram->setUniform1uiv(location, count, v);
1389}
1390
1391void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1392{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001393 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394 mProgram->setUniform2uiv(location, count, v);
1395}
1396
1397void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1398{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001399 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400 mProgram->setUniform3uiv(location, count, v);
1401}
1402
1403void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1404{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001405 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406 mProgram->setUniform4uiv(location, count, v);
1407}
1408
1409void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1410{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001411 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1413}
1414
1415void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1416{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1419}
1420
1421void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1422{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001423 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1425}
1426
1427void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1428{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001429 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1431}
1432
1433void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1434{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001435 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1437}
1438
1439void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1440{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001441 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1443}
1444
1445void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1446{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001447 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1449}
1450
1451void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1452{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001453 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1455}
1456
1457void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1458{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1461}
1462
Geoff Lange1a27752015-10-05 13:16:04 -04001463void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001465 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466}
1467
Geoff Lange1a27752015-10-05 13:16:04 -04001468void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001470 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471}
1472
Geoff Lange1a27752015-10-05 13:16:04 -04001473void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476}
1477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478void Program::flagForDeletion()
1479{
1480 mDeleteStatus = true;
1481}
1482
1483bool Program::isFlaggedForDeletion() const
1484{
1485 return mDeleteStatus;
1486}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001487
Brandon Jones43a53e22014-08-28 16:23:22 -07001488void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001489{
1490 mInfoLog.reset();
1491
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001493 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001494 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001495 }
1496 else
1497 {
Jamie Madillf6113162015-05-07 11:49:21 -04001498 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001499 }
1500}
1501
Geoff Lang7dd2e102014-11-10 15:19:26 -05001502bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1503{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001504 // Skip cache if we're using an infolog, so we get the full error.
1505 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1506 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1507 {
1508 return mCachedValidateSamplersResult.value();
1509 }
1510
1511 if (mTextureUnitTypesCache.empty())
1512 {
1513 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1514 }
1515 else
1516 {
1517 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1518 }
1519
1520 // if any two active samplers in a program are of different types, but refer to the same
1521 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1522 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1523 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1524 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1525 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001526 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001527 ASSERT(uniform.isSampler());
1528
1529 if (!uniform.staticUse)
1530 continue;
1531
1532 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1533 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1534
1535 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1536 {
1537 GLuint textureUnit = dataPtr[arrayElement];
1538
1539 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1540 {
1541 if (infoLog)
1542 {
1543 (*infoLog) << "Sampler uniform (" << textureUnit
1544 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1545 << caps.maxCombinedTextureImageUnits << ")";
1546 }
1547
1548 mCachedValidateSamplersResult = false;
1549 return false;
1550 }
1551
1552 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1553 {
1554 if (textureType != mTextureUnitTypesCache[textureUnit])
1555 {
1556 if (infoLog)
1557 {
1558 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1559 "image unit ("
1560 << textureUnit << ").";
1561 }
1562
1563 mCachedValidateSamplersResult = false;
1564 return false;
1565 }
1566 }
1567 else
1568 {
1569 mTextureUnitTypesCache[textureUnit] = textureType;
1570 }
1571 }
1572 }
1573
1574 mCachedValidateSamplersResult = true;
1575 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576}
1577
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001578bool Program::isValidated() const
1579{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580 return mValidated;
1581}
1582
Geoff Lange1a27752015-10-05 13:16:04 -04001583GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001585 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586}
1587
1588void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1589{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001590 ASSERT(
1591 uniformBlockIndex <
1592 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593
Jamie Madill48ef11b2016-04-27 15:21:52 -04001594 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001595
1596 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001597 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001598 std::string string = uniformBlock.name;
1599
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001602 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603 }
1604
1605 strncpy(uniformBlockName, string.c_str(), bufSize);
1606 uniformBlockName[bufSize - 1] = '\0';
1607
1608 if (length)
1609 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001610 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001612 }
1613}
1614
Geoff Lange1a27752015-10-05 13:16:04 -04001615GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001616{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617 int maxLength = 0;
1618
1619 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001620 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001621 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1623 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001624 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 if (!uniformBlock.name.empty())
1626 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001627 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628
1629 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631
1632 maxLength = std::max(length + arrayLength, maxLength);
1633 }
1634 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001635 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636
1637 return maxLength;
1638}
1639
Geoff Lange1a27752015-10-05 13:16:04 -04001640GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001641{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001642 size_t subscript = GL_INVALID_INDEX;
1643 std::string baseName = gl::ParseUniformName(name, &subscript);
1644
Jamie Madill48ef11b2016-04-27 15:21:52 -04001645 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1647 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001648 const gl::UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001649 if (uniformBlock.name == baseName)
1650 {
1651 const bool arrayElementZero =
1652 (subscript == GL_INVALID_INDEX &&
1653 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1654 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1655 {
1656 return blockIndex;
1657 }
1658 }
1659 }
1660
1661 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001662}
1663
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001665{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001666 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1667 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001668}
1669
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001670void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1671{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001672 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001673 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001674}
1675
1676GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1677{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001678 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001679}
1680
1681void Program::resetUniformBlockBindings()
1682{
1683 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1684 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001685 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001686 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001687 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001688}
1689
Geoff Lang48dcae72014-02-05 16:28:24 -05001690void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1691{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001692 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001693 for (GLsizei i = 0; i < count; i++)
1694 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001695 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001696 }
1697
Jamie Madill48ef11b2016-04-27 15:21:52 -04001698 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001699}
1700
1701void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1702{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001703 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001704 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001705 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1706 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001707 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1708 if (length)
1709 {
1710 *length = lastNameIdx;
1711 }
1712 if (size)
1713 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001714 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001715 }
1716 if (type)
1717 {
1718 *type = varying.type;
1719 }
1720 if (name)
1721 {
1722 memcpy(name, varying.name.c_str(), lastNameIdx);
1723 name[lastNameIdx] = '\0';
1724 }
1725 }
1726}
1727
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001728GLsizei Program::getTransformFeedbackVaryingCount() const
1729{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001731 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001732 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001733 }
1734 else
1735 {
1736 return 0;
1737 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001738}
1739
1740GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1741{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001742 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001743 {
1744 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001745 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001746 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001747 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1748 }
1749
1750 return maxSize;
1751 }
1752 else
1753 {
1754 return 0;
1755 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001756}
1757
1758GLenum Program::getTransformFeedbackBufferMode() const
1759{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001760 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001761}
1762
Jamie Madillada9ecc2015-08-17 12:53:37 -04001763bool Program::linkVaryings(InfoLog &infoLog,
1764 const Shader *vertexShader,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001765 const Shader *fragmentShader) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001766{
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001767 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1768
Jamie Madill4cff2472015-08-21 16:53:18 -04001769 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1770 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771
Sami Väisänen46eaa942016-06-29 10:26:37 +03001772 std::map<GLuint, std::string> staticFragmentInputLocations;
1773
Jamie Madill4cff2472015-08-21 16:53:18 -04001774 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 bool matched = false;
1777
1778 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001779 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780 {
1781 continue;
1782 }
1783
Jamie Madill4cff2472015-08-21 16:53:18 -04001784 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001786 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001788 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001789 if (!linkValidateVaryings(infoLog, output.name, input, output,
1790 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 {
1792 return false;
1793 }
1794
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 matched = true;
1796 break;
1797 }
1798 }
1799
1800 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001801 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001803 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 return false;
1805 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001806
1807 // Check for aliased path rendering input bindings (if any).
1808 // If more than one binding refer statically to the same
1809 // location the link must fail.
1810
1811 if (!output.staticUse)
1812 continue;
1813
1814 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1815 if (inputBinding == -1)
1816 continue;
1817
1818 const auto it = staticFragmentInputLocations.find(inputBinding);
1819 if (it == std::end(staticFragmentInputLocations))
1820 {
1821 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1822 }
1823 else
1824 {
1825 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1826 << it->second;
1827 return false;
1828 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 }
1830
Jamie Madillada9ecc2015-08-17 12:53:37 -04001831 // TODO(jmadill): verify no unmatched vertex varyings?
1832
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 return true;
1834}
1835
Martin Radev4c4c8e72016-08-04 12:25:34 +03001836bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001837{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001838 // Check that uniforms defined in the vertex and fragment shaders are identical
1839 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001840 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1841 const std::vector<sh::Uniform> &fragmentUniforms =
1842 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001843
Jamie Madillea918db2015-08-18 14:48:59 -04001844 for (const sh::Uniform &vertexUniform : vertexUniforms)
1845 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001846 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001847 }
1848
1849 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1850 {
1851 auto entry = linkedUniforms.find(fragmentUniform.name);
1852 if (entry != linkedUniforms.end())
1853 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001854 LinkedUniform *vertexUniform = &entry->second;
1855 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1856 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001857 {
1858 return false;
1859 }
1860 }
1861 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001862 return true;
1863}
1864
1865bool Program::linkUniforms(gl::InfoLog &infoLog,
1866 const gl::Caps &caps,
1867 const Bindings &uniformBindings)
1868{
1869 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1870 {
1871 ASSERT(mState.mAttachedComputeShader == nullptr);
1872 if (!validateVertexAndFragmentUniforms(infoLog))
1873 {
1874 return false;
1875 }
1876 }
Jamie Madillea918db2015-08-18 14:48:59 -04001877
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1879 // Also check the maximum uniform vector and sampler counts.
1880 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1881 {
1882 return false;
1883 }
1884
Geoff Langd8605522016-04-13 10:19:12 -04001885 if (!indexUniforms(infoLog, caps, uniformBindings))
1886 {
1887 return false;
1888 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001889
Jamie Madillea918db2015-08-18 14:48:59 -04001890 return true;
1891}
1892
Geoff Langd8605522016-04-13 10:19:12 -04001893bool Program::indexUniforms(gl::InfoLog &infoLog,
1894 const gl::Caps &caps,
1895 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001896{
Geoff Langd8605522016-04-13 10:19:12 -04001897 // Uniforms awaiting a location
1898 std::vector<VariableLocation> unboundUniforms;
1899 std::map<GLuint, VariableLocation> boundUniforms;
1900 int maxUniformLocation = -1;
1901
1902 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001903 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001905 const gl::LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906
Geoff Langd8605522016-04-13 10:19:12 -04001907 if (uniform.isBuiltIn())
1908 {
1909 continue;
1910 }
1911
1912 int bindingLocation = uniformBindings.getBinding(uniform.name);
1913
1914 // Verify that this location isn't bound twice
1915 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1916 {
1917 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1918 return false;
1919 }
1920
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1922 {
Geoff Langd8605522016-04-13 10:19:12 -04001923 VariableLocation location(uniform.name, arrayIndex,
1924 static_cast<unsigned int>(uniformIndex));
1925
1926 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001927 {
Geoff Langd8605522016-04-13 10:19:12 -04001928 boundUniforms[bindingLocation] = location;
1929 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1930 }
1931 else
1932 {
1933 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 }
1935 }
1936 }
Geoff Langd8605522016-04-13 10:19:12 -04001937
1938 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1939 // not be assigned to those locations.
1940 std::set<GLuint> reservedLocations;
1941 for (const auto &binding : uniformBindings)
1942 {
1943 GLuint location = binding.second;
1944 if (boundUniforms.find(location) == boundUniforms.end())
1945 {
1946 reservedLocations.insert(location);
1947 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1948 }
1949 }
1950
1951 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04001952 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04001953 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1954 static_cast<size_t>(maxUniformLocation + 1)));
1955
1956 // Assign bound uniforms
1957 for (const auto &boundUniform : boundUniforms)
1958 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001959 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04001960 }
1961
1962 // Assign reserved uniforms
1963 for (const auto &reservedLocation : reservedLocations)
1964 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001965 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04001966 }
1967
1968 // Assign unbound uniforms
1969 size_t nextUniformLocation = 0;
1970 for (const auto &unboundUniform : unboundUniforms)
1971 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001972 while (mState.mUniformLocations[nextUniformLocation].used ||
1973 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04001974 {
1975 nextUniformLocation++;
1976 }
1977
Jamie Madill48ef11b2016-04-27 15:21:52 -04001978 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
1979 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04001980 nextUniformLocation++;
1981 }
1982
1983 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984}
1985
Martin Radev4c4c8e72016-08-04 12:25:34 +03001986bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1987 const std::string &uniformName,
1988 const sh::InterfaceBlockField &vertexUniform,
1989 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990{
Jamie Madillc4c744222015-11-04 09:39:47 -05001991 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1992 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993 {
1994 return false;
1995 }
1996
1997 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1998 {
Jamie Madillf6113162015-05-07 11:49:21 -04001999 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000 return false;
2001 }
2002
2003 return true;
2004}
2005
2006// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill9082b982016-04-27 15:21:51 -04002007bool Program::linkAttributes(const ContextState &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04002008 InfoLog &infoLog,
Geoff Langd8605522016-04-13 10:19:12 -04002009 const Bindings &attributeBindings,
Jamie Madill3da79b72015-04-27 11:09:17 -04002010 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002013 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002014 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002015
2016 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002017 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002018 {
Jamie Madillf6113162015-05-07 11:49:21 -04002019 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002020 return false;
2021 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002023 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002024
Jamie Madillc349ec02015-08-21 16:53:12 -04002025 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002026 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002027 {
2028 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029 ASSERT(attribute.staticUse);
2030
Geoff Langd8605522016-04-13 10:19:12 -04002031 int bindingLocation = attributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002032 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002033 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002034 attribute.location = bindingLocation;
2035 }
2036
2037 if (attribute.location != -1)
2038 {
2039 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002040 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041
Jamie Madill63805b42015-08-25 13:17:39 -04002042 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
Jamie Madillf6113162015-05-07 11:49:21 -04002044 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002045 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046
2047 return false;
2048 }
2049
Jamie Madill63805b42015-08-25 13:17:39 -04002050 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051 {
Jamie Madill63805b42015-08-25 13:17:39 -04002052 const int regLocation = attribute.location + reg;
2053 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002054
2055 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002056 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002057 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002059 // TODO(jmadill): fix aliasing on ES2
2060 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002062 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002063 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 return false;
2065 }
2066 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002067 else
2068 {
Jamie Madill63805b42015-08-25 13:17:39 -04002069 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071
Jamie Madill63805b42015-08-25 13:17:39 -04002072 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073 }
2074 }
2075 }
2076
2077 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002078 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080 ASSERT(attribute.staticUse);
2081
Jamie Madillc349ec02015-08-21 16:53:12 -04002082 // Not set by glBindAttribLocation or by location layout qualifier
2083 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084 {
Jamie Madill63805b42015-08-25 13:17:39 -04002085 int regs = VariableRegisterCount(attribute.type);
2086 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087
Jamie Madill63805b42015-08-25 13:17:39 -04002088 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 {
Jamie Madillf6113162015-05-07 11:49:21 -04002090 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 }
2093
Jamie Madillc349ec02015-08-21 16:53:12 -04002094 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002095 }
2096 }
2097
Jamie Madill48ef11b2016-04-27 15:21:52 -04002098 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002100 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002101 ASSERT(attribute.location != -1);
2102 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002103
Jamie Madill63805b42015-08-25 13:17:39 -04002104 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002106 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 }
2108 }
2109
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 return true;
2111}
2112
Martin Radev4c4c8e72016-08-04 12:25:34 +03002113bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2114 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2115 const std::string &errorMessage,
2116 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002118 GLuint blockCount = 0;
2119 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002121 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002122 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002123 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002124 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002125 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002126 return false;
2127 }
2128 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002130 return true;
2131}
Jamie Madille473dee2015-08-18 14:49:01 -04002132
Martin Radev4c4c8e72016-08-04 12:25:34 +03002133bool Program::validateVertexAndFragmentInterfaceBlocks(
2134 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2135 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2136 InfoLog &infoLog) const
2137{
2138 // Check that interface blocks defined in the vertex and fragment shaders are identical
2139 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2140 UniformBlockMap linkedUniformBlocks;
2141
2142 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2143 {
2144 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2145 }
2146
Jamie Madille473dee2015-08-18 14:49:01 -04002147 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 {
Jamie Madille473dee2015-08-18 14:49:01 -04002149 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150 if (entry != linkedUniformBlocks.end())
2151 {
2152 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2153 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2154 {
2155 return false;
2156 }
2157 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002158 }
2159 return true;
2160}
Jamie Madille473dee2015-08-18 14:49:01 -04002161
Martin Radev4c4c8e72016-08-04 12:25:34 +03002162bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2163{
2164 if (mState.mAttachedComputeShader)
2165 {
2166 const Shader &computeShader = *mState.mAttachedComputeShader;
2167 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2168
2169 if (!validateUniformBlocksCount(
2170 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2171 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2172 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002174 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002176 return true;
2177 }
2178
2179 const Shader &vertexShader = *mState.mAttachedVertexShader;
2180 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2181
2182 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2183 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2184
2185 if (!validateUniformBlocksCount(
2186 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2187 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2188 {
2189 return false;
2190 }
2191 if (!validateUniformBlocksCount(
2192 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2193 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2194 infoLog))
2195 {
2196
2197 return false;
2198 }
2199 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2200 infoLog))
2201 {
2202 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002203 }
Jamie Madille473dee2015-08-18 14:49:01 -04002204
Geoff Lang7dd2e102014-11-10 15:19:26 -05002205 return true;
2206}
2207
Martin Radev4c4c8e72016-08-04 12:25:34 +03002208bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog,
2209 const sh::InterfaceBlock &vertexInterfaceBlock,
2210 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002211{
2212 const char* blockName = vertexInterfaceBlock.name.c_str();
2213 // validate blocks for the same member types
2214 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2215 {
Jamie Madillf6113162015-05-07 11:49:21 -04002216 infoLog << "Types for interface block '" << blockName
2217 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218 return false;
2219 }
2220 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2221 {
Jamie Madillf6113162015-05-07 11:49:21 -04002222 infoLog << "Array sizes differ for interface block '" << blockName
2223 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 return false;
2225 }
2226 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2227 {
Jamie Madillf6113162015-05-07 11:49:21 -04002228 infoLog << "Layout qualifiers differ for interface block '" << blockName
2229 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002230 return false;
2231 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002232 const unsigned int numBlockMembers =
2233 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2235 {
2236 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2237 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2238 if (vertexMember.name != fragmentMember.name)
2239 {
Jamie Madillf6113162015-05-07 11:49:21 -04002240 infoLog << "Name mismatch for field " << blockMemberIndex
2241 << " of interface block '" << blockName
2242 << "': (in vertex: '" << vertexMember.name
2243 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002244 return false;
2245 }
2246 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2247 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2248 {
2249 return false;
2250 }
2251 }
2252 return true;
2253}
2254
2255bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2256 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2257{
2258 if (vertexVariable.type != fragmentVariable.type)
2259 {
Jamie Madillf6113162015-05-07 11:49:21 -04002260 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002261 return false;
2262 }
2263 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2264 {
Jamie Madillf6113162015-05-07 11:49:21 -04002265 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002266 return false;
2267 }
2268 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2269 {
Jamie Madillf6113162015-05-07 11:49:21 -04002270 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002271 return false;
2272 }
2273
2274 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2275 {
Jamie Madillf6113162015-05-07 11:49:21 -04002276 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002277 return false;
2278 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002279 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002280 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2281 {
2282 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2283 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2284
2285 if (vertexMember.name != fragmentMember.name)
2286 {
Jamie Madillf6113162015-05-07 11:49:21 -04002287 infoLog << "Name mismatch for field '" << memberIndex
2288 << "' of " << variableName
2289 << ": (in vertex: '" << vertexMember.name
2290 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002291 return false;
2292 }
2293
2294 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2295 vertexMember.name + "'";
2296
2297 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2298 {
2299 return false;
2300 }
2301 }
2302
2303 return true;
2304}
2305
2306bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2307{
Cooper Partin1acf4382015-06-12 12:38:57 -07002308#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2309 const bool validatePrecision = true;
2310#else
2311 const bool validatePrecision = false;
2312#endif
2313
2314 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002315 {
2316 return false;
2317 }
2318
2319 return true;
2320}
2321
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002322bool Program::linkValidateVaryings(InfoLog &infoLog,
2323 const std::string &varyingName,
2324 const sh::Varying &vertexVarying,
2325 const sh::Varying &fragmentVarying,
2326 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002327{
2328 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2329 {
2330 return false;
2331 }
2332
Jamie Madille9cc4692015-02-19 16:00:13 -05002333 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002334 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002335 infoLog << "Interpolation types for " << varyingName
2336 << " differ between vertex and fragment shaders.";
2337 return false;
2338 }
2339
2340 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2341 {
2342 infoLog << "Invariance for " << varyingName
2343 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002344 return false;
2345 }
2346
2347 return true;
2348}
2349
Jamie Madillccdf74b2015-08-18 10:46:12 -04002350bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2351 const std::vector<const sh::Varying *> &varyings,
2352 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002353{
2354 size_t totalComponents = 0;
2355
Jamie Madillccdf74b2015-08-18 10:46:12 -04002356 std::set<std::string> uniqueNames;
2357
Jamie Madill48ef11b2016-04-27 15:21:52 -04002358 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 {
2360 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002361 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002362 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002363 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002365 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002366 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002367 infoLog << "Two transform feedback varyings specify the same output variable ("
2368 << tfVaryingName << ").";
2369 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002370 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002371 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002372
Geoff Lang1a683462015-09-29 15:09:59 -04002373 if (varying->isArray())
2374 {
2375 infoLog << "Capture of arrays is undefined and not supported.";
2376 return false;
2377 }
2378
Jamie Madillccdf74b2015-08-18 10:46:12 -04002379 // TODO(jmadill): Investigate implementation limits on D3D11
2380 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002381 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002382 componentCount > caps.maxTransformFeedbackSeparateComponents)
2383 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002384 infoLog << "Transform feedback varying's " << varying->name << " components ("
2385 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002386 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387 return false;
2388 }
2389
2390 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002391 found = true;
2392 break;
2393 }
2394 }
2395
Jamie Madill89bb70e2015-08-31 14:18:39 -04002396 if (tfVaryingName.find('[') != std::string::npos)
2397 {
Geoff Lang1a683462015-09-29 15:09:59 -04002398 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002399 return false;
2400 }
2401
Geoff Lang7dd2e102014-11-10 15:19:26 -05002402 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2403 ASSERT(found);
2404 }
2405
Jamie Madill48ef11b2016-04-27 15:21:52 -04002406 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002407 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002408 {
Jamie Madillf6113162015-05-07 11:49:21 -04002409 infoLog << "Transform feedback varying total components (" << totalComponents
2410 << ") exceed the maximum interleaved components ("
2411 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002412 return false;
2413 }
2414
2415 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002416}
2417
Jamie Madillccdf74b2015-08-18 10:46:12 -04002418void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2419{
2420 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002421 mState.mTransformFeedbackVaryingVars.clear();
2422 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002423 {
2424 for (const sh::Varying *varying : varyings)
2425 {
2426 if (tfVaryingName == varying->name)
2427 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002428 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002429 break;
2430 }
2431 }
2432 }
2433}
2434
2435std::vector<const sh::Varying *> Program::getMergedVaryings() const
2436{
2437 std::set<std::string> uniqueNames;
2438 std::vector<const sh::Varying *> varyings;
2439
Jamie Madill48ef11b2016-04-27 15:21:52 -04002440 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002441 {
2442 if (uniqueNames.count(varying.name) == 0)
2443 {
2444 uniqueNames.insert(varying.name);
2445 varyings.push_back(&varying);
2446 }
2447 }
2448
Jamie Madill48ef11b2016-04-27 15:21:52 -04002449 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002450 {
2451 if (uniqueNames.count(varying.name) == 0)
2452 {
2453 uniqueNames.insert(varying.name);
2454 varyings.push_back(&varying);
2455 }
2456 }
2457
2458 return varyings;
2459}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002460
2461void Program::linkOutputVariables()
2462{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002463 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002464 ASSERT(fragmentShader != nullptr);
2465
2466 // Skip this step for GLES2 shaders.
2467 if (fragmentShader->getShaderVersion() == 100)
2468 return;
2469
Jamie Madilla0a9e122015-09-02 15:54:30 -04002470 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002471
2472 // TODO(jmadill): any caps validation here?
2473
2474 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2475 outputVariableIndex++)
2476 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002477 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002478
2479 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2480 if (outputVariable.isBuiltIn())
2481 continue;
2482
2483 // Since multiple output locations must be specified, use 0 for non-specified locations.
2484 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2485
2486 ASSERT(outputVariable.staticUse);
2487
2488 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2489 elementIndex++)
2490 {
2491 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002492 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002493 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002494 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002495 VariableLocation(outputVariable.name, element, outputVariableIndex);
2496 }
2497 }
2498}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002499
Martin Radev4c4c8e72016-08-04 12:25:34 +03002500bool Program::flattenUniformsAndCheckCapsForShader(const gl::Shader &shader,
2501 GLuint maxUniformComponents,
2502 GLuint maxTextureImageUnits,
2503 const std::string &componentsErrorMessage,
2504 const std::string &samplerErrorMessage,
2505 std::vector<LinkedUniform> &samplerUniforms,
2506 InfoLog &infoLog)
2507{
2508 VectorAndSamplerCount vasCount;
2509 for (const sh::Uniform &uniform : shader.getUniforms())
2510 {
2511 if (uniform.staticUse)
2512 {
2513 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2514 }
2515 }
2516
2517 if (vasCount.vectorCount > maxUniformComponents)
2518 {
2519 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2520 return false;
2521 }
2522
2523 if (vasCount.samplerCount > maxTextureImageUnits)
2524 {
2525 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2526 return false;
2527 }
2528
2529 return true;
2530}
2531
Jamie Madill62d31cb2015-09-11 13:25:51 -04002532bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2533{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002534 std::vector<LinkedUniform> samplerUniforms;
2535
Martin Radev4c4c8e72016-08-04 12:25:34 +03002536 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002537 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002538 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2539
2540 // TODO (mradev): check whether we need finer-grained component counting
2541 if (!flattenUniformsAndCheckCapsForShader(
2542 *computeShader, caps.maxComputeUniformComponents / 4,
2543 caps.maxComputeTextureImageUnits,
2544 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2545 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2546 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002547 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002548 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002549 }
2550 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002551 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002552 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002553 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002554
Martin Radev4c4c8e72016-08-04 12:25:34 +03002555 if (!flattenUniformsAndCheckCapsForShader(
2556 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2557 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2558 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2559 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002560 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002561 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002562 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002563 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002564
Martin Radev4c4c8e72016-08-04 12:25:34 +03002565 if (!flattenUniformsAndCheckCapsForShader(
2566 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2567 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2568 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2569 infoLog))
2570 {
2571 return false;
2572 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002573 }
2574
Jamie Madill48ef11b2016-04-27 15:21:52 -04002575 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002576 mSamplerUniformRange.end =
2577 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2578
Jamie Madill48ef11b2016-04-27 15:21:52 -04002579 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002580
Jamie Madill62d31cb2015-09-11 13:25:51 -04002581 return true;
2582}
2583
2584Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002585 const std::string &fullName,
2586 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002587{
2588 VectorAndSamplerCount vectorAndSamplerCount;
2589
2590 if (uniform.isStruct())
2591 {
2592 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2593 {
2594 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2595
2596 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2597 {
2598 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2599 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2600
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002601 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002602 }
2603 }
2604
2605 return vectorAndSamplerCount;
2606 }
2607
2608 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002609 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002610 if (!UniformInList(mState.getUniforms(), fullName) &&
2611 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002612 {
2613 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2614 uniform.arraySize, -1,
2615 sh::BlockMemberInfo::getDefaultBlockInfo());
2616 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002617
2618 // Store sampler uniforms separately, so we'll append them to the end of the list.
2619 if (isSampler)
2620 {
2621 samplerUniforms->push_back(linkedUniform);
2622 }
2623 else
2624 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002625 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002626 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002627 }
2628
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002629 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002630
2631 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2632 // Likewise, don't count "real" uniforms towards sampler count.
2633 vectorAndSamplerCount.vectorCount =
2634 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002635 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002636
2637 return vectorAndSamplerCount;
2638}
2639
2640void Program::gatherInterfaceBlockInfo()
2641{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002642 ASSERT(mState.mUniformBlocks.empty());
2643
2644 if (mState.mAttachedComputeShader)
2645 {
2646 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2647
2648 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2649 {
2650
2651 // Only 'packed' blocks are allowed to be considered inactive.
2652 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2653 continue;
2654
2655 for (gl::UniformBlock &block : mState.mUniformBlocks)
2656 {
2657 if (block.name == computeBlock.name)
2658 {
2659 block.computeStaticUse = computeBlock.staticUse;
2660 }
2661 }
2662
2663 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2664 }
2665 return;
2666 }
2667
Jamie Madill62d31cb2015-09-11 13:25:51 -04002668 std::set<std::string> visitedList;
2669
Jamie Madill48ef11b2016-04-27 15:21:52 -04002670 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002671
Jamie Madill62d31cb2015-09-11 13:25:51 -04002672 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2673 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002674 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002675 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2676 continue;
2677
2678 if (visitedList.count(vertexBlock.name) > 0)
2679 continue;
2680
2681 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2682 visitedList.insert(vertexBlock.name);
2683 }
2684
Jamie Madill48ef11b2016-04-27 15:21:52 -04002685 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002686
2687 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2688 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002689 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002690 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2691 continue;
2692
2693 if (visitedList.count(fragmentBlock.name) > 0)
2694 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002695 for (gl::UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002696 {
2697 if (block.name == fragmentBlock.name)
2698 {
2699 block.fragmentStaticUse = fragmentBlock.staticUse;
2700 }
2701 }
2702
2703 continue;
2704 }
2705
2706 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2707 visitedList.insert(fragmentBlock.name);
2708 }
2709}
2710
Jamie Madill4a3c2342015-10-08 12:58:45 -04002711template <typename VarT>
2712void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2713 const std::string &prefix,
2714 int blockIndex)
2715{
2716 for (const VarT &field : fields)
2717 {
2718 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2719
2720 if (field.isStruct())
2721 {
2722 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2723 {
2724 const std::string uniformElementName =
2725 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2726 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2727 }
2728 }
2729 else
2730 {
2731 // If getBlockMemberInfo returns false, the uniform is optimized out.
2732 sh::BlockMemberInfo memberInfo;
2733 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2734 {
2735 continue;
2736 }
2737
2738 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2739 blockIndex, memberInfo);
2740
2741 // Since block uniforms have no location, we don't need to store them in the uniform
2742 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002743 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002744 }
2745 }
2746}
2747
Jamie Madill62d31cb2015-09-11 13:25:51 -04002748void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2749{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002750 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002751 size_t blockSize = 0;
2752
2753 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002754 std::stringstream blockNameStr;
2755 blockNameStr << interfaceBlock.name;
2756 if (interfaceBlock.arraySize > 0)
2757 {
2758 blockNameStr << "[0]";
2759 }
2760 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002761 {
2762 return;
2763 }
2764
2765 // Track the first and last uniform index to determine the range of active uniforms in the
2766 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002767 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002768 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002769 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002770
2771 std::vector<unsigned int> blockUniformIndexes;
2772 for (size_t blockUniformIndex = firstBlockUniformIndex;
2773 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2774 {
2775 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2776 }
2777
2778 if (interfaceBlock.arraySize > 0)
2779 {
2780 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2781 {
2782 UniformBlock block(interfaceBlock.name, true, arrayElement);
2783 block.memberUniformIndexes = blockUniformIndexes;
2784
Martin Radev4c4c8e72016-08-04 12:25:34 +03002785 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002786 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002787 case GL_VERTEX_SHADER:
2788 {
2789 block.vertexStaticUse = interfaceBlock.staticUse;
2790 break;
2791 }
2792 case GL_FRAGMENT_SHADER:
2793 {
2794 block.fragmentStaticUse = interfaceBlock.staticUse;
2795 break;
2796 }
2797 case GL_COMPUTE_SHADER:
2798 {
2799 block.computeStaticUse = interfaceBlock.staticUse;
2800 break;
2801 }
2802 default:
2803 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002804 }
2805
Qin Jiajia0350a642016-11-01 17:01:51 +08002806 // Since all block elements in an array share the same active uniforms, they will all be
2807 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2808 // here we will add every block element in the array.
2809 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002810 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002811 }
2812 }
2813 else
2814 {
2815 UniformBlock block(interfaceBlock.name, false, 0);
2816 block.memberUniformIndexes = blockUniformIndexes;
2817
Martin Radev4c4c8e72016-08-04 12:25:34 +03002818 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002820 case GL_VERTEX_SHADER:
2821 {
2822 block.vertexStaticUse = interfaceBlock.staticUse;
2823 break;
2824 }
2825 case GL_FRAGMENT_SHADER:
2826 {
2827 block.fragmentStaticUse = interfaceBlock.staticUse;
2828 break;
2829 }
2830 case GL_COMPUTE_SHADER:
2831 {
2832 block.computeStaticUse = interfaceBlock.staticUse;
2833 break;
2834 }
2835 default:
2836 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002837 }
2838
Jamie Madill4a3c2342015-10-08 12:58:45 -04002839 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002840 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002841 }
2842}
2843
2844template <typename T>
2845void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2846{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002847 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2848 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002849 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2850
2851 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2852 {
2853 // Do a cast conversion for boolean types. From the spec:
2854 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2855 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Geoff Lang399d1a12016-11-15 14:55:06 +00002856 for (GLsizei component = 0; component < count; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002857 {
2858 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2859 }
2860 }
2861 else
2862 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002863 // Invalide the validation cache if we modify the sampler data.
Geoff Lang399d1a12016-11-15 14:55:06 +00002864 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002865 {
2866 mCachedValidateSamplersResult.reset();
2867 }
2868
Geoff Lang399d1a12016-11-15 14:55:06 +00002869 memcpy(destPointer, v, sizeof(T) * count);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002870 }
2871}
2872
2873template <size_t cols, size_t rows, typename T>
2874void Program::setMatrixUniformInternal(GLint location,
2875 GLsizei count,
2876 GLboolean transpose,
2877 const T *v)
2878{
2879 if (!transpose)
2880 {
2881 setUniformInternal(location, count * cols * rows, v);
2882 return;
2883 }
2884
2885 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002886 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2887 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002888 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Geoff Lang399d1a12016-11-15 14:55:06 +00002889 for (GLsizei element = 0; element < count; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002890 {
2891 size_t elementOffset = element * rows * cols;
2892
2893 for (size_t row = 0; row < rows; ++row)
2894 {
2895 for (size_t col = 0; col < cols; ++col)
2896 {
2897 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2898 }
2899 }
2900 }
2901}
2902
2903template <typename DestT>
2904void Program::getUniformInternal(GLint location, DestT *dataOut) const
2905{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002906 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2907 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908
2909 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2910
2911 GLenum componentType = VariableComponentType(uniform.type);
2912 if (componentType == GLTypeToGLenum<DestT>::value)
2913 {
2914 memcpy(dataOut, srcPointer, uniform.getElementSize());
2915 return;
2916 }
2917
Corentin Wallez6596c462016-03-17 17:26:58 -04002918 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002919
2920 switch (componentType)
2921 {
2922 case GL_INT:
2923 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2924 break;
2925 case GL_UNSIGNED_INT:
2926 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2927 break;
2928 case GL_BOOL:
2929 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2930 break;
2931 case GL_FLOAT:
2932 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2933 break;
2934 default:
2935 UNREACHABLE();
2936 }
2937}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002938}