blob: 29be350feaa0350682056a5b17b25350b14dbda2 [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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380bool Program::attachShader(Shader *shader)
381{
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 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300386 if (mState.mAttachedVertexShader)
387 {
388 return false;
389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
Martin Radev4c4c8e72016-08-04 12:25:34 +0300391 mState.mAttachedVertexShader = shader;
392 mState.mAttachedVertexShader->addRef();
393 break;
394 }
395 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300397 if (mState.mAttachedFragmentShader)
398 {
399 return false;
400 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
Martin Radev4c4c8e72016-08-04 12:25:34 +0300402 mState.mAttachedFragmentShader = shader;
403 mState.mAttachedFragmentShader->addRef();
404 break;
405 }
406 case GL_COMPUTE_SHADER:
407 {
408 if (mState.mAttachedComputeShader)
409 {
410 return false;
411 }
412
413 mState.mAttachedComputeShader = shader;
414 mState.mAttachedComputeShader->addRef();
415 break;
416 }
417 default:
418 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
421 return true;
422}
423
424bool Program::detachShader(Shader *shader)
425{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300426 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300428 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300430 if (mState.mAttachedVertexShader != shader)
431 {
432 return false;
433 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
Martin Radev4c4c8e72016-08-04 12:25:34 +0300435 shader->release();
436 mState.mAttachedVertexShader = nullptr;
437 break;
438 }
439 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300441 if (mState.mAttachedFragmentShader != shader)
442 {
443 return false;
444 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445
Martin Radev4c4c8e72016-08-04 12:25:34 +0300446 shader->release();
447 mState.mAttachedFragmentShader = nullptr;
448 break;
449 }
450 case GL_COMPUTE_SHADER:
451 {
452 if (mState.mAttachedComputeShader != shader)
453 {
454 return false;
455 }
456
457 shader->release();
458 mState.mAttachedComputeShader = nullptr;
459 break;
460 }
461 default:
462 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465 return true;
466}
467
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000468int Program::getAttachedShadersCount() const
469{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300470 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
471 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000472}
473
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474void Program::bindAttributeLocation(GLuint index, const char *name)
475{
Geoff Langd8605522016-04-13 10:19:12 -0400476 mAttributeBindings.bindLocation(index, name);
477}
478
479void Program::bindUniformLocation(GLuint index, const char *name)
480{
481 // Bind the base uniform name only since array indices other than 0 cannot be bound
482 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483}
484
Sami Väisänen46eaa942016-06-29 10:26:37 +0300485void Program::bindFragmentInputLocation(GLint index, const char *name)
486{
487 mFragmentInputBindings.bindLocation(index, name);
488}
489
490BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
491{
492 BindingInfo ret;
493 ret.type = GL_NONE;
494 ret.valid = false;
495
496 const Shader *fragmentShader = mState.getAttachedFragmentShader();
497 ASSERT(fragmentShader);
498
499 // Find the actual fragment shader varying we're interested in
500 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
501
502 for (const auto &binding : mFragmentInputBindings)
503 {
504 if (binding.second != static_cast<GLuint>(index))
505 continue;
506
507 ret.valid = true;
508
509 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400510 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300511
512 for (const auto &in : inputs)
513 {
514 if (in.name == originalName)
515 {
516 if (in.isArray())
517 {
518 // The client wants to bind either "name" or "name[0]".
519 // GL ES 3.1 spec refers to active array names with language such as:
520 // "if the string identifies the base name of an active array, where the
521 // string would exactly match the name of the variable if the suffix "[0]"
522 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400523 if (arrayIndex == GL_INVALID_INDEX)
524 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300525
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400526 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300527 }
528 else
529 {
530 ret.name = in.mappedName;
531 }
532 ret.type = in.type;
533 return ret;
534 }
535 }
536 }
537
538 return ret;
539}
540
541void Program::pathFragmentInputGen(GLint index,
542 GLenum genMode,
543 GLint components,
544 const GLfloat *coeffs)
545{
546 // If the location is -1 then the command is silently ignored
547 if (index == -1)
548 return;
549
550 const auto &binding = getFragmentInputBindingInfo(index);
551
552 // If the input doesn't exist then then the command is silently ignored
553 // This could happen through optimization for example, the shader translator
554 // decides that a variable is not actually being used and optimizes it away.
555 if (binding.name.empty())
556 return;
557
558 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
559}
560
Martin Radev4c4c8e72016-08-04 12:25:34 +0300561// The attached shaders are checked for linking errors by matching up their variables.
562// Uniform, input and output variables get collected.
563// The code gets compiled into binaries.
Jamie Madill9082b982016-04-27 15:21:51 -0400564Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000565{
566 unlink(false);
567
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000568 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000569 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000570
Martin Radev4c4c8e72016-08-04 12:25:34 +0300571 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500572
Martin Radev4c4c8e72016-08-04 12:25:34 +0300573 bool isComputeShaderAttached = (mState.mAttachedComputeShader != nullptr);
574 bool nonComputeShadersAttached =
575 (mState.mAttachedVertexShader != nullptr || mState.mAttachedFragmentShader != nullptr);
576 // Check whether we both have a compute and non-compute shaders attached.
577 // If there are of both types attached, then linking should fail.
578 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
579 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500580 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300581 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
582 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400583 }
584
Martin Radev4c4c8e72016-08-04 12:25:34 +0300585 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500586 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300587 if (!mState.mAttachedComputeShader->isCompiled())
588 {
589 mInfoLog << "Attached compute shader is not compiled.";
590 return NoError();
591 }
592 ASSERT(mState.mAttachedComputeShader->getType() == GL_COMPUTE_SHADER);
593
594 mState.mComputeShaderLocalSize = mState.mAttachedComputeShader->getWorkGroupSize();
595
596 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
597 // If the work group size is not specified, a link time error should occur.
598 if (!mState.mComputeShaderLocalSize.isDeclared())
599 {
600 mInfoLog << "Work group size is not specified.";
601 return NoError();
602 }
603
604 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
605 {
606 return NoError();
607 }
608
609 if (!linkUniformBlocks(mInfoLog, caps))
610 {
611 return NoError();
612 }
613
614 rx::LinkResult result = mProgram->link(data, mInfoLog);
615
616 if (result.error.isError() || !result.linkSuccess)
617 {
618 return result.error;
619 }
620 }
621 else
622 {
623 if (!mState.mAttachedFragmentShader || !mState.mAttachedFragmentShader->isCompiled())
624 {
625 return NoError();
626 }
627 ASSERT(mState.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
628
629 if (!mState.mAttachedVertexShader || !mState.mAttachedVertexShader->isCompiled())
630 {
631 return NoError();
632 }
633 ASSERT(mState.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
634
635 if (mState.mAttachedFragmentShader->getShaderVersion() !=
636 mState.mAttachedVertexShader->getShaderVersion())
637 {
638 mInfoLog << "Fragment shader version does not match vertex shader version.";
639 return NoError();
640 }
641
642 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mState.mAttachedVertexShader))
643 {
644 return NoError();
645 }
646
647 if (!linkVaryings(mInfoLog, mState.mAttachedVertexShader, mState.mAttachedFragmentShader))
648 {
649 return NoError();
650 }
651
652 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
653 {
654 return NoError();
655 }
656
657 if (!linkUniformBlocks(mInfoLog, caps))
658 {
659 return NoError();
660 }
661
662 const auto &mergedVaryings = getMergedVaryings();
663
664 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
665 {
666 return NoError();
667 }
668
669 linkOutputVariables();
670
671 rx::LinkResult result = mProgram->link(data, mInfoLog);
672 if (result.error.isError() || !result.linkSuccess)
673 {
674 return result.error;
675 }
676
677 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500678 }
679
Jamie Madill4a3c2342015-10-08 12:58:45 -0400680 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400681
Geoff Lang7dd2e102014-11-10 15:19:26 -0500682 mLinked = true;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300683 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000684}
685
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000686// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687void Program::unlink(bool destroy)
688{
689 if (destroy) // Object being destructed
690 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400691 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400693 mState.mAttachedFragmentShader->release();
694 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695 }
696
Jamie Madill48ef11b2016-04-27 15:21:52 -0400697 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400699 mState.mAttachedVertexShader->release();
700 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300702
703 if (mState.mAttachedComputeShader)
704 {
705 mState.mAttachedComputeShader->release();
706 mState.mAttachedComputeShader = nullptr;
707 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708 }
709
Jamie Madill48ef11b2016-04-27 15:21:52 -0400710 mState.mAttributes.clear();
711 mState.mActiveAttribLocationsMask.reset();
712 mState.mTransformFeedbackVaryingVars.clear();
713 mState.mUniforms.clear();
714 mState.mUniformLocations.clear();
715 mState.mUniformBlocks.clear();
716 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300717 mState.mComputeShaderLocalSize.fill(1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500718
Geoff Lang7dd2e102014-11-10 15:19:26 -0500719 mValidated = false;
720
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000721 mLinked = false;
722}
723
Geoff Lange1a27752015-10-05 13:16:04 -0400724bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000725{
726 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
Geoff Lang7dd2e102014-11-10 15:19:26 -0500729Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000730{
731 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000732
Geoff Lang7dd2e102014-11-10 15:19:26 -0500733#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
734 return Error(GL_NO_ERROR);
735#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400736 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
737 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000738 {
Jamie Madillf6113162015-05-07 11:49:21 -0400739 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500740 return Error(GL_NO_ERROR);
741 }
742
Geoff Langc46cc2f2015-10-01 17:16:20 -0400743 BinaryInputStream stream(binary, length);
744
Geoff Lang7dd2e102014-11-10 15:19:26 -0500745 int majorVersion = stream.readInt<int>();
746 int minorVersion = stream.readInt<int>();
747 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
748 {
Jamie Madillf6113162015-05-07 11:49:21 -0400749 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500750 return Error(GL_NO_ERROR);
751 }
752
753 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
754 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
755 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
756 {
Jamie Madillf6113162015-05-07 11:49:21 -0400757 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500758 return Error(GL_NO_ERROR);
759 }
760
Martin Radev4c4c8e72016-08-04 12:25:34 +0300761 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
762 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
763 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
764
Jamie Madill63805b42015-08-25 13:17:39 -0400765 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
766 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400767 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500768
Jamie Madill3da79b72015-04-27 11:09:17 -0400769 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400770 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400771 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
772 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400773 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400774 LoadShaderVar(&stream, &attrib);
775 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400776 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400777 }
778
Jamie Madill62d31cb2015-09-11 13:25:51 -0400779 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400781 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
782 {
783 LinkedUniform uniform;
784 LoadShaderVar(&stream, &uniform);
785
786 uniform.blockIndex = stream.readInt<int>();
787 uniform.blockInfo.offset = stream.readInt<int>();
788 uniform.blockInfo.arrayStride = stream.readInt<int>();
789 uniform.blockInfo.matrixStride = stream.readInt<int>();
790 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
791
Jamie Madill48ef11b2016-04-27 15:21:52 -0400792 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400793 }
794
795 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400796 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400797 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
798 uniformIndexIndex++)
799 {
800 VariableLocation variable;
801 stream.readString(&variable.name);
802 stream.readInt(&variable.element);
803 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400804 stream.readBool(&variable.used);
805 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400806
Jamie Madill48ef11b2016-04-27 15:21:52 -0400807 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400808 }
809
810 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400812 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
813 ++uniformBlockIndex)
814 {
815 UniformBlock uniformBlock;
816 stream.readString(&uniformBlock.name);
817 stream.readBool(&uniformBlock.isArray);
818 stream.readInt(&uniformBlock.arrayElement);
819 stream.readInt(&uniformBlock.dataSize);
820 stream.readBool(&uniformBlock.vertexStaticUse);
821 stream.readBool(&uniformBlock.fragmentStaticUse);
822
823 unsigned int numMembers = stream.readInt<unsigned int>();
824 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
825 {
826 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
827 }
828
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830 }
831
Brandon Jones1048ea72015-10-06 15:34:52 -0700832 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400833 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700834 for (unsigned int transformFeedbackVaryingIndex = 0;
835 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
836 ++transformFeedbackVaryingIndex)
837 {
838 sh::Varying varying;
839 stream.readInt(&varying.arraySize);
840 stream.readInt(&varying.type);
841 stream.readString(&varying.name);
842
Jamie Madill48ef11b2016-04-27 15:21:52 -0400843 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700844 }
845
Jamie Madill48ef11b2016-04-27 15:21:52 -0400846 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400847
Jamie Madill80a6fc02015-08-21 16:53:16 -0400848 unsigned int outputVarCount = stream.readInt<unsigned int>();
849 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
850 {
851 int locationIndex = stream.readInt<int>();
852 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400853 stream.readInt(&locationData.element);
854 stream.readInt(&locationData.index);
855 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400856 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400857 }
858
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400859 stream.readInt(&mSamplerUniformRange.start);
860 stream.readInt(&mSamplerUniformRange.end);
861
Geoff Lang7dd2e102014-11-10 15:19:26 -0500862 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
863 if (result.error.isError() || !result.linkSuccess)
864 {
Geoff Langb543aff2014-09-30 14:52:54 -0400865 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000866 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000867
Geoff Lang7dd2e102014-11-10 15:19:26 -0500868 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400869 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500870#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
871}
872
873Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
874{
875 if (binaryFormat)
876 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400877 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878 }
879
880 BinaryOutputStream stream;
881
Geoff Lang7dd2e102014-11-10 15:19:26 -0500882 stream.writeInt(ANGLE_MAJOR_VERSION);
883 stream.writeInt(ANGLE_MINOR_VERSION);
884 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
885
Martin Radev4c4c8e72016-08-04 12:25:34 +0300886 stream.writeInt(mState.mComputeShaderLocalSize[0]);
887 stream.writeInt(mState.mComputeShaderLocalSize[1]);
888 stream.writeInt(mState.mComputeShaderLocalSize[2]);
889
Jamie Madill48ef11b2016-04-27 15:21:52 -0400890 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891
Jamie Madill48ef11b2016-04-27 15:21:52 -0400892 stream.writeInt(mState.mAttributes.size());
893 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400894 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400895 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400896 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400897 }
898
Jamie Madill48ef11b2016-04-27 15:21:52 -0400899 stream.writeInt(mState.mUniforms.size());
900 for (const gl::LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400901 {
902 WriteShaderVar(&stream, uniform);
903
904 // FIXME: referenced
905
906 stream.writeInt(uniform.blockIndex);
907 stream.writeInt(uniform.blockInfo.offset);
908 stream.writeInt(uniform.blockInfo.arrayStride);
909 stream.writeInt(uniform.blockInfo.matrixStride);
910 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
911 }
912
Jamie Madill48ef11b2016-04-27 15:21:52 -0400913 stream.writeInt(mState.mUniformLocations.size());
914 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400915 {
916 stream.writeString(variable.name);
917 stream.writeInt(variable.element);
918 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400919 stream.writeInt(variable.used);
920 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400921 }
922
Jamie Madill48ef11b2016-04-27 15:21:52 -0400923 stream.writeInt(mState.mUniformBlocks.size());
924 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400925 {
926 stream.writeString(uniformBlock.name);
927 stream.writeInt(uniformBlock.isArray);
928 stream.writeInt(uniformBlock.arrayElement);
929 stream.writeInt(uniformBlock.dataSize);
930
931 stream.writeInt(uniformBlock.vertexStaticUse);
932 stream.writeInt(uniformBlock.fragmentStaticUse);
933
934 stream.writeInt(uniformBlock.memberUniformIndexes.size());
935 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
936 {
937 stream.writeInt(memberUniformIndex);
938 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400939 }
940
Jamie Madill48ef11b2016-04-27 15:21:52 -0400941 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
942 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700943 {
944 stream.writeInt(varying.arraySize);
945 stream.writeInt(varying.type);
946 stream.writeString(varying.name);
947 }
948
Jamie Madill48ef11b2016-04-27 15:21:52 -0400949 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400950
Jamie Madill48ef11b2016-04-27 15:21:52 -0400951 stream.writeInt(mState.mOutputVariables.size());
952 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400953 {
954 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400955 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400956 stream.writeInt(outputPair.second.index);
957 stream.writeString(outputPair.second.name);
958 }
959
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400960 stream.writeInt(mSamplerUniformRange.start);
961 stream.writeInt(mSamplerUniformRange.end);
962
Geoff Lang7dd2e102014-11-10 15:19:26 -0500963 gl::Error error = mProgram->save(&stream);
964 if (error.isError())
965 {
966 return error;
967 }
968
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700969 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400970 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500971
972 if (streamLength > bufSize)
973 {
974 if (length)
975 {
976 *length = 0;
977 }
978
979 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
980 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
981 // sizes and then copy it.
982 return Error(GL_INVALID_OPERATION);
983 }
984
985 if (binary)
986 {
987 char *ptr = reinterpret_cast<char*>(binary);
988
Jamie Madill48ef11b2016-04-27 15:21:52 -0400989 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500990 ptr += streamLength;
991
992 ASSERT(ptr - streamLength == binary);
993 }
994
995 if (length)
996 {
997 *length = streamLength;
998 }
999
1000 return Error(GL_NO_ERROR);
1001}
1002
1003GLint Program::getBinaryLength() const
1004{
1005 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001006 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007 if (error.isError())
1008 {
1009 return 0;
1010 }
1011
1012 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001013}
1014
Geoff Langc5629752015-12-07 16:29:04 -05001015void Program::setBinaryRetrievableHint(bool retrievable)
1016{
1017 // TODO(jmadill) : replace with dirty bits
1018 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001019 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001020}
1021
1022bool Program::getBinaryRetrievableHint() const
1023{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001024 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001025}
1026
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001027void Program::release()
1028{
1029 mRefCount--;
1030
1031 if (mRefCount == 0 && mDeleteStatus)
1032 {
1033 mResourceManager->deleteProgram(mHandle);
1034 }
1035}
1036
1037void Program::addRef()
1038{
1039 mRefCount++;
1040}
1041
1042unsigned int Program::getRefCount() const
1043{
1044 return mRefCount;
1045}
1046
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001047int Program::getInfoLogLength() const
1048{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001049 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001050}
1051
Geoff Lange1a27752015-10-05 13:16:04 -04001052void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001053{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001054 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001055}
1056
Geoff Lange1a27752015-10-05 13:16:04 -04001057void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001058{
1059 int total = 0;
1060
Martin Radev4c4c8e72016-08-04 12:25:34 +03001061 if (mState.mAttachedComputeShader)
1062 {
1063 if (total < maxCount)
1064 {
1065 shaders[total] = mState.mAttachedComputeShader->getHandle();
1066 total++;
1067 }
1068 }
1069
Jamie Madill48ef11b2016-04-27 15:21:52 -04001070 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001071 {
1072 if (total < maxCount)
1073 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001074 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001075 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001076 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001077 }
1078
Jamie Madill48ef11b2016-04-27 15:21:52 -04001079 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001080 {
1081 if (total < maxCount)
1082 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001083 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001084 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001085 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001086 }
1087
1088 if (count)
1089 {
1090 *count = total;
1091 }
1092}
1093
Geoff Lange1a27752015-10-05 13:16:04 -04001094GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001095{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001096 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001098 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001100 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001101 }
1102 }
1103
Austin Kinrossb8af7232015-03-16 22:33:25 -07001104 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001105}
1106
Jamie Madill63805b42015-08-25 13:17:39 -04001107bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001108{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001109 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1110 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111}
1112
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001113void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1114{
Jamie Madillc349ec02015-08-21 16:53:12 -04001115 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001116 {
1117 if (bufsize > 0)
1118 {
1119 name[0] = '\0';
1120 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001121
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001122 if (length)
1123 {
1124 *length = 0;
1125 }
1126
1127 *type = GL_NONE;
1128 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001129 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001130 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001131
1132 size_t attributeIndex = 0;
1133
Jamie Madill48ef11b2016-04-27 15:21:52 -04001134 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001135 {
1136 // Skip over inactive attributes
1137 if (attribute.staticUse)
1138 {
1139 if (static_cast<size_t>(index) == attributeIndex)
1140 {
1141 break;
1142 }
1143 attributeIndex++;
1144 }
1145 }
1146
Jamie Madill48ef11b2016-04-27 15:21:52 -04001147 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1148 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001149
1150 if (bufsize > 0)
1151 {
1152 const char *string = attrib.name.c_str();
1153
1154 strncpy(name, string, bufsize);
1155 name[bufsize - 1] = '\0';
1156
1157 if (length)
1158 {
1159 *length = static_cast<GLsizei>(strlen(name));
1160 }
1161 }
1162
1163 // Always a single 'type' instance
1164 *size = 1;
1165 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001166}
1167
Geoff Lange1a27752015-10-05 13:16:04 -04001168GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001169{
Jamie Madillc349ec02015-08-21 16:53:12 -04001170 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001171 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001172 return 0;
1173 }
1174
1175 GLint count = 0;
1176
Jamie Madill48ef11b2016-04-27 15:21:52 -04001177 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001178 {
1179 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001180 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181
1182 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001183}
1184
Geoff Lange1a27752015-10-05 13:16:04 -04001185GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001186{
Jamie Madillc349ec02015-08-21 16:53:12 -04001187 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001188 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001189 return 0;
1190 }
1191
1192 size_t maxLength = 0;
1193
Jamie Madill48ef11b2016-04-27 15:21:52 -04001194 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001195 {
1196 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001197 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001198 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001200 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201
Jamie Madillc349ec02015-08-21 16:53:12 -04001202 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203}
1204
Geoff Lang7dd2e102014-11-10 15:19:26 -05001205GLint Program::getFragDataLocation(const std::string &name) const
1206{
1207 std::string baseName(name);
1208 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001209 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001210 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001211 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001212 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1213 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001214 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001215 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001216 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001218}
1219
Geoff Lange1a27752015-10-05 13:16:04 -04001220void Program::getActiveUniform(GLuint index,
1221 GLsizei bufsize,
1222 GLsizei *length,
1223 GLint *size,
1224 GLenum *type,
1225 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001226{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001227 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001228 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001229 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001230 ASSERT(index < mState.mUniforms.size());
1231 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232
1233 if (bufsize > 0)
1234 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001235 std::string string = uniform.name;
1236 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237 {
1238 string += "[0]";
1239 }
1240
1241 strncpy(name, string.c_str(), bufsize);
1242 name[bufsize - 1] = '\0';
1243
1244 if (length)
1245 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001246 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247 }
1248 }
1249
Jamie Madill62d31cb2015-09-11 13:25:51 -04001250 *size = uniform.elementCount();
1251 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252 }
1253 else
1254 {
1255 if (bufsize > 0)
1256 {
1257 name[0] = '\0';
1258 }
1259
1260 if (length)
1261 {
1262 *length = 0;
1263 }
1264
1265 *size = 0;
1266 *type = GL_NONE;
1267 }
1268}
1269
Geoff Lange1a27752015-10-05 13:16:04 -04001270GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001271{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001273 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001274 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001275 }
1276 else
1277 {
1278 return 0;
1279 }
1280}
1281
Geoff Lange1a27752015-10-05 13:16:04 -04001282GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001283{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001284 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285
1286 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001287 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001288 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001289 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001290 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001291 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001292 size_t length = uniform.name.length() + 1u;
1293 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001294 {
1295 length += 3; // Counting in "[0]".
1296 }
1297 maxLength = std::max(length, maxLength);
1298 }
1299 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001300 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001301
Jamie Madill62d31cb2015-09-11 13:25:51 -04001302 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001303}
1304
1305GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1306{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001307 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
1308 const gl::LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001310 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001311 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1312 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1313 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1314 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1315 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1316 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1317 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1318 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1319 default:
1320 UNREACHABLE();
1321 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001322 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001323 return 0;
1324}
1325
1326bool Program::isValidUniformLocation(GLint location) const
1327{
Jamie Madille2e406c2016-06-02 13:04:10 -04001328 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001329 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1330 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001331}
1332
1333bool Program::isIgnoredUniformLocation(GLint location) const
1334{
1335 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1336 // out
1337 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001338 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1339 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340}
1341
Jamie Madill62d31cb2015-09-11 13:25:51 -04001342const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001344 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1345 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346}
1347
Jamie Madill62d31cb2015-09-11 13:25:51 -04001348GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001350 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001351}
1352
Jamie Madill62d31cb2015-09-11 13:25:51 -04001353GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001355 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356}
1357
1358void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1359{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001360 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361 mProgram->setUniform1fv(location, count, v);
1362}
1363
1364void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1365{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001366 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001367 mProgram->setUniform2fv(location, count, v);
1368}
1369
1370void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1371{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001372 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373 mProgram->setUniform3fv(location, count, v);
1374}
1375
1376void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1377{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001378 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379 mProgram->setUniform4fv(location, count, v);
1380}
1381
1382void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1383{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001384 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385 mProgram->setUniform1iv(location, count, v);
1386}
1387
1388void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1389{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001390 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391 mProgram->setUniform2iv(location, count, v);
1392}
1393
1394void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1395{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001396 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397 mProgram->setUniform3iv(location, count, v);
1398}
1399
1400void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1401{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001402 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403 mProgram->setUniform4iv(location, count, v);
1404}
1405
1406void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1407{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001408 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409 mProgram->setUniform1uiv(location, count, v);
1410}
1411
1412void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1413{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001414 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415 mProgram->setUniform2uiv(location, count, v);
1416}
1417
1418void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1419{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001420 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421 mProgram->setUniform3uiv(location, count, v);
1422}
1423
1424void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1425{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001426 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427 mProgram->setUniform4uiv(location, count, v);
1428}
1429
1430void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1431{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001432 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1434}
1435
1436void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1437{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001438 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1440}
1441
1442void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1443{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001444 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1446}
1447
1448void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1449{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001450 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1452}
1453
1454void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1455{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001456 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001457 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1458}
1459
1460void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1461{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1464}
1465
1466void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1467{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001468 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1470}
1471
1472void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1473{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001474 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1476}
1477
1478void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1479{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001480 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1482}
1483
Geoff Lange1a27752015-10-05 13:16:04 -04001484void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001485{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001486 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487}
1488
Geoff Lange1a27752015-10-05 13:16:04 -04001489void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001490{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001491 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492}
1493
Geoff Lange1a27752015-10-05 13:16:04 -04001494void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001496 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001497}
1498
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499void Program::flagForDeletion()
1500{
1501 mDeleteStatus = true;
1502}
1503
1504bool Program::isFlaggedForDeletion() const
1505{
1506 return mDeleteStatus;
1507}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001508
Brandon Jones43a53e22014-08-28 16:23:22 -07001509void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001510{
1511 mInfoLog.reset();
1512
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001514 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001515 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001516 }
1517 else
1518 {
Jamie Madillf6113162015-05-07 11:49:21 -04001519 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001520 }
1521}
1522
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1524{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001525 // Skip cache if we're using an infolog, so we get the full error.
1526 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1527 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1528 {
1529 return mCachedValidateSamplersResult.value();
1530 }
1531
1532 if (mTextureUnitTypesCache.empty())
1533 {
1534 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1535 }
1536 else
1537 {
1538 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1539 }
1540
1541 // if any two active samplers in a program are of different types, but refer to the same
1542 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1543 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1544 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1545 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1546 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001547 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001548 ASSERT(uniform.isSampler());
1549
1550 if (!uniform.staticUse)
1551 continue;
1552
1553 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1554 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1555
1556 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1557 {
1558 GLuint textureUnit = dataPtr[arrayElement];
1559
1560 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1561 {
1562 if (infoLog)
1563 {
1564 (*infoLog) << "Sampler uniform (" << textureUnit
1565 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1566 << caps.maxCombinedTextureImageUnits << ")";
1567 }
1568
1569 mCachedValidateSamplersResult = false;
1570 return false;
1571 }
1572
1573 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1574 {
1575 if (textureType != mTextureUnitTypesCache[textureUnit])
1576 {
1577 if (infoLog)
1578 {
1579 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1580 "image unit ("
1581 << textureUnit << ").";
1582 }
1583
1584 mCachedValidateSamplersResult = false;
1585 return false;
1586 }
1587 }
1588 else
1589 {
1590 mTextureUnitTypesCache[textureUnit] = textureType;
1591 }
1592 }
1593 }
1594
1595 mCachedValidateSamplersResult = true;
1596 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001597}
1598
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001599bool Program::isValidated() const
1600{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601 return mValidated;
1602}
1603
Geoff Lange1a27752015-10-05 13:16:04 -04001604GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001605{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001606 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001607}
1608
1609void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1610{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001611 ASSERT(
1612 uniformBlockIndex <
1613 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614
Jamie Madill48ef11b2016-04-27 15:21:52 -04001615 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001616
1617 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001618 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619 std::string string = uniformBlock.name;
1620
Jamie Madill62d31cb2015-09-11 13:25:51 -04001621 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001623 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 }
1625
1626 strncpy(uniformBlockName, string.c_str(), bufSize);
1627 uniformBlockName[bufSize - 1] = '\0';
1628
1629 if (length)
1630 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001631 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001633 }
1634}
1635
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001637{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001638 ASSERT(
1639 uniformBlockIndex <
1640 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001641
Jamie Madill48ef11b2016-04-27 15:21:52 -04001642 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643
1644 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001645 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 case GL_UNIFORM_BLOCK_DATA_SIZE:
1647 *params = static_cast<GLint>(uniformBlock.dataSize);
1648 break;
1649 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001650 *params =
1651 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652 break;
1653 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1654 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1655 break;
1656 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1657 {
1658 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1659 {
1660 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1661 }
1662 }
1663 break;
1664 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001666 break;
1667 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001668 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001669 break;
1670 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001671 }
1672}
1673
Geoff Lange1a27752015-10-05 13:16:04 -04001674GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001675{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001676 int maxLength = 0;
1677
1678 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001679 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001680 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1682 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001683 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001684 if (!uniformBlock.name.empty())
1685 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001686 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001687
1688 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001689 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001690
1691 maxLength = std::max(length + arrayLength, maxLength);
1692 }
1693 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001694 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695
1696 return maxLength;
1697}
1698
Geoff Lange1a27752015-10-05 13:16:04 -04001699GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001701 size_t subscript = GL_INVALID_INDEX;
1702 std::string baseName = gl::ParseUniformName(name, &subscript);
1703
Jamie Madill48ef11b2016-04-27 15:21:52 -04001704 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001705 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1706 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001707 const gl::UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001708 if (uniformBlock.name == baseName)
1709 {
1710 const bool arrayElementZero =
1711 (subscript == GL_INVALID_INDEX &&
1712 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1713 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1714 {
1715 return blockIndex;
1716 }
1717 }
1718 }
1719
1720 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001721}
1722
Jamie Madill62d31cb2015-09-11 13:25:51 -04001723const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001724{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001725 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1726 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001727}
1728
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001729void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1730{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001731 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001732 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001733}
1734
1735GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1736{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001737 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001738}
1739
1740void Program::resetUniformBlockBindings()
1741{
1742 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1743 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001744 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001745 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001746 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001747}
1748
Geoff Lang48dcae72014-02-05 16:28:24 -05001749void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1750{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001751 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001752 for (GLsizei i = 0; i < count; i++)
1753 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001754 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001755 }
1756
Jamie Madill48ef11b2016-04-27 15:21:52 -04001757 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001758}
1759
1760void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1761{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001762 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001763 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001764 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1765 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001766 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1767 if (length)
1768 {
1769 *length = lastNameIdx;
1770 }
1771 if (size)
1772 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001773 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001774 }
1775 if (type)
1776 {
1777 *type = varying.type;
1778 }
1779 if (name)
1780 {
1781 memcpy(name, varying.name.c_str(), lastNameIdx);
1782 name[lastNameIdx] = '\0';
1783 }
1784 }
1785}
1786
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001787GLsizei Program::getTransformFeedbackVaryingCount() const
1788{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001790 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001791 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001792 }
1793 else
1794 {
1795 return 0;
1796 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001797}
1798
1799GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1800{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001802 {
1803 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001804 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001805 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001806 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1807 }
1808
1809 return maxSize;
1810 }
1811 else
1812 {
1813 return 0;
1814 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001815}
1816
1817GLenum Program::getTransformFeedbackBufferMode() const
1818{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001819 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820}
1821
Jamie Madillada9ecc2015-08-17 12:53:37 -04001822bool Program::linkVaryings(InfoLog &infoLog,
1823 const Shader *vertexShader,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001824 const Shader *fragmentShader) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825{
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001826 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1827
Jamie Madill4cff2472015-08-21 16:53:18 -04001828 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1829 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830
Sami Väisänen46eaa942016-06-29 10:26:37 +03001831 std::map<GLuint, std::string> staticFragmentInputLocations;
1832
Jamie Madill4cff2472015-08-21 16:53:18 -04001833 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001835 bool matched = false;
1836
1837 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001838 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 {
1840 continue;
1841 }
1842
Jamie Madill4cff2472015-08-21 16:53:18 -04001843 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001845 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001846 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001847 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001848 if (!linkValidateVaryings(infoLog, output.name, input, output,
1849 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 {
1851 return false;
1852 }
1853
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854 matched = true;
1855 break;
1856 }
1857 }
1858
1859 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001860 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001862 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001863 return false;
1864 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001865
1866 // Check for aliased path rendering input bindings (if any).
1867 // If more than one binding refer statically to the same
1868 // location the link must fail.
1869
1870 if (!output.staticUse)
1871 continue;
1872
1873 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1874 if (inputBinding == -1)
1875 continue;
1876
1877 const auto it = staticFragmentInputLocations.find(inputBinding);
1878 if (it == std::end(staticFragmentInputLocations))
1879 {
1880 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1881 }
1882 else
1883 {
1884 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1885 << it->second;
1886 return false;
1887 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 }
1889
Jamie Madillada9ecc2015-08-17 12:53:37 -04001890 // TODO(jmadill): verify no unmatched vertex varyings?
1891
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 return true;
1893}
1894
Martin Radev4c4c8e72016-08-04 12:25:34 +03001895bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001896{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001897 // Check that uniforms defined in the vertex and fragment shaders are identical
1898 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001899 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1900 const std::vector<sh::Uniform> &fragmentUniforms =
1901 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001902
Jamie Madillea918db2015-08-18 14:48:59 -04001903 for (const sh::Uniform &vertexUniform : vertexUniforms)
1904 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001905 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001906 }
1907
1908 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1909 {
1910 auto entry = linkedUniforms.find(fragmentUniform.name);
1911 if (entry != linkedUniforms.end())
1912 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 LinkedUniform *vertexUniform = &entry->second;
1914 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1915 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001916 {
1917 return false;
1918 }
1919 }
1920 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001921 return true;
1922}
1923
1924bool Program::linkUniforms(gl::InfoLog &infoLog,
1925 const gl::Caps &caps,
1926 const Bindings &uniformBindings)
1927{
1928 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1929 {
1930 ASSERT(mState.mAttachedComputeShader == nullptr);
1931 if (!validateVertexAndFragmentUniforms(infoLog))
1932 {
1933 return false;
1934 }
1935 }
Jamie Madillea918db2015-08-18 14:48:59 -04001936
Jamie Madill62d31cb2015-09-11 13:25:51 -04001937 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1938 // Also check the maximum uniform vector and sampler counts.
1939 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1940 {
1941 return false;
1942 }
1943
Geoff Langd8605522016-04-13 10:19:12 -04001944 if (!indexUniforms(infoLog, caps, uniformBindings))
1945 {
1946 return false;
1947 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001948
Jamie Madillea918db2015-08-18 14:48:59 -04001949 return true;
1950}
1951
Geoff Langd8605522016-04-13 10:19:12 -04001952bool Program::indexUniforms(gl::InfoLog &infoLog,
1953 const gl::Caps &caps,
1954 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001955{
Geoff Langd8605522016-04-13 10:19:12 -04001956 // Uniforms awaiting a location
1957 std::vector<VariableLocation> unboundUniforms;
1958 std::map<GLuint, VariableLocation> boundUniforms;
1959 int maxUniformLocation = -1;
1960
1961 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001962 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001963 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001964 const gl::LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001965
Geoff Langd8605522016-04-13 10:19:12 -04001966 if (uniform.isBuiltIn())
1967 {
1968 continue;
1969 }
1970
1971 int bindingLocation = uniformBindings.getBinding(uniform.name);
1972
1973 // Verify that this location isn't bound twice
1974 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1975 {
1976 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1977 return false;
1978 }
1979
Jamie Madill62d31cb2015-09-11 13:25:51 -04001980 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1981 {
Geoff Langd8605522016-04-13 10:19:12 -04001982 VariableLocation location(uniform.name, arrayIndex,
1983 static_cast<unsigned int>(uniformIndex));
1984
1985 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001986 {
Geoff Langd8605522016-04-13 10:19:12 -04001987 boundUniforms[bindingLocation] = location;
1988 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1989 }
1990 else
1991 {
1992 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001993 }
1994 }
1995 }
Geoff Langd8605522016-04-13 10:19:12 -04001996
1997 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1998 // not be assigned to those locations.
1999 std::set<GLuint> reservedLocations;
2000 for (const auto &binding : uniformBindings)
2001 {
2002 GLuint location = binding.second;
2003 if (boundUniforms.find(location) == boundUniforms.end())
2004 {
2005 reservedLocations.insert(location);
2006 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
2007 }
2008 }
2009
2010 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04002011 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04002012 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
2013 static_cast<size_t>(maxUniformLocation + 1)));
2014
2015 // Assign bound uniforms
2016 for (const auto &boundUniform : boundUniforms)
2017 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002018 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002019 }
2020
2021 // Assign reserved uniforms
2022 for (const auto &reservedLocation : reservedLocations)
2023 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002024 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002025 }
2026
2027 // Assign unbound uniforms
2028 size_t nextUniformLocation = 0;
2029 for (const auto &unboundUniform : unboundUniforms)
2030 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002031 while (mState.mUniformLocations[nextUniformLocation].used ||
2032 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002033 {
2034 nextUniformLocation++;
2035 }
2036
Jamie Madill48ef11b2016-04-27 15:21:52 -04002037 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
2038 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002039 nextUniformLocation++;
2040 }
2041
2042 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002043}
2044
Martin Radev4c4c8e72016-08-04 12:25:34 +03002045bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2046 const std::string &uniformName,
2047 const sh::InterfaceBlockField &vertexUniform,
2048 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049{
Jamie Madillc4c744222015-11-04 09:39:47 -05002050 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2051 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002052 {
2053 return false;
2054 }
2055
2056 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2057 {
Jamie Madillf6113162015-05-07 11:49:21 -04002058 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002059 return false;
2060 }
2061
2062 return true;
2063}
2064
2065// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill9082b982016-04-27 15:21:51 -04002066bool Program::linkAttributes(const ContextState &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04002067 InfoLog &infoLog,
Geoff Langd8605522016-04-13 10:19:12 -04002068 const Bindings &attributeBindings,
Jamie Madill3da79b72015-04-27 11:09:17 -04002069 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070{
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002072 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002073 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002074
2075 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002076 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002077 {
Jamie Madillf6113162015-05-07 11:49:21 -04002078 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002079 return false;
2080 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002082 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002083
Jamie Madillc349ec02015-08-21 16:53:12 -04002084 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002085 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002086 {
2087 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088 ASSERT(attribute.staticUse);
2089
Geoff Langd8605522016-04-13 10:19:12 -04002090 int bindingLocation = attributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002091 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002092 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002093 attribute.location = bindingLocation;
2094 }
2095
2096 if (attribute.location != -1)
2097 {
2098 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002099 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100
Jamie Madill63805b42015-08-25 13:17:39 -04002101 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102 {
Jamie Madillf6113162015-05-07 11:49:21 -04002103 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002104 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105
2106 return false;
2107 }
2108
Jamie Madill63805b42015-08-25 13:17:39 -04002109 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 {
Jamie Madill63805b42015-08-25 13:17:39 -04002111 const int regLocation = attribute.location + reg;
2112 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113
2114 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002115 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002116 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 // TODO(jmadill): fix aliasing on ES2
2119 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002121 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002122 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 return false;
2124 }
2125 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002126 else
2127 {
Jamie Madill63805b42015-08-25 13:17:39 -04002128 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002129 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130
Jamie Madill63805b42015-08-25 13:17:39 -04002131 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132 }
2133 }
2134 }
2135
2136 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002137 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002138 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139 ASSERT(attribute.staticUse);
2140
Jamie Madillc349ec02015-08-21 16:53:12 -04002141 // Not set by glBindAttribLocation or by location layout qualifier
2142 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 {
Jamie Madill63805b42015-08-25 13:17:39 -04002144 int regs = VariableRegisterCount(attribute.type);
2145 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146
Jamie Madill63805b42015-08-25 13:17:39 -04002147 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 {
Jamie Madillf6113162015-05-07 11:49:21 -04002149 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002150 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 }
2152
Jamie Madillc349ec02015-08-21 16:53:12 -04002153 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 }
2155 }
2156
Jamie Madill48ef11b2016-04-27 15:21:52 -04002157 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002158 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002159 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002160 ASSERT(attribute.location != -1);
2161 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002162
Jamie Madill63805b42015-08-25 13:17:39 -04002163 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002165 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002166 }
2167 }
2168
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169 return true;
2170}
2171
Martin Radev4c4c8e72016-08-04 12:25:34 +03002172bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2173 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2174 const std::string &errorMessage,
2175 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002177 GLuint blockCount = 0;
2178 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002180 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002181 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002182 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002183 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002184 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002185 return false;
2186 }
2187 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002189 return true;
2190}
Jamie Madille473dee2015-08-18 14:49:01 -04002191
Martin Radev4c4c8e72016-08-04 12:25:34 +03002192bool Program::validateVertexAndFragmentInterfaceBlocks(
2193 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2194 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2195 InfoLog &infoLog) const
2196{
2197 // Check that interface blocks defined in the vertex and fragment shaders are identical
2198 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2199 UniformBlockMap linkedUniformBlocks;
2200
2201 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2202 {
2203 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2204 }
2205
Jamie Madille473dee2015-08-18 14:49:01 -04002206 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207 {
Jamie Madille473dee2015-08-18 14:49:01 -04002208 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002209 if (entry != linkedUniformBlocks.end())
2210 {
2211 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2212 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2213 {
2214 return false;
2215 }
2216 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002217 }
2218 return true;
2219}
Jamie Madille473dee2015-08-18 14:49:01 -04002220
Martin Radev4c4c8e72016-08-04 12:25:34 +03002221bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2222{
2223 if (mState.mAttachedComputeShader)
2224 {
2225 const Shader &computeShader = *mState.mAttachedComputeShader;
2226 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2227
2228 if (!validateUniformBlocksCount(
2229 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2230 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2231 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002232 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002233 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002235 return true;
2236 }
2237
2238 const Shader &vertexShader = *mState.mAttachedVertexShader;
2239 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2240
2241 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2242 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2243
2244 if (!validateUniformBlocksCount(
2245 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2246 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2247 {
2248 return false;
2249 }
2250 if (!validateUniformBlocksCount(
2251 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2252 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2253 infoLog))
2254 {
2255
2256 return false;
2257 }
2258 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2259 infoLog))
2260 {
2261 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002262 }
Jamie Madille473dee2015-08-18 14:49:01 -04002263
Geoff Lang7dd2e102014-11-10 15:19:26 -05002264 return true;
2265}
2266
Martin Radev4c4c8e72016-08-04 12:25:34 +03002267bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog,
2268 const sh::InterfaceBlock &vertexInterfaceBlock,
2269 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002270{
2271 const char* blockName = vertexInterfaceBlock.name.c_str();
2272 // validate blocks for the same member types
2273 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2274 {
Jamie Madillf6113162015-05-07 11:49:21 -04002275 infoLog << "Types for interface block '" << blockName
2276 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002277 return false;
2278 }
2279 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2280 {
Jamie Madillf6113162015-05-07 11:49:21 -04002281 infoLog << "Array sizes differ for interface block '" << blockName
2282 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002283 return false;
2284 }
2285 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2286 {
Jamie Madillf6113162015-05-07 11:49:21 -04002287 infoLog << "Layout qualifiers differ for interface block '" << blockName
2288 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289 return false;
2290 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002291 const unsigned int numBlockMembers =
2292 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2294 {
2295 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2296 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2297 if (vertexMember.name != fragmentMember.name)
2298 {
Jamie Madillf6113162015-05-07 11:49:21 -04002299 infoLog << "Name mismatch for field " << blockMemberIndex
2300 << " of interface block '" << blockName
2301 << "': (in vertex: '" << vertexMember.name
2302 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002303 return false;
2304 }
2305 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2306 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2307 {
2308 return false;
2309 }
2310 }
2311 return true;
2312}
2313
2314bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2315 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2316{
2317 if (vertexVariable.type != fragmentVariable.type)
2318 {
Jamie Madillf6113162015-05-07 11:49:21 -04002319 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002320 return false;
2321 }
2322 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2323 {
Jamie Madillf6113162015-05-07 11:49:21 -04002324 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002325 return false;
2326 }
2327 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2328 {
Jamie Madillf6113162015-05-07 11:49:21 -04002329 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002330 return false;
2331 }
2332
2333 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2334 {
Jamie Madillf6113162015-05-07 11:49:21 -04002335 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002336 return false;
2337 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002338 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002339 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2340 {
2341 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2342 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2343
2344 if (vertexMember.name != fragmentMember.name)
2345 {
Jamie Madillf6113162015-05-07 11:49:21 -04002346 infoLog << "Name mismatch for field '" << memberIndex
2347 << "' of " << variableName
2348 << ": (in vertex: '" << vertexMember.name
2349 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 return false;
2351 }
2352
2353 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2354 vertexMember.name + "'";
2355
2356 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2357 {
2358 return false;
2359 }
2360 }
2361
2362 return true;
2363}
2364
2365bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2366{
Cooper Partin1acf4382015-06-12 12:38:57 -07002367#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2368 const bool validatePrecision = true;
2369#else
2370 const bool validatePrecision = false;
2371#endif
2372
2373 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002374 {
2375 return false;
2376 }
2377
2378 return true;
2379}
2380
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002381bool Program::linkValidateVaryings(InfoLog &infoLog,
2382 const std::string &varyingName,
2383 const sh::Varying &vertexVarying,
2384 const sh::Varying &fragmentVarying,
2385 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386{
2387 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2388 {
2389 return false;
2390 }
2391
Jamie Madille9cc4692015-02-19 16:00:13 -05002392 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002393 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002394 infoLog << "Interpolation types for " << varyingName
2395 << " differ between vertex and fragment shaders.";
2396 return false;
2397 }
2398
2399 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2400 {
2401 infoLog << "Invariance for " << varyingName
2402 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403 return false;
2404 }
2405
2406 return true;
2407}
2408
Jamie Madillccdf74b2015-08-18 10:46:12 -04002409bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2410 const std::vector<const sh::Varying *> &varyings,
2411 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002412{
2413 size_t totalComponents = 0;
2414
Jamie Madillccdf74b2015-08-18 10:46:12 -04002415 std::set<std::string> uniqueNames;
2416
Jamie Madill48ef11b2016-04-27 15:21:52 -04002417 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002418 {
2419 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002420 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002421 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002422 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002423 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002424 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002425 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002426 infoLog << "Two transform feedback varyings specify the same output variable ("
2427 << tfVaryingName << ").";
2428 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002429 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002430 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002431
Geoff Lang1a683462015-09-29 15:09:59 -04002432 if (varying->isArray())
2433 {
2434 infoLog << "Capture of arrays is undefined and not supported.";
2435 return false;
2436 }
2437
Jamie Madillccdf74b2015-08-18 10:46:12 -04002438 // TODO(jmadill): Investigate implementation limits on D3D11
2439 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002440 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441 componentCount > caps.maxTransformFeedbackSeparateComponents)
2442 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002443 infoLog << "Transform feedback varying's " << varying->name << " components ("
2444 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002445 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002446 return false;
2447 }
2448
2449 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 found = true;
2451 break;
2452 }
2453 }
2454
Jamie Madill89bb70e2015-08-31 14:18:39 -04002455 if (tfVaryingName.find('[') != std::string::npos)
2456 {
Geoff Lang1a683462015-09-29 15:09:59 -04002457 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002458 return false;
2459 }
2460
Geoff Lang7dd2e102014-11-10 15:19:26 -05002461 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2462 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002463 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002464 }
2465
Jamie Madill48ef11b2016-04-27 15:21:52 -04002466 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002467 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002468 {
Jamie Madillf6113162015-05-07 11:49:21 -04002469 infoLog << "Transform feedback varying total components (" << totalComponents
2470 << ") exceed the maximum interleaved components ("
2471 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472 return false;
2473 }
2474
2475 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002476}
2477
Jamie Madillccdf74b2015-08-18 10:46:12 -04002478void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2479{
2480 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002481 mState.mTransformFeedbackVaryingVars.clear();
2482 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002483 {
2484 for (const sh::Varying *varying : varyings)
2485 {
2486 if (tfVaryingName == varying->name)
2487 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002488 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002489 break;
2490 }
2491 }
2492 }
2493}
2494
2495std::vector<const sh::Varying *> Program::getMergedVaryings() const
2496{
2497 std::set<std::string> uniqueNames;
2498 std::vector<const sh::Varying *> varyings;
2499
Jamie Madill48ef11b2016-04-27 15:21:52 -04002500 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002501 {
2502 if (uniqueNames.count(varying.name) == 0)
2503 {
2504 uniqueNames.insert(varying.name);
2505 varyings.push_back(&varying);
2506 }
2507 }
2508
Jamie Madill48ef11b2016-04-27 15:21:52 -04002509 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002510 {
2511 if (uniqueNames.count(varying.name) == 0)
2512 {
2513 uniqueNames.insert(varying.name);
2514 varyings.push_back(&varying);
2515 }
2516 }
2517
2518 return varyings;
2519}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002520
2521void Program::linkOutputVariables()
2522{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002523 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002524 ASSERT(fragmentShader != nullptr);
2525
2526 // Skip this step for GLES2 shaders.
2527 if (fragmentShader->getShaderVersion() == 100)
2528 return;
2529
Jamie Madilla0a9e122015-09-02 15:54:30 -04002530 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002531
2532 // TODO(jmadill): any caps validation here?
2533
2534 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2535 outputVariableIndex++)
2536 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002537 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002538
2539 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2540 if (outputVariable.isBuiltIn())
2541 continue;
2542
2543 // Since multiple output locations must be specified, use 0 for non-specified locations.
2544 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2545
2546 ASSERT(outputVariable.staticUse);
2547
2548 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2549 elementIndex++)
2550 {
2551 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002552 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002553 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002554 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002555 VariableLocation(outputVariable.name, element, outputVariableIndex);
2556 }
2557 }
2558}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002559
Martin Radev4c4c8e72016-08-04 12:25:34 +03002560bool Program::flattenUniformsAndCheckCapsForShader(const gl::Shader &shader,
2561 GLuint maxUniformComponents,
2562 GLuint maxTextureImageUnits,
2563 const std::string &componentsErrorMessage,
2564 const std::string &samplerErrorMessage,
2565 std::vector<LinkedUniform> &samplerUniforms,
2566 InfoLog &infoLog)
2567{
2568 VectorAndSamplerCount vasCount;
2569 for (const sh::Uniform &uniform : shader.getUniforms())
2570 {
2571 if (uniform.staticUse)
2572 {
2573 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2574 }
2575 }
2576
2577 if (vasCount.vectorCount > maxUniformComponents)
2578 {
2579 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2580 return false;
2581 }
2582
2583 if (vasCount.samplerCount > maxTextureImageUnits)
2584 {
2585 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2586 return false;
2587 }
2588
2589 return true;
2590}
2591
Jamie Madill62d31cb2015-09-11 13:25:51 -04002592bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2593{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002594 std::vector<LinkedUniform> samplerUniforms;
2595
Martin Radev4c4c8e72016-08-04 12:25:34 +03002596 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002597 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002598 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2599
2600 // TODO (mradev): check whether we need finer-grained component counting
2601 if (!flattenUniformsAndCheckCapsForShader(
2602 *computeShader, caps.maxComputeUniformComponents / 4,
2603 caps.maxComputeTextureImageUnits,
2604 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2605 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2606 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002607 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002608 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002609 }
2610 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002611 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002612 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002613 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002614
Martin Radev4c4c8e72016-08-04 12:25:34 +03002615 if (!flattenUniformsAndCheckCapsForShader(
2616 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2617 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2618 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2619 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002620 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002621 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002622 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002623 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002624
Martin Radev4c4c8e72016-08-04 12:25:34 +03002625 if (!flattenUniformsAndCheckCapsForShader(
2626 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2627 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2628 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2629 infoLog))
2630 {
2631 return false;
2632 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002633 }
2634
Jamie Madill48ef11b2016-04-27 15:21:52 -04002635 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002636 mSamplerUniformRange.end =
2637 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2638
Jamie Madill48ef11b2016-04-27 15:21:52 -04002639 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002640
Jamie Madill62d31cb2015-09-11 13:25:51 -04002641 return true;
2642}
2643
2644Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002645 const std::string &fullName,
2646 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002647{
2648 VectorAndSamplerCount vectorAndSamplerCount;
2649
2650 if (uniform.isStruct())
2651 {
2652 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2653 {
2654 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2655
2656 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2657 {
2658 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2659 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2660
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002661 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002662 }
2663 }
2664
2665 return vectorAndSamplerCount;
2666 }
2667
2668 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002669 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002670 if (!UniformInList(mState.getUniforms(), fullName) &&
2671 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002672 {
2673 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2674 uniform.arraySize, -1,
2675 sh::BlockMemberInfo::getDefaultBlockInfo());
2676 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002677
2678 // Store sampler uniforms separately, so we'll append them to the end of the list.
2679 if (isSampler)
2680 {
2681 samplerUniforms->push_back(linkedUniform);
2682 }
2683 else
2684 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002685 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002686 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002687 }
2688
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002689 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002690
2691 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2692 // Likewise, don't count "real" uniforms towards sampler count.
2693 vectorAndSamplerCount.vectorCount =
2694 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002695 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002696
2697 return vectorAndSamplerCount;
2698}
2699
2700void Program::gatherInterfaceBlockInfo()
2701{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002702 ASSERT(mState.mUniformBlocks.empty());
2703
2704 if (mState.mAttachedComputeShader)
2705 {
2706 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2707
2708 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2709 {
2710
2711 // Only 'packed' blocks are allowed to be considered inactive.
2712 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2713 continue;
2714
2715 for (gl::UniformBlock &block : mState.mUniformBlocks)
2716 {
2717 if (block.name == computeBlock.name)
2718 {
2719 block.computeStaticUse = computeBlock.staticUse;
2720 }
2721 }
2722
2723 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2724 }
2725 return;
2726 }
2727
Jamie Madill62d31cb2015-09-11 13:25:51 -04002728 std::set<std::string> visitedList;
2729
Jamie Madill48ef11b2016-04-27 15:21:52 -04002730 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002731
Jamie Madill62d31cb2015-09-11 13:25:51 -04002732 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2733 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002734 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002735 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2736 continue;
2737
2738 if (visitedList.count(vertexBlock.name) > 0)
2739 continue;
2740
2741 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2742 visitedList.insert(vertexBlock.name);
2743 }
2744
Jamie Madill48ef11b2016-04-27 15:21:52 -04002745 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002746
2747 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2748 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002749 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002750 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2751 continue;
2752
2753 if (visitedList.count(fragmentBlock.name) > 0)
2754 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002755 for (gl::UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002756 {
2757 if (block.name == fragmentBlock.name)
2758 {
2759 block.fragmentStaticUse = fragmentBlock.staticUse;
2760 }
2761 }
2762
2763 continue;
2764 }
2765
2766 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2767 visitedList.insert(fragmentBlock.name);
2768 }
2769}
2770
Jamie Madill4a3c2342015-10-08 12:58:45 -04002771template <typename VarT>
2772void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2773 const std::string &prefix,
2774 int blockIndex)
2775{
2776 for (const VarT &field : fields)
2777 {
2778 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2779
2780 if (field.isStruct())
2781 {
2782 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2783 {
2784 const std::string uniformElementName =
2785 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2786 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2787 }
2788 }
2789 else
2790 {
2791 // If getBlockMemberInfo returns false, the uniform is optimized out.
2792 sh::BlockMemberInfo memberInfo;
2793 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2794 {
2795 continue;
2796 }
2797
2798 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2799 blockIndex, memberInfo);
2800
2801 // Since block uniforms have no location, we don't need to store them in the uniform
2802 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002803 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002804 }
2805 }
2806}
2807
Jamie Madill62d31cb2015-09-11 13:25:51 -04002808void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2809{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002810 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002811 size_t blockSize = 0;
2812
2813 // Don't define this block at all if it's not active in the implementation.
2814 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2815 {
2816 return;
2817 }
2818
2819 // Track the first and last uniform index to determine the range of active uniforms in the
2820 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002821 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002822 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002823 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002824
2825 std::vector<unsigned int> blockUniformIndexes;
2826 for (size_t blockUniformIndex = firstBlockUniformIndex;
2827 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2828 {
2829 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2830 }
2831
2832 if (interfaceBlock.arraySize > 0)
2833 {
2834 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2835 {
2836 UniformBlock block(interfaceBlock.name, true, arrayElement);
2837 block.memberUniformIndexes = blockUniformIndexes;
2838
Martin Radev4c4c8e72016-08-04 12:25:34 +03002839 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002840 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002841 case GL_VERTEX_SHADER:
2842 {
2843 block.vertexStaticUse = interfaceBlock.staticUse;
2844 break;
2845 }
2846 case GL_FRAGMENT_SHADER:
2847 {
2848 block.fragmentStaticUse = interfaceBlock.staticUse;
2849 break;
2850 }
2851 case GL_COMPUTE_SHADER:
2852 {
2853 block.computeStaticUse = interfaceBlock.staticUse;
2854 break;
2855 }
2856 default:
2857 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002858 }
2859
Jamie Madill4a3c2342015-10-08 12:58:45 -04002860 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2861 size_t blockElementSize = 0;
2862 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2863 {
2864 continue;
2865 }
2866
2867 ASSERT(blockElementSize == blockSize);
2868 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002869 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002870 }
2871 }
2872 else
2873 {
2874 UniformBlock block(interfaceBlock.name, false, 0);
2875 block.memberUniformIndexes = blockUniformIndexes;
2876
Martin Radev4c4c8e72016-08-04 12:25:34 +03002877 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002878 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002879 case GL_VERTEX_SHADER:
2880 {
2881 block.vertexStaticUse = interfaceBlock.staticUse;
2882 break;
2883 }
2884 case GL_FRAGMENT_SHADER:
2885 {
2886 block.fragmentStaticUse = interfaceBlock.staticUse;
2887 break;
2888 }
2889 case GL_COMPUTE_SHADER:
2890 {
2891 block.computeStaticUse = interfaceBlock.staticUse;
2892 break;
2893 }
2894 default:
2895 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002896 }
2897
Jamie Madill4a3c2342015-10-08 12:58:45 -04002898 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002899 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002900 }
2901}
2902
2903template <typename T>
2904void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2905{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002906 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2907 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002908 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2909
2910 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2911 {
2912 // Do a cast conversion for boolean types. From the spec:
2913 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2914 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2915 for (GLsizei component = 0; component < count; ++component)
2916 {
2917 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2918 }
2919 }
2920 else
2921 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002922 // Invalide the validation cache if we modify the sampler data.
2923 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2924 {
2925 mCachedValidateSamplersResult.reset();
2926 }
2927
Jamie Madill62d31cb2015-09-11 13:25:51 -04002928 memcpy(destPointer, v, sizeof(T) * count);
2929 }
2930}
2931
2932template <size_t cols, size_t rows, typename T>
2933void Program::setMatrixUniformInternal(GLint location,
2934 GLsizei count,
2935 GLboolean transpose,
2936 const T *v)
2937{
2938 if (!transpose)
2939 {
2940 setUniformInternal(location, count * cols * rows, v);
2941 return;
2942 }
2943
2944 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002945 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2946 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002947 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2948 for (GLsizei element = 0; element < count; ++element)
2949 {
2950 size_t elementOffset = element * rows * cols;
2951
2952 for (size_t row = 0; row < rows; ++row)
2953 {
2954 for (size_t col = 0; col < cols; ++col)
2955 {
2956 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2957 }
2958 }
2959 }
2960}
2961
2962template <typename DestT>
2963void Program::getUniformInternal(GLint location, DestT *dataOut) const
2964{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002965 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2966 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002967
2968 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2969
2970 GLenum componentType = VariableComponentType(uniform.type);
2971 if (componentType == GLTypeToGLenum<DestT>::value)
2972 {
2973 memcpy(dataOut, srcPointer, uniform.getElementSize());
2974 return;
2975 }
2976
Corentin Wallez6596c462016-03-17 17:26:58 -04002977 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002978
2979 switch (componentType)
2980 {
2981 case GL_INT:
2982 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2983 break;
2984 case GL_UNSIGNED_INT:
2985 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2986 break;
2987 case GL_BOOL:
2988 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2989 break;
2990 case GL_FLOAT:
2991 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2992 break;
2993 default:
2994 UNREACHABLE();
2995 }
2996}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997}