blob: 961ff3c31e56b1e1e5ee33d4ae3f7fded586b1db [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Jamie Madill9082b982016-04-27 15:21:51 -040020#include "libANGLE/ContextState.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040025#include "libANGLE/queryconversions.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040026#include "libANGLE/Uniform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028namespace gl
29{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000030
Geoff Lang7dd2e102014-11-10 15:19:26 -050031namespace
32{
33
Jamie Madill62d31cb2015-09-11 13:25:51 -040034void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
35{
36 stream->writeInt(var.type);
37 stream->writeInt(var.precision);
38 stream->writeString(var.name);
39 stream->writeString(var.mappedName);
40 stream->writeInt(var.arraySize);
41 stream->writeInt(var.staticUse);
42 stream->writeString(var.structName);
43 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050044}
45
Jamie Madill62d31cb2015-09-11 13:25:51 -040046void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
47{
48 var->type = stream->readInt<GLenum>();
49 var->precision = stream->readInt<GLenum>();
50 var->name = stream->readString();
51 var->mappedName = stream->readString();
52 var->arraySize = stream->readInt<unsigned int>();
53 var->staticUse = stream->readBool();
54 var->structName = stream->readString();
55}
56
Jamie Madill62d31cb2015-09-11 13:25:51 -040057// This simplified cast function doesn't need to worry about advanced concepts like
58// depth range values, or casting to bool.
59template <typename DestT, typename SrcT>
60DestT UniformStateQueryCast(SrcT value);
61
62// From-Float-To-Integer Casts
63template <>
64GLint UniformStateQueryCast(GLfloat value)
65{
66 return clampCast<GLint>(roundf(value));
67}
68
69template <>
70GLuint UniformStateQueryCast(GLfloat value)
71{
72 return clampCast<GLuint>(roundf(value));
73}
74
75// From-Integer-to-Integer Casts
76template <>
77GLint UniformStateQueryCast(GLuint value)
78{
79 return clampCast<GLint>(value);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLint value)
84{
85 return clampCast<GLuint>(value);
86}
87
88// From-Boolean-to-Anything Casts
89template <>
90GLfloat UniformStateQueryCast(GLboolean value)
91{
92 return (value == GL_TRUE ? 1.0f : 0.0f);
93}
94
95template <>
96GLint UniformStateQueryCast(GLboolean value)
97{
98 return (value == GL_TRUE ? 1 : 0);
99}
100
101template <>
102GLuint UniformStateQueryCast(GLboolean value)
103{
104 return (value == GL_TRUE ? 1u : 0u);
105}
106
107// Default to static_cast
108template <typename DestT, typename SrcT>
109DestT UniformStateQueryCast(SrcT value)
110{
111 return static_cast<DestT>(value);
112}
113
114template <typename SrcT, typename DestT>
115void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
116{
117 for (int comp = 0; comp < components; ++comp)
118 {
119 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
120 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
121 size_t offset = comp * 4;
122 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
123 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
124 }
125}
126
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400127bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
128{
129 for (const LinkedUniform &uniform : list)
130 {
131 if (uniform.name == name)
132 return true;
133 }
134
135 return false;
136}
137
Jamie Madill62d31cb2015-09-11 13:25:51 -0400138} // anonymous namespace
139
Jamie Madill4a3c2342015-10-08 12:58:45 -0400140const char *const g_fakepath = "C:\\fakepath";
141
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400142InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000143{
144}
145
146InfoLog::~InfoLog()
147{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000148}
149
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400150size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000151{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400152 const std::string &logString = mStream.str();
153 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000154}
155
Geoff Lange1a27752015-10-05 13:16:04 -0400156void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000157{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400158 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000159
160 if (bufSize > 0)
161 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400162 const std::string str(mStream.str());
163
164 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000165 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400166 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
167 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000168 }
169
170 infoLog[index] = '\0';
171 }
172
173 if (length)
174 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400175 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000176 }
177}
178
179// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300180// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000181// messages, so lets remove all occurrences of this fake file path from the log.
182void InfoLog::appendSanitized(const char *message)
183{
184 std::string msg(message);
185
186 size_t found;
187 do
188 {
189 found = msg.find(g_fakepath);
190 if (found != std::string::npos)
191 {
192 msg.erase(found, strlen(g_fakepath));
193 }
194 }
195 while (found != std::string::npos);
196
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400197 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000198}
199
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000200void InfoLog::reset()
201{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000202}
203
Geoff Langd8605522016-04-13 10:19:12 -0400204VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000205{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500206}
207
Geoff Langd8605522016-04-13 10:19:12 -0400208VariableLocation::VariableLocation(const std::string &name,
209 unsigned int element,
210 unsigned int index)
211 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500212{
213}
214
Geoff Langd8605522016-04-13 10:19:12 -0400215void Program::Bindings::bindLocation(GLuint index, const std::string &name)
216{
217 mBindings[name] = index;
218}
219
220int Program::Bindings::getBinding(const std::string &name) const
221{
222 auto iter = mBindings.find(name);
223 return (iter != mBindings.end()) ? iter->second : -1;
224}
225
226Program::Bindings::const_iterator Program::Bindings::begin() const
227{
228 return mBindings.begin();
229}
230
231Program::Bindings::const_iterator Program::Bindings::end() const
232{
233 return mBindings.end();
234}
235
Jamie Madill48ef11b2016-04-27 15:21:52 -0400236ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500237 : mLabel(),
238 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400239 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300240 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500241 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
242 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400243{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300244 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400245}
246
Jamie Madill48ef11b2016-04-27 15:21:52 -0400247ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400248{
249 if (mAttachedVertexShader != nullptr)
250 {
251 mAttachedVertexShader->release();
252 }
253
254 if (mAttachedFragmentShader != nullptr)
255 {
256 mAttachedFragmentShader->release();
257 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300258
259 if (mAttachedComputeShader != nullptr)
260 {
261 mAttachedComputeShader->release();
262 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400263}
264
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500266{
267 return mLabel;
268}
269
Jamie Madill48ef11b2016-04-27 15:21:52 -0400270const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400271{
272 for (const LinkedUniform &linkedUniform : mUniforms)
273 {
274 if (linkedUniform.name == name)
275 {
276 return &linkedUniform;
277 }
278 }
279
280 return nullptr;
281}
282
Jamie Madill48ef11b2016-04-27 15:21:52 -0400283GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400284{
285 size_t subscript = GL_INVALID_INDEX;
286 std::string baseName = gl::ParseUniformName(name, &subscript);
287
288 for (size_t location = 0; location < mUniformLocations.size(); ++location)
289 {
290 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400291 if (!uniformLocation.used)
292 {
293 continue;
294 }
295
296 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400297
298 if (uniform.name == baseName)
299 {
Geoff Langd8605522016-04-13 10:19:12 -0400300 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400301 {
Geoff Langd8605522016-04-13 10:19:12 -0400302 if (uniformLocation.element == subscript ||
303 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
304 {
305 return static_cast<GLint>(location);
306 }
307 }
308 else
309 {
310 if (subscript == GL_INVALID_INDEX)
311 {
312 return static_cast<GLint>(location);
313 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400314 }
315 }
316 }
317
318 return -1;
319}
320
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321GLuint ProgramState::getUniformIndex(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400322{
323 size_t subscript = GL_INVALID_INDEX;
324 std::string baseName = gl::ParseUniformName(name, &subscript);
325
326 // The app is not allowed to specify array indices other than 0 for arrays of basic types
327 if (subscript != 0 && subscript != GL_INVALID_INDEX)
328 {
329 return GL_INVALID_INDEX;
330 }
331
332 for (size_t index = 0; index < mUniforms.size(); index++)
333 {
334 const LinkedUniform &uniform = mUniforms[index];
335 if (uniform.name == baseName)
336 {
337 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
338 {
339 return static_cast<GLuint>(index);
340 }
341 }
342 }
343
344 return GL_INVALID_INDEX;
345}
346
Jamie Madill7aea7e02016-05-10 10:39:45 -0400347Program::Program(rx::GLImplFactory *factory, ResourceManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400348 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500350 mLinked(false),
351 mDeleteStatus(false),
352 mRefCount(0),
353 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400354 mHandle(handle),
355 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500356{
357 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000358
359 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500360 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361}
362
363Program::~Program()
364{
365 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000366
Geoff Lang7dd2e102014-11-10 15:19:26 -0500367 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
Geoff Lang70d0f492015-12-10 17:45:46 -0500370void Program::setLabel(const std::string &label)
371{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400372 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500373}
374
375const std::string &Program::getLabel() const
376{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400377 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500378}
379
Jamie Madillef300b12016-10-07 15:12:09 -0400380void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300382 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300384 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Jamie Madillef300b12016-10-07 15:12:09 -0400386 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300387 mState.mAttachedVertexShader = shader;
388 mState.mAttachedVertexShader->addRef();
389 break;
390 }
391 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392 {
Jamie Madillef300b12016-10-07 15:12:09 -0400393 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300394 mState.mAttachedFragmentShader = shader;
395 mState.mAttachedFragmentShader->addRef();
396 break;
397 }
398 case GL_COMPUTE_SHADER:
399 {
Jamie Madillef300b12016-10-07 15:12:09 -0400400 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300401 mState.mAttachedComputeShader = shader;
402 mState.mAttachedComputeShader->addRef();
403 break;
404 }
405 default:
406 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408}
409
410bool Program::detachShader(Shader *shader)
411{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300412 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300414 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300416 if (mState.mAttachedVertexShader != shader)
417 {
418 return false;
419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
Martin Radev4c4c8e72016-08-04 12:25:34 +0300421 shader->release();
422 mState.mAttachedVertexShader = nullptr;
423 break;
424 }
425 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 if (mState.mAttachedFragmentShader != shader)
428 {
429 return false;
430 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 shader->release();
433 mState.mAttachedFragmentShader = nullptr;
434 break;
435 }
436 case GL_COMPUTE_SHADER:
437 {
438 if (mState.mAttachedComputeShader != shader)
439 {
440 return false;
441 }
442
443 shader->release();
444 mState.mAttachedComputeShader = nullptr;
445 break;
446 }
447 default:
448 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451 return true;
452}
453
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000454int Program::getAttachedShadersCount() const
455{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300456 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
457 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000458}
459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460void Program::bindAttributeLocation(GLuint index, const char *name)
461{
Geoff Langd8605522016-04-13 10:19:12 -0400462 mAttributeBindings.bindLocation(index, name);
463}
464
465void Program::bindUniformLocation(GLuint index, const char *name)
466{
467 // Bind the base uniform name only since array indices other than 0 cannot be bound
468 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469}
470
Sami Väisänen46eaa942016-06-29 10:26:37 +0300471void Program::bindFragmentInputLocation(GLint index, const char *name)
472{
473 mFragmentInputBindings.bindLocation(index, name);
474}
475
476BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
477{
478 BindingInfo ret;
479 ret.type = GL_NONE;
480 ret.valid = false;
481
482 const Shader *fragmentShader = mState.getAttachedFragmentShader();
483 ASSERT(fragmentShader);
484
485 // Find the actual fragment shader varying we're interested in
486 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
487
488 for (const auto &binding : mFragmentInputBindings)
489 {
490 if (binding.second != static_cast<GLuint>(index))
491 continue;
492
493 ret.valid = true;
494
495 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400496 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300497
498 for (const auto &in : inputs)
499 {
500 if (in.name == originalName)
501 {
502 if (in.isArray())
503 {
504 // The client wants to bind either "name" or "name[0]".
505 // GL ES 3.1 spec refers to active array names with language such as:
506 // "if the string identifies the base name of an active array, where the
507 // string would exactly match the name of the variable if the suffix "[0]"
508 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400509 if (arrayIndex == GL_INVALID_INDEX)
510 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300511
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400512 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300513 }
514 else
515 {
516 ret.name = in.mappedName;
517 }
518 ret.type = in.type;
519 return ret;
520 }
521 }
522 }
523
524 return ret;
525}
526
527void Program::pathFragmentInputGen(GLint index,
528 GLenum genMode,
529 GLint components,
530 const GLfloat *coeffs)
531{
532 // If the location is -1 then the command is silently ignored
533 if (index == -1)
534 return;
535
536 const auto &binding = getFragmentInputBindingInfo(index);
537
538 // If the input doesn't exist then then the command is silently ignored
539 // This could happen through optimization for example, the shader translator
540 // decides that a variable is not actually being used and optimizes it away.
541 if (binding.name.empty())
542 return;
543
544 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
545}
546
Martin Radev4c4c8e72016-08-04 12:25:34 +0300547// The attached shaders are checked for linking errors by matching up their variables.
548// Uniform, input and output variables get collected.
549// The code gets compiled into binaries.
Jamie Madill9082b982016-04-27 15:21:51 -0400550Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000551{
552 unlink(false);
553
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000554 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000555 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000556
Martin Radev4c4c8e72016-08-04 12:25:34 +0300557 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558
Martin Radev4c4c8e72016-08-04 12:25:34 +0300559 bool isComputeShaderAttached = (mState.mAttachedComputeShader != nullptr);
560 bool nonComputeShadersAttached =
561 (mState.mAttachedVertexShader != nullptr || mState.mAttachedFragmentShader != nullptr);
562 // Check whether we both have a compute and non-compute shaders attached.
563 // If there are of both types attached, then linking should fail.
564 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
565 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500566 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300567 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
568 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400569 }
570
Martin Radev4c4c8e72016-08-04 12:25:34 +0300571 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500572 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300573 if (!mState.mAttachedComputeShader->isCompiled())
574 {
575 mInfoLog << "Attached compute shader is not compiled.";
576 return NoError();
577 }
578 ASSERT(mState.mAttachedComputeShader->getType() == GL_COMPUTE_SHADER);
579
580 mState.mComputeShaderLocalSize = mState.mAttachedComputeShader->getWorkGroupSize();
581
582 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
583 // If the work group size is not specified, a link time error should occur.
584 if (!mState.mComputeShaderLocalSize.isDeclared())
585 {
586 mInfoLog << "Work group size is not specified.";
587 return NoError();
588 }
589
590 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
591 {
592 return NoError();
593 }
594
595 if (!linkUniformBlocks(mInfoLog, caps))
596 {
597 return NoError();
598 }
599
600 rx::LinkResult result = mProgram->link(data, mInfoLog);
601
602 if (result.error.isError() || !result.linkSuccess)
603 {
604 return result.error;
605 }
606 }
607 else
608 {
609 if (!mState.mAttachedFragmentShader || !mState.mAttachedFragmentShader->isCompiled())
610 {
611 return NoError();
612 }
613 ASSERT(mState.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
614
615 if (!mState.mAttachedVertexShader || !mState.mAttachedVertexShader->isCompiled())
616 {
617 return NoError();
618 }
619 ASSERT(mState.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
620
621 if (mState.mAttachedFragmentShader->getShaderVersion() !=
622 mState.mAttachedVertexShader->getShaderVersion())
623 {
624 mInfoLog << "Fragment shader version does not match vertex shader version.";
625 return NoError();
626 }
627
628 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mState.mAttachedVertexShader))
629 {
630 return NoError();
631 }
632
633 if (!linkVaryings(mInfoLog, mState.mAttachedVertexShader, mState.mAttachedFragmentShader))
634 {
635 return NoError();
636 }
637
638 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
639 {
640 return NoError();
641 }
642
643 if (!linkUniformBlocks(mInfoLog, caps))
644 {
645 return NoError();
646 }
647
648 const auto &mergedVaryings = getMergedVaryings();
649
650 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
651 {
652 return NoError();
653 }
654
655 linkOutputVariables();
656
657 rx::LinkResult result = mProgram->link(data, mInfoLog);
658 if (result.error.isError() || !result.linkSuccess)
659 {
660 return result.error;
661 }
662
663 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500664 }
665
Jamie Madill4a3c2342015-10-08 12:58:45 -0400666 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400667
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668 mLinked = true;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300669 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000670}
671
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000672// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673void Program::unlink(bool destroy)
674{
675 if (destroy) // Object being destructed
676 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400677 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400679 mState.mAttachedFragmentShader->release();
680 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681 }
682
Jamie Madill48ef11b2016-04-27 15:21:52 -0400683 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400685 mState.mAttachedVertexShader->release();
686 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300688
689 if (mState.mAttachedComputeShader)
690 {
691 mState.mAttachedComputeShader->release();
692 mState.mAttachedComputeShader = nullptr;
693 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694 }
695
Jamie Madill48ef11b2016-04-27 15:21:52 -0400696 mState.mAttributes.clear();
697 mState.mActiveAttribLocationsMask.reset();
698 mState.mTransformFeedbackVaryingVars.clear();
699 mState.mUniforms.clear();
700 mState.mUniformLocations.clear();
701 mState.mUniformBlocks.clear();
702 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 mState.mComputeShaderLocalSize.fill(1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500704
Geoff Lang7dd2e102014-11-10 15:19:26 -0500705 mValidated = false;
706
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000707 mLinked = false;
708}
709
Geoff Lange1a27752015-10-05 13:16:04 -0400710bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000711{
712 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713}
714
Geoff Lang7dd2e102014-11-10 15:19:26 -0500715Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000716{
717 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000718
Geoff Lang7dd2e102014-11-10 15:19:26 -0500719#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
720 return Error(GL_NO_ERROR);
721#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400722 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
723 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000724 {
Jamie Madillf6113162015-05-07 11:49:21 -0400725 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500726 return Error(GL_NO_ERROR);
727 }
728
Geoff Langc46cc2f2015-10-01 17:16:20 -0400729 BinaryInputStream stream(binary, length);
730
Geoff Lang7dd2e102014-11-10 15:19:26 -0500731 int majorVersion = stream.readInt<int>();
732 int minorVersion = stream.readInt<int>();
733 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
734 {
Jamie Madillf6113162015-05-07 11:49:21 -0400735 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500736 return Error(GL_NO_ERROR);
737 }
738
739 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
740 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
741 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
742 {
Jamie Madillf6113162015-05-07 11:49:21 -0400743 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500744 return Error(GL_NO_ERROR);
745 }
746
Martin Radev4c4c8e72016-08-04 12:25:34 +0300747 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
748 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
749 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
750
Jamie Madill63805b42015-08-25 13:17:39 -0400751 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
752 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400753 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500754
Jamie Madill3da79b72015-04-27 11:09:17 -0400755 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400756 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400757 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
758 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400759 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400760 LoadShaderVar(&stream, &attrib);
761 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400762 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400763 }
764
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400766 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400767 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
768 {
769 LinkedUniform uniform;
770 LoadShaderVar(&stream, &uniform);
771
772 uniform.blockIndex = stream.readInt<int>();
773 uniform.blockInfo.offset = stream.readInt<int>();
774 uniform.blockInfo.arrayStride = stream.readInt<int>();
775 uniform.blockInfo.matrixStride = stream.readInt<int>();
776 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
777
Jamie Madill48ef11b2016-04-27 15:21:52 -0400778 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400779 }
780
781 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400782 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400783 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
784 uniformIndexIndex++)
785 {
786 VariableLocation variable;
787 stream.readString(&variable.name);
788 stream.readInt(&variable.element);
789 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400790 stream.readBool(&variable.used);
791 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792
Jamie Madill48ef11b2016-04-27 15:21:52 -0400793 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400794 }
795
796 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400797 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400798 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
799 ++uniformBlockIndex)
800 {
801 UniformBlock uniformBlock;
802 stream.readString(&uniformBlock.name);
803 stream.readBool(&uniformBlock.isArray);
804 stream.readInt(&uniformBlock.arrayElement);
805 stream.readInt(&uniformBlock.dataSize);
806 stream.readBool(&uniformBlock.vertexStaticUse);
807 stream.readBool(&uniformBlock.fragmentStaticUse);
808
809 unsigned int numMembers = stream.readInt<unsigned int>();
810 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
811 {
812 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
813 }
814
Jamie Madill48ef11b2016-04-27 15:21:52 -0400815 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 }
817
Brandon Jones1048ea72015-10-06 15:34:52 -0700818 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400819 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700820 for (unsigned int transformFeedbackVaryingIndex = 0;
821 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
822 ++transformFeedbackVaryingIndex)
823 {
824 sh::Varying varying;
825 stream.readInt(&varying.arraySize);
826 stream.readInt(&varying.type);
827 stream.readString(&varying.name);
828
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700830 }
831
Jamie Madill48ef11b2016-04-27 15:21:52 -0400832 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400833
Jamie Madill80a6fc02015-08-21 16:53:16 -0400834 unsigned int outputVarCount = stream.readInt<unsigned int>();
835 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
836 {
837 int locationIndex = stream.readInt<int>();
838 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400839 stream.readInt(&locationData.element);
840 stream.readInt(&locationData.index);
841 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400843 }
844
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400845 stream.readInt(&mSamplerUniformRange.start);
846 stream.readInt(&mSamplerUniformRange.end);
847
Geoff Lang7dd2e102014-11-10 15:19:26 -0500848 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
849 if (result.error.isError() || !result.linkSuccess)
850 {
Geoff Langb543aff2014-09-30 14:52:54 -0400851 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000852 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000853
Geoff Lang7dd2e102014-11-10 15:19:26 -0500854 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400855 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500856#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
857}
858
859Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
860{
861 if (binaryFormat)
862 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400863 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500864 }
865
866 BinaryOutputStream stream;
867
Geoff Lang7dd2e102014-11-10 15:19:26 -0500868 stream.writeInt(ANGLE_MAJOR_VERSION);
869 stream.writeInt(ANGLE_MINOR_VERSION);
870 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
871
Martin Radev4c4c8e72016-08-04 12:25:34 +0300872 stream.writeInt(mState.mComputeShaderLocalSize[0]);
873 stream.writeInt(mState.mComputeShaderLocalSize[1]);
874 stream.writeInt(mState.mComputeShaderLocalSize[2]);
875
Jamie Madill48ef11b2016-04-27 15:21:52 -0400876 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500877
Jamie Madill48ef11b2016-04-27 15:21:52 -0400878 stream.writeInt(mState.mAttributes.size());
879 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400880 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400881 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400882 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400883 }
884
Jamie Madill48ef11b2016-04-27 15:21:52 -0400885 stream.writeInt(mState.mUniforms.size());
886 for (const gl::LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400887 {
888 WriteShaderVar(&stream, uniform);
889
890 // FIXME: referenced
891
892 stream.writeInt(uniform.blockIndex);
893 stream.writeInt(uniform.blockInfo.offset);
894 stream.writeInt(uniform.blockInfo.arrayStride);
895 stream.writeInt(uniform.blockInfo.matrixStride);
896 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
897 }
898
Jamie Madill48ef11b2016-04-27 15:21:52 -0400899 stream.writeInt(mState.mUniformLocations.size());
900 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400901 {
902 stream.writeString(variable.name);
903 stream.writeInt(variable.element);
904 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400905 stream.writeInt(variable.used);
906 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400907 }
908
Jamie Madill48ef11b2016-04-27 15:21:52 -0400909 stream.writeInt(mState.mUniformBlocks.size());
910 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400911 {
912 stream.writeString(uniformBlock.name);
913 stream.writeInt(uniformBlock.isArray);
914 stream.writeInt(uniformBlock.arrayElement);
915 stream.writeInt(uniformBlock.dataSize);
916
917 stream.writeInt(uniformBlock.vertexStaticUse);
918 stream.writeInt(uniformBlock.fragmentStaticUse);
919
920 stream.writeInt(uniformBlock.memberUniformIndexes.size());
921 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
922 {
923 stream.writeInt(memberUniformIndex);
924 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400925 }
926
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
928 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700929 {
930 stream.writeInt(varying.arraySize);
931 stream.writeInt(varying.type);
932 stream.writeString(varying.name);
933 }
934
Jamie Madill48ef11b2016-04-27 15:21:52 -0400935 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400936
Jamie Madill48ef11b2016-04-27 15:21:52 -0400937 stream.writeInt(mState.mOutputVariables.size());
938 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400939 {
940 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400941 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400942 stream.writeInt(outputPair.second.index);
943 stream.writeString(outputPair.second.name);
944 }
945
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400946 stream.writeInt(mSamplerUniformRange.start);
947 stream.writeInt(mSamplerUniformRange.end);
948
Geoff Lang7dd2e102014-11-10 15:19:26 -0500949 gl::Error error = mProgram->save(&stream);
950 if (error.isError())
951 {
952 return error;
953 }
954
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700955 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400956 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500957
958 if (streamLength > bufSize)
959 {
960 if (length)
961 {
962 *length = 0;
963 }
964
965 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
966 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
967 // sizes and then copy it.
968 return Error(GL_INVALID_OPERATION);
969 }
970
971 if (binary)
972 {
973 char *ptr = reinterpret_cast<char*>(binary);
974
Jamie Madill48ef11b2016-04-27 15:21:52 -0400975 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500976 ptr += streamLength;
977
978 ASSERT(ptr - streamLength == binary);
979 }
980
981 if (length)
982 {
983 *length = streamLength;
984 }
985
986 return Error(GL_NO_ERROR);
987}
988
989GLint Program::getBinaryLength() const
990{
991 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400992 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500993 if (error.isError())
994 {
995 return 0;
996 }
997
998 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000999}
1000
Geoff Langc5629752015-12-07 16:29:04 -05001001void Program::setBinaryRetrievableHint(bool retrievable)
1002{
1003 // TODO(jmadill) : replace with dirty bits
1004 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001005 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001006}
1007
1008bool Program::getBinaryRetrievableHint() const
1009{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001010 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001011}
1012
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013void Program::release()
1014{
1015 mRefCount--;
1016
1017 if (mRefCount == 0 && mDeleteStatus)
1018 {
1019 mResourceManager->deleteProgram(mHandle);
1020 }
1021}
1022
1023void Program::addRef()
1024{
1025 mRefCount++;
1026}
1027
1028unsigned int Program::getRefCount() const
1029{
1030 return mRefCount;
1031}
1032
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001033int Program::getInfoLogLength() const
1034{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001035 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001036}
1037
Geoff Lange1a27752015-10-05 13:16:04 -04001038void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001039{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001040 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001041}
1042
Geoff Lange1a27752015-10-05 13:16:04 -04001043void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001044{
1045 int total = 0;
1046
Martin Radev4c4c8e72016-08-04 12:25:34 +03001047 if (mState.mAttachedComputeShader)
1048 {
1049 if (total < maxCount)
1050 {
1051 shaders[total] = mState.mAttachedComputeShader->getHandle();
1052 total++;
1053 }
1054 }
1055
Jamie Madill48ef11b2016-04-27 15:21:52 -04001056 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001057 {
1058 if (total < maxCount)
1059 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001060 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001061 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001062 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001063 }
1064
Jamie Madill48ef11b2016-04-27 15:21:52 -04001065 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001066 {
1067 if (total < maxCount)
1068 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001069 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001070 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001071 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001072 }
1073
1074 if (count)
1075 {
1076 *count = total;
1077 }
1078}
1079
Geoff Lange1a27752015-10-05 13:16:04 -04001080GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001082 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001084 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001086 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087 }
1088 }
1089
Austin Kinrossb8af7232015-03-16 22:33:25 -07001090 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001091}
1092
Jamie Madill63805b42015-08-25 13:17:39 -04001093bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001094{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001095 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1096 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097}
1098
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001099void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1100{
Jamie Madillc349ec02015-08-21 16:53:12 -04001101 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001102 {
1103 if (bufsize > 0)
1104 {
1105 name[0] = '\0';
1106 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001108 if (length)
1109 {
1110 *length = 0;
1111 }
1112
1113 *type = GL_NONE;
1114 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001115 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001117
1118 size_t attributeIndex = 0;
1119
Jamie Madill48ef11b2016-04-27 15:21:52 -04001120 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001121 {
1122 // Skip over inactive attributes
1123 if (attribute.staticUse)
1124 {
1125 if (static_cast<size_t>(index) == attributeIndex)
1126 {
1127 break;
1128 }
1129 attributeIndex++;
1130 }
1131 }
1132
Jamie Madill48ef11b2016-04-27 15:21:52 -04001133 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1134 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001135
1136 if (bufsize > 0)
1137 {
1138 const char *string = attrib.name.c_str();
1139
1140 strncpy(name, string, bufsize);
1141 name[bufsize - 1] = '\0';
1142
1143 if (length)
1144 {
1145 *length = static_cast<GLsizei>(strlen(name));
1146 }
1147 }
1148
1149 // Always a single 'type' instance
1150 *size = 1;
1151 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001152}
1153
Geoff Lange1a27752015-10-05 13:16:04 -04001154GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001155{
Jamie Madillc349ec02015-08-21 16:53:12 -04001156 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001157 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001158 return 0;
1159 }
1160
1161 GLint count = 0;
1162
Jamie Madill48ef11b2016-04-27 15:21:52 -04001163 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001164 {
1165 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001166 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001167
1168 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001169}
1170
Geoff Lange1a27752015-10-05 13:16:04 -04001171GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001172{
Jamie Madillc349ec02015-08-21 16:53:12 -04001173 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001174 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001175 return 0;
1176 }
1177
1178 size_t maxLength = 0;
1179
Jamie Madill48ef11b2016-04-27 15:21:52 -04001180 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001181 {
1182 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001183 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001184 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001185 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001186 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187
Jamie Madillc349ec02015-08-21 16:53:12 -04001188 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189}
1190
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191GLint Program::getFragDataLocation(const std::string &name) const
1192{
1193 std::string baseName(name);
1194 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001195 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001196 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001197 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1199 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001200 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001202 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001204}
1205
Geoff Lange1a27752015-10-05 13:16:04 -04001206void Program::getActiveUniform(GLuint index,
1207 GLsizei bufsize,
1208 GLsizei *length,
1209 GLint *size,
1210 GLenum *type,
1211 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001212{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001213 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001214 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001215 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001216 ASSERT(index < mState.mUniforms.size());
1217 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001218
1219 if (bufsize > 0)
1220 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001221 std::string string = uniform.name;
1222 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 {
1224 string += "[0]";
1225 }
1226
1227 strncpy(name, string.c_str(), bufsize);
1228 name[bufsize - 1] = '\0';
1229
1230 if (length)
1231 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001232 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233 }
1234 }
1235
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236 *size = uniform.elementCount();
1237 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001238 }
1239 else
1240 {
1241 if (bufsize > 0)
1242 {
1243 name[0] = '\0';
1244 }
1245
1246 if (length)
1247 {
1248 *length = 0;
1249 }
1250
1251 *size = 0;
1252 *type = GL_NONE;
1253 }
1254}
1255
Geoff Lange1a27752015-10-05 13:16:04 -04001256GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001257{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001259 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001260 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001261 }
1262 else
1263 {
1264 return 0;
1265 }
1266}
1267
Geoff Lange1a27752015-10-05 13:16:04 -04001268GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001269{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271
1272 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001273 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001274 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001275 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001276 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001278 size_t length = uniform.name.length() + 1u;
1279 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280 {
1281 length += 3; // Counting in "[0]".
1282 }
1283 maxLength = std::max(length, maxLength);
1284 }
1285 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001286 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287
Jamie Madill62d31cb2015-09-11 13:25:51 -04001288 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289}
1290
1291GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1292{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001293 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
1294 const gl::LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001296 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001297 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1298 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1299 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1300 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1301 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1302 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1303 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1304 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1305 default:
1306 UNREACHABLE();
1307 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001308 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309 return 0;
1310}
1311
1312bool Program::isValidUniformLocation(GLint location) const
1313{
Jamie Madille2e406c2016-06-02 13:04:10 -04001314 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001315 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1316 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001317}
1318
1319bool Program::isIgnoredUniformLocation(GLint location) const
1320{
1321 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1322 // out
1323 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001324 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1325 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001326}
1327
Jamie Madill62d31cb2015-09-11 13:25:51 -04001328const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001330 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1331 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001332}
1333
Jamie Madill62d31cb2015-09-11 13:25:51 -04001334GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001336 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337}
1338
Jamie Madill62d31cb2015-09-11 13:25:51 -04001339GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001341 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001342}
1343
1344void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1345{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001346 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347 mProgram->setUniform1fv(location, count, v);
1348}
1349
1350void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1351{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001352 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353 mProgram->setUniform2fv(location, count, v);
1354}
1355
1356void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1357{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001358 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 mProgram->setUniform3fv(location, count, v);
1360}
1361
1362void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1363{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001364 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365 mProgram->setUniform4fv(location, count, v);
1366}
1367
1368void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1369{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001370 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371 mProgram->setUniform1iv(location, count, v);
1372}
1373
1374void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1375{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001376 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377 mProgram->setUniform2iv(location, count, v);
1378}
1379
1380void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1381{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001382 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383 mProgram->setUniform3iv(location, count, v);
1384}
1385
1386void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1387{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001388 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389 mProgram->setUniform4iv(location, count, v);
1390}
1391
1392void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1393{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001394 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395 mProgram->setUniform1uiv(location, count, v);
1396}
1397
1398void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1399{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001400 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401 mProgram->setUniform2uiv(location, count, v);
1402}
1403
1404void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1405{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001406 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407 mProgram->setUniform3uiv(location, count, v);
1408}
1409
1410void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1411{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001412 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 mProgram->setUniform4uiv(location, count, v);
1414}
1415
1416void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1417{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001418 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1420}
1421
1422void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1423{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001424 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1426}
1427
1428void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1429{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001430 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1432}
1433
1434void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1435{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001436 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1438}
1439
1440void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1441{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001442 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1444}
1445
1446void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1447{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001448 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1450}
1451
1452void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1453{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001454 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1456}
1457
1458void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1459{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001460 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1462}
1463
1464void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1465{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001466 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1468}
1469
Geoff Lange1a27752015-10-05 13:16:04 -04001470void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001472 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001473}
1474
Geoff Lange1a27752015-10-05 13:16:04 -04001475void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478}
1479
Geoff Lange1a27752015-10-05 13:16:04 -04001480void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001482 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483}
1484
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485void Program::flagForDeletion()
1486{
1487 mDeleteStatus = true;
1488}
1489
1490bool Program::isFlaggedForDeletion() const
1491{
1492 return mDeleteStatus;
1493}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001494
Brandon Jones43a53e22014-08-28 16:23:22 -07001495void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001496{
1497 mInfoLog.reset();
1498
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001500 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001501 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001502 }
1503 else
1504 {
Jamie Madillf6113162015-05-07 11:49:21 -04001505 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001506 }
1507}
1508
Geoff Lang7dd2e102014-11-10 15:19:26 -05001509bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1510{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001511 // Skip cache if we're using an infolog, so we get the full error.
1512 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1513 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1514 {
1515 return mCachedValidateSamplersResult.value();
1516 }
1517
1518 if (mTextureUnitTypesCache.empty())
1519 {
1520 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1521 }
1522 else
1523 {
1524 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1525 }
1526
1527 // if any two active samplers in a program are of different types, but refer to the same
1528 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1529 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1530 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1531 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1532 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001533 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001534 ASSERT(uniform.isSampler());
1535
1536 if (!uniform.staticUse)
1537 continue;
1538
1539 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1540 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1541
1542 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1543 {
1544 GLuint textureUnit = dataPtr[arrayElement];
1545
1546 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1547 {
1548 if (infoLog)
1549 {
1550 (*infoLog) << "Sampler uniform (" << textureUnit
1551 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1552 << caps.maxCombinedTextureImageUnits << ")";
1553 }
1554
1555 mCachedValidateSamplersResult = false;
1556 return false;
1557 }
1558
1559 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1560 {
1561 if (textureType != mTextureUnitTypesCache[textureUnit])
1562 {
1563 if (infoLog)
1564 {
1565 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1566 "image unit ("
1567 << textureUnit << ").";
1568 }
1569
1570 mCachedValidateSamplersResult = false;
1571 return false;
1572 }
1573 }
1574 else
1575 {
1576 mTextureUnitTypesCache[textureUnit] = textureType;
1577 }
1578 }
1579 }
1580
1581 mCachedValidateSamplersResult = true;
1582 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583}
1584
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001585bool Program::isValidated() const
1586{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587 return mValidated;
1588}
1589
Geoff Lange1a27752015-10-05 13:16:04 -04001590GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001591{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001592 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593}
1594
1595void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1596{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001597 ASSERT(
1598 uniformBlockIndex <
1599 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001600
Jamie Madill48ef11b2016-04-27 15:21:52 -04001601 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001602
1603 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001604 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001605 std::string string = uniformBlock.name;
1606
Jamie Madill62d31cb2015-09-11 13:25:51 -04001607 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001608 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001609 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001610 }
1611
1612 strncpy(uniformBlockName, string.c_str(), bufSize);
1613 uniformBlockName[bufSize - 1] = '\0';
1614
1615 if (length)
1616 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001617 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001619 }
1620}
1621
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001623{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001624 ASSERT(
1625 uniformBlockIndex <
1626 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627
Jamie Madill48ef11b2016-04-27 15:21:52 -04001628 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629
1630 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001631 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 case GL_UNIFORM_BLOCK_DATA_SIZE:
1633 *params = static_cast<GLint>(uniformBlock.dataSize);
1634 break;
1635 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 *params =
1637 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638 break;
1639 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1640 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1641 break;
1642 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1643 {
1644 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1645 {
1646 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1647 }
1648 }
1649 break;
1650 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001651 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652 break;
1653 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001654 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655 break;
1656 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001657 }
1658}
1659
Geoff Lange1a27752015-10-05 13:16:04 -04001660GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001661{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662 int maxLength = 0;
1663
1664 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001665 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001666 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1668 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001669 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670 if (!uniformBlock.name.empty())
1671 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001672 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673
1674 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001675 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676
1677 maxLength = std::max(length + arrayLength, maxLength);
1678 }
1679 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001680 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681
1682 return maxLength;
1683}
1684
Geoff Lange1a27752015-10-05 13:16:04 -04001685GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001687 size_t subscript = GL_INVALID_INDEX;
1688 std::string baseName = gl::ParseUniformName(name, &subscript);
1689
Jamie Madill48ef11b2016-04-27 15:21:52 -04001690 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001691 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1692 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001693 const gl::UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001694 if (uniformBlock.name == baseName)
1695 {
1696 const bool arrayElementZero =
1697 (subscript == GL_INVALID_INDEX &&
1698 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1699 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1700 {
1701 return blockIndex;
1702 }
1703 }
1704 }
1705
1706 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001707}
1708
Jamie Madill62d31cb2015-09-11 13:25:51 -04001709const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001710{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001711 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1712 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001713}
1714
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001715void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1716{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001717 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001718 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001719}
1720
1721GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1722{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001723 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001724}
1725
1726void Program::resetUniformBlockBindings()
1727{
1728 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1729 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001730 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001731 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001732 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001733}
1734
Geoff Lang48dcae72014-02-05 16:28:24 -05001735void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1736{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001737 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001738 for (GLsizei i = 0; i < count; i++)
1739 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001740 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001741 }
1742
Jamie Madill48ef11b2016-04-27 15:21:52 -04001743 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001744}
1745
1746void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1747{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001748 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001749 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001750 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1751 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001752 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1753 if (length)
1754 {
1755 *length = lastNameIdx;
1756 }
1757 if (size)
1758 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001759 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001760 }
1761 if (type)
1762 {
1763 *type = varying.type;
1764 }
1765 if (name)
1766 {
1767 memcpy(name, varying.name.c_str(), lastNameIdx);
1768 name[lastNameIdx] = '\0';
1769 }
1770 }
1771}
1772
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001773GLsizei Program::getTransformFeedbackVaryingCount() const
1774{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001776 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001777 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001778 }
1779 else
1780 {
1781 return 0;
1782 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001783}
1784
1785GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1786{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001788 {
1789 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001790 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001791 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001792 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1793 }
1794
1795 return maxSize;
1796 }
1797 else
1798 {
1799 return 0;
1800 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001801}
1802
1803GLenum Program::getTransformFeedbackBufferMode() const
1804{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001805 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806}
1807
Jamie Madillada9ecc2015-08-17 12:53:37 -04001808bool Program::linkVaryings(InfoLog &infoLog,
1809 const Shader *vertexShader,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001810 const Shader *fragmentShader) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811{
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001812 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1813
Jamie Madill4cff2472015-08-21 16:53:18 -04001814 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1815 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816
Sami Väisänen46eaa942016-06-29 10:26:37 +03001817 std::map<GLuint, std::string> staticFragmentInputLocations;
1818
Jamie Madill4cff2472015-08-21 16:53:18 -04001819 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 bool matched = false;
1822
1823 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001824 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825 {
1826 continue;
1827 }
1828
Jamie Madill4cff2472015-08-21 16:53:18 -04001829 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001831 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001833 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001834 if (!linkValidateVaryings(infoLog, output.name, input, output,
1835 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001836 {
1837 return false;
1838 }
1839
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840 matched = true;
1841 break;
1842 }
1843 }
1844
1845 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001846 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001847 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001848 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849 return false;
1850 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001851
1852 // Check for aliased path rendering input bindings (if any).
1853 // If more than one binding refer statically to the same
1854 // location the link must fail.
1855
1856 if (!output.staticUse)
1857 continue;
1858
1859 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1860 if (inputBinding == -1)
1861 continue;
1862
1863 const auto it = staticFragmentInputLocations.find(inputBinding);
1864 if (it == std::end(staticFragmentInputLocations))
1865 {
1866 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1867 }
1868 else
1869 {
1870 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1871 << it->second;
1872 return false;
1873 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 }
1875
Jamie Madillada9ecc2015-08-17 12:53:37 -04001876 // TODO(jmadill): verify no unmatched vertex varyings?
1877
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 return true;
1879}
1880
Martin Radev4c4c8e72016-08-04 12:25:34 +03001881bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001882{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001883 // Check that uniforms defined in the vertex and fragment shaders are identical
1884 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001885 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1886 const std::vector<sh::Uniform> &fragmentUniforms =
1887 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001888
Jamie Madillea918db2015-08-18 14:48:59 -04001889 for (const sh::Uniform &vertexUniform : vertexUniforms)
1890 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001891 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001892 }
1893
1894 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1895 {
1896 auto entry = linkedUniforms.find(fragmentUniform.name);
1897 if (entry != linkedUniforms.end())
1898 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001899 LinkedUniform *vertexUniform = &entry->second;
1900 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1901 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001902 {
1903 return false;
1904 }
1905 }
1906 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001907 return true;
1908}
1909
1910bool Program::linkUniforms(gl::InfoLog &infoLog,
1911 const gl::Caps &caps,
1912 const Bindings &uniformBindings)
1913{
1914 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1915 {
1916 ASSERT(mState.mAttachedComputeShader == nullptr);
1917 if (!validateVertexAndFragmentUniforms(infoLog))
1918 {
1919 return false;
1920 }
1921 }
Jamie Madillea918db2015-08-18 14:48:59 -04001922
Jamie Madill62d31cb2015-09-11 13:25:51 -04001923 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1924 // Also check the maximum uniform vector and sampler counts.
1925 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1926 {
1927 return false;
1928 }
1929
Geoff Langd8605522016-04-13 10:19:12 -04001930 if (!indexUniforms(infoLog, caps, uniformBindings))
1931 {
1932 return false;
1933 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934
Jamie Madillea918db2015-08-18 14:48:59 -04001935 return true;
1936}
1937
Geoff Langd8605522016-04-13 10:19:12 -04001938bool Program::indexUniforms(gl::InfoLog &infoLog,
1939 const gl::Caps &caps,
1940 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001941{
Geoff Langd8605522016-04-13 10:19:12 -04001942 // Uniforms awaiting a location
1943 std::vector<VariableLocation> unboundUniforms;
1944 std::map<GLuint, VariableLocation> boundUniforms;
1945 int maxUniformLocation = -1;
1946
1947 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001948 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001949 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001950 const gl::LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001951
Geoff Langd8605522016-04-13 10:19:12 -04001952 if (uniform.isBuiltIn())
1953 {
1954 continue;
1955 }
1956
1957 int bindingLocation = uniformBindings.getBinding(uniform.name);
1958
1959 // Verify that this location isn't bound twice
1960 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1961 {
1962 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1963 return false;
1964 }
1965
Jamie Madill62d31cb2015-09-11 13:25:51 -04001966 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1967 {
Geoff Langd8605522016-04-13 10:19:12 -04001968 VariableLocation location(uniform.name, arrayIndex,
1969 static_cast<unsigned int>(uniformIndex));
1970
1971 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001972 {
Geoff Langd8605522016-04-13 10:19:12 -04001973 boundUniforms[bindingLocation] = location;
1974 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1975 }
1976 else
1977 {
1978 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001979 }
1980 }
1981 }
Geoff Langd8605522016-04-13 10:19:12 -04001982
1983 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1984 // not be assigned to those locations.
1985 std::set<GLuint> reservedLocations;
1986 for (const auto &binding : uniformBindings)
1987 {
1988 GLuint location = binding.second;
1989 if (boundUniforms.find(location) == boundUniforms.end())
1990 {
1991 reservedLocations.insert(location);
1992 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1993 }
1994 }
1995
1996 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04001997 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04001998 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1999 static_cast<size_t>(maxUniformLocation + 1)));
2000
2001 // Assign bound uniforms
2002 for (const auto &boundUniform : boundUniforms)
2003 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002004 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002005 }
2006
2007 // Assign reserved uniforms
2008 for (const auto &reservedLocation : reservedLocations)
2009 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002010 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002011 }
2012
2013 // Assign unbound uniforms
2014 size_t nextUniformLocation = 0;
2015 for (const auto &unboundUniform : unboundUniforms)
2016 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002017 while (mState.mUniformLocations[nextUniformLocation].used ||
2018 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002019 {
2020 nextUniformLocation++;
2021 }
2022
Jamie Madill48ef11b2016-04-27 15:21:52 -04002023 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
2024 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002025 nextUniformLocation++;
2026 }
2027
2028 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002029}
2030
Martin Radev4c4c8e72016-08-04 12:25:34 +03002031bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2032 const std::string &uniformName,
2033 const sh::InterfaceBlockField &vertexUniform,
2034 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035{
Jamie Madillc4c744222015-11-04 09:39:47 -05002036 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2037 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038 {
2039 return false;
2040 }
2041
2042 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2043 {
Jamie Madillf6113162015-05-07 11:49:21 -04002044 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045 return false;
2046 }
2047
2048 return true;
2049}
2050
2051// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill9082b982016-04-27 15:21:51 -04002052bool Program::linkAttributes(const ContextState &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04002053 InfoLog &infoLog,
Geoff Langd8605522016-04-13 10:19:12 -04002054 const Bindings &attributeBindings,
Jamie Madill3da79b72015-04-27 11:09:17 -04002055 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002058 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002059 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002060
2061 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002062 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002063 {
Jamie Madillf6113162015-05-07 11:49:21 -04002064 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002065 return false;
2066 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002068 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002069
Jamie Madillc349ec02015-08-21 16:53:12 -04002070 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002071 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002072 {
2073 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 ASSERT(attribute.staticUse);
2075
Geoff Langd8605522016-04-13 10:19:12 -04002076 int bindingLocation = attributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002077 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002078 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002079 attribute.location = bindingLocation;
2080 }
2081
2082 if (attribute.location != -1)
2083 {
2084 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002085 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086
Jamie Madill63805b42015-08-25 13:17:39 -04002087 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088 {
Jamie Madillf6113162015-05-07 11:49:21 -04002089 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002090 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091
2092 return false;
2093 }
2094
Jamie Madill63805b42015-08-25 13:17:39 -04002095 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 {
Jamie Madill63805b42015-08-25 13:17:39 -04002097 const int regLocation = attribute.location + reg;
2098 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099
2100 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002101 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002102 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002104 // TODO(jmadill): fix aliasing on ES2
2105 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002107 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002108 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109 return false;
2110 }
2111 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002112 else
2113 {
Jamie Madill63805b42015-08-25 13:17:39 -04002114 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002115 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116
Jamie Madill63805b42015-08-25 13:17:39 -04002117 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118 }
2119 }
2120 }
2121
2122 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002123 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002124 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 ASSERT(attribute.staticUse);
2126
Jamie Madillc349ec02015-08-21 16:53:12 -04002127 // Not set by glBindAttribLocation or by location layout qualifier
2128 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129 {
Jamie Madill63805b42015-08-25 13:17:39 -04002130 int regs = VariableRegisterCount(attribute.type);
2131 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132
Jamie Madill63805b42015-08-25 13:17:39 -04002133 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 {
Jamie Madillf6113162015-05-07 11:49:21 -04002135 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002136 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002137 }
2138
Jamie Madillc349ec02015-08-21 16:53:12 -04002139 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002140 }
2141 }
2142
Jamie Madill48ef11b2016-04-27 15:21:52 -04002143 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002144 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002145 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002146 ASSERT(attribute.location != -1);
2147 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002148
Jamie Madill63805b42015-08-25 13:17:39 -04002149 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002150 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002151 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 }
2153 }
2154
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155 return true;
2156}
2157
Martin Radev4c4c8e72016-08-04 12:25:34 +03002158bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2159 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2160 const std::string &errorMessage,
2161 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002162{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002163 GLuint blockCount = 0;
2164 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002165 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002166 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002167 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002168 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002169 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002170 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002171 return false;
2172 }
2173 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002175 return true;
2176}
Jamie Madille473dee2015-08-18 14:49:01 -04002177
Martin Radev4c4c8e72016-08-04 12:25:34 +03002178bool Program::validateVertexAndFragmentInterfaceBlocks(
2179 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2180 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2181 InfoLog &infoLog) const
2182{
2183 // Check that interface blocks defined in the vertex and fragment shaders are identical
2184 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2185 UniformBlockMap linkedUniformBlocks;
2186
2187 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2188 {
2189 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2190 }
2191
Jamie Madille473dee2015-08-18 14:49:01 -04002192 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193 {
Jamie Madille473dee2015-08-18 14:49:01 -04002194 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002195 if (entry != linkedUniformBlocks.end())
2196 {
2197 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2198 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2199 {
2200 return false;
2201 }
2202 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002203 }
2204 return true;
2205}
Jamie Madille473dee2015-08-18 14:49:01 -04002206
Martin Radev4c4c8e72016-08-04 12:25:34 +03002207bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2208{
2209 if (mState.mAttachedComputeShader)
2210 {
2211 const Shader &computeShader = *mState.mAttachedComputeShader;
2212 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2213
2214 if (!validateUniformBlocksCount(
2215 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2216 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2217 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002219 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002220 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002221 return true;
2222 }
2223
2224 const Shader &vertexShader = *mState.mAttachedVertexShader;
2225 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2226
2227 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2228 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2229
2230 if (!validateUniformBlocksCount(
2231 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2232 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2233 {
2234 return false;
2235 }
2236 if (!validateUniformBlocksCount(
2237 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2238 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2239 infoLog))
2240 {
2241
2242 return false;
2243 }
2244 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2245 infoLog))
2246 {
2247 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002248 }
Jamie Madille473dee2015-08-18 14:49:01 -04002249
Geoff Lang7dd2e102014-11-10 15:19:26 -05002250 return true;
2251}
2252
Martin Radev4c4c8e72016-08-04 12:25:34 +03002253bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog,
2254 const sh::InterfaceBlock &vertexInterfaceBlock,
2255 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002256{
2257 const char* blockName = vertexInterfaceBlock.name.c_str();
2258 // validate blocks for the same member types
2259 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2260 {
Jamie Madillf6113162015-05-07 11:49:21 -04002261 infoLog << "Types for interface block '" << blockName
2262 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002263 return false;
2264 }
2265 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2266 {
Jamie Madillf6113162015-05-07 11:49:21 -04002267 infoLog << "Array sizes differ for interface block '" << blockName
2268 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269 return false;
2270 }
2271 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2272 {
Jamie Madillf6113162015-05-07 11:49:21 -04002273 infoLog << "Layout qualifiers differ for interface block '" << blockName
2274 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002275 return false;
2276 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002277 const unsigned int numBlockMembers =
2278 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002279 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2280 {
2281 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2282 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2283 if (vertexMember.name != fragmentMember.name)
2284 {
Jamie Madillf6113162015-05-07 11:49:21 -04002285 infoLog << "Name mismatch for field " << blockMemberIndex
2286 << " of interface block '" << blockName
2287 << "': (in vertex: '" << vertexMember.name
2288 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289 return false;
2290 }
2291 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2292 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2293 {
2294 return false;
2295 }
2296 }
2297 return true;
2298}
2299
2300bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2301 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2302{
2303 if (vertexVariable.type != fragmentVariable.type)
2304 {
Jamie Madillf6113162015-05-07 11:49:21 -04002305 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002306 return false;
2307 }
2308 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2309 {
Jamie Madillf6113162015-05-07 11:49:21 -04002310 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002311 return false;
2312 }
2313 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2314 {
Jamie Madillf6113162015-05-07 11:49:21 -04002315 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002316 return false;
2317 }
2318
2319 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2320 {
Jamie Madillf6113162015-05-07 11:49:21 -04002321 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 return false;
2323 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002324 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2326 {
2327 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2328 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2329
2330 if (vertexMember.name != fragmentMember.name)
2331 {
Jamie Madillf6113162015-05-07 11:49:21 -04002332 infoLog << "Name mismatch for field '" << memberIndex
2333 << "' of " << variableName
2334 << ": (in vertex: '" << vertexMember.name
2335 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002336 return false;
2337 }
2338
2339 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2340 vertexMember.name + "'";
2341
2342 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2343 {
2344 return false;
2345 }
2346 }
2347
2348 return true;
2349}
2350
2351bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2352{
Cooper Partin1acf4382015-06-12 12:38:57 -07002353#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2354 const bool validatePrecision = true;
2355#else
2356 const bool validatePrecision = false;
2357#endif
2358
2359 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002360 {
2361 return false;
2362 }
2363
2364 return true;
2365}
2366
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002367bool Program::linkValidateVaryings(InfoLog &infoLog,
2368 const std::string &varyingName,
2369 const sh::Varying &vertexVarying,
2370 const sh::Varying &fragmentVarying,
2371 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002372{
2373 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2374 {
2375 return false;
2376 }
2377
Jamie Madille9cc4692015-02-19 16:00:13 -05002378 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002380 infoLog << "Interpolation types for " << varyingName
2381 << " differ between vertex and fragment shaders.";
2382 return false;
2383 }
2384
2385 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2386 {
2387 infoLog << "Invariance for " << varyingName
2388 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002389 return false;
2390 }
2391
2392 return true;
2393}
2394
Jamie Madillccdf74b2015-08-18 10:46:12 -04002395bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2396 const std::vector<const sh::Varying *> &varyings,
2397 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002398{
2399 size_t totalComponents = 0;
2400
Jamie Madillccdf74b2015-08-18 10:46:12 -04002401 std::set<std::string> uniqueNames;
2402
Jamie Madill48ef11b2016-04-27 15:21:52 -04002403 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002404 {
2405 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002406 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002407 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002408 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002409 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002410 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002412 infoLog << "Two transform feedback varyings specify the same output variable ("
2413 << tfVaryingName << ").";
2414 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002416 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002417
Geoff Lang1a683462015-09-29 15:09:59 -04002418 if (varying->isArray())
2419 {
2420 infoLog << "Capture of arrays is undefined and not supported.";
2421 return false;
2422 }
2423
Jamie Madillccdf74b2015-08-18 10:46:12 -04002424 // TODO(jmadill): Investigate implementation limits on D3D11
2425 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002426 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002427 componentCount > caps.maxTransformFeedbackSeparateComponents)
2428 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002429 infoLog << "Transform feedback varying's " << varying->name << " components ("
2430 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002431 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002432 return false;
2433 }
2434
2435 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002436 found = true;
2437 break;
2438 }
2439 }
2440
Jamie Madill89bb70e2015-08-31 14:18:39 -04002441 if (tfVaryingName.find('[') != std::string::npos)
2442 {
Geoff Lang1a683462015-09-29 15:09:59 -04002443 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002444 return false;
2445 }
2446
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2448 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002449 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 }
2451
Jamie Madill48ef11b2016-04-27 15:21:52 -04002452 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002453 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002454 {
Jamie Madillf6113162015-05-07 11:49:21 -04002455 infoLog << "Transform feedback varying total components (" << totalComponents
2456 << ") exceed the maximum interleaved components ("
2457 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002458 return false;
2459 }
2460
2461 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002462}
2463
Jamie Madillccdf74b2015-08-18 10:46:12 -04002464void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2465{
2466 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002467 mState.mTransformFeedbackVaryingVars.clear();
2468 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002469 {
2470 for (const sh::Varying *varying : varyings)
2471 {
2472 if (tfVaryingName == varying->name)
2473 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002474 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002475 break;
2476 }
2477 }
2478 }
2479}
2480
2481std::vector<const sh::Varying *> Program::getMergedVaryings() const
2482{
2483 std::set<std::string> uniqueNames;
2484 std::vector<const sh::Varying *> varyings;
2485
Jamie Madill48ef11b2016-04-27 15:21:52 -04002486 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002487 {
2488 if (uniqueNames.count(varying.name) == 0)
2489 {
2490 uniqueNames.insert(varying.name);
2491 varyings.push_back(&varying);
2492 }
2493 }
2494
Jamie Madill48ef11b2016-04-27 15:21:52 -04002495 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002496 {
2497 if (uniqueNames.count(varying.name) == 0)
2498 {
2499 uniqueNames.insert(varying.name);
2500 varyings.push_back(&varying);
2501 }
2502 }
2503
2504 return varyings;
2505}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002506
2507void Program::linkOutputVariables()
2508{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002509 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002510 ASSERT(fragmentShader != nullptr);
2511
2512 // Skip this step for GLES2 shaders.
2513 if (fragmentShader->getShaderVersion() == 100)
2514 return;
2515
Jamie Madilla0a9e122015-09-02 15:54:30 -04002516 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002517
2518 // TODO(jmadill): any caps validation here?
2519
2520 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2521 outputVariableIndex++)
2522 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002523 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002524
2525 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2526 if (outputVariable.isBuiltIn())
2527 continue;
2528
2529 // Since multiple output locations must be specified, use 0 for non-specified locations.
2530 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2531
2532 ASSERT(outputVariable.staticUse);
2533
2534 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2535 elementIndex++)
2536 {
2537 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002538 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002539 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002540 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002541 VariableLocation(outputVariable.name, element, outputVariableIndex);
2542 }
2543 }
2544}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002545
Martin Radev4c4c8e72016-08-04 12:25:34 +03002546bool Program::flattenUniformsAndCheckCapsForShader(const gl::Shader &shader,
2547 GLuint maxUniformComponents,
2548 GLuint maxTextureImageUnits,
2549 const std::string &componentsErrorMessage,
2550 const std::string &samplerErrorMessage,
2551 std::vector<LinkedUniform> &samplerUniforms,
2552 InfoLog &infoLog)
2553{
2554 VectorAndSamplerCount vasCount;
2555 for (const sh::Uniform &uniform : shader.getUniforms())
2556 {
2557 if (uniform.staticUse)
2558 {
2559 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2560 }
2561 }
2562
2563 if (vasCount.vectorCount > maxUniformComponents)
2564 {
2565 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2566 return false;
2567 }
2568
2569 if (vasCount.samplerCount > maxTextureImageUnits)
2570 {
2571 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2572 return false;
2573 }
2574
2575 return true;
2576}
2577
Jamie Madill62d31cb2015-09-11 13:25:51 -04002578bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2579{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002580 std::vector<LinkedUniform> samplerUniforms;
2581
Martin Radev4c4c8e72016-08-04 12:25:34 +03002582 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002583 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002584 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2585
2586 // TODO (mradev): check whether we need finer-grained component counting
2587 if (!flattenUniformsAndCheckCapsForShader(
2588 *computeShader, caps.maxComputeUniformComponents / 4,
2589 caps.maxComputeTextureImageUnits,
2590 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2591 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2592 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002593 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002594 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002595 }
2596 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002597 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002598 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002599 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002600
Martin Radev4c4c8e72016-08-04 12:25:34 +03002601 if (!flattenUniformsAndCheckCapsForShader(
2602 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2603 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2604 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2605 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002606 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002607 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002608 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002609 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002610
Martin Radev4c4c8e72016-08-04 12:25:34 +03002611 if (!flattenUniformsAndCheckCapsForShader(
2612 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2613 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2614 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2615 infoLog))
2616 {
2617 return false;
2618 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002619 }
2620
Jamie Madill48ef11b2016-04-27 15:21:52 -04002621 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002622 mSamplerUniformRange.end =
2623 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2624
Jamie Madill48ef11b2016-04-27 15:21:52 -04002625 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002626
Jamie Madill62d31cb2015-09-11 13:25:51 -04002627 return true;
2628}
2629
2630Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002631 const std::string &fullName,
2632 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002633{
2634 VectorAndSamplerCount vectorAndSamplerCount;
2635
2636 if (uniform.isStruct())
2637 {
2638 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2639 {
2640 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2641
2642 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2643 {
2644 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2645 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2646
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002647 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002648 }
2649 }
2650
2651 return vectorAndSamplerCount;
2652 }
2653
2654 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002655 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002656 if (!UniformInList(mState.getUniforms(), fullName) &&
2657 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002658 {
2659 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2660 uniform.arraySize, -1,
2661 sh::BlockMemberInfo::getDefaultBlockInfo());
2662 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002663
2664 // Store sampler uniforms separately, so we'll append them to the end of the list.
2665 if (isSampler)
2666 {
2667 samplerUniforms->push_back(linkedUniform);
2668 }
2669 else
2670 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002671 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002672 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002673 }
2674
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002675 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002676
2677 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2678 // Likewise, don't count "real" uniforms towards sampler count.
2679 vectorAndSamplerCount.vectorCount =
2680 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002681 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002682
2683 return vectorAndSamplerCount;
2684}
2685
2686void Program::gatherInterfaceBlockInfo()
2687{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002688 ASSERT(mState.mUniformBlocks.empty());
2689
2690 if (mState.mAttachedComputeShader)
2691 {
2692 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2693
2694 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2695 {
2696
2697 // Only 'packed' blocks are allowed to be considered inactive.
2698 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2699 continue;
2700
2701 for (gl::UniformBlock &block : mState.mUniformBlocks)
2702 {
2703 if (block.name == computeBlock.name)
2704 {
2705 block.computeStaticUse = computeBlock.staticUse;
2706 }
2707 }
2708
2709 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2710 }
2711 return;
2712 }
2713
Jamie Madill62d31cb2015-09-11 13:25:51 -04002714 std::set<std::string> visitedList;
2715
Jamie Madill48ef11b2016-04-27 15:21:52 -04002716 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002717
Jamie Madill62d31cb2015-09-11 13:25:51 -04002718 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2719 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002720 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002721 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2722 continue;
2723
2724 if (visitedList.count(vertexBlock.name) > 0)
2725 continue;
2726
2727 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2728 visitedList.insert(vertexBlock.name);
2729 }
2730
Jamie Madill48ef11b2016-04-27 15:21:52 -04002731 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002732
2733 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2734 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002735 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002736 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2737 continue;
2738
2739 if (visitedList.count(fragmentBlock.name) > 0)
2740 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002741 for (gl::UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002742 {
2743 if (block.name == fragmentBlock.name)
2744 {
2745 block.fragmentStaticUse = fragmentBlock.staticUse;
2746 }
2747 }
2748
2749 continue;
2750 }
2751
2752 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2753 visitedList.insert(fragmentBlock.name);
2754 }
2755}
2756
Jamie Madill4a3c2342015-10-08 12:58:45 -04002757template <typename VarT>
2758void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2759 const std::string &prefix,
2760 int blockIndex)
2761{
2762 for (const VarT &field : fields)
2763 {
2764 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2765
2766 if (field.isStruct())
2767 {
2768 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2769 {
2770 const std::string uniformElementName =
2771 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2772 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2773 }
2774 }
2775 else
2776 {
2777 // If getBlockMemberInfo returns false, the uniform is optimized out.
2778 sh::BlockMemberInfo memberInfo;
2779 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2780 {
2781 continue;
2782 }
2783
2784 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2785 blockIndex, memberInfo);
2786
2787 // Since block uniforms have no location, we don't need to store them in the uniform
2788 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002789 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002790 }
2791 }
2792}
2793
Jamie Madill62d31cb2015-09-11 13:25:51 -04002794void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2795{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002796 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002797 size_t blockSize = 0;
2798
2799 // Don't define this block at all if it's not active in the implementation.
2800 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2801 {
2802 return;
2803 }
2804
2805 // Track the first and last uniform index to determine the range of active uniforms in the
2806 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002807 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002808 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002809 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002810
2811 std::vector<unsigned int> blockUniformIndexes;
2812 for (size_t blockUniformIndex = firstBlockUniformIndex;
2813 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2814 {
2815 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2816 }
2817
2818 if (interfaceBlock.arraySize > 0)
2819 {
2820 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2821 {
2822 UniformBlock block(interfaceBlock.name, true, arrayElement);
2823 block.memberUniformIndexes = blockUniformIndexes;
2824
Martin Radev4c4c8e72016-08-04 12:25:34 +03002825 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002827 case GL_VERTEX_SHADER:
2828 {
2829 block.vertexStaticUse = interfaceBlock.staticUse;
2830 break;
2831 }
2832 case GL_FRAGMENT_SHADER:
2833 {
2834 block.fragmentStaticUse = interfaceBlock.staticUse;
2835 break;
2836 }
2837 case GL_COMPUTE_SHADER:
2838 {
2839 block.computeStaticUse = interfaceBlock.staticUse;
2840 break;
2841 }
2842 default:
2843 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002844 }
2845
Jamie Madill4a3c2342015-10-08 12:58:45 -04002846 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2847 size_t blockElementSize = 0;
2848 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2849 {
2850 continue;
2851 }
2852
2853 ASSERT(blockElementSize == blockSize);
2854 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002855 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 }
2857 }
2858 else
2859 {
2860 UniformBlock block(interfaceBlock.name, false, 0);
2861 block.memberUniformIndexes = blockUniformIndexes;
2862
Martin Radev4c4c8e72016-08-04 12:25:34 +03002863 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002864 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002865 case GL_VERTEX_SHADER:
2866 {
2867 block.vertexStaticUse = interfaceBlock.staticUse;
2868 break;
2869 }
2870 case GL_FRAGMENT_SHADER:
2871 {
2872 block.fragmentStaticUse = interfaceBlock.staticUse;
2873 break;
2874 }
2875 case GL_COMPUTE_SHADER:
2876 {
2877 block.computeStaticUse = interfaceBlock.staticUse;
2878 break;
2879 }
2880 default:
2881 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002882 }
2883
Jamie Madill4a3c2342015-10-08 12:58:45 -04002884 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002885 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002886 }
2887}
2888
2889template <typename T>
2890void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2891{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002892 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2893 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002894 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2895
2896 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2897 {
2898 // Do a cast conversion for boolean types. From the spec:
2899 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2900 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2901 for (GLsizei component = 0; component < count; ++component)
2902 {
2903 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2904 }
2905 }
2906 else
2907 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002908 // Invalide the validation cache if we modify the sampler data.
2909 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2910 {
2911 mCachedValidateSamplersResult.reset();
2912 }
2913
Jamie Madill62d31cb2015-09-11 13:25:51 -04002914 memcpy(destPointer, v, sizeof(T) * count);
2915 }
2916}
2917
2918template <size_t cols, size_t rows, typename T>
2919void Program::setMatrixUniformInternal(GLint location,
2920 GLsizei count,
2921 GLboolean transpose,
2922 const T *v)
2923{
2924 if (!transpose)
2925 {
2926 setUniformInternal(location, count * cols * rows, v);
2927 return;
2928 }
2929
2930 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002931 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2932 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002933 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2934 for (GLsizei element = 0; element < count; ++element)
2935 {
2936 size_t elementOffset = element * rows * cols;
2937
2938 for (size_t row = 0; row < rows; ++row)
2939 {
2940 for (size_t col = 0; col < cols; ++col)
2941 {
2942 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2943 }
2944 }
2945 }
2946}
2947
2948template <typename DestT>
2949void Program::getUniformInternal(GLint location, DestT *dataOut) const
2950{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002951 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2952 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002953
2954 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2955
2956 GLenum componentType = VariableComponentType(uniform.type);
2957 if (componentType == GLTypeToGLenum<DestT>::value)
2958 {
2959 memcpy(dataOut, srcPointer, uniform.getElementSize());
2960 return;
2961 }
2962
Corentin Wallez6596c462016-03-17 17:26:58 -04002963 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002964
2965 switch (componentType)
2966 {
2967 case GL_INT:
2968 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2969 break;
2970 case GL_UNSIGNED_INT:
2971 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2972 break;
2973 case GL_BOOL:
2974 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2975 break;
2976 case GL_FLOAT:
2977 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2978 break;
2979 default:
2980 UNREACHABLE();
2981 }
2982}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002983}